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 struct graphics_context gc = {
135 plot(&gc, w, h, dive);
139 static void print(int divenr, cairo_t *cr, double x, double y, double w, double h, PangoFontDescription *font)
143 dive = get_dive(divenr);
147 cairo_translate(cr, x, y);
149 /* Plus 5% on all sides */
150 cairo_translate(cr, w/20, h/20);
153 /* We actually want to scale the text and the lines now */
154 cairo_scale(cr, 0.5, 0.5);
156 /* Dive plot in the upper two thirds - note the scaling */
157 show_dive_profile(dive, cr, w*2, h*1.33);
159 /* Dive information in the lower third */
160 cairo_translate(cr, 0, h*1.33);
162 show_dive_text(dive, cr, w*2, h*0.67, font);
167 static void draw_page(GtkPrintOperation *operation,
168 GtkPrintContext *context,
175 PangoFontDescription *font;
177 cr = gtk_print_context_get_cairo_context(context);
178 font = pango_font_description_from_string("Sans");
180 w = gtk_print_context_get_width(context)/2;
181 h = gtk_print_context_get_height(context)/3;
184 print(nr+0, cr, 0, 0, w, h, font);
185 print(nr+1, cr, w, 0, w, h, font);
186 print(nr+2, cr, 0, h, w, h, font);
187 print(nr+3, cr, w, h, w, h, font);
188 print(nr+4, cr, 0, 2*h, w, h, font);
189 print(nr+5, cr, w, 2*h, w, h, font);
191 pango_font_description_free(font);
194 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
198 static GtkPrintSettings *settings = NULL;
203 GtkPrintOperation *print;
204 GtkPrintOperationResult res;
207 print = gtk_print_operation_new();
208 if (settings != NULL)
209 gtk_print_operation_set_print_settings(print, settings);
210 pages = (dive_table.nr + 5) / 6;
211 gtk_print_operation_set_n_pages(print, pages);
212 g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
213 g_signal_connect(print, "draw_page", G_CALLBACK(draw_page), NULL);
214 res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
215 GTK_WINDOW(main_window), NULL);
216 if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
217 if (settings != NULL)
218 g_object_unref(settings);
219 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
221 g_object_unref(print);