gtk/subclass/entry.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::object::Cast;
4use glib::object::IsA;
5use glib::subclass::prelude::*;
6use glib::translate::*;
7
8use super::widget::WidgetImpl;
9use crate::Widget;
10use crate::{Entry, ffi};
11
12pub trait EntryImpl: WidgetImpl + ObjectSubclass<Type: IsA<Entry>> {
13 /// Class handler for the [`populate-popup`][struct@crate::Entry#populate-popup] signal. If
14 /// non-[`None`], this will be called to add additional entries to the context
15 /// menu when it is displayed.
16 fn populate_popup(&self, popup: &Widget) {
17 self.parent_populate_popup(popup)
18 }
19
20 /// Class handler for the [`activate`][struct@crate::Entry#activate] signal. The default
21 /// implementation calls [`GtkWindowExt::activate_default()`][crate::prelude::GtkWindowExt::activate_default()] on the entry’s top-level
22 /// window.
23 fn activate(&self) {
24 self.parent_activate()
25 }
26}
27
28pub trait EntryImplExt: EntryImpl {
29 fn parent_populate_popup(&self, popup: &Widget) {
30 unsafe {
31 let data = Self::type_data();
32 let parent_class = data.as_ref().parent_class() as *mut ffi::GtkEntryClass;
33 if let Some(f) = (*parent_class).populate_popup {
34 f(
35 self.obj().unsafe_cast_ref::<Entry>().to_glib_none().0,
36 popup.to_glib_none().0,
37 )
38 }
39 }
40 }
41 fn parent_activate(&self) {
42 unsafe {
43 let data = Self::type_data();
44 let parent_class = data.as_ref().parent_class() as *mut ffi::GtkEntryClass;
45 if let Some(f) = (*parent_class).activate {
46 f(self.obj().unsafe_cast_ref::<Entry>().to_glib_none().0)
47 }
48 }
49 }
50}
51
52impl<T: EntryImpl> EntryImplExt for T {}
53
54unsafe impl<T: EntryImpl> IsSubclassable<T> for Entry {
55 fn class_init(class: &mut glib::Class<Self>) {
56 Self::parent_class_init::<T>(class);
57
58 if !crate::rt::is_initialized() {
59 panic!("GTK has to be initialized first");
60 }
61
62 let klass = class.as_mut();
63 klass.populate_popup = Some(entry_populate_popup::<T>);
64 klass.activate = Some(entry_activate::<T>);
65 }
66}
67
68unsafe extern "C" fn entry_populate_popup<T: EntryImpl>(
69 ptr: *mut ffi::GtkEntry,
70 popupptr: *mut ffi::GtkWidget,
71) {
72 unsafe {
73 let instance = &*(ptr as *mut T::Instance);
74 let imp = instance.imp();
75 let popup: Borrowed<Widget> = from_glib_borrow(popupptr);
76
77 imp.populate_popup(&popup)
78 }
79}
80
81unsafe extern "C" fn entry_activate<T: EntryImpl>(ptr: *mut ffi::GtkEntry) {
82 unsafe {
83 let instance = &*(ptr as *mut T::Instance);
84 let imp = instance.imp();
85
86 imp.activate()
87 }
88}