summaryrefslogtreecommitdiffstats
path: root/src/gtk_helpers.rs
blob: 964a0cc1a3a6f23ec673481ac941c257859db703 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// -*- coding: utf-8 -*-
//
// Copyright 2021 Michael Buesch <m@bues.ch>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//

pub use gdk::prelude::*;
pub use gdk;
pub use gdk_pixbuf::prelude::*;
pub use gdk_pixbuf;
pub use gio::prelude::*;
pub use glib;
pub use gtk::prelude::*;
pub use gtk;

#[macro_export]
macro_rules! gsigparam {
    ($param:expr, $type:ty) => {
        $param.get::<$type>().unwrap()
    }
}

#[macro_export]
macro_rules! gsignal_connect_to {
    ($instance:ident, $method:ident, $error_return:expr) => {
        Box::new(move |param| {
            match $instance.try_borrow() {
                Ok(inst) => inst.$method(param),
                Err(_) => $error_return,
            }
        })
    }
}

#[macro_export]
macro_rules! gsignal_connect_to_mut {
    ($instance:ident, $method:ident, $error_return:expr) => {
        Box::new(move |param| {
            match $instance.try_borrow_mut() {
                Ok(mut inst) => inst.$method(param),
                Err(_) => $error_return,
            }
        })
    }
}

pub type GSigHandler = Box<dyn Fn(&[glib::Value]) -> Option<glib::Value> + 'static>;

fn prepare_message_dialog(msg: &mut gtk::MessageDialog) {
    // Make the text selectable.
    let area = msg.message_area();
    if let Some(cont) = area.downcast_ref::<gtk::Container>() {
        for child in cont.children() {
            if let Some(label) = child.downcast_ref::<gtk::Label>() {
                label.set_selectable(true);
            }
        }
    }
    // Auto-close the dialog.
    msg.connect_response(|msg, _resp| msg.close());
}

pub fn messagebox_info<T: glib::IsA<gtk::Window>>(parent: Option<&T>,
                                                  text: &str) {
    let mut msg = gtk::MessageDialog::new(parent,
                                          gtk::DialogFlags::MODAL,
                                          gtk::MessageType::Info,
                                          gtk::ButtonsType::Ok,
                                          text);
    prepare_message_dialog(&mut msg);
    msg.run();
}

pub fn messagebox_error<T: glib::IsA<gtk::Window>>(parent: Option<&T>,
                                                   text: &str) {
    let mut msg = gtk::MessageDialog::new(parent,
                                          gtk::DialogFlags::MODAL,
                                          gtk::MessageType::Error,
                                          gtk::ButtonsType::Ok,
                                          text);
    prepare_message_dialog(&mut msg);
    msg.run();
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface