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