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