8 #include "display-gtk.h"
11 * You know what? Maybe somebody can do a real Pango layout thing.
14 static void show_dive_text(struct dive *dive, cairo_t *cr, double w, double h, PangoFontDescription *font)
16 int len, width, height, maxwidth, maxheight;
19 char buffer[1024], divenr[20];
21 maxwidth = w * PANGO_SCALE;
22 maxheight = h * PANGO_SCALE * 0.9;
24 layout = pango_cairo_create_layout(cr);
25 pango_layout_set_font_description(layout, font);
26 pango_layout_set_width(layout, maxwidth);
27 pango_layout_set_height(layout, maxheight);
28 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
32 snprintf(divenr, sizeof(divenr), "Dive #%d - ", dive->number);
35 tm = gmtime(&dive->when);
36 len = snprintf(buffer, sizeof(buffer),
37 "<span size=\"large\">"
38 "%s%s, %s %d, %d %d:%02d"
42 monthname(tm->tm_mon),
43 tm->tm_mday, tm->tm_year + 1900,
44 tm->tm_hour, tm->tm_min);
46 pango_layout_set_justify(layout, 1);
47 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
48 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
50 pango_layout_set_markup(layout, buffer, len);
51 pango_layout_get_size(layout, &width, &height);
53 cairo_move_to(cr, 0, 0);
54 pango_cairo_show_layout(cr, layout);
57 * This is still problematic: a long dive location will clash
58 * with the depth/duration information. Need to mask that or
59 * create a box or something.
61 snprintf(buffer, sizeof(buffer),
62 "<span size=\"small\">"
66 to_feet(dive->maxdepth),
67 dive->duration.seconds / 60,
68 dive->duration.seconds % 60);
70 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT);
71 pango_layout_set_markup(layout, buffer, -1);
73 cairo_move_to(cr, 0, 0);
74 pango_cairo_show_layout(cr, layout);
76 len = snprintf(buffer, sizeof(buffer), "%s\n\n%s",
77 dive->location ? : "",
81 pango_layout_set_height(layout, maxheight);
82 pango_layout_set_attributes(layout, NULL);
83 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
84 pango_layout_set_text(layout, buffer, len);
86 cairo_move_to(cr, 0, height / (double) PANGO_SCALE);
87 pango_cairo_show_layout(cr, layout);
89 g_object_unref(layout);
92 static void show_dive_profile(struct dive *dive, cairo_t *cr, double w, double h)
94 struct graphics_context gc = {
99 plot(&gc, w, h, dive);
103 static void print(int divenr, cairo_t *cr, double x, double y, double w, double h, PangoFontDescription *font)
107 dive = get_dive(divenr);
111 cairo_translate(cr, x, y);
113 /* Plus 5% on all sides */
114 cairo_translate(cr, w/20, h/20);
117 /* We actually want to scale the text and the lines now */
118 cairo_scale(cr, 0.5, 0.5);
120 /* Dive plot in the upper two thirds - note the scaling */
121 show_dive_profile(dive, cr, w*2, h*1.33);
123 /* Dive information in the lower third */
124 cairo_translate(cr, 0, h*1.33);
126 show_dive_text(dive, cr, w*2, h*0.67, font);
131 static void draw_page(GtkPrintOperation *operation,
132 GtkPrintContext *context,
139 PangoFontDescription *font;
141 cr = gtk_print_context_get_cairo_context(context);
142 font = pango_font_description_from_string("Sans");
144 w = gtk_print_context_get_width(context)/2;
145 h = gtk_print_context_get_height(context)/3;
148 print(nr+0, cr, 0, 0, w, h, font);
149 print(nr+1, cr, w, 0, w, h, font);
150 print(nr+2, cr, 0, h, w, h, font);
151 print(nr+3, cr, w, h, w, h, font);
152 print(nr+4, cr, 0, 2*h, w, h, font);
153 print(nr+5, cr, w, 2*h, w, h, font);
155 pango_font_description_free(font);
158 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
162 static GtkPrintSettings *settings = NULL;
167 GtkPrintOperation *print;
168 GtkPrintOperationResult res;
171 print = gtk_print_operation_new();
172 if (settings != NULL)
173 gtk_print_operation_set_print_settings(print, settings);
174 pages = (dive_table.nr + 5) / 6;
175 gtk_print_operation_set_n_pages(print, pages);
176 g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
177 g_signal_connect(print, "draw_page", G_CALLBACK(draw_page), NULL);
178 res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
179 GTK_WINDOW(main_window), NULL);
180 if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
181 if (settings != NULL)
182 g_object_unref(settings);
183 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
185 g_object_unref(print);