1use crate::{ffi, 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 = "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 let mut error = std::ptr::null_mut();
144 let ret =
145 ffi::gtk_alert_dialog_choose_finish(_source_object as *mut _, res, &mut error);
146 let result = if error.is_null() {
147 Ok(ret)
148 } else {
149 Err(from_glib_full(error))
150 };
151 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
152 Box_::from_raw(user_data as *mut _);
153 let callback: P = callback.into_inner();
154 callback(result);
155 }
156 let callback = choose_trampoline::<P>;
157 unsafe {
158 ffi::gtk_alert_dialog_choose(
159 self.to_glib_none().0,
160 parent.map(|p| p.as_ref()).to_glib_none().0,
161 cancellable.map(|p| p.as_ref()).to_glib_none().0,
162 Some(callback),
163 Box_::into_raw(user_data) as *mut _,
164 );
165 }
166 }
167
168 pub fn choose_future(
169 &self,
170 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
171 ) -> Pin<Box_<dyn std::future::Future<Output = Result<i32, glib::Error>> + 'static>> {
172 let parent = parent.map(ToOwned::to_owned);
173 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
174 obj.choose(
175 parent.as_ref().map(::std::borrow::Borrow::borrow),
176 Some(cancellable),
177 move |res| {
178 send.resolve(res);
179 },
180 );
181 }))
182 }
183
184 #[doc(alias = "gtk_alert_dialog_get_buttons")]
190 #[doc(alias = "get_buttons")]
191 pub fn buttons(&self) -> Vec<glib::GString> {
192 unsafe {
193 FromGlibPtrContainer::from_glib_none(ffi::gtk_alert_dialog_get_buttons(
194 self.to_glib_none().0,
195 ))
196 }
197 }
198
199 #[doc(alias = "gtk_alert_dialog_get_cancel_button")]
205 #[doc(alias = "get_cancel_button")]
206 #[doc(alias = "cancel-button")]
207 pub fn cancel_button(&self) -> i32 {
208 unsafe { ffi::gtk_alert_dialog_get_cancel_button(self.to_glib_none().0) }
209 }
210
211 #[doc(alias = "gtk_alert_dialog_get_default_button")]
217 #[doc(alias = "get_default_button")]
218 #[doc(alias = "default-button")]
219 pub fn default_button(&self) -> i32 {
220 unsafe { ffi::gtk_alert_dialog_get_default_button(self.to_glib_none().0) }
221 }
222
223 #[doc(alias = "gtk_alert_dialog_get_detail")]
229 #[doc(alias = "get_detail")]
230 pub fn detail(&self) -> glib::GString {
231 unsafe { from_glib_none(ffi::gtk_alert_dialog_get_detail(self.to_glib_none().0)) }
232 }
233
234 #[doc(alias = "gtk_alert_dialog_get_message")]
240 #[doc(alias = "get_message")]
241 pub fn message(&self) -> glib::GString {
242 unsafe { from_glib_none(ffi::gtk_alert_dialog_get_message(self.to_glib_none().0)) }
243 }
244
245 #[doc(alias = "gtk_alert_dialog_get_modal")]
252 #[doc(alias = "get_modal")]
253 #[doc(alias = "modal")]
254 pub fn is_modal(&self) -> bool {
255 unsafe { from_glib(ffi::gtk_alert_dialog_get_modal(self.to_glib_none().0)) }
256 }
257
258 #[doc(alias = "gtk_alert_dialog_set_buttons")]
262 #[doc(alias = "buttons")]
263 pub fn set_buttons(&self, labels: &[&str]) {
264 unsafe {
265 ffi::gtk_alert_dialog_set_buttons(self.to_glib_none().0, labels.to_glib_none().0);
266 }
267 }
268
269 #[doc(alias = "gtk_alert_dialog_set_cancel_button")]
276 #[doc(alias = "cancel-button")]
277 pub fn set_cancel_button(&self, button: i32) {
278 unsafe {
279 ffi::gtk_alert_dialog_set_cancel_button(self.to_glib_none().0, button);
280 }
281 }
282
283 #[doc(alias = "gtk_alert_dialog_set_default_button")]
290 #[doc(alias = "default-button")]
291 pub fn set_default_button(&self, button: i32) {
292 unsafe {
293 ffi::gtk_alert_dialog_set_default_button(self.to_glib_none().0, button);
294 }
295 }
296
297 #[doc(alias = "gtk_alert_dialog_set_detail")]
301 #[doc(alias = "detail")]
302 pub fn set_detail(&self, detail: &str) {
303 unsafe {
304 ffi::gtk_alert_dialog_set_detail(self.to_glib_none().0, detail.to_glib_none().0);
305 }
306 }
307
308 #[doc(alias = "gtk_alert_dialog_set_message")]
312 #[doc(alias = "message")]
313 pub fn set_message(&self, message: &str) {
314 unsafe {
315 ffi::gtk_alert_dialog_set_message(self.to_glib_none().0, message.to_glib_none().0);
316 }
317 }
318
319 #[doc(alias = "gtk_alert_dialog_set_modal")]
324 #[doc(alias = "modal")]
325 pub fn set_modal(&self, modal: bool) {
326 unsafe {
327 ffi::gtk_alert_dialog_set_modal(self.to_glib_none().0, modal.into_glib());
328 }
329 }
330
331 #[doc(alias = "gtk_alert_dialog_show")]
342 pub fn show(&self, parent: Option<&impl IsA<Window>>) {
343 unsafe {
344 ffi::gtk_alert_dialog_show(
345 self.to_glib_none().0,
346 parent.map(|p| p.as_ref()).to_glib_none().0,
347 );
348 }
349 }
350
351 #[cfg(feature = "v4_10")]
352 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
353 #[doc(alias = "buttons")]
354 pub fn connect_buttons_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
355 unsafe extern "C" fn notify_buttons_trampoline<F: Fn(&AlertDialog) + 'static>(
356 this: *mut ffi::GtkAlertDialog,
357 _param_spec: glib::ffi::gpointer,
358 f: glib::ffi::gpointer,
359 ) {
360 let f: &F = &*(f as *const F);
361 f(&from_glib_borrow(this))
362 }
363 unsafe {
364 let f: Box_<F> = Box_::new(f);
365 connect_raw(
366 self.as_ptr() as *mut _,
367 b"notify::buttons\0".as_ptr() as *const _,
368 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
369 notify_buttons_trampoline::<F> as *const (),
370 )),
371 Box_::into_raw(f),
372 )
373 }
374 }
375
376 #[cfg(feature = "v4_10")]
377 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
378 #[doc(alias = "cancel-button")]
379 pub fn connect_cancel_button_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
380 unsafe extern "C" fn notify_cancel_button_trampoline<F: Fn(&AlertDialog) + 'static>(
381 this: *mut ffi::GtkAlertDialog,
382 _param_spec: glib::ffi::gpointer,
383 f: glib::ffi::gpointer,
384 ) {
385 let f: &F = &*(f as *const F);
386 f(&from_glib_borrow(this))
387 }
388 unsafe {
389 let f: Box_<F> = Box_::new(f);
390 connect_raw(
391 self.as_ptr() as *mut _,
392 b"notify::cancel-button\0".as_ptr() as *const _,
393 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
394 notify_cancel_button_trampoline::<F> as *const (),
395 )),
396 Box_::into_raw(f),
397 )
398 }
399 }
400
401 #[cfg(feature = "v4_10")]
402 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
403 #[doc(alias = "default-button")]
404 pub fn connect_default_button_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
405 unsafe extern "C" fn notify_default_button_trampoline<F: Fn(&AlertDialog) + 'static>(
406 this: *mut ffi::GtkAlertDialog,
407 _param_spec: glib::ffi::gpointer,
408 f: glib::ffi::gpointer,
409 ) {
410 let f: &F = &*(f as *const F);
411 f(&from_glib_borrow(this))
412 }
413 unsafe {
414 let f: Box_<F> = Box_::new(f);
415 connect_raw(
416 self.as_ptr() as *mut _,
417 b"notify::default-button\0".as_ptr() as *const _,
418 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
419 notify_default_button_trampoline::<F> as *const (),
420 )),
421 Box_::into_raw(f),
422 )
423 }
424 }
425
426 #[cfg(feature = "v4_10")]
427 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
428 #[doc(alias = "detail")]
429 pub fn connect_detail_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
430 unsafe extern "C" fn notify_detail_trampoline<F: Fn(&AlertDialog) + 'static>(
431 this: *mut ffi::GtkAlertDialog,
432 _param_spec: glib::ffi::gpointer,
433 f: glib::ffi::gpointer,
434 ) {
435 let f: &F = &*(f as *const F);
436 f(&from_glib_borrow(this))
437 }
438 unsafe {
439 let f: Box_<F> = Box_::new(f);
440 connect_raw(
441 self.as_ptr() as *mut _,
442 b"notify::detail\0".as_ptr() as *const _,
443 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
444 notify_detail_trampoline::<F> as *const (),
445 )),
446 Box_::into_raw(f),
447 )
448 }
449 }
450
451 #[cfg(feature = "v4_10")]
452 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
453 #[doc(alias = "message")]
454 pub fn connect_message_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
455 unsafe extern "C" fn notify_message_trampoline<F: Fn(&AlertDialog) + 'static>(
456 this: *mut ffi::GtkAlertDialog,
457 _param_spec: glib::ffi::gpointer,
458 f: glib::ffi::gpointer,
459 ) {
460 let f: &F = &*(f as *const F);
461 f(&from_glib_borrow(this))
462 }
463 unsafe {
464 let f: Box_<F> = Box_::new(f);
465 connect_raw(
466 self.as_ptr() as *mut _,
467 b"notify::message\0".as_ptr() as *const _,
468 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
469 notify_message_trampoline::<F> as *const (),
470 )),
471 Box_::into_raw(f),
472 )
473 }
474 }
475
476 #[cfg(feature = "v4_10")]
477 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
478 #[doc(alias = "modal")]
479 pub fn connect_modal_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
480 unsafe extern "C" fn notify_modal_trampoline<F: Fn(&AlertDialog) + 'static>(
481 this: *mut ffi::GtkAlertDialog,
482 _param_spec: glib::ffi::gpointer,
483 f: glib::ffi::gpointer,
484 ) {
485 let f: &F = &*(f as *const F);
486 f(&from_glib_borrow(this))
487 }
488 unsafe {
489 let f: Box_<F> = Box_::new(f);
490 connect_raw(
491 self.as_ptr() as *mut _,
492 b"notify::modal\0".as_ptr() as *const _,
493 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
494 notify_modal_trampoline::<F> as *const (),
495 )),
496 Box_::into_raw(f),
497 )
498 }
499 }
500}
501
502#[cfg(feature = "v4_10")]
503#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
504impl Default for AlertDialog {
505 fn default() -> Self {
506 glib::object::Object::new::<Self>()
507 }
508}
509
510#[must_use = "The builder must be built to be used"]
515pub struct AlertDialogBuilder {
516 builder: glib::object::ObjectBuilder<'static, AlertDialog>,
517}
518
519impl AlertDialogBuilder {
520 fn new() -> Self {
521 Self {
522 builder: glib::object::Object::builder(),
523 }
524 }
525
526 #[cfg(feature = "v4_10")]
534 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
535 pub fn buttons(self, buttons: impl Into<glib::StrV>) -> Self {
536 Self {
537 builder: self.builder.property("buttons", buttons.into()),
538 }
539 }
540
541 #[cfg(feature = "v4_10")]
551 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
552 pub fn cancel_button(self, cancel_button: i32) -> Self {
553 Self {
554 builder: self.builder.property("cancel-button", cancel_button),
555 }
556 }
557
558 #[cfg(feature = "v4_10")]
568 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
569 pub fn default_button(self, default_button: i32) -> Self {
570 Self {
571 builder: self.builder.property("default-button", default_button),
572 }
573 }
574
575 #[cfg(feature = "v4_10")]
577 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
578 pub fn detail(self, detail: impl Into<glib::GString>) -> Self {
579 Self {
580 builder: self.builder.property("detail", detail.into()),
581 }
582 }
583
584 #[cfg(feature = "v4_10")]
586 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
587 pub fn message(self, message: impl Into<glib::GString>) -> Self {
588 Self {
589 builder: self.builder.property("message", message.into()),
590 }
591 }
592
593 #[cfg(feature = "v4_10")]
595 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
596 pub fn modal(self, modal: bool) -> Self {
597 Self {
598 builder: self.builder.property("modal", modal),
599 }
600 }
601
602 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
605 pub fn build(self) -> AlertDialog {
606 assert_initialized_main_thread!();
607 self.builder.build()
608 }
609}