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[1024], divenr[20];
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);
48 tm = gmtime(&dive->when);
49 len = snprintf(buffer, sizeof(buffer),
50 "%s%s, %s %d, %d %d:%02d",
53 monthname(tm->tm_mon),
54 tm->tm_mday, tm->tm_year + 1900,
55 tm->tm_hour, tm->tm_min);
57 set_font(layout, font, FONT_LARGE, PANGO_ALIGN_LEFT);
58 pango_layout_set_text(layout, buffer, len);
59 pango_layout_get_size(layout, &width, &height);
61 cairo_move_to(cr, 0, 0);
62 pango_cairo_show_layout(cr, layout);
65 * This is still problematic: a long dive location will clash
66 * with the depth/duration information. Need to mask that or
67 * create a box or something.
69 depth = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
70 snprintf(buffer, sizeof(buffer),
71 "Max depth: %.*f %s\n"
74 decimals, depth, unit,
75 (dive->duration.seconds+59) / 60,
78 set_font(layout, font, FONT_SMALL, PANGO_ALIGN_RIGHT);
79 pango_layout_set_text(layout, buffer, -1);
81 cairo_move_to(cr, 0, 0);
82 pango_cairo_show_layout(cr, layout);
85 * Show the dive location
87 * .. or at least a space to get the size.
89 * Move down by the size of the date, and limit the
90 * width to the same width as the date string.
92 cairo_translate(cr, 0, height / (double) PANGO_SCALE);
94 pango_layout_set_height(layout, 1);
95 pango_layout_set_width(layout, width);
97 set_font(layout, font, FONT_NORMAL, PANGO_ALIGN_LEFT);
98 pango_layout_set_text(layout, dive->location ? : " ", -1);
100 cairo_move_to(cr, 0, 0);
101 pango_cairo_show_layout(cr, layout);
103 pango_layout_get_size(layout, &width, &height);
106 * Show the dive notes
109 /* Move down by the size of the location (x2) */
111 cairo_translate(cr, 0, height / (double) PANGO_SCALE);
114 /* Use the full width and remaining height for notes */
115 pango_layout_set_height(layout, maxheight);
116 pango_layout_set_width(layout, maxwidth);
117 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
118 pango_layout_set_justify(layout, 1);
119 pango_layout_set_text(layout, dive->notes, -1);
121 cairo_move_to(cr, 0, 0);
122 pango_cairo_show_layout(cr, layout);
124 g_object_unref(layout);
127 static void show_dive_profile(struct dive *dive, cairo_t *cr, double w, double h)
129 struct graphics_context gc = {
134 plot(&gc, w, h, dive);
138 static void print(int divenr, cairo_t *cr, double x, double y, double w, double h, PangoFontDescription *font)
142 dive = get_dive(divenr);
146 cairo_translate(cr, x, y);
148 /* Plus 5% on all sides */
149 cairo_translate(cr, w/20, h/20);
152 /* We actually want to scale the text and the lines now */
153 cairo_scale(cr, 0.5, 0.5);
155 /* Dive plot in the upper two thirds - note the scaling */
156 show_dive_profile(dive, cr, w*2, h*1.33);
158 /* Dive information in the lower third */
159 cairo_translate(cr, 0, h*1.33);
161 show_dive_text(dive, cr, w*2, h*0.67, font);
166 static void draw_page(GtkPrintOperation *operation,
167 GtkPrintContext *context,
174 PangoFontDescription *font;
176 cr = gtk_print_context_get_cairo_context(context);
177 font = pango_font_description_from_string("Sans");
179 w = gtk_print_context_get_width(context)/2;
180 h = gtk_print_context_get_height(context)/3;
183 print(nr+0, cr, 0, 0, w, h, font);
184 print(nr+1, cr, w, 0, w, h, font);
185 print(nr+2, cr, 0, h, w, h, font);
186 print(nr+3, cr, w, h, w, h, font);
187 print(nr+4, cr, 0, 2*h, w, h, font);
188 print(nr+5, cr, w, 2*h, w, h, font);
190 pango_font_description_free(font);
193 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
197 static GtkPrintSettings *settings = NULL;
202 GtkPrintOperation *print;
203 GtkPrintOperationResult res;
206 print = gtk_print_operation_new();
207 if (settings != NULL)
208 gtk_print_operation_set_print_settings(print, settings);
209 pages = (dive_table.nr + 5) / 6;
210 gtk_print_operation_set_n_pages(print, pages);
211 g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
212 g_signal_connect(print, "draw_page", G_CALLBACK(draw_page), NULL);
213 res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
214 GTK_WINDOW(main_window), NULL);
215 if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
216 if (settings != NULL)
217 g_object_unref(settings);
218 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
220 g_object_unref(print);