[go: up one dir, main page]

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