]> Pileus Git - ~andy/gtk/blob - tests/testprint.c
1da7af97d93e7560ef2a9a1be0ea1d5895100753
[~andy/gtk] / tests / testprint.c
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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22 #include <math.h>
23 #include <pango/pangocairo.h>
24 #include <gtk/gtk.h>
25 #include <gtk/gtkprintoperation.h>
26 #include "testprintfileoperation.h"
27
28 static void
29 request_page_setup (GtkPrintOperation *operation,
30                     GtkPrintContext *context,
31                     int page_nr,
32                     GtkPageSetup *setup)
33 {
34   /* Make the second page landscape mode a5 */
35   if (page_nr == 1)
36     {
37       GtkPaperSize *a5_size = gtk_paper_size_new ("iso_a5");
38       
39       gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_LANDSCAPE);
40       gtk_page_setup_set_paper_size (setup, a5_size);
41       gtk_paper_size_free (a5_size);
42     }
43 }
44
45 static void
46 draw_page (GtkPrintOperation *operation,
47            GtkPrintContext *context,
48            int page_nr)
49 {
50   cairo_t *cr;
51   PangoLayout *layout;
52   PangoFontDescription *desc;
53   
54   cr = gtk_print_context_get_cairo_context (context);
55
56   /* Draw a red rectangle, as wide as the paper (inside the margins) */
57   cairo_set_source_rgb (cr, 1.0, 0, 0);
58   cairo_rectangle (cr, 0, 0, gtk_print_context_get_width (context), 50);
59   
60   cairo_fill (cr);
61
62   /* Draw some lines */
63   cairo_move_to (cr, 20, 10);
64   cairo_line_to (cr, 40, 20);
65   cairo_arc (cr, 60, 60, 20, 0, G_PI);
66   cairo_line_to (cr, 80, 20);
67   
68   cairo_set_source_rgb (cr, 0, 0, 0);
69   cairo_set_line_width (cr, 5);
70   cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
71   cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
72   
73   cairo_stroke (cr);
74
75   /* Draw some text */
76   
77   layout = gtk_print_context_create_pango_layout (context);
78   pango_layout_set_text (layout, "Hello World! Printing is easy", -1);
79   desc = pango_font_description_from_string ("sans 28");
80   pango_layout_set_font_description (layout, desc);
81   pango_font_description_free (desc);
82
83   cairo_move_to (cr, 30, 20);
84   pango_cairo_layout_path (cr, layout);
85
86   /* Font Outline */
87   cairo_set_source_rgb (cr, 0.93, 1.0, 0.47);
88   cairo_set_line_width (cr, 0.5);
89   cairo_stroke_preserve (cr);
90
91   /* Font Fill */
92   cairo_set_source_rgb (cr, 0, 0.0, 1.0);
93   cairo_fill (cr);
94   
95   g_object_unref (layout);
96 }
97
98 int
99 main (int argc, char **argv)
100 {
101   GtkPrintOperation *print;
102   GtkPrintOperationResult res;
103   TestPrintFileOperation *print_file;
104
105   gtk_init (&argc, &argv);
106
107   /* Test some random drawing, with per-page paper settings */
108   print = gtk_print_operation_new ();
109   gtk_print_operation_set_n_pages (print, 2);
110   gtk_print_operation_set_unit (print, GTK_UNIT_MM);
111   gtk_print_operation_set_export_filename (print, "test.pdf");
112   g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), NULL);
113   g_signal_connect (print, "request_page_setup", G_CALLBACK (request_page_setup), NULL);
114   res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_EXPORT, NULL, NULL);
115
116   /* Test subclassing of GtkPrintOperation */
117   print_file = test_print_file_operation_new ("testprint.c");
118   test_print_file_operation_set_font_size (print_file, 12.0);
119   gtk_print_operation_set_export_filename (GTK_PRINT_OPERATION (print_file), "test2.pdf");
120   res = gtk_print_operation_run (GTK_PRINT_OPERATION (print_file), GTK_PRINT_OPERATION_ACTION_EXPORT, NULL, NULL);
121   
122   return 0;
123 }