8 #include "display-gtk.h"
10 #define FONT_NORMAL (12)
11 #define FONT_SMALL (FONT_NORMAL / 1.2)
12 #define FONT_LARGE (FONT_NORMAL * 1.2)
14 static struct options print_options;
16 #define PRETTYOPTIONCALLBACK(name, option) \
17 static void name(GtkWidget *w, gpointer data) \
19 option = GTK_TOGGLE_BUTTON(w)->active; \
22 PRETTYOPTIONCALLBACK(print_profiles_toggle, print_options.print_profiles)
25 static void set_font(PangoLayout *layout, PangoFontDescription *font,
26 double size, int align)
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);
36 * You know what? Maybe somebody can do a real Pango layout thing.
39 static void show_dive_text(struct dive *dive, cairo_t *cr, double w,
40 double h, PangoFontDescription *font)
44 int len, decimals, width, height, maxwidth, maxheight;
47 char buffer[80], divenr[20], *people;
49 maxwidth = w * PANGO_SCALE;
50 maxheight = h * PANGO_SCALE * 0.9;
52 layout = pango_cairo_create_layout(cr);
53 pango_layout_set_width(layout, maxwidth);
54 pango_layout_set_height(layout, maxheight);
58 snprintf(divenr, sizeof(divenr), "Dive #%d - ", dive->number);
60 tm = gmtime(&dive->when);
61 len = snprintf(buffer, sizeof(buffer),
62 "%s%s, %s %d, %d %d:%02d",
65 monthname(tm->tm_mon),
66 tm->tm_mday, tm->tm_year + 1900,
67 tm->tm_hour, tm->tm_min);
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);
73 cairo_move_to(cr, 0, 0);
74 pango_cairo_show_layout(cr, layout);
77 if (!people || !*people) {
78 people = dive->divemaster;
83 depth = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
84 snprintf(buffer, sizeof(buffer),
85 "Max depth: %.*f %s\n"
88 decimals, depth, unit,
89 (dive->duration.seconds+59) / 60,
92 set_font(layout, font, FONT_SMALL, PANGO_ALIGN_RIGHT);
93 pango_layout_set_text(layout, buffer, -1);
95 cairo_move_to(cr, 0, 0);
96 pango_cairo_show_layout(cr, layout);
99 * Show the dive location
101 * .. or at least a space to get the size.
103 * Move down by the size of the date, and limit the
104 * width to the same width as the date string.
106 cairo_translate(cr, 0, height / (double) PANGO_SCALE);
108 pango_layout_set_height(layout, 1);
109 pango_layout_set_width(layout, width);
111 set_font(layout, font, FONT_NORMAL, PANGO_ALIGN_LEFT);
112 pango_layout_set_text(layout, dive->location ? : " ", -1);
114 cairo_move_to(cr, 0, 0);
115 pango_cairo_show_layout(cr, layout);
117 pango_layout_get_size(layout, &width, &height);
120 * Show the dive notes
123 /* Move down by the size of the location (x2) */
125 cairo_translate(cr, 0, height / (double) PANGO_SCALE);
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);
135 cairo_move_to(cr, 0, 0);
136 pango_cairo_show_layout(cr, layout);
138 g_object_unref(layout);
141 static void show_table_header(cairo_t *cr, double w, double h,
142 PangoFontDescription *font)
144 int len, width, height, maxwidth, maxheight;
148 maxwidth = w * PANGO_SCALE;
149 maxheight = h * PANGO_SCALE * 0.9;
151 layout = pango_cairo_create_layout(cr);
152 pango_layout_set_width(layout, maxwidth);
153 pango_layout_set_height(layout, maxheight);
155 len = snprintf(buffer, sizeof(buffer),
156 "Dive# - Date - Depth - Time - Master"
157 " Buddy -- Location");
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);
163 //cairo_move_to(cr, 0, 0);
164 pango_cairo_show_layout(cr, layout);
167 static void show_dive_table(struct dive *dive, cairo_t *cr, double w,
168 double h, PangoFontDescription *font)
172 int len, decimals, width, height, maxwidth, maxheight;
175 char buffer[160], divenr[20];
177 maxwidth = w * PANGO_SCALE;
178 maxheight = h * PANGO_SCALE * 0.9;
180 layout = pango_cairo_create_layout(cr);
181 pango_layout_set_width(layout, maxwidth);
182 pango_layout_set_height(layout, maxheight);
186 snprintf(divenr, sizeof(divenr), "#%d -", dive->number);
188 depth = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
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",
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,
201 (dive->duration.seconds+59) / 60,
202 dive->divemaster ? : " ",
204 dive->location ? : " "
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);
211 cairo_move_to(cr, 0, 0);
212 pango_cairo_show_layout(cr, layout);
215 //* Show the 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);
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);
230 cairo_move_to(cr, 0, 0);
231 pango_cairo_show_layout(cr, layout);
233 g_object_unref(layout);
236 static void show_dive_profile(struct dive *dive, cairo_t *cr, double w,
239 cairo_rectangle_int_t drawing_area = { w/20.0, h/20.0, w, h};
240 struct graphics_context gc = {
245 plot(&gc, &drawing_area, dive);
249 static void print(int divenr, cairo_t *cr, double x, double y, double w,
250 double h, PangoFontDescription *font)
254 dive = get_dive(divenr);
258 cairo_translate(cr, x, y);
260 /* Plus 5% on all sides */
261 cairo_translate(cr, w/20, h/20);
264 /* We actually want to scale the text and the lines now */
265 cairo_scale(cr, 0.5, 0.5);
267 /* Dive plot in the upper two thirds - note the scaling */
268 show_dive_profile(dive, cr, w*2, h*1.33);
270 /* Dive information in the lower third */
271 cairo_translate(cr, 0, h*1.33);
273 show_dive_text(dive, cr, w*2, h*0.67, font);
278 static void print_pretty_table(int divenr, cairo_t *cr, double x, double y,
279 double w, double h, PangoFontDescription *font)
283 dive = get_dive(divenr);
287 cairo_translate(cr, x, y);
289 /* Plus 5% on all sides */
290 cairo_translate(cr, w/20, h/20);
293 /* We actually want to scale the text and the lines now */
294 cairo_scale(cr, 0.5, 0.5);
296 show_dive_text(dive, cr, w*2, h*2, font);
301 static void print_table_header(cairo_t *cr, double x, double y,
302 double w, double h, PangoFontDescription *font)
305 cairo_translate(cr, x, y);
307 /* Plus 5% on all sides */
308 cairo_translate(cr, w/20, h/20);
311 /* We actually want to scale the text and the lines now */
312 cairo_scale(cr, 0.5, 0.5);
314 show_table_header(cr, w*2, h*2, font);
319 static void print_table(int divenr, cairo_t *cr, double x, double y,
320 double w, double h, PangoFontDescription *font)
324 dive = get_dive(divenr);
328 cairo_translate(cr, x, y);
330 /* Plus 5% on all sides */
331 cairo_translate(cr, w/20, h/20);
334 /* We actually want to scale the text and the lines now */
335 cairo_scale(cr, 0.5, 0.5);
337 show_dive_table(dive, cr, w*2, h*2, font);
342 static void draw_page(GtkPrintOperation *operation,
343 GtkPrintContext *context,
350 PangoFontDescription *font;
352 cr = gtk_print_context_get_cairo_context(context);
353 font = pango_font_description_from_string("Sans");
355 w = gtk_print_context_get_width(context)/2;
356 h = gtk_print_context_get_height(context)/3;
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);
366 pango_font_description_free(font);
369 static void draw_pretty_table(GtkPrintOperation *operation,
370 GtkPrintContext *context,
377 PangoFontDescription *font;
379 cr = gtk_print_context_get_cairo_context(context);
380 font = pango_font_description_from_string("Sans");
382 w = gtk_print_context_get_width(context);
383 h = gtk_print_context_get_height(context)/15;
387 for (i = 0; i < 15; i++) {
388 print_pretty_table(nr+i, cr, 0, 0+h*i, w, h, font);
391 pango_font_description_free(font);
394 static void draw_table(GtkPrintOperation *operation,
395 GtkPrintContext *context,
400 int n_dive_per_page = 25;
403 PangoFontDescription *font;
405 cr = gtk_print_context_get_cairo_context(context);
406 font = pango_font_description_from_string("Sans");
408 w = gtk_print_context_get_width(context);
409 h = gtk_print_context_get_height(context)/(n_dive_per_page+1);
411 nr = page_nr*n_dive_per_page;
412 print_table_header(cr, 0, 0+h, w, h, font);
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);
418 pango_font_description_free(font);
421 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
423 int dives_per_page = 1;
424 if (print_options.type == PRETTY) {
425 if (print_options.print_profiles){
434 pages = (dive_table.nr + dives_per_page - 1) / dives_per_page;
435 gtk_print_operation_set_n_pages(operation, pages);
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);
443 // type == pretty - enable the profile option
444 gtk_widget_set_sensitive(w, TRUE);
448 #define OPTIONCALLBACK(name, type, value) \
449 static void name(GtkWidget *w, gpointer data) \
451 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) \
452 print_options.type = value; \
453 update_print_window(data); \
456 OPTIONCALLBACK(set_pretty, type, PRETTY)
457 OPTIONCALLBACK(set_table, type, TABLE)
459 static GtkWidget *print_dialog(GtkPrintOperation *operation, gpointer user_data)
461 GtkWidget *vbox, *button, *radio1, *radio2, *frame, *box;
462 gtk_print_operation_set_custom_tab_label(operation, "Dive details");
464 vbox = gtk_vbox_new(TRUE, 5);
466 frame = gtk_frame_new("Print type");
467 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 1);
469 box = gtk_hbox_new(FALSE, 2);
470 gtk_container_add(GTK_CONTAINER(frame), box);
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);
483 frame = gtk_frame_new("Print options");
484 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 1);
486 box = gtk_hbox_new(FALSE, 3);
487 gtk_container_add(GTK_CONTAINER(frame), box);
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);
494 g_signal_connect(radio1, "toggled", G_CALLBACK(set_pretty), button);
495 g_signal_connect(radio2, "toggled", G_CALLBACK(set_table), button);
497 gtk_widget_show_all(vbox);
501 static void print_dialog_apply(GtkPrintOperation *operation, GtkWidget *widget, gpointer user_data)
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);
508 g_signal_connect(operation, "draw_page",
509 G_CALLBACK(draw_pretty_table), NULL);
512 g_signal_connect(operation, "draw_page",
513 G_CALLBACK(draw_table), NULL);
517 static GtkPrintSettings *settings = NULL;
521 GtkPrintOperation *print;
522 GtkPrintOperationResult res;
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));
539 g_object_unref(print);