3 * GtkPrintOperation offers a simple API to support printing
4 * in a cross-platform way.
12 #define HEADER_HEIGHT (10*72/25.4)
13 #define HEADER_GAP (3*72/25.4)
27 begin_print (GtkPrintOperation *operation,
28 GtkPrintContext *context,
31 PrintData *data = (PrintData *)user_data;
36 height = gtk_print_context_get_height (context) - HEADER_HEIGHT - HEADER_GAP;
38 data->lines_per_page = floor (height / data->font_size);
40 bytes = g_resources_lookup_data (data->resourcename, 0, NULL);
42 data->lines = g_strsplit (g_bytes_get_data (bytes, NULL), "\n", 0);
43 g_bytes_unref (bytes);
46 while (data->lines[i] != NULL)
50 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->resourcename, -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 ("monospace");
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->resourcename);
140 g_strfreev (data->lines);
146 do_printing (GtkWidget *do_widget)
148 GtkPrintOperation *operation;
149 GtkPrintSettings *settings;
151 GError *error = NULL;
153 operation = gtk_print_operation_new ();
154 data = g_new0 (PrintData, 1);
155 data->resourcename = g_strdup ("/sources/printing.c");
156 data->font_size = 12.0;
158 g_signal_connect (G_OBJECT (operation), "begin-print",
159 G_CALLBACK (begin_print), data);
160 g_signal_connect (G_OBJECT (operation), "draw-page",
161 G_CALLBACK (draw_page), data);
162 g_signal_connect (G_OBJECT (operation), "end-print",
163 G_CALLBACK (end_print), data);
165 gtk_print_operation_set_use_full_page (operation, FALSE);
166 gtk_print_operation_set_unit (operation, GTK_UNIT_POINTS);
167 gtk_print_operation_set_embed_page_setup (operation, TRUE);
169 settings = gtk_print_settings_new ();
171 gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_BASENAME, "gtk-demo");
172 gtk_print_operation_set_print_settings (operation, settings);
174 gtk_print_operation_run (operation, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW (do_widget), &error);
176 g_object_unref (operation);
177 g_object_unref (settings);
183 dialog = gtk_message_dialog_new (GTK_WINDOW (do_widget),
184 GTK_DIALOG_DESTROY_WITH_PARENT,
187 "%s", error->message);
188 g_error_free (error);
190 g_signal_connect (dialog, "response",
191 G_CALLBACK (gtk_widget_destroy), NULL);
193 gtk_widget_show (dialog);