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