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