2 /* creates the UI for the info frame -
3 * controlled through the following interfaces:
5 * void flush_dive_info_changes(struct dive *dive)
6 * void show_dive_info(struct dive *dive)
9 * GtkWidget *extended_dive_info_widget(void)
19 #include "display-gtk.h"
22 static GtkEntry *location, *buddy, *divemaster;
23 static GtkTextBuffer *notes;
27 unused char *get_text(GtkTextBuffer *buffer)
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);
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)
41 return ((old && strcmp(old,new)) ||
42 (!old && strcmp("",new)));
45 unused char *get_combo_box_entry_text(GtkComboBoxEntry *combo_box, char **textp)
51 entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box)));
52 new = gtk_entry_get_text(entry);
55 if (!text_changed(old,new))
63 void flush_dive_info_changes(struct dive *dive)
66 char *old_text, *new_text;
72 new_text = get_combo_box_entry_text(location, &dive->location);
74 add_location(new_text);
78 new_text = get_combo_box_entry_text(divemaster, &dive->divemaster);
84 new_text = get_combo_box_entry_text(buddy, &dive->buddy);
90 old_text = dive->notes;
91 dive->notes = get_text(notes);
92 if (text_changed(old_text,dive->notes))
98 mark_divelist_changed(TRUE);
102 #define SET_TEXT_VALUE(x) \
103 gtk_entry_set_text(x, dive && dive->x ? dive->x : "")
105 void show_dive_info(struct dive *dive)
111 /* dive number and location (or lacking that, the date) go in the window title */
112 tm = gmtime(&dive->when);
113 text = dive->location;
117 snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
119 snprintf(buffer, sizeof(buffer), "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
121 weekday(tm->tm_wday),
122 tm->tm_mon+1, tm->tm_mday,
124 tm->tm_hour, tm->tm_min);
128 text += 10; /* Skip the "Dive #0 - " part */
129 gtk_window_set_title(GTK_WINDOW(main_window), text);
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);
137 static GtkEntry *text_value(GtkWidget *box, const char *label)
140 GtkWidget *frame = gtk_frame_new(label);
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);
150 unused GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions)
153 GtkWidget *combo_box;
154 GtkWidget *frame = gtk_frame_new(label);
155 GtkEntryCompletion *completion;
157 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
159 combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
160 gtk_container_add(GTK_CONTAINER(frame), combo_box);
162 entry = GTK_ENTRY(GTK_BIN(combo_box)->child);
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);
172 return GTK_COMBO_BOX_ENTRY(combo_box);
175 static GtkTextBuffer *text_view(GtkWidget *box, const char *label)
177 GtkWidget *view, *vbox;
178 GtkTextBuffer *buffer;
179 GtkWidget *frame = gtk_frame_new(label);
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);
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);
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);
198 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
200 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
208 } found_string_entry;
209 static GtkTreeIter string_entry_location;
211 static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
213 const char *string = data;
217 gtk_tree_model_get(model, iter, 0, &entry, -1);
218 cmp = strcmp(entry, string);
220 /* Stop. The entry is bigger than the new one */
226 found_string_entry = MATCH_EXACT;
230 string_entry_location = *iter;
231 found_string_entry = MATCH_AFTER;
235 static int match_list(GtkListStore *list, const char *string)
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;
242 static GtkListStore *location_list, *people_list;
244 static void add_string_list_entry(const char *string, GtkListStore *list)
246 GtkTreeIter *iter, loc;
248 if (!string || !*string)
251 switch (match_list(list, string)) {
258 iter = &string_entry_location;
261 gtk_list_store_insert_after(list, &loc, iter);
262 gtk_list_store_set(list, &loc, 0, string, -1);
265 void add_people(const char *string)
267 add_string_list_entry(string, people_list);
270 void add_location(const char *string)
272 add_string_list_entry(string, location_list);
275 GtkWidget *extended_dive_info_widget(void)
277 GtkWidget *vbox, *hbox;
278 vbox = gtk_vbox_new(FALSE, 6);
280 people_list = gtk_list_store_new(1, G_TYPE_STRING);
281 location_list = gtk_list_store_new(1, G_TYPE_STRING);
283 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
284 location = text_value(vbox, "Location");
286 hbox = gtk_hbox_new(FALSE, 3);
287 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
289 divemaster = text_value(hbox, "Divemaster");
290 buddy = text_value(hbox, "Buddy");
292 notes = text_view(vbox, "Notes");