Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit 14fed09

Browse files
committed
subclass support for GtkEntry
1 parent 1c5ac89 commit 14fed09

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

gtk/src/subclass/entry.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
use glib::subclass::prelude::*;
4+
use glib::translate::*;
5+
use glib::Cast;
6+
7+
use super::widget::WidgetImpl;
8+
use crate::Entry;
9+
use crate::Widget;
10+
11+
pub trait EntryImpl: EntryImplExt + WidgetImpl {
12+
fn populate_popup(&self, entry: &Self::Type, popup: &Widget) {
13+
self.parent_populate_popup(entry, popup)
14+
}
15+
16+
fn activate(&self, entry: &Self::Type) {
17+
self.parent_activate(entry)
18+
}
19+
}
20+
21+
pub trait EntryImplExt: ObjectSubclass {
22+
fn parent_populate_popup(&self, entry: &Self::Type, popup: &Widget);
23+
fn parent_activate(&self, entry: &Self::Type);
24+
}
25+
26+
impl<T: EntryImpl> EntryImplExt for T {
27+
fn parent_populate_popup(&self, entry: &Self::Type, popup: &Widget) {
28+
unsafe {
29+
let data = T::type_data();
30+
let parent_class = data.as_ref().parent_class() as *mut ffi::GtkEntryClass;
31+
if let Some(f) = (*parent_class).populate_popup {
32+
f(
33+
entry.unsafe_cast_ref::<Entry>().to_glib_none().0,
34+
popup.to_glib_none().0,
35+
)
36+
}
37+
}
38+
}
39+
40+
fn parent_activate(&self, entry: &Self::Type) {
41+
unsafe {
42+
let data = T::type_data();
43+
let parent_class = data.as_ref().parent_class() as *mut ffi::GtkEntryClass;
44+
if let Some(f) = (*parent_class).activate {
45+
f(entry.unsafe_cast_ref::<Entry>().to_glib_none().0)
46+
}
47+
}
48+
}
49+
}
50+
51+
unsafe impl<T: EntryImpl> IsSubclassable<T> for Entry {
52+
fn class_init(class: &mut glib::Class<Self>) {
53+
Self::parent_class_init::<T>(class);
54+
55+
if !crate::rt::is_initialized() {
56+
panic!("GTK has to be initialized first");
57+
}
58+
59+
let klass = class.as_mut();
60+
klass.populate_popup = Some(entry_populate_popup::<T>);
61+
klass.activate = Some(entry_activate::<T>);
62+
}
63+
}
64+
65+
unsafe extern "C" fn entry_populate_popup<T: EntryImpl>(
66+
ptr: *mut ffi::GtkEntry,
67+
popupptr: *mut ffi::GtkWidget,
68+
) {
69+
let instance = &*(ptr as *mut T::Instance);
70+
let imp = instance.imp();
71+
let wrap: Borrowed<Entry> = from_glib_borrow(ptr);
72+
let popup: Borrowed<Widget> = from_glib_borrow(popupptr);
73+
74+
imp.populate_popup(wrap.unsafe_cast_ref(), &popup)
75+
}
76+
77+
unsafe extern "C" fn entry_activate<T: EntryImpl>(ptr: *mut ffi::GtkEntry) {
78+
let instance = &*(ptr as *mut T::Instance);
79+
let imp = instance.imp();
80+
let wrap: Borrowed<Entry> = from_glib_borrow(ptr);
81+
82+
imp.activate(wrap.unsafe_cast_ref())
83+
}

gtk/src/subclass/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod cell_renderer_toggle;
1717
pub mod container;
1818
pub mod dialog;
1919
pub mod drawing_area;
20+
pub mod entry;
2021
pub mod event_box;
2122
pub mod fixed;
2223
pub mod header_bar;
@@ -56,6 +57,7 @@ pub mod prelude {
5657
pub use super::container::{ContainerClassSubclassExt, ContainerImpl, ContainerImplExt};
5758
pub use super::dialog::{DialogImpl, DialogImplExt};
5859
pub use super::drawing_area::DrawingAreaImpl;
60+
pub use super::entry::{EntryImpl, EntryImplExt};
5961
pub use super::event_box::EventBoxImpl;
6062
pub use super::fixed::FixedImpl;
6163
pub use super::header_bar::HeaderBarImpl;

0 commit comments

Comments
 (0)