]> Pileus Git - ~andy/gtk/blob - tests/testprintfileoperation.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testprintfileoperation.c
1 #include <pango/pangocairo.h>
2 #include <gtk/gtk.h>
3 #include <math.h>
4 #include "testprintfileoperation.h"
5
6 /* In points */
7 #define HEADER_HEIGHT (10*72/25.4)
8 #define HEADER_GAP (3*72/25.4)
9
10 G_DEFINE_TYPE (TestPrintFileOperation, test_print_file_operation, GTK_TYPE_PRINT_OPERATION)
11
12 static void
13 test_print_file_operation_finalize (GObject *object)
14 {
15   TestPrintFileOperation *op = TEST_PRINT_FILE_OPERATION (object);
16   
17   g_free (op->filename);
18   
19   G_OBJECT_CLASS (test_print_file_operation_parent_class)->finalize (object);
20 }
21
22 static void
23 test_print_file_operation_init (TestPrintFileOperation *operation)
24 {
25   gtk_print_operation_set_unit (GTK_PRINT_OPERATION (operation), GTK_UNIT_POINTS);
26   operation->font_size = 14.0;
27 }
28
29 TestPrintFileOperation *
30 test_print_file_operation_new (const char *filename)
31 {
32   TestPrintFileOperation *op;
33
34   op = g_object_new (TEST_TYPE_PRINT_FILE_OPERATION, NULL);
35
36   op->filename = g_strdup (filename);
37   
38   return op;
39 }  
40   
41 void
42 test_print_file_operation_set_font_size (TestPrintFileOperation *op,
43                                          double points)
44 {
45   op->font_size = points;
46 }
47
48 static void
49 test_print_file_operation_begin_print (GtkPrintOperation *operation, GtkPrintContext *context)
50 {
51   TestPrintFileOperation *op = TEST_PRINT_FILE_OPERATION (operation);
52   char *contents;
53   int i;
54   double height;
55
56   height = gtk_print_context_get_height (context) - HEADER_HEIGHT - HEADER_GAP;
57   
58   op->lines_per_page = floor (height / op->font_size);
59   
60   g_file_get_contents (op->filename,
61                        &contents,
62                        NULL, NULL);
63
64   op->lines = g_strsplit (contents, "\n", 0);
65   g_free (contents);
66
67   i = 0;
68   while (op->lines[i] != NULL)
69     i++;
70   
71   op->num_lines = i;
72   op->num_pages = (op->num_lines - 1) / op->lines_per_page + 1;
73   gtk_print_operation_set_n_pages (operation, op->num_pages);
74 }
75
76 static void
77 test_print_file_operation_draw_page (GtkPrintOperation *operation,
78                                      GtkPrintContext *context,
79                                      int page_nr)
80 {
81   cairo_t *cr;
82   PangoLayout *layout;
83   TestPrintFileOperation *op = TEST_PRINT_FILE_OPERATION (operation);
84   double width, text_height;
85   int line, i, layout_height;
86   PangoFontDescription *desc;
87   char *page_str;
88
89   cr = gtk_print_context_get_cairo_context (context);
90   width = gtk_print_context_get_width (context);
91
92   cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
93   
94   cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
95   cairo_fill_preserve (cr);
96   
97   cairo_set_source_rgb (cr, 0, 0, 0);
98   cairo_set_line_width (cr, 1);
99   cairo_stroke (cr);
100
101   layout = gtk_print_context_create_pango_layout (context);
102
103   desc = pango_font_description_from_string ("sans 14");
104   pango_layout_set_font_description (layout, desc);
105   pango_font_description_free (desc);
106
107   pango_layout_set_text (layout, op->filename, -1);
108   pango_layout_set_width (layout, width);
109   pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
110                               
111   pango_layout_get_size (layout, NULL, &layout_height);
112   text_height = (double)layout_height / PANGO_SCALE;
113
114   cairo_move_to (cr, width / 2,  (HEADER_HEIGHT - text_height) / 2);
115   pango_cairo_show_layout (cr, layout);
116
117   page_str = g_strdup_printf ("%d/%d", page_nr + 1, op->num_pages);
118   pango_layout_set_text (layout, page_str, -1);
119   g_free (page_str);
120   pango_layout_set_alignment (layout, PANGO_ALIGN_RIGHT);
121                               
122   cairo_move_to (cr, width - 2, (HEADER_HEIGHT - text_height) / 2);
123   pango_cairo_show_layout (cr, layout);
124   
125   g_object_unref (layout);
126   
127   layout = gtk_print_context_create_pango_layout (context);
128   
129   desc = pango_font_description_from_string ("mono");
130   pango_font_description_set_size (desc, op->font_size * PANGO_SCALE);
131   pango_layout_set_font_description (layout, desc);
132   pango_font_description_free (desc);
133   
134   cairo_move_to (cr, 0, HEADER_HEIGHT + HEADER_GAP);
135   line = page_nr * op->lines_per_page;
136   for (i = 0; i < op->lines_per_page && line < op->num_lines; i++, line++) {
137     pango_layout_set_text (layout, op->lines[line], -1);
138     pango_cairo_show_layout (cr, layout);
139     cairo_rel_move_to (cr, 0, op->font_size);
140   }
141
142   g_object_unref (layout);
143 }
144
145 static void
146 test_print_file_operation_end_print (GtkPrintOperation *operation, GtkPrintContext *context)
147 {
148   TestPrintFileOperation *op = TEST_PRINT_FILE_OPERATION (operation);
149   g_strfreev (op->lines);
150 }
151
152 static void
153 test_print_file_operation_class_init (TestPrintFileOperationClass *class)
154 {
155   GObjectClass *gobject_class = (GObjectClass *)class;
156   GtkPrintOperationClass *print_class = (GtkPrintOperationClass *)class;
157
158   gobject_class->finalize = test_print_file_operation_finalize;
159   print_class->begin_print = test_print_file_operation_begin_print;
160   print_class->draw_page = test_print_file_operation_draw_page;
161   print_class->end_print = test_print_file_operation_end_print;
162   
163 }