gtk/
message_dialog.rs

1// Copyright 2013-2016, The Gtk-rs Project Developers.
2// See the COPYRIGHT file at the top-level directory of this distribution.
3// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
4
5use glib::object::{Cast, IsA};
6use glib::translate::*;
7use gtk_sys;
8use libc::c_char;
9use std::ptr;
10use ButtonsType;
11use DialogFlags;
12use MessageDialog;
13use MessageType;
14use Widget;
15use Window;
16
17impl MessageDialog {
18    pub fn new<T: IsA<Window>>(
19        parent: Option<&T>,
20        flags: DialogFlags,
21        type_: MessageType,
22        buttons: ButtonsType,
23        message: &str,
24    ) -> MessageDialog {
25        assert_initialized_main_thread!();
26        unsafe {
27            let message: Stash<*const c_char, _> = message.to_glib_none();
28            Widget::from_glib_none(gtk_sys::gtk_message_dialog_new(
29                parent.map(|p| p.as_ref()).to_glib_none().0,
30                flags.to_glib(),
31                type_.to_glib(),
32                buttons.to_glib(),
33                b"%s\0".as_ptr() as *const c_char,
34                message.0,
35                ptr::null::<c_char>(),
36            ))
37            .unsafe_cast()
38        }
39    }
40}
41
42pub trait MessageDialogExt: 'static {
43    fn set_secondary_markup(&self, message: Option<&str>);
44
45    fn set_secondary_text(&self, message: Option<&str>);
46}
47
48impl<O: IsA<MessageDialog>> MessageDialogExt for O {
49    fn set_secondary_markup(&self, message: Option<&str>) {
50        match message {
51            Some(m) => unsafe {
52                let message: Stash<*const c_char, _> = m.to_glib_none();
53                gtk_sys::gtk_message_dialog_format_secondary_markup(
54                    self.as_ref().to_glib_none().0,
55                    b"%s\0".as_ptr() as *const c_char,
56                    message.0,
57                    ptr::null::<c_char>(),
58                )
59            },
60            None => unsafe {
61                gtk_sys::gtk_message_dialog_format_secondary_markup(
62                    self.as_ref().to_glib_none().0,
63                    ptr::null::<c_char>(),
64                )
65            },
66        }
67    }
68
69    fn set_secondary_text(&self, message: Option<&str>) {
70        match message {
71            Some(m) => unsafe {
72                let message: Stash<*const c_char, _> = m.to_glib_none();
73                gtk_sys::gtk_message_dialog_format_secondary_text(
74                    self.as_ref().to_glib_none().0,
75                    b"%s\0".as_ptr() as *const c_char,
76                    message.0,
77                    ptr::null::<c_char>(),
78                )
79            },
80            None => unsafe {
81                gtk_sys::gtk_message_dialog_format_secondary_text(
82                    self.as_ref().to_glib_none().0,
83                    ptr::null::<c_char>(),
84                )
85            },
86        }
87    }
88}