gtk4/subclass/
scale.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 [`Scale`].
5
6use glib::translate::*;
7
8use crate::{ffi, prelude::*, subclass::prelude::*, Orientable, Scale};
9
10pub trait ScaleImpl: RangeImpl + ObjectSubclass<Type: IsA<Scale> + IsA<Orientable>> {
11    #[doc(alias = "get_layout_offsets")]
12    fn layout_offsets(&self) -> (i32, i32) {
13        self.parent_layout_offsets()
14    }
15}
16
17pub trait ScaleImplExt: ScaleImpl {
18    fn parent_layout_offsets(&self) -> (i32, i32) {
19        unsafe {
20            let data = Self::type_data();
21            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkScaleClass;
22            let mut x = 0;
23            let mut y = 0;
24            if let Some(f) = (*parent_class).get_layout_offsets {
25                f(
26                    self.obj().unsafe_cast_ref::<Scale>().to_glib_none().0,
27                    &mut x,
28                    &mut y,
29                );
30            }
31            (x, y)
32        }
33    }
34}
35
36impl<T: ScaleImpl> ScaleImplExt for T {}
37
38unsafe impl<T: ScaleImpl> IsSubclassable<T> for Scale {
39    fn class_init(class: &mut glib::Class<Self>) {
40        Self::parent_class_init::<T>(class);
41
42        let klass = class.as_mut();
43        klass.get_layout_offsets = Some(scale_get_layout_offsets::<T>);
44    }
45}
46
47unsafe extern "C" fn scale_get_layout_offsets<T: ScaleImpl>(
48    ptr: *mut ffi::GtkScale,
49    x_ptr: *mut libc::c_int,
50    y_ptr: *mut libc::c_int,
51) {
52    let instance = &*(ptr as *mut T::Instance);
53    let imp = instance.imp();
54
55    let (x, y) = imp.layout_offsets();
56    *x_ptr = x;
57    *y_ptr = y;
58}