]> git.tdb.fi Git - ext/subsurface.git/blob - print.c
cochran: add support for importing the exported CSV files
[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         cairo_rectangle_int_t drawing_area = { w/20.0, h/20.0, w, h};
131         struct graphics_context gc = {
132                 .printer = 1,
133                 .cr = cr
134         };
135         cairo_save(cr);
136         plot(&gc, &drawing_area, dive);
137         cairo_restore(cr);
138 }
139
140 static void print(int divenr, cairo_t *cr, double x, double y, double w, double h, PangoFontDescription *font)
141 {
142         struct dive *dive;
143
144         dive = get_dive(divenr);
145         if (!dive)
146                 return;
147         cairo_save(cr);
148         cairo_translate(cr, x, y);
149
150         /* Plus 5% on all sides */
151         cairo_translate(cr, w/20, h/20);
152         w *= 0.9; h *= 0.9;
153
154         /* We actually want to scale the text and the lines now */
155         cairo_scale(cr, 0.5, 0.5);
156
157         /* Dive plot in the upper two thirds - note the scaling */
158         show_dive_profile(dive, cr, w*2, h*1.33);
159
160         /* Dive information in the lower third */
161         cairo_translate(cr, 0, h*1.33);
162
163         show_dive_text(dive, cr, w*2, h*0.67, font);
164
165         cairo_restore(cr);
166 }
167
168 static void draw_page(GtkPrintOperation *operation,
169                         GtkPrintContext *context,
170                         gint page_nr,
171                         gpointer user_data)
172 {
173         int nr;
174         cairo_t *cr;
175         double w, h;
176         PangoFontDescription *font;
177
178         cr = gtk_print_context_get_cairo_context(context);
179         font = pango_font_description_from_string("Sans");
180
181         w = gtk_print_context_get_width(context)/2;
182         h = gtk_print_context_get_height(context)/3;
183
184         nr = page_nr*6;
185         print(nr+0, cr, 0,   0, w, h, font);
186         print(nr+1, cr, w,   0, w, h, font);
187         print(nr+2, cr, 0,   h, w, h, font);
188         print(nr+3, cr, w,   h, w, h, font);
189         print(nr+4, cr, 0, 2*h, w, h, font);
190         print(nr+5, cr, w, 2*h, w, h, font);
191
192         pango_font_description_free(font);
193 }
194
195 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
196 {
197 }
198
199 static GtkPrintSettings *settings = NULL;
200
201 void do_print(void)
202 {
203         int pages;
204         GtkPrintOperation *print;
205         GtkPrintOperationResult res;
206
207         repaint_dive();
208         print = gtk_print_operation_new();
209         if (settings != NULL)
210                 gtk_print_operation_set_print_settings(print, settings);
211         pages = (dive_table.nr + 5) / 6;
212         gtk_print_operation_set_n_pages(print, pages);
213         g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
214         g_signal_connect(print, "draw_page", G_CALLBACK(draw_page), NULL);
215         res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
216                                          GTK_WINDOW(main_window), NULL);
217         if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
218                 if (settings != NULL)
219                         g_object_unref(settings);
220                 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
221         }
222         g_object_unref(print);
223 }