]> Pileus Git - ~andy/gtk/blob - gtk/gtkprintcontext.c
Silence new gcc warnings
[~andy/gtk] / gtk / gtkprintcontext.c
1 /* GTK - The GIMP Toolkit
2  * gtkprintcontext.c: Print Context
3  * Copyright (C) 2006, Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22 #include "gtkprintoperation-private.h"
23
24 typedef struct _GtkPrintContextClass GtkPrintContextClass;
25
26 #define GTK_IS_PRINT_CONTEXT_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_CONTEXT))
27 #define GTK_PRINT_CONTEXT_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_CONTEXT, GtkPrintContextClass))
28 #define GTK_PRINT_CONTEXT_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_CONTEXT, GtkPrintContextClass))
29
30 #define MM_PER_INCH 25.4
31 #define POINTS_PER_INCH 72
32
33 struct _GtkPrintContext
34 {
35   GObject parent_instance;
36
37   GtkPrintOperation *op;
38   cairo_t *cr;
39   GtkPageSetup *page_setup;
40
41   gdouble surface_dpi_x;
42   gdouble surface_dpi_y;
43   
44   gdouble pixels_per_unit_x;
45   gdouble pixels_per_unit_y;
46
47   gboolean has_hard_margins;
48   gdouble hard_margin_top;
49   gdouble hard_margin_bottom;
50   gdouble hard_margin_left;
51   gdouble hard_margin_right;
52
53 };
54
55 struct _GtkPrintContextClass
56 {
57   GObjectClass parent_class;
58 };
59
60 G_DEFINE_TYPE (GtkPrintContext, gtk_print_context, G_TYPE_OBJECT)
61
62 static void
63 gtk_print_context_finalize (GObject *object)
64 {
65   GtkPrintContext *context = GTK_PRINT_CONTEXT (object);
66
67   if (context->page_setup)
68     g_object_unref (context->page_setup);
69
70   if (context->cr)
71     cairo_destroy (context->cr);
72   
73   G_OBJECT_CLASS (gtk_print_context_parent_class)->finalize (object);
74 }
75
76 static void
77 gtk_print_context_init (GtkPrintContext *context)
78 {
79 }
80
81 static void
82 gtk_print_context_class_init (GtkPrintContextClass *class)
83 {
84   GObjectClass *gobject_class = (GObjectClass *)class;
85
86   gobject_class->finalize = gtk_print_context_finalize;
87 }
88
89
90 GtkPrintContext *
91 _gtk_print_context_new (GtkPrintOperation *op)
92 {
93   GtkPrintContext *context;
94
95   context = g_object_new (GTK_TYPE_PRINT_CONTEXT, NULL);
96
97   context->op = op;
98   context->cr = NULL;
99   context->has_hard_margins = FALSE;
100   
101   return context;
102 }
103
104 static PangoFontMap *
105 _gtk_print_context_get_fontmap (GtkPrintContext *context)
106 {
107   return pango_cairo_font_map_get_default ();
108 }
109
110 /**
111  * gtk_print_context_set_cairo_context:
112  * @context: a #GtkPrintContext
113  * @cr: the cairo context
114  * @dpi_x: the horizontal resolution to use with @cr
115  * @dpi_y: the vertical resolution to use with @cr
116  *
117  * Sets a new cairo context on a print context. 
118  * 
119  * This function is intended to be used when implementing
120  * an internal print preview, it is not needed for printing,
121  * since GTK+ itself creates a suitable cairo context in that
122  * case.
123  *
124  * Since: 2.10 
125  */
126 void
127 gtk_print_context_set_cairo_context (GtkPrintContext *context,
128                                      cairo_t         *cr,
129                                      double           dpi_x,
130                                      double           dpi_y)
131 {
132   if (context->cr)
133     cairo_destroy (context->cr);
134
135   context->cr = cairo_reference (cr);
136   context->surface_dpi_x = dpi_x;
137   context->surface_dpi_y = dpi_y;
138
139   switch (context->op->priv->unit)
140     {
141     default:
142     case GTK_UNIT_PIXEL:
143       /* Do nothing, this is the cairo default unit */
144       context->pixels_per_unit_x = 1.0;
145       context->pixels_per_unit_y = 1.0;
146       break;
147     case GTK_UNIT_POINTS:
148       context->pixels_per_unit_x = dpi_x / POINTS_PER_INCH;
149       context->pixels_per_unit_y = dpi_y / POINTS_PER_INCH;
150       break;
151     case GTK_UNIT_INCH:
152       context->pixels_per_unit_x = dpi_x;
153       context->pixels_per_unit_y = dpi_y;
154       break;
155     case GTK_UNIT_MM:
156       context->pixels_per_unit_x = dpi_x / MM_PER_INCH;
157       context->pixels_per_unit_y = dpi_y / MM_PER_INCH;
158       break;
159     }
160   cairo_scale (context->cr,
161                context->pixels_per_unit_x,
162                context->pixels_per_unit_y);
163 }
164
165
166 void
167 _gtk_print_context_rotate_according_to_orientation (GtkPrintContext *context)
168 {
169   cairo_t *cr = context->cr;
170   cairo_matrix_t matrix;
171   GtkPaperSize *paper_size;
172   gdouble width, height;
173
174   paper_size = gtk_page_setup_get_paper_size (context->page_setup);
175
176   width = gtk_paper_size_get_width (paper_size, GTK_UNIT_INCH);
177   width = width * context->surface_dpi_x / context->pixels_per_unit_x;
178   height = gtk_paper_size_get_height (paper_size, GTK_UNIT_INCH);
179   height = height * context->surface_dpi_y / context->pixels_per_unit_y;
180   
181   switch (gtk_page_setup_get_orientation (context->page_setup))
182     {
183     default:
184     case GTK_PAGE_ORIENTATION_PORTRAIT:
185       break;
186     case GTK_PAGE_ORIENTATION_LANDSCAPE:
187       cairo_translate (cr, 0, height);
188       cairo_matrix_init (&matrix,
189                          0, -1,
190                          1,  0,
191                          0,  0);
192       cairo_transform (cr, &matrix);
193       break;
194     case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
195       cairo_translate (cr, width, height);
196       cairo_matrix_init (&matrix,
197                          -1,  0,
198                           0, -1,
199                           0,  0);
200       cairo_transform (cr, &matrix);
201       break;
202     case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
203       cairo_translate (cr, width, 0);
204       cairo_matrix_init (&matrix,
205                           0,  1,
206                          -1,  0,
207                           0,  0);
208       cairo_transform (cr, &matrix);
209       break;
210     }
211 }
212
213 void
214 _gtk_print_context_translate_into_margin (GtkPrintContext *context)
215 {
216   gdouble left, top;
217
218   g_return_if_fail (GTK_IS_PRINT_CONTEXT (context));
219
220   /* We do it this way to also handle GTK_UNIT_PIXELS */
221   left = gtk_page_setup_get_left_margin (context->page_setup, GTK_UNIT_INCH);
222   top = gtk_page_setup_get_top_margin (context->page_setup, GTK_UNIT_INCH);
223
224   cairo_translate (context->cr,
225                    left * context->surface_dpi_x / context->pixels_per_unit_x,
226                    top * context->surface_dpi_y / context->pixels_per_unit_y);
227 }
228
229 void
230 _gtk_print_context_set_page_setup (GtkPrintContext *context,
231                                    GtkPageSetup    *page_setup)
232 {
233   g_return_if_fail (GTK_IS_PRINT_CONTEXT (context));
234   g_return_if_fail (page_setup == NULL ||
235                     GTK_IS_PAGE_SETUP (page_setup));
236   
237   g_object_ref (page_setup);
238
239   if (context->page_setup != NULL)
240     g_object_unref (context->page_setup);
241
242   context->page_setup = page_setup;
243 }
244
245 /**
246  * gtk_print_context_get_cairo_context:
247  * @context: a #GtkPrintContext
248  *
249  * Obtains the cairo context that is associated with the
250  * #GtkPrintContext.
251  *
252  * Return value: (transfer none): the cairo context of @context
253  *
254  * Since: 2.10
255  */
256 cairo_t *
257 gtk_print_context_get_cairo_context (GtkPrintContext *context)
258 {
259   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
260
261   return context->cr;
262 }
263
264 /**
265  * gtk_print_context_get_page_setup:
266  * @context: a #GtkPrintContext
267  *
268  * Obtains the #GtkPageSetup that determines the page
269  * dimensions of the #GtkPrintContext.
270  *
271  * Return value: (transfer none): the page setup of @context
272  *
273  * Since: 2.10
274  */
275 GtkPageSetup *
276 gtk_print_context_get_page_setup (GtkPrintContext *context)
277 {
278   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
279
280   return context->page_setup;
281 }
282
283 /**
284  * gtk_print_context_get_width:
285  * @context: a #GtkPrintContext
286  *
287  * Obtains the width of the #GtkPrintContext, in pixels.
288  *
289  * Return value: the width of @context
290  *
291  * Since: 2.10 
292  */
293 gdouble
294 gtk_print_context_get_width (GtkPrintContext *context)
295 {
296   GtkPrintOperationPrivate *priv;
297   gdouble width;
298
299   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);
300
301   priv = context->op->priv;
302
303   if (priv->use_full_page)
304     width = gtk_page_setup_get_paper_width (context->page_setup, GTK_UNIT_INCH);
305   else
306     width = gtk_page_setup_get_page_width (context->page_setup, GTK_UNIT_INCH);
307
308   /* Really dpi_x? What about landscape? what does dpi_x mean in that case? */
309   return width * context->surface_dpi_x / context->pixels_per_unit_x;
310 }
311
312 /**
313  * gtk_print_context_get_height:
314  * @context: a #GtkPrintContext
315  * 
316  * Obtains the height of the #GtkPrintContext, in pixels.
317  *
318  * Return value: the height of @context
319  *
320  * Since: 2.10
321  */
322 gdouble
323 gtk_print_context_get_height (GtkPrintContext *context)
324 {
325   GtkPrintOperationPrivate *priv;
326   gdouble height;
327
328   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);
329
330   priv = context->op->priv;
331
332   if (priv->use_full_page)
333     height = gtk_page_setup_get_paper_height (context->page_setup, GTK_UNIT_INCH);
334   else
335     height = gtk_page_setup_get_page_height (context->page_setup, GTK_UNIT_INCH);
336
337   /* Really dpi_y? What about landscape? what does dpi_y mean in that case? */
338   return height * context->surface_dpi_y / context->pixels_per_unit_y;
339 }
340
341 /**
342  * gtk_print_context_get_dpi_x:
343  * @context: a #GtkPrintContext
344  * 
345  * Obtains the horizontal resolution of the #GtkPrintContext,
346  * in dots per inch.
347  *
348  * Return value: the horizontal resolution of @context
349  *
350  * Since: 2.10
351  */
352 gdouble
353 gtk_print_context_get_dpi_x (GtkPrintContext *context)
354 {
355   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);
356
357   return context->surface_dpi_x;
358 }
359
360 /**
361  * gtk_print_context_get_dpi_y:
362  * @context: a #GtkPrintContext
363  * 
364  * Obtains the vertical resolution of the #GtkPrintContext,
365  * in dots per inch.
366  *
367  * Return value: the vertical resolution of @context
368  *
369  * Since: 2.10
370  */
371 gdouble
372 gtk_print_context_get_dpi_y (GtkPrintContext *context)
373 {
374   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);
375
376   return context->surface_dpi_y;
377 }
378
379 /**
380  * gtk_print_context_get_hard_margins:
381  * @context: a #GtkPrintContext
382  * @top: (out): top hardware printer margin
383  * @bottom: (out): bottom hardware printer margin
384  * @left: (out): left hardware printer margin
385  * @right: (out): right hardware printer margin
386  *
387  * Obtains the hardware printer margins of the #GtkPrintContext, in units.
388  *
389  * Return value: %TRUE if the hard margins were retrieved
390  *
391  * Since: 2.20
392  */
393 gboolean
394 gtk_print_context_get_hard_margins (GtkPrintContext *context,
395                                     gdouble         *top,
396                                     gdouble         *bottom,
397                                     gdouble         *left,
398                                     gdouble         *right)
399 {
400   if (context->has_hard_margins)
401     {
402       *top    = context->hard_margin_top / context->pixels_per_unit_y;
403       *bottom = context->hard_margin_bottom / context->pixels_per_unit_y;
404       *left   = context->hard_margin_left / context->pixels_per_unit_x;
405       *right  = context->hard_margin_right / context->pixels_per_unit_x;
406     }
407
408   return context->has_hard_margins;
409 }
410
411 /**
412  * gtk_print_context_set_hard_margins:
413  * @context: a #GtkPrintContext
414  * @top: top hardware printer margin
415  * @bottom: bottom hardware printer margin
416  * @left: left hardware printer margin
417  * @right: right hardware printer margin
418  *
419  * set the hard margins in pixel coordinates
420  */
421 void
422 _gtk_print_context_set_hard_margins (GtkPrintContext *context,
423                                      gdouble          top,
424                                      gdouble          bottom,
425                                      gdouble          left,
426                                      gdouble          right)
427 {
428   context->hard_margin_top    = top;
429   context->hard_margin_bottom = bottom;
430   context->hard_margin_left   = left;
431   context->hard_margin_right  = right;
432   context->has_hard_margins   = TRUE;
433 }
434
435 /**
436  * gtk_print_context_get_pango_fontmap:
437  * @context: a #GtkPrintContext
438  *
439  * Returns a #PangoFontMap that is suitable for use
440  * with the #GtkPrintContext.
441  *
442  * Return value: (transfer none): the font map of @context
443  *
444  * Since: 2.10
445  */
446 PangoFontMap *
447 gtk_print_context_get_pango_fontmap (GtkPrintContext *context)
448 {
449   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
450
451   return _gtk_print_context_get_fontmap (context);
452 }
453
454 /**
455  * gtk_print_context_create_pango_context:
456  * @context: a #GtkPrintContext
457  *
458  * Creates a new #PangoContext that can be used with the
459  * #GtkPrintContext.
460  *
461  * Return value: (transfer full): a new Pango context for @context
462  * 
463  * Since: 2.10
464  */
465 PangoContext *
466 gtk_print_context_create_pango_context (GtkPrintContext *context)
467 {
468   PangoContext *pango_context;
469   cairo_font_options_t *options;
470
471   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
472   
473   pango_context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (_gtk_print_context_get_fontmap (context)));
474
475   options = cairo_font_options_create ();
476   cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_OFF);
477   pango_cairo_context_set_font_options (pango_context, options);
478   cairo_font_options_destroy (options);
479   
480   /* We use the unit-scaled resolution, as we still want 
481    * fonts given in points to work 
482    */
483   pango_cairo_context_set_resolution (pango_context,
484                                       context->surface_dpi_y / context->pixels_per_unit_y);
485   return pango_context;
486 }
487
488 /**
489  * gtk_print_context_create_pango_layout:
490  * @context: a #GtkPrintContext
491  *
492  * Creates a new #PangoLayout that is suitable for use
493  * with the #GtkPrintContext.
494  * 
495  * Return value: (transfer full): a new Pango layout for @context
496  *
497  * Since: 2.10
498  */
499 PangoLayout *
500 gtk_print_context_create_pango_layout (GtkPrintContext *context)
501 {
502   PangoContext *pango_context;
503   PangoLayout *layout;
504
505   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
506
507   pango_context = gtk_print_context_create_pango_context (context);
508   layout = pango_layout_new (pango_context);
509
510   pango_cairo_update_context (context->cr, pango_context);
511   g_object_unref (pango_context);
512
513   return layout;
514 }