]> git.tdb.fi Git - ext/subsurface.git/blob - main.c
Add radio buttons for temperature and volume
[ext/subsurface.git] / main.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <time.h>
5
6 #include "dive.h"
7 #include "divelist.h"
8 #include "display.h"
9
10 GtkWidget *main_window;
11 GtkWidget *main_vbox;
12 GtkWidget *error_info_bar;
13 GtkWidget *error_label;
14 int        error_count;
15 struct DiveList   dive_list;
16
17 struct units output_units;
18
19 static int sortfn(const void *_a, const void *_b)
20 {
21         const struct dive *a = *(void **)_a;
22         const struct dive *b = *(void **)_b;
23
24         if (a->when < b->when)
25                 return -1;
26         if (a->when > b->when)
27                 return 1;
28         return 0;
29 }
30
31 /*
32  * This doesn't really report anything at all. We just sort the
33  * dives, the GUI does the reporting
34  */
35 static void report_dives(void)
36 {
37         int i;
38
39         qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
40
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];
45                 struct dive *merged;
46
47                 if (prev->when + prev->duration.seconds < dive->when)
48                         continue;
49
50                 merged = try_to_merge(prev, dive);
51                 if (!merged)
52                         continue;
53
54                 free(prev);
55                 free(dive);
56                 *pp = merged;
57                 dive_table.nr--;
58                 memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
59
60                 /* Redo the new 'i'th dive */
61                 i--;
62         }
63 }
64
65 static void parse_argument(const char *arg)
66 {
67         const char *p = arg+1;
68
69         do {
70                 switch (*p) {
71                 case 'v':
72                         verbose++;
73                         continue;
74                 default:
75                         fprintf(stderr, "Bad argument '%s'\n", arg);
76                         exit(1);
77                 }
78         } while (*++p);
79 }
80
81 static void on_destroy(GtkWidget* w, gpointer data)
82 {
83         gtk_main_quit();
84 }
85
86 static GtkWidget *dive_profile;
87
88 void repaint_dive(void)
89 {
90         update_dive_info(current_dive);
91         gtk_widget_queue_draw(dive_profile);
92 }
93
94 static char *existing_filename;
95
96 static void on_info_bar_response(GtkWidget *widget, gint response,
97                                  gpointer data)
98 {
99         if (response == GTK_RESPONSE_OK)
100         {
101                 gtk_widget_destroy(widget);
102                 error_info_bar = NULL;
103         }
104 }
105
106 static void report_error(GError* error)
107 {
108         if (error == NULL)
109         {
110                 return;
111         }
112         
113         if (error_info_bar == NULL)
114         {
115                 error_count = 1;
116                 error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
117                                                                GTK_RESPONSE_OK,
118                                                                NULL);
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),
121                                               GTK_MESSAGE_ERROR);
122                 
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);
126                 
127                 gtk_box_pack_start(GTK_BOX(main_vbox), error_info_bar, FALSE, FALSE, 0);
128                 gtk_widget_show_all(main_vbox);
129         }
130         else
131         {
132                 error_count++;
133                 char buffer[256];
134                 snprintf(buffer, sizeof(buffer), "Failed to open %i files.", error_count);
135                 gtk_label_set(GTK_LABEL(error_label), buffer);
136         }
137 }
138
139 static void file_open(GtkWidget *w, gpointer data)
140 {
141         GtkWidget *dialog;
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,
147                 NULL);
148         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
149
150         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
151                 GSList *filenames;
152                 char *filename;
153                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
154                 
155                 GError *error = NULL;
156                 while(filenames != NULL) {
157                         filename = (char *)filenames->data;
158                         parse_xml_file(filename, &error);
159                         if (error != NULL)
160                         {
161                                 report_error(error);
162                                 g_error_free(error);
163                                 error = NULL;
164                         }
165                         
166                         g_free(filename);
167                         filenames = g_slist_next(filenames);
168                 }
169                 g_slist_free(filenames);
170                 report_dives();
171                 dive_list_update_dives(dive_list);
172         }
173         gtk_widget_destroy(dialog);
174 }
175
176 static void file_save(GtkWidget *w, gpointer data)
177 {
178         GtkWidget *dialog;
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,
184                 NULL);
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");
188         } else
189                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
190
191         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
192                 char *filename;
193                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
194                 save_dives(filename);
195                 g_free(filename);
196         }
197         gtk_widget_destroy(dialog);
198 }
199
200 static void quit(GtkWidget *w, gpointer data)
201 {
202         gtk_main_quit();
203 }
204
205 static void create_radio(GtkWidget *dialog, const char *name, ...)
206 {
207         va_list args;
208         GtkRadioButton *group = NULL;
209         GtkWidget *box, *label;
210
211         box = gtk_hbox_new(TRUE, 10);
212         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), box);
213         gtk_widget_show(box);
214
215         label = gtk_label_new(name);
216         gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
217         gtk_widget_show(label);
218
219         va_start(args, name);
220         for (;;) {
221                 int enabled;
222                 const char *name;
223                 GtkWidget *button;
224                 void *callback_fn;
225
226                 name = va_arg(args, char *);
227                 if (!name)
228                         break;
229                 callback_fn = va_arg(args, void *);
230                 enabled = va_arg(args, int);
231
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);
238         }
239         va_end(args);
240 }
241
242 #define UNITCALLBACK(name, type, value)                         \
243 static void name(GtkWidget *w, gpointer data)                   \
244 {                                                               \
245         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
246                 output_units.type = value;                      \
247         repaint_dive();                                         \
248         dive_list_update_dives(dive_list);                      \
249 }
250
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)
259
260 static void unit_dialog(GtkWidget *w, gpointer data)
261 {
262         GtkWidget *dialog;
263
264         dialog = gtk_dialog_new_with_buttons("Units",
265                 GTK_WINDOW(main_window),
266                 GTK_DIALOG_DESTROY_WITH_PARENT, NULL);
267
268         create_radio(dialog, "Depth:",
269                 "Meter", set_meter, (output_units.length == METERS),
270                 "Feet",  set_feet, (output_units.length == FEET),
271                 NULL);
272
273         create_radio(dialog, "Pressure:",
274                 "Bar", set_bar, (output_units.pressure == BAR),
275                 "PSI",  set_psi, (output_units.pressure == PSI),
276                 NULL);
277
278         create_radio(dialog, "Volume:",
279                 "Liter",  set_liter, (output_units.volume == LITER),
280                 "CuFt", set_cuft, (output_units.volume == CUFT),
281                 NULL);
282
283         create_radio(dialog, "Temperature:",
284                 "Celsius", set_celsius, (output_units.temperature == CELSIUS),
285                 "Fahrenheit",  set_fahrenheit, (output_units.temperature == FAHRENHEIT),
286                 NULL);
287
288         gtk_widget_show(dialog);
289 }
290
291 static GtkActionEntry menu_items[] = {
292         { "FileMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
293         { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<control>O", NULL, G_CALLBACK(file_open) },
294         { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<control>S", NULL, G_CALLBACK(file_save) },
295         { "Quit",           GTK_STOCK_QUIT, NULL,   "<control>Q", NULL, G_CALLBACK(quit) },
296         { "Units",          NULL, "Units", NULL, NULL, G_CALLBACK(unit_dialog) },
297 };
298 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
299
300 static const gchar* ui_string = " \
301         <ui> \
302                 <menubar name=\"MainMenu\"> \
303                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
304                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
305                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
306                                 <separator name=\"Separator1\"/> \
307                                 <menuitem name=\"Units\" action=\"Units\" /> \
308                                 <separator name=\"Separator2\"/> \
309                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
310                         </menu> \
311                 </menubar> \
312         </ui> \
313 ";
314
315 static GtkWidget *get_menubar_menu(GtkWidget *window)
316 {
317         GtkActionGroup *action_group = gtk_action_group_new("Menu");
318         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
319
320         GtkUIManager *ui_manager = gtk_ui_manager_new();
321         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
322         GError* error = 0;
323         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
324
325         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
326         GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
327
328         return menu;
329 }
330
331 int main(int argc, char **argv)
332 {
333         int i;
334         GtkWidget *win;
335         GtkWidget *paned;
336         GtkWidget *info_box;
337         GtkWidget *notebook;
338         GtkWidget *frame;
339         GtkWidget *dive_info;
340         GtkWidget *menubar;
341         GtkWidget *vbox;
342
343         output_units = SI_units;
344         parse_xml_init();
345
346         gtk_init(&argc, &argv);
347
348         error_info_bar = NULL;
349         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
350         g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
351         main_window = win;
352
353         vbox = gtk_vbox_new(FALSE, 0);
354         gtk_container_add(GTK_CONTAINER(win), vbox);
355         main_vbox = vbox;
356
357         menubar = get_menubar_menu(win);
358         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
359
360         /* HPane for left the dive list, and right the dive info */
361         paned = gtk_hpaned_new();
362         gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
363
364         /* Create the actual divelist */
365         dive_list = dive_list_create();
366         gtk_paned_add1(GTK_PANED(paned), dive_list.container_widget);
367
368         /* VBox for dive info, and tabs */
369         info_box = gtk_vbox_new(FALSE, 6);
370         gtk_paned_add2(GTK_PANED(paned), info_box);
371
372         /* Frame for minimal dive info */
373         frame = dive_info_frame();
374         gtk_box_pack_start(GTK_BOX(info_box), frame, FALSE, TRUE, 6);
375
376         /* Notebook for dive info vs profile vs .. */
377         notebook = gtk_notebook_new();
378         gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
379
380         /* Frame for dive profile */
381         dive_profile = dive_profile_widget();
382         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
383
384         /* Frame for extended dive info */
385         dive_info = extended_dive_info_widget();
386         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Extended Dive Info"));
387
388         gtk_widget_set_app_paintable(win, TRUE);
389         gtk_widget_show_all(win);
390         
391         for (i = 1; i < argc; i++) {
392                 const char *a = argv[i];
393
394                 if (a[0] == '-') {
395                         parse_argument(a);
396                         continue;
397                 }
398                 GError *error = NULL;
399                 parse_xml_file(a, &error);
400                 
401                 if (error != NULL)
402                 {
403                         report_error(error);
404                         g_error_free(error);
405                         error = NULL;
406                 }
407         }
408
409         report_dives();
410         dive_list_update_dives(dive_list);
411
412         gtk_main();
413         return 0;
414 }