3 * GtkPrintOperation offers a simple API to support printing
4 * in a cross-platform way.
10 #include "demo-common.h"
13 #define HEADER_HEIGHT (10*72/25.4)
14 #define HEADER_GAP (3*72/25.4)
28 begin_print (GtkPrintOperation *operation,
29 GtkPrintContext *context,
32 PrintData *data = (PrintData *)user_data;
37 height = gtk_print_context_get_height (context) - HEADER_HEIGHT - HEADER_GAP;
39 data->lines_per_page = floor (height / data->font_size);
41 g_file_get_contents (data->filename, &contents, NULL, NULL);
43 data->lines = g_strsplit (contents, "\n", 0);
47 while (data->lines[i] != NULL)
51 data->num_pages = (data->num_lines - 1) / data->lines_per_page + 1;
52 gtk_print_operation_set_n_pages (operation, data->num_pages);
56 draw_page (GtkPrintOperation *operation,
57 GtkPrintContext *context,
61 PrintData *data = (PrintData *)user_data;
64 gint text_width, text_height;
67 PangoFontDescription *desc;
70 cr = gtk_print_context_get_cairo_context (context);
71 width = gtk_print_context_get_width (context);
73 cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
75 cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
76 cairo_fill_preserve (cr);
78 cairo_set_source_rgb (cr, 0, 0, 0);
79 cairo_set_line_width (cr, 1);
82 layout = gtk_print_context_create_pango_layout (context);
84 desc = pango_font_description_from_string ("sans 14");
85 pango_layout_set_font_description (layout, desc);
86 pango_font_description_free (desc);
88 pango_layout_set_text (layout, data->filename, -1);
89 pango_layout_get_pixel_size (layout, &text_width, &text_height);
91 if (text_width > width)
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);
98 cairo_move_to (cr, (width - text_width) / 2, (HEADER_HEIGHT - text_height) / 2);
99 pango_cairo_show_layout (cr, layout);
101 page_str = g_strdup_printf ("%d/%d", page_nr + 1, data->num_pages);
102 pango_layout_set_text (layout, page_str, -1);
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);
110 g_object_unref (layout);
112 layout = gtk_print_context_create_pango_layout (context);
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);
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++)
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);
129 g_object_unref (layout);
133 end_print (GtkPrintOperation *operation,
134 GtkPrintContext *context,
137 PrintData *data = (PrintData *)user_data;
139 g_free (data->filename);
140 g_strfreev (data->lines);
146 do_printing (GtkWidget *do_widget)
148 GtkPrintOperation *operation;
150 GError *error = NULL;
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;
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);
164 gtk_print_operation_set_use_full_page (operation, FALSE);
165 gtk_print_operation_set_unit (operation, GTK_UNIT_POINTS);
167 gtk_print_operation_run (operation, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW (do_widget), &error);
169 g_object_unref (operation);
175 dialog = gtk_message_dialog_new (GTK_WINDOW (do_widget),
176 GTK_DIALOG_DESTROY_WITH_PARENT,
179 "%s", error->message);
180 g_error_free (error);
182 g_signal_connect (dialog, "response",
183 G_CALLBACK (gtk_widget_destroy), NULL);
185 gtk_widget_show (dialog);