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