Skip to main content

gtk4/
print_job.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::boxed::Box as Box_;
4
5use glib::translate::*;
6
7use crate::{PrintJob, ffi};
8
9impl PrintJob {
10    /// Sends the print job off to the printer.
11    /// ## `callback`
12    /// function
13    ///   to call when the job completes or an error occurs
14    #[doc(alias = "gtk_print_job_send")]
15    pub fn send<P: Fn(&PrintJob, Result<(), glib::Error>) + 'static>(&self, callback: P) {
16        let callback_data: Box_<P> = Box_::new(callback);
17        unsafe extern "C" fn callback_func<P: Fn(&PrintJob, Result<(), glib::Error>) + 'static>(
18            print_job: *mut ffi::GtkPrintJob,
19            user_data: glib::ffi::gpointer,
20            error: *const glib::ffi::GError,
21        ) {
22            unsafe {
23                let print_job = from_glib_borrow(print_job);
24                let result = if error.is_null() {
25                    Ok(())
26                } else {
27                    Err(from_glib_none(error))
28                };
29                let callback: &P = &*(user_data as *mut _);
30                (*callback)(&print_job, result);
31            }
32        }
33        let callback = Some(callback_func::<P> as _);
34        unsafe extern "C" fn dnotify_func<P>(data: glib::ffi::gpointer) {
35            unsafe {
36                let _callback: Box_<P> = Box_::from_raw(data as *mut _);
37            }
38        }
39        let destroy_call3 = Some(dnotify_func::<P> as _);
40        let super_callback0: Box_<P> = callback_data;
41        unsafe {
42            ffi::gtk_print_job_send(
43                self.to_glib_none().0,
44                callback,
45                Box_::into_raw(super_callback0) as *mut _,
46                destroy_call3,
47            );
48        }
49    }
50}