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::{translate::*, GString};
7
8use crate::{ffi, prelude::*, subclass::prelude::*, CellEditable, CellLayout, ComboBox};
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    fn changed(&self) {
21        self.parent_changed()
22    }
23    fn format_entry_text(&self, path: &str) -> Option<GString> {
24        self.parent_format_entry_text(path)
25    }
26}
27
28#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
29#[allow(deprecated)]
30pub trait ComboBoxImplExt: ComboBoxImpl {
31    #[cfg(feature = "v4_6")]
32    #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
33    fn parent_activate(&self) {
34        unsafe {
35            let data = Self::type_data();
36            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkComboBoxClass;
37            if let Some(f) = (*parent_class).activate {
38                f(self.obj().unsafe_cast_ref::<ComboBox>().to_glib_none().0)
39            }
40        }
41    }
42    fn parent_changed(&self) {
43        unsafe {
44            let data = Self::type_data();
45            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkComboBoxClass;
46            if let Some(f) = (*parent_class).changed {
47                f(self.obj().unsafe_cast_ref::<ComboBox>().to_glib_none().0)
48            }
49        }
50    }
51    fn parent_format_entry_text(&self, path: &str) -> Option<GString> {
52        unsafe {
53            let data = Self::type_data();
54            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkComboBoxClass;
55            if let Some(f) = (*parent_class).format_entry_text {
56                return Some(from_glib_full(f(
57                    self.obj().unsafe_cast_ref::<ComboBox>().to_glib_none().0,
58                    path.to_glib_none().0,
59                )));
60            }
61            None
62        }
63    }
64}
65
66impl<T: ComboBoxImpl> ComboBoxImplExt for T {}
67
68unsafe impl<T: ComboBoxImpl> IsSubclassable<T> for ComboBox {
69    fn class_init(class: &mut glib::Class<Self>) {
70        Self::parent_class_init::<T>(class);
71
72        let klass = class.as_mut();
73        klass.changed = Some(combo_box_changed::<T>);
74        klass.format_entry_text = Some(combo_box_format_entry_text::<T>);
75        #[cfg(feature = "v4_6")]
76        #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
77        {
78            klass.activate = Some(combo_box_activate::<T>);
79        };
80    }
81}
82
83unsafe extern "C" fn combo_box_changed<T: ComboBoxImpl>(ptr: *mut ffi::GtkComboBox) {
84    let instance = &*(ptr as *mut T::Instance);
85    let imp = instance.imp();
86
87    imp.changed()
88}
89
90unsafe extern "C" fn combo_box_format_entry_text<T: ComboBoxImpl>(
91    ptr: *mut ffi::GtkComboBox,
92    pathptr: *const libc::c_char,
93) -> *mut libc::c_char {
94    let instance = &*(ptr as *mut T::Instance);
95    let imp = instance.imp();
96    let path: Borrowed<GString> = from_glib_borrow(pathptr);
97
98    imp.format_entry_text(path.as_str()).into_glib_ptr()
99}
100
101#[cfg(feature = "v4_6")]
102#[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
103unsafe extern "C" fn combo_box_activate<T: ComboBoxImpl>(ptr: *mut ffi::GtkComboBox) {
104    let instance = &*(ptr as *mut T::Instance);
105    let imp = instance.imp();
106
107    imp.activate()
108}