10 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
13 * When showing dive profiles, we scale things to the
14 * current dive. However, we don't scale past less than
15 * 30 minutes or 90 ft, just so that small dives show
18 static int round_seconds_up(int seconds)
20 return MAX(30*60, ROUND_UP(seconds, 60*10));
23 static int round_feet_up(int feet)
25 return MAX(90, ROUND_UP(feet+5, 15));
28 /* Scale to 0,0 -> maxx,maxy */
29 #define SCALE(x,y) (x)*maxx/scalex+topx,(y)*maxy/scaley+topy
31 static void plot(cairo_t *cr, int w, int h, struct dive *dive, int samples, struct sample *sample)
34 double topx, topy, maxx, maxy;
35 double scalex, scaley;
36 int maxtime, maxdepth;
37 int begins, sec, depth;
44 cairo_set_line_width(cr, 2);
46 /* Get plot scaling limits */
47 maxtime = round_seconds_up(dive->duration.seconds);
48 maxdepth = round_feet_up(to_feet(dive->maxdepth));
50 /* Depth markers: every 15 ft */
53 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
54 for (i = 15; i < maxdepth; i += 15) {
55 cairo_move_to(cr, SCALE(0, i));
56 cairo_line_to(cr, SCALE(1, i));
59 /* Time markers: every 5 min */
62 for (i = 5*60; i < maxtime; i += 5*60) {
63 cairo_move_to(cr, SCALE(i, 0));
64 cairo_line_to(cr, SCALE(i, 1));
71 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
72 begins = sample->time.seconds;
73 cairo_move_to(cr, SCALE(sample->time.seconds, to_feet(sample->depth)));
74 for (i = 1; i < dive->samples; i++) {
76 sec = sample->time.seconds;
77 depth = to_feet(sample->depth);
78 cairo_line_to(cr, SCALE(sec, depth));
81 cairo_line_to(cr, SCALE(sec, 0));
82 cairo_line_to(cr, SCALE(begins, 0));
84 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.20);
85 cairo_fill_preserve(cr);
86 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
89 /* Bounding box last */
90 scalex = scaley = 1.0;
91 cairo_set_source_rgb(cr, 1, 1, 1);
92 cairo_move_to(cr, SCALE(0,0));
93 cairo_line_to(cr, SCALE(0,1));
94 cairo_line_to(cr, SCALE(1,1));
95 cairo_line_to(cr, SCALE(1,0));
101 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
103 struct dive *dive = current_dive;
107 w = widget->allocation.width;
108 h = widget->allocation.height;
110 cr = gdk_cairo_create(widget->window);
111 cairo_set_source_rgb(cr, 0, 0, 0);
114 if (dive && dive->samples)
115 plot(cr, w, h, dive, dive->samples, dive->sample);
122 GtkWidget *dive_profile_frame(void)
127 frame = gtk_frame_new("Dive profile");
128 gtk_widget_show(frame);
129 da = gtk_drawing_area_new();
130 gtk_widget_set_size_request(da, 450, 350);
131 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
132 gtk_container_add(GTK_CONTAINER(frame), da);