]> git.tdb.fi Git - ext/subsurface.git/blob - info.c
Merge branches 'mingw-port-cleanup' and 'combobox-text-entry-fix' of git://github...
[ext/subsurface.git] / info.c
1 /* info.c */
2 /* creates the UI for the info frame - 
3  * controlled through the following interfaces:
4  * 
5  * void flush_dive_info_changes(struct dive *dive)
6  * void show_dive_info(struct dive *dive)
7  *
8  * called from gtk-ui:
9  * GtkWidget *extended_dive_info_widget(void)
10  */
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <time.h>
15
16 #include "dive.h"
17 #include "display.h"
18 #include "display-gtk.h"
19 #include "divelist.h"
20
21 static GtkComboBoxEntry *location, *buddy, *divemaster;
22 static GtkTextBuffer *notes;
23 static int location_changed = 1, notes_changed = 1;
24 static int divemaster_changed = 1, buddy_changed = 1;
25
26 static char *get_text(GtkTextBuffer *buffer)
27 {
28         GtkTextIter start;
29         GtkTextIter end;
30
31         gtk_text_buffer_get_start_iter(buffer, &start);
32         gtk_text_buffer_get_end_iter(buffer, &end);
33         return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
34 }
35
36 /* old is NULL or a valid string, new is a valid string
37  * NOTW: NULL and "" need to be treated as "unchanged" */
38 static int text_changed(char *old, char *new)
39 {
40         return ((old && strcmp(old,new)) ||
41                 (!old && strcmp("",new)));
42 }
43
44 void flush_dive_info_changes(struct dive *dive)
45 {
46         char *old_text;
47         int changed = 0;
48
49         if (!dive)
50                 return;
51
52         if (location_changed) {
53                 char *new_text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(location));
54                 old_text = dive->location;
55                 dive->location = new_text;
56                 add_location(new_text);
57                 if (text_changed(old_text,dive->location))
58                         changed = 1;
59                 if (old_text)
60                         g_free(old_text);
61         }
62
63         if (divemaster_changed) {
64                 char *new_text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(divemaster));
65                 old_text = dive->divemaster;
66                 dive->divemaster = new_text;
67                 add_people(new_text);
68                 if (text_changed(old_text,dive->divemaster))
69                         changed = 1;
70                 if (old_text)
71                         g_free(old_text);
72         }
73
74         if (buddy_changed) {
75                 char *new_text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(buddy));
76                 old_text = dive->buddy;
77                 dive->buddy = new_text;
78                 add_people(new_text);
79                 if (text_changed(old_text,dive->buddy))
80                         changed = 1;
81                 if (old_text)
82                         g_free(old_text);
83         }
84
85         if (notes_changed) {
86                 old_text = dive->notes;
87                 dive->notes = get_text(notes);
88                 if (text_changed(old_text,dive->notes))
89                         changed = 1;
90                 if (old_text)
91                         g_free(old_text);
92         }
93         if (changed)
94                 mark_divelist_changed(TRUE);
95 }
96
97 static void set_combo_box_entry_text(GtkComboBoxEntry *combo_box, const char *text)
98 {
99         GtkEntry *entry = GTK_ENTRY(GTK_BIN(combo_box)->child);
100         gtk_entry_set_text(entry, text);
101 }
102
103 #define SET_TEXT_ENTRY(x) \
104         set_combo_box_entry_text(x, dive && dive->x ? dive->x : "")
105
106 void show_dive_info(struct dive *dive)
107 {
108         struct tm *tm;
109         const char *text;
110         char buffer[80];
111
112         /* dive number and location (or lacking that, the date) go in the window title */
113         tm = gmtime(&dive->when);
114         text = dive->location;
115         if (!text)
116                 text = "";
117         if (*text) {
118                 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
119         } else {
120                 snprintf(buffer, sizeof(buffer), "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
121                         dive->number,
122                         weekday(tm->tm_wday),
123                         tm->tm_mon+1, tm->tm_mday,
124                         tm->tm_year+1900,
125                         tm->tm_hour, tm->tm_min);
126         }
127         text = buffer;
128         if (!dive->number)
129                 text += 10;     /* Skip the "Dive #0 - " part */
130         gtk_window_set_title(GTK_WINDOW(main_window), text);
131
132         SET_TEXT_ENTRY(divemaster);
133         SET_TEXT_ENTRY(buddy);
134         SET_TEXT_ENTRY(location);
135         gtk_text_buffer_set_text(notes, dive && dive->notes ? dive->notes : "", -1);
136 }
137
138 static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions)
139 {
140         GtkEntry *entry;
141         GtkWidget *combo_box;
142         GtkWidget *frame = gtk_frame_new(label);
143         GtkEntryCompletion *completion;
144
145         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
146
147         combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
148         gtk_container_add(GTK_CONTAINER(frame), combo_box);
149
150         entry = GTK_ENTRY(GTK_BIN(combo_box)->child);
151
152         completion = gtk_entry_completion_new();
153         gtk_entry_completion_set_text_column(completion, 0);
154         gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
155         gtk_entry_completion_set_inline_completion(completion, TRUE);
156         gtk_entry_completion_set_inline_selection(completion, TRUE);
157         gtk_entry_completion_set_popup_single_match(completion, FALSE);
158         gtk_entry_set_completion(entry, completion);
159
160         return GTK_COMBO_BOX_ENTRY(combo_box);
161 }
162
163 static GtkTextBuffer *text_view(GtkWidget *box, const char *label)
164 {
165         GtkWidget *view, *vbox;
166         GtkTextBuffer *buffer;
167         GtkWidget *frame = gtk_frame_new(label);
168
169         gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
170         box = gtk_hbox_new(FALSE, 3);
171         gtk_container_add(GTK_CONTAINER(frame), box);
172         vbox = gtk_vbox_new(FALSE, 3);
173         gtk_container_add(GTK_CONTAINER(box), vbox);
174
175         GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
176         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
177         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
178
179         view = gtk_text_view_new();
180         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
181         gtk_container_add(GTK_CONTAINER(scrolled_window), view);
182
183         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
184
185         gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
186         return buffer;
187 }
188
189 static enum {
190         MATCH_EXACT,
191         MATCH_PREPEND,
192         MATCH_AFTER
193 } found_string_entry;
194 static GtkTreeIter string_entry_location;
195
196 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
197 {
198         const char *string = data;
199         char *entry;
200         int cmp;
201
202         gtk_tree_model_get(model, iter, 0, &entry, -1);
203         cmp = strcmp(entry, string);
204
205         /* Stop. The entry is bigger than the new one */
206         if (cmp > 0)
207                 return TRUE;
208
209         /* Exact match */
210         if (!cmp) {
211                 found_string_entry = MATCH_EXACT;
212                 return TRUE;
213         }
214
215         string_entry_location = *iter;
216         found_string_entry = MATCH_AFTER;
217         return FALSE;
218 }
219
220 static int match_list(GtkListStore *list, const char *string)
221 {
222         found_string_entry = MATCH_PREPEND;
223         gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
224         return found_string_entry;
225 }
226
227 static GtkListStore *location_list, *people_list;
228
229 static void add_string_list_entry(const char *string, GtkListStore *list)
230 {
231         GtkTreeIter *iter, loc;
232
233         if (!string || !*string)
234                 return;
235
236         switch (match_list(list, string)) {
237         case MATCH_EXACT:
238                 return;
239         case MATCH_PREPEND:
240                 iter = NULL;
241                 break;
242         case MATCH_AFTER:
243                 iter = &string_entry_location;
244                 break;
245         }
246         gtk_list_store_insert_after(list, &loc, iter);
247         gtk_list_store_set(list, &loc, 0, string, -1);
248 }
249
250 void add_people(const char *string)
251 {
252         add_string_list_entry(string, people_list);
253 }
254
255 void add_location(const char *string)
256 {
257         add_string_list_entry(string, location_list);
258 }
259
260 GtkWidget *extended_dive_info_widget(void)
261 {
262         GtkWidget *vbox, *hbox;
263         vbox = gtk_vbox_new(FALSE, 6);
264
265         people_list = gtk_list_store_new(1, G_TYPE_STRING);
266         location_list = gtk_list_store_new(1, G_TYPE_STRING);
267
268         gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
269         location = text_entry(vbox, "Location", location_list);
270
271         hbox = gtk_hbox_new(FALSE, 3);
272         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
273
274         divemaster = text_entry(hbox, "Divemaster", people_list);
275         buddy = text_entry(hbox, "Buddy", people_list);
276
277         notes = text_view(vbox, "Notes");
278         return vbox;
279 }