]> git.tdb.fi Git - ext/subsurface.git/blob - info.c
Make double-clicking on the dive list bring up the dive editor
[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;
22 static GtkTextView *notes;
23 static GtkListStore *location_list, *people_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 void show_dive_info(struct dive *dive)
66 {
67         struct tm *tm;
68         const char *text;
69         char buffer[80];
70
71         /* dive number and location (or lacking that, the date) go in the window title */
72         tm = gmtime(&dive->when);
73         text = dive->location;
74         if (!text)
75                 text = "";
76         if (*text) {
77                 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
78         } else {
79                 snprintf(buffer, sizeof(buffer), "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
80                         dive->number,
81                         weekday(tm->tm_wday),
82                         tm->tm_mon+1, tm->tm_mday,
83                         tm->tm_year+1900,
84                         tm->tm_hour, tm->tm_min);
85         }
86         text = buffer;
87         if (!dive->number)
88                 text += 10;     /* Skip the "Dive #0 - " part */
89         gtk_window_set_title(GTK_WINDOW(main_window), text);
90
91         SET_TEXT_VALUE(divemaster);
92         SET_TEXT_VALUE(buddy);
93         SET_TEXT_VALUE(location);
94         gtk_text_buffer_set_text(gtk_text_view_get_buffer(notes),
95                 dive && dive->notes ? dive->notes : "", -1);
96 }
97
98 static GtkEntry *text_value(GtkWidget *box, const char *label)
99 {
100         GtkWidget *widget;
101         GtkWidget *frame = gtk_frame_new(label);
102
103         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
104         widget = gtk_entry_new();
105         gtk_widget_set_can_focus(widget, FALSE);
106         gtk_editable_set_editable(GTK_EDITABLE(widget), FALSE);
107         gtk_container_add(GTK_CONTAINER(frame), widget);
108         return GTK_ENTRY(widget);
109 }
110
111 static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions, const char *text)
112 {
113         GtkEntry *entry;
114         GtkWidget *combo_box;
115         GtkWidget *frame = gtk_frame_new(label);
116         GtkEntryCompletion *completion;
117
118         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
119
120         combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
121         gtk_container_add(GTK_CONTAINER(frame), combo_box);
122
123         entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
124         if (text && *text)
125                 gtk_entry_set_text(entry, text);
126
127         completion = gtk_entry_completion_new();
128         gtk_entry_completion_set_text_column(completion, 0);
129         gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
130         gtk_entry_completion_set_inline_completion(completion, TRUE);
131         gtk_entry_completion_set_inline_selection(completion, TRUE);
132         gtk_entry_completion_set_popup_single_match(completion, FALSE);
133         gtk_entry_set_completion(entry, completion);
134
135         return GTK_COMBO_BOX_ENTRY(combo_box);
136 }
137
138 enum writable {
139         READ_ONLY,
140         READ_WRITE
141 };
142
143 static GtkTextView *text_view(GtkWidget *box, const char *label, enum writable writable)
144 {
145         GtkWidget *view, *vbox;
146         GtkWidget *frame = gtk_frame_new(label);
147
148         gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
149         box = gtk_hbox_new(FALSE, 3);
150         gtk_container_add(GTK_CONTAINER(frame), box);
151         vbox = gtk_vbox_new(FALSE, 3);
152         gtk_container_add(GTK_CONTAINER(box), vbox);
153
154         GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
155         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
156         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
157
158         view = gtk_text_view_new();
159         if (writable == READ_ONLY) {
160                 gtk_widget_set_can_focus(view, FALSE);
161                 gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
162                 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
163         }
164         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
165         gtk_container_add(GTK_CONTAINER(scrolled_window), view);
166         gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
167         return GTK_TEXT_VIEW(view);
168 }
169
170 static enum {
171         MATCH_EXACT,
172         MATCH_PREPEND,
173         MATCH_AFTER
174 } found_string_entry;
175 static GtkTreeIter string_entry_location;
176
177 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
178 {
179         const char *string = data;
180         char *entry;
181         int cmp;
182
183         gtk_tree_model_get(model, iter, 0, &entry, -1);
184         cmp = strcmp(entry, string);
185
186         /* Stop. The entry is bigger than the new one */
187         if (cmp > 0)
188                 return TRUE;
189
190         /* Exact match */
191         if (!cmp) {
192                 found_string_entry = MATCH_EXACT;
193                 return TRUE;
194         }
195
196         string_entry_location = *iter;
197         found_string_entry = MATCH_AFTER;
198         return FALSE;
199 }
200
201 static int match_list(GtkListStore *list, const char *string)
202 {
203         found_string_entry = MATCH_PREPEND;
204         gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
205         return found_string_entry;
206 }
207
208 static void add_string_list_entry(const char *string, GtkListStore *list)
209 {
210         GtkTreeIter *iter, loc;
211
212         if (!string || !*string)
213                 return;
214
215         switch (match_list(list, string)) {
216         case MATCH_EXACT:
217                 return;
218         case MATCH_PREPEND:
219                 iter = NULL;
220                 break;
221         case MATCH_AFTER:
222                 iter = &string_entry_location;
223                 break;
224         }
225         gtk_list_store_insert_after(list, &loc, iter);
226         gtk_list_store_set(list, &loc, 0, string, -1);
227 }
228
229 void add_people(const char *string)
230 {
231         add_string_list_entry(string, people_list);
232 }
233
234 void add_location(const char *string)
235 {
236         add_string_list_entry(string, location_list);
237 }
238
239 struct dive_info {
240         GtkComboBoxEntry *location, *divemaster, *buddy;
241         GtkTextView *notes;
242 };
243
244 static void save_dive_info_changes(struct dive *dive, struct dive_info *info)
245 {
246         char *old_text, *new_text;
247         int changed = 0;
248
249         new_text = get_combo_box_entry_text(info->location, &dive->location);
250         if (new_text) {
251                 add_location(new_text);
252                 changed = 1;
253         }
254
255         new_text = get_combo_box_entry_text(info->divemaster, &dive->divemaster);
256         if (new_text) {
257                 add_people(new_text);
258                 changed = 1;
259         }
260
261         new_text = get_combo_box_entry_text(info->buddy, &dive->buddy);
262         if (new_text) {
263                 add_people(new_text);
264                 changed = 1;
265         }
266
267         old_text = dive->notes;
268         dive->notes = get_text(info->notes);
269         if (text_changed(old_text,dive->notes))
270                 changed = 1;
271         if (old_text)
272                 g_free(old_text);
273
274         if (changed) {
275                 mark_divelist_changed(TRUE);
276                 flush_divelist(dive);
277         }
278 }
279
280 static void dive_info_widget(GtkWidget *box, struct dive *dive, struct dive_info *info)
281 {
282         GtkWidget *hbox;
283
284         info->location = text_entry(box, "Location", location_list, dive->location);
285
286         hbox = gtk_hbox_new(FALSE, 3);
287         gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0);
288
289         info->divemaster = text_entry(hbox, "Dive master", people_list, dive->divemaster);
290         info->buddy = text_entry(hbox, "Buddy", people_list, dive->buddy);
291
292         info->notes = text_view(box, "Notes", READ_WRITE);
293         if (dive->notes && *dive->notes)
294                 gtk_text_buffer_set_text(gtk_text_view_get_buffer(info->notes), dive->notes, -1);
295 }
296
297 int edit_dive_info(struct dive *dive)
298 {
299         int success;
300         GtkWidget *dialog, *vbox;
301         struct dive_info info;
302
303         if (!dive)
304                 return 0;
305
306         dialog = gtk_dialog_new_with_buttons("Dive Info",
307                 GTK_WINDOW(main_window),
308                 GTK_DIALOG_DESTROY_WITH_PARENT,
309                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
310                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
311                 NULL);
312
313         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
314         dive_info_widget(vbox, dive, &info);
315
316         gtk_widget_show_all(dialog);
317         success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
318         if (success)
319                 save_dive_info_changes(dive, &info);
320
321         gtk_widget_destroy(dialog);
322
323         return success;
324 }
325
326 GtkWidget *extended_dive_info_widget(void)
327 {
328         GtkWidget *vbox, *hbox;
329         vbox = gtk_vbox_new(FALSE, 6);
330
331         people_list = gtk_list_store_new(1, G_TYPE_STRING);
332         location_list = gtk_list_store_new(1, G_TYPE_STRING);
333
334         gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
335         location = text_value(vbox, "Location");
336
337         hbox = gtk_hbox_new(FALSE, 3);
338         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
339
340         divemaster = text_value(hbox, "Divemaster");
341         buddy = text_value(hbox, "Buddy");
342
343         notes = text_view(vbox, "Notes", READ_ONLY);
344         return vbox;
345 }