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