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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::TargetFlags;
use glib::translate::*;
use libc::c_char;
use std::ffi::CStr;

/// A [`TargetEntry`][crate::TargetEntry] represents a single type of
/// data than can be supplied for by a widget for a selection
/// or for supplied or received during drag-and-drop.
#[derive(Clone, Debug)]
#[repr(C)]
#[doc(alias = "GtkTargetEntry")]
pub struct TargetEntry {
    target: String,
    flags: TargetFlags,
    info: u32,
}

impl TargetEntry {
    /// Makes a new [`TargetEntry`][crate::TargetEntry].
    /// ## `target`
    /// String identifier for target
    /// ## `flags`
    /// Set of flags, see [`TargetFlags`][crate::TargetFlags]
    /// ## `info`
    /// an ID that will be passed back to the application
    ///
    /// # Returns
    ///
    /// a pointer to a new [`TargetEntry`][crate::TargetEntry].
    ///  Free with `gtk_target_entry_free()`
    pub fn new(target: &str, flags: TargetFlags, info: u32) -> TargetEntry {
        assert_initialized_main_thread!();
        Self {
            target: target.to_owned(),
            flags,
            info,
        }
    }

    #[doc(alias = "get_target")]
    pub fn target(&self) -> &str {
        &self.target
    }

    #[doc(alias = "get_flags")]
    pub fn flags(&self) -> TargetFlags {
        self.flags
    }

    #[doc(alias = "get_info")]
    pub fn info(&self) -> u32 {
        self.info
    }
}

#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *const ffi::GtkTargetEntry> for TargetEntry {
    type Storage = (Box<ffi::GtkTargetEntry>, Stash<'a, *mut c_char, String>);

    #[inline]
    fn to_glib_none(&'a self) -> Stash<'a, *const ffi::GtkTargetEntry, Self> {
        let target = self.target.to_glib_none();

        let target_entry = Box::new(ffi::GtkTargetEntry {
            target: target.0,
            flags: self.flags.bits(),
            info: self.info,
        });
        Stash(&*target_entry, (target_entry, target))
    }
}

#[doc(hidden)]
impl<'a> ToGlibPtrMut<'a, *mut ffi::GtkTargetEntry> for TargetEntry {
    type Storage = (Box<ffi::GtkTargetEntry>, Stash<'a, *mut c_char, String>);

    #[inline]
    fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::GtkTargetEntry, Self> {
        let target = self.target.to_glib_none();

        let mut target_entry = Box::new(ffi::GtkTargetEntry {
            target: target.0,
            flags: self.flags.bits(),
            info: self.info,
        });
        StashMut(&mut *target_entry, (target_entry, target))
    }
}

#[doc(hidden)]
impl FromGlibPtrNone<*const ffi::GtkTargetEntry> for TargetEntry {
    unsafe fn from_glib_none(ptr: *const ffi::GtkTargetEntry) -> Self {
        Self {
            target: CStr::from_ptr((*ptr).target).to_string_lossy().into_owned(),
            flags: TargetFlags::from_bits((*ptr).flags).unwrap(),
            info: (*ptr).info,
        }
    }
}

#[doc(hidden)]
impl FromGlibPtrNone<*mut ffi::GtkTargetEntry> for TargetEntry {
    unsafe fn from_glib_none(ptr: *mut ffi::GtkTargetEntry) -> Self {
        Self {
            target: CStr::from_ptr((*ptr).target).to_string_lossy().into_owned(),
            flags: TargetFlags::from_bits((*ptr).flags).unwrap(),
            info: (*ptr).info,
        }
    }
}

#[doc(hidden)]
impl FromGlibPtrFull<*mut ffi::GtkTargetEntry> for TargetEntry {
    #[inline]
    unsafe fn from_glib_full(ptr: *mut ffi::GtkTargetEntry) -> Self {
        let target_entry = Self {
            target: CStr::from_ptr((*ptr).target).to_string_lossy().into_owned(),
            flags: TargetFlags::from_bits((*ptr).flags).unwrap(),
            info: (*ptr).info,
        };
        ffi::gtk_target_entry_free(ptr);
        target_entry
    }
}

impl glib::StaticType for TargetEntry {
    fn static_type() -> glib::types::Type {
        skip_assert_initialized!();
        unsafe { from_glib(ffi::gtk_target_entry_get_type()) }
    }
}