]> git.tdb.fi Git - ext/subsurface.git/blob - print.c
Add completely BS dive text printing
[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 static void show_text(cairo_t *cr, int size, double x, double y, const char *fmt, ...)
11 {
12         va_list args;
13         char buffer[256], *p;
14
15         va_start(args, fmt);
16         vsnprintf(buffer, sizeof(buffer), fmt, args);
17         va_end(args);
18
19         cairo_set_font_size(cr, size);
20
21         p = buffer;
22         do {
23                 char *n = strchr(p, '\n');
24                 if (n)
25                         *n++ = 0;
26                 cairo_move_to(cr, x, y);
27                 cairo_show_text(cr, p);
28                 p = n;
29                 y += size;
30         } while (p);
31 }
32
33 /*
34  * You know what? Maybe somebody can do a real Pango layout thing.
35  *
36  * I'm going to do this with the cairo engine instead. I can only learn so
37  * many new interfaces.
38  */
39 static void show_dive_text(struct dive *dive, cairo_t *cr, double w, double h)
40 {
41         struct tm *tm;
42
43         tm = gmtime(&dive->when);
44         show_text(cr, 16, 0, 2, "Dive #%d - %s, %s %d, %d    %d:%02d",
45                 dive->number,
46                 weekday(tm->tm_wday),
47                 monthname(tm->tm_mon),
48                 tm->tm_mday, tm->tm_year + 1900,
49                 tm->tm_hour, tm->tm_min);
50
51         show_text(cr, 10, w*0.6, 0,
52                 "Max depth: %d ft\nDuration: %d:%02d",
53                 to_feet(dive->maxdepth),
54                 dive->duration.seconds / 60,
55                 dive->duration.seconds % 60);
56
57         show_text(cr, 10, 0, 20, "%s", dive->location ?: "");
58         show_text(cr, 10, 0, 30, "%s", dive->notes ?: "");
59 }
60
61 static void show_dive_profile(struct dive *dive, cairo_t *cr, double w, double h)
62 {
63         struct graphics_context gc = {
64                 .printer = 1,
65                 .cr = cr
66         };
67         plot(&gc, w, h, dive);
68 }
69
70 static void print(int divenr, cairo_t *cr, double x, double y, double w, double h)
71 {
72         struct dive *dive;
73
74         dive = get_dive(divenr);
75         if (!dive)
76                 return;
77         cairo_save(cr);
78         cairo_translate(cr, x, y);
79
80         /* We actually want to scale the text and the lines now */
81         cairo_scale(cr, 0.5, 0.5);
82
83         /* Dive plot in the upper 75% - note the scaling */
84         show_dive_profile(dive, cr, w*2, h*1.5);
85
86         /* Dive information in the lower 25% */
87         cairo_translate(cr, 0, h*1.5);
88         show_dive_text(dive, cr, w*2, h*0.5);
89
90         cairo_restore(cr);
91 }
92
93 static void draw_page(GtkPrintOperation *operation,
94                         GtkPrintContext *context,
95                         gint page_nr,
96                         gpointer user_data)
97 {
98         int nr;
99         cairo_t *cr;
100         double w, h;
101
102         cr = gtk_print_context_get_cairo_context(context);
103
104         w = gtk_print_context_get_width(context)/2;
105         h = gtk_print_context_get_height(context)/3;
106
107         nr = page_nr*6;
108         print(nr+0, cr, 0,   0, w, h);
109         print(nr+1, cr, w,   0, w, h);
110         print(nr+2, cr, 0,   h, w, h);
111         print(nr+3, cr, w,   h, w, h);
112         print(nr+2, cr, 0, 2*h, w, h);
113         print(nr+3, cr, w, 2*h, w, h);
114 }
115
116 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
117 {
118 }
119
120 static GtkPrintSettings *settings = NULL;
121
122 void do_print(void)
123 {
124         int pages;
125         GtkPrintOperation *print;
126         GtkPrintOperationResult res;
127
128         print = gtk_print_operation_new();
129         if (settings != NULL)
130                 gtk_print_operation_set_print_settings(print, settings);
131         pages = (dive_table.nr + 5) / 6;
132         gtk_print_operation_set_n_pages(print, pages);
133         g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
134         g_signal_connect(print, "draw_page", G_CALLBACK(draw_page), NULL);
135         res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
136                                          GTK_WINDOW(main_window), NULL);
137         if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
138                 if (settings != NULL)
139                         g_object_unref(settings);
140                 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
141         }
142         g_object_unref(print);
143 }