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    /// 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
34pub trait ScaleImplExt: ScaleImpl {
35    fn parent_layout_offsets(&self) -> (i32, i32) {
36        unsafe {
37            let data = Self::type_data();
38            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkScaleClass;
39            let mut x = 0;
40            let mut y = 0;
41            if let Some(f) = (*parent_class).get_layout_offsets {
42                f(
43                    self.obj().unsafe_cast_ref::<Scale>().to_glib_none().0,
44                    &mut x,
45                    &mut y,
46                );
47            }
48            (x, y)
49        }
50    }
51}
52
53impl<T: ScaleImpl> ScaleImplExt for T {}
54
55unsafe impl<T: ScaleImpl> IsSubclassable<T> for Scale {
56    fn class_init(class: &mut glib::Class<Self>) {
57        Self::parent_class_init::<T>(class);
58
59        let klass = class.as_mut();
60        klass.get_layout_offsets = Some(scale_get_layout_offsets::<T>);
61    }
62}
63
64unsafe extern "C" fn scale_get_layout_offsets<T: ScaleImpl>(
65    ptr: *mut ffi::GtkScale,
66    x_ptr: *mut libc::c_int,
67    y_ptr: *mut libc::c_int,
68) {
69    let instance = &*(ptr as *mut T::Instance);
70    let imp = instance.imp();
71
72    let (x, y) = imp.layout_offsets();
73    *x_ptr = x;
74    *y_ptr = y;
75}