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 #define OPTIONCALLBACK(name, option) \
15 static void name(GtkWidget *w, gpointer data) \
17 option = GTK_TOGGLE_BUTTON(w)->active; \
20 OPTIONCALLBACK(print_profiles_toggle, visible_cols.print_profiles)
23 static void set_font(PangoLayout *layout, PangoFontDescription *font, double size, int align)
25 pango_font_description_set_size(font, size * PANGO_SCALE);
26 pango_layout_set_font_description(layout, font);
27 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
28 pango_layout_set_alignment(layout, align);
33 * You know what? Maybe somebody can do a real Pango layout thing.
36 static void show_dive_text(struct dive *dive, cairo_t *cr, double w, double h, PangoFontDescription *font)
40 int len, decimals, width, height, maxwidth, maxheight;
43 char buffer[80], divenr[20], *people;
45 maxwidth = w * PANGO_SCALE;
46 maxheight = h * PANGO_SCALE * 0.9;
48 layout = pango_cairo_create_layout(cr);
49 pango_layout_set_width(layout, maxwidth);
50 pango_layout_set_height(layout, maxheight);
54 snprintf(divenr, sizeof(divenr), "Dive #%d - ", dive->number);
56 tm = gmtime(&dive->when);
57 len = snprintf(buffer, sizeof(buffer),
58 "%s%s, %s %d, %d %d:%02d",
61 monthname(tm->tm_mon),
62 tm->tm_mday, tm->tm_year + 1900,
63 tm->tm_hour, tm->tm_min);
65 set_font(layout, font, FONT_LARGE, PANGO_ALIGN_LEFT);
66 pango_layout_set_text(layout, buffer, len);
67 pango_layout_get_size(layout, &width, &height);
69 cairo_move_to(cr, 0, 0);
70 pango_cairo_show_layout(cr, layout);
73 if (!people || !*people) {
74 people = dive->divemaster;
79 depth = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
80 snprintf(buffer, sizeof(buffer),
81 "Max depth: %.*f %s\n"
84 decimals, depth, unit,
85 (dive->duration.seconds+59) / 60,
88 set_font(layout, font, FONT_SMALL, PANGO_ALIGN_RIGHT);
89 pango_layout_set_text(layout, buffer, -1);
91 cairo_move_to(cr, 0, 0);
92 pango_cairo_show_layout(cr, layout);
95 * Show the dive location
97 * .. or at least a space to get the size.
99 * Move down by the size of the date, and limit the
100 * width to the same width as the date string.
102 cairo_translate(cr, 0, height / (double) PANGO_SCALE);
104 pango_layout_set_height(layout, 1);
105 pango_layout_set_width(layout, width);
107 set_font(layout, font, FONT_NORMAL, PANGO_ALIGN_LEFT);
108 pango_layout_set_text(layout, dive->location ? : " ", -1);
110 cairo_move_to(cr, 0, 0);
111 pango_cairo_show_layout(cr, layout);
113 pango_layout_get_size(layout, &width, &height);
116 * Show the dive notes
119 /* Move down by the size of the location (x2) */
121 cairo_translate(cr, 0, height / (double) PANGO_SCALE);
124 /* Use the full width and remaining height for notes */
125 pango_layout_set_height(layout, maxheight);
126 pango_layout_set_width(layout, maxwidth);
127 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
128 pango_layout_set_justify(layout, 1);
129 pango_layout_set_text(layout, dive->notes, -1);
131 cairo_move_to(cr, 0, 0);
132 pango_cairo_show_layout(cr, layout);
134 g_object_unref(layout);
137 static void show_dive_profile(struct dive *dive, cairo_t *cr, double w, double h)
139 cairo_rectangle_int_t drawing_area = { w/20.0, h/20.0, w, h};
140 struct graphics_context gc = {
145 plot(&gc, &drawing_area, dive);
149 static void print(int divenr, cairo_t *cr, double x, double y, double w, double h, PangoFontDescription *font)
153 dive = get_dive(divenr);
157 cairo_translate(cr, x, y);
159 /* Plus 5% on all sides */
160 cairo_translate(cr, w/20, h/20);
163 /* We actually want to scale the text and the lines now */
164 cairo_scale(cr, 0.5, 0.5);
166 /* Dive plot in the upper two thirds - note the scaling */
167 show_dive_profile(dive, cr, w*2, h*1.33);
169 /* Dive information in the lower third */
170 cairo_translate(cr, 0, h*1.33);
172 show_dive_text(dive, cr, w*2, h*0.67, font);
177 static void print_table(int divenr, cairo_t *cr, double x, double y, double w, double h, PangoFontDescription *font)
181 dive = get_dive(divenr);
185 cairo_translate(cr, x, y);
187 /* Plus 5% on all sides */
188 cairo_translate(cr, w/20, h/20);
191 /* We actually want to scale the text and the lines now */
192 cairo_scale(cr, 0.5, 0.5);
194 show_dive_text(dive, cr, w*2, h*2, font);
199 static void draw_page(GtkPrintOperation *operation,
200 GtkPrintContext *context,
207 PangoFontDescription *font;
209 cr = gtk_print_context_get_cairo_context(context);
210 font = pango_font_description_from_string("Sans");
212 w = gtk_print_context_get_width(context)/2;
213 h = gtk_print_context_get_height(context)/3;
216 print(nr+0, cr, 0, 0, w, h, font);
217 print(nr+1, cr, w, 0, w, h, font);
218 print(nr+2, cr, 0, h, w, h, font);
219 print(nr+3, cr, w, h, w, h, font);
220 print(nr+4, cr, 0, 2*h, w, h, font);
221 print(nr+5, cr, w, 2*h, w, h, font);
223 pango_font_description_free(font);
226 static void draw_page_table(GtkPrintOperation *operation,
227 GtkPrintContext *context,
234 PangoFontDescription *font;
236 cr = gtk_print_context_get_cairo_context(context);
237 font = pango_font_description_from_string("Sans");
239 w = gtk_print_context_get_width(context);
240 h = gtk_print_context_get_height(context)/15;
244 for (i = 0; i < 15; i++) {
245 print_table(nr+i, cr, 0, 0+h*i, w, h, font);
248 pango_font_description_free(font);
251 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
254 if (visible_cols.print_profiles){
255 pages = (dive_table.nr + 5) / 6;
256 gtk_print_operation_set_n_pages(operation, pages);
258 pages = (dive_table.nr + 9) / 15;
259 gtk_print_operation_set_n_pages(operation, pages);
263 static GtkWidget *print_dialog(GtkPrintOperation *operation, gpointer user_data)
265 GtkWidget *vbox, *button, *frame, *box;
266 gtk_print_operation_set_custom_tab_label(operation, "Dive details");
268 vbox = gtk_vbox_new(TRUE, 5);
270 frame = gtk_frame_new("Print options");
271 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 1);
273 box = gtk_hbox_new(FALSE, 1);
274 gtk_container_add(GTK_CONTAINER(frame), box);
275 button = gtk_check_button_new_with_label("Show profiles");
276 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), visible_cols.print_profiles);
277 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 6);
278 g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(print_profiles_toggle), NULL);
280 gtk_widget_show_all(vbox);
284 static void print_dialog_apply(GtkPrintOperation *operation, GtkWidget *widget, gpointer user_data)
286 if (visible_cols.print_profiles){
287 g_signal_connect(operation, "draw_page", G_CALLBACK(draw_page), NULL);
289 g_signal_connect(operation, "draw_page", G_CALLBACK(draw_page_table), NULL);
293 static GtkPrintSettings *settings = NULL;
298 GtkPrintOperation *print;
299 GtkPrintOperationResult res;
302 print = gtk_print_operation_new();
303 gtk_print_operation_set_unit(print, GTK_UNIT_POINTS);
304 if (settings != NULL)
305 gtk_print_operation_set_print_settings(print, settings);
306 g_signal_connect(print, "create-custom-widget", G_CALLBACK(print_dialog), NULL);
307 g_signal_connect(print, "custom-widget-apply", G_CALLBACK(print_dialog_apply), NULL);
308 g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
309 res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
310 GTK_WINDOW(main_window), NULL);
311 if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
312 if (settings != NULL)
313 g_object_unref(settings);
314 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
316 g_object_unref(print);