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