8 #include "display-gtk.h"
10 /* Why doesn't pango/gtk have these quoting functions? */
11 static inline int add_char(char *buffer, size_t size, int len, char c)
18 /* Add an escape string "atomically" - all or nothing */
19 static int add_str(char *buffer, size_t size, int len, const char *s)
23 while ((c = *s++) != 0) {
31 static int add_quoted_string(char *buffer, size_t size, int len, const char *s)
40 unsigned char c = *s++;
43 len = add_char(buffer, size, len, c);
58 len = add_str(buffer, size, len, escape);
67 * You know what? Maybe somebody can do a real Pango layout thing.
70 static void show_dive_text(struct dive *dive, cairo_t *cr, double w, double h)
75 char buffer[1024], divenr[20];
77 layout = pango_cairo_create_layout(cr);
78 pango_layout_set_width(layout, w * PANGO_SCALE);
79 pango_layout_set_height(layout, h * PANGO_SCALE * 0.9);
80 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
84 snprintf(divenr, sizeof(divenr), "Dive #%d - ", dive->number);
87 tm = gmtime(&dive->when);
88 len = snprintf(buffer, sizeof(buffer),
89 "<span size=\"large\">"
90 "%s%s, %s %d, %d %d:%02d"
94 monthname(tm->tm_mon),
95 tm->tm_mday, tm->tm_year + 1900,
96 tm->tm_hour, tm->tm_min);
99 * Leave an empty line even if no location: otherwise the notes can
100 * overrun the depth/duration information.
103 len = add_quoted_string(buffer, sizeof(buffer), len, dive->location);
105 len = add_char(buffer, sizeof(buffer), len, '\n');
108 len = add_char(buffer, sizeof(buffer), len, '\n');
109 len = add_quoted_string(buffer, sizeof(buffer), len, dive->notes);
112 pango_layout_set_justify(layout, 1);
113 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
114 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
115 pango_layout_set_markup(layout, buffer, len);
117 cairo_move_to(cr, 0, 0);
118 pango_cairo_show_layout(cr, layout);
121 * This is still problematic: a long dive location will clash
122 * with the depth/duration information. Need to mask that or
123 * create a box or something.
125 snprintf(buffer, sizeof(buffer),
126 "<span size=\"small\">"
130 to_feet(dive->maxdepth),
131 dive->duration.seconds / 60,
132 dive->duration.seconds % 60);
134 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT);
135 pango_layout_set_markup(layout, buffer, -1);
137 cairo_move_to(cr, 0, 0);
138 pango_cairo_show_layout(cr, layout);
140 g_object_unref(layout);
143 static void show_dive_profile(struct dive *dive, cairo_t *cr, double w, double h)
145 struct graphics_context gc = {
150 plot(&gc, w, h, dive);
154 static void print(int divenr, cairo_t *cr, double x, double y, double w, double h)
158 dive = get_dive(divenr);
162 cairo_translate(cr, x, y);
164 /* Plus 5% on all sides */
165 cairo_translate(cr, w/20, h/20);
168 /* We actually want to scale the text and the lines now */
169 cairo_scale(cr, 0.5, 0.5);
171 /* Dive plot in the upper two thirds - note the scaling */
172 show_dive_profile(dive, cr, w*2, h*1.33);
174 /* Dive information in the lower third */
175 cairo_translate(cr, 0, h*1.33);
177 show_dive_text(dive, cr, w*2, h*0.67);
182 static void draw_page(GtkPrintOperation *operation,
183 GtkPrintContext *context,
191 cr = gtk_print_context_get_cairo_context(context);
193 w = gtk_print_context_get_width(context)/2;
194 h = gtk_print_context_get_height(context)/3;
197 print(nr+0, cr, 0, 0, w, h);
198 print(nr+1, cr, w, 0, w, h);
199 print(nr+2, cr, 0, h, w, h);
200 print(nr+3, cr, w, h, w, h);
201 print(nr+4, cr, 0, 2*h, w, h);
202 print(nr+5, cr, w, 2*h, w, h);
205 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
209 static GtkPrintSettings *settings = NULL;
214 GtkPrintOperation *print;
215 GtkPrintOperationResult res;
218 print = gtk_print_operation_new();
219 if (settings != NULL)
220 gtk_print_operation_set_print_settings(print, settings);
221 pages = (dive_table.nr + 5) / 6;
222 gtk_print_operation_set_n_pages(print, pages);
223 g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
224 g_signal_connect(print, "draw_page", G_CALLBACK(draw_page), NULL);
225 res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
226 GTK_WINDOW(main_window), NULL);
227 if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
228 if (settings != NULL)
229 g_object_unref(settings);
230 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
232 g_object_unref(print);