1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// from webkit2gtk-gir-files
// DO NOT EDIT

#[cfg(any(feature = "v2_16", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_16")))]
use crate::PrintCustomWidget;
use crate::PrintOperationResponse;
use crate::WebView;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use glib::StaticType;
use glib::ToValue;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem::transmute;

glib::wrapper! {
    /// Controls a print operation.
    ///
    /// A [`PrintOperation`][crate::PrintOperation] controls a print operation in WebKit. With
    /// a similar API to `GtkPrintOperation`, it lets you set the print
    /// settings with [`PrintOperationExt::set_print_settings()`][crate::prelude::PrintOperationExt::set_print_settings()] or
    /// display the print dialog with [`PrintOperationExt::run_dialog()`][crate::prelude::PrintOperationExt::run_dialog()].
    ///
    /// # Implements
    ///
    /// [`PrintOperationExt`][trait@crate::prelude::PrintOperationExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "WebKitPrintOperation")]
    pub struct PrintOperation(Object<ffi::WebKitPrintOperation, ffi::WebKitPrintOperationClass>);

    match fn {
        type_ => || ffi::webkit_print_operation_get_type(),
    }
}

impl PrintOperation {
    pub const NONE: Option<&'static PrintOperation> = None;

    /// Create a new [`PrintOperation`][crate::PrintOperation] to print `web_view` contents.
    /// ## `web_view`
    /// a [`WebView`][crate::WebView]
    ///
    /// # Returns
    ///
    /// a new [`PrintOperation`][crate::PrintOperation].
    #[doc(alias = "webkit_print_operation_new")]
    pub fn new(web_view: &impl IsA<WebView>) -> PrintOperation {
        skip_assert_initialized!();
        unsafe {
            from_glib_full(ffi::webkit_print_operation_new(
                web_view.as_ref().to_glib_none().0,
            ))
        }
    }

    // rustdoc-stripper-ignore-next
    /// Creates a new builder-pattern struct instance to construct [`PrintOperation`] objects.
    ///
    /// This method returns an instance of [`PrintOperationBuilder`](crate::builders::PrintOperationBuilder) which can be used to create [`PrintOperation`] objects.
    pub fn builder() -> PrintOperationBuilder {
        PrintOperationBuilder::default()
    }
}

impl Default for PrintOperation {
    fn default() -> Self {
        glib::object::Object::new::<Self>(&[])
    }
}

#[derive(Clone, Default)]
// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`PrintOperation`] objects.
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
#[must_use = "The builder must be built to be used"]
pub struct PrintOperationBuilder {
    page_setup: Option<gtk::PageSetup>,
    print_settings: Option<gtk::PrintSettings>,
    web_view: Option<WebView>,
}

impl PrintOperationBuilder {
    // rustdoc-stripper-ignore-next
    /// Create a new [`PrintOperationBuilder`].
    pub fn new() -> Self {
        Self::default()
    }

    // rustdoc-stripper-ignore-next
    /// Build the [`PrintOperation`].
    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
    pub fn build(self) -> PrintOperation {
        let mut properties: Vec<(&str, &dyn ToValue)> = vec![];
        if let Some(ref page_setup) = self.page_setup {
            properties.push(("page-setup", page_setup));
        }
        if let Some(ref print_settings) = self.print_settings {
            properties.push(("print-settings", print_settings));
        }
        if let Some(ref web_view) = self.web_view {
            properties.push(("web-view", web_view));
        }
        glib::Object::new::<PrintOperation>(&properties)
    }

    /// The initial [`gtk::PageSetup`][crate::gtk::PageSetup] for the print operation.
    pub fn page_setup(mut self, page_setup: &gtk::PageSetup) -> Self {
        self.page_setup = Some(page_setup.clone());
        self
    }

    /// The initial [`gtk::PrintSettings`][crate::gtk::PrintSettings] for the print operation.
    pub fn print_settings(mut self, print_settings: &gtk::PrintSettings) -> Self {
        self.print_settings = Some(print_settings.clone());
        self
    }

    /// The [`WebView`][crate::WebView] that will be printed.
    pub fn web_view(mut self, web_view: &impl IsA<WebView>) -> Self {
        self.web_view = Some(web_view.clone().upcast());
        self
    }
}

/// Trait containing all [`struct@PrintOperation`] methods.
///
/// # Implementors
///
/// [`PrintOperation`][struct@crate::PrintOperation]
pub trait PrintOperationExt: 'static {
    /// Return the current page setup of `self`.
    ///
    /// It returns [`None`] until
    /// either [`set_page_setup()`][Self::set_page_setup()] or [`run_dialog()`][Self::run_dialog()]
    /// have been called.
    ///
    /// # Returns
    ///
    /// the current [`gtk::PageSetup`][crate::gtk::PageSetup] of `self`.
    #[doc(alias = "webkit_print_operation_get_page_setup")]
    #[doc(alias = "get_page_setup")]
    fn page_setup(&self) -> Option<gtk::PageSetup>;

    /// Return the current print settings of `self`.
    ///
    /// It returns [`None`] until
    /// either [`set_print_settings()`][Self::set_print_settings()] or [`run_dialog()`][Self::run_dialog()]
    /// have been called.
    ///
    /// # Returns
    ///
    /// the current [`gtk::PrintSettings`][crate::gtk::PrintSettings] of `self`.
    #[doc(alias = "webkit_print_operation_get_print_settings")]
    #[doc(alias = "get_print_settings")]
    fn print_settings(&self) -> Option<gtk::PrintSettings>;

    /// Start a print operation using current print settings and page setup.
    ///
    /// Start a print operation using current print settings and page setup
    /// without showing the print dialog. If either print settings or page setup
    /// are not set with [`set_print_settings()`][Self::set_print_settings()] and
    /// [`set_page_setup()`][Self::set_page_setup()], the default options will be used
    /// and the print job will be sent to the default printer.
    /// The `signal::PrintOperation::finished` signal is emitted when the printing
    /// operation finishes. If an error occurs while printing the signal
    /// `signal::PrintOperation::failed` is emitted before `signal::PrintOperation::finished`.
    #[doc(alias = "webkit_print_operation_print")]
    fn print(&self);

    /// Run the print dialog and start printing.
    ///
    /// Run the print dialog and start printing using the options selected by
    /// the user. This method returns when the print dialog is closed.
    /// If the print dialog is cancelled [`PrintOperationResponse::Cancel`][crate::PrintOperationResponse::Cancel]
    /// is returned. If the user clicks on the print button, [`PrintOperationResponse::Print`][crate::PrintOperationResponse::Print]
    /// is returned and the print operation starts. In this case, the `signal::PrintOperation::finished`
    /// signal is emitted when the operation finishes. If an error occurs while printing, the signal
    /// `signal::PrintOperation::failed` is emitted before `signal::PrintOperation::finished`.
    /// If the print dialog is not cancelled current print settings and page setup of `self`
    /// are updated with options selected by the user when Print button is pressed in print dialog.
    /// You can get the updated print settings and page setup by calling
    /// [`print_settings()`][Self::print_settings()] and [`page_setup()`][Self::page_setup()]
    /// after this method.
    /// ## `parent`
    /// transient parent of the print dialog
    ///
    /// # Returns
    ///
    /// the [`PrintOperationResponse`][crate::PrintOperationResponse] of the print dialog
    #[doc(alias = "webkit_print_operation_run_dialog")]
    fn run_dialog(&self, parent: Option<&impl IsA<gtk::Window>>) -> PrintOperationResponse;

    /// Set the current page setup of `self`.
    ///
    /// Current page setup is used for the
    /// initial values of the print dialog when [`run_dialog()`][Self::run_dialog()] is called.
    /// ## `page_setup`
    /// a [`gtk::PageSetup`][crate::gtk::PageSetup] to set
    #[doc(alias = "webkit_print_operation_set_page_setup")]
    fn set_page_setup(&self, page_setup: &gtk::PageSetup);

    /// Set the current print settings of `self`.
    ///
    /// Set the current print settings of `self`. Current print settings are used for
    /// the initial values of the print dialog when [`run_dialog()`][Self::run_dialog()] is called.
    /// ## `print_settings`
    /// a [`gtk::PrintSettings`][crate::gtk::PrintSettings] to set
    #[doc(alias = "webkit_print_operation_set_print_settings")]
    fn set_print_settings(&self, print_settings: &gtk::PrintSettings);

    /// The [`WebView`][crate::WebView] that will be printed.
    #[doc(alias = "web-view")]
    fn web_view(&self) -> Option<WebView>;

    /// Emitted when displaying the print dialog with [`run_dialog()`][Self::run_dialog()].
    /// The returned [`PrintCustomWidget`][crate::PrintCustomWidget] will be added to the print dialog and
    /// it will be owned by the `print_operation`. However, the object is guaranteed
    /// to be alive until the `signal::PrintCustomWidget::apply` is emitted.
    ///
    /// # Returns
    ///
    /// A [`PrintCustomWidget`][crate::PrintCustomWidget] that will be embedded in the dialog.
    #[cfg(any(feature = "v2_16", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_16")))]
    #[doc(alias = "create-custom-widget")]
    fn connect_create_custom_widget<F: Fn(&Self) -> PrintCustomWidget + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    /// Emitted when an error occurs while printing. The given `error`, of the domain
    /// `WEBKIT_PRINT_ERROR`, contains further details of the failure.
    /// The `signal::PrintOperation::finished` signal is emitted after this one.
    /// ## `error`
    /// the [`glib::Error`][crate::glib::Error] that was triggered
    #[doc(alias = "failed")]
    fn connect_failed<F: Fn(&Self, &glib::Error) + 'static>(&self, f: F) -> SignalHandlerId;

    /// Emitted when the print operation has finished doing everything
    /// required for printing.
    #[doc(alias = "finished")]
    fn connect_finished<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    #[doc(alias = "page-setup")]
    fn connect_page_setup_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    #[doc(alias = "print-settings")]
    fn connect_print_settings_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<PrintOperation>> PrintOperationExt for O {
    fn page_setup(&self) -> Option<gtk::PageSetup> {
        unsafe {
            from_glib_none(ffi::webkit_print_operation_get_page_setup(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn print_settings(&self) -> Option<gtk::PrintSettings> {
        unsafe {
            from_glib_none(ffi::webkit_print_operation_get_print_settings(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn print(&self) {
        unsafe {
            ffi::webkit_print_operation_print(self.as_ref().to_glib_none().0);
        }
    }

    fn run_dialog(&self, parent: Option<&impl IsA<gtk::Window>>) -> PrintOperationResponse {
        unsafe {
            from_glib(ffi::webkit_print_operation_run_dialog(
                self.as_ref().to_glib_none().0,
                parent.map(|p| p.as_ref()).to_glib_none().0,
            ))
        }
    }

    fn set_page_setup(&self, page_setup: &gtk::PageSetup) {
        unsafe {
            ffi::webkit_print_operation_set_page_setup(
                self.as_ref().to_glib_none().0,
                page_setup.to_glib_none().0,
            );
        }
    }

    fn set_print_settings(&self, print_settings: &gtk::PrintSettings) {
        unsafe {
            ffi::webkit_print_operation_set_print_settings(
                self.as_ref().to_glib_none().0,
                print_settings.to_glib_none().0,
            );
        }
    }

    fn web_view(&self) -> Option<WebView> {
        glib::ObjectExt::property(self.as_ref(), "web-view")
    }

    #[cfg(any(feature = "v2_16", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_16")))]
    fn connect_create_custom_widget<F: Fn(&Self) -> PrintCustomWidget + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn create_custom_widget_trampoline<
            P: IsA<PrintOperation>,
            F: Fn(&P) -> PrintCustomWidget + 'static,
        >(
            this: *mut ffi::WebKitPrintOperation,
            f: glib::ffi::gpointer,
        ) -> *mut ffi::WebKitPrintCustomWidget {
            let f: &F = &*(f as *const F);
            f(PrintOperation::from_glib_borrow(this).unsafe_cast_ref()).to_glib_full()
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"create-custom-widget\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    create_custom_widget_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_failed<F: Fn(&Self, &glib::Error) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn failed_trampoline<
            P: IsA<PrintOperation>,
            F: Fn(&P, &glib::Error) + 'static,
        >(
            this: *mut ffi::WebKitPrintOperation,
            error: *mut glib::ffi::GError,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                PrintOperation::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(error),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"failed\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    failed_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_finished<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn finished_trampoline<P: IsA<PrintOperation>, F: Fn(&P) + 'static>(
            this: *mut ffi::WebKitPrintOperation,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(PrintOperation::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"finished\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    finished_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_page_setup_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_page_setup_trampoline<
            P: IsA<PrintOperation>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::WebKitPrintOperation,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(PrintOperation::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::page-setup\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_page_setup_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_print_settings_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_print_settings_trampoline<
            P: IsA<PrintOperation>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::WebKitPrintOperation,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(PrintOperation::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::print-settings\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_print_settings_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for PrintOperation {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("PrintOperation")
    }
}