|
| 1 | +// Take a look at the license at the top of the repository in the LICENSE file. |
| 2 | + |
| 3 | +use crate::{Container, Widget}; |
| 4 | +use glib::translate::*; |
| 5 | +use glib::{value::FromValue, IsA, ToValue}; |
| 6 | + |
| 7 | +pub trait ContainerExtManual: 'static { |
| 8 | + #[doc(alias = "gtk_container_child_get_property")] |
| 9 | + fn child_property_value(&self, child: &impl IsA<Widget>, property_name: &str) -> glib::Value; |
| 10 | + |
| 11 | + #[doc(alias = "gtk_container_child_get_property")] |
| 12 | + fn child_property<V: for<'b> FromValue<'b> + 'static>( |
| 13 | + &self, |
| 14 | + child: &impl IsA<Widget>, |
| 15 | + property_name: &str, |
| 16 | + ) -> V; |
| 17 | + |
| 18 | + #[doc(alias = "gtk_container_child_set_property")] |
| 19 | + fn child_set_property( |
| 20 | + &self, |
| 21 | + child: &impl IsA<Widget>, |
| 22 | + property_name: &str, |
| 23 | + value: &dyn ToValue, |
| 24 | + ); |
| 25 | +} |
| 26 | + |
| 27 | +impl<O: IsA<Container>> ContainerExtManual for O { |
| 28 | + fn child_property_value(&self, child: &impl IsA<Widget>, property_name: &str) -> glib::Value { |
| 29 | + unsafe { |
| 30 | + let container_class = glib::Class::<Container>::from_type(O::static_type()).unwrap(); |
| 31 | + let pspec: Option<glib::ParamSpec> = |
| 32 | + from_glib_none(ffi::gtk_container_class_find_child_property( |
| 33 | + container_class.as_ref() as *const _ as *const glib::gobject_ffi::GObjectClass, |
| 34 | + property_name.to_glib_none().0, |
| 35 | + )); |
| 36 | + let pspec = pspec.unwrap_or_else(|| { |
| 37 | + panic!("The Container property '{}' doesn't exists", property_name) |
| 38 | + }); |
| 39 | + |
| 40 | + if !pspec.flags().contains(glib::ParamFlags::READABLE) |
| 41 | + || !pspec.flags().contains(glib::ParamFlags::READWRITE) |
| 42 | + { |
| 43 | + panic!("The Container property '{}' is not readable", property_name); |
| 44 | + } |
| 45 | + |
| 46 | + let mut value = glib::Value::from_type(pspec.value_type()); |
| 47 | + ffi::gtk_container_child_get_property( |
| 48 | + self.as_ref().to_glib_none().0, |
| 49 | + child.as_ref().to_glib_none().0, |
| 50 | + property_name.to_glib_none().0, |
| 51 | + value.to_glib_none_mut().0, |
| 52 | + ); |
| 53 | + value |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + fn child_property<V: for<'b> FromValue<'b> + 'static>( |
| 58 | + &self, |
| 59 | + child: &impl IsA<Widget>, |
| 60 | + property_name: &str, |
| 61 | + ) -> V { |
| 62 | + let value = self.child_property_value(child, property_name); |
| 63 | + value |
| 64 | + .get_owned::<V>() |
| 65 | + .expect("Failed to get value of container") |
| 66 | + } |
| 67 | + |
| 68 | + fn child_set_property( |
| 69 | + &self, |
| 70 | + child: &impl IsA<Widget>, |
| 71 | + property_name: &str, |
| 72 | + value: &dyn ToValue, |
| 73 | + ) { |
| 74 | + unsafe { |
| 75 | + let container_class = glib::Class::<Container>::from_type(O::static_type()).unwrap(); |
| 76 | + let pspec: Option<glib::ParamSpec> = |
| 77 | + from_glib_none(ffi::gtk_container_class_find_child_property( |
| 78 | + container_class.as_ref() as *const _ as *const glib::gobject_ffi::GObjectClass, |
| 79 | + property_name.to_glib_none().0, |
| 80 | + )); |
| 81 | + let pspec = pspec.unwrap_or_else(|| { |
| 82 | + panic!("The Container property '{}' doesn't exists", property_name) |
| 83 | + }); |
| 84 | + |
| 85 | + if !pspec.flags().contains(glib::ParamFlags::WRITABLE) |
| 86 | + || !pspec.flags().contains(glib::ParamFlags::READWRITE) |
| 87 | + { |
| 88 | + panic!( |
| 89 | + "The Container property '{}' is not writeable", |
| 90 | + property_name |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + assert!( |
| 95 | + pspec.value_type().is_a(value.value_type()), |
| 96 | + "The Container property '{}' is value is of wrong type. Expected '{}' but got '{}'", |
| 97 | + property_name, |
| 98 | + pspec.value_type(), |
| 99 | + value.value_type() |
| 100 | + ); |
| 101 | + |
| 102 | + ffi::gtk_container_child_set_property( |
| 103 | + self.as_ref().to_glib_none().0, |
| 104 | + child.as_ref().to_glib_none().0, |
| 105 | + property_name.to_glib_none().0, |
| 106 | + value.to_value().to_glib_none().0, |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments