Skip to main content

gtk4/auto/
alert_dialog.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use 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    /// Collects the arguments that are needed to present a message to the user.
15    ///
16    /// The message is shown with the [`choose()`][Self::choose()]
17    /// function.
18    ///
19    /// If you don't need to wait for a button to be clicked, you can use
20    /// [`show()`][Self::show()].
21    ///
22    /// ## Properties
23    ///
24    ///
25    /// #### `buttons`
26    ///  Labels for buttons to show in the alert.
27    ///
28    /// The labels should be translated and may contain
29    /// a `_` character to indicate the mnemonic character.
30    ///
31    /// If this property is not set, then a 'Close' button is
32    /// automatically created.
33    ///
34    /// Readable | Writeable
35    ///
36    ///
37    /// #### `cancel-button`
38    ///  Determines what happens when the <kbd>Escape</kbd> key is pressed
39    /// while the alert is shown.
40    ///
41    /// If this property holds the index of a button in [`buttons`][struct@crate::AlertDialog#buttons],
42    /// then pressing Escape is treated as if that button was pressed. If it is -1
43    /// or not a valid index for the `buttons` array, then an error is returned.
44    ///
45    /// If `buttons` is `NULL`, then the automatically created 'Close' button
46    /// is treated as both cancel and default button, so 0 is returned.
47    ///
48    /// Readable | Writeable
49    ///
50    ///
51    /// #### `default-button`
52    ///  Determines what happens when the <kbd>Return</kbd> key is pressed
53    /// while the alert is shown.
54    ///
55    /// If this property holds the index of a button in [`buttons`][struct@crate::AlertDialog#buttons],
56    /// then pressing Return is treated as if that button was pressed. If it is -1
57    /// or not a valid index for the `buttons` array, then nothing happens.
58    ///
59    /// If `buttons` is `NULL`, then the automatically created 'Close' button
60    /// is treated as both cancel and default button, so 0 is returned.
61    ///
62    /// Readable | Writeable
63    ///
64    ///
65    /// #### `detail`
66    ///  The detail text for the alert.
67    ///
68    /// Readable | Writeable
69    ///
70    ///
71    /// #### `message`
72    ///  The message for the alert.
73    ///
74    /// Readable | Writeable
75    ///
76    ///
77    /// #### `modal`
78    ///  Whether the alert is modal.
79    ///
80    /// Readable | Writeable
81    ///
82    /// # Implements
83    ///
84    /// [`trait@glib::ObjectExt`]
85    #[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    //#[doc(alias = "gtk_alert_dialog_new")]
95    //pub fn new(format: &str, : /*Unknown conversion*//*Unimplemented*/Basic: VarArgs) -> AlertDialog {
96    //    unsafe { TODO: call ffi:gtk_alert_dialog_new() }
97    //}
98
99    // rustdoc-stripper-ignore-next
100    /// Creates a new builder-pattern struct instance to construct [`AlertDialog`] objects.
101    ///
102    /// This method returns an instance of [`AlertDialogBuilder`](crate::builders::AlertDialogBuilder) which can be used to create [`AlertDialog`] objects.
103    pub fn builder() -> AlertDialogBuilder {
104        AlertDialogBuilder::new()
105    }
106
107    /// Shows the alert to the user.
108    ///
109    /// It is ok to pass `NULL` for the callback if the alert
110    /// does not have more than one button. A simpler API for
111    /// this case is [`show()`][Self::show()].
112    /// ## `parent`
113    /// the parent window
114    /// ## `cancellable`
115    /// a cancellable to cancel the operation
116    /// ## `callback`
117    /// a callback to call
118    ///   when the operation is complete
119    #[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    /// Returns the button labels for the alert.
187    ///
188    /// # Returns
189    ///
190    /// the button labels
191    #[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    /// Returns the index of the cancel button.
202    ///
203    /// # Returns
204    ///
205    /// the index of the cancel button, or -1
206    #[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    /// Returns the index of the default button.
214    ///
215    /// # Returns
216    ///
217    /// the index of the default button, or -1
218    #[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    /// Returns the detail text that will be shown in the alert.
226    ///
227    /// # Returns
228    ///
229    /// the detail text
230    #[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    /// Returns the message that will be shown in the alert.
237    ///
238    /// # Returns
239    ///
240    /// the message
241    #[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    /// Returns whether the alert blocks interaction
248    /// with the parent window while it is presented.
249    ///
250    /// # Returns
251    ///
252    /// true if the alert is modal
253    #[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    /// Sets the button labels for the alert.
261    /// ## `labels`
262    /// the new button labels
263    #[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    /// Sets the index of the cancel button.
272    ///
273    /// See [`cancel-button`][struct@crate::AlertDialog#cancel-button] for
274    /// details of how this value is used.
275    /// ## `button`
276    /// the new cancel button
277    #[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    /// Sets the index of the default button.
286    ///
287    /// See [`default-button`][struct@crate::AlertDialog#default-button] for
288    /// details of how this value is used.
289    /// ## `button`
290    /// the new default button
291    #[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    /// Sets the detail text that will be shown in the alert.
300    /// ## `detail`
301    /// the new detail text
302    #[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    /// Sets the message that will be shown in the alert.
311    /// ## `message`
312    /// the new message
313    #[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    /// Sets whether the alert blocks interaction
322    /// with the parent window while it is presented.
323    /// ## `modal`
324    /// the new value
325    #[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    /// Shows the alert to the user.
334    ///
335    /// This function is a simpler version of [`choose()`][Self::choose()]
336    /// intended for dialogs with a single button.
337    ///
338    /// If you want to cancel the dialog or if the alert has more than one
339    /// button, you should use that function instead and provide it with a
340    /// [`gio::Cancellable`][crate::gio::Cancellable] and callback respectively.
341    /// ## `parent`
342    /// the parent window
343    #[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// rustdoc-stripper-ignore-next
525/// A [builder-pattern] type to construct [`AlertDialog`] objects.
526///
527/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
528#[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    /// Labels for buttons to show in the alert.
541    ///
542    /// The labels should be translated and may contain
543    /// a `_` character to indicate the mnemonic character.
544    ///
545    /// If this property is not set, then a 'Close' button is
546    /// automatically created.
547    #[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    /// Determines what happens when the <kbd>Escape</kbd> key is pressed
556    /// while the alert is shown.
557    ///
558    /// If this property holds the index of a button in [`buttons`][struct@crate::AlertDialog#buttons],
559    /// then pressing Escape is treated as if that button was pressed. If it is -1
560    /// or not a valid index for the `buttons` array, then an error is returned.
561    ///
562    /// If `buttons` is `NULL`, then the automatically created 'Close' button
563    /// is treated as both cancel and default button, so 0 is returned.
564    #[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    /// Determines what happens when the <kbd>Return</kbd> key is pressed
573    /// while the alert is shown.
574    ///
575    /// If this property holds the index of a button in [`buttons`][struct@crate::AlertDialog#buttons],
576    /// then pressing Return is treated as if that button was pressed. If it is -1
577    /// or not a valid index for the `buttons` array, then nothing happens.
578    ///
579    /// If `buttons` is `NULL`, then the automatically created 'Close' button
580    /// is treated as both cancel and default button, so 0 is returned.
581    #[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    /// The detail text for the alert.
590    #[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    /// The message for the alert.
599    #[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    /// Whether the alert is modal.
608    #[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    // rustdoc-stripper-ignore-next
617    /// Build the [`AlertDialog`].
618    #[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}