graphene/auto/
rect.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{Point, ffi};
6use glib::translate::*;
7
8glib::wrapper! {
9    /// The location and size of a rectangle region.
10    ///
11    /// The width and height of a [`Rect`][crate::Rect] can be negative; for instance,
12    /// a [`Rect`][crate::Rect] with an origin of [ 0, 0 ] and a size of [ 10, 10 ] is
13    /// equivalent to a [`Rect`][crate::Rect] with an origin of [ 10, 10 ] and a size
14    /// of [ -10, -10 ].
15    ///
16    /// Application code can normalize rectangles using [`normalize()`][Self::normalize()];
17    /// this function will ensure that the width and height of a rectangle are
18    /// positive values. All functions taking a [`Rect`][crate::Rect] as an argument
19    /// will internally operate on a normalized copy; all functions returning a
20    /// [`Rect`][crate::Rect] will always return a normalized rectangle.
21    pub struct Rect(BoxedInline<ffi::graphene_rect_t>);
22
23    match fn {
24        copy => |ptr| glib::gobject_ffi::g_boxed_copy(ffi::graphene_rect_get_type(), ptr as *mut _) as *mut ffi::graphene_rect_t,
25        free => |ptr| glib::gobject_ffi::g_boxed_free(ffi::graphene_rect_get_type(), ptr as *mut _),
26        type_ => || ffi::graphene_rect_get_type(),
27    }
28}
29
30impl Rect {
31    /// Checks whether a [`Rect`][crate::Rect] contains the given coordinates.
32    /// ## `p`
33    /// a [`Point`][crate::Point]
34    ///
35    /// # Returns
36    ///
37    /// `true` if the rectangle contains the point
38    #[doc(alias = "graphene_rect_contains_point")]
39    pub fn contains_point(&self, p: &Point) -> bool {
40        unsafe { ffi::graphene_rect_contains_point(self.to_glib_none().0, p.to_glib_none().0) }
41    }
42
43    /// Checks whether a [`Rect`][crate::Rect] fully contains the given
44    /// rectangle.
45    /// ## `b`
46    /// a [`Rect`][crate::Rect]
47    ///
48    /// # Returns
49    ///
50    /// `true` if the rectangle `self` fully contains `b`
51    #[doc(alias = "graphene_rect_contains_rect")]
52    pub fn contains_rect(&self, b: &Rect) -> bool {
53        unsafe { ffi::graphene_rect_contains_rect(self.to_glib_none().0, b.to_glib_none().0) }
54    }
55
56    #[doc(alias = "graphene_rect_equal")]
57    fn equal(&self, b: &Rect) -> bool {
58        unsafe { ffi::graphene_rect_equal(self.to_glib_none().0, b.to_glib_none().0) }
59    }
60
61    /// Expands a [`Rect`][crate::Rect] to contain the given [`Point`][crate::Point].
62    /// ## `p`
63    /// a [`Point`][crate::Point]
64    ///
65    /// # Returns
66    ///
67    ///
68    /// ## `res`
69    /// return location for the expanded rectangle
70    #[doc(alias = "graphene_rect_expand")]
71    #[must_use]
72    pub fn expand(&self, p: &Point) -> Rect {
73        unsafe {
74            let mut res = Rect::uninitialized();
75            ffi::graphene_rect_expand(
76                self.to_glib_none().0,
77                p.to_glib_none().0,
78                res.to_glib_none_mut().0,
79            );
80            res
81        }
82    }
83
84    /// Compute the area of given normalized rectangle.
85    ///
86    /// # Returns
87    ///
88    /// the area of the normalized rectangle
89    #[doc(alias = "graphene_rect_get_area")]
90    #[doc(alias = "get_area")]
91    pub fn area(&self) -> f32 {
92        unsafe { ffi::graphene_rect_get_area(self.to_glib_none().0) }
93    }
94
95    /// Retrieves the coordinates of the bottom-left corner of the given rectangle.
96    ///
97    /// # Returns
98    ///
99    ///
100    /// ## `p`
101    /// return location for a [`Point`][crate::Point]
102    #[doc(alias = "graphene_rect_get_bottom_left")]
103    #[doc(alias = "get_bottom_left")]
104    pub fn bottom_left(&self) -> Point {
105        unsafe {
106            let mut p = Point::uninitialized();
107            ffi::graphene_rect_get_bottom_left(self.to_glib_none().0, p.to_glib_none_mut().0);
108            p
109        }
110    }
111
112    /// Retrieves the coordinates of the bottom-right corner of the given rectangle.
113    ///
114    /// # Returns
115    ///
116    ///
117    /// ## `p`
118    /// return location for a [`Point`][crate::Point]
119    #[doc(alias = "graphene_rect_get_bottom_right")]
120    #[doc(alias = "get_bottom_right")]
121    pub fn bottom_right(&self) -> Point {
122        unsafe {
123            let mut p = Point::uninitialized();
124            ffi::graphene_rect_get_bottom_right(self.to_glib_none().0, p.to_glib_none_mut().0);
125            p
126        }
127    }
128
129    /// Retrieves the coordinates of the center of the given rectangle.
130    ///
131    /// # Returns
132    ///
133    ///
134    /// ## `p`
135    /// return location for a [`Point`][crate::Point]
136    #[doc(alias = "graphene_rect_get_center")]
137    #[doc(alias = "get_center")]
138    pub fn center(&self) -> Point {
139        unsafe {
140            let mut p = Point::uninitialized();
141            ffi::graphene_rect_get_center(self.to_glib_none().0, p.to_glib_none_mut().0);
142            p
143        }
144    }
145
146    /// Retrieves the normalized height of the given rectangle.
147    ///
148    /// # Returns
149    ///
150    /// the normalized height of the rectangle
151    #[doc(alias = "graphene_rect_get_height")]
152    #[doc(alias = "get_height")]
153    pub fn height(&self) -> f32 {
154        unsafe { ffi::graphene_rect_get_height(self.to_glib_none().0) }
155    }
156
157    /// Retrieves the coordinates of the top-left corner of the given rectangle.
158    ///
159    /// # Returns
160    ///
161    ///
162    /// ## `p`
163    /// return location for a [`Point`][crate::Point]
164    #[doc(alias = "graphene_rect_get_top_left")]
165    #[doc(alias = "get_top_left")]
166    pub fn top_left(&self) -> Point {
167        unsafe {
168            let mut p = Point::uninitialized();
169            ffi::graphene_rect_get_top_left(self.to_glib_none().0, p.to_glib_none_mut().0);
170            p
171        }
172    }
173
174    /// Retrieves the coordinates of the top-right corner of the given rectangle.
175    ///
176    /// # Returns
177    ///
178    ///
179    /// ## `p`
180    /// return location for a [`Point`][crate::Point]
181    #[doc(alias = "graphene_rect_get_top_right")]
182    #[doc(alias = "get_top_right")]
183    pub fn top_right(&self) -> Point {
184        unsafe {
185            let mut p = Point::uninitialized();
186            ffi::graphene_rect_get_top_right(self.to_glib_none().0, p.to_glib_none_mut().0);
187            p
188        }
189    }
190
191    /// Retrieves the normalized width of the given rectangle.
192    ///
193    /// # Returns
194    ///
195    /// the normalized width of the rectangle
196    #[doc(alias = "graphene_rect_get_width")]
197    #[doc(alias = "get_width")]
198    pub fn width(&self) -> f32 {
199        unsafe { ffi::graphene_rect_get_width(self.to_glib_none().0) }
200    }
201
202    /// Retrieves the normalized X coordinate of the origin of the given
203    /// rectangle.
204    ///
205    /// # Returns
206    ///
207    /// the normalized X coordinate of the rectangle
208    #[doc(alias = "graphene_rect_get_x")]
209    #[doc(alias = "get_x")]
210    pub fn x(&self) -> f32 {
211        unsafe { ffi::graphene_rect_get_x(self.to_glib_none().0) }
212    }
213
214    /// Retrieves the normalized Y coordinate of the origin of the given
215    /// rectangle.
216    ///
217    /// # Returns
218    ///
219    /// the normalized Y coordinate of the rectangle
220    #[doc(alias = "graphene_rect_get_y")]
221    #[doc(alias = "get_y")]
222    pub fn y(&self) -> f32 {
223        unsafe { ffi::graphene_rect_get_y(self.to_glib_none().0) }
224    }
225
226    /// Changes the given rectangle to be smaller, or larger depending on the
227    /// given inset parameters.
228    ///
229    /// To create an inset rectangle, use positive `d_x` or `d_y` values; to
230    /// create a larger, encompassing rectangle, use negative `d_x` or `d_y`
231    /// values.
232    ///
233    /// The origin of the rectangle is offset by `d_x` and `d_y`, while the size
234    /// is adjusted by `(2 * `d_x`, 2 * `d_y`)`. If `d_x` and `d_y` are positive
235    /// values, the size of the rectangle is decreased; if `d_x` and `d_y` are
236    /// negative values, the size of the rectangle is increased.
237    ///
238    /// If the size of the resulting inset rectangle has a negative width or
239    /// height then the size will be set to zero.
240    /// ## `d_x`
241    /// the horizontal inset
242    /// ## `d_y`
243    /// the vertical inset
244    ///
245    /// # Returns
246    ///
247    /// the inset rectangle
248    #[doc(alias = "graphene_rect_inset")]
249    pub fn inset(&mut self, d_x: f32, d_y: f32) {
250        unsafe {
251            ffi::graphene_rect_inset(self.to_glib_none_mut().0, d_x, d_y);
252        }
253    }
254
255    /// Changes the given rectangle to be smaller, or larger depending on the
256    /// given inset parameters.
257    ///
258    /// To create an inset rectangle, use positive `d_x` or `d_y` values; to
259    /// create a larger, encompassing rectangle, use negative `d_x` or `d_y`
260    /// values.
261    ///
262    /// The origin of the rectangle is offset by `d_x` and `d_y`, while the size
263    /// is adjusted by `(2 * `d_x`, 2 * `d_y`)`. If `d_x` and `d_y` are positive
264    /// values, the size of the rectangle is decreased; if `d_x` and `d_y` are
265    /// negative values, the size of the rectangle is increased.
266    ///
267    /// If the size of the resulting inset rectangle has a negative width or
268    /// height then the size will be set to zero.
269    /// ## `d_x`
270    /// the horizontal inset
271    /// ## `d_y`
272    /// the vertical inset
273    ///
274    /// # Returns
275    ///
276    ///
277    /// ## `res`
278    /// return location for the inset rectangle
279    #[doc(alias = "graphene_rect_inset_r")]
280    #[must_use]
281    pub fn inset_r(&self, d_x: f32, d_y: f32) -> Rect {
282        unsafe {
283            let mut res = Rect::uninitialized();
284            ffi::graphene_rect_inset_r(self.to_glib_none().0, d_x, d_y, res.to_glib_none_mut().0);
285            res
286        }
287    }
288
289    /// Linearly interpolates the origin and size of the two given
290    /// rectangles.
291    /// ## `b`
292    /// a [`Rect`][crate::Rect]
293    /// ## `factor`
294    /// the linear interpolation factor
295    ///
296    /// # Returns
297    ///
298    ///
299    /// ## `res`
300    /// return location for the
301    ///  interpolated rectangle
302    #[doc(alias = "graphene_rect_interpolate")]
303    #[must_use]
304    pub fn interpolate(&self, b: &Rect, factor: f64) -> Rect {
305        unsafe {
306            let mut res = Rect::uninitialized();
307            ffi::graphene_rect_interpolate(
308                self.to_glib_none().0,
309                b.to_glib_none().0,
310                factor,
311                res.to_glib_none_mut().0,
312            );
313            res
314        }
315    }
316
317    /// Computes the intersection of the two given rectangles.
318    ///
319    /// ![](rectangle-intersection.png)
320    ///
321    /// The intersection in the image above is the blue outline.
322    ///
323    /// If the two rectangles do not intersect, `res` will contain
324    /// a degenerate rectangle with origin in (0, 0) and a size of 0.
325    /// ## `b`
326    /// a [`Rect`][crate::Rect]
327    ///
328    /// # Returns
329    ///
330    /// `true` if the two rectangles intersect
331    ///
332    /// ## `res`
333    /// return location for
334    ///  a [`Rect`][crate::Rect]
335    #[doc(alias = "graphene_rect_intersection")]
336    pub fn intersection(&self, b: &Rect) -> Option<Rect> {
337        unsafe {
338            let mut res = Rect::uninitialized();
339            let ret = ffi::graphene_rect_intersection(
340                self.to_glib_none().0,
341                b.to_glib_none().0,
342                res.to_glib_none_mut().0,
343            );
344            if ret { Some(res) } else { None }
345        }
346    }
347
348    /// Normalizes the passed rectangle.
349    ///
350    /// This function ensures that the size of the rectangle is made of
351    /// positive values, and that the origin is the top-left corner of
352    /// the rectangle.
353    ///
354    /// # Returns
355    ///
356    /// the normalized rectangle
357    #[doc(alias = "graphene_rect_normalize")]
358    pub fn normalize(&mut self) {
359        unsafe {
360            ffi::graphene_rect_normalize(self.to_glib_none_mut().0);
361        }
362    }
363
364    /// Normalizes the passed rectangle.
365    ///
366    /// This function ensures that the size of the rectangle is made of
367    /// positive values, and that the origin is in the top-left corner
368    /// of the rectangle.
369    ///
370    /// # Returns
371    ///
372    ///
373    /// ## `res`
374    /// the return location for the
375    ///  normalized rectangle
376    #[doc(alias = "graphene_rect_normalize_r")]
377    #[must_use]
378    pub fn normalize_r(&self) -> Rect {
379        unsafe {
380            let mut res = Rect::uninitialized();
381            ffi::graphene_rect_normalize_r(self.to_glib_none().0, res.to_glib_none_mut().0);
382            res
383        }
384    }
385
386    /// Offsets the origin by `d_x` and `d_y`.
387    ///
388    /// The size of the rectangle is unchanged.
389    /// ## `d_x`
390    /// the horizontal offset
391    /// ## `d_y`
392    /// the vertical offset
393    ///
394    /// # Returns
395    ///
396    /// the offset rectangle
397    #[doc(alias = "graphene_rect_offset")]
398    pub fn offset(&mut self, d_x: f32, d_y: f32) {
399        unsafe {
400            ffi::graphene_rect_offset(self.to_glib_none_mut().0, d_x, d_y);
401        }
402    }
403
404    /// Offsets the origin of the given rectangle by `d_x` and `d_y`.
405    ///
406    /// The size of the rectangle is left unchanged.
407    /// ## `d_x`
408    /// the horizontal offset
409    /// ## `d_y`
410    /// the vertical offset
411    ///
412    /// # Returns
413    ///
414    ///
415    /// ## `res`
416    /// return location for the offset
417    ///  rectangle
418    #[doc(alias = "graphene_rect_offset_r")]
419    #[must_use]
420    pub fn offset_r(&self, d_x: f32, d_y: f32) -> Rect {
421        unsafe {
422            let mut res = Rect::uninitialized();
423            ffi::graphene_rect_offset_r(self.to_glib_none().0, d_x, d_y, res.to_glib_none_mut().0);
424            res
425        }
426    }
427
428    /// Rounds the origin of the given rectangle to its nearest
429    /// integer value and and recompute the size so that the
430    /// rectangle is large enough to contain all the conrners
431    /// of the original rectangle.
432    ///
433    /// This function is the equivalent of calling `floor` on
434    /// the coordinates of the origin, and recomputing the size
435    /// calling `ceil` on the bottom-right coordinates.
436    ///
437    /// If you want to be sure that the rounded rectangle
438    /// completely covers the area that was covered by the
439    /// original rectangle — i.e. you want to cover the area
440    /// including all its corners — this function will make sure
441    /// that the size is recomputed taking into account the ceiling
442    /// of the coordinates of the bottom-right corner.
443    /// If the difference between the original coordinates and the
444    /// coordinates of the rounded rectangle is greater than the
445    /// difference between the original size and and the rounded
446    /// size, then the move of the origin would not be compensated
447    /// by a move in the anti-origin, leaving the corners of the
448    /// original rectangle outside the rounded one.
449    ///
450    /// # Returns
451    ///
452    ///
453    /// ## `res`
454    /// return location for the
455    ///  rectangle with rounded extents
456    #[doc(alias = "graphene_rect_round_extents")]
457    #[must_use]
458    pub fn round_extents(&self) -> Rect {
459        unsafe {
460            let mut res = Rect::uninitialized();
461            ffi::graphene_rect_round_extents(self.to_glib_none().0, res.to_glib_none_mut().0);
462            res
463        }
464    }
465
466    /// Scales the size and origin of a rectangle horizontaly by `s_h`,
467    /// and vertically by `s_v`. The result `res` is normalized.
468    /// ## `s_h`
469    /// horizontal scale factor
470    /// ## `s_v`
471    /// vertical scale factor
472    ///
473    /// # Returns
474    ///
475    ///
476    /// ## `res`
477    /// return location for the
478    ///  scaled rectangle
479    #[doc(alias = "graphene_rect_scale")]
480    #[must_use]
481    pub fn scale(&self, s_h: f32, s_v: f32) -> Rect {
482        unsafe {
483            let mut res = Rect::uninitialized();
484            ffi::graphene_rect_scale(self.to_glib_none().0, s_h, s_v, res.to_glib_none_mut().0);
485            res
486        }
487    }
488
489    /// Computes the union of the two given rectangles.
490    ///
491    /// ![](rectangle-union.png)
492    ///
493    /// The union in the image above is the blue outline.
494    /// ## `b`
495    /// a [`Rect`][crate::Rect]
496    ///
497    /// # Returns
498    ///
499    ///
500    /// ## `res`
501    /// return location for a [`Rect`][crate::Rect]
502    #[doc(alias = "graphene_rect_union")]
503    #[must_use]
504    pub fn union(&self, b: &Rect) -> Rect {
505        unsafe {
506            let mut res = Rect::uninitialized();
507            ffi::graphene_rect_union(
508                self.to_glib_none().0,
509                b.to_glib_none().0,
510                res.to_glib_none_mut().0,
511            );
512            res
513        }
514    }
515
516    /// Returns a degenerate rectangle with origin fixed at (0, 0) and
517    /// a size of 0, 0.
518    ///
519    /// # Returns
520    ///
521    /// a fixed rectangle
522    #[doc(alias = "graphene_rect_zero")]
523    pub fn zero() -> Rect {
524        assert_initialized_main_thread!();
525        unsafe { from_glib_none(ffi::graphene_rect_zero()) }
526    }
527}
528
529impl PartialEq for Rect {
530    #[inline]
531    fn eq(&self, other: &Self) -> bool {
532        self.equal(other)
533    }
534}
535
536impl Eq for Rect {}