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 void set_font(PangoLayout *layout, PangoFontDescription *font, double size, int align)
16 pango_font_description_set_size(font, size * PANGO_SCALE);
17 pango_layout_set_font_description(layout, font);
18 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
19 pango_layout_set_alignment(layout, align);
24 * You know what? Maybe somebody can do a real Pango layout thing.
27 static void show_dive_text(struct dive *dive, cairo_t *cr, double w, double h, PangoFontDescription *font)
31 int len, decimals, width, height, maxwidth, maxheight;
34 char buffer[80], divenr[20], *people;
36 maxwidth = w * PANGO_SCALE;
37 maxheight = h * PANGO_SCALE * 0.9;
39 layout = pango_cairo_create_layout(cr);
40 pango_layout_set_width(layout, maxwidth);
41 pango_layout_set_height(layout, maxheight);
45 snprintf(divenr, sizeof(divenr), "Dive #%d - ", dive->number);
47 tm = gmtime(&dive->when);
48 len = snprintf(buffer, sizeof(buffer),
49 "%s%s, %s %d, %d %d:%02d",
52 monthname(tm->tm_mon),
53 tm->tm_mday, tm->tm_year + 1900,
54 tm->tm_hour, tm->tm_min);
56 set_font(layout, font, FONT_LARGE, PANGO_ALIGN_LEFT);
57 pango_layout_set_text(layout, buffer, len);
58 pango_layout_get_size(layout, &width, &height);
60 cairo_move_to(cr, 0, 0);
61 pango_cairo_show_layout(cr, layout);
64 if (!people || !*people) {
65 people = dive->divemaster;
70 depth = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
71 snprintf(buffer, sizeof(buffer),
72 "Max depth: %.*f %s\n"
75 decimals, depth, unit,
76 (dive->duration.seconds+59) / 60,
79 set_font(layout, font, FONT_SMALL, PANGO_ALIGN_RIGHT);
80 pango_layout_set_text(layout, buffer, -1);
82 cairo_move_to(cr, 0, 0);
83 pango_cairo_show_layout(cr, layout);
86 * Show the dive location
88 * .. or at least a space to get the size.
90 * Move down by the size of the date, and limit the
91 * width to the same width as the date string.
93 cairo_translate(cr, 0, height / (double) PANGO_SCALE);
95 pango_layout_set_height(layout, 1);
96 pango_layout_set_width(layout, width);
98 set_font(layout, font, FONT_NORMAL, PANGO_ALIGN_LEFT);
99 pango_layout_set_text(layout, dive->location ? : " ", -1);
101 cairo_move_to(cr, 0, 0);
102 pango_cairo_show_layout(cr, layout);
104 pango_layout_get_size(layout, &width, &height);
107 * Show the dive notes
110 /* Move down by the size of the location (x2) */
112 cairo_translate(cr, 0, height / (double) PANGO_SCALE);
115 /* Use the full width and remaining height for notes */
116 pango_layout_set_height(layout, maxheight);
117 pango_layout_set_width(layout, maxwidth);
118 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
119 pango_layout_set_justify(layout, 1);
120 pango_layout_set_text(layout, dive->notes, -1);
122 cairo_move_to(cr, 0, 0);
123 pango_cairo_show_layout(cr, layout);
125 g_object_unref(layout);
128 static void show_dive_profile(struct dive *dive, cairo_t *cr, double w, double h)
130 cairo_rectangle_int_t drawing_area = { w/20.0, h/20.0, w, h};
131 struct graphics_context gc = {
136 plot(&gc, &drawing_area, dive);
140 static void print(int divenr, cairo_t *cr, double x, double y, double w, double h, PangoFontDescription *font)
144 dive = get_dive(divenr);
148 cairo_translate(cr, x, y);
150 /* Plus 5% on all sides */
151 cairo_translate(cr, w/20, h/20);
154 /* We actually want to scale the text and the lines now */
155 cairo_scale(cr, 0.5, 0.5);
157 /* Dive plot in the upper two thirds - note the scaling */
158 show_dive_profile(dive, cr, w*2, h*1.33);
160 /* Dive information in the lower third */
161 cairo_translate(cr, 0, h*1.33);
163 show_dive_text(dive, cr, w*2, h*0.67, font);
168 static void draw_page(GtkPrintOperation *operation,
169 GtkPrintContext *context,
176 PangoFontDescription *font;
178 cr = gtk_print_context_get_cairo_context(context);
179 font = pango_font_description_from_string("Sans");
181 w = gtk_print_context_get_width(context)/2;
182 h = gtk_print_context_get_height(context)/3;
185 print(nr+0, cr, 0, 0, w, h, font);
186 print(nr+1, cr, w, 0, w, h, font);
187 print(nr+2, cr, 0, h, w, h, font);
188 print(nr+3, cr, w, h, w, h, font);
189 print(nr+4, cr, 0, 2*h, w, h, font);
190 print(nr+5, cr, w, 2*h, w, h, font);
192 pango_font_description_free(font);
195 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
199 static GtkPrintSettings *settings = NULL;
204 GtkPrintOperation *print;
205 GtkPrintOperationResult res;
208 print = gtk_print_operation_new();
209 if (settings != NULL)
210 gtk_print_operation_set_print_settings(print, settings);
211 pages = (dive_table.nr + 5) / 6;
212 gtk_print_operation_set_n_pages(print, pages);
213 g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
214 g_signal_connect(print, "draw_page", G_CALLBACK(draw_page), NULL);
215 res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
216 GTK_WINDOW(main_window), NULL);
217 if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
218 if (settings != NULL)
219 g_object_unref(settings);
220 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
222 g_object_unref(print);