9 GtkWidget *main_window;
11 static int sortfn(const void *_a, const void *_b)
13 const struct dive *a = *(void **)_a;
14 const struct dive *b = *(void **)_b;
16 if (a->when < b->when)
18 if (a->when > b->when)
23 static int alloc_samples;
25 /* Don't pick a zero for MERGE_MIN() */
26 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
27 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
29 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
31 int nr = dive->samples;
34 if (nr >= alloc_samples) {
35 alloc_samples = (alloc_samples + 64) * 3 / 2;
36 dive = realloc(dive, dive_size(alloc_samples));
41 d = dive->sample + nr;
44 d->time.seconds = time;
49 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
51 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
53 int asamples = a->samples;
54 int bsamples = b->samples;
55 struct sample *as = a->sample;
56 struct sample *bs = b->sample;
65 at = asamples ? as->time.seconds : -1;
66 bt = bsamples ? bs->time.seconds + offset : -1;
68 /* No samples? All done! */
72 /* Only samples from a? */
75 res = add_sample(as, at, res);
81 /* Only samples from b? */
84 res = add_sample(bs, bt, res);
95 /* same-time sample: add a merged sample. Take the non-zero ones */
98 sample.depth = as->depth;
99 if (as->temperature.mkelvin)
100 sample.temperature = as->temperature;
101 if (as->tankpressure.mbar)
102 sample.tankpressure = as->tankpressure;
104 sample.tankindex = as->tankindex;
106 res = add_sample(&sample, at, res);
115 static char *merge_text(const char *a, const char *b)
125 res = malloc(strlen(a) + strlen(b) + 9);
128 sprintf(res, "(%s) or (%s)", a, b);
133 * This could do a lot more merging. Right now it really only
134 * merges almost exact duplicates - something that happens easily
135 * with overlapping dive downloads.
137 static struct dive *try_to_merge(struct dive *a, struct dive *b)
142 if (a->when != b->when)
146 res = malloc(dive_size(alloc_samples));
149 memset(res, 0, dive_size(alloc_samples));
152 res->name = merge_text(a->name, b->name);
153 res->location = merge_text(a->location, b->location);
154 res->notes = merge_text(a->notes, b->notes);
155 MERGE_MAX(res, a, b, maxdepth.mm);
156 MERGE_MAX(res, a, b, meandepth.mm); /* recalc! */
157 MERGE_MAX(res, a, b, duration.seconds);
158 MERGE_MAX(res, a, b, surfacetime.seconds);
159 MERGE_MAX(res, a, b, airtemp.mkelvin);
160 MERGE_MIN(res, a, b, watertemp.mkelvin);
161 MERGE_MAX(res, a, b, beginning_pressure.mbar);
162 MERGE_MAX(res, a, b, end_pressure.mbar);
163 for (i = 0; i < MAX_MIXES; i++) {
164 if (a->gasmix[i].o2.permille) {
165 res->gasmix[i] = a->gasmix[i];
168 res->gasmix[i] = b->gasmix[i];
170 return merge_samples(res, a, b, 0);
174 * This doesn't really report anything at all. We just sort the
175 * dives, the GUI does the reporting
177 static void report_dives(void)
181 qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
183 for (i = 1; i < dive_table.nr; i++) {
184 struct dive **pp = &dive_table.dives[i-1];
185 struct dive *prev = pp[0];
186 struct dive *dive = pp[1];
189 if (prev->when + prev->duration.seconds < dive->when)
192 merged = try_to_merge(prev, dive);
200 memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
202 /* Redo the new 'i'th dive */
207 static void parse_argument(const char *arg)
209 const char *p = arg+1;
217 fprintf(stderr, "Bad argument '%s'\n", arg);
223 static void on_destroy(GtkWidget* w, gpointer data)
228 static GtkWidget *dive_profile;
230 void repaint_dive(void)
232 update_dive_info(current_dive);
233 gtk_widget_queue_draw(dive_profile);
236 static char *existing_filename;
238 static void file_open(GtkWidget *w, gpointer data)
241 dialog = gtk_file_chooser_dialog_new("Open File",
242 GTK_WINDOW(main_window),
243 GTK_FILE_CHOOSER_ACTION_OPEN,
244 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
245 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
248 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
250 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
251 printf("Open: '%s'\n", filename);
254 gtk_widget_destroy(dialog);
257 static void file_save(GtkWidget *w, gpointer data)
260 dialog = gtk_file_chooser_dialog_new("Save File",
261 GTK_WINDOW(main_window),
262 GTK_FILE_CHOOSER_ACTION_SAVE,
263 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
264 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
266 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
267 if (!existing_filename) {
268 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
270 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename);
272 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
274 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
275 save_dives(filename);
278 gtk_widget_destroy(dialog);
281 static GtkItemFactoryEntry menu_items[] = {
282 { "/_File", NULL, NULL, 0, "<Branch>" },
283 { "/File/_Open", "<control>O", file_open, 0, "<StockItem>", GTK_STOCK_OPEN },
284 { "/File/_Save", "<control>S", file_save, 0, "<StockItem>", GTK_STOCK_SAVE },
286 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
288 /* This is just directly from the gtk menubar tutorial. */
289 static GtkWidget *get_menubar_menu(GtkWidget *window)
291 GtkItemFactory *item_factory;
292 GtkAccelGroup *accel_group;
294 accel_group = gtk_accel_group_new();
295 item_factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>", accel_group);
297 gtk_item_factory_create_items(item_factory, nmenu_items, menu_items, NULL);
298 gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
299 return gtk_item_factory_get_widget(item_factory, "<main>");
302 int main(int argc, char **argv)
315 gtk_init(&argc, &argv);
317 for (i = 1; i < argc; i++) {
318 const char *a = argv[i];
329 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
330 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(on_destroy), NULL);
333 vbox = gtk_vbox_new(FALSE, 0);
334 gtk_container_add(GTK_CONTAINER(win), vbox);
336 menubar = get_menubar_menu(win);
337 gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
339 /* Table for the list of dives, cairo window, and dive info */
340 table = gtk_table_new(2, 2, FALSE);
341 gtk_container_set_border_width(GTK_CONTAINER(table), 5);
342 gtk_box_pack_end(GTK_BOX(vbox), table, TRUE, TRUE, 0);
343 gtk_widget_show(table);
345 /* Create the atual divelist */
346 divelist = create_dive_list();
347 gtk_table_attach(GTK_TABLE(table), divelist, 0, 1, 0, 2,
348 0, GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0);
350 /* Frame for minimal dive info */
351 frame = dive_info_frame();
352 gtk_table_attach(GTK_TABLE(table), frame, 1, 2, 0, 1,
353 GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0, 0);
355 /* Notebook for dive info vs profile vs .. */
356 notebook = gtk_notebook_new();
357 gtk_table_attach_defaults(GTK_TABLE(table), notebook, 1, 2, 1, 2);
359 /* Frame for dive profile */
360 frame = dive_profile_frame();
361 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), frame, gtk_label_new("Dive Profile"));
362 dive_profile = frame;
364 /* Frame for extended dive info */
365 frame = extended_dive_info_frame();
366 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), frame, gtk_label_new("Extended dive Info"));
368 gtk_widget_set_app_paintable(win, TRUE);
369 gtk_widget_show_all(win);