Skip to main content

gtk4/auto/
print_job.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::{
6    NumberUpLayout, PageRange, PageSet, PageSetup, PrintPages, PrintSettings, PrintStatus, Printer,
7    ffi,
8};
9use glib::{
10    object::ObjectType as _,
11    prelude::*,
12    signal::{SignalHandlerId, connect_raw},
13    translate::*,
14};
15use std::boxed::Box as Box_;
16
17glib::wrapper! {
18    /// t use cairo
19    /// [`PrintJob`][crate::PrintJob] also supports printing of manually generated PostScript,
20    /// via [`set_source_file()`][Self::set_source_file()].
21    ///
22    /// ## Properties
23    ///
24    ///
25    /// #### `page-setup`
26    ///  Page setup.
27    ///
28    /// Readable | Writable | Construct Only
29    ///
30    ///
31    /// #### `printer`
32    ///  The printer to send the job to.
33    ///
34    /// Readable | Writable | Construct Only
35    ///
36    ///
37    /// #### `settings`
38    ///  Printer settings.
39    ///
40    /// Readable | Writable | Construct Only
41    ///
42    ///
43    /// #### `title`
44    ///  The title of the print job.
45    ///
46    /// Readable | Writable | Construct Only
47    ///
48    ///
49    /// #### `track-print-status`
50    ///  [`true`] if the print job will continue to emit status-changed
51    /// signals after the print data has been setn to the printer.
52    ///
53    /// Readable | Writable
54    ///
55    /// ## Signals
56    ///
57    ///
58    /// #### `status-changed`
59    ///  Emitted when the status of a job changes.
60    ///
61    /// The signal handler can use [`PrintJob::status()`][crate::PrintJob::status()]
62    /// to obtain the new status.
63    ///
64    ///
65    ///
66    /// # Implements
67    ///
68    /// [`trait@glib::ObjectExt`]
69    #[doc(alias = "GtkPrintJob")]
70    pub struct PrintJob(Object<ffi::GtkPrintJob>);
71
72    match fn {
73        type_ => || ffi::gtk_print_job_get_type(),
74    }
75}
76
77impl PrintJob {
78    /// Creates a new [`PrintJob`][crate::PrintJob].
79    /// ## `title`
80    /// the job title
81    /// ## `printer`
82    /// a [`Printer`][crate::Printer]
83    /// ## `settings`
84    /// a [`PrintSettings`][crate::PrintSettings]
85    /// ## `page_setup`
86    /// a [`PageSetup`][crate::PageSetup]
87    ///
88    /// # Returns
89    ///
90    /// a new [`PrintJob`][crate::PrintJob]
91    #[doc(alias = "gtk_print_job_new")]
92    pub fn new(
93        title: &str,
94        printer: &Printer,
95        settings: &PrintSettings,
96        page_setup: &PageSetup,
97    ) -> PrintJob {
98        skip_assert_initialized!();
99        unsafe {
100            from_glib_full(ffi::gtk_print_job_new(
101                title.to_glib_none().0,
102                printer.to_glib_none().0,
103                settings.to_glib_none().0,
104                page_setup.to_glib_none().0,
105            ))
106        }
107    }
108
109    /// Gets whether this job is printed collated.
110    ///
111    /// # Returns
112    ///
113    /// whether the job is printed collated
114    #[doc(alias = "gtk_print_job_get_collate")]
115    #[doc(alias = "get_collate")]
116    pub fn is_collate(&self) -> bool {
117        unsafe { from_glib(ffi::gtk_print_job_get_collate(self.to_glib_none().0)) }
118    }
119
120    /// Gets the n-up setting for this job.
121    ///
122    /// # Returns
123    ///
124    /// the n-up setting
125    #[doc(alias = "gtk_print_job_get_n_up")]
126    #[doc(alias = "get_n_up")]
127    pub fn n_up(&self) -> u32 {
128        unsafe { ffi::gtk_print_job_get_n_up(self.to_glib_none().0) }
129    }
130
131    /// Gets the n-up layout setting for this job.
132    ///
133    /// # Returns
134    ///
135    /// the n-up layout
136    #[doc(alias = "gtk_print_job_get_n_up_layout")]
137    #[doc(alias = "get_n_up_layout")]
138    pub fn n_up_layout(&self) -> NumberUpLayout {
139        unsafe { from_glib(ffi::gtk_print_job_get_n_up_layout(self.to_glib_none().0)) }
140    }
141
142    /// Gets the number of copies of this job.
143    ///
144    /// # Returns
145    ///
146    /// the number of copies
147    #[doc(alias = "gtk_print_job_get_num_copies")]
148    #[doc(alias = "get_num_copies")]
149    pub fn num_copies(&self) -> i32 {
150        unsafe { ffi::gtk_print_job_get_num_copies(self.to_glib_none().0) }
151    }
152
153    /// Gets the page ranges for this job.
154    ///
155    /// # Returns
156    ///
157    /// a pointer to an
158    ///   array of [`PageRange`][crate::PageRange] structs
159    #[doc(alias = "gtk_print_job_get_page_ranges")]
160    #[doc(alias = "get_page_ranges")]
161    pub fn page_ranges(&self) -> Vec<PageRange> {
162        unsafe {
163            let mut n_ranges = std::mem::MaybeUninit::uninit();
164            let ret = FromGlibContainer::from_glib_none_num(
165                ffi::gtk_print_job_get_page_ranges(self.to_glib_none().0, n_ranges.as_mut_ptr()),
166                n_ranges.assume_init() as _,
167            );
168            ret
169        }
170    }
171
172    /// Gets the [`PageSet`][crate::PageSet] setting for this job.
173    ///
174    /// # Returns
175    ///
176    /// the [`PageSet`][crate::PageSet] setting
177    #[doc(alias = "gtk_print_job_get_page_set")]
178    #[doc(alias = "get_page_set")]
179    pub fn page_set(&self) -> PageSet {
180        unsafe { from_glib(ffi::gtk_print_job_get_page_set(self.to_glib_none().0)) }
181    }
182
183    /// Gets the [`PrintPages`][crate::PrintPages] setting for this job.
184    ///
185    /// # Returns
186    ///
187    /// the [`PrintPages`][crate::PrintPages] setting
188    #[doc(alias = "gtk_print_job_get_pages")]
189    #[doc(alias = "get_pages")]
190    pub fn pages(&self) -> PrintPages {
191        unsafe { from_glib(ffi::gtk_print_job_get_pages(self.to_glib_none().0)) }
192    }
193
194    /// Gets the [`Printer`][crate::Printer] of the print job.
195    ///
196    /// # Returns
197    ///
198    /// the printer of @self
199    #[doc(alias = "gtk_print_job_get_printer")]
200    #[doc(alias = "get_printer")]
201    pub fn printer(&self) -> Printer {
202        unsafe { from_glib_none(ffi::gtk_print_job_get_printer(self.to_glib_none().0)) }
203    }
204
205    /// Gets whether this job is printed reversed.
206    ///
207    /// # Returns
208    ///
209    /// whether the job is printed reversed.
210    #[doc(alias = "gtk_print_job_get_reverse")]
211    #[doc(alias = "get_reverse")]
212    pub fn is_reverse(&self) -> bool {
213        unsafe { from_glib(ffi::gtk_print_job_get_reverse(self.to_glib_none().0)) }
214    }
215
216    /// Gets whether the job is printed rotated.
217    ///
218    /// # Returns
219    ///
220    /// whether the job is printed rotated
221    #[doc(alias = "gtk_print_job_get_rotate")]
222    #[doc(alias = "get_rotate")]
223    pub fn is_rotate(&self) -> bool {
224        unsafe { from_glib(ffi::gtk_print_job_get_rotate(self.to_glib_none().0)) }
225    }
226
227    /// Gets the scale for this job.
228    ///
229    /// # Returns
230    ///
231    /// the scale
232    #[doc(alias = "gtk_print_job_get_scale")]
233    #[doc(alias = "get_scale")]
234    pub fn scale(&self) -> f64 {
235        unsafe { ffi::gtk_print_job_get_scale(self.to_glib_none().0) }
236    }
237
238    /// Gets the [`PrintSettings`][crate::PrintSettings] of the print job.
239    ///
240    /// # Returns
241    ///
242    /// the settings of @self
243    #[doc(alias = "gtk_print_job_get_settings")]
244    #[doc(alias = "get_settings")]
245    pub fn settings(&self) -> PrintSettings {
246        unsafe { from_glib_none(ffi::gtk_print_job_get_settings(self.to_glib_none().0)) }
247    }
248
249    /// Gets the status of the print job.
250    ///
251    /// # Returns
252    ///
253    /// the status of @self
254    #[doc(alias = "gtk_print_job_get_status")]
255    #[doc(alias = "get_status")]
256    pub fn status(&self) -> PrintStatus {
257        unsafe { from_glib(ffi::gtk_print_job_get_status(self.to_glib_none().0)) }
258    }
259
260    /// Gets a cairo surface onto which the pages of
261    /// the print job should be rendered.
262    ///
263    /// # Returns
264    ///
265    /// the cairo surface of @self
266    #[doc(alias = "gtk_print_job_get_surface")]
267    #[doc(alias = "get_surface")]
268    pub fn surface(&self) -> Result<cairo::Surface, glib::Error> {
269        unsafe {
270            let mut error = std::ptr::null_mut();
271            let ret = ffi::gtk_print_job_get_surface(self.to_glib_none().0, &mut error);
272            if error.is_null() {
273                Ok(from_glib_none(ret))
274            } else {
275                Err(from_glib_full(error))
276            }
277        }
278    }
279
280    /// Gets the job title.
281    ///
282    /// # Returns
283    ///
284    /// the title of @self
285    #[doc(alias = "gtk_print_job_get_title")]
286    #[doc(alias = "get_title")]
287    pub fn title(&self) -> glib::GString {
288        unsafe { from_glib_none(ffi::gtk_print_job_get_title(self.to_glib_none().0)) }
289    }
290
291    /// Returns whether jobs will be tracked after printing.
292    ///
293    /// For details, see [`set_track_print_status()`][Self::set_track_print_status()].
294    ///
295    /// # Returns
296    ///
297    /// [`true`] if print job status will be reported after printing
298    #[doc(alias = "gtk_print_job_get_track_print_status")]
299    #[doc(alias = "get_track_print_status")]
300    #[doc(alias = "track-print-status")]
301    pub fn tracks_print_status(&self) -> bool {
302        unsafe {
303            from_glib(ffi::gtk_print_job_get_track_print_status(
304                self.to_glib_none().0,
305            ))
306        }
307    }
308
309    /// Sets whether this job is printed collated.
310    /// ## `collate`
311    /// whether the job is printed collated
312    #[doc(alias = "gtk_print_job_set_collate")]
313    pub fn set_collate(&self, collate: bool) {
314        unsafe {
315            ffi::gtk_print_job_set_collate(self.to_glib_none().0, collate.into_glib());
316        }
317    }
318
319    /// Sets the n-up setting for this job.
320    /// ## `n_up`
321    /// the n-up value
322    #[doc(alias = "gtk_print_job_set_n_up")]
323    pub fn set_n_up(&self, n_up: u32) {
324        unsafe {
325            ffi::gtk_print_job_set_n_up(self.to_glib_none().0, n_up);
326        }
327    }
328
329    /// Sets the n-up layout setting for this job.
330    /// ## `layout`
331    /// the n-up layout setting
332    #[doc(alias = "gtk_print_job_set_n_up_layout")]
333    pub fn set_n_up_layout(&self, layout: NumberUpLayout) {
334        unsafe {
335            ffi::gtk_print_job_set_n_up_layout(self.to_glib_none().0, layout.into_glib());
336        }
337    }
338
339    /// Sets the number of copies for this job.
340    /// ## `num_copies`
341    /// the number of copies
342    #[doc(alias = "gtk_print_job_set_num_copies")]
343    pub fn set_num_copies(&self, num_copies: i32) {
344        unsafe {
345            ffi::gtk_print_job_set_num_copies(self.to_glib_none().0, num_copies);
346        }
347    }
348
349    /// Sets the page ranges for this job.
350    /// ## `ranges`
351    /// pointer to an array of
352    ///    [`PageRange`][crate::PageRange] structs
353    #[doc(alias = "gtk_print_job_set_page_ranges")]
354    pub fn set_page_ranges(&self, ranges: &[PageRange]) {
355        let n_ranges = ranges.len() as _;
356        unsafe {
357            ffi::gtk_print_job_set_page_ranges(
358                self.to_glib_none().0,
359                ranges.to_glib_full(),
360                n_ranges,
361            );
362        }
363    }
364
365    /// Sets the [`PageSet`][crate::PageSet] setting for this job.
366    /// ## `page_set`
367    /// a [`PageSet`][crate::PageSet] setting
368    #[doc(alias = "gtk_print_job_set_page_set")]
369    pub fn set_page_set(&self, page_set: PageSet) {
370        unsafe {
371            ffi::gtk_print_job_set_page_set(self.to_glib_none().0, page_set.into_glib());
372        }
373    }
374
375    /// Sets the [`PrintPages`][crate::PrintPages] setting for this job.
376    /// ## `pages`
377    /// the [`PrintPages`][crate::PrintPages] setting
378    #[doc(alias = "gtk_print_job_set_pages")]
379    pub fn set_pages(&self, pages: PrintPages) {
380        unsafe {
381            ffi::gtk_print_job_set_pages(self.to_glib_none().0, pages.into_glib());
382        }
383    }
384
385    /// Sets whether this job is printed reversed.
386    /// ## `reverse`
387    /// whether the job is printed reversed
388    #[doc(alias = "gtk_print_job_set_reverse")]
389    pub fn set_reverse(&self, reverse: bool) {
390        unsafe {
391            ffi::gtk_print_job_set_reverse(self.to_glib_none().0, reverse.into_glib());
392        }
393    }
394
395    /// Sets whether this job is printed rotated.
396    /// ## `rotate`
397    /// whether to print rotated
398    #[doc(alias = "gtk_print_job_set_rotate")]
399    pub fn set_rotate(&self, rotate: bool) {
400        unsafe {
401            ffi::gtk_print_job_set_rotate(self.to_glib_none().0, rotate.into_glib());
402        }
403    }
404
405    /// Sets the scale for this job.
406    ///
407    /// 1.0 means unscaled.
408    /// ## `scale`
409    /// the scale
410    #[doc(alias = "gtk_print_job_set_scale")]
411    pub fn set_scale(&self, scale: f64) {
412        unsafe {
413            ffi::gtk_print_job_set_scale(self.to_glib_none().0, scale);
414        }
415    }
416
417    /// Make the [`PrintJob`][crate::PrintJob] send an existing document to the
418    /// printing system.
419    ///
420    /// The file can be in any format understood by the platforms
421    /// printing system (typically PostScript, but on many platforms
422    /// PDF may work too). See [`Printer::accepts_pdf()`][crate::Printer::accepts_pdf()] and
423    /// [`Printer::accepts_ps()`][crate::Printer::accepts_ps()].
424    ///
425    /// This is similar to [`set_source_file()`][Self::set_source_file()],
426    /// but takes expects an open file descriptor for the file,
427    /// instead of a filename.
428    /// ## `fd`
429    /// a file descriptor
430    ///
431    /// # Returns
432    ///
433    /// [`false`] if an error occurred
434    #[doc(alias = "gtk_print_job_set_source_fd")]
435    pub fn set_source_fd(&self, fd: i32) -> Result<(), glib::Error> {
436        unsafe {
437            let mut error = std::ptr::null_mut();
438            let is_ok = ffi::gtk_print_job_set_source_fd(self.to_glib_none().0, fd, &mut error);
439            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
440            if error.is_null() {
441                Ok(())
442            } else {
443                Err(from_glib_full(error))
444            }
445        }
446    }
447
448    /// Make the [`PrintJob`][crate::PrintJob] send an existing document to the
449    /// printing system.
450    ///
451    /// The file can be in any format understood by the platforms
452    /// printing system (typically PostScript, but on many platforms
453    /// PDF may work too). See [`Printer::accepts_pdf()`][crate::Printer::accepts_pdf()] and
454    /// [`Printer::accepts_ps()`][crate::Printer::accepts_ps()].
455    /// ## `filename`
456    /// the file to be printed
457    ///
458    /// # Returns
459    ///
460    /// [`false`] if an error occurred
461    #[doc(alias = "gtk_print_job_set_source_file")]
462    pub fn set_source_file(
463        &self,
464        filename: impl AsRef<std::path::Path>,
465    ) -> Result<(), glib::Error> {
466        unsafe {
467            let mut error = std::ptr::null_mut();
468            let is_ok = ffi::gtk_print_job_set_source_file(
469                self.to_glib_none().0,
470                filename.as_ref().to_glib_none().0,
471                &mut error,
472            );
473            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
474            if error.is_null() {
475                Ok(())
476            } else {
477                Err(from_glib_full(error))
478            }
479        }
480    }
481
482    ///
483    /// issues, and when the print job actually reaches the printer.
484    ///
485    /// This function is often implemented using some form of polling,
486    /// so it should not be enabled unless needed.
487    /// ## `track_status`
488    /// [`true`] to track status after printing
489    #[doc(alias = "gtk_print_job_set_track_print_status")]
490    #[doc(alias = "track-print-status")]
491    pub fn set_track_print_status(&self, track_status: bool) {
492        unsafe {
493            ffi::gtk_print_job_set_track_print_status(
494                self.to_glib_none().0,
495                track_status.into_glib(),
496            );
497        }
498    }
499
500    /// Page setup.
501    #[doc(alias = "page-setup")]
502    pub fn page_setup(&self) -> Option<PageSetup> {
503        ObjectExt::property(self, "page-setup")
504    }
505
506    /// Emitted when the status of a job changes.
507    ///
508    /// The signal handler can use [`status()`][Self::status()]
509    /// to obtain the new status.
510    #[doc(alias = "status-changed")]
511    pub fn connect_status_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
512        unsafe extern "C" fn status_changed_trampoline<F: Fn(&PrintJob) + 'static>(
513            this: *mut ffi::GtkPrintJob,
514            f: glib::ffi::gpointer,
515        ) {
516            unsafe {
517                let f: &F = &*(f as *const F);
518                f(&from_glib_borrow(this))
519            }
520        }
521        unsafe {
522            let f: Box_<F> = Box_::new(f);
523            connect_raw(
524                self.as_ptr() as *mut _,
525                c"status-changed".as_ptr(),
526                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
527                    status_changed_trampoline::<F> as *const (),
528                )),
529                Box_::into_raw(f),
530            )
531        }
532    }
533
534    #[doc(alias = "track-print-status")]
535    pub fn connect_track_print_status_notify<F: Fn(&Self) + 'static>(
536        &self,
537        f: F,
538    ) -> SignalHandlerId {
539        unsafe extern "C" fn notify_track_print_status_trampoline<F: Fn(&PrintJob) + 'static>(
540            this: *mut ffi::GtkPrintJob,
541            _param_spec: glib::ffi::gpointer,
542            f: glib::ffi::gpointer,
543        ) {
544            unsafe {
545                let f: &F = &*(f as *const F);
546                f(&from_glib_borrow(this))
547            }
548        }
549        unsafe {
550            let f: Box_<F> = Box_::new(f);
551            connect_raw(
552                self.as_ptr() as *mut _,
553                c"notify::track-print-status".as_ptr(),
554                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
555                    notify_track_print_status_trampoline::<F> as *const (),
556                )),
557                Box_::into_raw(f),
558            )
559        }
560    }
561}