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