From 5f6574545811d8cdea2aa3cff145079c72e41610 Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Fri, 20 Nov 2020 00:24:50 +0100 Subject: [PATCH] gtk: add Scrollable subclassing support --- gtk4/src/subclass/mod.rs | 2 ++ gtk4/src/subclass/scrollable.rs | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 gtk4/src/subclass/scrollable.rs diff --git a/gtk4/src/subclass/mod.rs b/gtk4/src/subclass/mod.rs index 9e5c7159b52b..13cafe0e8632 100644 --- a/gtk4/src/subclass/mod.rs +++ b/gtk4/src/subclass/mod.rs @@ -16,6 +16,7 @@ pub mod layout_manager; pub mod list_box_row; pub mod native_dialog; pub mod popover; +pub mod scrollable; pub mod sorter; pub mod style_context; pub mod toggle_button; @@ -37,6 +38,7 @@ pub mod prelude { pub use super::list_box_row::ListBoxRowImpl; pub use super::native_dialog::NativeDialogImpl; pub use super::popover::PopoverImpl; + pub use super::scrollable::ScrollableImpl; pub use super::sorter::SorterImpl; pub use super::style_context::StyleContextImpl; pub use super::toggle_button::ToggleButtonImpl; diff --git a/gtk4/src/subclass/scrollable.rs b/gtk4/src/subclass/scrollable.rs new file mode 100644 index 000000000000..423957bf39ce --- /dev/null +++ b/gtk4/src/subclass/scrollable.rs @@ -0,0 +1,37 @@ +use crate::{Border, Scrollable}; +use glib::subclass::prelude::*; +use glib::translate::*; +use glib::Cast; + +pub trait ScrollableImpl: ObjectImpl { + fn get_border(&self, scrollable: &Self::Type) -> Option; +} + +unsafe impl IsImplementable for Scrollable { + unsafe extern "C" fn interface_init( + iface: glib::ffi::gpointer, + _iface_data: glib::ffi::gpointer, + ) { + let scrollable_iface = &mut *(iface as *mut ffi::GtkScrollableInterface); + + scrollable_iface.get_border = Some(scrollable_get_border::); + } +} + +unsafe extern "C" fn scrollable_get_border( + scrollable: *mut ffi::GtkScrollable, + borderptr: *mut ffi::GtkBorder, +) -> glib::ffi::gboolean { + let instance = &*(scrollable as *mut T::Instance); + let imp = instance.get_impl(); + + if let Some(border) = + imp.get_border(from_glib_borrow::<_, Scrollable>(scrollable).unsafe_cast_ref()) + { + *borderptr = *border.to_glib_full(); + true.to_glib() + } else { + *borderptr = *std::ptr::null(); + false.to_glib() + } +}