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
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::Fixed;
use crate::Widget;
use glib::translate::*;
use glib::IsA;

// All this is in order to avoid the segfault. More info in :
// https://github.com/gtk-rs/gtk/issues/565
fn has_widget<O: IsA<Fixed>, T: IsA<Widget>>(c: &O, item: &T) -> bool {
    skip_assert_initialized!();
    unsafe {
        let glist = ffi::gtk_container_get_children(c.to_glib_none().0 as *mut _);
        let found = !glib::ffi::g_list_find(glist, item.to_glib_none().0 as _).is_null();
        glib::ffi::g_list_free(glist);
        found
    }
}

pub trait FixedExtManual: 'static {
    #[doc(alias = "get_child_x")]
    fn child_x<T: IsA<Widget>>(&self, item: &T) -> i32;

    fn set_child_x<T: IsA<Widget>>(&self, item: &T, x: i32);

    #[doc(alias = "get_child_y")]
    fn child_y<T: IsA<Widget>>(&self, item: &T) -> i32;

    fn set_child_y<T: IsA<Widget>>(&self, item: &T, y: i32);
}

impl<O: IsA<Fixed>> FixedExtManual for O {
    fn child_x<T: IsA<Widget>>(&self, item: &T) -> i32 {
        assert!(
            has_widget(self, item),
            "this item isn't in the Fixed's widget list"
        );
        let mut value = glib::Value::from(&0);
        unsafe {
            ffi::gtk_container_child_get_property(
                self.to_glib_none().0 as *mut _,
                item.as_ref().to_glib_none().0,
                "x".to_glib_none().0,
                value.to_glib_none_mut().0,
            );
        }
        value
            .get()
            .expect("Return Value for `FixedExtManual::get_child_x`")
    }

    fn set_child_x<T: IsA<Widget>>(&self, item: &T, x: i32) {
        assert!(
            has_widget(self, item),
            "this item isn't in the Fixed's widget list"
        );
        unsafe {
            ffi::gtk_container_child_set_property(
                self.to_glib_none().0 as *mut _,
                item.as_ref().to_glib_none().0,
                "x".to_glib_none().0,
                glib::Value::from(&x).to_glib_none().0,
            );
        }
    }

    fn child_y<T: IsA<Widget>>(&self, item: &T) -> i32 {
        assert!(
            has_widget(self, item),
            "this item isn't in the Fixed's widget list"
        );
        let mut value = glib::Value::from(&0);
        unsafe {
            ffi::gtk_container_child_get_property(
                self.to_glib_none().0 as *mut _,
                item.as_ref().to_glib_none().0,
                "y".to_glib_none().0,
                value.to_glib_none_mut().0,
            );
        }
        value
            .get()
            .expect("Return Value for `FixedExtManual::get_child_y`")
    }

    fn set_child_y<T: IsA<Widget>>(&self, item: &T, y: i32) {
        assert!(
            has_widget(self, item),
            "this item isn't in the Fixed's widget list"
        );
        unsafe {
            ffi::gtk_container_child_set_property(
                self.to_glib_none().0 as *mut _,
                item.as_ref().to_glib_none().0,
                "y".to_glib_none().0,
                glib::Value::from(&y).to_glib_none().0,
            );
        }
    }
}