1use crate::{Filter, Window, ffi};
6use glib::{
7 prelude::*,
8 signal::{SignalHandlerId, connect_raw},
9 translate::*,
10};
11use std::{boxed::Box as Box_, pin::Pin};
12
13glib::wrapper! {
14 #[doc(alias = "GtkFontDialog")]
66 pub struct FontDialog(Object<ffi::GtkFontDialog, ffi::GtkFontDialogClass>);
67
68 match fn {
69 type_ => || ffi::gtk_font_dialog_get_type(),
70 }
71}
72
73impl FontDialog {
74 #[doc(alias = "gtk_font_dialog_new")]
80 pub fn new() -> FontDialog {
81 assert_initialized_main_thread!();
82 unsafe { from_glib_full(ffi::gtk_font_dialog_new()) }
83 }
84
85 pub fn builder() -> FontDialogBuilder {
90 FontDialogBuilder::new()
91 }
92
93 #[doc(alias = "gtk_font_dialog_choose_face")]
108 pub fn choose_face<P: FnOnce(Result<pango::FontFace, glib::Error>) + 'static>(
109 &self,
110 parent: Option<&impl IsA<Window>>,
111 initial_value: Option<&impl IsA<pango::FontFace>>,
112 cancellable: Option<&impl IsA<gio::Cancellable>>,
113 callback: P,
114 ) {
115 let main_context = glib::MainContext::ref_thread_default();
116 let is_main_context_owner = main_context.is_owner();
117 let has_acquired_main_context = (!is_main_context_owner)
118 .then(|| main_context.acquire().ok())
119 .flatten();
120 assert!(
121 is_main_context_owner || has_acquired_main_context.is_some(),
122 "Async operations only allowed if the thread is owning the MainContext"
123 );
124
125 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
126 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
127 unsafe extern "C" fn choose_face_trampoline<
128 P: FnOnce(Result<pango::FontFace, glib::Error>) + 'static,
129 >(
130 _source_object: *mut glib::gobject_ffi::GObject,
131 res: *mut gio::ffi::GAsyncResult,
132 user_data: glib::ffi::gpointer,
133 ) {
134 unsafe {
135 let mut error = std::ptr::null_mut();
136 let ret = ffi::gtk_font_dialog_choose_face_finish(
137 _source_object as *mut _,
138 res,
139 &mut error,
140 );
141 let result = if error.is_null() {
142 Ok(from_glib_full(ret))
143 } else {
144 Err(from_glib_full(error))
145 };
146 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
147 Box_::from_raw(user_data as *mut _);
148 let callback: P = callback.into_inner();
149 callback(result);
150 }
151 }
152 let callback = choose_face_trampoline::<P>;
153 unsafe {
154 ffi::gtk_font_dialog_choose_face(
155 self.to_glib_none().0,
156 parent.map(|p| p.as_ref()).to_glib_none().0,
157 initial_value.map(|p| p.as_ref()).to_glib_none().0,
158 cancellable.map(|p| p.as_ref()).to_glib_none().0,
159 Some(callback),
160 Box_::into_raw(user_data) as *mut _,
161 );
162 }
163 }
164
165 pub fn choose_face_future(
166 &self,
167 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
168 initial_value: Option<&(impl IsA<pango::FontFace> + Clone + 'static)>,
169 ) -> Pin<Box_<dyn std::future::Future<Output = Result<pango::FontFace, glib::Error>> + 'static>>
170 {
171 let parent = parent.map(ToOwned::to_owned);
172 let initial_value = initial_value.map(ToOwned::to_owned);
173 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
174 obj.choose_face(
175 parent.as_ref().map(::std::borrow::Borrow::borrow),
176 initial_value.as_ref().map(::std::borrow::Borrow::borrow),
177 Some(cancellable),
178 move |res| {
179 send.resolve(res);
180 },
181 );
182 }))
183 }
184
185 #[doc(alias = "gtk_font_dialog_choose_family")]
198 pub fn choose_family<P: FnOnce(Result<pango::FontFamily, glib::Error>) + 'static>(
199 &self,
200 parent: Option<&impl IsA<Window>>,
201 initial_value: Option<&impl IsA<pango::FontFamily>>,
202 cancellable: Option<&impl IsA<gio::Cancellable>>,
203 callback: P,
204 ) {
205 let main_context = glib::MainContext::ref_thread_default();
206 let is_main_context_owner = main_context.is_owner();
207 let has_acquired_main_context = (!is_main_context_owner)
208 .then(|| main_context.acquire().ok())
209 .flatten();
210 assert!(
211 is_main_context_owner || has_acquired_main_context.is_some(),
212 "Async operations only allowed if the thread is owning the MainContext"
213 );
214
215 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
216 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
217 unsafe extern "C" fn choose_family_trampoline<
218 P: FnOnce(Result<pango::FontFamily, glib::Error>) + 'static,
219 >(
220 _source_object: *mut glib::gobject_ffi::GObject,
221 res: *mut gio::ffi::GAsyncResult,
222 user_data: glib::ffi::gpointer,
223 ) {
224 unsafe {
225 let mut error = std::ptr::null_mut();
226 let ret = ffi::gtk_font_dialog_choose_family_finish(
227 _source_object as *mut _,
228 res,
229 &mut error,
230 );
231 let result = if error.is_null() {
232 Ok(from_glib_full(ret))
233 } else {
234 Err(from_glib_full(error))
235 };
236 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
237 Box_::from_raw(user_data as *mut _);
238 let callback: P = callback.into_inner();
239 callback(result);
240 }
241 }
242 let callback = choose_family_trampoline::<P>;
243 unsafe {
244 ffi::gtk_font_dialog_choose_family(
245 self.to_glib_none().0,
246 parent.map(|p| p.as_ref()).to_glib_none().0,
247 initial_value.map(|p| p.as_ref()).to_glib_none().0,
248 cancellable.map(|p| p.as_ref()).to_glib_none().0,
249 Some(callback),
250 Box_::into_raw(user_data) as *mut _,
251 );
252 }
253 }
254
255 pub fn choose_family_future(
256 &self,
257 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
258 initial_value: Option<&(impl IsA<pango::FontFamily> + Clone + 'static)>,
259 ) -> Pin<Box_<dyn std::future::Future<Output = Result<pango::FontFamily, glib::Error>> + 'static>>
260 {
261 let parent = parent.map(ToOwned::to_owned);
262 let initial_value = initial_value.map(ToOwned::to_owned);
263 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
264 obj.choose_family(
265 parent.as_ref().map(::std::borrow::Borrow::borrow),
266 initial_value.as_ref().map(::std::borrow::Borrow::borrow),
267 Some(cancellable),
268 move |res| {
269 send.resolve(res);
270 },
271 );
272 }))
273 }
274
275 #[doc(alias = "gtk_font_dialog_choose_font")]
291 pub fn choose_font<P: FnOnce(Result<pango::FontDescription, glib::Error>) + 'static>(
292 &self,
293 parent: Option<&impl IsA<Window>>,
294 initial_value: Option<&pango::FontDescription>,
295 cancellable: Option<&impl IsA<gio::Cancellable>>,
296 callback: P,
297 ) {
298 let main_context = glib::MainContext::ref_thread_default();
299 let is_main_context_owner = main_context.is_owner();
300 let has_acquired_main_context = (!is_main_context_owner)
301 .then(|| main_context.acquire().ok())
302 .flatten();
303 assert!(
304 is_main_context_owner || has_acquired_main_context.is_some(),
305 "Async operations only allowed if the thread is owning the MainContext"
306 );
307
308 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
309 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
310 unsafe extern "C" fn choose_font_trampoline<
311 P: FnOnce(Result<pango::FontDescription, glib::Error>) + 'static,
312 >(
313 _source_object: *mut glib::gobject_ffi::GObject,
314 res: *mut gio::ffi::GAsyncResult,
315 user_data: glib::ffi::gpointer,
316 ) {
317 unsafe {
318 let mut error = std::ptr::null_mut();
319 let ret = ffi::gtk_font_dialog_choose_font_finish(
320 _source_object as *mut _,
321 res,
322 &mut error,
323 );
324 let result = if error.is_null() {
325 Ok(from_glib_full(ret))
326 } else {
327 Err(from_glib_full(error))
328 };
329 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
330 Box_::from_raw(user_data as *mut _);
331 let callback: P = callback.into_inner();
332 callback(result);
333 }
334 }
335 let callback = choose_font_trampoline::<P>;
336 unsafe {
337 ffi::gtk_font_dialog_choose_font(
338 self.to_glib_none().0,
339 parent.map(|p| p.as_ref()).to_glib_none().0,
340 mut_override(initial_value.to_glib_none().0),
341 cancellable.map(|p| p.as_ref()).to_glib_none().0,
342 Some(callback),
343 Box_::into_raw(user_data) as *mut _,
344 );
345 }
346 }
347
348 pub fn choose_font_future(
349 &self,
350 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
351 initial_value: Option<&pango::FontDescription>,
352 ) -> Pin<
353 Box_<
354 dyn std::future::Future<Output = Result<pango::FontDescription, glib::Error>> + 'static,
355 >,
356 > {
357 let parent = parent.map(ToOwned::to_owned);
358 let initial_value = initial_value.map(ToOwned::to_owned);
359 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
360 obj.choose_font(
361 parent.as_ref().map(::std::borrow::Borrow::borrow),
362 initial_value.as_ref().map(::std::borrow::Borrow::borrow),
363 Some(cancellable),
364 move |res| {
365 send.resolve(res);
366 },
367 );
368 }))
369 }
370
371 #[doc(alias = "gtk_font_dialog_get_filter")]
378 #[doc(alias = "get_filter")]
379 pub fn filter(&self) -> Option<Filter> {
380 unsafe { from_glib_none(ffi::gtk_font_dialog_get_filter(self.to_glib_none().0)) }
381 }
382
383 #[doc(alias = "gtk_font_dialog_get_font_map")]
390 #[doc(alias = "get_font_map")]
391 #[doc(alias = "font-map")]
392 pub fn font_map(&self) -> Option<pango::FontMap> {
393 unsafe { from_glib_none(ffi::gtk_font_dialog_get_font_map(self.to_glib_none().0)) }
394 }
395
396 #[doc(alias = "gtk_font_dialog_get_language")]
402 #[doc(alias = "get_language")]
403 pub fn language(&self) -> Option<pango::Language> {
404 unsafe { from_glib_full(ffi::gtk_font_dialog_get_language(self.to_glib_none().0)) }
405 }
406
407 #[doc(alias = "gtk_font_dialog_get_modal")]
414 #[doc(alias = "get_modal")]
415 #[doc(alias = "modal")]
416 pub fn is_modal(&self) -> bool {
417 unsafe { from_glib(ffi::gtk_font_dialog_get_modal(self.to_glib_none().0)) }
418 }
419
420 #[doc(alias = "gtk_font_dialog_get_title")]
426 #[doc(alias = "get_title")]
427 pub fn title(&self) -> glib::GString {
428 unsafe { from_glib_none(ffi::gtk_font_dialog_get_title(self.to_glib_none().0)) }
429 }
430
431 #[doc(alias = "gtk_font_dialog_set_filter")]
439 #[doc(alias = "filter")]
440 pub fn set_filter(&self, filter: Option<&impl IsA<Filter>>) {
441 unsafe {
442 ffi::gtk_font_dialog_set_filter(
443 self.to_glib_none().0,
444 filter.map(|p| p.as_ref()).to_glib_none().0,
445 );
446 }
447 }
448
449 #[doc(alias = "gtk_font_dialog_set_font_map")]
455 #[doc(alias = "font-map")]
456 pub fn set_font_map(&self, fontmap: Option<&impl IsA<pango::FontMap>>) {
457 unsafe {
458 ffi::gtk_font_dialog_set_font_map(
459 self.to_glib_none().0,
460 fontmap.map(|p| p.as_ref()).to_glib_none().0,
461 );
462 }
463 }
464
465 #[doc(alias = "gtk_font_dialog_set_language")]
469 #[doc(alias = "language")]
470 pub fn set_language(&self, language: &pango::Language) {
471 unsafe {
472 ffi::gtk_font_dialog_set_language(
473 self.to_glib_none().0,
474 mut_override(language.to_glib_none().0),
475 );
476 }
477 }
478
479 #[doc(alias = "gtk_font_dialog_set_modal")]
484 #[doc(alias = "modal")]
485 pub fn set_modal(&self, modal: bool) {
486 unsafe {
487 ffi::gtk_font_dialog_set_modal(self.to_glib_none().0, modal.into_glib());
488 }
489 }
490
491 #[doc(alias = "gtk_font_dialog_set_title")]
495 #[doc(alias = "title")]
496 pub fn set_title(&self, title: &str) {
497 unsafe {
498 ffi::gtk_font_dialog_set_title(self.to_glib_none().0, title.to_glib_none().0);
499 }
500 }
501
502 #[cfg(feature = "v4_10")]
503 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
504 #[doc(alias = "filter")]
505 pub fn connect_filter_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
506 unsafe extern "C" fn notify_filter_trampoline<F: Fn(&FontDialog) + 'static>(
507 this: *mut ffi::GtkFontDialog,
508 _param_spec: glib::ffi::gpointer,
509 f: glib::ffi::gpointer,
510 ) {
511 unsafe {
512 let f: &F = &*(f as *const F);
513 f(&from_glib_borrow(this))
514 }
515 }
516 unsafe {
517 let f: Box_<F> = Box_::new(f);
518 connect_raw(
519 self.as_ptr() as *mut _,
520 c"notify::filter".as_ptr() as *const _,
521 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
522 notify_filter_trampoline::<F> as *const (),
523 )),
524 Box_::into_raw(f),
525 )
526 }
527 }
528
529 #[cfg(feature = "v4_10")]
530 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
531 #[doc(alias = "font-map")]
532 pub fn connect_font_map_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
533 unsafe extern "C" fn notify_font_map_trampoline<F: Fn(&FontDialog) + 'static>(
534 this: *mut ffi::GtkFontDialog,
535 _param_spec: glib::ffi::gpointer,
536 f: glib::ffi::gpointer,
537 ) {
538 unsafe {
539 let f: &F = &*(f as *const F);
540 f(&from_glib_borrow(this))
541 }
542 }
543 unsafe {
544 let f: Box_<F> = Box_::new(f);
545 connect_raw(
546 self.as_ptr() as *mut _,
547 c"notify::font-map".as_ptr() as *const _,
548 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
549 notify_font_map_trampoline::<F> as *const (),
550 )),
551 Box_::into_raw(f),
552 )
553 }
554 }
555
556 #[cfg(feature = "v4_10")]
557 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
558 #[doc(alias = "language")]
559 pub fn connect_language_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
560 unsafe extern "C" fn notify_language_trampoline<F: Fn(&FontDialog) + 'static>(
561 this: *mut ffi::GtkFontDialog,
562 _param_spec: glib::ffi::gpointer,
563 f: glib::ffi::gpointer,
564 ) {
565 unsafe {
566 let f: &F = &*(f as *const F);
567 f(&from_glib_borrow(this))
568 }
569 }
570 unsafe {
571 let f: Box_<F> = Box_::new(f);
572 connect_raw(
573 self.as_ptr() as *mut _,
574 c"notify::language".as_ptr() as *const _,
575 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
576 notify_language_trampoline::<F> as *const (),
577 )),
578 Box_::into_raw(f),
579 )
580 }
581 }
582
583 #[cfg(feature = "v4_10")]
584 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
585 #[doc(alias = "modal")]
586 pub fn connect_modal_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
587 unsafe extern "C" fn notify_modal_trampoline<F: Fn(&FontDialog) + 'static>(
588 this: *mut ffi::GtkFontDialog,
589 _param_spec: glib::ffi::gpointer,
590 f: glib::ffi::gpointer,
591 ) {
592 unsafe {
593 let f: &F = &*(f as *const F);
594 f(&from_glib_borrow(this))
595 }
596 }
597 unsafe {
598 let f: Box_<F> = Box_::new(f);
599 connect_raw(
600 self.as_ptr() as *mut _,
601 c"notify::modal".as_ptr() as *const _,
602 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
603 notify_modal_trampoline::<F> as *const (),
604 )),
605 Box_::into_raw(f),
606 )
607 }
608 }
609
610 #[cfg(feature = "v4_10")]
611 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
612 #[doc(alias = "title")]
613 pub fn connect_title_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
614 unsafe extern "C" fn notify_title_trampoline<F: Fn(&FontDialog) + 'static>(
615 this: *mut ffi::GtkFontDialog,
616 _param_spec: glib::ffi::gpointer,
617 f: glib::ffi::gpointer,
618 ) {
619 unsafe {
620 let f: &F = &*(f as *const F);
621 f(&from_glib_borrow(this))
622 }
623 }
624 unsafe {
625 let f: Box_<F> = Box_::new(f);
626 connect_raw(
627 self.as_ptr() as *mut _,
628 c"notify::title".as_ptr() as *const _,
629 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
630 notify_title_trampoline::<F> as *const (),
631 )),
632 Box_::into_raw(f),
633 )
634 }
635 }
636}
637
638#[cfg(feature = "v4_10")]
639#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
640impl Default for FontDialog {
641 fn default() -> Self {
642 Self::new()
643 }
644}
645
646#[must_use = "The builder must be built to be used"]
651pub struct FontDialogBuilder {
652 builder: glib::object::ObjectBuilder<'static, FontDialog>,
653}
654
655impl FontDialogBuilder {
656 fn new() -> Self {
657 Self {
658 builder: glib::object::Object::builder(),
659 }
660 }
661
662 #[cfg(feature = "v4_10")]
664 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
665 pub fn filter(self, filter: &impl IsA<Filter>) -> Self {
666 Self {
667 builder: self.builder.property("filter", filter.clone().upcast()),
668 }
669 }
670
671 #[cfg(feature = "v4_10")]
676 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
677 pub fn font_map(self, font_map: &impl IsA<pango::FontMap>) -> Self {
678 Self {
679 builder: self.builder.property("font-map", font_map.clone().upcast()),
680 }
681 }
682
683 #[cfg(feature = "v4_10")]
685 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
686 pub fn language(self, language: &pango::Language) -> Self {
687 Self {
688 builder: self.builder.property("language", language),
689 }
690 }
691
692 #[cfg(feature = "v4_10")]
694 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
695 pub fn modal(self, modal: bool) -> Self {
696 Self {
697 builder: self.builder.property("modal", modal),
698 }
699 }
700
701 #[cfg(feature = "v4_10")]
704 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
705 pub fn title(self, title: impl Into<glib::GString>) -> Self {
706 Self {
707 builder: self.builder.property("title", title.into()),
708 }
709 }
710
711 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
714 pub fn build(self) -> FontDialog {
715 assert_initialized_main_thread!();
716 self.builder.build()
717 }
718}