]> git.tdb.fi Git - ext/subsurface.git/blob - print.c
Make the dive list start as a plain notebook page
[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 static void set_font(PangoLayout *layout, PangoFontDescription *font, double size, int align)
15 {
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);
20
21 }
22
23 /*
24  * You know what? Maybe somebody can do a real Pango layout thing.
25  * This is hacky.
26  */
27 static void show_dive_text(struct dive *dive, cairo_t *cr, double w, double h, PangoFontDescription *font)
28 {
29         double depth;
30         const char *unit;
31         int len, decimals, width, height, maxwidth, maxheight;
32         PangoLayout *layout;
33         struct tm *tm;
34         char buffer[80], divenr[20], *people;
35
36         maxwidth = w * PANGO_SCALE;
37         maxheight = h * PANGO_SCALE * 0.9;
38
39         layout = pango_cairo_create_layout(cr);
40         pango_layout_set_width(layout, maxwidth);
41         pango_layout_set_height(layout, maxheight);
42
43         *divenr = 0;
44         if (dive->number)
45                 snprintf(divenr, sizeof(divenr), "Dive #%d - ", dive->number);
46
47         tm = gmtime(&dive->when);
48         len = snprintf(buffer, sizeof(buffer),
49                 "%s%s, %s %d, %d   %d:%02d",
50                 divenr,
51                 weekday(tm->tm_wday),
52                 monthname(tm->tm_mon),
53                 tm->tm_mday, tm->tm_year + 1900,
54                 tm->tm_hour, tm->tm_min);
55
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);
59
60         cairo_move_to(cr, 0, 0);
61         pango_cairo_show_layout(cr, layout);
62
63         people = dive->buddy;
64         if (!people || !*people) {
65                 people = dive->divemaster;
66                 if (!people)
67                         people = "";
68         }
69
70         depth = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
71         snprintf(buffer, sizeof(buffer),
72                 "Max depth: %.*f %s\n"
73                 "Duration: %d min\n"
74                 "%s",
75                 decimals, depth, unit,
76                 (dive->duration.seconds+59) / 60,
77                 people);
78
79         set_font(layout, font, FONT_SMALL, PANGO_ALIGN_RIGHT);
80         pango_layout_set_text(layout, buffer, -1);
81
82         cairo_move_to(cr, 0, 0);
83         pango_cairo_show_layout(cr, layout);
84
85         /*
86          * Show the dive location
87          *
88          * .. or at least a space to get the size.
89          *
90          * Move down by the size of the date, and limit the
91          * width to the same width as the date string.
92          */
93         cairo_translate(cr, 0, height / (double) PANGO_SCALE);
94         maxheight -= height;
95         pango_layout_set_height(layout, 1);
96         pango_layout_set_width(layout, width);
97
98         set_font(layout, font, FONT_NORMAL, PANGO_ALIGN_LEFT);
99         pango_layout_set_text(layout, dive->location ? : " ", -1);
100
101         cairo_move_to(cr, 0, 0);
102         pango_cairo_show_layout(cr, layout);
103
104         pango_layout_get_size(layout, &width, &height);
105
106         /*
107          * Show the dive notes
108          */
109         if (dive->notes) {
110                 /* Move down by the size of the location (x2) */
111                 height = height * 2;
112                 cairo_translate(cr, 0, height / (double) PANGO_SCALE);
113                 maxheight -= height;
114
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);
121
122                 cairo_move_to(cr, 0, 0);
123                 pango_cairo_show_layout(cr, layout);
124         }
125         g_object_unref(layout);
126 }
127
128 static void show_dive_profile(struct dive *dive, cairo_t *cr, double w, double h)
129 {
130         struct graphics_context gc = {
131                 .printer = 1,
132                 .cr = cr
133         };
134         cairo_save(cr);
135         plot(&gc, w, h, dive);
136         cairo_restore(cr);
137 }
138
139 static void print(int divenr, cairo_t *cr, double x, double y, double w, double h, PangoFontDescription *font)
140 {
141         struct dive *dive;
142
143         dive = get_dive(divenr);
144         if (!dive)
145                 return;
146         cairo_save(cr);
147         cairo_translate(cr, x, y);
148
149         /* Plus 5% on all sides */
150         cairo_translate(cr, w/20, h/20);
151         w *= 0.9; h *= 0.9;
152
153         /* We actually want to scale the text and the lines now */
154         cairo_scale(cr, 0.5, 0.5);
155
156         /* Dive plot in the upper two thirds - note the scaling */
157         show_dive_profile(dive, cr, w*2, h*1.33);
158
159         /* Dive information in the lower third */
160         cairo_translate(cr, 0, h*1.33);
161
162         show_dive_text(dive, cr, w*2, h*0.67, font);
163
164         cairo_restore(cr);
165 }
166
167 static void draw_page(GtkPrintOperation *operation,
168                         GtkPrintContext *context,
169                         gint page_nr,
170                         gpointer user_data)
171 {
172         int nr;
173         cairo_t *cr;
174         double w, h;
175         PangoFontDescription *font;
176
177         cr = gtk_print_context_get_cairo_context(context);
178         font = pango_font_description_from_string("Sans");
179
180         w = gtk_print_context_get_width(context)/2;
181         h = gtk_print_context_get_height(context)/3;
182
183         nr = page_nr*6;
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);
190
191         pango_font_description_free(font);
192 }
193
194 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
195 {
196 }
197
198 static GtkPrintSettings *settings = NULL;
199
200 void do_print(void)
201 {
202         int pages;
203         GtkPrintOperation *print;
204         GtkPrintOperationResult res;
205
206         repaint_dive();
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));
220         }
221         g_object_unref(print);
222 }