gtk4/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::{ffi, PageSetup};
6use glib::translate::*;
7
8glib::wrapper! {
9 /// A [`PrintContext`][crate::PrintContext] encapsulates context information that is required when
10 /// drawing pages for printing.
11 ///
12 /// This includes the cairo context and important parameters like page size
13 /// and resolution. It also lets you easily create [`pango::Layout`][crate::pango::Layout] and
14 /// [`pango::Context`][crate::pango::Context] objects that match the font metrics of the cairo surface.
15 ///
16 /// [`PrintContext`][crate::PrintContext] objects get passed to the
17 /// [`begin-print`][struct@crate::PrintOperation#begin-print],
18 /// [`end-print`][struct@crate::PrintOperation#end-print],
19 /// [`request-page-setup`][struct@crate::PrintOperation#request-page-setup] and
20 /// [`draw-page`][struct@crate::PrintOperation#draw-page] signals on the
21 /// [`PrintOperation`][crate::PrintOperation] object.
22 ///
23 /// ## Using GtkPrintContext in a ::draw-page callback
24 ///
25 /// **⚠️ The following code is in c ⚠️**
26 ///
27 /// ```c
28 /// static void
29 /// draw_page (GtkPrintOperation *operation,
30 /// GtkPrintContext *context,
31 /// int page_nr)
32 /// {
33 /// cairo_t *cr;
34 /// PangoLayout *layout;
35 /// PangoFontDescription *desc;
36 ///
37 /// cr = gtk_print_context_get_cairo_context (context);
38 ///
39 /// // Draw a red rectangle, as wide as the paper (inside the margins)
40 /// cairo_set_source_rgb (cr, 1.0, 0, 0);
41 /// cairo_rectangle (cr, 0, 0, gtk_print_context_get_width (context), 50);
42 ///
43 /// cairo_fill (cr);
44 ///
45 /// // Draw some lines
46 /// cairo_move_to (cr, 20, 10);
47 /// cairo_line_to (cr, 40, 20);
48 /// cairo_arc (cr, 60, 60, 20, 0, M_PI);
49 /// cairo_line_to (cr, 80, 20);
50 ///
51 /// cairo_set_source_rgb (cr, 0, 0, 0);
52 /// cairo_set_line_width (cr, 5);
53 /// cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
54 /// cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
55 ///
56 /// cairo_stroke (cr);
57 ///
58 /// // Draw some text
59 /// layout = gtk_print_context_create_pango_layout (context);
60 /// pango_layout_set_text (layout, "Hello World! Printing is easy", -1);
61 /// desc = pango_font_description_from_string ("sans 28");
62 /// pango_layout_set_font_description (layout, desc);
63 /// pango_font_description_free (desc);
64 ///
65 /// cairo_move_to (cr, 30, 20);
66 /// pango_cairo_layout_path (cr, layout);
67 ///
68 /// // Font Outline
69 /// cairo_set_source_rgb (cr, 0.93, 1.0, 0.47);
70 /// cairo_set_line_width (cr, 0.5);
71 /// cairo_stroke_preserve (cr);
72 ///
73 /// // Font Fill
74 /// cairo_set_source_rgb (cr, 0, 0.0, 1.0);
75 /// cairo_fill (cr);
76 ///
77 /// g_object_unref (layout);
78 /// }
79 /// ```
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) -> 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) -> 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) -> 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],
164 /// 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 = std::mem::MaybeUninit::uninit();
186 let mut bottom = std::mem::MaybeUninit::uninit();
187 let mut left = std::mem::MaybeUninit::uninit();
188 let mut right = std::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) -> 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) -> 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}