gtk4/subclass/
range.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 [`Range`].
5
6use glib::translate::*;
7
8use crate::{ffi, prelude::*, subclass::prelude::*, Border, Orientable, Range, ScrollType};
9
10pub trait RangeImpl: WidgetImpl + ObjectSubclass<Type: IsA<Range> + IsA<Orientable>> {
11    fn adjust_bounds(&self, new_value: f64) {
12        self.parent_adjust_bounds(new_value)
13    }
14
15    fn change_value(&self, scroll_type: ScrollType, new_value: f64) -> glib::Propagation {
16        self.parent_change_value(scroll_type, new_value)
17    }
18
19    #[doc(alias = "get_range_border")]
20    fn range_border(&self) -> Border {
21        self.parent_range_border()
22    }
23
24    fn move_slider(&self, scroll_type: ScrollType) {
25        self.parent_move_slider(scroll_type)
26    }
27
28    fn value_changed(&self) {
29        self.parent_value_changed()
30    }
31}
32
33pub trait RangeImplExt: RangeImpl {
34    fn parent_adjust_bounds(&self, new_value: f64) {
35        unsafe {
36            let data = Self::type_data();
37            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkRangeClass;
38            if let Some(f) = (*parent_class).adjust_bounds {
39                f(
40                    self.obj().unsafe_cast_ref::<Range>().to_glib_none().0,
41                    new_value,
42                )
43            }
44        }
45    }
46
47    fn parent_change_value(&self, scroll_type: ScrollType, new_value: f64) -> glib::Propagation {
48        unsafe {
49            let data = Self::type_data();
50            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkRangeClass;
51            let f = (*parent_class)
52                .change_value
53                .expect("No parent class impl for \"change_value\"");
54            from_glib(f(
55                self.obj().unsafe_cast_ref::<Range>().to_glib_none().0,
56                scroll_type.into_glib(),
57                new_value,
58            ))
59        }
60    }
61
62    fn parent_range_border(&self) -> Border {
63        unsafe {
64            let data = Self::type_data();
65            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkRangeClass;
66            let mut border = Border::default();
67            if let Some(f) = (*parent_class).get_range_border {
68                f(
69                    self.obj().unsafe_cast_ref::<Range>().to_glib_none().0,
70                    border.to_glib_none_mut().0,
71                );
72            }
73            border
74        }
75    }
76
77    fn parent_move_slider(&self, scroll_type: ScrollType) {
78        unsafe {
79            let data = Self::type_data();
80            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkRangeClass;
81            if let Some(f) = (*parent_class).move_slider {
82                f(
83                    self.obj().unsafe_cast_ref::<Range>().to_glib_none().0,
84                    scroll_type.into_glib(),
85                )
86            }
87        }
88    }
89
90    fn parent_value_changed(&self) {
91        unsafe {
92            let data = Self::type_data();
93            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkRangeClass;
94            if let Some(f) = (*parent_class).value_changed {
95                f(self.obj().unsafe_cast_ref::<Range>().to_glib_none().0)
96            }
97        }
98    }
99}
100
101impl<T: RangeImpl> RangeImplExt for T {}
102
103unsafe impl<T: RangeImpl> IsSubclassable<T> for Range {
104    fn class_init(class: &mut glib::Class<Self>) {
105        Self::parent_class_init::<T>(class);
106
107        let klass = class.as_mut();
108        klass.adjust_bounds = Some(range_adjust_bounds::<T>);
109        klass.change_value = Some(range_change_value::<T>);
110        klass.get_range_border = Some(range_get_range_border::<T>);
111        klass.move_slider = Some(range_move_slider::<T>);
112        klass.value_changed = Some(range_value_changed::<T>);
113    }
114}
115
116unsafe extern "C" fn range_adjust_bounds<T: RangeImpl>(ptr: *mut ffi::GtkRange, new_value: f64) {
117    let instance = &*(ptr as *mut T::Instance);
118    let imp = instance.imp();
119
120    imp.adjust_bounds(new_value)
121}
122
123unsafe extern "C" fn range_change_value<T: RangeImpl>(
124    ptr: *mut ffi::GtkRange,
125    scroll_type: ffi::GtkScrollType,
126    new_value: f64,
127) -> glib::ffi::gboolean {
128    let instance = &*(ptr as *mut T::Instance);
129    let imp = instance.imp();
130
131    imp.change_value(from_glib(scroll_type), new_value)
132        .into_glib()
133}
134
135unsafe extern "C" fn range_get_range_border<T: RangeImpl>(
136    ptr: *mut ffi::GtkRange,
137    borderptr: *mut ffi::GtkBorder,
138) {
139    let instance = &*(ptr as *mut T::Instance);
140    let imp = instance.imp();
141
142    let border = imp.range_border();
143    *borderptr = *border.to_glib_none().0;
144}
145
146unsafe extern "C" fn range_move_slider<T: RangeImpl>(
147    ptr: *mut ffi::GtkRange,
148    scroll_type: ffi::GtkScrollType,
149) {
150    let instance = &*(ptr as *mut T::Instance);
151    let imp = instance.imp();
152
153    imp.move_slider(from_glib(scroll_type))
154}
155
156unsafe extern "C" fn range_value_changed<T: RangeImpl>(ptr: *mut ffi::GtkRange) {
157    let instance = &*(ptr as *mut T::Instance);
158    let imp = instance.imp();
159
160    imp.value_changed()
161}