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
// Copyright 2013-2017, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

#[cfg(feature = "glib")]
use glib::translate::*;
use ffi::enums::RegionOverlap;
use RectangleInt;
use ffi;

use ffi::cairo_region_t;
use ffi::enums::Status;

pub struct Region(*mut cairo_region_t);

#[cfg(feature = "glib")]
#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_region_t> for &'a Region {
    type Storage = &'a Region;

    #[inline]
    fn to_glib_none(&self) -> Stash<'a, *mut ffi::cairo_region_t, &'a Region> {
        Stash(self.0, *self)
    }
}

#[cfg(feature = "glib")]
#[doc(hidden)]
impl<'a> ToGlibPtrMut<'a, *mut ffi::cairo_region_t> for Region {
    type Storage = &'a mut Self;

    #[inline]
    fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::cairo_region_t, Self> {
        StashMut(self.0, self)
    }
}

#[cfg(feature = "glib")]
#[doc(hidden)]
impl FromGlibPtrNone<*mut ffi::cairo_region_t> for Region {
    #[inline]
    unsafe fn from_glib_none(ptr: *mut ffi::cairo_region_t) -> Region {
        Self::from_raw_none(ptr)
    }
}

#[cfg(feature = "glib")]
#[doc(hidden)]
impl FromGlibPtrFull<*mut ffi::cairo_region_t> for Region {
    #[inline]
    unsafe fn from_glib_full(ptr: *mut ffi::cairo_region_t) -> Region {
        Self::from_raw_full(ptr)
    }
}

impl AsRef<Region> for Region {
    fn as_ref(&self) -> &Region {
        self
    }
}

impl Clone for Region {
    fn clone(&self) -> Region {
        unsafe { Self::from_raw_none(self.to_raw_none()) }
    }
}

impl Drop for Region {
    fn drop(&mut self) {
        unsafe { ffi::cairo_region_destroy(self.0); }
    }
}

impl PartialEq for Region {
    fn eq(&self, other: &Region) -> bool {
        unsafe {
            ffi::cairo_region_equal(self.0, other.0).as_bool()
        }
    }
}

impl Region {
    #[inline]
    unsafe fn from_raw_none(ptr: *mut ffi::cairo_region_t) -> Region {
        assert!(!ptr.is_null());
        ffi::cairo_region_reference(ptr);
        Region(ptr)
    }

    #[inline]
    unsafe fn from_raw_full(ptr: *mut ffi::cairo_region_t) -> Region {
        assert!(!ptr.is_null());
        Region(ptr)
    }

    #[doc(hidden)]
    pub fn to_raw_none(&self) -> *mut ffi::cairo_region_t {
        self.0
    }

    pub fn create() -> Region {
        unsafe {
            Self::from_raw_full(ffi::cairo_region_create())
        }
    }

    pub fn create_rectangle(rectangle: &RectangleInt) -> Region {
        unsafe {
            Self::from_raw_full(ffi::cairo_region_create_rectangle(rectangle.to_raw_none()))
        }
    }

    // pub fn create_rectangles(rectangle: &[&RectangleInt]) -> Region {
    //     unsafe {
    //         Self::from_raw_full(ffi::cairo_region_create_rectangles(rectangle.to_raw_none()))
    //     }
    // }

    pub fn copy(&self) -> Region {
        unsafe {
            Self::from_raw_full(ffi::cairo_region_copy(self.0))
        }
    }

    pub fn status(&self) -> Status {
        unsafe {
            ffi::cairo_region_status(self.0)
        }
    }

    pub fn get_extents(&self, rectangle: &mut RectangleInt) {
        unsafe {
            ffi::cairo_region_get_extents(self.0, rectangle.to_raw_none())
        }
    }

    pub fn num_rectangles(&self) -> i32 {
        unsafe {
            ffi::cairo_region_num_rectangles(self.0)
        }
    }

    pub fn get_rectangle(&self, nth: i32) -> RectangleInt {
        unsafe {
            let rectangle: RectangleInt = ::std::mem::zeroed();
            ffi::cairo_region_get_rectangle(self.0, nth, rectangle.to_raw_none());
            rectangle
        }
    }

    pub fn is_empty(&self) -> bool {
        unsafe {
            ffi::cairo_region_is_empty(self.0).as_bool()
        }
    }

    pub fn contains_point(&self, x: i32, y: i32) -> bool {
        unsafe {
            ffi::cairo_region_contains_point(self.0, x, y).as_bool()
        }
    }

    pub fn contains_rectangle(&self, rectangle: &RectangleInt) -> RegionOverlap {
        unsafe {
            ffi::cairo_region_contains_rectangle(self.0, rectangle.to_raw_none())
        }
    }

    pub fn translate(&self, dx: i32, dy: i32) {
        unsafe {
            ffi::cairo_region_translate(self.0, dx, dy)
        }
    }

    pub fn intersect(&self, other: &Region) -> Status {
        unsafe {
            ffi::cairo_region_intersect(self.0, other.0)
        }
    }

    pub fn intersect_rectangle(&self, rectangle: &RectangleInt) -> Status {
        unsafe {
            ffi::cairo_region_intersect_rectangle(self.0, rectangle.to_raw_none())
        }
    }

    pub fn subtract(&self, other: &Region) -> Status {
        unsafe {
            ffi::cairo_region_subtract(self.0, other.0)
        }
    }

    pub fn subtract_rectangle(&self, rectangle: &RectangleInt) -> Status {
        unsafe {
            ffi::cairo_region_subtract_rectangle(self.0, rectangle.to_raw_none())
        }
    }

    pub fn union(&self, other: &Region) -> Status {
        unsafe {
            ffi::cairo_region_union(self.0, other.0)
        }
    }

    pub fn union_rectangle(&self, rectangle: &RectangleInt) -> Status {
        unsafe {
            ffi::cairo_region_union_rectangle(self.0, rectangle.to_raw_none())
        }
    }

    pub fn xor(&self, other: &Region) -> Status {
        unsafe {
            ffi::cairo_region_xor(self.0, other.0)
        }
    }

    pub fn xor_rectangle(&self, rectangle: &RectangleInt) -> Status {
        unsafe {
            ffi::cairo_region_xor_rectangle(self.0, rectangle.to_raw_none())
        }
    }
}