]> Pileus Git - ~andy/gtk/blob - tests/testnouiprint.c
09a678f375687ec3e66ef9d4e27febe7569d2bf9
[~andy/gtk] / tests / testnouiprint.c
1 /* -*- Mode: C; c-basic-offset: 2; -*- */
2 /* Gtk+ - non-ui printing
3  *
4  * Copyright (C) 2006 Alexander Larsson <alexl@redhat.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22 #include "gtk/gtkprintoperation.h"
23 #include <math.h>
24
25 static void
26 draw_page (GtkPrintOperation *operation,
27            GtkPrintContext *context,
28            int page_nr)
29 {
30   cairo_t *cr;
31   PangoLayout *layout;
32   PangoFontDescription *desc;
33   
34   cr = gtk_print_context_get_cairo_context (context);
35
36   /* Draw a red rectangle, as wide as the paper (inside the margins) */
37   cairo_set_source_rgb (cr, 1.0, 0, 0);
38   cairo_rectangle (cr, 0, 0, gtk_print_context_get_width (context), 50);
39   
40   cairo_fill (cr);
41
42   /* Draw some lines */
43   cairo_move_to (cr, 20, 10);
44   cairo_line_to (cr, 40, 20);
45   cairo_arc (cr, 60, 60, 20, 0, G_PI);
46   cairo_line_to (cr, 80, 20);
47   
48   cairo_set_source_rgb (cr, 0, 0, 0);
49   cairo_set_line_width (cr, 5);
50   cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
51   cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
52   
53   cairo_stroke (cr);
54
55   /* Draw some text */
56   
57   layout = gtk_print_context_create_pango_layout (context);
58   pango_layout_set_text (layout, "Hello World! Printing is easy", -1);
59   desc = pango_font_description_from_string ("sans 28");
60   pango_layout_set_font_description (layout, desc);
61   pango_font_description_free (desc);
62
63   cairo_move_to (cr, 30, 20);
64   pango_cairo_layout_path (cr, layout);
65
66   /* Font Outline */
67   cairo_set_source_rgb (cr, 0.93, 1.0, 0.47);
68   cairo_set_line_width (cr, 0.5);
69   cairo_stroke_preserve (cr);
70
71   /* Font Fill */
72   cairo_set_source_rgb (cr, 0, 0.0, 1.0);
73   cairo_fill (cr);
74   
75   g_object_unref (layout);
76 }
77
78
79 int
80 main (int argc, char **argv)
81 {
82   GMainLoop *loop;
83   GtkPrintOperation *print;
84   GtkPrintOperationResult res;
85   GtkPrintSettings *settings;
86
87   g_type_init (); 
88  
89   loop = g_main_loop_new (NULL, TRUE);
90
91   settings = gtk_print_settings_new ();
92   /* gtk_print_settings_set_printer (settings, "printer"); */
93   
94   print = gtk_print_operation_new ();
95   gtk_print_operation_set_print_settings (print, settings);
96   gtk_print_operation_set_n_pages (print, 1);
97   gtk_print_operation_set_unit (print, GTK_UNIT_MM);
98   g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), NULL);
99   res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT, NULL, NULL);
100
101   return 0;
102 }