]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/printing.c
gtk-demo printing gives wrong results on win32 because it doesn't set the
[~andy/gtk] / demos / gtk-demo / printing.c
1 /* Printing
2  *
3  * GtkPrintOperation offers a simple API to support printing
4  * in a cross-platform way.
5  *
6  */
7
8 #include <math.h>
9 #include <gtk/gtk.h>
10 #include "demo-common.h"
11
12 /* In points */
13 #define HEADER_HEIGHT (10*72/25.4)
14 #define HEADER_GAP (3*72/25.4)
15
16 typedef struct 
17 {
18   gchar *filename;
19   gdouble font_size;
20
21   gint lines_per_page;  
22   gchar **lines;
23   gint num_lines;
24   gint num_pages;
25 } PrintData;
26
27 static void
28 begin_print (GtkPrintOperation *operation, 
29              GtkPrintContext   *context,
30              gpointer           user_data)
31 {
32   PrintData *data = (PrintData *)user_data;
33   char *contents;
34   int i;
35   double height;
36
37   height = gtk_print_context_get_height (context) - HEADER_HEIGHT - HEADER_GAP;
38   
39   data->lines_per_page = floor (height / data->font_size);
40   
41   g_file_get_contents (data->filename, &contents, NULL, NULL);
42
43   data->lines = g_strsplit (contents, "\n", 0);
44   g_free (contents);
45
46   i = 0;
47   while (data->lines[i] != NULL)
48     i++;
49   
50   data->num_lines = i;
51   data->num_pages = (data->num_lines - 1) / data->lines_per_page + 1;
52   gtk_print_operation_set_n_pages (operation, data->num_pages);
53 }
54
55 static void
56 draw_page (GtkPrintOperation *operation,
57            GtkPrintContext   *context,
58            gint               page_nr,
59            gpointer           user_data)
60 {
61   PrintData *data = (PrintData *)user_data;
62   cairo_t *cr;
63   PangoLayout *layout;
64   gint text_width, text_height;
65   gdouble width;
66   gint line, i;
67   PangoFontDescription *desc;
68   gchar *page_str;
69
70   cr = gtk_print_context_get_cairo_context (context);
71   width = gtk_print_context_get_width (context);
72
73   cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
74   
75   cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
76   cairo_fill_preserve (cr);
77   
78   cairo_set_source_rgb (cr, 0, 0, 0);
79   cairo_set_line_width (cr, 1);
80   cairo_stroke (cr);
81
82   layout = gtk_print_context_create_pango_layout (context);
83
84   desc = pango_font_description_from_string ("sans 14");
85   pango_layout_set_font_description (layout, desc);
86   pango_font_description_free (desc);
87
88   pango_layout_set_text (layout, data->filename, -1);
89   pango_layout_get_pixel_size (layout, &text_width, &text_height);
90
91   if (text_width > width)
92     {
93       pango_layout_set_width (layout, width);
94       pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_START);
95       pango_layout_get_pixel_size (layout, &text_width, &text_height);
96     }
97
98   cairo_move_to (cr, (width - text_width) / 2,  (HEADER_HEIGHT - text_height) / 2);
99   pango_cairo_show_layout (cr, layout);
100
101   page_str = g_strdup_printf ("%d/%d", page_nr + 1, data->num_pages);
102   pango_layout_set_text (layout, page_str, -1);
103   g_free (page_str);
104
105   pango_layout_set_width (layout, -1);
106   pango_layout_get_pixel_size (layout, &text_width, &text_height);
107   cairo_move_to (cr, width - text_width - 4, (HEADER_HEIGHT - text_height) / 2);
108   pango_cairo_show_layout (cr, layout);
109   
110   g_object_unref (layout);
111   
112   layout = gtk_print_context_create_pango_layout (context);
113   
114   desc = pango_font_description_from_string ("mono");
115   pango_font_description_set_size (desc, data->font_size * PANGO_SCALE);
116   pango_layout_set_font_description (layout, desc);
117   pango_font_description_free (desc);
118   
119   cairo_move_to (cr, 0, HEADER_HEIGHT + HEADER_GAP);
120   line = page_nr * data->lines_per_page;
121   for (i = 0; i < data->lines_per_page && line < data->num_lines; i++) 
122     {
123       pango_layout_set_text (layout, data->lines[line], -1);
124       pango_cairo_show_layout (cr, layout);
125       cairo_rel_move_to (cr, 0, data->font_size);
126       line++;
127     }
128
129   g_object_unref (layout);
130 }
131
132 static void
133 end_print (GtkPrintOperation *operation, 
134            GtkPrintContext   *context,
135            gpointer           user_data)
136 {
137   PrintData *data = (PrintData *)user_data;
138
139   g_free (data->filename);
140   g_strfreev (data->lines);
141   g_free (data);
142 }
143
144
145 GtkWidget *
146 do_printing (GtkWidget *do_widget)
147 {
148   GtkPrintOperation *operation;
149   PrintData *data;
150   GError *error = NULL;
151
152   operation = gtk_print_operation_new ();
153   data = g_new0 (PrintData, 1);
154   data->filename = demo_find_file ("printing.c", NULL);
155   data->font_size = 12.0;
156
157   g_signal_connect (G_OBJECT (operation), "begin-print", 
158                     G_CALLBACK (begin_print), data);
159   g_signal_connect (G_OBJECT (operation), "draw-page", 
160                     G_CALLBACK (draw_page), data);
161   g_signal_connect (G_OBJECT (operation), "end-print", 
162                     G_CALLBACK (end_print), data);
163
164   gtk_print_operation_set_use_full_page (operation, FALSE);
165   gtk_print_operation_set_unit (operation, GTK_UNIT_POINTS);
166
167   gtk_print_operation_run (operation, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW (do_widget), &error);
168
169   g_object_unref (operation);
170
171   if (error)
172     {
173       GtkWidget *dialog;
174       
175       dialog = gtk_message_dialog_new (GTK_WINDOW (do_widget),
176                                        GTK_DIALOG_DESTROY_WITH_PARENT,
177                                        GTK_MESSAGE_ERROR,
178                                        GTK_BUTTONS_CLOSE,
179                                        "%s", error->message);
180       g_error_free (error);
181       
182       g_signal_connect (dialog, "response",
183                         G_CALLBACK (gtk_widget_destroy), NULL);
184       
185       gtk_widget_show (dialog);      
186     }
187   
188
189   return NULL;
190 }