]> git.tdb.fi Git - ext/subsurface.git/blob - print.c
Merge branch 'trips' of git://git.hohndel.org/subsurface
[ext/subsurface.git] / print.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdarg.h>
4 #include <gtk/gtk.h>
5
6 #include "dive.h"
7 #include "display.h"
8 #include "display-gtk.h"
9
10 #define FONT_NORMAL (12)
11 #define FONT_SMALL (FONT_NORMAL / 1.2)
12 #define FONT_LARGE (FONT_NORMAL * 1.2)
13
14 #define OPTIONCALLBACK(name, option) \
15 static void name(GtkWidget *w, gpointer data) \
16 { \
17         option = GTK_TOGGLE_BUTTON(w)->active; \
18 }
19
20 OPTIONCALLBACK(print_profiles_toggle, visible_cols.print_profiles)
21
22
23 static void set_font(PangoLayout *layout, PangoFontDescription *font, double size, int align)
24 {
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);
29
30 }
31
32 /*
33  * You know what? Maybe somebody can do a real Pango layout thing.
34  * This is hacky.
35  */
36 static void show_dive_text(struct dive *dive, cairo_t *cr, double w, double h, PangoFontDescription *font)
37 {
38         double depth;
39         const char *unit;
40         int len, decimals, width, height, maxwidth, maxheight;
41         PangoLayout *layout;
42         struct tm *tm;
43         char buffer[80], divenr[20], *people;
44
45         maxwidth = w * PANGO_SCALE;
46         maxheight = h * PANGO_SCALE * 0.9;
47
48         layout = pango_cairo_create_layout(cr);
49         pango_layout_set_width(layout, maxwidth);
50         pango_layout_set_height(layout, maxheight);
51
52         *divenr = 0;
53         if (dive->number)
54                 snprintf(divenr, sizeof(divenr), "Dive #%d - ", dive->number);
55
56         tm = gmtime(&dive->when);
57         len = snprintf(buffer, sizeof(buffer),
58                 "%s%s, %s %d, %d   %d:%02d",
59                 divenr,
60                 weekday(tm->tm_wday),
61                 monthname(tm->tm_mon),
62                 tm->tm_mday, tm->tm_year + 1900,
63                 tm->tm_hour, tm->tm_min);
64
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);
68
69         cairo_move_to(cr, 0, 0);
70         pango_cairo_show_layout(cr, layout);
71
72         people = dive->buddy;
73         if (!people || !*people) {
74                 people = dive->divemaster;
75                 if (!people)
76                         people = "";
77         }
78
79         depth = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
80         snprintf(buffer, sizeof(buffer),
81                 "Max depth: %.*f %s\n"
82                 "Duration: %d min\n"
83                 "%s",
84                 decimals, depth, unit,
85                 (dive->duration.seconds+59) / 60,
86                 people);
87
88         set_font(layout, font, FONT_SMALL, PANGO_ALIGN_RIGHT);
89         pango_layout_set_text(layout, buffer, -1);
90
91         cairo_move_to(cr, 0, 0);
92         pango_cairo_show_layout(cr, layout);
93
94         /*
95          * Show the dive location
96          *
97          * .. or at least a space to get the size.
98          *
99          * Move down by the size of the date, and limit the
100          * width to the same width as the date string.
101          */
102         cairo_translate(cr, 0, height / (double) PANGO_SCALE);
103         maxheight -= height;
104         pango_layout_set_height(layout, 1);
105         pango_layout_set_width(layout, width);
106
107         set_font(layout, font, FONT_NORMAL, PANGO_ALIGN_LEFT);
108         pango_layout_set_text(layout, dive->location ? : " ", -1);
109
110         cairo_move_to(cr, 0, 0);
111         pango_cairo_show_layout(cr, layout);
112
113         pango_layout_get_size(layout, &width, &height);
114
115         /*
116          * Show the dive notes
117          */
118         if (dive->notes) {
119                 /* Move down by the size of the location (x2) */
120                 height = height * 2;
121                 cairo_translate(cr, 0, height / (double) PANGO_SCALE);
122                 maxheight -= height;
123
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);
130
131                 cairo_move_to(cr, 0, 0);
132                 pango_cairo_show_layout(cr, layout);
133         }
134         g_object_unref(layout);
135 }
136
137 static void show_dive_profile(struct dive *dive, cairo_t *cr, double w, double h)
138 {
139         cairo_rectangle_int_t drawing_area = { w/20.0, h/20.0, w, h};
140         struct graphics_context gc = {
141                 .printer = 1,
142                 .cr = cr
143         };
144         cairo_save(cr);
145         plot(&gc, &drawing_area, dive);
146         cairo_restore(cr);
147 }
148
149 static void print(int divenr, cairo_t *cr, double x, double y, double w, double h, PangoFontDescription *font)
150 {
151         struct dive *dive;
152
153         dive = get_dive(divenr);
154         if (!dive)
155                 return;
156         cairo_save(cr);
157         cairo_translate(cr, x, y);
158
159         /* Plus 5% on all sides */
160         cairo_translate(cr, w/20, h/20);
161         w *= 0.9; h *= 0.9;
162
163         /* We actually want to scale the text and the lines now */
164         cairo_scale(cr, 0.5, 0.5);
165
166         /* Dive plot in the upper two thirds - note the scaling */
167         show_dive_profile(dive, cr, w*2, h*1.33);
168
169         /* Dive information in the lower third */
170         cairo_translate(cr, 0, h*1.33);
171
172         show_dive_text(dive, cr, w*2, h*0.67, font);
173
174         cairo_restore(cr);
175 }
176
177 static void print_table(int divenr, cairo_t *cr, double x, double y, double w, double h, PangoFontDescription *font)
178 {
179         struct dive *dive;
180
181         dive = get_dive(divenr);
182         if (!dive)
183                 return;
184         cairo_save(cr);
185         cairo_translate(cr, x, y);
186
187         /* Plus 5% on all sides */
188         cairo_translate(cr, w/20, h/20);
189         w *= 0.9; h *= 0.9;
190
191         /* We actually want to scale the text and the lines now */
192         cairo_scale(cr, 0.5, 0.5);
193
194         show_dive_text(dive, cr, w*2, h*2, font);
195
196         cairo_restore(cr);
197 }
198
199 static void draw_page(GtkPrintOperation *operation,
200                         GtkPrintContext *context,
201                         gint page_nr,
202                         gpointer user_data)
203 {
204         int nr;
205         cairo_t *cr;
206         double w, h;
207         PangoFontDescription *font;
208
209         cr = gtk_print_context_get_cairo_context(context);
210         font = pango_font_description_from_string("Sans");
211
212         w = gtk_print_context_get_width(context)/2;
213         h = gtk_print_context_get_height(context)/3;
214
215         nr = page_nr*6;
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);
222
223         pango_font_description_free(font);
224 }
225
226 static void draw_page_table(GtkPrintOperation *operation,
227                         GtkPrintContext *context,
228                         gint page_nr,
229                         gpointer user_data)
230 {
231         int nr;
232         cairo_t *cr;
233         double w, h;
234         PangoFontDescription *font;
235
236         cr = gtk_print_context_get_cairo_context(context);
237         font = pango_font_description_from_string("Sans");
238
239         w = gtk_print_context_get_width(context);
240         h = gtk_print_context_get_height(context)/15;
241
242         nr = page_nr*15;
243         int i;
244         for (i = 0; i < 15; i++) {
245                 print_table(nr+i, cr, 0,   0+h*i, w, h, font);
246         }
247
248         pango_font_description_free(font);
249 }
250
251 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
252 {
253         int pages;
254         if (visible_cols.print_profiles){
255                 pages = (dive_table.nr + 5) / 6;
256                 gtk_print_operation_set_n_pages(operation, pages);
257         } else {
258                 pages = (dive_table.nr + 9) / 15;
259                 gtk_print_operation_set_n_pages(operation, pages);
260         }
261 }
262
263 static GtkWidget *print_dialog(GtkPrintOperation *operation, gpointer user_data)
264 {
265         GtkWidget *vbox, *button, *frame, *box;
266         gtk_print_operation_set_custom_tab_label(operation, "Dive details");
267
268         vbox = gtk_vbox_new(TRUE, 5);
269
270         frame = gtk_frame_new("Print options");
271         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 1);
272
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);
279
280         gtk_widget_show_all(vbox);
281         return vbox;
282 }
283
284 static void print_dialog_apply(GtkPrintOperation *operation, GtkWidget *widget, gpointer user_data)
285 {
286         if (visible_cols.print_profiles){
287                 g_signal_connect(operation, "draw_page", G_CALLBACK(draw_page), NULL);
288         } else {
289                 g_signal_connect(operation, "draw_page", G_CALLBACK(draw_page_table), NULL);
290         }
291 }
292
293 static GtkPrintSettings *settings = NULL;
294
295 void do_print(void)
296 {
297         int pages;
298         GtkPrintOperation *print;
299         GtkPrintOperationResult res;
300
301         repaint_dive();
302         print = gtk_print_operation_new();
303         if (settings != NULL)
304                 gtk_print_operation_set_print_settings(print, settings);
305         g_signal_connect(print, "create-custom-widget", G_CALLBACK(print_dialog), NULL);
306         g_signal_connect(print, "custom-widget-apply", G_CALLBACK(print_dialog_apply), NULL);
307         g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
308         res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
309                                          GTK_WINDOW(main_window), NULL);
310         if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
311                 if (settings != NULL)
312                         g_object_unref(settings);
313                 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
314         }
315         g_object_unref(print);
316 }