|
| 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 | +} |
0 commit comments