Skip to main content

gtk4/subclass/
combo_box.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3// rustdoc-stripper-ignore-next
4//! Traits intended for subclassing [`ComboBox`].
5
6use glib::{GString, translate::*};
7
8use crate::{CellEditable, CellLayout, ComboBox, ffi, prelude::*, subclass::prelude::*};
9
10#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
11#[allow(deprecated)]
12pub trait ComboBoxImpl:
13    WidgetImpl + ObjectSubclass<Type: IsA<ComboBox> + IsA<CellEditable> + IsA<CellLayout>>
14{
15    #[cfg(feature = "v4_6")]
16    #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
17    fn activate(&self) {
18        self.parent_activate()
19    }
20    /// Signal is emitted when the active item is changed.
21    fn changed(&self) {
22        self.parent_changed()
23    }
24    /// s entry is displayed.
25    fn format_entry_text(&self, path: &str) -> Option<GString> {
26        self.parent_format_entry_text(path)
27    }
28}
29
30#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
31#[allow(deprecated)]
32pub trait ComboBoxImplExt: ComboBoxImpl {
33    #[cfg(feature = "v4_6")]
34    #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
35    fn parent_activate(&self) {
36        unsafe {
37            let data = Self::type_data();
38            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkComboBoxClass;
39            if let Some(f) = (*parent_class).activate {
40                f(self.obj().unsafe_cast_ref::<ComboBox>().to_glib_none().0)
41            }
42        }
43    }
44    fn parent_changed(&self) {
45        unsafe {
46            let data = Self::type_data();
47            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkComboBoxClass;
48            if let Some(f) = (*parent_class).changed {
49                f(self.obj().unsafe_cast_ref::<ComboBox>().to_glib_none().0)
50            }
51        }
52    }
53    fn parent_format_entry_text(&self, path: &str) -> Option<GString> {
54        unsafe {
55            let data = Self::type_data();
56            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkComboBoxClass;
57            if let Some(f) = (*parent_class).format_entry_text {
58                return Some(from_glib_full(f(
59                    self.obj().unsafe_cast_ref::<ComboBox>().to_glib_none().0,
60                    path.to_glib_none().0,
61                )));
62            }
63            None
64        }
65    }
66}
67
68impl<T: ComboBoxImpl> ComboBoxImplExt for T {}
69
70unsafe impl<T: ComboBoxImpl> IsSubclassable<T> for ComboBox {
71    fn class_init(class: &mut glib::Class<Self>) {
72        Self::parent_class_init::<T>(class);
73
74        let klass = class.as_mut();
75        klass.changed = Some(combo_box_changed::<T>);
76        klass.format_entry_text = Some(combo_box_format_entry_text::<T>);
77        #[cfg(feature = "v4_6")]
78        #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
79        {
80            klass.activate = Some(combo_box_activate::<T>);
81        };
82    }
83}
84
85unsafe extern "C" fn combo_box_changed<T: ComboBoxImpl>(ptr: *mut ffi::GtkComboBox) {
86    unsafe {
87        let instance = &*(ptr as *mut T::Instance);
88        let imp = instance.imp();
89
90        imp.changed()
91    }
92}
93
94unsafe extern "C" fn combo_box_format_entry_text<T: ComboBoxImpl>(
95    ptr: *mut ffi::GtkComboBox,
96    pathptr: *const libc::c_char,
97) -> *mut libc::c_char {
98    unsafe {
99        let instance = &*(ptr as *mut T::Instance);
100        let imp = instance.imp();
101        let path: Borrowed<GString> = from_glib_borrow(pathptr);
102
103        imp.format_entry_text(path.as_str()).into_glib_ptr()
104    }
105}
106
107#[cfg(feature = "v4_6")]
108#[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
109unsafe extern "C" fn combo_box_activate<T: ComboBoxImpl>(ptr: *mut ffi::GtkComboBox) {
110    unsafe {
111        let instance = &*(ptr as *mut T::Instance);
112        let imp = instance.imp();
113
114        imp.activate()
115    }
116}