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)
29 int len, width, height, maxwidth, maxheight;
32 char buffer[1024], divenr[20];
34 maxwidth = w * PANGO_SCALE;
35 maxheight = h * PANGO_SCALE * 0.9;
37 layout = pango_cairo_create_layout(cr);
38 pango_layout_set_width(layout, maxwidth);
39 pango_layout_set_height(layout, maxheight);
43 snprintf(divenr, sizeof(divenr), "Dive #%d - ", dive->number);
46 tm = gmtime(&dive->when);
47 len = snprintf(buffer, sizeof(buffer),
48 "%s%s, %s %d, %d %d:%02d",
51 monthname(tm->tm_mon),
52 tm->tm_mday, tm->tm_year + 1900,
53 tm->tm_hour, tm->tm_min);
55 set_font(layout, font, FONT_LARGE, PANGO_ALIGN_LEFT);
56 pango_layout_set_text(layout, buffer, len);
57 pango_layout_get_size(layout, &width, &height);
59 cairo_move_to(cr, 0, 0);
60 pango_cairo_show_layout(cr, layout);
63 * This is still problematic: a long dive location will clash
64 * with the depth/duration information. Need to mask that or
65 * create a box or something.
67 snprintf(buffer, sizeof(buffer),
71 to_feet(dive->maxdepth),
72 dive->duration.seconds / 60,
73 dive->duration.seconds % 60,
76 set_font(layout, font, FONT_SMALL, PANGO_ALIGN_RIGHT);
77 pango_layout_set_text(layout, buffer, -1);
79 cairo_move_to(cr, 0, 0);
80 pango_cairo_show_layout(cr, layout);
83 * Show the dive location
85 * .. or at least a space to get the size.
87 * Move down by the size of the date, and limit the
88 * width to the same width as the date string.
90 cairo_translate(cr, 0, height / (double) PANGO_SCALE);
92 pango_layout_set_height(layout, 1);
93 pango_layout_set_width(layout, width);
95 set_font(layout, font, FONT_NORMAL, PANGO_ALIGN_LEFT);
96 pango_layout_set_text(layout, dive->location ? : " ", -1);
98 cairo_move_to(cr, 0, 0);
99 pango_cairo_show_layout(cr, layout);
101 pango_layout_get_size(layout, &width, &height);
104 * Show the dive notes
107 /* Move down by the size of the location (1.5) */
108 height = height * 3 / 2;
109 cairo_translate(cr, 0, height / (double) PANGO_SCALE);
112 /* Use the full width and remaining height for notes */
113 pango_layout_set_height(layout, maxheight);
114 pango_layout_set_width(layout, maxwidth);
115 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
116 pango_layout_set_justify(layout, 1);
117 pango_layout_set_text(layout, dive->notes, -1);
119 cairo_move_to(cr, 0, 0);
120 pango_cairo_show_layout(cr, layout);
122 g_object_unref(layout);
125 static void show_dive_profile(struct dive *dive, cairo_t *cr, double w, double h)
127 struct graphics_context gc = {
132 plot(&gc, w, h, dive);
136 static void print(int divenr, cairo_t *cr, double x, double y, double w, double h, PangoFontDescription *font)
140 dive = get_dive(divenr);
144 cairo_translate(cr, x, y);
146 /* Plus 5% on all sides */
147 cairo_translate(cr, w/20, h/20);
150 /* We actually want to scale the text and the lines now */
151 cairo_scale(cr, 0.5, 0.5);
153 /* Dive plot in the upper two thirds - note the scaling */
154 show_dive_profile(dive, cr, w*2, h*1.33);
156 /* Dive information in the lower third */
157 cairo_translate(cr, 0, h*1.33);
159 show_dive_text(dive, cr, w*2, h*0.67, font);
164 static void draw_page(GtkPrintOperation *operation,
165 GtkPrintContext *context,
172 PangoFontDescription *font;
174 cr = gtk_print_context_get_cairo_context(context);
175 font = pango_font_description_from_string("Sans");
177 w = gtk_print_context_get_width(context)/2;
178 h = gtk_print_context_get_height(context)/3;
181 print(nr+0, cr, 0, 0, w, h, font);
182 print(nr+1, cr, w, 0, w, h, font);
183 print(nr+2, cr, 0, h, w, h, font);
184 print(nr+3, cr, w, h, w, h, font);
185 print(nr+4, cr, 0, 2*h, w, h, font);
186 print(nr+5, cr, w, 2*h, w, h, font);
188 pango_font_description_free(font);
191 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
195 static GtkPrintSettings *settings = NULL;
200 GtkPrintOperation *print;
201 GtkPrintOperationResult res;
204 print = gtk_print_operation_new();
205 if (settings != NULL)
206 gtk_print_operation_set_print_settings(print, settings);
207 pages = (dive_table.nr + 5) / 6;
208 gtk_print_operation_set_n_pages(print, pages);
209 g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
210 g_signal_connect(print, "draw_page", G_CALLBACK(draw_page), NULL);
211 res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
212 GTK_WINDOW(main_window), NULL);
213 if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
214 if (settings != NULL)
215 g_object_unref(settings);
216 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
218 g_object_unref(print);