10 GtkWidget *main_window;
12 GtkWidget *error_info_bar;
13 GtkWidget *error_label;
15 struct DiveList dive_list;
17 struct units output_units;
19 static int sortfn(const void *_a, const void *_b)
21 const struct dive *a = *(void **)_a;
22 const struct dive *b = *(void **)_b;
24 if (a->when < b->when)
26 if (a->when > b->when)
32 * This doesn't really report anything at all. We just sort the
33 * dives, the GUI does the reporting
35 static void report_dives(void)
39 qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
41 for (i = 1; i < dive_table.nr; i++) {
42 struct dive **pp = &dive_table.dives[i-1];
43 struct dive *prev = pp[0];
44 struct dive *dive = pp[1];
47 if (prev->when + prev->duration.seconds < dive->when)
50 merged = try_to_merge(prev, dive);
58 memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
60 /* Redo the new 'i'th dive */
65 static void parse_argument(const char *arg)
67 const char *p = arg+1;
75 fprintf(stderr, "Bad argument '%s'\n", arg);
81 static void on_destroy(GtkWidget* w, gpointer data)
86 static GtkWidget *dive_profile;
88 void repaint_dive(void)
90 update_dive_info(current_dive);
91 gtk_widget_queue_draw(dive_profile);
94 static char *existing_filename;
96 static void on_info_bar_response(GtkWidget *widget, gint response,
99 if (response == GTK_RESPONSE_OK)
101 gtk_widget_destroy(widget);
102 error_info_bar = NULL;
106 static void report_error(GError* error)
113 if (error_info_bar == NULL)
116 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
119 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
120 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
123 error_label = gtk_label_new(error->message);
124 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
125 gtk_container_add(GTK_CONTAINER(container), error_label);
127 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
128 gtk_widget_show_all(main_vbox);
134 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
135 gtk_label_set(GTK_LABEL(error_label), buffer);
139 static void file_open(GtkWidget *w, gpointer data)
142 dialog = gtk_file_chooser_dialog_new("Open File",
143 GTK_WINDOW(main_window),
144 GTK_FILE_CHOOSER_ACTION_OPEN,
145 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
146 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
148 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
150 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
153 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
155 GError *error = NULL;
156 while(filenames != NULL) {
157 filename = (char *)filenames->data;
158 parse_xml_file(filename, &error);
167 filenames = g_slist_next(filenames);
169 g_slist_free(filenames);
171 dive_list_update_dives(dive_list);
173 gtk_widget_destroy(dialog);
176 static void file_save(GtkWidget *w, gpointer data)
179 dialog = gtk_file_chooser_dialog_new("Save File",
180 GTK_WINDOW(main_window),
181 GTK_FILE_CHOOSER_ACTION_SAVE,
182 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
183 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
185 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
186 if (!existing_filename) {
187 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
189 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
191 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
193 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
194 save_dives(filename);
197 gtk_widget_destroy(dialog);
200 static void quit(GtkWidget *w, gpointer data)
205 static void create_radio(GtkWidget *dialog, const char *name, ...)
208 GtkRadioButton *group = NULL;
209 GtkWidget *box, *label;
211 box = gtk_hbox_new(TRUE, 10);
212 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), box);
213 gtk_widget_show(box);
215 label = gtk_label_new(name);
216 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
217 gtk_widget_show(label);
219 va_start(args, name);
226 name = va_arg(args, char *);
229 callback_fn = va_arg(args, void *);
230 enabled = va_arg(args, int);
232 button = gtk_radio_button_new_with_label_from_widget(group, name);
233 group = GTK_RADIO_BUTTON(button);
234 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
235 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
236 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
237 gtk_widget_show(button);
242 #define UNITCALLBACK(name, type, value) \
243 static void name(GtkWidget *w, gpointer data) \
245 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
246 menu_units.type = value; \
249 static struct units menu_units;
251 UNITCALLBACK(set_meter, length, METERS)
252 UNITCALLBACK(set_feet, length, FEET)
253 UNITCALLBACK(set_bar, pressure, BAR)
254 UNITCALLBACK(set_psi, pressure, PSI)
255 UNITCALLBACK(set_liter, volume, LITER)
256 UNITCALLBACK(set_cuft, volume, CUFT)
257 UNITCALLBACK(set_celsius, temperature, CELSIUS)
258 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
260 static void unit_dialog(GtkWidget *w, gpointer data)
265 menu_units = output_units;
267 dialog = gtk_dialog_new_with_buttons("Units",
268 GTK_WINDOW(main_window),
269 GTK_DIALOG_DESTROY_WITH_PARENT,
270 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
271 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
274 create_radio(dialog, "Depth:",
275 "Meter", set_meter, (output_units.length == METERS),
276 "Feet", set_feet, (output_units.length == FEET),
279 create_radio(dialog, "Pressure:",
280 "Bar", set_bar, (output_units.pressure == BAR),
281 "PSI", set_psi, (output_units.pressure == PSI),
284 create_radio(dialog, "Volume:",
285 "Liter", set_liter, (output_units.volume == LITER),
286 "CuFt", set_cuft, (output_units.volume == CUFT),
289 create_radio(dialog, "Temperature:",
290 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
291 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
294 gtk_widget_show(dialog);
295 result = gtk_dialog_run(GTK_DIALOG(dialog));
296 if (result == GTK_RESPONSE_ACCEPT) {
297 output_units = menu_units;
298 update_dive_list_units(&dive_list);
301 gtk_widget_destroy(dialog);
304 static GtkActionEntry menu_items[] = {
305 { "FileMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
306 { "OpenFile", GTK_STOCK_OPEN, NULL, "<control>O", NULL, G_CALLBACK(file_open) },
307 { "SaveFile", GTK_STOCK_SAVE, NULL, "<control>S", NULL, G_CALLBACK(file_save) },
308 { "Quit", GTK_STOCK_QUIT, NULL, "<control>Q", NULL, G_CALLBACK(quit) },
309 { "Units", NULL, "Units", NULL, NULL, G_CALLBACK(unit_dialog) },
311 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
313 static const gchar* ui_string = " \
315 <menubar name=\"MainMenu\"> \
316 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
317 <menuitem name=\"Open\" action=\"OpenFile\" /> \
318 <menuitem name=\"Save\" action=\"SaveFile\" /> \
319 <separator name=\"Separator1\"/> \
320 <menuitem name=\"Units\" action=\"Units\" /> \
321 <separator name=\"Separator2\"/> \
322 <menuitem name=\"Quit\" action=\"Quit\" /> \
328 static GtkWidget *get_menubar_menu(GtkWidget *window)
330 GtkActionGroup *action_group = gtk_action_group_new("Menu");
331 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
333 GtkUIManager *ui_manager = gtk_ui_manager_new();
334 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
336 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
338 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
339 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
344 int main(int argc, char **argv)
352 GtkWidget *dive_info;
356 output_units = SI_units;
359 gtk_init(&argc, &argv);
361 error_info_bar = NULL;
362 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
363 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
366 vbox = gtk_vbox_new(FALSE, 0);
367 gtk_container_add(GTK_CONTAINER(win), vbox);
370 menubar = get_menubar_menu(win);
371 gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
373 /* HPane for left the dive list, and right the dive info */
374 paned = gtk_hpaned_new();
375 gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
377 /* Create the actual divelist */
378 dive_list = dive_list_create();
379 gtk_paned_add1(GTK_PANED(paned), dive_list.container_widget);
381 /* VBox for dive info, and tabs */
382 info_box = gtk_vbox_new(FALSE, 6);
383 gtk_paned_add2(GTK_PANED(paned), info_box);
385 /* Frame for minimal dive info */
386 frame = dive_info_frame();
387 gtk_box_pack_start(GTK_BOX(info_box), frame, FALSE, TRUE, 6);
389 /* Notebook for dive info vs profile vs .. */
390 notebook = gtk_notebook_new();
391 gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
393 /* Frame for dive profile */
394 dive_profile = dive_profile_widget();
395 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
397 /* Frame for extended dive info */
398 dive_info = extended_dive_info_widget();
399 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Extended Dive Info"));
401 gtk_widget_set_app_paintable(win, TRUE);
402 gtk_widget_show_all(win);
404 for (i = 1; i < argc; i++) {
405 const char *a = argv[i];
411 GError *error = NULL;
412 parse_xml_file(a, &error);
423 dive_list_update_dives(dive_list);