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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Take a look at the license at the top of the repository in the LICENSE file.

// rustdoc-stripper-ignore-next
//! Traits intended for subclassing [`CellAreaContext`](crate::CellAreaContext).

use std::mem::MaybeUninit;

use glib::translate::*;

use crate::{prelude::*, subclass::prelude::*, CellAreaContext};

#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
#[allow(deprecated)]
pub trait CellAreaContextImpl: CellAreaContextImplExt + ObjectImpl {
    /// Resets any previously cached request and allocation
    /// data.
    ///
    /// When underlying [`TreeModel`][crate::TreeModel] data changes its
    /// important to reset the context if the content
    /// size is allowed to shrink. If the content size
    /// is only allowed to grow (this is usually an option
    /// for views rendering large data stores as a measure
    /// of optimization), then only the row that changed
    /// or was inserted needs to be (re)requested with
    /// gtk_cell_area_get_preferred_width().
    ///
    /// When the new overall size of the context requires
    /// that the allocated size changes (or whenever this
    /// allocation changes at all), the variable row
    /// sizes need to be re-requested for every row.
    ///
    /// For instance, if the rows are displayed all with
    /// the same width from top to bottom then a change
    /// in the allocated width necessitates a recalculation
    /// of all the displayed row heights using
    /// gtk_cell_area_get_preferred_height_for_width().
    ///
    /// # Deprecated since 4.10
    ///
    /// This object will be removed in GTK 5
    fn reset(&self) {
        self.parent_reset()
    }

    /// Gets the accumulative preferred height for @width for all rows
    /// which have been requested for the same said @width with this context.
    ///
    /// After gtk_cell_area_context_reset() is called and/or before ever
    /// requesting the size of a [`CellArea`][crate::CellArea], the returned values are -1.
    ///
    /// # Deprecated since 4.10
    ///
    /// This object will be removed in GTK 5
    /// ## `width`
    /// a proposed width for allocation
    ///
    /// # Returns
    ///
    ///
    /// ## `minimum_height`
    /// location to store the minimum height
    ///
    /// ## `natural_height`
    /// location to store the natural height
    fn preferred_height_for_width(&self, width: i32) -> (i32, i32) {
        self.parent_preferred_height_for_width(width)
    }

    /// Gets the accumulative preferred width for @height for all rows which
    /// have been requested for the same said @height with this context.
    ///
    /// After gtk_cell_area_context_reset() is called and/or before ever
    /// requesting the size of a [`CellArea`][crate::CellArea], the returned values are -1.
    ///
    /// # Deprecated since 4.10
    ///
    /// This object will be removed in GTK 5
    /// ## `height`
    /// a proposed height for allocation
    ///
    /// # Returns
    ///
    ///
    /// ## `minimum_width`
    /// location to store the minimum width
    ///
    /// ## `natural_width`
    /// location to store the natural width
    fn preferred_width_for_height(&self, height: i32) -> (i32, i32) {
        self.parent_preferred_width_for_height(height)
    }

    /// Allocates a width and/or a height for all rows which are to be
    /// rendered with @self.
    ///
    /// Usually allocation is performed only horizontally or sometimes
    /// vertically since a group of rows are usually rendered side by
    /// side vertically or horizontally and share either the same width
    /// or the same height. Sometimes they are allocated in both horizontal
    /// and vertical orientations producing a homogeneous effect of the
    /// rows. This is generally the case for [`TreeView`][crate::TreeView] when
    /// `GtkTreeView:fixed-height-mode` is enabled.
    ///
    /// # Deprecated since 4.10
    ///
    /// This object will be removed in GTK 5
    /// ## `width`
    /// the allocated width for all [`TreeModel`][crate::TreeModel] rows rendered
    ///   with @self, or -1
    /// ## `height`
    /// the allocated height for all [`TreeModel`][crate::TreeModel] rows rendered
    ///   with @self, or -1
    fn allocate(&self, width: i32, height: i32) {
        self.parent_allocate(width, height)
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::CellAreaContextImplExt> Sealed for T {}
}

#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
#[allow(deprecated)]
pub trait CellAreaContextImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_reset(&self) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkCellAreaContextClass;
            if let Some(f) = (*parent_class).reset {
                f(self
                    .obj()
                    .unsafe_cast_ref::<CellAreaContext>()
                    .to_glib_none()
                    .0)
            }
        }
    }

    fn parent_preferred_height_for_width(&self, width: i32) -> (i32, i32) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkCellAreaContextClass;
            if let Some(f) = (*parent_class).get_preferred_height_for_width {
                let mut minimum_size = MaybeUninit::uninit();
                let mut natural_size = MaybeUninit::uninit();
                f(
                    self.obj()
                        .unsafe_cast_ref::<CellAreaContext>()
                        .to_glib_none()
                        .0,
                    width,
                    minimum_size.as_mut_ptr(),
                    natural_size.as_mut_ptr(),
                );
                (minimum_size.assume_init(), natural_size.assume_init())
            } else {
                (-1, -1)
            }
        }
    }

    fn parent_preferred_width_for_height(&self, height: i32) -> (i32, i32) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkCellAreaContextClass;
            if let Some(f) = (*parent_class).get_preferred_width_for_height {
                let mut minimum_size = MaybeUninit::uninit();
                let mut natural_size = MaybeUninit::uninit();
                f(
                    self.obj()
                        .unsafe_cast_ref::<CellAreaContext>()
                        .to_glib_none()
                        .0,
                    height,
                    minimum_size.as_mut_ptr(),
                    natural_size.as_mut_ptr(),
                );
                (minimum_size.assume_init(), natural_size.assume_init())
            } else {
                (-1, -1)
            }
        }
    }

    fn parent_allocate(&self, width: i32, height: i32) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkCellAreaContextClass;
            if let Some(f) = (*parent_class).allocate {
                f(
                    self.obj()
                        .unsafe_cast_ref::<CellAreaContext>()
                        .to_glib_none()
                        .0,
                    width,
                    height,
                )
            }
        }
    }
}

impl<T: CellAreaContextImpl> CellAreaContextImplExt for T {}

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

        assert_initialized_main_thread!();

        let klass = class.as_mut();
        klass.reset = Some(cell_area_context_reset::<T>);
        klass.get_preferred_height_for_width =
            Some(cell_area_context_get_preferred_height_for_width::<T>);
        klass.get_preferred_width_for_height =
            Some(cell_area_context_get_preferred_width_for_height::<T>);
        klass.allocate = Some(cell_area_context_allocate::<T>);
    }
}

unsafe extern "C" fn cell_area_context_reset<T: CellAreaContextImpl>(
    ptr: *mut ffi::GtkCellAreaContext,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.reset()
}

unsafe extern "C" fn cell_area_context_get_preferred_height_for_width<T: CellAreaContextImpl>(
    ptr: *mut ffi::GtkCellAreaContext,
    width: i32,
    minimum_height: *mut libc::c_int,
    natural_height: *mut libc::c_int,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    let (min_height, nat_height) = imp.preferred_height_for_width(width);
    *minimum_height = min_height;
    *natural_height = nat_height;
}

unsafe extern "C" fn cell_area_context_get_preferred_width_for_height<T: CellAreaContextImpl>(
    ptr: *mut ffi::GtkCellAreaContext,
    height: i32,
    minimum_width: *mut libc::c_int,
    natural_width: *mut libc::c_int,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    let (min_width, nat_width) = imp.preferred_width_for_height(height);
    *minimum_width = min_width;
    *natural_width = nat_width;
}

unsafe extern "C" fn cell_area_context_allocate<T: CellAreaContextImpl>(
    ptr: *mut ffi::GtkCellAreaContext,
    width: i32,
    height: i32,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.allocate(width, height)
}