// -*- coding: utf-8 -*- // // Copyright 2021 Michael Buesch // // 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; pub use gdk::prelude::*; pub use gdk_pixbuf; pub use gdk_pixbuf::prelude::*; pub use gio::prelude::*; pub use glib; pub use gtk; pub use gtk::prelude::*; use lazy_static::lazy_static; use std::sync::{ atomic::{AtomicBool, Ordering}, Arc, }; lazy_static! { static ref G_RUNNING: Arc = Arc::new(AtomicBool::new(false)); } pub fn g_enter() -> bool { !G_RUNNING.fetch_or(true, Ordering::AcqRel) } pub fn g_exit() { G_RUNNING.store(false, Ordering::Release) } pub type GSigHandler = Box Option + 'static>; #[macro_export] macro_rules! gsigparam { ($param:expr, $type:ty) => { $param.get::<$type>().unwrap() }; } #[macro_export] macro_rules! gsignal_connect_to { ($instance:expr, $method:ident, $error_return:expr) => { Box::new(move |param| { if g_enter() { let ret = $instance.borrow().$method(param); g_exit(); ret } else { $error_return } }) }; } #[macro_export] macro_rules! gsignal_connect_to_mut { ($instance:expr, $method:ident, $error_return:expr) => { Box::new(move |param| { if g_enter() { let ret = $instance.borrow_mut().$method(param); g_exit(); ret } else { $error_return } }) }; } pub fn make_messagebox_text_selectable(msg: &mut gtk::MessageDialog) { let area = msg.message_area(); if let Some(cont) = area.downcast_ref::() { for child in cont.children() { if let Some(label) = child.downcast_ref::() { label.set_selectable(true); } } } } fn run_messagebox(msg: &mut gtk::MessageDialog) { make_messagebox_text_selectable(msg); msg.connect_response(|msg, _resp| msg.close()); msg.run(); } #[allow(dead_code)] pub fn messagebox_info>(parent: Option<&T>, text: &str) { let mut msg = gtk::MessageDialog::new( parent, gtk::DialogFlags::MODAL, gtk::MessageType::Info, gtk::ButtonsType::Ok, text, ); run_messagebox(&mut msg); } #[allow(dead_code)] pub fn messagebox_error>(parent: Option<&T>, text: &str) { let mut msg = gtk::MessageDialog::new( parent, gtk::DialogFlags::MODAL, gtk::MessageType::Error, gtk::ButtonsType::Ok, text, ); run_messagebox(&mut msg); } // vim: ts=4 sw=4 expandtab