]> git.tdb.fi Git - ext/subsurface.git/blob - info.c
Add completions to the dive location, buddy and divemaster entries
[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 GtkEntry *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                 old_text = dive->location;
54                 dive->location = gtk_editable_get_chars(GTK_EDITABLE(location), 0, -1);
55                 if (text_changed(old_text,dive->location))
56                         changed = 1;
57                 if (old_text)
58                         g_free(old_text);
59         }
60
61         if (divemaster_changed) {
62                 old_text = dive->divemaster;
63                 dive->divemaster = gtk_editable_get_chars(GTK_EDITABLE(divemaster), 0, -1);
64                 if (text_changed(old_text,dive->divemaster))
65                         changed = 1;
66                 if (old_text)
67                         g_free(old_text);
68         }
69
70         if (buddy_changed) {
71                 old_text = dive->buddy;
72                 dive->buddy = gtk_editable_get_chars(GTK_EDITABLE(buddy), 0, -1);
73                 if (text_changed(old_text,dive->buddy))
74                         changed = 1;
75                 if (old_text)
76                         g_free(old_text);
77         }
78
79         if (notes_changed) {
80                 old_text = dive->notes;
81                 dive->notes = get_text(notes);
82                 if (text_changed(old_text,dive->notes))
83                         changed = 1;
84                 if (old_text)
85                         g_free(old_text);
86         }
87         if (changed)
88                 mark_divelist_changed(TRUE);
89 }
90
91 #define SET_TEXT_ENTRY(x) \
92         gtk_entry_set_text(x, dive && dive->x ? dive->x : "")
93
94 void show_dive_info(struct dive *dive)
95 {
96         struct tm *tm;
97         const char *text;
98         char buffer[80];
99
100         /* dive number and location (or lacking that, the date) go in the window title */
101         tm = gmtime(&dive->when);
102         text = dive->location;
103         if (!text)
104                 text = "";
105         if (*text) {
106                 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
107         } else {
108                 snprintf(buffer, sizeof(buffer), "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
109                         dive->number,
110                         weekday(tm->tm_wday),
111                         tm->tm_mon+1, tm->tm_mday,
112                         tm->tm_year+1900,
113                         tm->tm_hour, tm->tm_min);
114         }
115         text = buffer;
116         if (!dive->number)
117                 text += 10;     /* Skip the "Dive #0 - " part */
118         gtk_window_set_title(GTK_WINDOW(main_window), text);
119
120         SET_TEXT_ENTRY(divemaster);
121         SET_TEXT_ENTRY(buddy);
122         SET_TEXT_ENTRY(location);
123         gtk_text_buffer_set_text(notes, dive && dive->notes ? dive->notes : "", -1);
124 }
125
126 static GtkEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions)
127 {
128         GtkWidget *entry;
129         GtkWidget *frame = gtk_frame_new(label);
130
131         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
132
133         entry = gtk_entry_new();
134         gtk_container_add(GTK_CONTAINER(frame), entry);
135
136         if (completions) {
137                 GtkEntryCompletion *completion;
138                 completion = gtk_entry_completion_new();
139                 gtk_entry_completion_set_text_column(completion, 0);
140                 gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
141                 gtk_entry_set_completion(GTK_ENTRY(entry), completion);
142         }
143
144         return GTK_ENTRY(entry);
145 }
146
147 static GtkTextBuffer *text_view(GtkWidget *box, const char *label)
148 {
149         GtkWidget *view, *vbox;
150         GtkTextBuffer *buffer;
151         GtkWidget *frame = gtk_frame_new(label);
152
153         gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
154         box = gtk_hbox_new(FALSE, 3);
155         gtk_container_add(GTK_CONTAINER(frame), box);
156         vbox = gtk_vbox_new(FALSE, 3);
157         gtk_container_add(GTK_CONTAINER(box), vbox);
158
159         GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
160         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
161         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
162
163         view = gtk_text_view_new();
164         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
165         gtk_container_add(GTK_CONTAINER(scrolled_window), view);
166
167         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
168
169         gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
170         return buffer;
171 }
172
173 static int found_string_entry;
174
175 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
176 {
177         const char *string = data;
178         char *entry;
179
180         gtk_tree_model_get(model, iter, 0, &entry, -1);
181         if (strcmp(entry, string))
182                 return FALSE;
183         found_string_entry = 1;
184         return TRUE;
185 }
186
187 static int match_list(GtkListStore *list, const char *string)
188 {
189         found_string_entry = 0;
190         gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
191         return found_string_entry;
192 }
193
194 static GtkListStore *location_list, *people_list;
195
196 static void add_string_list_entry(const char *string, GtkListStore *list)
197 {
198         GtkTreeIter iter;
199
200         if (!string || !*string)
201                 return;
202
203         if (match_list(list, string))
204                 return;
205
206         /* Fixme! Check for duplicates! */
207         gtk_list_store_append(list, &iter);
208         gtk_list_store_set(list, &iter, 0, string, -1);
209 }
210
211 void add_people(const char *string)
212 {
213         add_string_list_entry(string, people_list);
214 }
215
216 void add_location(const char *string)
217 {
218         add_string_list_entry(string, location_list);
219 }
220
221 GtkWidget *extended_dive_info_widget(void)
222 {
223         GtkWidget *vbox, *hbox;
224         vbox = gtk_vbox_new(FALSE, 6);
225
226         people_list = gtk_list_store_new(1, G_TYPE_STRING);
227         location_list = gtk_list_store_new(1, G_TYPE_STRING);
228
229         gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
230         location = text_entry(vbox, "Location", location_list);
231
232         hbox = gtk_hbox_new(FALSE, 3);
233         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
234
235         divemaster = text_entry(hbox, "Divemaster", people_list);
236         buddy = text_entry(hbox, "Buddy", people_list);
237
238         notes = text_view(vbox, "Notes");
239         return vbox;
240 }