1 | /* GTK - The GIMP Toolkit |
2 | * testprint.c: Print example |
3 | * Copyright (C) 2006, Red Hat, Inc. |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. |
9 | * |
10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU Lesser General Public |
16 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
17 | */ |
18 | |
19 | #include "config.h" |
20 | #include <math.h> |
21 | #include <pango/pangocairo.h> |
22 | #include <gtk/gtk.h> |
23 | #include "testprintfileoperation.h" |
24 | |
25 | static void |
26 | request_page_setup (GtkPrintOperation *operation, |
27 | GtkPrintContext *context, |
28 | int page_nr, |
29 | GtkPageSetup *setup) |
30 | { |
31 | /* Make the second page landscape mode a5 */ |
32 | if (page_nr == 1) |
33 | { |
34 | GtkPaperSize *a5_size = gtk_paper_size_new (name: "iso_a5" ); |
35 | |
36 | gtk_page_setup_set_orientation (setup, orientation: GTK_PAGE_ORIENTATION_LANDSCAPE); |
37 | gtk_page_setup_set_paper_size (setup, size: a5_size); |
38 | gtk_paper_size_free (size: a5_size); |
39 | } |
40 | } |
41 | |
42 | static void |
43 | draw_page (GtkPrintOperation *operation, |
44 | GtkPrintContext *context, |
45 | int page_nr) |
46 | { |
47 | cairo_t *cr; |
48 | PangoLayout *layout; |
49 | PangoFontDescription *desc; |
50 | |
51 | cr = gtk_print_context_get_cairo_context (context); |
52 | |
53 | /* Draw a red rectangle, as wide as the paper (inside the margins) */ |
54 | cairo_set_source_rgb (cr, red: 1.0, green: 0, blue: 0); |
55 | cairo_rectangle (cr, x: 0, y: 0, width: gtk_print_context_get_width (context), height: 50); |
56 | |
57 | cairo_fill (cr); |
58 | |
59 | /* Draw some lines */ |
60 | cairo_move_to (cr, x: 20, y: 10); |
61 | cairo_line_to (cr, x: 40, y: 20); |
62 | cairo_arc (cr, xc: 60, yc: 60, radius: 20, angle1: 0, G_PI); |
63 | cairo_line_to (cr, x: 80, y: 20); |
64 | |
65 | cairo_set_source_rgb (cr, red: 0, green: 0, blue: 0); |
66 | cairo_set_line_width (cr, width: 5); |
67 | cairo_set_line_cap (cr, line_cap: CAIRO_LINE_CAP_ROUND); |
68 | cairo_set_line_join (cr, line_join: CAIRO_LINE_JOIN_ROUND); |
69 | |
70 | cairo_stroke (cr); |
71 | |
72 | /* Draw some text */ |
73 | |
74 | layout = gtk_print_context_create_pango_layout (context); |
75 | pango_layout_set_text (layout, text: "Hello World! Printing is easy" , length: -1); |
76 | desc = pango_font_description_from_string (str: "sans 28" ); |
77 | pango_layout_set_font_description (layout, desc); |
78 | pango_font_description_free (desc); |
79 | |
80 | cairo_move_to (cr, x: 30, y: 20); |
81 | pango_cairo_layout_path (cr, layout); |
82 | |
83 | /* Font Outline */ |
84 | cairo_set_source_rgb (cr, red: 0.93, green: 1.0, blue: 0.47); |
85 | cairo_set_line_width (cr, width: 0.5); |
86 | cairo_stroke_preserve (cr); |
87 | |
88 | /* Font Fill */ |
89 | cairo_set_source_rgb (cr, red: 0, green: 0.0, blue: 1.0); |
90 | cairo_fill (cr); |
91 | |
92 | g_object_unref (object: layout); |
93 | } |
94 | |
95 | int |
96 | main (int argc, char **argv) |
97 | { |
98 | GtkPrintOperation *print; |
99 | TestPrintFileOperation *print_file; |
100 | |
101 | gtk_init (); |
102 | |
103 | /* Test some random drawing, with per-page paper settings */ |
104 | print = gtk_print_operation_new (); |
105 | gtk_print_operation_set_n_pages (op: print, n_pages: 2); |
106 | gtk_print_operation_set_unit (op: print, unit: GTK_UNIT_MM); |
107 | gtk_print_operation_set_export_filename (op: print, filename: "test.pdf" ); |
108 | g_signal_connect (print, "draw_page" , G_CALLBACK (draw_page), NULL); |
109 | g_signal_connect (print, "request_page_setup" , G_CALLBACK (request_page_setup), NULL); |
110 | gtk_print_operation_run (op: print, action: GTK_PRINT_OPERATION_ACTION_EXPORT, NULL, NULL); |
111 | |
112 | /* Test subclassing of GtkPrintOperation */ |
113 | print_file = test_print_file_operation_new (filename: "testprint.c" ); |
114 | test_print_file_operation_set_font_size (op: print_file, points: 12.0); |
115 | gtk_print_operation_set_export_filename (GTK_PRINT_OPERATION (print_file), filename: "test2.pdf" ); |
116 | gtk_print_operation_run (GTK_PRINT_OPERATION (print_file), action: GTK_PRINT_OPERATION_ACTION_EXPORT, NULL, NULL); |
117 | |
118 | return 0; |
119 | } |
120 | |