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    /// Signal which allows you to change how the text
25    ///    displayed in a combo box’s entry is displayed.
26    fn format_entry_text(&self, path: &str) -> Option<GString> {
27        self.parent_format_entry_text(path)
28    }
29}
30
31#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
32#[allow(deprecated)]
33pub trait ComboBoxImplExt: ComboBoxImpl {
34    #[cfg(feature = "v4_6")]
35    #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
36    fn parent_activate(&self) {
37        unsafe {
38            let data = Self::type_data();
39            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkComboBoxClass;
40            if let Some(f) = (*parent_class).activate {
41                f(self.obj().unsafe_cast_ref::<ComboBox>().to_glib_none().0)
42            }
43        }
44    }
45    fn parent_changed(&self) {
46        unsafe {
47            let data = Self::type_data();
48            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkComboBoxClass;
49            if let Some(f) = (*parent_class).changed {
50                f(self.obj().unsafe_cast_ref::<ComboBox>().to_glib_none().0)
51            }
52        }
53    }
54    fn parent_format_entry_text(&self, path: &str) -> Option<GString> {
55        unsafe {
56            let data = Self::type_data();
57            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkComboBoxClass;
58            if let Some(f) = (*parent_class).format_entry_text {
59                return Some(from_glib_full(f(
60                    self.obj().unsafe_cast_ref::<ComboBox>().to_glib_none().0,
61                    path.to_glib_none().0,
62                )));
63            }
64            None
65        }
66    }
67}
68
69impl<T: ComboBoxImpl> ComboBoxImplExt for T {}
70
71unsafe impl<T: ComboBoxImpl> IsSubclassable<T> for ComboBox {
72    fn class_init(class: &mut glib::Class<Self>) {
73        Self::parent_class_init::<T>(class);
74
75        let klass = class.as_mut();
76        klass.changed = Some(combo_box_changed::<T>);
77        klass.format_entry_text = Some(combo_box_format_entry_text::<T>);
78        #[cfg(feature = "v4_6")]
79        #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
80        {
81            klass.activate = Some(combo_box_activate::<T>);
82        };
83    }
84}
85
86unsafe extern "C" fn combo_box_changed<T: ComboBoxImpl>(ptr: *mut ffi::GtkComboBox) {
87    unsafe {
88        let instance = &*(ptr as *mut T::Instance);
89        let imp = instance.imp();
90
91        imp.changed()
92    }
93}
94
95unsafe extern "C" fn combo_box_format_entry_text<T: ComboBoxImpl>(
96    ptr: *mut ffi::GtkComboBox,
97    pathptr: *const libc::c_char,
98) -> *mut libc::c_char {
99    unsafe {
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
108#[cfg(feature = "v4_6")]
109#[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
110unsafe extern "C" fn combo_box_activate<T: ComboBoxImpl>(ptr: *mut ffi::GtkComboBox) {
111    unsafe {
112        let instance = &*(ptr as *mut T::Instance);
113        let imp = instance.imp();
114
115        imp.activate()
116    }
117}