]> git.tdb.fi Git - ext/subsurface.git/blob - main.c
Make it possible to load multiple files at once.
[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 struct DiveList   dive_list;
12
13 static int sortfn(const void *_a, const void *_b)
14 {
15         const struct dive *a = *(void **)_a;
16         const struct dive *b = *(void **)_b;
17
18         if (a->when < b->when)
19                 return -1;
20         if (a->when > b->when)
21                 return 1;
22         return 0;
23 }
24
25 /*
26  * This doesn't really report anything at all. We just sort the
27  * dives, the GUI does the reporting
28  */
29 static void report_dives(void)
30 {
31         int i;
32
33         qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
34
35         for (i = 1; i < dive_table.nr; i++) {
36                 struct dive **pp = &dive_table.dives[i-1];
37                 struct dive *prev = pp[0];
38                 struct dive *dive = pp[1];
39                 struct dive *merged;
40
41                 if (prev->when + prev->duration.seconds < dive->when)
42                         continue;
43
44                 merged = try_to_merge(prev, dive);
45                 if (!merged)
46                         continue;
47
48                 free(prev);
49                 free(dive);
50                 *pp = merged;
51                 dive_table.nr--;
52                 memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
53
54                 /* Redo the new 'i'th dive */
55                 i--;
56         }
57 }
58
59 static void parse_argument(const char *arg)
60 {
61         const char *p = arg+1;
62
63         do {
64                 switch (*p) {
65                 case 'v':
66                         verbose++;
67                         continue;
68                 default:
69                         fprintf(stderr, "Bad argument '%s'\n", arg);
70                         exit(1);
71                 }
72         } while (*++p);
73 }
74
75 static void on_destroy(GtkWidget* w, gpointer data)
76 {
77         gtk_main_quit();
78 }
79
80 static GtkWidget *dive_profile;
81
82 void repaint_dive(void)
83 {
84         update_dive_info(current_dive);
85         gtk_widget_queue_draw(dive_profile);
86 }
87
88 static char *existing_filename;
89
90 static void file_open(GtkWidget *w, gpointer data)
91 {
92         GtkWidget *dialog;
93         dialog = gtk_file_chooser_dialog_new("Open File",
94                 GTK_WINDOW(main_window),
95                 GTK_FILE_CHOOSER_ACTION_OPEN,
96                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
97                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
98                 NULL);
99         gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
100
101         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
102                 GSList *filenames;
103                 char *filename;
104                 filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
105                 
106                 while(filenames != NULL) {
107                         filename = (char *)filenames->data;
108                         parse_xml_file(filename);
109                         g_free(filename);
110                         filenames = g_slist_next(filenames);
111                 }
112                 g_slist_free(filenames);
113                 report_dives();
114                 dive_list_update_dives(dive_list);
115         }
116         gtk_widget_destroy(dialog);
117 }
118
119 static void file_save(GtkWidget *w, gpointer data)
120 {
121         GtkWidget *dialog;
122         dialog = gtk_file_chooser_dialog_new("Save File",
123                 GTK_WINDOW(main_window),
124                 GTK_FILE_CHOOSER_ACTION_SAVE,
125                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
126                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
127                 NULL);
128         gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
129         if (!existing_filename) {
130                 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
131         } else
132                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
133
134         if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
135                 char *filename;
136                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
137                 save_dives(filename);
138                 g_free(filename);
139         }
140         gtk_widget_destroy(dialog);
141 }
142
143 static void quit(GtkWidget *w, gpointer data)
144 {
145         gtk_main_quit();
146 }
147
148 static GtkActionEntry menu_items[] = {
149         { "FileMenuAction", GTK_STOCK_FILE, "Log", NULL, NULL, NULL},
150         { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<control>O", NULL, G_CALLBACK(file_open) },
151         { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<control>S", NULL, G_CALLBACK(file_save) },
152         { "Quit",           GTK_STOCK_QUIT, NULL,   "<control>Q", NULL, G_CALLBACK(quit) },
153 };
154 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
155
156 static const gchar* ui_string = " \
157         <ui> \
158                 <menubar name=\"MainMenu\"> \
159                         <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
160                                 <menuitem name=\"Open\" action=\"OpenFile\" /> \
161                                 <menuitem name=\"Save\" action=\"SaveFile\" /> \
162                                 <separator name=\"Seperator\"/> \
163                                 <menuitem name=\"Quit\" action=\"Quit\" /> \
164                         </menu> \
165                 </menubar> \
166         </ui> \
167 ";
168
169 static GtkWidget *get_menubar_menu(GtkWidget *window)
170 {
171         GtkActionGroup *action_group = gtk_action_group_new("Menu");
172         gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
173
174         GtkUIManager *ui_manager = gtk_ui_manager_new();
175         gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
176         GError* error = 0;
177         gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
178
179         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
180         GtkWidget* menu = gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
181
182         return menu;
183 }
184
185 int main(int argc, char **argv)
186 {
187         int i;
188         GtkWidget *win;
189         GtkWidget *paned;
190         GtkWidget *info_box;
191         GtkWidget *notebook;
192         GtkWidget *frame;
193         GtkWidget *dive_info;
194         GtkWidget *menubar;
195         GtkWidget *vbox;
196
197         parse_xml_init();
198
199         gtk_init(&argc, &argv);
200
201         for (i = 1; i < argc; i++) {
202                 const char *a = argv[i];
203
204                 if (a[0] == '-') {
205                         parse_argument(a);
206                         continue;
207                 }
208                 parse_xml_file(a);
209         }
210
211         report_dives();
212
213         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
214         g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
215         main_window = win;
216
217         vbox = gtk_vbox_new(FALSE, 0);
218         gtk_container_add(GTK_CONTAINER(win), vbox);
219
220         menubar = get_menubar_menu(win);
221         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
222
223         /* HPane for left the dive list, and right the dive info */
224         paned = gtk_hpaned_new();
225         gtk_box_pack_end(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
226
227         /* Create the actual divelist */
228         dive_list = dive_list_create();
229         gtk_paned_add1(GTK_PANED(paned), dive_list.container_widget);
230
231         /* VBox for dive info, and tabs */
232         info_box = gtk_vbox_new(FALSE, 6);
233         gtk_paned_add2(GTK_PANED(paned), info_box);
234
235         /* Frame for minimal dive info */
236         frame = dive_info_frame();
237         gtk_box_pack_start(GTK_BOX(info_box), frame, FALSE, TRUE, 6);
238
239         /* Notebook for dive info vs profile vs .. */
240         notebook = gtk_notebook_new();
241         gtk_box_pack_start(GTK_BOX(info_box), notebook, TRUE, TRUE, 6);
242
243         /* Frame for dive profile */
244         dive_profile = dive_profile_widget();
245         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_profile, gtk_label_new("Dive Profile"));
246
247         /* Frame for extended dive info */
248         dive_info = extended_dive_info_widget();
249         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dive_info, gtk_label_new("Extended Dive Info"));
250
251         gtk_widget_set_app_paintable(win, TRUE);
252         gtk_widget_show_all(win);
253
254         gtk_main();
255         return 0;
256 }