Skip to main content

gtk/auto/
print_context.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{PageSetup, ffi};
6use glib::translate::*;
7
8glib::wrapper! {
9    /// A GtkPrintContext encapsulates context information that is required when
10    /// drawing pages for printing, such as the cairo context and important
11    /// parameters like page size and resolution. It also lets you easily
12    /// create [`pango::Layout`][crate::pango::Layout] and [`pango::Context`][crate::pango::Context] objects that match the font metrics
13    /// of the cairo surface.
14    ///
15    /// GtkPrintContext objects gets passed to the [`begin-print`][struct@crate::PrintOperation#begin-print],
16    /// [`end-print`][struct@crate::PrintOperation#end-print], [`request-page-setup`][struct@crate::PrintOperation#request-page-setup] and
17    /// [`draw-page`][struct@crate::PrintOperation#draw-page] signals on the [`PrintOperation`][crate::PrintOperation].
18    ///
19    /// ## Using GtkPrintContext in a [`draw-page`][struct@crate::PrintOperation#draw-page] callback
20    ///
21    ///
22    ///
23    /// **⚠️ The following code is in C ⚠️**
24    ///
25    /// ```C
26    /// static void
27    /// draw_page (GtkPrintOperation *operation,
28    ///        GtkPrintContext   *context,
29    ///        int                page_nr)
30    /// {
31    ///   cairo_t *cr;
32    ///   PangoLayout *layout;
33    ///   PangoFontDescription *desc;
34    ///
35    ///   cr = gtk_print_context_get_cairo_context (context);
36    ///
37    ///   // Draw a red rectangle, as wide as the paper (inside the margins)
38    ///   cairo_set_source_rgb (cr, 1.0, 0, 0);
39    ///   cairo_rectangle (cr, 0, 0, gtk_print_context_get_width (context), 50);
40    ///
41    ///   cairo_fill (cr);
42    ///
43    ///   // Draw some lines
44    ///   cairo_move_to (cr, 20, 10);
45    ///   cairo_line_to (cr, 40, 20);
46    ///   cairo_arc (cr, 60, 60, 20, 0, M_PI);
47    ///   cairo_line_to (cr, 80, 20);
48    ///
49    ///   cairo_set_source_rgb (cr, 0, 0, 0);
50    ///   cairo_set_line_width (cr, 5);
51    ///   cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
52    ///   cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
53    ///
54    ///   cairo_stroke (cr);
55    ///
56    ///   // Draw some text
57    ///   layout = gtk_print_context_create_pango_layout (context);
58    ///   pango_layout_set_text (layout, "Hello World! Printing is easy", -1);
59    ///   desc = pango_font_description_from_string ("sans 28");
60    ///   pango_layout_set_font_description (layout, desc);
61    ///   pango_font_description_free (desc);
62    ///
63    ///   cairo_move_to (cr, 30, 20);
64    ///   pango_cairo_layout_path (cr, layout);
65    ///
66    ///   // Font Outline
67    ///   cairo_set_source_rgb (cr, 0.93, 1.0, 0.47);
68    ///   cairo_set_line_width (cr, 0.5);
69    ///   cairo_stroke_preserve (cr);
70    ///
71    ///   // Font Fill
72    ///   cairo_set_source_rgb (cr, 0, 0.0, 1.0);
73    ///   cairo_fill (cr);
74    ///
75    ///   g_object_unref (layout);
76    /// }
77    /// ```
78    ///
79    /// Printing support was added in GTK+ 2.10.
80    ///
81    /// # Implements
82    ///
83    /// [`trait@glib::ObjectExt`]
84    #[doc(alias = "GtkPrintContext")]
85    pub struct PrintContext(Object<ffi::GtkPrintContext>);
86
87    match fn {
88        type_ => || ffi::gtk_print_context_get_type(),
89    }
90}
91
92impl PrintContext {
93    /// Creates a new [`pango::Context`][crate::pango::Context] that can be used with the
94    /// [`PrintContext`][crate::PrintContext].
95    ///
96    /// # Returns
97    ///
98    /// a new Pango context for `self`
99    #[doc(alias = "gtk_print_context_create_pango_context")]
100    pub fn create_pango_context(&self) -> Option<pango::Context> {
101        unsafe {
102            from_glib_full(ffi::gtk_print_context_create_pango_context(
103                self.to_glib_none().0,
104            ))
105        }
106    }
107
108    /// Creates a new [`pango::Layout`][crate::pango::Layout] that is suitable for use
109    /// with the [`PrintContext`][crate::PrintContext].
110    ///
111    /// # Returns
112    ///
113    /// a new Pango layout for `self`
114    #[doc(alias = "gtk_print_context_create_pango_layout")]
115    pub fn create_pango_layout(&self) -> Option<pango::Layout> {
116        unsafe {
117            from_glib_full(ffi::gtk_print_context_create_pango_layout(
118                self.to_glib_none().0,
119            ))
120        }
121    }
122
123    /// Obtains the cairo context that is associated with the
124    /// [`PrintContext`][crate::PrintContext].
125    ///
126    /// # Returns
127    ///
128    /// the cairo context of `self`
129    #[doc(alias = "gtk_print_context_get_cairo_context")]
130    #[doc(alias = "get_cairo_context")]
131    pub fn cairo_context(&self) -> Option<cairo::Context> {
132        unsafe {
133            from_glib_none(ffi::gtk_print_context_get_cairo_context(
134                self.to_glib_none().0,
135            ))
136        }
137    }
138
139    /// Obtains the horizontal resolution of the [`PrintContext`][crate::PrintContext],
140    /// in dots per inch.
141    ///
142    /// # Returns
143    ///
144    /// the horizontal resolution of `self`
145    #[doc(alias = "gtk_print_context_get_dpi_x")]
146    #[doc(alias = "get_dpi_x")]
147    pub fn dpi_x(&self) -> f64 {
148        unsafe { ffi::gtk_print_context_get_dpi_x(self.to_glib_none().0) }
149    }
150
151    /// Obtains the vertical resolution of the [`PrintContext`][crate::PrintContext],
152    /// in dots per inch.
153    ///
154    /// # Returns
155    ///
156    /// the vertical resolution of `self`
157    #[doc(alias = "gtk_print_context_get_dpi_y")]
158    #[doc(alias = "get_dpi_y")]
159    pub fn dpi_y(&self) -> f64 {
160        unsafe { ffi::gtk_print_context_get_dpi_y(self.to_glib_none().0) }
161    }
162
163    /// Obtains the hardware printer margins of the [`PrintContext`][crate::PrintContext], in units.
164    ///
165    /// # Returns
166    ///
167    /// [`true`] if the hard margins were retrieved
168    ///
169    /// ## `top`
170    /// top hardware printer margin
171    ///
172    /// ## `bottom`
173    /// bottom hardware printer margin
174    ///
175    /// ## `left`
176    /// left hardware printer margin
177    ///
178    /// ## `right`
179    /// right hardware printer margin
180    #[doc(alias = "gtk_print_context_get_hard_margins")]
181    #[doc(alias = "get_hard_margins")]
182    pub fn hard_margins(&self) -> Option<(f64, f64, f64, f64)> {
183        unsafe {
184            let mut top = std::mem::MaybeUninit::uninit();
185            let mut bottom = std::mem::MaybeUninit::uninit();
186            let mut left = std::mem::MaybeUninit::uninit();
187            let mut right = std::mem::MaybeUninit::uninit();
188            let ret = from_glib(ffi::gtk_print_context_get_hard_margins(
189                self.to_glib_none().0,
190                top.as_mut_ptr(),
191                bottom.as_mut_ptr(),
192                left.as_mut_ptr(),
193                right.as_mut_ptr(),
194            ));
195            if ret {
196                Some((
197                    top.assume_init(),
198                    bottom.assume_init(),
199                    left.assume_init(),
200                    right.assume_init(),
201                ))
202            } else {
203                None
204            }
205        }
206    }
207
208    /// Obtains the height of the [`PrintContext`][crate::PrintContext], in pixels.
209    ///
210    /// # Returns
211    ///
212    /// the height of `self`
213    #[doc(alias = "gtk_print_context_get_height")]
214    #[doc(alias = "get_height")]
215    pub fn height(&self) -> f64 {
216        unsafe { ffi::gtk_print_context_get_height(self.to_glib_none().0) }
217    }
218
219    /// Obtains the [`PageSetup`][crate::PageSetup] that determines the page
220    /// dimensions of the [`PrintContext`][crate::PrintContext].
221    ///
222    /// # Returns
223    ///
224    /// the page setup of `self`
225    #[doc(alias = "gtk_print_context_get_page_setup")]
226    #[doc(alias = "get_page_setup")]
227    pub fn page_setup(&self) -> Option<PageSetup> {
228        unsafe { from_glib_none(ffi::gtk_print_context_get_page_setup(self.to_glib_none().0)) }
229    }
230
231    /// Returns a [`pango::FontMap`][crate::pango::FontMap] that is suitable for use
232    /// with the [`PrintContext`][crate::PrintContext].
233    ///
234    /// # Returns
235    ///
236    /// the font map of `self`
237    #[doc(alias = "gtk_print_context_get_pango_fontmap")]
238    #[doc(alias = "get_pango_fontmap")]
239    pub fn pango_fontmap(&self) -> Option<pango::FontMap> {
240        unsafe {
241            from_glib_none(ffi::gtk_print_context_get_pango_fontmap(
242                self.to_glib_none().0,
243            ))
244        }
245    }
246
247    /// Obtains the width of the [`PrintContext`][crate::PrintContext], in pixels.
248    ///
249    /// # Returns
250    ///
251    /// the width of `self`
252    #[doc(alias = "gtk_print_context_get_width")]
253    #[doc(alias = "get_width")]
254    pub fn width(&self) -> f64 {
255        unsafe { ffi::gtk_print_context_get_width(self.to_glib_none().0) }
256    }
257
258    /// Sets a new cairo context on a print context.
259    ///
260    /// This function is intended to be used when implementing
261    /// an internal print preview, it is not needed for printing,
262    /// since GTK+ itself creates a suitable cairo context in that
263    /// case.
264    /// ## `cr`
265    /// the cairo context
266    /// ## `dpi_x`
267    /// the horizontal resolution to use with `cr`
268    /// ## `dpi_y`
269    /// the vertical resolution to use with `cr`
270    #[doc(alias = "gtk_print_context_set_cairo_context")]
271    pub fn set_cairo_context(&self, cr: &cairo::Context, dpi_x: f64, dpi_y: f64) {
272        unsafe {
273            ffi::gtk_print_context_set_cairo_context(
274                self.to_glib_none().0,
275                mut_override(cr.to_glib_none().0),
276                dpi_x,
277                dpi_y,
278            );
279        }
280    }
281}