gtk4/subclass/
scale_button.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 [`ScaleButton`].
5
6use glib::translate::*;
7
8use crate::{ffi, prelude::*, subclass::prelude::*, Orientable, ScaleButton};
9
10pub trait ScaleButtonImpl:
11    WidgetImpl + ObjectSubclass<Type: IsA<ScaleButton> + IsA<Orientable>>
12{
13    fn value_changed(&self, new_value: f64) {
14        self.parent_value_changed(new_value)
15    }
16}
17
18pub trait ScaleButtonImplExt: ScaleButtonImpl {
19    fn parent_value_changed(&self, new_value: f64) {
20        unsafe {
21            let data = Self::type_data();
22            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkScaleButtonClass;
23            if let Some(f) = (*parent_class).value_changed {
24                f(
25                    self.obj().unsafe_cast_ref::<ScaleButton>().to_glib_none().0,
26                    new_value,
27                )
28            }
29        }
30    }
31}
32
33impl<T: ScaleButtonImpl> ScaleButtonImplExt for T {}
34
35unsafe impl<T: ScaleButtonImpl> IsSubclassable<T> for ScaleButton {
36    fn class_init(class: &mut glib::Class<Self>) {
37        Self::parent_class_init::<T>(class);
38
39        let klass = class.as_mut();
40        klass.value_changed = Some(scale_button_value_changed::<T>);
41    }
42}
43
44unsafe extern "C" fn scale_button_value_changed<T: ScaleButtonImpl>(
45    ptr: *mut ffi::GtkScaleButton,
46    new_value: f64,
47) {
48    let instance = &*(ptr as *mut T::Instance);
49    let imp = instance.imp();
50
51    imp.value_changed(new_value)
52}