]> git.tdb.fi Git - ext/subsurface.git/blob - print.c
Add the capability to print a dive profile
[ext/subsurface.git] / print.c
1 #include <gtk/gtk.h>
2
3 #include "dive.h"
4 #include "display.h"
5
6 static void draw_page(GtkPrintOperation *operation,
7                         GtkPrintContext *context,
8                         gint page_nr,
9                         gpointer user_data)
10 {
11         cairo_t *cr;
12         PangoLayout *layout;
13         double w, h;
14         struct graphics_context gc = { 0 };
15
16         cr = gtk_print_context_get_cairo_context(context);
17         gc.cr = cr;
18
19         layout=gtk_print_context_create_pango_layout(context);
20
21         w = gtk_print_context_get_width(context);
22         h = gtk_print_context_get_height(context);
23
24         /* Do the profile on the top third of the page.. */
25         cairo_set_source_rgb(cr, 0, 0, 0);
26         cairo_rectangle(cr, 0, 0, w, h/3);
27         cairo_fill(cr);
28         plot(&gc, w, h/3, current_dive);
29
30         pango_cairo_show_layout(cr,layout);
31         g_object_unref(layout);
32 }
33
34 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
35 {
36 }
37
38 static GtkPrintSettings *settings = NULL;
39
40 void do_print(void)
41 {
42         GtkPrintOperation *print;
43         GtkPrintOperationResult res;
44
45         print = gtk_print_operation_new();
46         if (settings != NULL)
47                 gtk_print_operation_set_print_settings(print, settings);
48         gtk_print_operation_set_n_pages(print, 1);
49         g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
50         g_signal_connect(print, "draw_page", G_CALLBACK(draw_page), NULL);
51         res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
52                                          GTK_WINDOW(main_window), NULL);
53         if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
54                 if (settings != NULL)
55                         g_object_unref(settings);
56                 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
57         }
58         g_object_unref(print);
59 }