Skip to main content

gtk/
message_dialog.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::DialogFlags;
4use crate::MessageDialog;
5use crate::MessageType;
6use crate::Widget;
7use crate::Window;
8use crate::{ButtonsType, ffi};
9use glib::object::{Cast, IsA};
10use glib::translate::*;
11use libc::c_char;
12use std::ptr;
13
14impl MessageDialog {
15    /// Creates a new message dialog, which is a simple dialog with some text
16    /// the user may want to see. When the user clicks a button a “response”
17    /// signal is emitted with response IDs from [`ResponseType`][crate::ResponseType]. See
18    /// [`Dialog`][crate::Dialog] for more details.
19    /// ## `parent`
20    /// transient parent, or [`None`] for none
21    /// ## `flags`
22    /// flags
23    /// ## `type_`
24    /// type of message
25    /// ## `buttons`
26    /// set of buttons to use
27    /// ## `message_format`
28    /// `printf()`-style format string, or [`None`]
29    ///
30    /// # Returns
31    ///
32    /// a new [`MessageDialog`][crate::MessageDialog]
33    #[doc(alias = "gtk_message_dialog_new")]
34    pub fn new<T: IsA<Window>>(
35        parent: Option<&T>,
36        flags: DialogFlags,
37        type_: MessageType,
38        buttons: ButtonsType,
39        message: &str,
40    ) -> MessageDialog {
41        assert_initialized_main_thread!();
42        unsafe {
43            let message: Stash<*const c_char, _> = message.to_glib_none();
44            Widget::from_glib_none(ffi::gtk_message_dialog_new(
45                parent.map(|p| p.as_ref()).to_glib_none().0,
46                flags.into_glib(),
47                type_.into_glib(),
48                buttons.into_glib(),
49                c"%s".as_ptr() as *const c_char,
50                message.0,
51                ptr::null::<c_char>(),
52            ))
53            .unsafe_cast()
54        }
55    }
56
57    /// Creates a new message dialog, which is a simple dialog with some text that
58    /// is marked up with the [Pango text markup language][PangoMarkupFormat].
59    /// When the user clicks a button a “response” signal is emitted with
60    /// response IDs from [`ResponseType`][crate::ResponseType]. See [`Dialog`][crate::Dialog] for more details.
61    ///
62    /// Special XML characters in the `printf()` arguments passed to this
63    /// function will automatically be escaped as necessary.
64    /// (See `g_markup_printf_escaped()` for how this is implemented.)
65    /// Usually this is what you want, but if you have an existing
66    /// Pango markup string that you want to use literally as the
67    /// label, then you need to use [`MessageDialogExt::set_markup()`][crate::prelude::MessageDialogExt::set_markup()]
68    /// instead, since you can’t pass the markup string either
69    /// as the format (it might contain “%” characters) or as a string
70    /// argument.
71    ///
72    ///
73    /// **⚠️ The following code is in C ⚠️**
74    ///
75    /// ```C
76    ///  GtkWidget *dialog;
77    ///  GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT;
78    ///  dialog = gtk_message_dialog_new (parent_window,
79    ///                                   flags,
80    ///                                   GTK_MESSAGE_ERROR,
81    ///                                   GTK_BUTTONS_CLOSE,
82    ///                                   NULL);
83    ///  gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog),
84    ///                                 markup);
85    /// ```
86    /// ## `parent`
87    /// transient parent, or [`None`] for none
88    /// ## `flags`
89    /// flags
90    /// ## `type_`
91    /// type of message
92    /// ## `buttons`
93    /// set of buttons to use
94    /// ## `message_format`
95    /// `printf()`-style format string, or [`None`]
96    ///
97    /// # Returns
98    ///
99    /// a new [`MessageDialog`][crate::MessageDialog]
100    #[doc(alias = "gtk_message_dialog_new_with_markup")]
101    #[doc(alias = "new_with_markup")]
102    pub fn with_markup<T: IsA<Window>>(
103        parent: Option<&T>,
104        flags: DialogFlags,
105        type_: MessageType,
106        buttons: ButtonsType,
107        message: Option<&str>,
108    ) -> MessageDialog {
109        assert_initialized_main_thread!();
110        unsafe {
111            Widget::from_glib_none(ffi::gtk_message_dialog_new_with_markup(
112                parent.map(|p| p.as_ref()).to_glib_none().0,
113                flags.into_glib(),
114                type_.into_glib(),
115                buttons.into_glib(),
116                message.to_glib_none().0,
117            ))
118            .unsafe_cast()
119        }
120    }
121}
122
123impl Default for MessageDialog {
124    fn default() -> Self {
125        assert_initialized_main_thread!();
126        glib::Object::new()
127    }
128}
129
130pub trait MessageDialogExtManual: IsA<MessageDialog> + 'static {
131    /// Sets the secondary text of the message dialog to be `message_format` (with
132    /// `printf()`-style), which is marked up with the
133    /// [Pango text markup language][PangoMarkupFormat].
134    ///
135    /// Due to an oversight, this function does not escape special XML characters
136    /// like [`MessageDialog::with_markup()`][crate::MessageDialog::with_markup()] does. Thus, if the arguments
137    /// may contain special XML characters, you should use `g_markup_printf_escaped()`
138    /// to escape it.
139    ///
140    ///
141    ///
142    /// **⚠️ The following code is in C ⚠️**
143    ///
144    /// ```C
145    /// gchar *msg;
146    ///
147    /// msg = g_markup_printf_escaped (message_format, ...);
148    /// gtk_message_dialog_format_secondary_markup (message_dialog,
149    ///                                             "%s", msg);
150    /// g_free (msg);
151    /// ```
152    /// ## `message_format`
153    /// `printf()`-style markup string (see
154    ///  [Pango markup format][PangoMarkupFormat]), or [`None`]
155    #[doc(alias = "gtk_message_dialog_format_secondary_markup")]
156    fn format_secondary_markup(&self, message: Option<&str>) {
157        match message {
158            Some(m) => unsafe {
159                let message: Stash<*const c_char, _> = m.to_glib_none();
160                ffi::gtk_message_dialog_format_secondary_markup(
161                    self.as_ref().to_glib_none().0,
162                    c"%s".as_ptr() as *const c_char,
163                    message.0,
164                    ptr::null::<c_char>(),
165                )
166            },
167            None => unsafe {
168                ffi::gtk_message_dialog_format_secondary_markup(
169                    self.as_ref().to_glib_none().0,
170                    ptr::null::<c_char>(),
171                )
172            },
173        }
174    }
175
176    /// Sets the secondary text of the message dialog to be `message_format`
177    /// (with `printf()`-style).
178    /// ## `message_format`
179    /// `printf()`-style format string, or [`None`]
180    #[doc(alias = "gtk_message_dialog_format_secondary_text")]
181    fn format_secondary_text(&self, message: Option<&str>) {
182        match message {
183            Some(m) => unsafe {
184                let message: Stash<*const c_char, _> = m.to_glib_none();
185                ffi::gtk_message_dialog_format_secondary_text(
186                    self.as_ref().to_glib_none().0,
187                    c"%s".as_ptr() as *const c_char,
188                    message.0,
189                    ptr::null::<c_char>(),
190                )
191            },
192            None => unsafe {
193                ffi::gtk_message_dialog_format_secondary_text(
194                    self.as_ref().to_glib_none().0,
195                    ptr::null::<c_char>(),
196                )
197            },
198        }
199    }
200}
201
202impl<O: IsA<MessageDialog>> MessageDialogExtManual for O {}