]> git.tdb.fi Git - ext/subsurface.git/blob - main.c
Create a gtk window
[ext/subsurface.git] / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <gtk/gtk.h>
5 #include <gdk/gdk.h>
6 #include <cairo.h>
7
8 #include "dive.h"
9
10 static void show_dive(int nr, struct dive *dive)
11 {
12         int i;
13         struct tm *tm;
14
15         tm = gmtime(&dive->when);
16
17         printf("At %02d:%02d:%02d %04d-%02d-%02d  (%d ft max, %d minutes)\n",
18                 tm->tm_hour, tm->tm_min, tm->tm_sec,
19                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
20                 to_feet(dive->maxdepth), dive->duration.seconds / 60);
21
22         if (!verbose)
23                 return;
24
25         for (i = 0; i < dive->samples; i++) {
26                 struct sample *s = dive->sample + i;
27
28                 printf("%4d:%02d: %3d ft, %2d C, %4d PSI\n",
29                         s->time.seconds / 60,
30                         s->time.seconds % 60,
31                         to_feet(s->depth),
32                         to_C(s->temperature),
33                         to_PSI(s->tankpressure));
34         }
35 }
36
37 static int sortfn(const void *_a, const void *_b)
38 {
39         const struct dive *a = *(void **)_a;
40         const struct dive *b = *(void **)_b;
41
42         if (a->when < b->when)
43                 return -1;
44         if (a->when > b->when)
45                 return 1;
46         return 0;
47 }
48
49 /*
50  * This doesn't really report anything at all. We just sort the
51  * dives, the GUI does the reporting
52  */
53 static void report_dives(void)
54 {
55         qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
56 }
57
58 static void parse_argument(const char *arg)
59 {
60         const char *p = arg+1;
61
62         do {
63                 switch (*p) {
64                 case 'v':
65                         verbose++;
66                         continue;
67                 default:
68                         fprintf(stderr, "Bad argument '%s'\n", arg);
69                         exit(1);
70                 }
71         } while (*++p);
72 }
73
74 static void on_destroy(GtkWidget* w, gpointer data)
75 {
76         gtk_main_quit();
77 }
78
79 static gboolean on_expose(GtkWidget* w, GdkEventExpose* e, gpointer data)
80 {
81         cairo_t* cr;
82         cr = gdk_cairo_create(w->window);
83         cairo_destroy(cr);
84         return FALSE;
85 }
86
87 int main(int argc, char **argv)
88 {
89         int i;
90         GtkWidget* win;
91
92         parse_xml_init();
93
94         gtk_init(&argc, &argv);
95
96         for (i = 1; i < argc; i++) {
97                 const char *a = argv[i];
98
99                 if (a[0] == '-') {
100                         parse_argument(a);
101                         continue;
102                 }
103                 parse_xml_file(a);
104         }
105
106         report_dives();
107
108         win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
109         g_signal_connect(G_OBJECT(win), "destroy",      G_CALLBACK(on_destroy), NULL);
110         g_signal_connect(G_OBJECT(win), "expose-event", G_CALLBACK(on_expose), NULL);
111         gtk_widget_set_app_paintable(win, TRUE);
112         gtk_widget_show_all(win);
113
114         gtk_main();
115         return 0;
116 }
117