gtk4/subclass/
cell_area_context.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 [`CellAreaContext`].
5
6use std::mem::MaybeUninit;
7
8use glib::translate::*;
9
10use crate::{ffi, prelude::*, subclass::prelude::*, CellAreaContext};
11
12#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
13#[allow(deprecated)]
14pub trait CellAreaContextImpl: ObjectImpl + ObjectSubclass<Type: IsA<CellAreaContext>> {
15    fn reset(&self) {
16        self.parent_reset()
17    }
18
19    fn preferred_height_for_width(&self, width: i32) -> (i32, i32) {
20        self.parent_preferred_height_for_width(width)
21    }
22
23    fn preferred_width_for_height(&self, height: i32) -> (i32, i32) {
24        self.parent_preferred_width_for_height(height)
25    }
26
27    fn allocate(&self, width: i32, height: i32) {
28        self.parent_allocate(width, height)
29    }
30}
31
32#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
33#[allow(deprecated)]
34pub trait CellAreaContextImplExt: CellAreaContextImpl {
35    fn parent_reset(&self) {
36        unsafe {
37            let data = Self::type_data();
38            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkCellAreaContextClass;
39            if let Some(f) = (*parent_class).reset {
40                f(self
41                    .obj()
42                    .unsafe_cast_ref::<CellAreaContext>()
43                    .to_glib_none()
44                    .0)
45            }
46        }
47    }
48
49    fn parent_preferred_height_for_width(&self, width: i32) -> (i32, i32) {
50        unsafe {
51            let data = Self::type_data();
52            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkCellAreaContextClass;
53            if let Some(f) = (*parent_class).get_preferred_height_for_width {
54                let mut minimum_size = MaybeUninit::uninit();
55                let mut natural_size = MaybeUninit::uninit();
56                f(
57                    self.obj()
58                        .unsafe_cast_ref::<CellAreaContext>()
59                        .to_glib_none()
60                        .0,
61                    width,
62                    minimum_size.as_mut_ptr(),
63                    natural_size.as_mut_ptr(),
64                );
65                (minimum_size.assume_init(), natural_size.assume_init())
66            } else {
67                (-1, -1)
68            }
69        }
70    }
71
72    fn parent_preferred_width_for_height(&self, height: i32) -> (i32, i32) {
73        unsafe {
74            let data = Self::type_data();
75            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkCellAreaContextClass;
76            if let Some(f) = (*parent_class).get_preferred_width_for_height {
77                let mut minimum_size = MaybeUninit::uninit();
78                let mut natural_size = MaybeUninit::uninit();
79                f(
80                    self.obj()
81                        .unsafe_cast_ref::<CellAreaContext>()
82                        .to_glib_none()
83                        .0,
84                    height,
85                    minimum_size.as_mut_ptr(),
86                    natural_size.as_mut_ptr(),
87                );
88                (minimum_size.assume_init(), natural_size.assume_init())
89            } else {
90                (-1, -1)
91            }
92        }
93    }
94
95    fn parent_allocate(&self, width: i32, height: i32) {
96        unsafe {
97            let data = Self::type_data();
98            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkCellAreaContextClass;
99            if let Some(f) = (*parent_class).allocate {
100                f(
101                    self.obj()
102                        .unsafe_cast_ref::<CellAreaContext>()
103                        .to_glib_none()
104                        .0,
105                    width,
106                    height,
107                )
108            }
109        }
110    }
111}
112
113impl<T: CellAreaContextImpl> CellAreaContextImplExt for T {}
114
115unsafe impl<T: CellAreaContextImpl> IsSubclassable<T> for CellAreaContext {
116    fn class_init(class: &mut glib::Class<Self>) {
117        Self::parent_class_init::<T>(class);
118
119        assert_initialized_main_thread!();
120
121        let klass = class.as_mut();
122        klass.reset = Some(cell_area_context_reset::<T>);
123        klass.get_preferred_height_for_width =
124            Some(cell_area_context_get_preferred_height_for_width::<T>);
125        klass.get_preferred_width_for_height =
126            Some(cell_area_context_get_preferred_width_for_height::<T>);
127        klass.allocate = Some(cell_area_context_allocate::<T>);
128    }
129}
130
131unsafe extern "C" fn cell_area_context_reset<T: CellAreaContextImpl>(
132    ptr: *mut ffi::GtkCellAreaContext,
133) {
134    let instance = &*(ptr as *mut T::Instance);
135    let imp = instance.imp();
136
137    imp.reset()
138}
139
140unsafe extern "C" fn cell_area_context_get_preferred_height_for_width<T: CellAreaContextImpl>(
141    ptr: *mut ffi::GtkCellAreaContext,
142    width: i32,
143    minimum_height: *mut libc::c_int,
144    natural_height: *mut libc::c_int,
145) {
146    let instance = &*(ptr as *mut T::Instance);
147    let imp = instance.imp();
148
149    let (min_height, nat_height) = imp.preferred_height_for_width(width);
150    *minimum_height = min_height;
151    *natural_height = nat_height;
152}
153
154unsafe extern "C" fn cell_area_context_get_preferred_width_for_height<T: CellAreaContextImpl>(
155    ptr: *mut ffi::GtkCellAreaContext,
156    height: i32,
157    minimum_width: *mut libc::c_int,
158    natural_width: *mut libc::c_int,
159) {
160    let instance = &*(ptr as *mut T::Instance);
161    let imp = instance.imp();
162
163    let (min_width, nat_width) = imp.preferred_width_for_height(height);
164    *minimum_width = min_width;
165    *natural_width = nat_width;
166}
167
168unsafe extern "C" fn cell_area_context_allocate<T: CellAreaContextImpl>(
169    ptr: *mut ffi::GtkCellAreaContext,
170    width: i32,
171    height: i32,
172) {
173    let instance = &*(ptr as *mut T::Instance);
174    let imp = instance.imp();
175
176    imp.allocate(width, height)
177}