]> git.tdb.fi Git - ext/subsurface.git/blob - main.c
Make the divelist font configurable
[ext/subsurface.git] / main.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <time.h>
5
6 #include <gconf/gconf-client.h>
7
8 #include "dive.h"
9 #include "divelist.h"
10 #include "display.h"
11
12 GtkWidget *main_window;
13 GtkWidget *main_vbox;
14 GtkWidget *error_info_bar;
15 GtkWidget *error_label;
16 int        error_count;
17
18 #define DIVELIST_DEFAULT_FONT "Sans 8"
19 const char *divelist_font;
20
21 GConfClient *gconf;
22 struct units output_units;
23
24 #define GCONF_NAME(x) "/apps/subsurface/" #x
25
26 static int sortfn(const void *_a, const void *_b)
27 {
28         const struct dive *a = *(void **)_a;
29         const struct dive *b = *(void **)_b;
30
31         if (a->when < b->when)
32                 return -1;
33         if (a->when > b->when)
34                 return 1;
35         return 0;
36 }
37
38 /*
39  * This doesn't really report anything at all. We just sort the
40  * dives, the GUI does the reporting
41  */
42 void report_dives(void)
43 {
44         int i;
45
46         qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
47
48         for (i = 1; i < dive_table.nr; i++) {
49                 struct dive **pp = &dive_table.dives[i-1];
50                 struct dive *prev = pp[0];
51                 struct dive *dive = pp[1];
52                 struct dive *merged;
53
54                 if (prev->when + prev->duration.seconds < dive->when)
55                         continue;
56
57                 merged = try_to_merge(prev, dive);
58                 if (!merged)
59                         continue;
60
61                 free(prev);
62                 free(dive);
63                 *pp = merged;
64                 dive_table.nr--;
65                 memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
66
67                 /* Redo the new 'i'th dive */
68                 i--;
69         }
70 }
71
72 static void parse_argument(const char *arg)
73 {
74         const char *p = arg+1;
75
76         do {
77                 switch (*p) {
78                 case 'v':
79                         verbose++;
80                         continue;
81                 default:
82                         fprintf(stderr, "Bad argument '%s'\n", arg);
83                         exit(1);
84                 }
85         } while (*++p);
86 }
87
88 static void on_destroy(GtkWidget* w, gpointer data)
89 {
90         gtk_main_quit();
91 }
92
93 static GtkWidget *dive_profile;
94
95 void update_dive(struct dive *new_dive)
96 {
97         static struct dive *buffered_dive;
98         struct dive *old_dive = buffered_dive;
99
100         if (old_dive) {
101                 flush_dive_info_changes(old_dive);
102                 flush_dive_equipment_changes(old_dive);
103                 flush_divelist(old_dive);
104         }
105         if (new_dive) {
106                 show_dive_info(new_dive);
107                 show_dive_equipment(new_dive);
108         }
109         buffered_dive = new_dive;
110 }
111
112 void repaint_dive(void)
113 {
114         update_dive(current_dive);
115         gtk_widget_queue_draw(dive_profile);
116 }
117
118 static char *existing_filename;
119
120 static void on_info_bar_response(GtkWidget *widget, gint response,
121                                  gpointer data)
122 {
123         if (response == GTK_RESPONSE_OK)
124         {
125                 gtk_widget_destroy(widget);
126                 error_info_bar = NULL;
127         }
128 }
129
130 void report_error(GError* error)
131 {
132         if (error == NULL)
133         {
134                 return;
135         }
136         
137         if (error_info_bar == NULL)
138         {
139                 error_count = 1;
140                 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
141                                                                GTK_RESPONSE_OK,
142                                                                NULL);
143                 g_signal_connect(error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
144                 gtk_info_bar_set_message_type(GTK_INFO_BAR(error_info_bar),
145                                               GTK_MESSAGE_ERROR);
146                 
147                 error_label = gtk_label_new(error->message);
148                 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(error_info_bar));
149                 gtk_container_add(GTK_CONTAINER(container), error_label);
150                 
151                 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
152                 gtk_widget_show_all(main_vbox);
153         }
154         else
155         {
156                 error_count++;
157                 char buffer[256];
158                 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
159                 gtk_label_set(GTK_LABEL(error_label), buffer);
160         }
161 }
162
163 static void file_open(GtkWidget *w, gpointer data)
164 {
165         GtkWidget *dialog;
166         dialog = gtk_file_chooser_dialog_new("Open File",
167                 GTK_WINDOW(main_window),
168                 GTK_FILE_CHOOSER_ACTION_OPEN,
169                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
170                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
171                 NULL);
172         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
173
174         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
175                 GSList *filenames;
176                 char *filename;
177                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
178                 
179                 GError *error = NULL;
180                 while(filenames != NULL) {
181                         filename = (char *)filenames->data;
182                         parse_xml_file(filename, &error);
183                         if (error != NULL)
184                         {
185                                 report_error(error);
186                                 g_error_free(error);
187                                 error = NULL;
188                         }
189                         
190                         g_free(filename);
191                         filenames = g_slist_next(filenames);
192                 }
193                 g_slist_free(filenames);
194                 report_dives();
195                 dive_list_update_dives();
196         }
197         gtk_widget_destroy(dialog);
198 }
199
200 static void file_save(GtkWidget *w, gpointer data)
201 {
202         GtkWidget *dialog;
203         dialog = gtk_file_chooser_dialog_new("Save File",
204                 GTK_WINDOW(main_window),
205                 GTK_FILE_CHOOSER_ACTION_SAVE,
206                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
207                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
208                 NULL);
209         gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
210         if (!existing_filename) {
211                 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
212         } else
213                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
214
215         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
216                 char *filename;
217                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
218                 save_dives(filename);
219                 g_free(filename);
220         }
221         gtk_widget_destroy(dialog);
222 }
223
224 static void quit(GtkWidget *w, gpointer data)
225 {
226         gtk_main_quit();
227 }
228
229 static void create_radio(GtkWidget *vbox, const char *name, ...)
230 {
231         va_list args;
232         GtkRadioButton *group = NULL;
233         GtkWidget *box, *label;
234
235         box = gtk_hbox_new(TRUE, 10);
236         gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 0);
237
238         label = gtk_label_new(name);
239         gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
240
241         va_start(args, name);
242         for (;;) {
243                 int enabled;
244                 const char *name;
245                 GtkWidget *button;
246                 void *callback_fn;
247
248                 name = va_arg(args, char *);
249                 if (!name)
250                         break;
251                 callback_fn = va_arg(args, void *);
252                 enabled = va_arg(args, int);
253
254                 button = gtk_radio_button_new_with_label_from_widget(group, name);
255                 group = GTK_RADIO_BUTTON(button);
256                 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
257                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), enabled);
258                 g_signal_connect(button, "toggled", G_CALLBACK(callback_fn), NULL);
259         }
260         va_end(args);
261 }
262
263 #define UNITCALLBACK(name, type, value)                         \
264 static void name(GtkWidget *w, gpointer data)                   \
265 {                                                               \
266         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
267                 menu_units.type = value;                        \
268 }
269
270 static struct units menu_units;
271
272 UNITCALLBACK(set_meter, length, METERS)
273 UNITCALLBACK(set_feet, length, FEET)
274 UNITCALLBACK(set_bar, pressure, BAR)
275 UNITCALLBACK(set_psi, pressure, PSI)
276 UNITCALLBACK(set_liter, volume, LITER)
277 UNITCALLBACK(set_cuft, volume, CUFT)
278 UNITCALLBACK(set_celsius, temperature, CELSIUS)
279 UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT)
280
281 static void preferences_dialog(GtkWidget *w, gpointer data)
282 {
283         int result;
284         GtkWidget *dialog, *font, *frame, *box;
285
286         menu_units = output_units;
287
288         dialog = gtk_dialog_new_with_buttons("Preferences",
289                 GTK_WINDOW(main_window),
290                 GTK_DIALOG_DESTROY_WITH_PARENT,
291                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
292                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
293                 NULL);
294
295         frame = gtk_frame_new("Units");
296         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, FALSE, FALSE, 5);
297
298         box = gtk_vbox_new(FALSE, 6);
299         gtk_container_add(GTK_CONTAINER(frame), box);
300
301         create_radio(box, "Depth:",
302                 "Meter", set_meter, (output_units.length == METERS),
303                 "Feet",  set_feet, (output_units.length == FEET),
304                 NULL);
305
306         create_radio(box, "Pressure:",
307                 "Bar", set_bar, (output_units.pressure == BAR),
308                 "PSI",  set_psi, (output_units.pressure == PSI),
309                 NULL);
310
311         create_radio(box, "Volume:",
312                 "Liter",  set_liter, (output_units.volume == LITER),
313                 "CuFt", set_cuft, (output_units.volume == CUFT),
314                 NULL);
315
316         create_radio(box, "Temperature:",
317                 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
318                 "Fahrenheit",  set_fahrenheit, (output_units.temperature == FAHRENHEIT),
319                 NULL);
320
321         font = gtk_font_button_new_with_font(divelist_font);
322         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), font, FALSE, FALSE, 5);
323
324         gtk_widget_show_all(dialog);
325         result = gtk_dialog_run(GTK_DIALOG(dialog));
326         if (result == GTK_RESPONSE_ACCEPT) {
327                 /* Make sure to flush any modified old dive data with old units */
328                 update_dive(NULL);
329
330                 divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
331                 set_divelist_font(divelist_font);
332
333                 output_units = menu_units;
334                 update_dive_list_units();
335                 repaint_dive();
336                 gconf_client_set_bool(gconf, GCONF_NAME(feet), output_units.length == FEET, NULL);
337                 gconf_client_set_bool(gconf, GCONF_NAME(psi), output_units.pressure == PSI, NULL);
338                 gconf_client_set_bool(gconf, GCONF_NAME(cuft), output_units.volume == CUFT, NULL);
339                 gconf_client_set_bool(gconf, GCONF_NAME(fahrenheit), output_units.temperature == FAHRENHEIT, NULL);
340                 gconf_client_set_string(gconf, GCONF_NAME(divelist_font), divelist_font, NULL);
341         }
342         gtk_widget_destroy(dialog);
343 }
344
345 static void renumber_dives(int nr)
346 {
347         int i;
348
349         for (i = 0; i < dive_table.nr; i++) {
350                 struct dive *dive = dive_table.dives[i];
351                 dive->number = nr + i;
352         }
353 }
354
355 static void renumber_dialog(GtkWidget *w, gpointer data)
356 {
357         int result;
358         GtkWidget *dialog, *frame, *button;
359
360         dialog = gtk_dialog_new_with_buttons("Renumber",
361                 GTK_WINDOW(main_window),
362                 GTK_DIALOG_DESTROY_WITH_PARENT,
363                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
364                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
365                 NULL);
366
367         frame = gtk_frame_new("New starting number");
368         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), frame);
369
370         button = gtk_spin_button_new_with_range(1, 50000, 1);
371         gtk_container_add(GTK_CONTAINER(frame), button);
372
373         gtk_widget_show_all(dialog);
374         result = gtk_dialog_run(GTK_DIALOG(dialog));
375         if (result == GTK_RESPONSE_ACCEPT) {
376                 int nr = gtk_spin_button_get_value(GTK_SPIN_BUTTON(button));
377                 renumber_dives(nr);
378                 repaint_dive();
379         }
380         gtk_widget_destroy(dialog);
381 }
382
383 static GtkActionEntry menu_items[] = {
384         { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
385         { "LogMenuAction",  GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
386         { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<control>O", NULL, G_CALLBACK(file_open) },
387         { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<control>S", NULL, G_CALLBACK(file_save) },
388         { "Print",          GTK_STOCK_PRINT, NULL,  "<control>P", NULL, G_CALLBACK(do_print) },
389         { "Import",         NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) },
390         { "Preferences",    NULL, "Preferences", NULL, NULL, G_CALLBACK(preferences_dialog) },
391         { "Renumber",       NULL, "Renumber", NULL, NULL, G_CALLBACK(renumber_dialog) },
392         { "Quit",           GTK_STOCK_QUIT, NULL,   "<control>Q", NULL, G_CALLBACK(quit) },
393 };
394 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
395
396 static const gchar* ui_string = " \
397         <ui> \
398                 <menubar name=\"MainMenu\"> \
399                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
400                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
401                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
402                                 <menuitem name=\"Print\" action=\"Print\" /> \
403                                 <separator name=\"Separator1\"/> \
404                                 <menuitem name=\"Import\" action=\"Import\" /> \
405                                 <separator name=\"Separator2\"/> \
406                                 <menuitem name=\"Preferences\" action=\"Preferences\" /> \
407                                 <separator name=\"Separator3\"/> \
408                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
409                         </menu> \
410                         <menu name=\"LogMenu\" action=\"LogMenuAction\"> \
411                                 <menuitem name=\"Renumber\" action=\"Renumber\" /> \
412                         </menu> \
413                 </menubar> \
414         </ui> \
415 ";
416
417 static GtkWidget *get_menubar_menu(GtkWidget *window)
418 {
419         GtkActionGroup *action_group = gtk_action_group_new("Menu");
420         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
421
422         GtkUIManager *ui_manager = gtk_ui_manager_new();
423         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
424         GError* error = 0;
425         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
426
427         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
428         GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
429
430         return menu;
431 }
432
433 static void switch_page(GtkNotebook *notebook, gint arg1, gpointer user_data)
434 {
435         repaint_dive();
436 }
437
438 int main(int argc, char **argv)
439 {
440         int i;
441         GtkWidget *win;
442         GtkWidget *paned;
443         GtkWidget *info_box;
444         GtkWidget *notebook;
445         GtkWidget *dive_info;
446         GtkWidget *dive_list;
447         GtkWidget *equipment;
448         GtkWidget *menubar;
449         GtkWidget *vbox;
450
451         output_units = SI_units;
452         parse_xml_init();
453
454         gtk_init(&argc, &argv);
455
456         g_type_init();
457         gconf = gconf_client_get_default();
458
459         if (gconf_client_get_bool(gconf, GCONF_NAME(feet), NULL))
460                 output_units.length = FEET;
461         if (gconf_client_get_bool(gconf, GCONF_NAME(psi), NULL))
462                 output_units.pressure = PSI;
463         if (gconf_client_get_bool(gconf, GCONF_NAME(cuft), NULL))
464                 output_units.volume = CUFT;
465         if (gconf_client_get_bool(gconf, GCONF_NAME(fahrenheit), NULL))
466                 output_units.temperature = FAHRENHEIT;
467
468         divelist_font = gconf_client_get_string(gconf, GCONF_NAME(divelist_font), NULL);
469         if (!divelist_font)
470                 divelist_font = DIVELIST_DEFAULT_FONT;
471
472         error_info_bar = NULL;
473         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
474         gtk_window_set_icon_from_file(GTK_WINDOW(win), "icon.svg", NULL);
475         g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
476         main_window = win;
477
478         vbox = gtk_vbox_new(FALSE, 0);
479         gtk_container_add(GTK_CONTAINER(win), vbox);
480         main_vbox = vbox;
481
482         menubar = get_menubar_menu(win);
483         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
484
485         /* HPane for left the dive list, and right the dive info */
486         paned = gtk_vpaned_new();
487         gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
488
489         /* Create the actual divelist */
490         dive_list = dive_list_create();
491         gtk_paned_add2(GTK_PANED(paned), dive_list);
492
493         /* VBox for dive info, and tabs */
494         info_box = gtk_vbox_new(FALSE, 6);
495         gtk_paned_add1(GTK_PANED(paned), info_box);
496
497         /* Notebook for dive info vs profile vs .. */
498         notebook = gtk_notebook_new();
499         g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page), NULL);
500         gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
501
502         /* Frame for dive profile */
503         dive_profile = dive_profile_widget();
504         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
505
506         /* Frame for extended dive info */
507         dive_info = extended_dive_info_widget();
508         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Dive Notes"));
509
510         /* Frame for dive equipment */
511         equipment = equipment_widget();
512         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), equipment, gtk_label_new("Equipment"));
513
514         gtk_widget_set_app_paintable(win, TRUE);
515         gtk_widget_show_all(win);
516         
517         for (i = 1; i < argc; i++) {
518                 const char *a = argv[i];
519
520                 if (a[0] == '-') {
521                         parse_argument(a);
522                         continue;
523                 }
524                 GError *error = NULL;
525                 parse_xml_file(a, &error);
526                 
527                 if (error != NULL)
528                 {
529                         report_error(error);
530                         g_error_free(error);
531                         error = NULL;
532                 }
533         }
534
535         report_dives();
536         dive_list_update_dives();
537
538         gtk_main();
539         return 0;
540 }