gtk4/subclass/
scale.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Take a look at the license at the top of the repository in the LICENSE file.

// rustdoc-stripper-ignore-next
//! Traits intended for subclassing [`Scale`].

use glib::translate::*;

use crate::{ffi, prelude::*, subclass::prelude::*, Orientable, Scale};

pub trait ScaleImpl: RangeImpl + ObjectSubclass<Type: IsA<Scale> + IsA<Orientable>> {
    /// Obtains the coordinates where the scale will draw the
    /// [`pango::Layout`][crate::pango::Layout] representing the text in the scale.
    ///
    /// Remember when using the [`pango::Layout`][crate::pango::Layout] function you need to
    /// convert to and from pixels using `PANGO_PIXELS()` or `PANGO_SCALE`.
    ///
    /// If the [`draw-value`][struct@crate::Scale#draw-value] property is [`false`], the return
    /// values are undefined.
    ///
    /// # Returns
    ///
    ///
    /// ## `x`
    /// location to store X offset of layout
    ///
    /// ## `y`
    /// location to store Y offset of layout
    #[doc(alias = "get_layout_offsets")]
    fn layout_offsets(&self) -> (i32, i32) {
        self.parent_layout_offsets()
    }
}

pub trait ScaleImplExt: ScaleImpl {
    fn parent_layout_offsets(&self) -> (i32, i32) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkScaleClass;
            let mut x = 0;
            let mut y = 0;
            if let Some(f) = (*parent_class).get_layout_offsets {
                f(
                    self.obj().unsafe_cast_ref::<Scale>().to_glib_none().0,
                    &mut x,
                    &mut y,
                );
            }
            (x, y)
        }
    }
}

impl<T: ScaleImpl> ScaleImplExt for T {}

unsafe impl<T: ScaleImpl> IsSubclassable<T> for Scale {
    fn class_init(class: &mut glib::Class<Self>) {
        Self::parent_class_init::<T>(class);

        let klass = class.as_mut();
        klass.get_layout_offsets = Some(scale_get_layout_offsets::<T>);
    }
}

unsafe extern "C" fn scale_get_layout_offsets<T: ScaleImpl>(
    ptr: *mut ffi::GtkScale,
    x_ptr: *mut libc::c_int,
    y_ptr: *mut libc::c_int,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    let (x, y) = imp.layout_offsets();
    *x_ptr = x;
    *y_ptr = y;
}