gdk/
atom.rs

1// Copyright 2013-2015, The Gtk-rs Project Developers.
2// See the COPYRIGHT file at the top-level directory of this distribution.
3// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
4
5use gdk_sys;
6use glib::translate::*;
7use glib::GString;
8use glib_sys;
9use std::mem;
10use std::ptr;
11
12#[derive(Copy, Clone, PartialEq, Eq)]
13pub struct Atom(gdk_sys::GdkAtom);
14
15pub const NONE: Atom = Atom(0 as *mut _);
16pub const SELECTION_PRIMARY: Atom = Atom(1 as *mut _);
17pub const SELECTION_SECONDARY: Atom = Atom(2 as *mut _);
18pub const SELECTION_CLIPBOARD: Atom = Atom(69 as *mut _);
19pub const TARGET_BITMAP: Atom = Atom(5 as *mut _);
20pub const TARGET_COLORMAP: Atom = Atom(7 as *mut _);
21pub const TARGET_DRAWABLE: Atom = Atom(17 as *mut _);
22pub const TARGET_PIXMAP: Atom = Atom(20 as *mut _);
23pub const TARGET_STRING: Atom = Atom(31 as *mut _);
24pub const SELECTION_TYPE_ATOM: Atom = Atom(4 as *mut _);
25pub const SELECTION_TYPE_BITMAP: Atom = Atom(5 as *mut _);
26pub const SELECTION_TYPE_COLORMAP: Atom = Atom(7 as *mut _);
27pub const SELECTION_TYPE_DRAWABLE: Atom = Atom(17 as *mut _);
28pub const SELECTION_TYPE_INTEGER: Atom = Atom(19 as *mut _);
29pub const SELECTION_TYPE_PIXMAP: Atom = Atom(20 as *mut _);
30pub const SELECTION_TYPE_WINDOW: Atom = Atom(33 as *mut _);
31pub const SELECTION_TYPE_STRING: Atom = Atom(31 as *mut _);
32
33impl Atom {
34    pub fn intern(atom_name: &str) -> Atom {
35        assert_initialized_main_thread!();
36        unsafe {
37            Atom(gdk_sys::gdk_atom_intern(
38                atom_name.to_glib_none().0,
39                false.to_glib(),
40            ))
41        }
42    }
43
44    pub fn name(self) -> GString {
45        unsafe { from_glib_full(gdk_sys::gdk_atom_name(self.0)) }
46    }
47
48    pub unsafe fn value(self) -> usize {
49        self.0 as usize
50    }
51}
52
53impl GlibPtrDefault for Atom {
54    type GlibType = gdk_sys::GdkAtom;
55}
56
57#[doc(hidden)]
58impl Uninitialized for Atom {
59    #[inline]
60    unsafe fn uninitialized() -> Self {
61        mem::uninitialized()
62    }
63}
64
65impl<'a> ToGlibPtr<'a, gdk_sys::GdkAtom> for Atom {
66    type Storage = ();
67
68    #[inline]
69    fn to_glib_none(&self) -> Stash<'a, gdk_sys::GdkAtom, Atom> {
70        Stash(self.0, ())
71    }
72}
73
74impl<'a> ToGlibPtrMut<'a, *mut gdk_sys::GdkAtom> for Atom {
75    type Storage = ();
76
77    #[inline]
78    fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut gdk_sys::GdkAtom, Atom> {
79        StashMut(&mut self.0, ())
80    }
81}
82
83impl<'a> ToGlibContainerFromSlice<'a, *mut gdk_sys::GdkAtom> for &'a Atom {
84    type Storage = (
85        Vec<Stash<'a, gdk_sys::GdkAtom, &'a Atom>>,
86        Option<Vec<gdk_sys::GdkAtom>>,
87    );
88
89    fn to_glib_none_from_slice(t: &'a [&'a Atom]) -> (*mut gdk_sys::GdkAtom, Self::Storage) {
90        skip_assert_initialized!();
91
92        let v: Vec<_> = t.iter().map(|s| s.to_glib_none()).collect();
93        let mut v_ptr: Vec<_> = v.iter().map(|s| s.0).collect();
94        v_ptr.push(ptr::null_mut());
95
96        (v_ptr.as_ptr() as *mut gdk_sys::GdkAtom, (v, Some(v_ptr)))
97    }
98
99    fn to_glib_container_from_slice(t: &'a [&'a Atom]) -> (*mut gdk_sys::GdkAtom, Self::Storage) {
100        skip_assert_initialized!();
101
102        let v: Vec<_> = t.iter().map(|s| s.to_glib_none()).collect();
103
104        let v_ptr = unsafe {
105            let v_ptr = glib_sys::g_malloc0(mem::size_of::<gdk_sys::GdkAtom>() * (t.len() + 1))
106                as *mut gdk_sys::GdkAtom;
107
108            for (i, s) in v.iter().enumerate() {
109                ptr::write(v_ptr.offset(i as isize), s.0);
110            }
111
112            v_ptr
113        };
114
115        (v_ptr, (v, None))
116    }
117
118    fn to_glib_full_from_slice(_: &[&'a Atom]) -> *mut gdk_sys::GdkAtom {
119        skip_assert_initialized!();
120
121        unimplemented!()
122    }
123}
124
125impl<'a> ToGlibContainerFromSlice<'a, *const gdk_sys::GdkAtom> for &'a Atom {
126    type Storage = (
127        Vec<Stash<'a, gdk_sys::GdkAtom, &'a Atom>>,
128        Option<Vec<gdk_sys::GdkAtom>>,
129    );
130
131    fn to_glib_none_from_slice(t: &'a [&'a Atom]) -> (*const gdk_sys::GdkAtom, Self::Storage) {
132        skip_assert_initialized!();
133
134        let v: Vec<_> = t.iter().map(|s| s.to_glib_none()).collect();
135        let mut v_ptr: Vec<_> = v.iter().map(|s| s.0).collect();
136        v_ptr.push(ptr::null_mut());
137
138        (v_ptr.as_ptr() as *const gdk_sys::GdkAtom, (v, Some(v_ptr)))
139    }
140
141    fn to_glib_container_from_slice(t: &'a [&'a Atom]) -> (*const gdk_sys::GdkAtom, Self::Storage) {
142        skip_assert_initialized!();
143
144        let v: Vec<_> = t.iter().map(|s| s.to_glib_none()).collect();
145
146        let v_ptr = unsafe {
147            let v_ptr = glib_sys::g_malloc0(mem::size_of::<gdk_sys::GdkAtom>() * (t.len() + 1))
148                as *mut gdk_sys::GdkAtom;
149
150            for (i, s) in v.iter().enumerate() {
151                ptr::write(v_ptr.offset(i as isize), s.0);
152            }
153
154            v_ptr as *const gdk_sys::GdkAtom
155        };
156
157        (v_ptr, (v, None))
158    }
159
160    fn to_glib_full_from_slice(_: &[&'a Atom]) -> *const gdk_sys::GdkAtom {
161        skip_assert_initialized!();
162
163        unimplemented!()
164    }
165}
166
167impl FromGlibPtrNone<gdk_sys::GdkAtom> for Atom {
168    #[inline]
169    unsafe fn from_glib_none(ptr: gdk_sys::GdkAtom) -> Atom {
170        Atom(ptr)
171    }
172}
173
174impl FromGlibPtrBorrow<gdk_sys::GdkAtom> for Atom {
175    #[inline]
176    unsafe fn from_glib_borrow(ptr: gdk_sys::GdkAtom) -> Atom {
177        Atom(ptr)
178    }
179}
180
181impl FromGlibPtrFull<gdk_sys::GdkAtom> for Atom {
182    #[inline]
183    unsafe fn from_glib_full(_: gdk_sys::GdkAtom) -> Atom {
184        unimplemented!()
185    }
186}
187
188impl FromGlibContainerAsVec<gdk_sys::GdkAtom, *mut gdk_sys::GdkAtom> for Atom {
189    unsafe fn from_glib_none_num_as_vec(ptr: *mut gdk_sys::GdkAtom, num: usize) -> Vec<Self> {
190        if num == 0 || ptr.is_null() {
191            return Vec::new();
192        }
193
194        let mut res = Vec::with_capacity(num);
195        for i in 0..num {
196            res.push(from_glib_none(ptr::read(ptr.offset(i as isize))));
197        }
198        res
199    }
200
201    unsafe fn from_glib_container_num_as_vec(ptr: *mut gdk_sys::GdkAtom, num: usize) -> Vec<Self> {
202        let res = FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num);
203        glib_sys::g_free(ptr as *mut _);
204        res
205    }
206
207    unsafe fn from_glib_full_num_as_vec(ptr: *mut gdk_sys::GdkAtom, num: usize) -> Vec<Self> {
208        if num == 0 || ptr.is_null() {
209            return Vec::new();
210        }
211
212        let mut res = Vec::with_capacity(num);
213        for i in 0..num {
214            res.push(from_glib_full(ptr::read(ptr.offset(i as isize))));
215        }
216        glib_sys::g_free(ptr as *mut _);
217        res
218    }
219}
220
221impl FromGlibPtrArrayContainerAsVec<gdk_sys::GdkAtom, *mut gdk_sys::GdkAtom> for Atom {
222    unsafe fn from_glib_none_as_vec(ptr: *mut gdk_sys::GdkAtom) -> Vec<Self> {
223        FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, c_ptr_array_len(ptr))
224    }
225
226    unsafe fn from_glib_container_as_vec(ptr: *mut gdk_sys::GdkAtom) -> Vec<Self> {
227        FromGlibContainerAsVec::from_glib_container_num_as_vec(ptr, c_ptr_array_len(ptr))
228    }
229
230    unsafe fn from_glib_full_as_vec(ptr: *mut gdk_sys::GdkAtom) -> Vec<Self> {
231        FromGlibContainerAsVec::from_glib_full_num_as_vec(ptr, c_ptr_array_len(ptr))
232    }
233}
234
235impl<'a> From<&'a str> for Atom {
236    fn from(r: &'a str) -> Atom {
237        skip_assert_initialized!();
238        Atom::intern(r)
239    }
240}