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 imperial(GtkWidget *w, gpointer data)
207 output_units = IMPERIAL_units;
211 static void metric(GtkWidget *w, gpointer data)
213 output_units = SI_units;
217 static GtkActionEntry menu_items[] = {
218 { "FileMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
219 { "OpenFile", GTK_STOCK_OPEN, NULL, "<control>O", NULL, G_CALLBACK(file_open) },
220 { "SaveFile", GTK_STOCK_SAVE, NULL, "<control>S", NULL, G_CALLBACK(file_save) },
221 { "Quit", GTK_STOCK_QUIT, NULL, "<control>Q", NULL, G_CALLBACK(quit) },
222 { "Metric", NULL, "Metric", NULL, NULL, G_CALLBACK(metric) },
223 { "Imperial", NULL, "Imperial", NULL, NULL, G_CALLBACK(imperial) },
225 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
227 static const gchar* ui_string = " \
229 <menubar name=\"MainMenu\"> \
230 <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
231 <menuitem name=\"Open\" action=\"OpenFile\" /> \
232 <menuitem name=\"Save\" action=\"SaveFile\" /> \
233 <separator name=\"Seperator\"/> \
234 <menuitem name=\"Metric\" action=\"Metric\" /> \
235 <menuitem name=\"Imperial\" action=\"Imperial\" /> \
236 <separator name=\"Seperator\"/> \
237 <menuitem name=\"Quit\" action=\"Quit\" /> \
243 static GtkWidget *get_menubar_menu(GtkWidget *window)
245 GtkActionGroup *action_group = gtk_action_group_new("Menu");
246 gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
248 GtkUIManager *ui_manager = gtk_ui_manager_new();
249 gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
251 gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
253 gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
254 GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
259 int main(int argc, char **argv)
267 GtkWidget *dive_info;
271 output_units = SI_units;
274 gtk_init(&argc, &argv);
276 error_info_bar = NULL;
277 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
278 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
281 vbox = gtk_vbox_new(FALSE, 0);
282 gtk_container_add(GTK_CONTAINER(win), vbox);
285 menubar = get_menubar_menu(win);
286 gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
288 /* HPane for left the dive list, and right the dive info */
289 paned = gtk_hpaned_new();
290 gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
292 /* Create the actual divelist */
293 dive_list = dive_list_create();
294 gtk_paned_add1(GTK_PANED(paned), dive_list.container_widget);
296 /* VBox for dive info, and tabs */
297 info_box = gtk_vbox_new(FALSE, 6);
298 gtk_paned_add2(GTK_PANED(paned), info_box);
300 /* Frame for minimal dive info */
301 frame = dive_info_frame();
302 gtk_box_pack_start(GTK_BOX(info_box), frame, FALSE, TRUE, 6);
304 /* Notebook for dive info vs profile vs .. */
305 notebook = gtk_notebook_new();
306 gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
308 /* Frame for dive profile */
309 dive_profile = dive_profile_widget();
310 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
312 /* Frame for extended dive info */
313 dive_info = extended_dive_info_widget();
314 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Extended Dive Info"));
316 gtk_widget_set_app_paintable(win, TRUE);
317 gtk_widget_show_all(win);
319 for (i = 1; i < argc; i++) {
320 const char *a = argv[i];
326 GError *error = NULL;
327 parse_xml_file(a, &error);
338 dive_list_update_dives(dive_list);