6 #include <gconf/gconf-client.h>
12 GtkWidget *main_window;
14 GtkWidget *error_info_bar;
15 GtkWidget *error_label;
17 struct DiveList dive_list;
20 struct units output_units;
22 #define GCONF_NAME(x) "/apps/diveclog/" #x
24 static int sortfn(const void *_a, const void *_b)
26 const struct dive *a = *(void **)_a;
27 const struct dive *b = *(void **)_b;
29 if (a->when < b->when)
31 if (a->when > b->when)
37 * This doesn't really report anything at all. We just sort the
38 * dives, the GUI does the reporting
40 static void report_dives(void)
44 qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
46 for (i = 1; i < dive_table.nr; i++) {
47 struct dive **pp = &dive_table.dives[i-1];
48 struct dive *prev = pp[0];
49 struct dive *dive = pp[1];
52 if (prev->when + prev->duration.seconds < dive->when)
55 merged = try_to_merge(prev, dive);
63 memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
65 /* Redo the new 'i'th dive */
70 static void parse_argument(const char *arg)
72 const char *p = arg+1;
80 fprintf(stderr, "Bad argument '%s'\n", arg);
86 static void on_destroy(GtkWidget* w, gpointer data)
91 static GtkWidget *dive_profile;
93 void repaint_dive(void)
95 update_dive_info(current_dive);
96 gtk_widget_queue_draw(dive_profile);
99 static char *existing_filename;
101 static void on_info_bar_response(GtkWidget *widget, gint response,
104 if (response == GTK_RESPONSE_OK)
106 gtk_widget_destroy(widget);
107 error_info_bar = NULL;
111 static void report_error(GError* error)
118 if (error_info_bar == NULL)
121 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
124 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
125 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
128 error_label = gtk_label_new(error->message);
129 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
130 gtk_container_add(GTK_CONTAINER(container), error_label);
132 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
133 gtk_widget_show_all(main_vbox);
139 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
140 gtk_label_set(GTK_LABEL(error_label), buffer);
144 static void file_open(GtkWidget *w, gpointer data)
147 dialog = gtk_file_chooser_dialog_new("Open File",
148 GTK_WINDOW(main_window),
149 GTK_FILE_CHOOSER_ACTION_OPEN,
150 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
151 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
153 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
155 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
158 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
160 GError *error = NULL;
161 while(filenames != NULL) {
162 filename = (char *)filenames->data;
163 parse_xml_file(filename, &error);
172 filenames = g_slist_next(filenames);
174 g_slist_free(filenames);
176 dive_list_update_dives(dive_list);
178 gtk_widget_destroy(dialog);
181 static void file_save(GtkWidget *w, gpointer data)
184 dialog = gtk_file_chooser_dialog_new("Save File",
185 GTK_WINDOW(main_window),
186 GTK_FILE_CHOOSER_ACTION_SAVE,
187 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
188 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
190 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
191 if (!existing_filename) {
192 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
194 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
196 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
198 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
199 save_dives(filename);
202 gtk_widget_destroy(dialog);
205 static void quit(GtkWidget *w, gpointer data)
210 static void create_radio(GtkWidget *dialog, const char *name, ...)
213 GtkRadioButton *group = NULL;
214 GtkWidget *box, *label;
216 box = gtk_hbox_new(TRUE, 10);
217 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), box);
219 label = gtk_label_new(name);
220 gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
222 va_start(args, name);
229 name = va_arg(args, char *);
232 callback_fn = va_arg(args, void *);
233 enabled = va_arg(args, int);
235 button = gtk_radio_button_new_with_label_from_widget(group, name);
236 group = GTK_RADIO_BUTTON(button);
237 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
238 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
239 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
244 #define UNITCALLBACK(name, type, value) \
245 static void name(GtkWidget *w, gpointer data) \
247 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
248 menu_units.type = value; \
251 static struct units menu_units;
253 UNITCALLBACK(set_meter, length, METERS)
254 UNITCALLBACK(set_feet, length, FEET)
255 UNITCALLBACK(set_bar, pressure, BAR)
256 UNITCALLBACK(set_psi, pressure, PSI)
257 UNITCALLBACK(set_liter, volume, LITER)
258 UNITCALLBACK(set_cuft, volume, CUFT)
259 UNITCALLBACK(set_celsius, temperature, CELSIUS)
260 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
262 static void unit_dialog(GtkWidget *w, gpointer data)
267 menu_units = output_units;
269 dialog = gtk_dialog_new_with_buttons("Units",
270 GTK_WINDOW(main_window),
271 GTK_DIALOG_DESTROY_WITH_PARENT,
272 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
273 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
276 create_radio(dialog, "Depth:",
277 "Meter", set_meter, (output_units.length == METERS),
278 "Feet", set_feet, (output_units.length == FEET),
281 create_radio(dialog, "Pressure:",
282 "Bar", set_bar, (output_units.pressure == BAR),
283 "PSI", set_psi, (output_units.pressure == PSI),
286 create_radio(dialog, "Volume:",
287 "Liter", set_liter, (output_units.volume == LITER),
288 "CuFt", set_cuft, (output_units.volume == CUFT),
291 create_radio(dialog, "Temperature:",
292 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
293 "Fahrenheit", set_fahrenheit, (output_units.temperature == FAHRENHEIT),
296 gtk_widget_show_all(dialog);
297 result = gtk_dialog_run(GTK_DIALOG(dialog));
298 if (result == GTK_RESPONSE_ACCEPT) {
299 output_units = menu_units;
300 update_dive_list_units(&dive_list);
302 gconf_client_set_bool(gconf, GCONF_NAME(feet), output_units.length == FEET, NULL);
303 gconf_client_set_bool(gconf, GCONF_NAME(psi), output_units.pressure == PSI, NULL);
304 gconf_client_set_bool(gconf, GCONF_NAME(cuft), output_units.volume == CUFT, NULL);
305 gconf_client_set_bool(gconf, GCONF_NAME(fahrenheit), output_units.temperature == FAHRENHEIT, NULL);
307 gtk_widget_destroy(dialog);
310 static GtkActionEntry menu_items[] = {
311 { "FileMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
312 { "OpenFile", GTK_STOCK_OPEN, NULL, "<control>O", NULL, G_CALLBACK(file_open) },
313 { "SaveFile", GTK_STOCK_SAVE, NULL, "<control>S", NULL, G_CALLBACK(file_save) },
314 { "Quit", GTK_STOCK_QUIT, NULL, "<control>Q", NULL, G_CALLBACK(quit) },
315 { "Units", NULL, "Units", NULL, NULL, G_CALLBACK(unit_dialog) },
317 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
319 static const gchar* ui_string = " \
321 <menubar name=\"MainMenu\"> \
322 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
323 <menuitem name=\"Open\" action=\"OpenFile\" /> \
324 <menuitem name=\"Save\" action=\"SaveFile\" /> \
325 <separator name=\"Separator1\"/> \
326 <menuitem name=\"Units\" action=\"Units\" /> \
327 <separator name=\"Separator2\"/> \
328 <menuitem name=\"Quit\" action=\"Quit\" /> \
334 static GtkWidget *get_menubar_menu(GtkWidget *window)
336 GtkActionGroup *action_group = gtk_action_group_new("Menu");
337 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
339 GtkUIManager *ui_manager = gtk_ui_manager_new();
340 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
342 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
344 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
345 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
350 int main(int argc, char **argv)
358 GtkWidget *dive_info;
359 GtkWidget *cylinder_management;
363 output_units = SI_units;
366 gtk_init(&argc, &argv);
369 gconf = gconf_client_get_default();
371 if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
372 output_units.length = FEET;
373 if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
374 output_units.pressure = PSI;
375 if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
376 output_units.volume = CUFT;
377 if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
378 output_units.temperature = FAHRENHEIT;
380 error_info_bar = NULL;
381 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
382 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
385 vbox = gtk_vbox_new(FALSE, 0);
386 gtk_container_add(GTK_CONTAINER(win), vbox);
389 menubar = get_menubar_menu(win);
390 gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
392 /* HPane for left the dive list, and right the dive info */
393 paned = gtk_hpaned_new();
394 gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
396 /* Create the actual divelist */
397 dive_list = dive_list_create();
398 gtk_paned_add1(GTK_PANED(paned), dive_list.container_widget);
400 /* VBox for dive info, and tabs */
401 info_box = gtk_vbox_new(FALSE, 6);
402 gtk_paned_add2(GTK_PANED(paned), info_box);
404 /* Frame for minimal dive info */
405 frame = dive_info_frame();
406 gtk_box_pack_start(GTK_BOX(info_box), frame, FALSE, TRUE, 6);
408 /* Notebook for dive info vs profile vs .. */
409 notebook = gtk_notebook_new();
410 gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
412 /* Frame for dive profile */
413 dive_profile = dive_profile_widget();
414 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
416 /* Frame for extended dive info */
417 dive_info = extended_dive_info_widget();
418 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
420 /* Frame for extended dive info */
421 cylinder_management = cylinder_management_widget();
422 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), cylinder_management, gtk_label_new("Cylinders"));
424 gtk_widget_set_app_paintable(win, TRUE);
425 gtk_widget_show_all(win);
427 for (i = 1; i < argc; i++) {
428 const char *a = argv[i];
434 GError *error = NULL;
435 parse_xml_file(a, &error);
446 dive_list_update_dives(dive_list);