]> git.tdb.fi Git - ext/subsurface.git/blob - profile.c
Print the end temperature of the dive
[ext/subsurface.git] / profile.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <string.h>
5 #include <time.h>
6
7 #include "dive.h"
8 #include "display.h"
9 #include "divelist.h"
10
11 int selected_dive = 0;
12
13 /* Plot info with smoothing and one-, two- and three-minute minimums and maximums */
14 struct plot_info {
15         int nr;
16         int maxtime;
17         struct plot_data {
18                 int sec;
19                 int val;
20                 int smoothed;
21                 struct plot_data *min[3];
22                 struct plot_data *max[3];
23                 int avg[3];
24         } entry[];
25 };
26 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
27
28 /* Scale to 0,0 -> maxx,maxy */
29 #define SCALEX(gc,x)  (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
30 #define SCALEY(gc,y)  (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
31 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
32
33 static void move_to(struct graphics_context *gc, double x, double y)
34 {
35         cairo_move_to(gc->cr, SCALE(gc, x, y));
36 }
37
38 static void line_to(struct graphics_context *gc, double x, double y)
39 {
40         cairo_line_to(gc->cr, SCALE(gc, x, y));
41 }
42
43 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
44 {
45         if (gc->printer) {
46                 /* Black is white and white is black */
47                 double sum = r+g+b;
48                 if (sum > 2)
49                         r = g = b = 0;
50                 else if (sum < 1)
51                         r = g = b = 1;
52         }
53         cairo_set_source_rgba(gc->cr, r, g, b, a);
54 }
55
56 static void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
57 {
58         set_source_rgba(gc, r, g, b, 1);
59 }
60
61 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
62
63 /*
64  * When showing dive profiles, we scale things to the
65  * current dive. However, we don't scale past less than
66  * 30 minutes or 90 ft, just so that small dives show
67  * up as such.
68  */
69 static int round_seconds_up(int seconds)
70 {
71         return MAX(30*60, ROUND_UP(seconds, 60*10));
72 }
73
74 static int round_depth_up(depth_t depth)
75 {
76         unsigned mm = depth.mm;
77         /* Minimum 30m */
78         return MAX(30000, ROUND_UP(mm+3000, 10000));
79 }
80
81 typedef struct {
82         int size;
83         double r,g,b;
84         enum {CENTER,LEFT} halign;
85         enum {MIDDLE,TOP,BOTTOM} valign;
86 } text_render_options_t;
87
88 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
89                       double x, double y, const char *fmt, ...)
90 {
91         cairo_t *cr = gc->cr;
92         cairo_text_extents_t extents;
93         double dx, dy;
94         char buffer[80];
95         va_list args;
96
97         va_start(args, fmt);
98         vsnprintf(buffer, sizeof(buffer), fmt, args);
99         va_end(args);
100
101         cairo_set_font_size(cr, tro->size);
102         cairo_text_extents(cr, buffer, &extents);
103         dx = 0;
104         switch (tro->halign) {
105         case CENTER:
106                 dx = -(extents.width/2 + extents.x_bearing);
107                 break;
108         case LEFT:
109                 dx = 0;
110                 break;
111         }
112         switch (tro->valign) {
113         case TOP:
114                 dy = extents.height * 1.2;
115                 break;
116         case BOTTOM:
117                 dy = -extents.height * 0.8;
118                 break;
119         case MIDDLE:
120                 dy = 0;
121                 break;
122         }
123
124         move_to(gc, x, y);
125         cairo_rel_move_to(cr, dx, dy);
126
127         cairo_text_path(cr, buffer);
128         set_source_rgb(gc, 0, 0, 0);
129         cairo_stroke(cr);
130
131         move_to(gc, x, y);
132         cairo_rel_move_to(cr, dx, dy);
133
134         set_source_rgb(gc, tro->r, tro->g, tro->b);
135         cairo_show_text(cr, buffer);
136 }
137
138 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
139 {
140         int sec = entry->sec;
141         depth_t depth = { entry->val };
142         const char *fmt;
143         double d;
144
145         switch (output_units.length) {
146         case METERS:
147                 d = depth.mm / 1000.0;
148                 fmt = "%.1f";
149                 break;
150         case FEET:
151                 d = to_feet(depth);
152                 fmt = "%.0f";
153                 break;
154         }
155         plot_text(gc, tro, sec, depth.mm, fmt, d);
156 }
157
158 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
159 {
160         static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
161         static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
162         int i;
163
164         for (i = 0; i < pi->nr; i++) {
165                 struct plot_data *entry = pi->entry + i;
166
167                 if (entry->val < 2000)
168                         continue;
169
170                 if (entry == entry->max[2])
171                         render_depth_sample(gc, entry, &deep);
172
173                 if (entry == entry->min[2])
174                         render_depth_sample(gc, entry, &shallow);
175         }
176 }
177
178 static void plot_depth_text(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
179 {
180         int maxtime, maxdepth;
181
182         /* Get plot scaling limits */
183         maxtime = round_seconds_up(dive->duration.seconds);
184         maxdepth = round_depth_up(dive->maxdepth);
185
186         gc->leftx = 0; gc->rightx = maxtime;
187         gc->topy = 0; gc->bottomy = maxdepth;
188
189         plot_text_samples(gc, pi);
190 }
191
192 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
193 {
194         int i;
195         struct plot_data *entry = pi->entry;
196
197         cairo_set_source_rgba(gc->cr, 1, 0.2, 0.2, 0.20);
198         move_to(gc, entry->sec, entry->smoothed);
199         for (i = 1; i < pi->nr; i++) {
200                 entry++;
201                 line_to(gc, entry->sec, entry->smoothed);
202         }
203         cairo_stroke(gc->cr);
204 }
205
206 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
207                                 int index, double a)
208 {
209         int i;
210         struct plot_data *entry = pi->entry;
211
212         cairo_set_source_rgba(gc->cr, 1, 0.2, 1, a);
213         move_to(gc, entry->sec, entry->min[index]->val);
214         for (i = 1; i < pi->nr; i++) {
215                 entry++;
216                 line_to(gc, entry->sec, entry->min[index]->val);
217         }
218         for (i = 1; i < pi->nr; i++) {
219                 line_to(gc, entry->sec, entry->max[index]->val);
220                 entry--;
221         }
222         cairo_close_path(gc->cr);
223         cairo_fill(gc->cr);
224 }
225
226 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
227 {
228         if (gc->printer)
229                 return;
230         plot_minmax_profile_minute(gc, pi, 2, 0.1);
231         plot_minmax_profile_minute(gc, pi, 1, 0.1);
232         plot_minmax_profile_minute(gc, pi, 0, 0.1);
233 }
234
235 static void plot_depth_profile(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
236 {
237         int i;
238         cairo_t *cr = gc->cr;
239         int begins, sec, depth;
240         struct plot_data *entry;
241         int maxtime, maxdepth, marker;
242
243         /* Get plot scaling limits */
244         maxtime = round_seconds_up(dive->duration.seconds);
245         maxdepth = round_depth_up(dive->maxdepth);
246
247         /* Time markers: every 5 min */
248         gc->leftx = 0; gc->rightx = maxtime;
249         gc->topy = 0; gc->bottomy = 1.0;
250         for (i = 5*60; i < maxtime; i += 5*60) {
251                 move_to(gc, i, 0);
252                 line_to(gc, i, 1);
253         }
254
255         /* Depth markers: every 30 ft or 10 m*/
256         gc->leftx = 0; gc->rightx = 1.0;
257         gc->topy = 0; gc->bottomy = maxdepth;
258         switch (output_units.length) {
259         case METERS: marker = 10000; break;
260         case FEET: marker = 9144; break;        /* 30 ft */
261         }
262
263         set_source_rgba(gc, 1, 1, 1, 0.5);
264         for (i = marker; i < maxdepth; i += marker) {
265                 move_to(gc, 0, i);
266                 line_to(gc, 1, i);
267         }
268         cairo_stroke(cr);
269
270         /* Show mean depth */
271         set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
272         move_to(gc, 0, dive->meandepth.mm);
273         line_to(gc, 1, dive->meandepth.mm);
274         cairo_stroke(cr);
275
276         gc->leftx = 0; gc->rightx = maxtime;
277
278         plot_smoothed_profile(gc, pi);
279         plot_minmax_profile(gc, pi);
280
281         entry = pi->entry;
282         set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
283         begins = entry->sec;
284         move_to(gc, entry->sec, entry->val);
285         for (i = 1; i < pi->nr; i++) {
286                 entry++;
287                 sec = entry->sec;
288                 if (sec <= maxtime) {
289                         depth = entry->val;
290                         line_to(gc, sec, depth);
291                 }
292         }
293         gc->topy = 0; gc->bottomy = 1.0;
294         line_to(gc, MIN(sec,maxtime), 0);
295         line_to(gc, begins, 0);
296         cairo_close_path(cr);
297         set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
298         cairo_fill_preserve(cr);
299         set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
300         cairo_stroke(cr);
301 }
302
303 static int setup_temperature_limits(struct dive *dive, struct graphics_context *gc)
304 {
305         int i;
306         int maxtime, mintemp, maxtemp;
307
308         /* Get plot scaling limits */
309         maxtime = round_seconds_up(dive->duration.seconds);
310         mintemp = INT_MAX;
311         maxtemp = 0;
312         for (i = 0; i < dive->samples; i++) {
313                 struct sample *sample = dive->sample+i;
314                 int mkelvin = sample->temperature.mkelvin;
315                 if (!mkelvin)
316                         continue;
317                 if (mkelvin > maxtemp)
318                         maxtemp = mkelvin;
319                 if (mkelvin < mintemp)
320                         mintemp = mkelvin;
321         }
322
323         gc->leftx = 0; gc->rightx = maxtime;
324         /* Show temperatures in roughly the lower third */
325         gc->topy = maxtemp + (maxtemp - mintemp)*2;
326         gc->bottomy = mintemp - (maxtemp - mintemp)/2;
327
328         return maxtemp > mintemp;
329 }
330
331 static void plot_single_temp_text(struct graphics_context *gc, int sec, temperature_t temperature)
332 {
333         int deg;
334         const char *unit;
335         static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
336
337         if (output_units.temperature == FAHRENHEIT) {
338                 deg = to_F(temperature);
339                 unit = "F";
340         } else {
341                 deg = to_C(temperature);
342                 unit = "C";
343         }
344         plot_text(gc, &tro, sec, temperature.mkelvin, "%d %s", deg, unit);
345 }
346
347 static void plot_temperature_text(struct dive *dive, struct graphics_context *gc)
348 {
349         int i;
350         int last = 0;
351         temperature_t last_temperature, last_printed_temp;
352
353         if (!setup_temperature_limits(dive, gc))
354                 return;
355
356         for (i = 0; i < dive->samples; i++) {
357                 struct sample *sample = dive->sample+i;
358                 if (sample->time.seconds > dive->duration.seconds)
359                         break; /* let's not plot surface temp events */
360                 int mkelvin = sample->temperature.mkelvin;
361                 int sec;
362                 if (!mkelvin)
363                         continue;
364                 last_temperature = sample->temperature;
365                 sec = sample->time.seconds;
366                 if (sec < last)
367                         continue;
368                 last = sec + 300;
369                 plot_single_temp_text(gc,sec,sample->temperature);
370                 last_printed_temp = last_temperature ;
371         }
372         /* it would be nice to print the end temperature, if it's different */
373         if (last_temperature.mkelvin != last_printed_temp.mkelvin) {
374                 plot_single_temp_text(gc,dive->duration.seconds,last_temperature);
375         }
376 }
377
378 static void plot_temperature_profile(struct dive *dive, struct graphics_context *gc)
379 {
380         int i;
381         cairo_t *cr = gc->cr;
382         int last = 0;
383
384         if (!setup_temperature_limits(dive, gc))
385                 return;
386
387         set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
388         for (i = 0; i < dive->samples; i++) {
389                 struct sample *sample = dive->sample+i;
390                 if (sample->time.seconds > dive->duration.seconds)
391                         break; /* let's not plot surface temp events */
392                 int mkelvin = sample->temperature.mkelvin;
393                 if (!mkelvin) {
394                         if (!last)
395                                 continue;
396                         mkelvin = last;
397                 }
398                 if (last)
399                         line_to(gc, sample->time.seconds, mkelvin);
400                 else
401                         move_to(gc, sample->time.seconds, mkelvin);
402                 last = mkelvin;
403         }
404         cairo_stroke(cr);
405 }
406
407 /* gets both the actual start and end pressure as well as the scaling factors */
408 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
409         pressure_t *startp, pressure_t *endp)
410 {
411         int i;
412         int min, max;
413
414         gc->leftx = 0; gc->rightx = round_seconds_up(dive->duration.seconds);
415
416         max = 0;
417         min = 5000000;
418         if (startp)
419                 startp->mbar = endp->mbar = 0;
420
421         for (i = 0; i < dive->samples; i++) {
422                 int mbar;
423                 struct sample *sample = dive->sample + i;
424
425                 /* FIXME! We only track cylinder 0 right now */
426                 if (sample->cylinderindex)
427                         continue;
428                 mbar = sample->cylinderpressure.mbar;
429                 if (!mbar)
430                         continue;
431                 if (mbar < min)
432                         min = mbar;
433                 if (mbar > max)
434                         max = mbar;
435         }
436         if (startp)
437                 startp->mbar = max;
438         if (endp)
439                 endp->mbar = min;
440         if (!max)
441                 return 0;
442         gc->topy = 0; gc->bottomy = max * 1.5;
443         return 1;
444 }
445
446 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc)
447 {
448         int i, sec = -1;
449
450         if (!get_cylinder_pressure_range(dive, gc, NULL, NULL))
451                 return;
452
453         cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
454
455         move_to(gc, 0, dive->cylinder[0].start.mbar);
456         for (i = 1; i < dive->samples; i++) {
457                 int mbar;
458                 struct sample *sample = dive->sample + i;
459
460                 mbar = sample->cylinderpressure.mbar;
461                 if (!mbar)
462                         continue;
463                 sec = sample->time.seconds;
464                 if (sec <= dive->duration.seconds)
465                         line_to(gc, sec, mbar);
466         }
467         /*
468          * We may have "surface time" events, in which case we don't go
469          * back to dive duration
470          */
471         if (sec < dive->duration.seconds)
472                 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
473         cairo_stroke(gc->cr);
474 }
475
476 /*
477  * Return air usage (in liters).
478  */
479 static double calculate_airuse(struct dive *dive)
480 {
481         double airuse = 0;
482         int i;
483
484         for (i = 0; i < MAX_CYLINDERS; i++) {
485                 cylinder_t *cyl = dive->cylinder + i;
486                 int size = cyl->type.size.mliter;
487                 double kilo_atm;
488
489                 if (!size)
490                         continue;
491
492                 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
493
494                 /* Liters of air at 1 atm == milliliters at 1k atm*/
495                 airuse += kilo_atm * size;
496         }
497         return airuse;
498 }
499
500 static void plot_info(struct dive *dive, struct graphics_context *gc)
501 {
502         text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
503         const double liters_per_cuft = 28.317;
504         const char *unit, *desc;
505         double airuse;
506
507         airuse = calculate_airuse(dive);
508         if (!airuse)
509                 return;
510
511         /* I really need to start addign some unit setting thing */
512         switch (output_units.volume) {
513         case LITER:
514                 unit = "l";
515                 break;
516         case CUFT:
517                 unit = "cuft";
518                 airuse /= liters_per_cuft;
519                 break;
520         }
521         plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
522         if (dive->duration.seconds) {
523                 double pressure = 1 + (dive->meandepth.mm / 10000.0);
524                 double sac = airuse / pressure * 60 / dive->duration.seconds;
525                 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
526         }
527         desc = dive->cylinder[0].type.description;
528         if (desc || dive->cylinder[0].gasmix.o2.permille) {
529                 int o2 = dive->cylinder[0].gasmix.o2.permille / 10;
530                 if (!desc)
531                         desc = "";
532                 if (!o2)
533                         o2 = 21;
534                 plot_text(gc, &tro, 0.8, 0.9, "%s (%d%%)", desc, o2);
535         }
536 }
537
538 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc)
539 {
540         pressure_t startp, endp;
541
542         if (get_cylinder_pressure_range(dive, gc, &startp, &endp)) {
543                 int start, end;
544                 const char *unit = "bar";
545
546                 switch (output_units.pressure) {
547                 case PASCAL:
548                         start = startp.mbar * 100;
549                         end = startp.mbar * 100;
550                         unit = "pascal";
551                         break;
552                 case BAR:
553                         start = (startp.mbar + 500) / 1000;
554                         end = (endp.mbar + 500) / 1000;
555                         unit = "bar";
556                         break;
557                 case PSI:
558                         start = to_PSI(startp);
559                         end = to_PSI(endp);
560                         unit = "psi";
561                         break;
562                 }
563
564                 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
565                 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
566                 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
567                           "%d %s", end, unit);
568         }
569 }
570
571 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
572 {
573         struct plot_data *p = entry;
574         int time = entry->sec;
575         int seconds = 90*(index+1);
576         struct plot_data *min, *max;
577         int avg, nr;
578
579         /* Go back 'seconds' in time */
580         while (p > first) {
581                 if (p[-1].sec < time - seconds)
582                         break;
583                 p--;
584         }
585
586         /* Then go forward until we hit an entry past the time */
587         min = max = p;
588         avg = p->val;
589         nr = 1;
590         while (++p < last) {
591                 int val = p->val;
592                 if (p->sec > time + seconds)
593                         break;
594                 avg += val;
595                 nr ++;
596                 if (val < min->val)
597                         min = p;
598                 if (val > max->val)
599                         max = p;
600         }
601         entry->min[index] = min;
602         entry->max[index] = max;
603         entry->avg[index] = (avg + nr/2) / nr;
604 }
605
606 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
607 {
608         analyze_plot_info_minmax_minute(entry, first, last, 0);
609         analyze_plot_info_minmax_minute(entry, first, last, 1);
610         analyze_plot_info_minmax_minute(entry, first, last, 2);
611 }
612
613 static struct plot_info *analyze_plot_info(struct plot_info *pi)
614 {
615         int i;
616         int nr = pi->nr;
617
618         /* Smoothing function: 5-point triangular smooth */
619         for (i = 2; i < nr-2; i++) {
620                 struct plot_data *entry = pi->entry+i;
621                 int val;
622
623                 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
624                 entry->smoothed = (val+4) / 9;
625         }
626
627         /* One-, two- and three-minute minmax data */
628         for (i = 0; i < nr; i++) {
629                 struct plot_data *entry = pi->entry +i;
630                 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
631         }
632         
633         return pi;
634 }
635
636 /*
637  * Create a plot-info with smoothing and ranged min/max
638  *
639  * This also makes sure that we have extra empty events on both
640  * sides, so that you can do end-points without having to worry
641  * about it.
642  */
643 static struct plot_info *depth_plot_info(struct dive *dive)
644 {
645         int i, nr = dive->samples + 4, sec;
646         size_t alloc_size = plot_info_size(nr);
647         struct plot_info *pi;
648
649         pi = malloc(alloc_size);
650         if (!pi)
651                 return pi;
652         memset(pi, 0, alloc_size);
653         pi->nr = nr;
654         sec = 0;
655         for (i = 0; i < dive->samples; i++) {
656                 struct sample *sample = dive->sample+i;
657                 struct plot_data *entry = pi->entry + i + 2;
658
659                 sec = entry->sec = sample->time.seconds;
660                 entry->val = sample->depth.mm;
661         }
662         /* Fill in the last two entries with empty values but valid times */
663         i = dive->samples + 2;
664         pi->entry[i].sec = sec + 20;
665         pi->entry[i+1].sec = sec + 40;
666
667         return analyze_plot_info(pi);
668 }
669
670 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
671 {
672         double topx, topy;
673         struct plot_info *pi = depth_plot_info(dive);
674
675         topx = w / 20.0;
676         topy = h / 20.0;
677         cairo_translate(gc->cr, topx, topy);
678         cairo_set_line_width(gc->cr, 2);
679         cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
680         cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
681
682         /*
683          * We can use "cairo_translate()" because that doesn't
684          * scale line width etc. But the actual scaling we need
685          * do set up ourselves..
686          *
687          * Snif. What a pity.
688          */
689         gc->maxx = (w - 2*topx);
690         gc->maxy = (h - 2*topy);
691
692         /* Temperature profile */
693         plot_temperature_profile(dive, gc);
694
695         /* Cylinder pressure plot */
696         plot_cylinder_pressure(dive, gc);
697
698         /* Depth profile */
699         plot_depth_profile(dive, gc, pi);
700
701         /* Text on top of all graphs.. */
702         plot_temperature_text(dive, gc);
703         plot_depth_text(dive, gc, pi);
704         plot_cylinder_pressure_text(dive, gc);
705
706         /* And info box in the lower right corner.. */
707         gc->leftx = 0; gc->rightx = 1.0;
708         gc->topy = 0; gc->bottomy = 1.0;
709         plot_info(dive, gc);
710
711         /* Bounding box last */
712         set_source_rgb(gc, 1, 1, 1);
713         move_to(gc, 0, 0);
714         line_to(gc, 0, 1);
715         line_to(gc, 1, 1);
716         line_to(gc, 1, 0);
717         cairo_close_path(gc->cr);
718         cairo_stroke(gc->cr);
719
720 }
721
722 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
723 {
724         struct dive *dive = current_dive;
725         struct graphics_context gc = { .printer = 0 };
726         int w,h;
727
728         w = widget->allocation.width;
729         h = widget->allocation.height;
730
731         gc.cr = gdk_cairo_create(widget->window);
732         set_source_rgb(&gc, 0, 0, 0);
733         cairo_paint(gc.cr);
734
735         if (dive)
736                 plot(&gc, w, h, dive);
737
738         cairo_destroy(gc.cr);
739
740         return FALSE;
741 }
742
743 GtkWidget *dive_profile_widget(void)
744 {
745         GtkWidget *da;
746
747         da = gtk_drawing_area_new();
748         gtk_widget_set_size_request(da, 350, 250);
749         g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
750
751         return da;
752 }