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