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`](crate::Scale).
5
6use glib::translate::*;
7
8use crate::{ffi, prelude::*, subclass::prelude::*, Scale};
9
10pub trait ScaleImpl: ScaleImplExt + RangeImpl {
11    /// Obtains the coordinates where the scale will draw the
12    /// [`pango::Layout`][crate::pango::Layout] representing the text in the scale.
13    ///
14    /// Remember when using the [`pango::Layout`][crate::pango::Layout] function you need to
15    /// convert to and from pixels using `PANGO_PIXELS()` or `PANGO_SCALE`.
16    ///
17    /// If the [`draw-value`][struct@crate::Scale#draw-value] property is [`false`], the return
18    /// values are undefined.
19    ///
20    /// # Returns
21    ///
22    ///
23    /// ## `x`
24    /// location to store X offset of layout
25    ///
26    /// ## `y`
27    /// location to store Y offset of layout
28    #[doc(alias = "get_layout_offsets")]
29    fn layout_offsets(&self) -> (i32, i32) {
30        self.parent_layout_offsets()
31    }
32}
33
34mod sealed {
35    pub trait Sealed {}
36    impl<T: super::ScaleImplExt> Sealed for T {}
37}
38
39pub trait ScaleImplExt: sealed::Sealed + ObjectSubclass {
40    fn parent_layout_offsets(&self) -> (i32, i32) {
41        unsafe {
42            let data = Self::type_data();
43            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkScaleClass;
44            let mut x = 0;
45            let mut y = 0;
46            if let Some(f) = (*parent_class).get_layout_offsets {
47                f(
48                    self.obj().unsafe_cast_ref::<Scale>().to_glib_none().0,
49                    &mut x,
50                    &mut y,
51                );
52            }
53            (x, y)
54        }
55    }
56}
57
58impl<T: ScaleImpl> ScaleImplExt for T {}
59
60unsafe impl<T: ScaleImpl> IsSubclassable<T> for Scale {
61    fn class_init(class: &mut glib::Class<Self>) {
62        Self::parent_class_init::<T>(class);
63
64        let klass = class.as_mut();
65        klass.get_layout_offsets = Some(scale_get_layout_offsets::<T>);
66    }
67}
68
69unsafe extern "C" fn scale_get_layout_offsets<T: ScaleImpl>(
70    ptr: *mut ffi::GtkScale,
71    x_ptr: *mut libc::c_int,
72    y_ptr: *mut libc::c_int,
73) {
74    let instance = &*(ptr as *mut T::Instance);
75    let imp = instance.imp();
76
77    let (x, y) = imp.layout_offsets();
78    *x_ptr = x;
79    *y_ptr = y;
80}