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::{ffi, Point};
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 /// 
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 {
345 Some(res)
346 } else {
347 None
348 }
349 }
350 }
351
352 /// Normalizes the passed rectangle.
353 ///
354 /// This function ensures that the size of the rectangle is made of
355 /// positive values, and that the origin is the top-left corner of
356 /// the rectangle.
357 ///
358 /// # Returns
359 ///
360 /// the normalized rectangle
361 #[doc(alias = "graphene_rect_normalize")]
362 pub fn normalize(&mut self) {
363 unsafe {
364 ffi::graphene_rect_normalize(self.to_glib_none_mut().0);
365 }
366 }
367
368 /// Normalizes the passed rectangle.
369 ///
370 /// This function ensures that the size of the rectangle is made of
371 /// positive values, and that the origin is in the top-left corner
372 /// of the rectangle.
373 ///
374 /// # Returns
375 ///
376 ///
377 /// ## `res`
378 /// the return location for the
379 /// normalized rectangle
380 #[doc(alias = "graphene_rect_normalize_r")]
381 #[must_use]
382 pub fn normalize_r(&self) -> Rect {
383 unsafe {
384 let mut res = Rect::uninitialized();
385 ffi::graphene_rect_normalize_r(self.to_glib_none().0, res.to_glib_none_mut().0);
386 res
387 }
388 }
389
390 /// Offsets the origin by `d_x` and `d_y`.
391 ///
392 /// The size of the rectangle is unchanged.
393 /// ## `d_x`
394 /// the horizontal offset
395 /// ## `d_y`
396 /// the vertical offset
397 ///
398 /// # Returns
399 ///
400 /// the offset rectangle
401 #[doc(alias = "graphene_rect_offset")]
402 pub fn offset(&mut self, d_x: f32, d_y: f32) {
403 unsafe {
404 ffi::graphene_rect_offset(self.to_glib_none_mut().0, d_x, d_y);
405 }
406 }
407
408 /// Offsets the origin of the given rectangle by `d_x` and `d_y`.
409 ///
410 /// The size of the rectangle is left unchanged.
411 /// ## `d_x`
412 /// the horizontal offset
413 /// ## `d_y`
414 /// the vertical offset
415 ///
416 /// # Returns
417 ///
418 ///
419 /// ## `res`
420 /// return location for the offset
421 /// rectangle
422 #[doc(alias = "graphene_rect_offset_r")]
423 #[must_use]
424 pub fn offset_r(&self, d_x: f32, d_y: f32) -> Rect {
425 unsafe {
426 let mut res = Rect::uninitialized();
427 ffi::graphene_rect_offset_r(self.to_glib_none().0, d_x, d_y, res.to_glib_none_mut().0);
428 res
429 }
430 }
431
432 /// Rounds the origin of the given rectangle to its nearest
433 /// integer value and and recompute the size so that the
434 /// rectangle is large enough to contain all the conrners
435 /// of the original rectangle.
436 ///
437 /// This function is the equivalent of calling `floor` on
438 /// the coordinates of the origin, and recomputing the size
439 /// calling `ceil` on the bottom-right coordinates.
440 ///
441 /// If you want to be sure that the rounded rectangle
442 /// completely covers the area that was covered by the
443 /// original rectangle — i.e. you want to cover the area
444 /// including all its corners — this function will make sure
445 /// that the size is recomputed taking into account the ceiling
446 /// of the coordinates of the bottom-right corner.
447 /// If the difference between the original coordinates and the
448 /// coordinates of the rounded rectangle is greater than the
449 /// difference between the original size and and the rounded
450 /// size, then the move of the origin would not be compensated
451 /// by a move in the anti-origin, leaving the corners of the
452 /// original rectangle outside the rounded one.
453 ///
454 /// # Returns
455 ///
456 ///
457 /// ## `res`
458 /// return location for the
459 /// rectangle with rounded extents
460 #[doc(alias = "graphene_rect_round_extents")]
461 #[must_use]
462 pub fn round_extents(&self) -> Rect {
463 unsafe {
464 let mut res = Rect::uninitialized();
465 ffi::graphene_rect_round_extents(self.to_glib_none().0, res.to_glib_none_mut().0);
466 res
467 }
468 }
469
470 /// Scales the size and origin of a rectangle horizontaly by `s_h`,
471 /// and vertically by `s_v`. The result `res` is normalized.
472 /// ## `s_h`
473 /// horizontal scale factor
474 /// ## `s_v`
475 /// vertical scale factor
476 ///
477 /// # Returns
478 ///
479 ///
480 /// ## `res`
481 /// return location for the
482 /// scaled rectangle
483 #[doc(alias = "graphene_rect_scale")]
484 #[must_use]
485 pub fn scale(&self, s_h: f32, s_v: f32) -> Rect {
486 unsafe {
487 let mut res = Rect::uninitialized();
488 ffi::graphene_rect_scale(self.to_glib_none().0, s_h, s_v, res.to_glib_none_mut().0);
489 res
490 }
491 }
492
493 /// Computes the union of the two given rectangles.
494 ///
495 /// 
496 ///
497 /// The union in the image above is the blue outline.
498 /// ## `b`
499 /// a [`Rect`][crate::Rect]
500 ///
501 /// # Returns
502 ///
503 ///
504 /// ## `res`
505 /// return location for a [`Rect`][crate::Rect]
506 #[doc(alias = "graphene_rect_union")]
507 #[must_use]
508 pub fn union(&self, b: &Rect) -> Rect {
509 unsafe {
510 let mut res = Rect::uninitialized();
511 ffi::graphene_rect_union(
512 self.to_glib_none().0,
513 b.to_glib_none().0,
514 res.to_glib_none_mut().0,
515 );
516 res
517 }
518 }
519
520 /// Returns a degenerate rectangle with origin fixed at (0, 0) and
521 /// a size of 0, 0.
522 ///
523 /// # Returns
524 ///
525 /// a fixed rectangle
526 #[doc(alias = "graphene_rect_zero")]
527 pub fn zero() -> Rect {
528 assert_initialized_main_thread!();
529 unsafe { from_glib_none(ffi::graphene_rect_zero()) }
530 }
531}
532
533impl PartialEq for Rect {
534 #[inline]
535 fn eq(&self, other: &Self) -> bool {
536 self.equal(other)
537 }
538}
539
540impl Eq for Rect {}