gtk4/
pad_action_entry.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::ffi::CStr;
4
5use glib::translate::*;
6
7use crate::{ffi, PadActionType};
8
9glib::wrapper! {
10    /// Struct defining a pad action entry.
11    #[doc(alias = "GtkPadActionEntry")]
12    pub struct PadActionEntry(BoxedInline<ffi::GtkPadActionEntry>);
13
14    match fn {
15        init => |ptr| init_action_entry(ptr),
16        copy_into => |dest, src| copy_into_action_entry(dest, src),
17        clear => |ptr| clear_action_entry(ptr),
18    }
19}
20
21impl PadActionEntry {
22    #[inline]
23    pub fn new(
24        type_: PadActionType,
25        index: i32,
26        mode: i32,
27        label: &str,
28        action_name: &str,
29    ) -> Self {
30        assert_initialized_main_thread!();
31        unsafe {
32            Self::unsafe_from(ffi::GtkPadActionEntry {
33                type_: type_.into_glib(),
34                index,
35                mode,
36                label: label.to_glib_full(),
37                action_name: action_name.to_glib_full(),
38            })
39        }
40    }
41
42    #[inline]
43    pub fn type_(&self) -> PadActionType {
44        unsafe { from_glib(self.inner.type_) }
45    }
46
47    #[inline]
48    pub fn index(&self) -> i32 {
49        self.inner.index
50    }
51
52    #[inline]
53    pub fn mode(&self) -> i32 {
54        self.inner.mode
55    }
56
57    #[inline]
58    pub fn label(&self) -> &str {
59        unsafe { CStr::from_ptr(self.inner.label).to_str().unwrap() }
60    }
61
62    #[inline]
63    pub fn action_name(&self) -> &str {
64        unsafe { CStr::from_ptr(self.inner.action_name).to_str().unwrap() }
65    }
66}
67unsafe fn init_action_entry(action_entry: *mut ffi::GtkPadActionEntry) {
68    std::ptr::write(action_entry, std::mem::zeroed());
69}
70
71unsafe fn copy_into_action_entry(
72    dest: *mut ffi::GtkPadActionEntry,
73    src: *const ffi::GtkPadActionEntry,
74) {
75    init_action_entry(dest);
76    (*dest).action_name = glib::ffi::g_strdup((*src).action_name);
77    (*dest).label = glib::ffi::g_strdup((*src).label);
78    (*dest).type_ = (*src).type_;
79    (*dest).index = (*src).index;
80    (*dest).mode = (*src).mode;
81}
82
83unsafe fn clear_action_entry(action_entry: *mut ffi::GtkPadActionEntry) {
84    glib::ffi::g_free((*action_entry).label as *mut _);
85    glib::ffi::g_free((*action_entry).action_name as *mut _);
86}