1use crate::{ffi, Filter, 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 = "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 let mut error = std::ptr::null_mut();
135 let ret =
136 ffi::gtk_font_dialog_choose_face_finish(_source_object as *mut _, res, &mut error);
137 let result = if error.is_null() {
138 Ok(from_glib_full(ret))
139 } else {
140 Err(from_glib_full(error))
141 };
142 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
143 Box_::from_raw(user_data as *mut _);
144 let callback: P = callback.into_inner();
145 callback(result);
146 }
147 let callback = choose_face_trampoline::<P>;
148 unsafe {
149 ffi::gtk_font_dialog_choose_face(
150 self.to_glib_none().0,
151 parent.map(|p| p.as_ref()).to_glib_none().0,
152 initial_value.map(|p| p.as_ref()).to_glib_none().0,
153 cancellable.map(|p| p.as_ref()).to_glib_none().0,
154 Some(callback),
155 Box_::into_raw(user_data) as *mut _,
156 );
157 }
158 }
159
160 pub fn choose_face_future(
161 &self,
162 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
163 initial_value: Option<&(impl IsA<pango::FontFace> + Clone + 'static)>,
164 ) -> Pin<Box_<dyn std::future::Future<Output = Result<pango::FontFace, glib::Error>> + 'static>>
165 {
166 let parent = parent.map(ToOwned::to_owned);
167 let initial_value = initial_value.map(ToOwned::to_owned);
168 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
169 obj.choose_face(
170 parent.as_ref().map(::std::borrow::Borrow::borrow),
171 initial_value.as_ref().map(::std::borrow::Borrow::borrow),
172 Some(cancellable),
173 move |res| {
174 send.resolve(res);
175 },
176 );
177 }))
178 }
179
180 #[doc(alias = "gtk_font_dialog_choose_family")]
193 pub fn choose_family<P: FnOnce(Result<pango::FontFamily, glib::Error>) + 'static>(
194 &self,
195 parent: Option<&impl IsA<Window>>,
196 initial_value: Option<&impl IsA<pango::FontFamily>>,
197 cancellable: Option<&impl IsA<gio::Cancellable>>,
198 callback: P,
199 ) {
200 let main_context = glib::MainContext::ref_thread_default();
201 let is_main_context_owner = main_context.is_owner();
202 let has_acquired_main_context = (!is_main_context_owner)
203 .then(|| main_context.acquire().ok())
204 .flatten();
205 assert!(
206 is_main_context_owner || has_acquired_main_context.is_some(),
207 "Async operations only allowed if the thread is owning the MainContext"
208 );
209
210 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
211 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
212 unsafe extern "C" fn choose_family_trampoline<
213 P: FnOnce(Result<pango::FontFamily, glib::Error>) + 'static,
214 >(
215 _source_object: *mut glib::gobject_ffi::GObject,
216 res: *mut gio::ffi::GAsyncResult,
217 user_data: glib::ffi::gpointer,
218 ) {
219 let mut error = std::ptr::null_mut();
220 let ret = ffi::gtk_font_dialog_choose_family_finish(
221 _source_object as *mut _,
222 res,
223 &mut error,
224 );
225 let result = if error.is_null() {
226 Ok(from_glib_full(ret))
227 } else {
228 Err(from_glib_full(error))
229 };
230 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
231 Box_::from_raw(user_data as *mut _);
232 let callback: P = callback.into_inner();
233 callback(result);
234 }
235 let callback = choose_family_trampoline::<P>;
236 unsafe {
237 ffi::gtk_font_dialog_choose_family(
238 self.to_glib_none().0,
239 parent.map(|p| p.as_ref()).to_glib_none().0,
240 initial_value.map(|p| p.as_ref()).to_glib_none().0,
241 cancellable.map(|p| p.as_ref()).to_glib_none().0,
242 Some(callback),
243 Box_::into_raw(user_data) as *mut _,
244 );
245 }
246 }
247
248 pub fn choose_family_future(
249 &self,
250 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
251 initial_value: Option<&(impl IsA<pango::FontFamily> + Clone + 'static)>,
252 ) -> Pin<Box_<dyn std::future::Future<Output = Result<pango::FontFamily, glib::Error>> + 'static>>
253 {
254 let parent = parent.map(ToOwned::to_owned);
255 let initial_value = initial_value.map(ToOwned::to_owned);
256 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
257 obj.choose_family(
258 parent.as_ref().map(::std::borrow::Borrow::borrow),
259 initial_value.as_ref().map(::std::borrow::Borrow::borrow),
260 Some(cancellable),
261 move |res| {
262 send.resolve(res);
263 },
264 );
265 }))
266 }
267
268 #[doc(alias = "gtk_font_dialog_choose_font")]
284 pub fn choose_font<P: FnOnce(Result<pango::FontDescription, glib::Error>) + 'static>(
285 &self,
286 parent: Option<&impl IsA<Window>>,
287 initial_value: Option<&pango::FontDescription>,
288 cancellable: Option<&impl IsA<gio::Cancellable>>,
289 callback: P,
290 ) {
291 let main_context = glib::MainContext::ref_thread_default();
292 let is_main_context_owner = main_context.is_owner();
293 let has_acquired_main_context = (!is_main_context_owner)
294 .then(|| main_context.acquire().ok())
295 .flatten();
296 assert!(
297 is_main_context_owner || has_acquired_main_context.is_some(),
298 "Async operations only allowed if the thread is owning the MainContext"
299 );
300
301 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
302 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
303 unsafe extern "C" fn choose_font_trampoline<
304 P: FnOnce(Result<pango::FontDescription, glib::Error>) + 'static,
305 >(
306 _source_object: *mut glib::gobject_ffi::GObject,
307 res: *mut gio::ffi::GAsyncResult,
308 user_data: glib::ffi::gpointer,
309 ) {
310 let mut error = std::ptr::null_mut();
311 let ret =
312 ffi::gtk_font_dialog_choose_font_finish(_source_object as *mut _, res, &mut error);
313 let result = if error.is_null() {
314 Ok(from_glib_full(ret))
315 } else {
316 Err(from_glib_full(error))
317 };
318 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
319 Box_::from_raw(user_data as *mut _);
320 let callback: P = callback.into_inner();
321 callback(result);
322 }
323 let callback = choose_font_trampoline::<P>;
324 unsafe {
325 ffi::gtk_font_dialog_choose_font(
326 self.to_glib_none().0,
327 parent.map(|p| p.as_ref()).to_glib_none().0,
328 mut_override(initial_value.to_glib_none().0),
329 cancellable.map(|p| p.as_ref()).to_glib_none().0,
330 Some(callback),
331 Box_::into_raw(user_data) as *mut _,
332 );
333 }
334 }
335
336 pub fn choose_font_future(
337 &self,
338 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
339 initial_value: Option<&pango::FontDescription>,
340 ) -> Pin<
341 Box_<
342 dyn std::future::Future<Output = Result<pango::FontDescription, glib::Error>> + 'static,
343 >,
344 > {
345 let parent = parent.map(ToOwned::to_owned);
346 let initial_value = initial_value.map(ToOwned::to_owned);
347 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
348 obj.choose_font(
349 parent.as_ref().map(::std::borrow::Borrow::borrow),
350 initial_value.as_ref().map(::std::borrow::Borrow::borrow),
351 Some(cancellable),
352 move |res| {
353 send.resolve(res);
354 },
355 );
356 }))
357 }
358
359 #[doc(alias = "gtk_font_dialog_get_filter")]
366 #[doc(alias = "get_filter")]
367 pub fn filter(&self) -> Option<Filter> {
368 unsafe { from_glib_none(ffi::gtk_font_dialog_get_filter(self.to_glib_none().0)) }
369 }
370
371 #[doc(alias = "gtk_font_dialog_get_font_map")]
378 #[doc(alias = "get_font_map")]
379 #[doc(alias = "font-map")]
380 pub fn font_map(&self) -> Option<pango::FontMap> {
381 unsafe { from_glib_none(ffi::gtk_font_dialog_get_font_map(self.to_glib_none().0)) }
382 }
383
384 #[doc(alias = "gtk_font_dialog_get_language")]
390 #[doc(alias = "get_language")]
391 pub fn language(&self) -> Option<pango::Language> {
392 unsafe { from_glib_full(ffi::gtk_font_dialog_get_language(self.to_glib_none().0)) }
393 }
394
395 #[doc(alias = "gtk_font_dialog_get_modal")]
402 #[doc(alias = "get_modal")]
403 #[doc(alias = "modal")]
404 pub fn is_modal(&self) -> bool {
405 unsafe { from_glib(ffi::gtk_font_dialog_get_modal(self.to_glib_none().0)) }
406 }
407
408 #[doc(alias = "gtk_font_dialog_get_title")]
414 #[doc(alias = "get_title")]
415 pub fn title(&self) -> glib::GString {
416 unsafe { from_glib_none(ffi::gtk_font_dialog_get_title(self.to_glib_none().0)) }
417 }
418
419 #[doc(alias = "gtk_font_dialog_set_filter")]
427 #[doc(alias = "filter")]
428 pub fn set_filter(&self, filter: Option<&impl IsA<Filter>>) {
429 unsafe {
430 ffi::gtk_font_dialog_set_filter(
431 self.to_glib_none().0,
432 filter.map(|p| p.as_ref()).to_glib_none().0,
433 );
434 }
435 }
436
437 #[doc(alias = "gtk_font_dialog_set_font_map")]
443 #[doc(alias = "font-map")]
444 pub fn set_font_map(&self, fontmap: Option<&impl IsA<pango::FontMap>>) {
445 unsafe {
446 ffi::gtk_font_dialog_set_font_map(
447 self.to_glib_none().0,
448 fontmap.map(|p| p.as_ref()).to_glib_none().0,
449 );
450 }
451 }
452
453 #[doc(alias = "gtk_font_dialog_set_language")]
457 #[doc(alias = "language")]
458 pub fn set_language(&self, language: &pango::Language) {
459 unsafe {
460 ffi::gtk_font_dialog_set_language(
461 self.to_glib_none().0,
462 mut_override(language.to_glib_none().0),
463 );
464 }
465 }
466
467 #[doc(alias = "gtk_font_dialog_set_modal")]
472 #[doc(alias = "modal")]
473 pub fn set_modal(&self, modal: bool) {
474 unsafe {
475 ffi::gtk_font_dialog_set_modal(self.to_glib_none().0, modal.into_glib());
476 }
477 }
478
479 #[doc(alias = "gtk_font_dialog_set_title")]
483 #[doc(alias = "title")]
484 pub fn set_title(&self, title: &str) {
485 unsafe {
486 ffi::gtk_font_dialog_set_title(self.to_glib_none().0, title.to_glib_none().0);
487 }
488 }
489
490 #[cfg(feature = "v4_10")]
491 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
492 #[doc(alias = "filter")]
493 pub fn connect_filter_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
494 unsafe extern "C" fn notify_filter_trampoline<F: Fn(&FontDialog) + 'static>(
495 this: *mut ffi::GtkFontDialog,
496 _param_spec: glib::ffi::gpointer,
497 f: glib::ffi::gpointer,
498 ) {
499 let f: &F = &*(f as *const F);
500 f(&from_glib_borrow(this))
501 }
502 unsafe {
503 let f: Box_<F> = Box_::new(f);
504 connect_raw(
505 self.as_ptr() as *mut _,
506 c"notify::filter".as_ptr() as *const _,
507 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
508 notify_filter_trampoline::<F> as *const (),
509 )),
510 Box_::into_raw(f),
511 )
512 }
513 }
514
515 #[cfg(feature = "v4_10")]
516 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
517 #[doc(alias = "font-map")]
518 pub fn connect_font_map_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
519 unsafe extern "C" fn notify_font_map_trampoline<F: Fn(&FontDialog) + 'static>(
520 this: *mut ffi::GtkFontDialog,
521 _param_spec: glib::ffi::gpointer,
522 f: glib::ffi::gpointer,
523 ) {
524 let f: &F = &*(f as *const F);
525 f(&from_glib_borrow(this))
526 }
527 unsafe {
528 let f: Box_<F> = Box_::new(f);
529 connect_raw(
530 self.as_ptr() as *mut _,
531 c"notify::font-map".as_ptr() as *const _,
532 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
533 notify_font_map_trampoline::<F> as *const (),
534 )),
535 Box_::into_raw(f),
536 )
537 }
538 }
539
540 #[cfg(feature = "v4_10")]
541 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
542 #[doc(alias = "language")]
543 pub fn connect_language_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
544 unsafe extern "C" fn notify_language_trampoline<F: Fn(&FontDialog) + 'static>(
545 this: *mut ffi::GtkFontDialog,
546 _param_spec: glib::ffi::gpointer,
547 f: glib::ffi::gpointer,
548 ) {
549 let f: &F = &*(f as *const F);
550 f(&from_glib_borrow(this))
551 }
552 unsafe {
553 let f: Box_<F> = Box_::new(f);
554 connect_raw(
555 self.as_ptr() as *mut _,
556 c"notify::language".as_ptr() as *const _,
557 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
558 notify_language_trampoline::<F> as *const (),
559 )),
560 Box_::into_raw(f),
561 )
562 }
563 }
564
565 #[cfg(feature = "v4_10")]
566 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
567 #[doc(alias = "modal")]
568 pub fn connect_modal_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
569 unsafe extern "C" fn notify_modal_trampoline<F: Fn(&FontDialog) + 'static>(
570 this: *mut ffi::GtkFontDialog,
571 _param_spec: glib::ffi::gpointer,
572 f: glib::ffi::gpointer,
573 ) {
574 let f: &F = &*(f as *const F);
575 f(&from_glib_borrow(this))
576 }
577 unsafe {
578 let f: Box_<F> = Box_::new(f);
579 connect_raw(
580 self.as_ptr() as *mut _,
581 c"notify::modal".as_ptr() as *const _,
582 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
583 notify_modal_trampoline::<F> as *const (),
584 )),
585 Box_::into_raw(f),
586 )
587 }
588 }
589
590 #[cfg(feature = "v4_10")]
591 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
592 #[doc(alias = "title")]
593 pub fn connect_title_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
594 unsafe extern "C" fn notify_title_trampoline<F: Fn(&FontDialog) + 'static>(
595 this: *mut ffi::GtkFontDialog,
596 _param_spec: glib::ffi::gpointer,
597 f: glib::ffi::gpointer,
598 ) {
599 let f: &F = &*(f as *const F);
600 f(&from_glib_borrow(this))
601 }
602 unsafe {
603 let f: Box_<F> = Box_::new(f);
604 connect_raw(
605 self.as_ptr() as *mut _,
606 c"notify::title".as_ptr() as *const _,
607 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
608 notify_title_trampoline::<F> as *const (),
609 )),
610 Box_::into_raw(f),
611 )
612 }
613 }
614}
615
616#[cfg(feature = "v4_10")]
617#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
618impl Default for FontDialog {
619 fn default() -> Self {
620 Self::new()
621 }
622}
623
624#[must_use = "The builder must be built to be used"]
629pub struct FontDialogBuilder {
630 builder: glib::object::ObjectBuilder<'static, FontDialog>,
631}
632
633impl FontDialogBuilder {
634 fn new() -> Self {
635 Self {
636 builder: glib::object::Object::builder(),
637 }
638 }
639
640 #[cfg(feature = "v4_10")]
642 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
643 pub fn filter(self, filter: &impl IsA<Filter>) -> Self {
644 Self {
645 builder: self.builder.property("filter", filter.clone().upcast()),
646 }
647 }
648
649 #[cfg(feature = "v4_10")]
654 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
655 pub fn font_map(self, font_map: &impl IsA<pango::FontMap>) -> Self {
656 Self {
657 builder: self.builder.property("font-map", font_map.clone().upcast()),
658 }
659 }
660
661 #[cfg(feature = "v4_10")]
663 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
664 pub fn language(self, language: &pango::Language) -> Self {
665 Self {
666 builder: self.builder.property("language", language),
667 }
668 }
669
670 #[cfg(feature = "v4_10")]
672 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
673 pub fn modal(self, modal: bool) -> Self {
674 Self {
675 builder: self.builder.property("modal", modal),
676 }
677 }
678
679 #[cfg(feature = "v4_10")]
682 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
683 pub fn title(self, title: impl Into<glib::GString>) -> Self {
684 Self {
685 builder: self.builder.property("title", title.into()),
686 }
687 }
688
689 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
692 pub fn build(self) -> FontDialog {
693 assert_initialized_main_thread!();
694 self.builder.build()
695 }
696}