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, ffi};
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        unsafe {
96            Self {
97                target: CStr::from_ptr((*ptr).target).to_string_lossy().into_owned(),
98                flags: TargetFlags::from_bits((*ptr).flags).unwrap(),
99                info: (*ptr).info,
100            }
101        }
102    }
103}
104
105#[doc(hidden)]
106impl FromGlibPtrNone<*mut ffi::GtkTargetEntry> for TargetEntry {
107    unsafe fn from_glib_none(ptr: *mut ffi::GtkTargetEntry) -> Self {
108        unsafe {
109            Self {
110                target: CStr::from_ptr((*ptr).target).to_string_lossy().into_owned(),
111                flags: TargetFlags::from_bits((*ptr).flags).unwrap(),
112                info: (*ptr).info,
113            }
114        }
115    }
116}
117
118#[doc(hidden)]
119impl FromGlibPtrFull<*mut ffi::GtkTargetEntry> for TargetEntry {
120    #[inline]
121    unsafe fn from_glib_full(ptr: *mut ffi::GtkTargetEntry) -> Self {
122        unsafe {
123            let target_entry = Self {
124                target: CStr::from_ptr((*ptr).target).to_string_lossy().into_owned(),
125                flags: TargetFlags::from_bits((*ptr).flags).unwrap(),
126                info: (*ptr).info,
127            };
128            ffi::gtk_target_entry_free(ptr);
129            target_entry
130        }
131    }
132}
133
134impl glib::types::StaticType for TargetEntry {
135    fn static_type() -> glib::types::Type {
136        skip_assert_initialized!();
137        unsafe { from_glib(ffi::gtk_target_entry_get_type()) }
138    }
139}