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
10G_DEFINE_TYPE (TestPrintFileOperation, test_print_file_operation, GTK_TYPE_PRINT_OPERATION)
11
12static void
13test_print_file_operation_finalize (GObject *object)
14{
15 TestPrintFileOperation *op = TEST_PRINT_FILE_OPERATION (object);
16
17 g_free (mem: op->filename);
18
19 G_OBJECT_CLASS (test_print_file_operation_parent_class)->finalize (object);
20}
21
22static void
23test_print_file_operation_init (TestPrintFileOperation *operation)
24{
25 gtk_print_operation_set_unit (GTK_PRINT_OPERATION (operation), unit: GTK_UNIT_POINTS);
26 operation->font_size = 14.0;
27}
28
29TestPrintFileOperation *
30test_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 (str: filename);
37
38 return op;
39}
40
41void
42test_print_file_operation_set_font_size (TestPrintFileOperation *op,
43 double points)
44{
45 op->font_size = points;
46}
47
48static void
49test_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 (x: height / op->font_size);
59
60 g_file_get_contents (filename: op->filename,
61 contents: &contents,
62 NULL, NULL);
63
64 op->lines = g_strsplit (string: contents, delimiter: "\n", max_tokens: 0);
65 g_free (mem: 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 (op: operation, n_pages: op->num_pages);
74}
75
76static void
77test_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, x: 0, y: 0, width, HEADER_HEIGHT);
93
94 cairo_set_source_rgb (cr, red: 0.8, green: 0.8, blue: 0.8);
95 cairo_fill_preserve (cr);
96
97 cairo_set_source_rgb (cr, red: 0, green: 0, blue: 0);
98 cairo_set_line_width (cr, width: 1);
99 cairo_stroke (cr);
100
101 layout = gtk_print_context_create_pango_layout (context);
102
103 desc = pango_font_description_from_string (str: "sans 14");
104 pango_layout_set_font_description (layout, desc);
105 pango_font_description_free (desc);
106
107 pango_layout_set_text (layout, text: op->filename, length: -1);
108 pango_layout_set_width (layout, width);
109 pango_layout_set_alignment (layout, alignment: PANGO_ALIGN_CENTER);
110
111 pango_layout_get_size (layout, NULL, height: &layout_height);
112 text_height = (double)layout_height / PANGO_SCALE;
113
114 cairo_move_to (cr, x: width / 2, y: (HEADER_HEIGHT - text_height) / 2);
115 pango_cairo_show_layout (cr, layout);
116
117 page_str = g_strdup_printf (format: "%d/%d", page_nr + 1, op->num_pages);
118 pango_layout_set_text (layout, text: page_str, length: -1);
119 g_free (mem: page_str);
120 pango_layout_set_alignment (layout, alignment: PANGO_ALIGN_RIGHT);
121
122 cairo_move_to (cr, x: width - 2, y: (HEADER_HEIGHT - text_height) / 2);
123 pango_cairo_show_layout (cr, layout);
124
125 g_object_unref (object: layout);
126
127 layout = gtk_print_context_create_pango_layout (context);
128
129 desc = pango_font_description_from_string (str: "mono");
130 pango_font_description_set_size (desc, size: 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, x: 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, text: op->lines[line], length: -1);
138 pango_cairo_show_layout (cr, layout);
139 cairo_rel_move_to (cr, dx: 0, dy: op->font_size);
140 }
141
142 g_object_unref (object: layout);
143}
144
145static void
146test_print_file_operation_end_print (GtkPrintOperation *operation, GtkPrintContext *context)
147{
148 TestPrintFileOperation *op = TEST_PRINT_FILE_OPERATION (operation);
149 g_strfreev (str_array: op->lines);
150}
151
152static void
153test_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}
164

source code of gtk/tests/testprintfileoperation.c