Add powerbox hook
[gtk-with-powerbox.git] / tests / testprintfileoperation.c
blobbbd27c40bcabcce62f846787d34820e43a13ab53
1 #include <pango/pangocairo.h>
2 #include <gtk/gtk.h>
3 #include <math.h>
4 #include "testprintfileoperation.h"
6 /* In points */
7 #define HEADER_HEIGHT (10*72/25.4)
8 #define HEADER_GAP (3*72/25.4)
10 G_DEFINE_TYPE (TestPrintFileOperation, test_print_file_operation, GTK_TYPE_PRINT_OPERATION)
12 static void
13 test_print_file_operation_finalize (GObject *object)
15 TestPrintFileOperation *op = TEST_PRINT_FILE_OPERATION (object);
17 g_free (op->filename);
19 G_OBJECT_CLASS (test_print_file_operation_parent_class)->finalize (object);
22 static void
23 test_print_file_operation_init (TestPrintFileOperation *operation)
25 gtk_print_operation_set_unit (GTK_PRINT_OPERATION (operation), GTK_UNIT_POINTS);
26 operation->font_size = 14.0;
29 TestPrintFileOperation *
30 test_print_file_operation_new (const char *filename)
32 TestPrintFileOperation *op;
34 op = g_object_new (TEST_TYPE_PRINT_FILE_OPERATION, NULL);
36 op->filename = g_strdup (filename);
38 return op;
41 void
42 test_print_file_operation_set_font_size (TestPrintFileOperation *op,
43 double points)
45 op->font_size = points;
48 static void
49 test_print_file_operation_begin_print (GtkPrintOperation *operation, GtkPrintContext *context)
51 TestPrintFileOperation *op = TEST_PRINT_FILE_OPERATION (operation);
52 char *contents;
53 int i;
54 double height;
56 height = gtk_print_context_get_height (context) - HEADER_HEIGHT - HEADER_GAP;
58 op->lines_per_page = floor (height / op->font_size);
60 g_file_get_contents (op->filename,
61 &contents,
62 NULL, NULL);
64 op->lines = g_strsplit (contents, "\n", 0);
65 g_free (contents);
67 i = 0;
68 while (op->lines[i] != NULL)
69 i++;
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);
76 static void
77 test_print_file_operation_draw_page (GtkPrintOperation *operation,
78 GtkPrintContext *context,
79 int page_nr)
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;
89 cr = gtk_print_context_get_cairo_context (context);
90 width = gtk_print_context_get_width (context);
92 cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
94 cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
95 cairo_fill_preserve (cr);
97 cairo_set_source_rgb (cr, 0, 0, 0);
98 cairo_set_line_width (cr, 1);
99 cairo_stroke (cr);
101 layout = gtk_print_context_create_pango_layout (context);
103 desc = pango_font_description_from_string ("sans 14");
104 pango_layout_set_font_description (layout, desc);
105 pango_font_description_free (desc);
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);
111 pango_layout_get_size (layout, NULL, &layout_height);
112 text_height = (double)layout_height / PANGO_SCALE;
114 cairo_move_to (cr, width / 2, (HEADER_HEIGHT - text_height) / 2);
115 pango_cairo_show_layout (cr, layout);
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);
122 cairo_move_to (cr, width - 2, (HEADER_HEIGHT - text_height) / 2);
123 pango_cairo_show_layout (cr, layout);
125 g_object_unref (layout);
127 layout = gtk_print_context_create_pango_layout (context);
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);
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);
142 g_object_unref (layout);
145 static void
146 test_print_file_operation_end_print (GtkPrintOperation *operation, GtkPrintContext *context)
148 TestPrintFileOperation *op = TEST_PRINT_FILE_OPERATION (operation);
149 g_strfreev (op->lines);
152 static void
153 test_print_file_operation_class_init (TestPrintFileOperationClass *class)
155 GObjectClass *gobject_class = (GObjectClass *)class;
156 GtkPrintOperationClass *print_class = (GtkPrintOperationClass *)class;
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;