]> git.tdb.fi Git - ext/subsurface.git/blob - info.c
Merge branch 'forlinus' of git://git.hohndel.org/subsurface
[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 show_dive_info(struct dive *dive)
6  *
7  * called from gtk-ui:
8  * GtkWidget *extended_dive_info_widget(void)
9  */
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <time.h>
14 #include <ctype.h>
15
16 #include "dive.h"
17 #include "display.h"
18 #include "display-gtk.h"
19 #include "divelist.h"
20
21 static GtkEntry *location, *buddy, *divemaster, *rating;
22 static GtkTextView *notes;
23 static GtkListStore *location_list, *people_list, *star_list;
24
25 static char *get_text(GtkTextView *view)
26 {
27         GtkTextBuffer *buffer;
28         GtkTextIter start;
29         GtkTextIter end;
30
31         buffer = gtk_text_view_get_buffer(view);
32         gtk_text_buffer_get_start_iter(buffer, &start);
33         gtk_text_buffer_get_end_iter(buffer, &end);
34         return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
35 }
36
37 /* old is NULL or a valid string, new is a valid string
38  * NOTW: NULL and "" need to be treated as "unchanged" */
39 static int text_changed(const char *old, const char *new)
40 {
41         return ((old && strcmp(old,new)) ||
42                 (!old && strcmp("",new)));
43 }
44
45 static char *get_combo_box_entry_text(GtkComboBoxEntry *combo_box, char **textp)
46 {
47         char *old = *textp;
48         const gchar *new;
49         GtkEntry *entry;
50
51         entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
52         new = gtk_entry_get_text(entry);
53         while (isspace(*new))
54                 new++;
55         if (!text_changed(old,new))
56                 return NULL;
57         free(old);
58         *textp = strdup(new);
59         return *textp;
60 }
61
62 #define SET_TEXT_VALUE(x) \
63         gtk_entry_set_text(x, dive && dive->x ? dive->x : "")
64
65 static int divename(char *buf, size_t size, struct dive *dive)
66 {
67         struct tm *tm = gmtime(&dive->when);
68         return snprintf(buf, size, "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
69                 dive->number,
70                 weekday(tm->tm_wday),
71                 tm->tm_mon+1, tm->tm_mday,
72                 tm->tm_year+1900,
73                 tm->tm_hour, tm->tm_min);
74 }
75
76 void show_dive_info(struct dive *dive)
77 {
78         const char *text;
79         char buffer[80];
80
81         /* dive number and location (or lacking that, the date) go in the window title */
82         text = dive->location;
83         if (!text)
84                 text = "";
85         if (*text) {
86                 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
87         } else {
88                 divename(buffer, sizeof(buffer), dive);
89         }
90         text = buffer;
91         if (!dive->number)
92                 text += 10;     /* Skip the "Dive #0 - " part */
93         gtk_window_set_title(GTK_WINDOW(main_window), text);
94
95         SET_TEXT_VALUE(divemaster);
96         SET_TEXT_VALUE(buddy);
97         SET_TEXT_VALUE(location);
98         gtk_entry_set_text(rating, star_strings[dive->rating]);
99         gtk_text_buffer_set_text(gtk_text_view_get_buffer(notes),
100                 dive && dive->notes ? dive->notes : "", -1);
101 }
102
103 static void info_menu_edit_cb(GtkMenuItem *menuitem, gpointer user_data)
104 {
105         edit_dive_info(current_dive);
106 }
107
108 static void populate_popup_cb(GtkTextView *entry, GtkMenu *menu, gpointer user_data)
109 {
110         GtkWidget *item = gtk_menu_item_new_with_label("Edit");
111         g_signal_connect(item, "activate", G_CALLBACK(info_menu_edit_cb), NULL);
112         gtk_widget_show(item); /* Yes, really */
113         gtk_menu_prepend(menu, item);
114 }
115
116 static GtkEntry *text_value(GtkWidget *box, const char *label)
117 {
118         GtkWidget *widget;
119         GtkWidget *frame = gtk_frame_new(label);
120
121         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
122         widget = gtk_entry_new();
123         gtk_widget_set_can_focus(widget, FALSE);
124         gtk_editable_set_editable(GTK_EDITABLE(widget), FALSE);
125         gtk_container_add(GTK_CONTAINER(frame), widget);
126         g_signal_connect(widget, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
127         return GTK_ENTRY(widget);
128 }
129
130 static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions, const char *text)
131 {
132         GtkEntry *entry;
133         GtkWidget *combo_box;
134         GtkWidget *frame = gtk_frame_new(label);
135         GtkEntryCompletion *completion;
136
137         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
138
139         combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
140         gtk_container_add(GTK_CONTAINER(frame), combo_box);
141
142         entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
143         if (text && *text)
144                 gtk_entry_set_text(entry, text);
145
146         completion = gtk_entry_completion_new();
147         gtk_entry_completion_set_text_column(completion, 0);
148         gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
149         gtk_entry_completion_set_inline_completion(completion, TRUE);
150         gtk_entry_completion_set_inline_selection(completion, TRUE);
151         gtk_entry_completion_set_popup_single_match(completion, FALSE);
152         gtk_entry_set_completion(entry, completion);
153
154         return GTK_COMBO_BOX_ENTRY(combo_box);
155 }
156
157 enum writable {
158         READ_ONLY,
159         READ_WRITE
160 };
161
162 static GtkTextView *text_view(GtkWidget *box, const char *label, enum writable writable)
163 {
164         GtkWidget *view, *vbox;
165         GtkWidget *frame = gtk_frame_new(label);
166
167         gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
168         box = gtk_hbox_new(FALSE, 3);
169         gtk_container_add(GTK_CONTAINER(frame), box);
170         vbox = gtk_vbox_new(FALSE, 3);
171         gtk_container_add(GTK_CONTAINER(box), vbox);
172
173         GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
174         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
175         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
176
177         view = gtk_text_view_new();
178         if (writable == READ_ONLY) {
179                 gtk_widget_set_can_focus(view, FALSE);
180                 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
181                 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
182                 g_signal_connect(view, "populate-popup", G_CALLBACK(populate_popup_cb), NULL);
183         }
184         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
185         gtk_container_add(GTK_CONTAINER(scrolled_window), view);
186         gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
187         return GTK_TEXT_VIEW(view);
188 }
189
190 static enum {
191         MATCH_EXACT,
192         MATCH_PREPEND,
193         MATCH_AFTER
194 } found_string_entry;
195 static GtkTreeIter string_entry_location;
196
197 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
198 {
199         const char *string = data;
200         char *entry;
201         int cmp;
202
203         gtk_tree_model_get(model, iter, 0, &entry, -1);
204         cmp = strcmp(entry, string);
205
206         /* Stop. The entry is bigger than the new one */
207         if (cmp > 0)
208                 return TRUE;
209
210         /* Exact match */
211         if (!cmp) {
212                 found_string_entry = MATCH_EXACT;
213                 return TRUE;
214         }
215
216         string_entry_location = *iter;
217         found_string_entry = MATCH_AFTER;
218         return FALSE;
219 }
220
221 static int match_list(GtkListStore *list, const char *string)
222 {
223         found_string_entry = MATCH_PREPEND;
224         gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
225         return found_string_entry;
226 }
227
228 static void add_string_list_entry(const char *string, GtkListStore *list)
229 {
230         GtkTreeIter *iter, loc;
231
232         if (!string || !*string)
233                 return;
234
235         switch (match_list(list, string)) {
236         case MATCH_EXACT:
237                 return;
238         case MATCH_PREPEND:
239                 iter = NULL;
240                 break;
241         case MATCH_AFTER:
242                 iter = &string_entry_location;
243                 break;
244         }
245         gtk_list_store_insert_after(list, &loc, iter);
246         gtk_list_store_set(list, &loc, 0, string, -1);
247 }
248
249 void add_people(const char *string)
250 {
251         add_string_list_entry(string, people_list);
252 }
253
254 void add_location(const char *string)
255 {
256         add_string_list_entry(string, location_list);
257 }
258
259 static int get_rating(const char *string)
260 {
261         int rating = 0;
262         int i;
263
264         for (i = 0; i <= 5; i++)
265                 if (!strcmp(star_strings[i],string))
266                         rating = i;
267         return rating;
268 }
269
270 struct dive_info {
271         GtkComboBoxEntry *location, *divemaster, *buddy, *rating;
272         GtkTextView *notes;
273 };
274
275 static void save_dive_info_changes(struct dive *dive, struct dive_info *info)
276 {
277         char *old_text, *new_text;
278         char *rating_string;
279         int changed = 0;
280
281         new_text = get_combo_box_entry_text(info->location, &dive->location);
282         if (new_text) {
283                 add_location(new_text);
284                 changed = 1;
285         }
286
287         new_text = get_combo_box_entry_text(info->divemaster, &dive->divemaster);
288         if (new_text) {
289                 add_people(new_text);
290                 changed = 1;
291         }
292
293         new_text = get_combo_box_entry_text(info->buddy, &dive->buddy);
294         if (new_text) {
295                 add_people(new_text);
296                 changed = 1;
297         }
298
299         rating_string = strdup(star_strings[dive->rating]);
300         new_text = get_combo_box_entry_text(info->rating, &rating_string);
301         if (new_text) {
302                 dive->rating = get_rating(rating_string);
303                 free(rating_string);
304                 changed =1;
305         }
306
307         old_text = dive->notes;
308         dive->notes = get_text(info->notes);
309         if (text_changed(old_text,dive->notes))
310                 changed = 1;
311         if (old_text)
312                 g_free(old_text);
313
314         if (changed) {
315                 mark_divelist_changed(TRUE);
316                 flush_divelist(dive);
317         }
318 }
319
320 static void dive_info_widget(GtkWidget *box, struct dive *dive, struct dive_info *info)
321 {
322         GtkWidget *hbox, *label, *cylinder, *frame;
323         char buffer[80];
324
325         divename(buffer, sizeof(buffer), dive);
326         label = gtk_label_new(buffer);
327         gtk_box_pack_start(GTK_BOX(box), label, FALSE, TRUE, 0);
328
329         info->location = text_entry(box, "Location", location_list, dive->location);
330
331         hbox = gtk_hbox_new(FALSE, 3);
332         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
333
334         info->divemaster = text_entry(hbox, "Dive master", people_list, dive->divemaster);
335         info->buddy = text_entry(hbox, "Buddy", people_list, dive->buddy);
336
337         hbox = gtk_hbox_new(FALSE, 3);
338         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
339
340         info->rating = text_entry(hbox, "Rating", star_list, star_strings[dive->rating]);
341
342         info->notes = text_view(box, "Notes", READ_WRITE);
343         if (dive->notes && *dive->notes)
344                 gtk_text_buffer_set_text(gtk_text_view_get_buffer(info->notes), dive->notes, -1);
345
346         hbox = gtk_hbox_new(FALSE, 3);
347         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
348
349         frame = gtk_frame_new("Cylinder");
350         cylinder = cylinder_list_widget();
351         gtk_container_add(GTK_CONTAINER(frame), cylinder);
352         gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
353 }
354
355 int edit_dive_info(struct dive *dive)
356 {
357         int success;
358         GtkWidget *dialog, *vbox;
359         struct dive_info info;
360
361         if (!dive)
362                 return 0;
363
364         dialog = gtk_dialog_new_with_buttons("Dive Info",
365                 GTK_WINDOW(main_window),
366                 GTK_DIALOG_DESTROY_WITH_PARENT,
367                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
368                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
369                 NULL);
370
371         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
372         dive_info_widget(vbox, dive, &info);
373
374         gtk_widget_show_all(dialog);
375         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
376         if (success)
377                 save_dive_info_changes(dive, &info);
378
379         gtk_widget_destroy(dialog);
380
381         return success;
382 }
383
384 GtkWidget *extended_dive_info_widget(void)
385 {
386         GtkWidget *vbox, *hbox;
387         vbox = gtk_vbox_new(FALSE, 6);
388
389         people_list = gtk_list_store_new(1, G_TYPE_STRING);
390         location_list = gtk_list_store_new(1, G_TYPE_STRING);
391         star_list = gtk_list_store_new(1, G_TYPE_STRING);
392         add_string_list_entry(ZERO_STARS, star_list);
393         add_string_list_entry(ONE_STARS, star_list);
394         add_string_list_entry(TWO_STARS, star_list);
395         add_string_list_entry(THREE_STARS, star_list);
396         add_string_list_entry(FOUR_STARS, star_list);
397         add_string_list_entry(FIVE_STARS, star_list);
398
399         gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
400         location = text_value(vbox, "Location");
401
402         hbox = gtk_hbox_new(FALSE, 3);
403         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
404
405         divemaster = text_value(hbox, "Divemaster");
406         buddy = text_value(hbox, "Buddy");
407
408         hbox = gtk_hbox_new(FALSE, 3);
409         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
410
411         rating = text_value(hbox, "Rating");
412
413         notes = text_view(vbox, "Notes", READ_ONLY);
414         return vbox;
415 }