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::{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    /// 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            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    /// Returns the button labels for the alert.
185    ///
186    /// # Returns
187    ///
188    /// the button labels
189    #[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    /// Returns the index of the cancel button.
200    ///
201    /// # Returns
202    ///
203    /// the index of the cancel button, or -1
204    #[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    /// Returns the index of the default button.
212    ///
213    /// # Returns
214    ///
215    /// the index of the default button, or -1
216    #[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    /// Returns the detail text that will be shown in the alert.
224    ///
225    /// # Returns
226    ///
227    /// the detail text
228    #[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    /// Returns the message that will be shown in the alert.
235    ///
236    /// # Returns
237    ///
238    /// the message
239    #[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    /// Returns whether the alert blocks interaction
246    /// with the parent window while it is presented.
247    ///
248    /// # Returns
249    ///
250    /// true if the alert is modal
251    #[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    /// Sets the button labels for the alert.
259    /// ## `labels`
260    /// the new button labels
261    #[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    /// Sets the index of the cancel button.
270    ///
271    /// See [`cancel-button`][struct@crate::AlertDialog#cancel-button] for
272    /// details of how this value is used.
273    /// ## `button`
274    /// the new cancel button
275    #[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    /// Sets the index of the default button.
284    ///
285    /// See [`default-button`][struct@crate::AlertDialog#default-button] for
286    /// details of how this value is used.
287    /// ## `button`
288    /// the new default button
289    #[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    /// Sets the detail text that will be shown in the alert.
298    /// ## `detail`
299    /// the new detail text
300    #[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    /// Sets the message that will be shown in the alert.
309    /// ## `message`
310    /// the new message
311    #[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    /// Sets whether the alert blocks interaction
320    /// with the parent window while it is presented.
321    /// ## `modal`
322    /// the new value
323    #[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    /// Shows the alert to the user.
332    ///
333    /// This function is a simpler version of [`choose()`][Self::choose()]
334    /// intended for dialogs with a single button.
335    ///
336    /// If you want to cancel the dialog or if the alert has more than one
337    /// button, you should use that function instead and provide it with a
338    /// [`gio::Cancellable`][crate::gio::Cancellable] and callback respectively.
339    /// ## `parent`
340    /// the parent window
341    #[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// rustdoc-stripper-ignore-next
511/// A [builder-pattern] type to construct [`AlertDialog`] objects.
512///
513/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
514#[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    /// Labels for buttons to show in the alert.
527    ///
528    /// The labels should be translated and may contain
529    /// a `_` character to indicate the mnemonic character.
530    ///
531    /// If this property is not set, then a 'Close' button is
532    /// automatically created.
533    #[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    /// Determines what happens when the <kbd>Escape</kbd> key is pressed
542    /// while the alert is shown.
543    ///
544    /// If this property holds the index of a button in [`buttons`][struct@crate::AlertDialog#buttons],
545    /// then pressing Escape is treated as if that button was pressed. If it is -1
546    /// or not a valid index for the `buttons` array, then an error is returned.
547    ///
548    /// If `buttons` is `NULL`, then the automatically created 'Close' button
549    /// is treated as both cancel and default button, so 0 is returned.
550    #[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    /// Determines what happens when the <kbd>Return</kbd> key is pressed
559    /// while the alert is shown.
560    ///
561    /// If this property holds the index of a button in [`buttons`][struct@crate::AlertDialog#buttons],
562    /// then pressing Return is treated as if that button was pressed. If it is -1
563    /// or not a valid index for the `buttons` array, then nothing happens.
564    ///
565    /// If `buttons` is `NULL`, then the automatically created 'Close' button
566    /// is treated as both cancel and default button, so 0 is returned.
567    #[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    /// The detail text for the alert.
576    #[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    /// The message for the alert.
585    #[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    /// Whether the alert is modal.
594    #[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    // rustdoc-stripper-ignore-next
603    /// Build the [`AlertDialog`].
604    #[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}