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;
53 gtk_print_operation_set_n_pages (operation, data->num_pages);
57 draw_page (GtkPrintOperation *operation,
58 GtkPrintContext *context,
62 PrintData *data = (PrintData *)user_data;
65 gint text_width, text_height;
68 PangoFontDescription *desc;
71 cr = gtk_print_context_get_cairo_context (context);
72 width = gtk_print_context_get_width (context);
74 cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
76 cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
77 cairo_fill_preserve (cr);
79 cairo_set_source_rgb (cr, 0, 0, 0);
80 cairo_set_line_width (cr, 1);
83 layout = gtk_print_context_create_pango_layout (context);
85 desc = pango_font_description_from_string ("sans 14");
86 pango_layout_set_font_description (layout, desc);
87 pango_font_description_free (desc);
89 pango_layout_set_text (layout, data->filename, -1);
90 pango_layout_get_pixel_size (layout, &text_width, &text_height);
92 if (text_width > width)
94 pango_layout_set_width (layout, width);
95 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_START);
96 pango_layout_get_pixel_size (layout, &text_width, &text_height);
99 cairo_move_to (cr, (width - text_width) / 2, (HEADER_HEIGHT - text_height) / 2);
100 pango_cairo_show_layout (cr, layout);
102 page_str = g_strdup_printf ("%d/%d", page_nr + 1, data->num_pages);
103 pango_layout_set_text (layout, page_str, -1);
106 pango_layout_set_width (layout, -1);
107 pango_layout_get_pixel_size (layout, &text_width, &text_height);
108 cairo_move_to (cr, width - text_width - 4, (HEADER_HEIGHT - text_height) / 2);
109 pango_cairo_show_layout (cr, layout);
111 g_object_unref (layout);
113 layout = gtk_print_context_create_pango_layout (context);
115 desc = pango_font_description_from_string ("monospace");
116 pango_font_description_set_size (desc, data->font_size * PANGO_SCALE);
117 pango_layout_set_font_description (layout, desc);
118 pango_font_description_free (desc);
120 cairo_move_to (cr, 0, HEADER_HEIGHT + HEADER_GAP);
121 line = page_nr * data->lines_per_page;
122 for (i = 0; i < data->lines_per_page && line < data->num_lines; i++)
124 pango_layout_set_text (layout, data->lines[line], -1);
125 pango_cairo_show_layout (cr, layout);
126 cairo_rel_move_to (cr, 0, data->font_size);
130 g_object_unref (layout);
134 end_print (GtkPrintOperation *operation,
135 GtkPrintContext *context,
138 PrintData *data = (PrintData *)user_data;
140 g_free (data->filename);
141 g_strfreev (data->lines);
147 do_printing (GtkWidget *do_widget)
149 GtkPrintOperation *operation;
150 GtkPrintSettings *settings;
152 GError *error = NULL;
154 operation = gtk_print_operation_new ();
155 data = g_new0 (PrintData, 1);
156 data->filename = demo_find_file ("printing.c", NULL);
157 data->font_size = 12.0;
159 g_signal_connect (G_OBJECT (operation), "begin-print",
160 G_CALLBACK (begin_print), data);
161 g_signal_connect (G_OBJECT (operation), "draw-page",
162 G_CALLBACK (draw_page), data);
163 g_signal_connect (G_OBJECT (operation), "end-print",
164 G_CALLBACK (end_print), data);
166 gtk_print_operation_set_use_full_page (operation, FALSE);
167 gtk_print_operation_set_unit (operation, GTK_UNIT_POINTS);
168 gtk_print_operation_set_embed_page_setup (operation, TRUE);
170 settings = gtk_print_settings_new ();
172 gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_BASENAME, "gtk-demo");
173 gtk_print_operation_set_print_settings (operation, settings);
175 gtk_print_operation_run (operation, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW (do_widget), &error);
177 g_object_unref (operation);
178 g_object_unref (settings);
184 dialog = gtk_message_dialog_new (GTK_WINDOW (do_widget),
185 GTK_DIALOG_DESTROY_WITH_PARENT,
188 "%s", error->message);
189 g_error_free (error);
191 g_signal_connect (dialog, "response",
192 G_CALLBACK (gtk_widget_destroy), NULL);
194 gtk_widget_show (dialog);