Skip to content

Commit a7bbf4e

Browse files
gtk: add DrawingArea subclassing support
1 parent 65425a9 commit a7bbf4e

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

gtk4/src/subclass/drawing_area.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use gtk_sys;
2+
3+
use glib::subclass::prelude::*;
4+
use glib::translate::*;
5+
use glib::Cast;
6+
7+
use super::widget::WidgetImpl;
8+
use DrawingArea;
9+
use Widget;
10+
11+
pub trait DrawingAreaImpl: DrawingAreaImplExt + WidgetImpl {
12+
fn resize(&self, drawing_area: &Self::Type, width: i32, height: i32) {
13+
self.parent_resize(drawing_area, width, height)
14+
}
15+
}
16+
17+
pub trait DrawingAreaImplExt: ObjectSubclass {
18+
fn parent_resize(&self, drawing_area: &Self::Type, width: i32, height: i32);
19+
}
20+
21+
impl<T: DrawingAreaImpl> DrawingAreaImplExt for T {
22+
fn parent_resize(&self, drawing_area: &Self::Type, width: i32, height: i32) {
23+
unsafe {
24+
let data = T::type_data();
25+
let parent_class =
26+
data.as_ref().get_parent_class() as *mut gtk_sys::GtkDrawingAreaClass;
27+
let f = (*parent_class)
28+
.resize
29+
.expect("No parent class impl for \"resize\"");
30+
f(
31+
drawing_area
32+
.unsafe_cast_ref::<DrawingArea>()
33+
.to_glib_none()
34+
.0,
35+
width,
36+
height,
37+
)
38+
}
39+
}
40+
}
41+
42+
unsafe impl<T: DrawingAreaImpl> IsSubclassable<T> for DrawingArea {
43+
fn override_vfuncs(class: &mut glib::Class<Self>) {
44+
<Widget as IsSubclassable<T>>::override_vfuncs(class);
45+
46+
let klass = class.as_mut();
47+
klass.resize = Some(drawing_area_resize::<T>);
48+
}
49+
}
50+
51+
unsafe extern "C" fn drawing_area_resize<T: DrawingAreaImpl>(
52+
ptr: *mut gtk_sys::GtkDrawingArea,
53+
width: i32,
54+
height: i32,
55+
) {
56+
let instance = &*(ptr as *mut T::Instance);
57+
let imp = instance.get_impl();
58+
let wrap: Borrowed<DrawingArea> = from_glib_borrow(ptr);
59+
60+
imp.resize(wrap.unsafe_cast_ref(), width, height)
61+
}

gtk4/src/subclass/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod application_window;
88
pub mod box_;
99
pub mod button;
1010
pub mod dialog;
11+
pub mod drawing_area;
1112
pub mod flow_box;
1213
pub mod flow_box_child;
1314
pub mod header_bar;
@@ -23,6 +24,7 @@ pub mod prelude {
2324
pub use super::box_::BoxImpl;
2425
pub use super::button::ButtonImpl;
2526
pub use super::dialog::DialogImpl;
27+
pub use super::drawing_area::DrawingAreaImpl;
2628
pub use super::flow_box::FlowBoxImpl;
2729
pub use super::flow_box_child::FlowBoxChildImpl;
2830
pub use super::header_bar::HeaderBarImpl;

0 commit comments

Comments
 (0)