Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
Issue #862 - Add size_allocate vfunc to gtk::Widget
Browse files Browse the repository at this point in the history
  • Loading branch information
hfiguiere committed Feb 22, 2020
1 parent 86c878a commit 69e1a9e
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/subclass/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::SelectionData;
use crate::TextDirection;
use cairo;
use cairo_sys;
use Allocation;
use SizeRequestMode;
use Widget;
use WidgetClass;
Expand Down Expand Up @@ -209,6 +210,10 @@ pub trait WidgetImpl: WidgetImplExt + ObjectImpl + 'static {
fn get_preferred_height_for_width(&self, widget: &Widget, width: i32) -> (i32, i32) {
self.parent_get_preferred_height_for_width(widget, width)
}

fn size_allocate(&self, widget: &Widget, allocation: &Allocation) {
self.parent_size_allocate(widget, allocation)
}
}

pub trait WidgetImplExt {
Expand Down Expand Up @@ -298,6 +303,7 @@ pub trait WidgetImplExt {
fn parent_get_preferred_width_for_height(&self, widget: &Widget, height: i32) -> (i32, i32);
fn parent_get_preferred_height(&self, widget: &Widget) -> (i32, i32);
fn parent_get_preferred_height_for_width(&self, widget: &Widget, width: i32) -> (i32, i32);
fn parent_size_allocate(&self, widget: &Widget, allocation: &Allocation);
}

impl<T: WidgetImpl + ObjectImpl> WidgetImplExt for T {
Expand Down Expand Up @@ -794,6 +800,18 @@ impl<T: WidgetImpl + ObjectImpl> WidgetImplExt for T {
(minimum_size.assume_init(), natural_size.assume_init())
}
}

fn parent_size_allocate(&self, widget: &Widget, allocation: &Allocation) {
unsafe {
let data = self.get_type_data();
let parent_class = data.as_ref().get_parent_class() as *mut gtk_sys::GtkWidgetClass;
let f = (*parent_class).size_allocate.unwrap();
f(
widget.to_glib_none().0,
mut_override(allocation.to_glib_none().0),
);
}
}
}

unsafe impl<T: ObjectSubclass + WidgetImpl> IsSubclassable<T> for WidgetClass {
Expand Down Expand Up @@ -834,6 +852,7 @@ unsafe impl<T: ObjectSubclass + WidgetImpl> IsSubclassable<T> for WidgetClass {
klass.get_preferred_height_for_width = Some(widget_get_preferred_height_for_width::<T>);
klass.get_preferred_height = Some(widget_get_preferred_height::<T>);
klass.get_preferred_width_for_height = Some(widget_get_preferred_width_for_height::<T>);
klass.size_allocate = Some(widget_size_allocate::<T>);
}
}
}
Expand Down Expand Up @@ -1349,3 +1368,17 @@ unsafe extern "C" fn widget_get_preferred_height_for_width<T: ObjectSubclass>(
*nat_height_ptr = nat_height;
}
}

unsafe extern "C" fn widget_size_allocate<T: ObjectSubclass>(
ptr: *mut gtk_sys::GtkWidget,
allocation: *mut gtk_sys::GtkAllocation,
) where
T: WidgetImpl,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.get_impl();
let wrap: Widget = from_glib_borrow(ptr);
let allocate: &Allocation = &from_glib_none(allocation);

imp.size_allocate(&wrap, allocate);
}

0 comments on commit 69e1a9e

Please sign in to comment.