]> git.tdb.fi Git - ext/subsurface.git/blob - print.c
Fix Segmentation fault when trying to print an empty plot.
[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 = { .printer = 1 };
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 half of the page.. */
25         if (current_dive)
26                 plot(&gc, w, h/2, current_dive);
27
28         pango_cairo_show_layout(cr,layout);
29         g_object_unref(layout);
30 }
31
32 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
33 {
34 }
35
36 static GtkPrintSettings *settings = NULL;
37
38 void do_print(void)
39 {
40         GtkPrintOperation *print;
41         GtkPrintOperationResult res;
42
43         print = gtk_print_operation_new();
44         if (settings != NULL)
45                 gtk_print_operation_set_print_settings(print, settings);
46         gtk_print_operation_set_n_pages(print, 1);
47         g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
48         g_signal_connect(print, "draw_page", G_CALLBACK(draw_page), NULL);
49         res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
50                                          GTK_WINDOW(main_window), NULL);
51         if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
52                 if (settings != NULL)
53                         g_object_unref(settings);
54                 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
55         }
56         g_object_unref(print);
57 }