gtk/auto/page_setup.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::{PageOrientation, PaperSize, Unit};
6use glib::translate::*;
7use std::{fmt, ptr};
8
9glib::wrapper! {
10 /// A GtkPageSetup object stores the page size, orientation and margins.
11 /// The idea is that you can get one of these from the page setup dialog
12 /// and then pass it to the [`PrintOperation`][crate::PrintOperation] when printing.
13 /// The benefit of splitting this out of the [`PrintSettings`][crate::PrintSettings] is that
14 /// these affect the actual layout of the page, and thus need to be set
15 /// long before user prints.
16 ///
17 /// ## Margins ## {`print`-margins}
18 /// The margins specified in this object are the “print margins”, i.e. the
19 /// parts of the page that the printer cannot print on. These are different
20 /// from the layout margins that a word processor uses; they are typically
21 /// used to determine the minimal size for the layout
22 /// margins.
23 ///
24 /// To obtain a [`PageSetup`][crate::PageSetup] use [`new()`][Self::new()] to get the defaults,
25 /// or use [`print_run_page_setup_dialog()`][crate::print_run_page_setup_dialog()] to show the page setup dialog
26 /// and receive the resulting page setup.
27 ///
28 /// ## A page setup dialog
29 ///
30 ///
31 ///
32 /// **⚠️ The following code is in C ⚠️**
33 ///
34 /// ```C
35 /// static GtkPrintSettings *settings = NULL;
36 /// static GtkPageSetup *page_setup = NULL;
37 ///
38 /// static void
39 /// do_page_setup (void)
40 /// {
41 /// GtkPageSetup *new_page_setup;
42 ///
43 /// if (settings == NULL)
44 /// settings = gtk_print_settings_new ();
45 ///
46 /// new_page_setup = gtk_print_run_page_setup_dialog (GTK_WINDOW (main_window),
47 /// page_setup, settings);
48 ///
49 /// if (page_setup)
50 /// g_object_unref (page_setup);
51 ///
52 /// page_setup = new_page_setup;
53 /// }
54 /// ```
55 ///
56 /// Printing support was added in GTK+ 2.10.
57 ///
58 /// # Implements
59 ///
60 /// [`trait@glib::ObjectExt`]
61 #[doc(alias = "GtkPageSetup")]
62 pub struct PageSetup(Object<ffi::GtkPageSetup>);
63
64 match fn {
65 type_ => || ffi::gtk_page_setup_get_type(),
66 }
67}
68
69impl PageSetup {
70 /// Creates a new [`PageSetup`][crate::PageSetup].
71 ///
72 /// # Returns
73 ///
74 /// a new [`PageSetup`][crate::PageSetup].
75 #[doc(alias = "gtk_page_setup_new")]
76 pub fn new() -> PageSetup {
77 assert_initialized_main_thread!();
78 unsafe { from_glib_full(ffi::gtk_page_setup_new()) }
79 }
80
81 /// Reads the page setup from the file `file_name`. Returns a
82 /// new [`PageSetup`][crate::PageSetup] object with the restored page setup,
83 /// or [`None`] if an error occurred. See [`to_file()`][Self::to_file()].
84 /// ## `file_name`
85 /// the filename to read the page setup from
86 ///
87 /// # Returns
88 ///
89 /// the restored [`PageSetup`][crate::PageSetup]
90 #[doc(alias = "gtk_page_setup_new_from_file")]
91 #[doc(alias = "new_from_file")]
92 pub fn from_file(file_name: impl AsRef<std::path::Path>) -> Result<PageSetup, glib::Error> {
93 assert_initialized_main_thread!();
94 unsafe {
95 let mut error = ptr::null_mut();
96 let ret =
97 ffi::gtk_page_setup_new_from_file(file_name.as_ref().to_glib_none().0, &mut error);
98 if error.is_null() {
99 Ok(from_glib_full(ret))
100 } else {
101 Err(from_glib_full(error))
102 }
103 }
104 }
105
106 /// Desrialize a page setup from an a{sv} variant in
107 /// the format produced by [`to_gvariant()`][Self::to_gvariant()].
108 /// ## `variant`
109 /// an a{sv} [`glib::Variant`][struct@crate::glib::Variant]
110 ///
111 /// # Returns
112 ///
113 /// a new [`PageSetup`][crate::PageSetup] object
114 #[doc(alias = "gtk_page_setup_new_from_gvariant")]
115 #[doc(alias = "new_from_gvariant")]
116 pub fn from_gvariant(variant: &glib::Variant) -> PageSetup {
117 assert_initialized_main_thread!();
118 unsafe {
119 from_glib_full(ffi::gtk_page_setup_new_from_gvariant(
120 variant.to_glib_none().0,
121 ))
122 }
123 }
124
125 /// Reads the page setup from the group `group_name` in the key file
126 /// `key_file`. Returns a new [`PageSetup`][crate::PageSetup] object with the restored
127 /// page setup, or [`None`] if an error occurred.
128 /// ## `key_file`
129 /// the [`glib::KeyFile`][crate::glib::KeyFile] to retrieve the page_setup from
130 /// ## `group_name`
131 /// the name of the group in the key_file to read, or [`None`]
132 /// to use the default name “Page Setup”
133 ///
134 /// # Returns
135 ///
136 /// the restored [`PageSetup`][crate::PageSetup]
137 #[doc(alias = "gtk_page_setup_new_from_key_file")]
138 #[doc(alias = "new_from_key_file")]
139 pub fn from_key_file(
140 key_file: &glib::KeyFile,
141 group_name: Option<&str>,
142 ) -> Result<PageSetup, glib::Error> {
143 assert_initialized_main_thread!();
144 unsafe {
145 let mut error = ptr::null_mut();
146 let ret = ffi::gtk_page_setup_new_from_key_file(
147 key_file.to_glib_none().0,
148 group_name.to_glib_none().0,
149 &mut error,
150 );
151 if error.is_null() {
152 Ok(from_glib_full(ret))
153 } else {
154 Err(from_glib_full(error))
155 }
156 }
157 }
158
159 #[doc(alias = "gtk_page_setup_copy")]
160 #[must_use]
161 pub fn copy(&self) -> Option<PageSetup> {
162 unsafe { from_glib_full(ffi::gtk_page_setup_copy(self.to_glib_none().0)) }
163 }
164
165 /// Gets the bottom margin in units of `unit`.
166 /// ## `unit`
167 /// the unit for the return value
168 ///
169 /// # Returns
170 ///
171 /// the bottom margin
172 #[doc(alias = "gtk_page_setup_get_bottom_margin")]
173 #[doc(alias = "get_bottom_margin")]
174 pub fn bottom_margin(&self, unit: Unit) -> f64 {
175 unsafe { ffi::gtk_page_setup_get_bottom_margin(self.to_glib_none().0, unit.into_glib()) }
176 }
177
178 /// Gets the left margin in units of `unit`.
179 /// ## `unit`
180 /// the unit for the return value
181 ///
182 /// # Returns
183 ///
184 /// the left margin
185 #[doc(alias = "gtk_page_setup_get_left_margin")]
186 #[doc(alias = "get_left_margin")]
187 pub fn left_margin(&self, unit: Unit) -> f64 {
188 unsafe { ffi::gtk_page_setup_get_left_margin(self.to_glib_none().0, unit.into_glib()) }
189 }
190
191 /// Gets the page orientation of the [`PageSetup`][crate::PageSetup].
192 ///
193 /// # Returns
194 ///
195 /// the page orientation
196 #[doc(alias = "gtk_page_setup_get_orientation")]
197 #[doc(alias = "get_orientation")]
198 pub fn orientation(&self) -> PageOrientation {
199 unsafe { from_glib(ffi::gtk_page_setup_get_orientation(self.to_glib_none().0)) }
200 }
201
202 /// Returns the page height in units of `unit`.
203 ///
204 /// Note that this function takes orientation and
205 /// margins into consideration.
206 /// See [`paper_height()`][Self::paper_height()].
207 /// ## `unit`
208 /// the unit for the return value
209 ///
210 /// # Returns
211 ///
212 /// the page height.
213 #[doc(alias = "gtk_page_setup_get_page_height")]
214 #[doc(alias = "get_page_height")]
215 pub fn page_height(&self, unit: Unit) -> f64 {
216 unsafe { ffi::gtk_page_setup_get_page_height(self.to_glib_none().0, unit.into_glib()) }
217 }
218
219 /// Returns the page width in units of `unit`.
220 ///
221 /// Note that this function takes orientation and
222 /// margins into consideration.
223 /// See [`paper_width()`][Self::paper_width()].
224 /// ## `unit`
225 /// the unit for the return value
226 ///
227 /// # Returns
228 ///
229 /// the page width.
230 #[doc(alias = "gtk_page_setup_get_page_width")]
231 #[doc(alias = "get_page_width")]
232 pub fn page_width(&self, unit: Unit) -> f64 {
233 unsafe { ffi::gtk_page_setup_get_page_width(self.to_glib_none().0, unit.into_glib()) }
234 }
235
236 /// Returns the paper height in units of `unit`.
237 ///
238 /// Note that this function takes orientation, but
239 /// not margins into consideration.
240 /// See [`page_height()`][Self::page_height()].
241 /// ## `unit`
242 /// the unit for the return value
243 ///
244 /// # Returns
245 ///
246 /// the paper height.
247 #[doc(alias = "gtk_page_setup_get_paper_height")]
248 #[doc(alias = "get_paper_height")]
249 pub fn paper_height(&self, unit: Unit) -> f64 {
250 unsafe { ffi::gtk_page_setup_get_paper_height(self.to_glib_none().0, unit.into_glib()) }
251 }
252
253 /// Gets the paper size of the [`PageSetup`][crate::PageSetup].
254 ///
255 /// # Returns
256 ///
257 /// the paper size
258 #[doc(alias = "gtk_page_setup_get_paper_size")]
259 #[doc(alias = "get_paper_size")]
260 pub fn paper_size(&self) -> PaperSize {
261 unsafe { from_glib_none(ffi::gtk_page_setup_get_paper_size(self.to_glib_none().0)) }
262 }
263
264 /// Returns the paper width in units of `unit`.
265 ///
266 /// Note that this function takes orientation, but
267 /// not margins into consideration.
268 /// See [`page_width()`][Self::page_width()].
269 /// ## `unit`
270 /// the unit for the return value
271 ///
272 /// # Returns
273 ///
274 /// the paper width.
275 #[doc(alias = "gtk_page_setup_get_paper_width")]
276 #[doc(alias = "get_paper_width")]
277 pub fn paper_width(&self, unit: Unit) -> f64 {
278 unsafe { ffi::gtk_page_setup_get_paper_width(self.to_glib_none().0, unit.into_glib()) }
279 }
280
281 /// Gets the right margin in units of `unit`.
282 /// ## `unit`
283 /// the unit for the return value
284 ///
285 /// # Returns
286 ///
287 /// the right margin
288 #[doc(alias = "gtk_page_setup_get_right_margin")]
289 #[doc(alias = "get_right_margin")]
290 pub fn right_margin(&self, unit: Unit) -> f64 {
291 unsafe { ffi::gtk_page_setup_get_right_margin(self.to_glib_none().0, unit.into_glib()) }
292 }
293
294 /// Gets the top margin in units of `unit`.
295 /// ## `unit`
296 /// the unit for the return value
297 ///
298 /// # Returns
299 ///
300 /// the top margin
301 #[doc(alias = "gtk_page_setup_get_top_margin")]
302 #[doc(alias = "get_top_margin")]
303 pub fn top_margin(&self, unit: Unit) -> f64 {
304 unsafe { ffi::gtk_page_setup_get_top_margin(self.to_glib_none().0, unit.into_glib()) }
305 }
306
307 /// Reads the page setup from the file `file_name`.
308 /// See [`to_file()`][Self::to_file()].
309 /// ## `file_name`
310 /// the filename to read the page setup from
311 ///
312 /// # Returns
313 ///
314 /// [`true`] on success
315 #[doc(alias = "gtk_page_setup_load_file")]
316 pub fn load_file(&self, file_name: impl AsRef<std::path::Path>) -> Result<(), glib::Error> {
317 unsafe {
318 let mut error = ptr::null_mut();
319 let is_ok = ffi::gtk_page_setup_load_file(
320 self.to_glib_none().0,
321 file_name.as_ref().to_glib_none().0,
322 &mut error,
323 );
324 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
325 if error.is_null() {
326 Ok(())
327 } else {
328 Err(from_glib_full(error))
329 }
330 }
331 }
332
333 /// Reads the page setup from the group `group_name` in the key file
334 /// `key_file`.
335 /// ## `key_file`
336 /// the [`glib::KeyFile`][crate::glib::KeyFile] to retrieve the page_setup from
337 /// ## `group_name`
338 /// the name of the group in the key_file to read, or [`None`]
339 /// to use the default name “Page Setup”
340 ///
341 /// # Returns
342 ///
343 /// [`true`] on success
344 #[doc(alias = "gtk_page_setup_load_key_file")]
345 pub fn load_key_file(
346 &self,
347 key_file: &glib::KeyFile,
348 group_name: Option<&str>,
349 ) -> Result<(), glib::Error> {
350 unsafe {
351 let mut error = ptr::null_mut();
352 let is_ok = ffi::gtk_page_setup_load_key_file(
353 self.to_glib_none().0,
354 key_file.to_glib_none().0,
355 group_name.to_glib_none().0,
356 &mut error,
357 );
358 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
359 if error.is_null() {
360 Ok(())
361 } else {
362 Err(from_glib_full(error))
363 }
364 }
365 }
366
367 /// Sets the bottom margin of the [`PageSetup`][crate::PageSetup].
368 /// ## `margin`
369 /// the new bottom margin in units of `unit`
370 /// ## `unit`
371 /// the units for `margin`
372 #[doc(alias = "gtk_page_setup_set_bottom_margin")]
373 pub fn set_bottom_margin(&self, margin: f64, unit: Unit) {
374 unsafe {
375 ffi::gtk_page_setup_set_bottom_margin(self.to_glib_none().0, margin, unit.into_glib());
376 }
377 }
378
379 /// Sets the left margin of the [`PageSetup`][crate::PageSetup].
380 /// ## `margin`
381 /// the new left margin in units of `unit`
382 /// ## `unit`
383 /// the units for `margin`
384 #[doc(alias = "gtk_page_setup_set_left_margin")]
385 pub fn set_left_margin(&self, margin: f64, unit: Unit) {
386 unsafe {
387 ffi::gtk_page_setup_set_left_margin(self.to_glib_none().0, margin, unit.into_glib());
388 }
389 }
390
391 /// Sets the page orientation of the [`PageSetup`][crate::PageSetup].
392 /// ## `orientation`
393 /// a [`PageOrientation`][crate::PageOrientation] value
394 #[doc(alias = "gtk_page_setup_set_orientation")]
395 pub fn set_orientation(&self, orientation: PageOrientation) {
396 unsafe {
397 ffi::gtk_page_setup_set_orientation(self.to_glib_none().0, orientation.into_glib());
398 }
399 }
400
401 /// Sets the paper size of the [`PageSetup`][crate::PageSetup] without
402 /// changing the margins. See
403 /// [`set_paper_size_and_default_margins()`][Self::set_paper_size_and_default_margins()].
404 /// ## `size`
405 /// a [`PaperSize`][crate::PaperSize]
406 #[doc(alias = "gtk_page_setup_set_paper_size")]
407 pub fn set_paper_size(&self, size: &PaperSize) {
408 unsafe {
409 ffi::gtk_page_setup_set_paper_size(
410 self.to_glib_none().0,
411 mut_override(size.to_glib_none().0),
412 );
413 }
414 }
415
416 /// Sets the paper size of the [`PageSetup`][crate::PageSetup] and modifies
417 /// the margins according to the new paper size.
418 /// ## `size`
419 /// a [`PaperSize`][crate::PaperSize]
420 #[doc(alias = "gtk_page_setup_set_paper_size_and_default_margins")]
421 pub fn set_paper_size_and_default_margins(&self, size: &PaperSize) {
422 unsafe {
423 ffi::gtk_page_setup_set_paper_size_and_default_margins(
424 self.to_glib_none().0,
425 mut_override(size.to_glib_none().0),
426 );
427 }
428 }
429
430 /// Sets the right margin of the [`PageSetup`][crate::PageSetup].
431 /// ## `margin`
432 /// the new right margin in units of `unit`
433 /// ## `unit`
434 /// the units for `margin`
435 #[doc(alias = "gtk_page_setup_set_right_margin")]
436 pub fn set_right_margin(&self, margin: f64, unit: Unit) {
437 unsafe {
438 ffi::gtk_page_setup_set_right_margin(self.to_glib_none().0, margin, unit.into_glib());
439 }
440 }
441
442 /// Sets the top margin of the [`PageSetup`][crate::PageSetup].
443 /// ## `margin`
444 /// the new top margin in units of `unit`
445 /// ## `unit`
446 /// the units for `margin`
447 #[doc(alias = "gtk_page_setup_set_top_margin")]
448 pub fn set_top_margin(&self, margin: f64, unit: Unit) {
449 unsafe {
450 ffi::gtk_page_setup_set_top_margin(self.to_glib_none().0, margin, unit.into_glib());
451 }
452 }
453
454 /// This function saves the information from `self` to `file_name`.
455 /// ## `file_name`
456 /// the file to save to
457 ///
458 /// # Returns
459 ///
460 /// [`true`] on success
461 #[doc(alias = "gtk_page_setup_to_file")]
462 pub fn to_file(&self, file_name: impl AsRef<std::path::Path>) -> Result<(), glib::Error> {
463 unsafe {
464 let mut error = ptr::null_mut();
465 let is_ok = ffi::gtk_page_setup_to_file(
466 self.to_glib_none().0,
467 file_name.as_ref().to_glib_none().0,
468 &mut error,
469 );
470 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
471 if error.is_null() {
472 Ok(())
473 } else {
474 Err(from_glib_full(error))
475 }
476 }
477 }
478
479 /// Serialize page setup to an a{sv} variant.
480 ///
481 /// # Returns
482 ///
483 /// a new, floating, [`glib::Variant`][struct@crate::glib::Variant]
484 #[doc(alias = "gtk_page_setup_to_gvariant")]
485 pub fn to_gvariant(&self) -> Option<glib::Variant> {
486 unsafe { from_glib_none(ffi::gtk_page_setup_to_gvariant(self.to_glib_none().0)) }
487 }
488
489 /// This function adds the page setup from `self` to `key_file`.
490 /// ## `key_file`
491 /// the [`glib::KeyFile`][crate::glib::KeyFile] to save the page setup to
492 /// ## `group_name`
493 /// the group to add the settings to in `key_file`,
494 /// or [`None`] to use the default name “Page Setup”
495 #[doc(alias = "gtk_page_setup_to_key_file")]
496 pub fn to_key_file(&self, key_file: &glib::KeyFile, group_name: Option<&str>) {
497 unsafe {
498 ffi::gtk_page_setup_to_key_file(
499 self.to_glib_none().0,
500 key_file.to_glib_none().0,
501 group_name.to_glib_none().0,
502 );
503 }
504 }
505}
506
507impl Default for PageSetup {
508 fn default() -> Self {
509 Self::new()
510 }
511}
512
513impl fmt::Display for PageSetup {
514 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
515 f.write_str("PageSetup")
516 }
517}