Skip to main content

gtk/
target_entry.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::TargetFlags;
4use glib::translate::*;
5use libc::c_char;
6use std::ffi::CStr;
7
8/// A [`TargetEntry`][crate::TargetEntry] represents a single type of
9/// data than can be supplied for by a widget for a selection
10/// or for supplied or received during drag-and-drop.
11#[derive(Clone, Debug)]
12#[repr(C)]
13#[doc(alias = "GtkTargetEntry")]
14pub struct TargetEntry {
15    target: String,
16    flags: TargetFlags,
17    info: u32,
18}
19
20impl TargetEntry {
21    /// Makes a new [`TargetEntry`][crate::TargetEntry].
22    /// ## `target`
23    /// String identifier for target
24    /// ## `flags`
25    /// Set of flags, see [`TargetFlags`][crate::TargetFlags]
26    /// ## `info`
27    /// an ID that will be passed back to the application
28    ///
29    /// # Returns
30    ///
31    /// a pointer to a new [`TargetEntry`][crate::TargetEntry].
32    ///  Free with `gtk_target_entry_free()`
33    pub fn new(target: &str, flags: TargetFlags, info: u32) -> TargetEntry {
34        assert_initialized_main_thread!();
35        Self {
36            target: target.to_owned(),
37            flags,
38            info,
39        }
40    }
41
42    #[doc(alias = "get_target")]
43    pub fn target(&self) -> &str {
44        &self.target
45    }
46
47    #[doc(alias = "get_flags")]
48    pub fn flags(&self) -> TargetFlags {
49        self.flags
50    }
51
52    #[doc(alias = "get_info")]
53    pub fn info(&self) -> u32 {
54        self.info
55    }
56}
57
58#[doc(hidden)]
59impl<'a> ToGlibPtr<'a, *const ffi::GtkTargetEntry> for TargetEntry {
60    type Storage = (Box<ffi::GtkTargetEntry>, Stash<'a, *mut c_char, String>);
61
62    #[inline]
63    fn to_glib_none(&'a self) -> Stash<'a, *const ffi::GtkTargetEntry, Self> {
64        let target = self.target.to_glib_none();
65
66        let target_entry = Box::new(ffi::GtkTargetEntry {
67            target: target.0,
68            flags: self.flags.bits(),
69            info: self.info,
70        });
71        Stash(&*target_entry, (target_entry, target))
72    }
73}
74
75#[doc(hidden)]
76impl<'a> ToGlibPtrMut<'a, *mut ffi::GtkTargetEntry> for TargetEntry {
77    type Storage = (Box<ffi::GtkTargetEntry>, Stash<'a, *mut c_char, String>);
78
79    #[inline]
80    fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::GtkTargetEntry, Self> {
81        let target = self.target.to_glib_none();
82
83        let mut target_entry = Box::new(ffi::GtkTargetEntry {
84            target: target.0,
85            flags: self.flags.bits(),
86            info: self.info,
87        });
88        StashMut(&mut *target_entry, (target_entry, target))
89    }
90}
91
92#[doc(hidden)]
93impl FromGlibPtrNone<*const ffi::GtkTargetEntry> for TargetEntry {
94    unsafe fn from_glib_none(ptr: *const ffi::GtkTargetEntry) -> Self {
95        Self {
96            target: CStr::from_ptr((*ptr).target).to_string_lossy().into_owned(),
97            flags: TargetFlags::from_bits((*ptr).flags).unwrap(),
98            info: (*ptr).info,
99        }
100    }
101}
102
103#[doc(hidden)]
104impl FromGlibPtrNone<*mut ffi::GtkTargetEntry> for TargetEntry {
105    unsafe fn from_glib_none(ptr: *mut ffi::GtkTargetEntry) -> Self {
106        Self {
107            target: CStr::from_ptr((*ptr).target).to_string_lossy().into_owned(),
108            flags: TargetFlags::from_bits((*ptr).flags).unwrap(),
109            info: (*ptr).info,
110        }
111    }
112}
113
114#[doc(hidden)]
115impl FromGlibPtrFull<*mut ffi::GtkTargetEntry> for TargetEntry {
116    #[inline]
117    unsafe fn from_glib_full(ptr: *mut ffi::GtkTargetEntry) -> Self {
118        let target_entry = Self {
119            target: CStr::from_ptr((*ptr).target).to_string_lossy().into_owned(),
120            flags: TargetFlags::from_bits((*ptr).flags).unwrap(),
121            info: (*ptr).info,
122        };
123        ffi::gtk_target_entry_free(ptr);
124        target_entry
125    }
126}
127
128impl glib::StaticType for TargetEntry {
129    fn static_type() -> glib::types::Type {
130        skip_assert_initialized!();
131        unsafe { from_glib(ffi::gtk_target_entry_get_type()) }
132    }
133}