gtk/
fixed.rs

1// Copyright 2013-2017, 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 glib::translate::*;
6use glib_sys;
7use gtk_sys;
8use Fixed;
9use IsA;
10use Value;
11use Widget;
12
13// All this is in order to avoid the segfault. More info in :
14// https://github.com/gtk-rs/gtk/issues/565
15fn has_widget<O: IsA<Fixed>, T: IsA<Widget>>(c: &O, item: &T) -> bool {
16    skip_assert_initialized!();
17    unsafe {
18        let glist = gtk_sys::gtk_container_get_children(c.to_glib_none().0 as *mut _);
19        let found = !glib_sys::g_list_find(glist, item.to_glib_none().0 as _).is_null();
20        glib_sys::g_list_free(glist);
21        found
22    }
23}
24
25pub trait FixedExtManual: 'static {
26    fn get_child_x<T: IsA<Widget>>(&self, item: &T) -> i32;
27
28    fn set_child_x<T: IsA<Widget>>(&self, item: &T, x: i32);
29
30    fn get_child_y<T: IsA<Widget>>(&self, item: &T) -> i32;
31
32    fn set_child_y<T: IsA<Widget>>(&self, item: &T, y: i32);
33}
34
35impl<O: IsA<Fixed>> FixedExtManual for O {
36    fn get_child_x<T: IsA<Widget>>(&self, item: &T) -> i32 {
37        assert!(
38            has_widget(self, item),
39            "this item isn't in the Fixed's widget list"
40        );
41        let mut value = Value::from(&0);
42        unsafe {
43            gtk_sys::gtk_container_child_get_property(
44                self.to_glib_none().0 as *mut _,
45                item.as_ref().to_glib_none().0,
46                "x".to_glib_none().0,
47                value.to_glib_none_mut().0,
48            );
49        }
50        value.get().unwrap()
51    }
52
53    fn set_child_x<T: IsA<Widget>>(&self, item: &T, x: i32) {
54        assert!(
55            has_widget(self, item),
56            "this item isn't in the Fixed's widget list"
57        );
58        unsafe {
59            gtk_sys::gtk_container_child_set_property(
60                self.to_glib_none().0 as *mut _,
61                item.as_ref().to_glib_none().0,
62                "x".to_glib_none().0,
63                Value::from(&x).to_glib_none().0,
64            );
65        }
66    }
67
68    fn get_child_y<T: IsA<Widget>>(&self, item: &T) -> i32 {
69        assert!(
70            has_widget(self, item),
71            "this item isn't in the Fixed's widget list"
72        );
73        let mut value = Value::from(&0);
74        unsafe {
75            gtk_sys::gtk_container_child_get_property(
76                self.to_glib_none().0 as *mut _,
77                item.as_ref().to_glib_none().0,
78                "y".to_glib_none().0,
79                value.to_glib_none_mut().0,
80            );
81        }
82        value.get().unwrap()
83    }
84
85    fn set_child_y<T: IsA<Widget>>(&self, item: &T, y: i32) {
86        assert!(
87            has_widget(self, item),
88            "this item isn't in the Fixed's widget list"
89        );
90        unsafe {
91            gtk_sys::gtk_container_child_set_property(
92                self.to_glib_none().0 as *mut _,
93                item.as_ref().to_glib_none().0,
94                "y".to_glib_none().0,
95                Value::from(&y).to_glib_none().0,
96            );
97        }
98    }
99}