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