1use crate::{ffi, PageSetup, PrintSettings, PrintSetup, Window};
6use glib::{
7 prelude::*,
8 signal::{connect_raw, SignalHandlerId},
9 translate::*,
10};
11use std::{boxed::Box as Box_, pin::Pin};
12
13glib::wrapper! {
14 #[doc(alias = "GtkPrintDialog")]
64 pub struct PrintDialog(Object<ffi::GtkPrintDialog, ffi::GtkPrintDialogClass>);
65
66 match fn {
67 type_ => || ffi::gtk_print_dialog_get_type(),
68 }
69}
70
71impl PrintDialog {
72 #[doc(alias = "gtk_print_dialog_new")]
78 pub fn new() -> PrintDialog {
79 assert_initialized_main_thread!();
80 unsafe { from_glib_full(ffi::gtk_print_dialog_new()) }
81 }
82
83 pub fn builder() -> PrintDialogBuilder {
88 PrintDialogBuilder::new()
89 }
90
91 #[doc(alias = "gtk_print_dialog_get_accept_label")]
98 #[doc(alias = "get_accept_label")]
99 #[doc(alias = "accept-label")]
100 pub fn accept_label(&self) -> glib::GString {
101 unsafe {
102 from_glib_none(ffi::gtk_print_dialog_get_accept_label(
103 self.to_glib_none().0,
104 ))
105 }
106 }
107
108 #[doc(alias = "gtk_print_dialog_get_modal")]
116 #[doc(alias = "get_modal")]
117 #[doc(alias = "modal")]
118 pub fn is_modal(&self) -> bool {
119 unsafe { from_glib(ffi::gtk_print_dialog_get_modal(self.to_glib_none().0)) }
120 }
121
122 #[doc(alias = "gtk_print_dialog_get_page_setup")]
128 #[doc(alias = "get_page_setup")]
129 #[doc(alias = "page-setup")]
130 pub fn page_setup(&self) -> Option<PageSetup> {
131 unsafe { from_glib_none(ffi::gtk_print_dialog_get_page_setup(self.to_glib_none().0)) }
132 }
133
134 #[doc(alias = "gtk_print_dialog_get_print_settings")]
140 #[doc(alias = "get_print_settings")]
141 #[doc(alias = "print-settings")]
142 pub fn print_settings(&self) -> Option<PrintSettings> {
143 unsafe {
144 from_glib_none(ffi::gtk_print_dialog_get_print_settings(
145 self.to_glib_none().0,
146 ))
147 }
148 }
149
150 #[doc(alias = "gtk_print_dialog_get_title")]
157 #[doc(alias = "get_title")]
158 pub fn title(&self) -> glib::GString {
159 unsafe { from_glib_none(ffi::gtk_print_dialog_get_title(self.to_glib_none().0)) }
160 }
161
162 #[doc(alias = "gtk_print_dialog_print")]
178 pub fn print<P: FnOnce(Result<gio::OutputStream, glib::Error>) + 'static>(
179 &self,
180 parent: Option<&impl IsA<Window>>,
181 setup: Option<&PrintSetup>,
182 cancellable: Option<&impl IsA<gio::Cancellable>>,
183 callback: P,
184 ) {
185 let main_context = glib::MainContext::ref_thread_default();
186 let is_main_context_owner = main_context.is_owner();
187 let has_acquired_main_context = (!is_main_context_owner)
188 .then(|| main_context.acquire().ok())
189 .flatten();
190 assert!(
191 is_main_context_owner || has_acquired_main_context.is_some(),
192 "Async operations only allowed if the thread is owning the MainContext"
193 );
194
195 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
196 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
197 unsafe extern "C" fn print_trampoline<
198 P: FnOnce(Result<gio::OutputStream, glib::Error>) + 'static,
199 >(
200 _source_object: *mut glib::gobject_ffi::GObject,
201 res: *mut gio::ffi::GAsyncResult,
202 user_data: glib::ffi::gpointer,
203 ) {
204 let mut error = std::ptr::null_mut();
205 let ret = ffi::gtk_print_dialog_print_finish(_source_object as *mut _, res, &mut error);
206 let result = if error.is_null() {
207 Ok(from_glib_full(ret))
208 } else {
209 Err(from_glib_full(error))
210 };
211 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
212 Box_::from_raw(user_data as *mut _);
213 let callback: P = callback.into_inner();
214 callback(result);
215 }
216 let callback = print_trampoline::<P>;
217 unsafe {
218 ffi::gtk_print_dialog_print(
219 self.to_glib_none().0,
220 parent.map(|p| p.as_ref()).to_glib_none().0,
221 setup.to_glib_none().0,
222 cancellable.map(|p| p.as_ref()).to_glib_none().0,
223 Some(callback),
224 Box_::into_raw(user_data) as *mut _,
225 );
226 }
227 }
228
229 pub fn print_future(
230 &self,
231 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
232 setup: Option<&PrintSetup>,
233 ) -> Pin<Box_<dyn std::future::Future<Output = Result<gio::OutputStream, glib::Error>> + 'static>>
234 {
235 let parent = parent.map(ToOwned::to_owned);
236 let setup = setup.map(ToOwned::to_owned);
237 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
238 obj.print(
239 parent.as_ref().map(::std::borrow::Borrow::borrow),
240 setup.as_ref().map(::std::borrow::Borrow::borrow),
241 Some(cancellable),
242 move |res| {
243 send.resolve(res);
244 },
245 );
246 }))
247 }
248
249 #[doc(alias = "gtk_print_dialog_print_file")]
265 pub fn print_file<P: FnOnce(Result<(), glib::Error>) + 'static>(
266 &self,
267 parent: Option<&impl IsA<Window>>,
268 setup: Option<&PrintSetup>,
269 file: &impl IsA<gio::File>,
270 cancellable: Option<&impl IsA<gio::Cancellable>>,
271 callback: P,
272 ) {
273 let main_context = glib::MainContext::ref_thread_default();
274 let is_main_context_owner = main_context.is_owner();
275 let has_acquired_main_context = (!is_main_context_owner)
276 .then(|| main_context.acquire().ok())
277 .flatten();
278 assert!(
279 is_main_context_owner || has_acquired_main_context.is_some(),
280 "Async operations only allowed if the thread is owning the MainContext"
281 );
282
283 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
284 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
285 unsafe extern "C" fn print_file_trampoline<P: FnOnce(Result<(), glib::Error>) + 'static>(
286 _source_object: *mut glib::gobject_ffi::GObject,
287 res: *mut gio::ffi::GAsyncResult,
288 user_data: glib::ffi::gpointer,
289 ) {
290 let mut error = std::ptr::null_mut();
291 ffi::gtk_print_dialog_print_file_finish(_source_object as *mut _, res, &mut error);
292 let result = if error.is_null() {
293 Ok(())
294 } else {
295 Err(from_glib_full(error))
296 };
297 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
298 Box_::from_raw(user_data as *mut _);
299 let callback: P = callback.into_inner();
300 callback(result);
301 }
302 let callback = print_file_trampoline::<P>;
303 unsafe {
304 ffi::gtk_print_dialog_print_file(
305 self.to_glib_none().0,
306 parent.map(|p| p.as_ref()).to_glib_none().0,
307 setup.to_glib_none().0,
308 file.as_ref().to_glib_none().0,
309 cancellable.map(|p| p.as_ref()).to_glib_none().0,
310 Some(callback),
311 Box_::into_raw(user_data) as *mut _,
312 );
313 }
314 }
315
316 pub fn print_file_future(
317 &self,
318 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
319 setup: Option<&PrintSetup>,
320 file: &(impl IsA<gio::File> + Clone + 'static),
321 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
322 let parent = parent.map(ToOwned::to_owned);
323 let setup = setup.map(ToOwned::to_owned);
324 let file = file.clone();
325 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
326 obj.print_file(
327 parent.as_ref().map(::std::borrow::Borrow::borrow),
328 setup.as_ref().map(::std::borrow::Borrow::borrow),
329 &file,
330 Some(cancellable),
331 move |res| {
332 send.resolve(res);
333 },
334 );
335 }))
336 }
337
338 #[doc(alias = "gtk_print_dialog_set_accept_label")]
344 #[doc(alias = "accept-label")]
345 pub fn set_accept_label(&self, accept_label: &str) {
346 unsafe {
347 ffi::gtk_print_dialog_set_accept_label(
348 self.to_glib_none().0,
349 accept_label.to_glib_none().0,
350 );
351 }
352 }
353
354 #[doc(alias = "gtk_print_dialog_set_modal")]
360 #[doc(alias = "modal")]
361 pub fn set_modal(&self, modal: bool) {
362 unsafe {
363 ffi::gtk_print_dialog_set_modal(self.to_glib_none().0, modal.into_glib());
364 }
365 }
366
367 #[doc(alias = "gtk_print_dialog_set_page_setup")]
371 #[doc(alias = "page-setup")]
372 pub fn set_page_setup(&self, page_setup: &PageSetup) {
373 unsafe {
374 ffi::gtk_print_dialog_set_page_setup(
375 self.to_glib_none().0,
376 page_setup.to_glib_none().0,
377 );
378 }
379 }
380
381 #[doc(alias = "gtk_print_dialog_set_print_settings")]
385 #[doc(alias = "print-settings")]
386 pub fn set_print_settings(&self, print_settings: &PrintSettings) {
387 unsafe {
388 ffi::gtk_print_dialog_set_print_settings(
389 self.to_glib_none().0,
390 print_settings.to_glib_none().0,
391 );
392 }
393 }
394
395 #[doc(alias = "gtk_print_dialog_set_title")]
399 #[doc(alias = "title")]
400 pub fn set_title(&self, title: &str) {
401 unsafe {
402 ffi::gtk_print_dialog_set_title(self.to_glib_none().0, title.to_glib_none().0);
403 }
404 }
405
406 #[doc(alias = "gtk_print_dialog_setup")]
425 pub fn setup<P: FnOnce(Result<PrintSetup, glib::Error>) + 'static>(
426 &self,
427 parent: Option<&impl IsA<Window>>,
428 cancellable: Option<&impl IsA<gio::Cancellable>>,
429 callback: P,
430 ) {
431 let main_context = glib::MainContext::ref_thread_default();
432 let is_main_context_owner = main_context.is_owner();
433 let has_acquired_main_context = (!is_main_context_owner)
434 .then(|| main_context.acquire().ok())
435 .flatten();
436 assert!(
437 is_main_context_owner || has_acquired_main_context.is_some(),
438 "Async operations only allowed if the thread is owning the MainContext"
439 );
440
441 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
442 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
443 unsafe extern "C" fn setup_trampoline<
444 P: FnOnce(Result<PrintSetup, glib::Error>) + 'static,
445 >(
446 _source_object: *mut glib::gobject_ffi::GObject,
447 res: *mut gio::ffi::GAsyncResult,
448 user_data: glib::ffi::gpointer,
449 ) {
450 let mut error = std::ptr::null_mut();
451 let ret = ffi::gtk_print_dialog_setup_finish(_source_object as *mut _, res, &mut error);
452 let result = if error.is_null() {
453 Ok(from_glib_full(ret))
454 } else {
455 Err(from_glib_full(error))
456 };
457 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
458 Box_::from_raw(user_data as *mut _);
459 let callback: P = callback.into_inner();
460 callback(result);
461 }
462 let callback = setup_trampoline::<P>;
463 unsafe {
464 ffi::gtk_print_dialog_setup(
465 self.to_glib_none().0,
466 parent.map(|p| p.as_ref()).to_glib_none().0,
467 cancellable.map(|p| p.as_ref()).to_glib_none().0,
468 Some(callback),
469 Box_::into_raw(user_data) as *mut _,
470 );
471 }
472 }
473
474 pub fn setup_future(
475 &self,
476 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
477 ) -> Pin<Box_<dyn std::future::Future<Output = Result<PrintSetup, glib::Error>> + 'static>>
478 {
479 let parent = parent.map(ToOwned::to_owned);
480 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
481 obj.setup(
482 parent.as_ref().map(::std::borrow::Borrow::borrow),
483 Some(cancellable),
484 move |res| {
485 send.resolve(res);
486 },
487 );
488 }))
489 }
490
491 #[cfg(feature = "v4_14")]
492 #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
493 #[doc(alias = "accept-label")]
494 pub fn connect_accept_label_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
495 unsafe extern "C" fn notify_accept_label_trampoline<F: Fn(&PrintDialog) + 'static>(
496 this: *mut ffi::GtkPrintDialog,
497 _param_spec: glib::ffi::gpointer,
498 f: glib::ffi::gpointer,
499 ) {
500 let f: &F = &*(f as *const F);
501 f(&from_glib_borrow(this))
502 }
503 unsafe {
504 let f: Box_<F> = Box_::new(f);
505 connect_raw(
506 self.as_ptr() as *mut _,
507 c"notify::accept-label".as_ptr() as *const _,
508 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
509 notify_accept_label_trampoline::<F> as *const (),
510 )),
511 Box_::into_raw(f),
512 )
513 }
514 }
515
516 #[cfg(feature = "v4_14")]
517 #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
518 #[doc(alias = "modal")]
519 pub fn connect_modal_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
520 unsafe extern "C" fn notify_modal_trampoline<F: Fn(&PrintDialog) + 'static>(
521 this: *mut ffi::GtkPrintDialog,
522 _param_spec: glib::ffi::gpointer,
523 f: glib::ffi::gpointer,
524 ) {
525 let f: &F = &*(f as *const F);
526 f(&from_glib_borrow(this))
527 }
528 unsafe {
529 let f: Box_<F> = Box_::new(f);
530 connect_raw(
531 self.as_ptr() as *mut _,
532 c"notify::modal".as_ptr() as *const _,
533 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
534 notify_modal_trampoline::<F> as *const (),
535 )),
536 Box_::into_raw(f),
537 )
538 }
539 }
540
541 #[cfg(feature = "v4_14")]
542 #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
543 #[doc(alias = "page-setup")]
544 pub fn connect_page_setup_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
545 unsafe extern "C" fn notify_page_setup_trampoline<F: Fn(&PrintDialog) + 'static>(
546 this: *mut ffi::GtkPrintDialog,
547 _param_spec: glib::ffi::gpointer,
548 f: glib::ffi::gpointer,
549 ) {
550 let f: &F = &*(f as *const F);
551 f(&from_glib_borrow(this))
552 }
553 unsafe {
554 let f: Box_<F> = Box_::new(f);
555 connect_raw(
556 self.as_ptr() as *mut _,
557 c"notify::page-setup".as_ptr() as *const _,
558 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
559 notify_page_setup_trampoline::<F> as *const (),
560 )),
561 Box_::into_raw(f),
562 )
563 }
564 }
565
566 #[cfg(feature = "v4_14")]
567 #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
568 #[doc(alias = "print-settings")]
569 pub fn connect_print_settings_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
570 unsafe extern "C" fn notify_print_settings_trampoline<F: Fn(&PrintDialog) + 'static>(
571 this: *mut ffi::GtkPrintDialog,
572 _param_spec: glib::ffi::gpointer,
573 f: glib::ffi::gpointer,
574 ) {
575 let f: &F = &*(f as *const F);
576 f(&from_glib_borrow(this))
577 }
578 unsafe {
579 let f: Box_<F> = Box_::new(f);
580 connect_raw(
581 self.as_ptr() as *mut _,
582 c"notify::print-settings".as_ptr() as *const _,
583 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
584 notify_print_settings_trampoline::<F> as *const (),
585 )),
586 Box_::into_raw(f),
587 )
588 }
589 }
590
591 #[cfg(feature = "v4_14")]
592 #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
593 #[doc(alias = "title")]
594 pub fn connect_title_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
595 unsafe extern "C" fn notify_title_trampoline<F: Fn(&PrintDialog) + 'static>(
596 this: *mut ffi::GtkPrintDialog,
597 _param_spec: glib::ffi::gpointer,
598 f: glib::ffi::gpointer,
599 ) {
600 let f: &F = &*(f as *const F);
601 f(&from_glib_borrow(this))
602 }
603 unsafe {
604 let f: Box_<F> = Box_::new(f);
605 connect_raw(
606 self.as_ptr() as *mut _,
607 c"notify::title".as_ptr() as *const _,
608 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
609 notify_title_trampoline::<F> as *const (),
610 )),
611 Box_::into_raw(f),
612 )
613 }
614 }
615}
616
617#[cfg(feature = "v4_14")]
618#[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
619impl Default for PrintDialog {
620 fn default() -> Self {
621 Self::new()
622 }
623}
624
625#[must_use = "The builder must be built to be used"]
630pub struct PrintDialogBuilder {
631 builder: glib::object::ObjectBuilder<'static, PrintDialog>,
632}
633
634impl PrintDialogBuilder {
635 fn new() -> Self {
636 Self {
637 builder: glib::object::Object::builder(),
638 }
639 }
640
641 #[cfg(feature = "v4_14")]
644 #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
645 pub fn accept_label(self, accept_label: impl Into<glib::GString>) -> Self {
646 Self {
647 builder: self.builder.property("accept-label", accept_label.into()),
648 }
649 }
650
651 #[cfg(feature = "v4_14")]
653 #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
654 pub fn modal(self, modal: bool) -> Self {
655 Self {
656 builder: self.builder.property("modal", modal),
657 }
658 }
659
660 #[cfg(feature = "v4_14")]
662 #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
663 pub fn page_setup(self, page_setup: &PageSetup) -> Self {
664 Self {
665 builder: self.builder.property("page-setup", page_setup.clone()),
666 }
667 }
668
669 #[cfg(feature = "v4_14")]
671 #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
672 pub fn print_settings(self, print_settings: &PrintSettings) -> Self {
673 Self {
674 builder: self
675 .builder
676 .property("print-settings", print_settings.clone()),
677 }
678 }
679
680 #[cfg(feature = "v4_14")]
683 #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
684 pub fn title(self, title: impl Into<glib::GString>) -> Self {
685 Self {
686 builder: self.builder.property("title", title.into()),
687 }
688 }
689
690 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
693 pub fn build(self) -> PrintDialog {
694 assert_initialized_main_thread!();
695 self.builder.build()
696 }
697}