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`](crate::ComboBox).
5
6use glib::{translate::*, GString};
7
8use crate::{ffi, prelude::*, subclass::prelude::*, ComboBox};
9
10#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
11#[allow(deprecated)]
12pub trait ComboBoxImpl: ComboBoxImplExt + WidgetImpl {
13    #[cfg(feature = "v4_6")]
14    #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
15    fn activate(&self) {
16        self.parent_activate()
17    }
18    /// Signal is emitted when the active item is changed.
19    fn changed(&self) {
20        self.parent_changed()
21    }
22    /// Signal which allows you to change how the text
23    ///    displayed in a combo box’s entry is displayed.
24    fn format_entry_text(&self, path: &str) -> Option<GString> {
25        self.parent_format_entry_text(path)
26    }
27}
28
29mod sealed {
30    pub trait Sealed {}
31    impl<T: super::ComboBoxImplExt> Sealed for T {}
32}
33
34#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
35#[allow(deprecated)]
36pub trait ComboBoxImplExt: sealed::Sealed + ObjectSubclass {
37    #[cfg(feature = "v4_6")]
38    #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
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::GtkComboBoxClass;
43            if let Some(f) = (*parent_class).activate {
44                f(self.obj().unsafe_cast_ref::<ComboBox>().to_glib_none().0)
45            }
46        }
47    }
48    fn parent_changed(&self) {
49        unsafe {
50            let data = Self::type_data();
51            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkComboBoxClass;
52            if let Some(f) = (*parent_class).changed {
53                f(self.obj().unsafe_cast_ref::<ComboBox>().to_glib_none().0)
54            }
55        }
56    }
57    fn parent_format_entry_text(&self, path: &str) -> Option<GString> {
58        unsafe {
59            let data = Self::type_data();
60            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkComboBoxClass;
61            if let Some(f) = (*parent_class).format_entry_text {
62                return Some(from_glib_full(f(
63                    self.obj().unsafe_cast_ref::<ComboBox>().to_glib_none().0,
64                    path.to_glib_none().0,
65                )));
66            }
67            None
68        }
69    }
70}
71
72impl<T: ComboBoxImpl> ComboBoxImplExt for T {}
73
74unsafe impl<T: ComboBoxImpl> IsSubclassable<T> for ComboBox {
75    fn class_init(class: &mut glib::Class<Self>) {
76        Self::parent_class_init::<T>(class);
77
78        let klass = class.as_mut();
79        klass.changed = Some(combo_box_changed::<T>);
80        klass.format_entry_text = Some(combo_box_format_entry_text::<T>);
81        #[cfg(feature = "v4_6")]
82        #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
83        {
84            klass.activate = Some(combo_box_activate::<T>);
85        };
86    }
87}
88
89unsafe extern "C" fn combo_box_changed<T: ComboBoxImpl>(ptr: *mut ffi::GtkComboBox) {
90    let instance = &*(ptr as *mut T::Instance);
91    let imp = instance.imp();
92
93    imp.changed()
94}
95
96unsafe extern "C" fn combo_box_format_entry_text<T: ComboBoxImpl>(
97    ptr: *mut ffi::GtkComboBox,
98    pathptr: *const libc::c_char,
99) -> *mut libc::c_char {
100    let instance = &*(ptr as *mut T::Instance);
101    let imp = instance.imp();
102    let path: Borrowed<GString> = from_glib_borrow(pathptr);
103
104    imp.format_entry_text(path.as_str()).into_glib_ptr()
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    let instance = &*(ptr as *mut T::Instance);
111    let imp = instance.imp();
112
113    imp.activate()
114}