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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::Point;
use glib::translate::*;

glib::wrapper! {
    /// The location and size of a rectangle region.
    ///
    /// The width and height of a [`Rect`][crate::Rect] can be negative; for instance,
    /// a [`Rect`][crate::Rect] with an origin of [ 0, 0 ] and a size of [ 10, 10 ] is
    /// equivalent to a [`Rect`][crate::Rect] with an origin of [ 10, 10 ] and a size
    /// of [ -10, -10 ].
    ///
    /// Application code can normalize rectangles using [`normalize()`][Self::normalize()];
    /// this function will ensure that the width and height of a rectangle are
    /// positive values. All functions taking a [`Rect`][crate::Rect] as an argument
    /// will internally operate on a normalized copy; all functions returning a
    /// [`Rect`][crate::Rect] will always return a normalized rectangle.
    #[derive(Debug, PartialOrd, Ord, Hash)]
    pub struct Rect(Boxed<ffi::graphene_rect_t>);

    match fn {
        copy => |ptr| glib::gobject_ffi::g_boxed_copy(ffi::graphene_rect_get_type(), ptr as *mut _) as *mut ffi::graphene_rect_t,
        free => |ptr| glib::gobject_ffi::g_boxed_free(ffi::graphene_rect_get_type(), ptr as *mut _),
        init => |_ptr| (),
        clear => |_ptr| (),
        type_ => || ffi::graphene_rect_get_type(),
    }
}

impl Rect {
    /// Checks whether a [`Rect`][crate::Rect] contains the given coordinates.
    /// ## `p`
    /// a [`Point`][crate::Point]
    ///
    /// # Returns
    ///
    /// `true` if the rectangle contains the point
    #[doc(alias = "graphene_rect_contains_point")]
    pub fn contains_point(&self, p: &Point) -> bool {
        unsafe {
            from_glib(ffi::graphene_rect_contains_point(
                self.to_glib_none().0,
                p.to_glib_none().0,
            ))
        }
    }

    /// Checks whether a [`Rect`][crate::Rect] fully contains the given
    /// rectangle.
    /// ## `b`
    /// a [`Rect`][crate::Rect]
    ///
    /// # Returns
    ///
    /// `true` if the rectangle `self` fully contains `b`
    #[doc(alias = "graphene_rect_contains_rect")]
    pub fn contains_rect(&self, b: &Rect) -> bool {
        unsafe {
            from_glib(ffi::graphene_rect_contains_rect(
                self.to_glib_none().0,
                b.to_glib_none().0,
            ))
        }
    }

    /// Checks whether the two given rectangle are equal.
    /// ## `b`
    /// a [`Rect`][crate::Rect]
    ///
    /// # Returns
    ///
    /// `true` if the rectangles are equal
    #[doc(alias = "graphene_rect_equal")]
    fn equal(&self, b: &Rect) -> bool {
        unsafe {
            from_glib(ffi::graphene_rect_equal(
                self.to_glib_none().0,
                b.to_glib_none().0,
            ))
        }
    }

    /// Expands a [`Rect`][crate::Rect] to contain the given [`Point`][crate::Point].
    /// ## `p`
    /// a [`Point`][crate::Point]
    ///
    /// # Returns
    ///
    ///
    /// ## `res`
    /// return location for the expanded rectangle
    #[doc(alias = "graphene_rect_expand")]
    pub fn expand(&self, p: &Point) -> Rect {
        unsafe {
            let mut res = Rect::uninitialized();
            ffi::graphene_rect_expand(
                self.to_glib_none().0,
                p.to_glib_none().0,
                res.to_glib_none_mut().0,
            );
            res
        }
    }

    /// Compute the area of given normalized rectangle.
    ///
    /// # Returns
    ///
    /// the area of the normalized rectangle
    #[doc(alias = "graphene_rect_get_area")]
    #[doc(alias = "get_area")]
    pub fn area(&self) -> f32 {
        unsafe { ffi::graphene_rect_get_area(self.to_glib_none().0) }
    }

    /// Retrieves the coordinates of the bottom-left corner of the given rectangle.
    ///
    /// # Returns
    ///
    ///
    /// ## `p`
    /// return location for a [`Point`][crate::Point]
    #[doc(alias = "graphene_rect_get_bottom_left")]
    #[doc(alias = "get_bottom_left")]
    pub fn bottom_left(&self) -> Point {
        unsafe {
            let mut p = Point::uninitialized();
            ffi::graphene_rect_get_bottom_left(self.to_glib_none().0, p.to_glib_none_mut().0);
            p
        }
    }

    /// Retrieves the coordinates of the bottom-right corner of the given rectangle.
    ///
    /// # Returns
    ///
    ///
    /// ## `p`
    /// return location for a [`Point`][crate::Point]
    #[doc(alias = "graphene_rect_get_bottom_right")]
    #[doc(alias = "get_bottom_right")]
    pub fn bottom_right(&self) -> Point {
        unsafe {
            let mut p = Point::uninitialized();
            ffi::graphene_rect_get_bottom_right(self.to_glib_none().0, p.to_glib_none_mut().0);
            p
        }
    }

    /// Retrieves the coordinates of the center of the given rectangle.
    ///
    /// # Returns
    ///
    ///
    /// ## `p`
    /// return location for a [`Point`][crate::Point]
    #[doc(alias = "graphene_rect_get_center")]
    #[doc(alias = "get_center")]
    pub fn center(&self) -> Point {
        unsafe {
            let mut p = Point::uninitialized();
            ffi::graphene_rect_get_center(self.to_glib_none().0, p.to_glib_none_mut().0);
            p
        }
    }

    /// Retrieves the normalized height of the given rectangle.
    ///
    /// # Returns
    ///
    /// the normalized height of the rectangle
    #[doc(alias = "graphene_rect_get_height")]
    #[doc(alias = "get_height")]
    pub fn height(&self) -> f32 {
        unsafe { ffi::graphene_rect_get_height(self.to_glib_none().0) }
    }

    /// Retrieves the coordinates of the top-left corner of the given rectangle.
    ///
    /// # Returns
    ///
    ///
    /// ## `p`
    /// return location for a [`Point`][crate::Point]
    #[doc(alias = "graphene_rect_get_top_left")]
    #[doc(alias = "get_top_left")]
    pub fn top_left(&self) -> Point {
        unsafe {
            let mut p = Point::uninitialized();
            ffi::graphene_rect_get_top_left(self.to_glib_none().0, p.to_glib_none_mut().0);
            p
        }
    }

    /// Retrieves the coordinates of the top-right corner of the given rectangle.
    ///
    /// # Returns
    ///
    ///
    /// ## `p`
    /// return location for a [`Point`][crate::Point]
    #[doc(alias = "graphene_rect_get_top_right")]
    #[doc(alias = "get_top_right")]
    pub fn top_right(&self) -> Point {
        unsafe {
            let mut p = Point::uninitialized();
            ffi::graphene_rect_get_top_right(self.to_glib_none().0, p.to_glib_none_mut().0);
            p
        }
    }

    //#[doc(alias = "graphene_rect_get_vertices")]
    //#[doc(alias = "get_vertices")]
    //pub fn vertices(&self, vertices: /*Unimplemented*/FixedArray TypeId { ns_id: 1, id: 16 }; 4) {
    //    unsafe { TODO: call ffi:graphene_rect_get_vertices() }
    //}

    /// Retrieves the normalized width of the given rectangle.
    ///
    /// # Returns
    ///
    /// the normalized width of the rectangle
    #[doc(alias = "graphene_rect_get_width")]
    #[doc(alias = "get_width")]
    pub fn width(&self) -> f32 {
        unsafe { ffi::graphene_rect_get_width(self.to_glib_none().0) }
    }

    /// Retrieves the normalized X coordinate of the origin of the given
    /// rectangle.
    ///
    /// # Returns
    ///
    /// the normalized X coordinate of the rectangle
    #[doc(alias = "graphene_rect_get_x")]
    #[doc(alias = "get_x")]
    pub fn x(&self) -> f32 {
        unsafe { ffi::graphene_rect_get_x(self.to_glib_none().0) }
    }

    /// Retrieves the normalized Y coordinate of the origin of the given
    /// rectangle.
    ///
    /// # Returns
    ///
    /// the normalized Y coordinate of the rectangle
    #[doc(alias = "graphene_rect_get_y")]
    #[doc(alias = "get_y")]
    pub fn y(&self) -> f32 {
        unsafe { ffi::graphene_rect_get_y(self.to_glib_none().0) }
    }

    /// Initializes the given [`Rect`][crate::Rect] with the given values.
    ///
    /// This function will implicitly normalize the [`Rect`][crate::Rect]
    /// before returning.
    /// ## `x`
    /// the X coordinate of the [`Rect`][crate::Rect]
    /// ## `y`
    /// the Y coordinate of the [`Rect`][crate::Rect]
    /// ## `width`
    /// the width of the [`Rect`][crate::Rect]
    /// ## `height`
    /// the height of the [`Rect`][crate::Rect]
    ///
    /// # Returns
    ///
    /// the initialized rectangle
    #[doc(alias = "graphene_rect_init")]
    pub fn init(&mut self, x: f32, y: f32, width: f32, height: f32) {
        unsafe {
            ffi::graphene_rect_init(self.to_glib_none_mut().0, x, y, width, height);
        }
    }

    /// Initializes `self` using the given `src` rectangle.
    ///
    /// This function will implicitly normalize the [`Rect`][crate::Rect]
    /// before returning.
    /// ## `src`
    /// a [`Rect`][crate::Rect]
    ///
    /// # Returns
    ///
    /// the initialized rectangle
    #[doc(alias = "graphene_rect_init_from_rect")]
    pub fn init_from_rect(&mut self, src: &Rect) {
        unsafe {
            ffi::graphene_rect_init_from_rect(self.to_glib_none_mut().0, src.to_glib_none().0);
        }
    }

    /// Changes the given rectangle to be smaller, or larger depending on the
    /// given inset parameters.
    ///
    /// To create an inset rectangle, use positive `d_x` or `d_y` values; to
    /// create a larger, encompassing rectangle, use negative `d_x` or `d_y`
    /// values.
    ///
    /// The origin of the rectangle is offset by `d_x` and `d_y`, while the size
    /// is adjusted by `(2 * `d_x`, 2 * `d_y`)`. If `d_x` and `d_y` are positive
    /// values, the size of the rectangle is decreased; if `d_x` and `d_y` are
    /// negative values, the size of the rectangle is increased.
    ///
    /// If the size of the resulting inset rectangle has a negative width or
    /// height then the size will be set to zero.
    /// ## `d_x`
    /// the horizontal inset
    /// ## `d_y`
    /// the vertical inset
    ///
    /// # Returns
    ///
    /// the inset rectangle
    #[doc(alias = "graphene_rect_inset")]
    pub fn inset(&mut self, d_x: f32, d_y: f32) -> Option<Rect> {
        unsafe {
            from_glib_none(ffi::graphene_rect_inset(
                self.to_glib_none_mut().0,
                d_x,
                d_y,
            ))
        }
    }

    /// Changes the given rectangle to be smaller, or larger depending on the
    /// given inset parameters.
    ///
    /// To create an inset rectangle, use positive `d_x` or `d_y` values; to
    /// create a larger, encompassing rectangle, use negative `d_x` or `d_y`
    /// values.
    ///
    /// The origin of the rectangle is offset by `d_x` and `d_y`, while the size
    /// is adjusted by `(2 * `d_x`, 2 * `d_y`)`. If `d_x` and `d_y` are positive
    /// values, the size of the rectangle is decreased; if `d_x` and `d_y` are
    /// negative values, the size of the rectangle is increased.
    ///
    /// If the size of the resulting inset rectangle has a negative width or
    /// height then the size will be set to zero.
    /// ## `d_x`
    /// the horizontal inset
    /// ## `d_y`
    /// the vertical inset
    ///
    /// # Returns
    ///
    ///
    /// ## `res`
    /// return location for the inset rectangle
    #[doc(alias = "graphene_rect_inset_r")]
    pub fn inset_r(&self, d_x: f32, d_y: f32) -> Rect {
        unsafe {
            let mut res = Rect::uninitialized();
            ffi::graphene_rect_inset_r(self.to_glib_none().0, d_x, d_y, res.to_glib_none_mut().0);
            res
        }
    }

    /// Linearly interpolates the origin and size of the two given
    /// rectangles.
    /// ## `b`
    /// a [`Rect`][crate::Rect]
    /// ## `factor`
    /// the linear interpolation factor
    ///
    /// # Returns
    ///
    ///
    /// ## `res`
    /// return location for the
    ///  interpolated rectangle
    #[doc(alias = "graphene_rect_interpolate")]
    pub fn interpolate(&self, b: &Rect, factor: f64) -> Rect {
        unsafe {
            let mut res = Rect::uninitialized();
            ffi::graphene_rect_interpolate(
                self.to_glib_none().0,
                b.to_glib_none().0,
                factor,
                res.to_glib_none_mut().0,
            );
            res
        }
    }

    /// Computes the intersection of the two given rectangles.
    ///
    /// ![](rectangle-intersection.png)
    ///
    /// The intersection in the image above is the blue outline.
    ///
    /// If the two rectangles do not intersect, `res` will contain
    /// a degenerate rectangle with origin in (0, 0) and a size of 0.
    /// ## `b`
    /// a [`Rect`][crate::Rect]
    ///
    /// # Returns
    ///
    /// `true` if the two rectangles intersect
    ///
    /// ## `res`
    /// return location for
    ///  a [`Rect`][crate::Rect]
    #[doc(alias = "graphene_rect_intersection")]
    pub fn intersection(&self, b: &Rect) -> Option<Rect> {
        unsafe {
            let mut res = Rect::uninitialized();
            let ret = from_glib(ffi::graphene_rect_intersection(
                self.to_glib_none().0,
                b.to_glib_none().0,
                res.to_glib_none_mut().0,
            ));
            if ret {
                Some(res)
            } else {
                None
            }
        }
    }

    /// Normalizes the passed rectangle.
    ///
    /// This function ensures that the size of the rectangle is made of
    /// positive values, and that the origin is the top-left corner of
    /// the rectangle.
    ///
    /// # Returns
    ///
    /// the normalized rectangle
    #[doc(alias = "graphene_rect_normalize")]
    pub fn normalize(&mut self) -> Option<Rect> {
        unsafe { from_glib_none(ffi::graphene_rect_normalize(self.to_glib_none_mut().0)) }
    }

    /// Normalizes the passed rectangle.
    ///
    /// This function ensures that the size of the rectangle is made of
    /// positive values, and that the origin is in the top-left corner
    /// of the rectangle.
    ///
    /// # Returns
    ///
    ///
    /// ## `res`
    /// the return location for the
    ///  normalized rectangle
    #[doc(alias = "graphene_rect_normalize_r")]
    pub fn normalize_r(&self) -> Rect {
        unsafe {
            let mut res = Rect::uninitialized();
            ffi::graphene_rect_normalize_r(self.to_glib_none().0, res.to_glib_none_mut().0);
            res
        }
    }

    /// Offsets the origin by `d_x` and `d_y`.
    ///
    /// The size of the rectangle is unchanged.
    /// ## `d_x`
    /// the horizontal offset
    /// ## `d_y`
    /// the vertical offset
    ///
    /// # Returns
    ///
    /// the offset rectangle
    #[doc(alias = "graphene_rect_offset")]
    pub fn offset(&mut self, d_x: f32, d_y: f32) -> Option<Rect> {
        unsafe {
            from_glib_none(ffi::graphene_rect_offset(
                self.to_glib_none_mut().0,
                d_x,
                d_y,
            ))
        }
    }

    /// Offsets the origin of the given rectangle by `d_x` and `d_y`.
    ///
    /// The size of the rectangle is left unchanged.
    /// ## `d_x`
    /// the horizontal offset
    /// ## `d_y`
    /// the vertical offset
    ///
    /// # Returns
    ///
    ///
    /// ## `res`
    /// return location for the offset
    ///  rectangle
    #[doc(alias = "graphene_rect_offset_r")]
    pub fn offset_r(&self, d_x: f32, d_y: f32) -> Rect {
        unsafe {
            let mut res = Rect::uninitialized();
            ffi::graphene_rect_offset_r(self.to_glib_none().0, d_x, d_y, res.to_glib_none_mut().0);
            res
        }
    }

    /// Rounds the origin of the given rectangle to its nearest
    /// integer value and and recompute the size so that the
    /// rectangle is large enough to contain all the conrners
    /// of the original rectangle.
    ///
    /// This function is the equivalent of calling `floor` on
    /// the coordinates of the origin, and recomputing the size
    /// calling `ceil` on the bottom-right coordinates.
    ///
    /// If you want to be sure that the rounded rectangle
    /// completely covers the area that was covered by the
    /// original rectangle — i.e. you want to cover the area
    /// including all its corners — this function will make sure
    /// that the size is recomputed taking into account the ceiling
    /// of the coordinates of the bottom-right corner.
    /// If the difference between the original coordinates and the
    /// coordinates of the rounded rectangle is greater than the
    /// difference between the original size and and the rounded
    /// size, then the move of the origin would not be compensated
    /// by a move in the anti-origin, leaving the corners of the
    /// original rectangle outside the rounded one.
    ///
    /// # Returns
    ///
    ///
    /// ## `res`
    /// return location for the
    ///  rectangle with rounded extents
    #[doc(alias = "graphene_rect_round_extents")]
    pub fn round_extents(&self) -> Rect {
        unsafe {
            let mut res = Rect::uninitialized();
            ffi::graphene_rect_round_extents(self.to_glib_none().0, res.to_glib_none_mut().0);
            res
        }
    }

    /// Scales the size and origin of a rectangle horizontaly by `s_h`,
    /// and vertically by `s_v`. The result `res` is normalized.
    /// ## `s_h`
    /// horizontal scale factor
    /// ## `s_v`
    /// vertical scale factor
    ///
    /// # Returns
    ///
    ///
    /// ## `res`
    /// return location for the
    ///  scaled rectangle
    #[doc(alias = "graphene_rect_scale")]
    pub fn scale(&self, s_h: f32, s_v: f32) -> Rect {
        unsafe {
            let mut res = Rect::uninitialized();
            ffi::graphene_rect_scale(self.to_glib_none().0, s_h, s_v, res.to_glib_none_mut().0);
            res
        }
    }

    /// Computes the union of the two given rectangles.
    ///
    /// ![](rectangle-union.png)
    ///
    /// The union in the image above is the blue outline.
    /// ## `b`
    /// a [`Rect`][crate::Rect]
    ///
    /// # Returns
    ///
    ///
    /// ## `res`
    /// return location for a [`Rect`][crate::Rect]
    #[doc(alias = "graphene_rect_union")]
    pub fn union(&self, b: &Rect) -> Rect {
        unsafe {
            let mut res = Rect::uninitialized();
            ffi::graphene_rect_union(
                self.to_glib_none().0,
                b.to_glib_none().0,
                res.to_glib_none_mut().0,
            );
            res
        }
    }

    /// Returns a degenerate rectangle with origin fixed at (0, 0) and
    /// a size of 0, 0.
    ///
    /// # Returns
    ///
    /// a fixed rectangle
    #[doc(alias = "graphene_rect_zero")]
    pub fn zero() -> Rect {
        assert_initialized_main_thread!();
        unsafe { from_glib_none(ffi::graphene_rect_zero()) }
    }
}

impl PartialEq for Rect {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.equal(other)
    }
}

impl Eq for Rect {}