1use crate::{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 = "GtkAlertDialog")]
86 pub struct AlertDialog(Object<ffi::GtkAlertDialog, ffi::GtkAlertDialogClass>);
87
88 match fn {
89 type_ => || ffi::gtk_alert_dialog_get_type(),
90 }
91}
92
93impl AlertDialog {
94 pub fn builder() -> AlertDialogBuilder {
104 AlertDialogBuilder::new()
105 }
106
107 #[doc(alias = "gtk_alert_dialog_choose")]
120 pub fn choose<P: FnOnce(Result<i32, glib::Error>) + 'static>(
121 &self,
122 parent: Option<&impl IsA<Window>>,
123 cancellable: Option<&impl IsA<gio::Cancellable>>,
124 callback: P,
125 ) {
126 let main_context = glib::MainContext::ref_thread_default();
127 let is_main_context_owner = main_context.is_owner();
128 let has_acquired_main_context = (!is_main_context_owner)
129 .then(|| main_context.acquire().ok())
130 .flatten();
131 assert!(
132 is_main_context_owner || has_acquired_main_context.is_some(),
133 "Async operations only allowed if the thread is owning the MainContext"
134 );
135
136 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
137 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
138 unsafe extern "C" fn choose_trampoline<P: FnOnce(Result<i32, glib::Error>) + 'static>(
139 _source_object: *mut glib::gobject_ffi::GObject,
140 res: *mut gio::ffi::GAsyncResult,
141 user_data: glib::ffi::gpointer,
142 ) {
143 unsafe {
144 let mut error = std::ptr::null_mut();
145 let ret =
146 ffi::gtk_alert_dialog_choose_finish(_source_object as *mut _, res, &mut error);
147 let result = if error.is_null() {
148 Ok(ret)
149 } else {
150 Err(from_glib_full(error))
151 };
152 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
153 Box_::from_raw(user_data as *mut _);
154 let callback: P = callback.into_inner();
155 callback(result);
156 }
157 }
158 let callback = choose_trampoline::<P>;
159 unsafe {
160 ffi::gtk_alert_dialog_choose(
161 self.to_glib_none().0,
162 parent.map(|p| p.as_ref()).to_glib_none().0,
163 cancellable.map(|p| p.as_ref()).to_glib_none().0,
164 Some(callback),
165 Box_::into_raw(user_data) as *mut _,
166 );
167 }
168 }
169
170 pub fn choose_future(
171 &self,
172 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
173 ) -> Pin<Box_<dyn std::future::Future<Output = Result<i32, glib::Error>> + 'static>> {
174 let parent = parent.map(ToOwned::to_owned);
175 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
176 obj.choose(
177 parent.as_ref().map(::std::borrow::Borrow::borrow),
178 Some(cancellable),
179 move |res| {
180 send.resolve(res);
181 },
182 );
183 }))
184 }
185
186 #[doc(alias = "gtk_alert_dialog_get_buttons")]
192 #[doc(alias = "get_buttons")]
193 pub fn buttons(&self) -> Vec<glib::GString> {
194 unsafe {
195 FromGlibPtrContainer::from_glib_none(ffi::gtk_alert_dialog_get_buttons(
196 self.to_glib_none().0,
197 ))
198 }
199 }
200
201 #[doc(alias = "gtk_alert_dialog_get_cancel_button")]
207 #[doc(alias = "get_cancel_button")]
208 #[doc(alias = "cancel-button")]
209 pub fn cancel_button(&self) -> i32 {
210 unsafe { ffi::gtk_alert_dialog_get_cancel_button(self.to_glib_none().0) }
211 }
212
213 #[doc(alias = "gtk_alert_dialog_get_default_button")]
219 #[doc(alias = "get_default_button")]
220 #[doc(alias = "default-button")]
221 pub fn default_button(&self) -> i32 {
222 unsafe { ffi::gtk_alert_dialog_get_default_button(self.to_glib_none().0) }
223 }
224
225 #[doc(alias = "gtk_alert_dialog_get_detail")]
231 #[doc(alias = "get_detail")]
232 pub fn detail(&self) -> glib::GString {
233 unsafe { from_glib_none(ffi::gtk_alert_dialog_get_detail(self.to_glib_none().0)) }
234 }
235
236 #[doc(alias = "gtk_alert_dialog_get_message")]
242 #[doc(alias = "get_message")]
243 pub fn message(&self) -> glib::GString {
244 unsafe { from_glib_none(ffi::gtk_alert_dialog_get_message(self.to_glib_none().0)) }
245 }
246
247 #[doc(alias = "gtk_alert_dialog_get_modal")]
254 #[doc(alias = "get_modal")]
255 #[doc(alias = "modal")]
256 pub fn is_modal(&self) -> bool {
257 unsafe { from_glib(ffi::gtk_alert_dialog_get_modal(self.to_glib_none().0)) }
258 }
259
260 #[doc(alias = "gtk_alert_dialog_set_buttons")]
264 #[doc(alias = "buttons")]
265 pub fn set_buttons(&self, labels: &[&str]) {
266 unsafe {
267 ffi::gtk_alert_dialog_set_buttons(self.to_glib_none().0, labels.to_glib_none().0);
268 }
269 }
270
271 #[doc(alias = "gtk_alert_dialog_set_cancel_button")]
278 #[doc(alias = "cancel-button")]
279 pub fn set_cancel_button(&self, button: i32) {
280 unsafe {
281 ffi::gtk_alert_dialog_set_cancel_button(self.to_glib_none().0, button);
282 }
283 }
284
285 #[doc(alias = "gtk_alert_dialog_set_default_button")]
292 #[doc(alias = "default-button")]
293 pub fn set_default_button(&self, button: i32) {
294 unsafe {
295 ffi::gtk_alert_dialog_set_default_button(self.to_glib_none().0, button);
296 }
297 }
298
299 #[doc(alias = "gtk_alert_dialog_set_detail")]
303 #[doc(alias = "detail")]
304 pub fn set_detail(&self, detail: &str) {
305 unsafe {
306 ffi::gtk_alert_dialog_set_detail(self.to_glib_none().0, detail.to_glib_none().0);
307 }
308 }
309
310 #[doc(alias = "gtk_alert_dialog_set_message")]
314 #[doc(alias = "message")]
315 pub fn set_message(&self, message: &str) {
316 unsafe {
317 ffi::gtk_alert_dialog_set_message(self.to_glib_none().0, message.to_glib_none().0);
318 }
319 }
320
321 #[doc(alias = "gtk_alert_dialog_set_modal")]
326 #[doc(alias = "modal")]
327 pub fn set_modal(&self, modal: bool) {
328 unsafe {
329 ffi::gtk_alert_dialog_set_modal(self.to_glib_none().0, modal.into_glib());
330 }
331 }
332
333 #[doc(alias = "gtk_alert_dialog_show")]
344 pub fn show(&self, parent: Option<&impl IsA<Window>>) {
345 unsafe {
346 ffi::gtk_alert_dialog_show(
347 self.to_glib_none().0,
348 parent.map(|p| p.as_ref()).to_glib_none().0,
349 );
350 }
351 }
352
353 #[cfg(feature = "v4_10")]
354 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
355 #[doc(alias = "buttons")]
356 pub fn connect_buttons_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
357 unsafe extern "C" fn notify_buttons_trampoline<F: Fn(&AlertDialog) + 'static>(
358 this: *mut ffi::GtkAlertDialog,
359 _param_spec: glib::ffi::gpointer,
360 f: glib::ffi::gpointer,
361 ) {
362 unsafe {
363 let f: &F = &*(f as *const F);
364 f(&from_glib_borrow(this))
365 }
366 }
367 unsafe {
368 let f: Box_<F> = Box_::new(f);
369 connect_raw(
370 self.as_ptr() as *mut _,
371 c"notify::buttons".as_ptr() as *const _,
372 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
373 notify_buttons_trampoline::<F> as *const (),
374 )),
375 Box_::into_raw(f),
376 )
377 }
378 }
379
380 #[cfg(feature = "v4_10")]
381 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
382 #[doc(alias = "cancel-button")]
383 pub fn connect_cancel_button_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
384 unsafe extern "C" fn notify_cancel_button_trampoline<F: Fn(&AlertDialog) + 'static>(
385 this: *mut ffi::GtkAlertDialog,
386 _param_spec: glib::ffi::gpointer,
387 f: glib::ffi::gpointer,
388 ) {
389 unsafe {
390 let f: &F = &*(f as *const F);
391 f(&from_glib_borrow(this))
392 }
393 }
394 unsafe {
395 let f: Box_<F> = Box_::new(f);
396 connect_raw(
397 self.as_ptr() as *mut _,
398 c"notify::cancel-button".as_ptr() as *const _,
399 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
400 notify_cancel_button_trampoline::<F> as *const (),
401 )),
402 Box_::into_raw(f),
403 )
404 }
405 }
406
407 #[cfg(feature = "v4_10")]
408 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
409 #[doc(alias = "default-button")]
410 pub fn connect_default_button_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
411 unsafe extern "C" fn notify_default_button_trampoline<F: Fn(&AlertDialog) + 'static>(
412 this: *mut ffi::GtkAlertDialog,
413 _param_spec: glib::ffi::gpointer,
414 f: glib::ffi::gpointer,
415 ) {
416 unsafe {
417 let f: &F = &*(f as *const F);
418 f(&from_glib_borrow(this))
419 }
420 }
421 unsafe {
422 let f: Box_<F> = Box_::new(f);
423 connect_raw(
424 self.as_ptr() as *mut _,
425 c"notify::default-button".as_ptr() as *const _,
426 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
427 notify_default_button_trampoline::<F> as *const (),
428 )),
429 Box_::into_raw(f),
430 )
431 }
432 }
433
434 #[cfg(feature = "v4_10")]
435 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
436 #[doc(alias = "detail")]
437 pub fn connect_detail_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
438 unsafe extern "C" fn notify_detail_trampoline<F: Fn(&AlertDialog) + 'static>(
439 this: *mut ffi::GtkAlertDialog,
440 _param_spec: glib::ffi::gpointer,
441 f: glib::ffi::gpointer,
442 ) {
443 unsafe {
444 let f: &F = &*(f as *const F);
445 f(&from_glib_borrow(this))
446 }
447 }
448 unsafe {
449 let f: Box_<F> = Box_::new(f);
450 connect_raw(
451 self.as_ptr() as *mut _,
452 c"notify::detail".as_ptr() as *const _,
453 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
454 notify_detail_trampoline::<F> as *const (),
455 )),
456 Box_::into_raw(f),
457 )
458 }
459 }
460
461 #[cfg(feature = "v4_10")]
462 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
463 #[doc(alias = "message")]
464 pub fn connect_message_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
465 unsafe extern "C" fn notify_message_trampoline<F: Fn(&AlertDialog) + 'static>(
466 this: *mut ffi::GtkAlertDialog,
467 _param_spec: glib::ffi::gpointer,
468 f: glib::ffi::gpointer,
469 ) {
470 unsafe {
471 let f: &F = &*(f as *const F);
472 f(&from_glib_borrow(this))
473 }
474 }
475 unsafe {
476 let f: Box_<F> = Box_::new(f);
477 connect_raw(
478 self.as_ptr() as *mut _,
479 c"notify::message".as_ptr() as *const _,
480 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
481 notify_message_trampoline::<F> as *const (),
482 )),
483 Box_::into_raw(f),
484 )
485 }
486 }
487
488 #[cfg(feature = "v4_10")]
489 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
490 #[doc(alias = "modal")]
491 pub fn connect_modal_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
492 unsafe extern "C" fn notify_modal_trampoline<F: Fn(&AlertDialog) + 'static>(
493 this: *mut ffi::GtkAlertDialog,
494 _param_spec: glib::ffi::gpointer,
495 f: glib::ffi::gpointer,
496 ) {
497 unsafe {
498 let f: &F = &*(f as *const F);
499 f(&from_glib_borrow(this))
500 }
501 }
502 unsafe {
503 let f: Box_<F> = Box_::new(f);
504 connect_raw(
505 self.as_ptr() as *mut _,
506 c"notify::modal".as_ptr() as *const _,
507 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
508 notify_modal_trampoline::<F> as *const (),
509 )),
510 Box_::into_raw(f),
511 )
512 }
513 }
514}
515
516#[cfg(feature = "v4_10")]
517#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
518impl Default for AlertDialog {
519 fn default() -> Self {
520 glib::object::Object::new::<Self>()
521 }
522}
523
524#[must_use = "The builder must be built to be used"]
529pub struct AlertDialogBuilder {
530 builder: glib::object::ObjectBuilder<'static, AlertDialog>,
531}
532
533impl AlertDialogBuilder {
534 fn new() -> Self {
535 Self {
536 builder: glib::object::Object::builder(),
537 }
538 }
539
540 #[cfg(feature = "v4_10")]
548 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
549 pub fn buttons(self, buttons: impl Into<glib::StrV>) -> Self {
550 Self {
551 builder: self.builder.property("buttons", buttons.into()),
552 }
553 }
554
555 #[cfg(feature = "v4_10")]
565 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
566 pub fn cancel_button(self, cancel_button: i32) -> Self {
567 Self {
568 builder: self.builder.property("cancel-button", cancel_button),
569 }
570 }
571
572 #[cfg(feature = "v4_10")]
582 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
583 pub fn default_button(self, default_button: i32) -> Self {
584 Self {
585 builder: self.builder.property("default-button", default_button),
586 }
587 }
588
589 #[cfg(feature = "v4_10")]
591 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
592 pub fn detail(self, detail: impl Into<glib::GString>) -> Self {
593 Self {
594 builder: self.builder.property("detail", detail.into()),
595 }
596 }
597
598 #[cfg(feature = "v4_10")]
600 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
601 pub fn message(self, message: impl Into<glib::GString>) -> Self {
602 Self {
603 builder: self.builder.property("message", message.into()),
604 }
605 }
606
607 #[cfg(feature = "v4_10")]
609 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
610 pub fn modal(self, modal: bool) -> Self {
611 Self {
612 builder: self.builder.property("modal", modal),
613 }
614 }
615
616 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
619 pub fn build(self) -> AlertDialog {
620 assert_initialized_main_thread!();
621 self.builder.build()
622 }
623}