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