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
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::prelude::*;
use crate::{AboutDialog, Window};
use glib::translate::*;
use glib::{IsA, Quark, ToValue};
use once_cell::sync::Lazy;

/// Determines whether a given keyval and modifier mask constitute
/// a valid keyboard accelerator.
///
/// For example, the `GDK_KEY_a` keyval plus [`gdk::ModifierType::CONTROL_MASK`][crate::gdk::ModifierType::CONTROL_MASK] mark is valid,
/// and matches the “Ctrl+a” accelerator. But, you can't, for instance, use
/// the `GDK_KEY_Control_L` keyval as an accelerator.
/// ## `keyval`
/// a GDK keyval
/// ## `modifiers`
/// modifier mask
///
/// # Returns
///
/// [`true`] if the accelerator is valid
#[doc(alias = "gtk_accelerator_valid")]
pub fn accelerator_valid(keyval: gdk::keys::Key, modifiers: gdk::ModifierType) -> bool {
    assert_initialized_main_thread!();
    unsafe {
        from_glib(ffi::gtk_accelerator_valid(
            keyval.into_glib(),
            modifiers.into_glib(),
        ))
    }
}

static SHOW_ABOUT_DIALOG_QUARK: Lazy<Quark> =
    Lazy::new(|| Quark::from_string("gtk-rs-about-dialog"));

/// A convenience function for showing an application’s about dialog.
///
/// The constructed dialog is associated with the parent window and
/// reused for future invocations of this function.
/// ## `parent`
/// the parent top-level window
/// ## `first_property_name`
/// the name of the first property
#[doc(alias = "gtk_show_about_dialog")]
pub fn show_about_dialog<P: IsA<Window>>(parent: Option<&P>, properties: &[(&str, &dyn ToValue)]) {
    assert_initialized_main_thread!();
    unsafe {
        if let Some(d) = parent.and_then(|p| p.qdata::<AboutDialog>(*SHOW_ABOUT_DIALOG_QUARK)) {
            d.as_ref().show();
        } else {
            let about_dialog = glib::Object::new::<AboutDialog>(properties)
                .expect("Failed to crate an about dialog");
            about_dialog.set_transient_for(parent);
            about_dialog.set_modal(true);
            about_dialog.set_destroy_with_parent(true);

            // cache the dialog if a parent is set
            if let Some(dialog_parent) = parent {
                dialog_parent.set_qdata(*SHOW_ABOUT_DIALOG_QUARK, about_dialog.clone());
            }
            about_dialog.show();
        };
    }
}

/// Return the type ids that have been registered after
/// calling [`test_register_all_types()`][crate::test_register_all_types()].
///
/// # Returns
///
///
///  0-terminated array of type ids
#[doc(alias = "gtk_test_list_all_types")]
pub fn test_list_all_types() -> Vec<glib::Type> {
    unsafe {
        let mut n_types = std::mem::MaybeUninit::uninit();
        let types = ffi::gtk_test_list_all_types(n_types.as_mut_ptr());
        FromGlibContainer::from_glib_container_num(types, n_types.assume_init() as usize)
    }
}