]> git.tdb.fi Git - ext/subsurface.git/blob - print.c
Fix profile and average depth for freedives
[ext/subsurface.git] / print.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdarg.h>
4 #include <gtk/gtk.h>
5
6 #include "dive.h"
7 #include "display.h"
8 #include "display-gtk.h"
9
10 #define FONT_NORMAL (12)
11 #define FONT_SMALL (FONT_NORMAL / 1.2)
12 #define FONT_LARGE (FONT_NORMAL * 1.2)
13
14 static struct options print_options;
15
16 #define PRETTYOPTIONCALLBACK(name, option) \
17 static void name(GtkWidget *w, gpointer data) \
18 { \
19         option = GTK_TOGGLE_BUTTON(w)->active; \
20 }
21
22 PRETTYOPTIONCALLBACK(print_profiles_toggle, print_options.print_profiles)
23
24
25 static void set_font(PangoLayout *layout, PangoFontDescription *font,
26         double size, int align)
27 {
28         pango_font_description_set_size(font, size * PANGO_SCALE);
29         pango_layout_set_font_description(layout, font);
30         pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
31         pango_layout_set_alignment(layout, align);
32
33 }
34
35 /*
36  * You know what? Maybe somebody can do a real Pango layout thing.
37  * This is hacky.
38  */
39 static void show_dive_text(struct dive *dive, cairo_t *cr, double w,
40         double h, PangoFontDescription *font)
41 {
42         double depth;
43         const char *unit;
44         int len, decimals, width, height, maxwidth, maxheight;
45         PangoLayout *layout;
46         struct tm *tm;
47         char buffer[80], divenr[20], *people;
48
49         maxwidth = w * PANGO_SCALE;
50         maxheight = h * PANGO_SCALE * 0.9;
51
52         layout = pango_cairo_create_layout(cr);
53         pango_layout_set_width(layout, maxwidth);
54         pango_layout_set_height(layout, maxheight);
55
56         *divenr = 0;
57         if (dive->number)
58                 snprintf(divenr, sizeof(divenr), "Dive #%d - ", dive->number);
59
60         tm = gmtime(&dive->when);
61         len = snprintf(buffer, sizeof(buffer),
62                 "%s%s, %s %d, %d   %d:%02d",
63                 divenr,
64                 weekday(tm->tm_wday),
65                 monthname(tm->tm_mon),
66                 tm->tm_mday, tm->tm_year + 1900,
67                 tm->tm_hour, tm->tm_min);
68
69         set_font(layout, font, FONT_LARGE, PANGO_ALIGN_LEFT);
70         pango_layout_set_text(layout, buffer, len);
71         pango_layout_get_size(layout, &width, &height);
72
73         cairo_move_to(cr, 0, 0);
74         pango_cairo_show_layout(cr, layout);
75
76         people = dive->buddy;
77         if (!people || !*people) {
78                 people = dive->divemaster;
79                 if (!people)
80                         people = "";
81         }
82
83         depth = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
84         snprintf(buffer, sizeof(buffer),
85                 "Max depth: %.*f %s\n"
86                 "Duration: %d min\n"
87                 "%s",
88                 decimals, depth, unit,
89                 (dive->duration.seconds+59) / 60,
90                 people);
91
92         set_font(layout, font, FONT_SMALL, PANGO_ALIGN_RIGHT);
93         pango_layout_set_text(layout, buffer, -1);
94
95         cairo_move_to(cr, 0, 0);
96         pango_cairo_show_layout(cr, layout);
97
98         /*
99          * Show the dive location
100          *
101          * .. or at least a space to get the size.
102          *
103          * Move down by the size of the date, and limit the
104          * width to the same width as the date string.
105          */
106         cairo_translate(cr, 0, height / (double) PANGO_SCALE);
107         maxheight -= height;
108         pango_layout_set_height(layout, 1);
109         pango_layout_set_width(layout, width);
110
111         set_font(layout, font, FONT_NORMAL, PANGO_ALIGN_LEFT);
112         pango_layout_set_text(layout, dive->location ? : " ", -1);
113
114         cairo_move_to(cr, 0, 0);
115         pango_cairo_show_layout(cr, layout);
116
117         pango_layout_get_size(layout, &width, &height);
118
119         /*
120          * Show the dive notes
121          */
122         if (dive->notes) {
123                 /* Move down by the size of the location (x2) */
124                 height = height * 2;
125                 cairo_translate(cr, 0, height / (double) PANGO_SCALE);
126                 maxheight -= height;
127
128                 /* Use the full width and remaining height for notes */
129                 pango_layout_set_height(layout, maxheight);
130                 pango_layout_set_width(layout, maxwidth);
131                 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
132                 pango_layout_set_justify(layout, 1);
133                 pango_layout_set_text(layout, dive->notes, -1);
134
135                 cairo_move_to(cr, 0, 0);
136                 pango_cairo_show_layout(cr, layout);
137         }
138         g_object_unref(layout);
139 }
140
141 static void show_table_header(cairo_t *cr, double w, double h,
142     PangoFontDescription *font)
143 {
144         int len, width, height, maxwidth, maxheight;
145         PangoLayout *layout;
146         char buffer[160];
147
148         maxwidth = w * PANGO_SCALE;
149         maxheight = h * PANGO_SCALE * 0.9;
150
151         layout = pango_cairo_create_layout(cr);
152         pango_layout_set_width(layout, maxwidth);
153         pango_layout_set_height(layout, maxheight);
154
155         len = snprintf(buffer, sizeof(buffer),
156                 "Dive# -  Date              - Depth - Time - Master"
157         " Buddy -- Location");
158
159         set_font(layout, font, FONT_LARGE, PANGO_ALIGN_LEFT);
160         pango_layout_set_text(layout, buffer, len);
161         pango_layout_get_size(layout, &width, &height);
162
163         //cairo_move_to(cr, 0, 0);
164         pango_cairo_show_layout(cr, layout);
165 }
166
167 static void show_dive_table(struct dive *dive, cairo_t *cr, double w,
168         double h, PangoFontDescription *font)
169 {
170         double depth;
171         const char *unit;
172         int len, decimals, width, height, maxwidth, maxheight;
173         PangoLayout *layout;
174         struct tm *tm;
175         char buffer[160], divenr[20];
176
177         maxwidth = w * PANGO_SCALE;
178         maxheight = h * PANGO_SCALE * 0.9;
179
180         layout = pango_cairo_create_layout(cr);
181         pango_layout_set_width(layout, maxwidth);
182         pango_layout_set_height(layout, maxheight);
183
184         *divenr = 0;
185         if (dive->number)
186                 snprintf(divenr, sizeof(divenr), "#%d -", dive->number);
187
188         depth = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
189
190         tm = gmtime(&dive->when);
191         len = snprintf(buffer, sizeof(buffer),
192                 "%s %s, %s %d, %d   %dh%02d  - %.*f %s - %d min - %s %s --  %s",
193                 divenr,
194                 weekday(tm->tm_wday),
195                 monthname(tm->tm_mon),
196                 tm->tm_mday, tm->tm_year + 1900,
197                 tm->tm_hour, tm->tm_min,
198                 decimals,
199                 depth,
200                 unit,
201                 (dive->duration.seconds+59) / 60,
202                 dive->divemaster ? : " ",
203                 dive->buddy ? : " ",
204                 dive->location ? : " "
205                 );
206
207         set_font(layout, font, FONT_NORMAL, PANGO_ALIGN_LEFT);
208         pango_layout_set_text(layout, buffer, len);
209         pango_layout_get_size(layout, &width, &height);
210
211         cairo_move_to(cr, 0, 0);
212         pango_cairo_show_layout(cr, layout);
213
214         ///*
215          //* Show the dive notes
216          //*/
217         if (dive->notes) {
218                 /* Move down by the size of the location (x2) */
219                 height = height * 1.3;
220                 cairo_translate(cr, 20, height / (double) PANGO_SCALE);
221                 maxheight -= height;
222
223                 /* Use the full width and remaining height for notes */
224                 pango_layout_set_height(layout, maxheight);
225                 pango_layout_set_width(layout, maxwidth);
226                 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
227                 pango_layout_set_justify(layout, 1);
228                 pango_layout_set_text(layout, dive->notes, -1);
229
230                 cairo_move_to(cr, 0, 0);
231                 pango_cairo_show_layout(cr, layout);
232         }
233         g_object_unref(layout);
234 }
235
236 static void show_dive_profile(struct dive *dive, cairo_t *cr, double w,
237         double h)
238 {
239         cairo_rectangle_int_t drawing_area = { w/20.0, h/20.0, w, h};
240         struct graphics_context gc = {
241                 .printer = 1,
242                 .cr = cr
243         };
244         cairo_save(cr);
245         plot(&gc, &drawing_area, dive);
246         cairo_restore(cr);
247 }
248
249 static void print(int divenr, cairo_t *cr, double x, double y, double w,
250         double h, PangoFontDescription *font)
251 {
252         struct dive *dive;
253
254         dive = get_dive(divenr);
255         if (!dive)
256                 return;
257         cairo_save(cr);
258         cairo_translate(cr, x, y);
259
260         /* Plus 5% on all sides */
261         cairo_translate(cr, w/20, h/20);
262         w *= 0.9; h *= 0.9;
263
264         /* We actually want to scale the text and the lines now */
265         cairo_scale(cr, 0.5, 0.5);
266
267         /* Dive plot in the upper two thirds - note the scaling */
268         show_dive_profile(dive, cr, w*2, h*1.33);
269
270         /* Dive information in the lower third */
271         cairo_translate(cr, 0, h*1.33);
272
273         show_dive_text(dive, cr, w*2, h*0.67, font);
274
275         cairo_restore(cr);
276 }
277
278 static void print_pretty_table(int divenr, cairo_t *cr, double x, double y,
279         double w, double h, PangoFontDescription *font)
280 {
281         struct dive *dive;
282
283         dive = get_dive(divenr);
284         if (!dive)
285                 return;
286         cairo_save(cr);
287         cairo_translate(cr, x, y);
288
289         /* Plus 5% on all sides */
290         cairo_translate(cr, w/20, h/20);
291         w *= 0.9; h *= 0.9;
292
293         /* We actually want to scale the text and the lines now */
294         cairo_scale(cr, 0.5, 0.5);
295
296         show_dive_text(dive, cr, w*2, h*2, font);
297
298         cairo_restore(cr);
299 }
300
301 static void print_table_header(cairo_t *cr, double x, double y,
302         double w, double h, PangoFontDescription *font)
303 {
304     cairo_save(cr);
305         cairo_translate(cr, x, y);
306
307         /* Plus 5% on all sides */
308         cairo_translate(cr, w/20, h/20);
309         w *= 0.9; h *= 0.9;
310
311         /* We actually want to scale the text and the lines now */
312         cairo_scale(cr, 0.5, 0.5);
313
314         show_table_header(cr, w*2, h*2, font);
315
316         cairo_restore(cr);
317 }
318
319 static void print_table(int divenr, cairo_t *cr, double x, double y,
320         double w, double h, PangoFontDescription *font)
321 {
322         struct dive *dive;
323
324         dive = get_dive(divenr);
325         if (!dive)
326                 return;
327         cairo_save(cr);
328         cairo_translate(cr, x, y);
329
330         /* Plus 5% on all sides */
331         cairo_translate(cr, w/20, h/20);
332         w *= 0.9; h *= 0.9;
333
334         /* We actually want to scale the text and the lines now */
335         cairo_scale(cr, 0.5, 0.5);
336
337         show_dive_table(dive, cr, w*2, h*2, font);
338
339         cairo_restore(cr);
340 }
341
342 static void draw_page(GtkPrintOperation *operation,
343                         GtkPrintContext *context,
344                         gint page_nr,
345                         gpointer user_data)
346 {
347         int nr;
348         cairo_t *cr;
349         double w, h;
350         PangoFontDescription *font;
351
352         cr = gtk_print_context_get_cairo_context(context);
353         font = pango_font_description_from_string("Sans");
354
355         w = gtk_print_context_get_width(context)/2;
356         h = gtk_print_context_get_height(context)/3;
357
358         nr = page_nr*6;
359         print(nr+0, cr, 0,   0, w, h, font);
360         print(nr+1, cr, w,   0, w, h, font);
361         print(nr+2, cr, 0,   h, w, h, font);
362         print(nr+3, cr, w,   h, w, h, font);
363         print(nr+4, cr, 0, 2*h, w, h, font);
364         print(nr+5, cr, w, 2*h, w, h, font);
365
366         pango_font_description_free(font);
367 }
368
369 static void draw_pretty_table(GtkPrintOperation *operation,
370                         GtkPrintContext *context,
371                         gint page_nr,
372                         gpointer user_data)
373 {
374         int nr;
375         cairo_t *cr;
376         double w, h;
377         PangoFontDescription *font;
378
379         cr = gtk_print_context_get_cairo_context(context);
380         font = pango_font_description_from_string("Sans");
381
382         w = gtk_print_context_get_width(context);
383         h = gtk_print_context_get_height(context)/15;
384
385         nr = page_nr*15;
386         int i;
387         for (i = 0; i < 15; i++) {
388                 print_pretty_table(nr+i, cr, 0,   0+h*i, w, h, font);
389         }
390
391         pango_font_description_free(font);
392 }
393
394 static void draw_table(GtkPrintOperation *operation,
395                         GtkPrintContext *context,
396                         gint page_nr,
397                         gpointer user_data)
398 {
399         int nr;
400         int n_dive_per_page = 25;
401         cairo_t *cr;
402         double w, h;
403         PangoFontDescription *font;
404
405         cr = gtk_print_context_get_cairo_context(context);
406         font = pango_font_description_from_string("Sans");
407
408         w = gtk_print_context_get_width(context);
409         h = gtk_print_context_get_height(context)/(n_dive_per_page+1);
410
411         nr = page_nr*n_dive_per_page;
412     print_table_header(cr, 0, 0+h, w, h, font);
413         int i;
414         for (i = 0; i < n_dive_per_page; i++) {
415                 print_table(nr+i, cr, 0,   h*1.5+h*i, w, h, font);
416         }
417
418         pango_font_description_free(font);
419 }
420
421 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
422 {
423         int dives_per_page = 1;
424         if (print_options.type == PRETTY) {
425                 if (print_options.print_profiles){
426                         dives_per_page = 6;
427                 } else {
428                         dives_per_page = 15;
429                 }
430         } else {
431                 dives_per_page = 25;
432         }
433         int pages;
434         pages = (dive_table.nr + dives_per_page - 1) / dives_per_page;
435                 gtk_print_operation_set_n_pages(operation, pages);
436 }
437
438 static void update_print_window(GtkWidget *w) {
439         if (print_options.type == TABLE) {
440                 // type == table - disable the profile option
441                 gtk_widget_set_sensitive(w, FALSE);
442         } else {
443                 // type == pretty - enable the profile option
444                 gtk_widget_set_sensitive(w, TRUE);
445         }
446 }
447
448 #define OPTIONCALLBACK(name, type, value) \
449 static void name(GtkWidget *w, gpointer data) \
450 {\
451         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
452                 print_options.type = value; \
453         update_print_window(data); \
454 }
455
456 OPTIONCALLBACK(set_pretty, type, PRETTY)
457 OPTIONCALLBACK(set_table, type, TABLE)
458
459 static GtkWidget *print_dialog(GtkPrintOperation *operation, gpointer user_data)
460 {
461         GtkWidget *vbox, *button, *radio1, *radio2, *frame, *box;
462         gtk_print_operation_set_custom_tab_label(operation, "Dive details");
463
464         vbox = gtk_vbox_new(TRUE, 5);
465
466         frame = gtk_frame_new("Print type");
467         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 1);
468
469         box = gtk_hbox_new(FALSE, 2);
470         gtk_container_add(GTK_CONTAINER(frame), box);
471
472         radio1 = gtk_radio_button_new_with_label (NULL, "Pretty print");
473         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio1),
474                 print_options.type == PRETTY);
475         radio2 = gtk_radio_button_new_with_label_from_widget (
476                 GTK_RADIO_BUTTON (radio1), "Table print");
477         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio2),
478                 print_options.type == TABLE);
479         gtk_box_pack_start (GTK_BOX (box), radio1, TRUE, TRUE, 0);
480         gtk_box_pack_start (GTK_BOX (box), radio2, TRUE, TRUE, 0);
481
482
483         frame = gtk_frame_new("Print options");
484         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 1);
485
486         box = gtk_hbox_new(FALSE, 3);
487         gtk_container_add(GTK_CONTAINER(frame), box);
488
489         button = gtk_check_button_new_with_label("Show profiles");
490         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), print_options.print_profiles);
491         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 2);
492         g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(print_profiles_toggle), NULL);
493
494         g_signal_connect(radio1, "toggled", G_CALLBACK(set_pretty), button);
495         g_signal_connect(radio2, "toggled", G_CALLBACK(set_table), button);
496
497         gtk_widget_show_all(vbox);
498         return vbox;
499 }
500
501 static void print_dialog_apply(GtkPrintOperation *operation, GtkWidget *widget, gpointer user_data)
502 {
503         if (print_options.type == PRETTY) {
504                 if (print_options.print_profiles){
505                         g_signal_connect(operation, "draw_page",
506                                 G_CALLBACK(draw_page), NULL);
507                 } else {
508                         g_signal_connect(operation, "draw_page",
509                                 G_CALLBACK(draw_pretty_table), NULL);
510                 }
511         } else {
512                 g_signal_connect(operation, "draw_page",
513                         G_CALLBACK(draw_table), NULL);
514         }
515 }
516
517 static GtkPrintSettings *settings = NULL;
518
519 void do_print(void)
520 {
521         GtkPrintOperation *print;
522         GtkPrintOperationResult res;
523
524         repaint_dive();
525         print = gtk_print_operation_new();
526         gtk_print_operation_set_unit(print, GTK_UNIT_POINTS);
527         if (settings != NULL)
528                 gtk_print_operation_set_print_settings(print, settings);
529         g_signal_connect(print, "create-custom-widget", G_CALLBACK(print_dialog), NULL);
530         g_signal_connect(print, "custom-widget-apply", G_CALLBACK(print_dialog_apply), NULL);
531         g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
532         res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
533                                          GTK_WINDOW(main_window), NULL);
534         if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
535                 if (settings != NULL)
536                         g_object_unref(settings);
537                 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
538         }
539         g_object_unref(print);
540 }