1 #include <pango/pangocairo.h>
4 #include "testprintfileoperation.h"
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
)
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
);
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
);
42 test_print_file_operation_set_font_size (TestPrintFileOperation
*op
,
45 op
->font_size
= points
;
49 test_print_file_operation_begin_print (GtkPrintOperation
*operation
, GtkPrintContext
*context
)
51 TestPrintFileOperation
*op
= TEST_PRINT_FILE_OPERATION (operation
);
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
,
64 op
->lines
= g_strsplit (contents
, "\n", 0);
68 while (op
->lines
[i
] != NULL
)
72 op
->num_pages
= (op
->num_lines
- 1) / op
->lines_per_page
+ 1;
73 gtk_print_operation_set_n_pages (operation
, op
->num_pages
);
77 test_print_file_operation_draw_page (GtkPrintOperation
*operation
,
78 GtkPrintContext
*context
,
83 TestPrintFileOperation
*op
= TEST_PRINT_FILE_OPERATION (operation
);
84 double width
, text_height
;
85 int line
, i
, layout_height
;
86 PangoFontDescription
*desc
;
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);
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);
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
);
146 test_print_file_operation_end_print (GtkPrintOperation
*operation
, GtkPrintContext
*context
)
148 TestPrintFileOperation
*op
= TEST_PRINT_FILE_OPERATION (operation
);
149 g_strfreev (op
->lines
);
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
;