Skip to main content

gtk/subclass/
entry.rs

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