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
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::Cursor;
use crate::EventMask;
use crate::Visual;
use crate::Window;
use cairo::{self, Surface};
use glib::object::IsA;
use glib::translate::*;
use libc::{c_char, c_int};
use std::ptr;

use crate::{WindowType, WindowTypeHint, WindowWindowClass};

pub struct WindowAttr {
    pub title: Option<String>,
    pub event_mask: EventMask,
    pub x: Option<i32>,
    pub y: Option<i32>,
    pub width: i32,
    pub height: i32,
    pub wclass: WindowWindowClass,
    pub visual: Option<Visual>,
    pub window_type: WindowType,
    pub cursor: Option<Cursor>,
    pub override_redirect: bool,
    pub type_hint: Option<WindowTypeHint>,
}

impl Default for WindowAttr {
    fn default() -> Self {
        skip_assert_initialized!();
        Self {
            title: None,
            event_mask: EventMask::empty(),
            x: None,
            y: None,
            width: 400,
            height: 300,
            wclass: WindowWindowClass::InputOutput,
            visual: None,
            window_type: WindowType::Toplevel,
            cursor: None,
            override_redirect: false,
            type_hint: None,
        }
    }
}

impl WindowAttr {
    #[doc(alias = "get_mask")]
    fn mask(&self) -> u32 {
        let mut mask: ffi::GdkWindowAttributesType = 0;
        if self.title.is_some() {
            mask |= ffi::GDK_WA_TITLE;
        }
        if self.x.is_some() {
            mask |= ffi::GDK_WA_X;
        }
        if self.y.is_some() {
            mask |= ffi::GDK_WA_Y;
        }
        if self.cursor.is_some() {
            mask |= ffi::GDK_WA_CURSOR;
        }
        if self.visual.is_some() {
            mask |= ffi::GDK_WA_VISUAL;
        }
        if self.override_redirect {
            mask |= ffi::GDK_WA_NOREDIR;
        }
        if self.type_hint.is_some() {
            mask |= ffi::GDK_WA_TYPE_HINT;
        }
        mask
    }
}

#[allow(clippy::type_complexity)]
impl<'a> ToGlibPtr<'a, *mut ffi::GdkWindowAttr> for WindowAttr {
    type Storage = (
        Box<ffi::GdkWindowAttr>,
        Stash<'a, *mut ffi::GdkVisual, Option<Visual>>,
        Stash<'a, *mut ffi::GdkCursor, Option<Cursor>>,
        Stash<'a, *const c_char, Option<String>>,
    );

    fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::GdkWindowAttr, Self> {
        let title = self.title.to_glib_none();
        let visual = self.visual.to_glib_none();
        let cursor = self.cursor.to_glib_none();

        let mut attrs = Box::new(ffi::GdkWindowAttr {
            title: title.0 as *mut c_char,
            event_mask: self.event_mask.bits() as i32,
            x: self.x.unwrap_or(0),
            y: self.y.unwrap_or(0),
            width: self.width,
            height: self.height,
            wclass: self.wclass.into_glib(),
            visual: visual.0,
            window_type: self.window_type.into_glib(),
            cursor: cursor.0,
            wmclass_name: ptr::null_mut(),
            wmclass_class: ptr::null_mut(),
            override_redirect: self.override_redirect.into_glib(),
            type_hint: self.type_hint.unwrap_or(WindowTypeHint::Normal).into_glib(),
        });

        Stash(&mut *attrs, (attrs, visual, cursor, title))
    }
}

impl Window {
    /// Creates a new [`Window`][crate::Window] using the attributes from
    /// `attributes`. See `GdkWindowAttr` and `GdkWindowAttributesType` for
    /// more details. Note: to use this on displays other than the default
    /// display, `parent` must be specified.
    /// ## `parent`
    /// a [`Window`][crate::Window], or [`None`] to create the window as a child of
    ///  the default root window for the default display.
    /// ## `attributes`
    /// attributes of the new window
    /// ## `attributes_mask`
    /// mask indicating which
    ///  fields in `attributes` are valid
    ///
    /// # Returns
    ///
    /// the new [`Window`][crate::Window]
    #[doc(alias = "gdk_window_new")]
    pub fn new(parent: Option<&Window>, attributes: &WindowAttr) -> Window {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::gdk_window_new(
                parent.to_glib_none().0,
                attributes.to_glib_none().0,
                attributes.mask() as c_int,
            ))
        }
    }

    /// Create a new surface that is as compatible as possible with the
    /// given `self`. For example the new surface will have the same
    /// fallback resolution and font options as `self`. Generally, the new
    /// surface will also use the same backend as `self`, unless that is
    /// not possible for some reason. The type of the returned surface may
    /// be examined with `cairo_surface_get_type()`.
    ///
    /// Initially the surface contents are all 0 (transparent if contents
    /// have transparency, black otherwise.)
    /// ## `content`
    /// the content for the new surface
    /// ## `width`
    /// width of the new surface
    /// ## `height`
    /// height of the new surface
    ///
    /// # Returns
    ///
    /// a pointer to the newly allocated surface. The caller
    /// owns the surface and should call `cairo_surface_destroy()` when done
    /// with it.
    ///
    /// This function always returns a valid pointer, but it will return a
    /// pointer to a “nil” surface if `other` is already in an error state
    /// or any other error occurs.
    #[doc(alias = "gdk_window_create_similar_surface")]
    pub fn create_similar_surface(
        &self,
        content: cairo::Content,
        width: i32,
        height: i32,
    ) -> Option<Surface> {
        unsafe {
            from_glib_full(ffi::gdk_window_create_similar_surface(
                self.to_glib_none().0,
                content.into(),
                width,
                height,
            ))
        }
    }

    /// Create a new image surface that is efficient to draw on the
    /// given `self`.
    ///
    /// Initially the surface contents are all 0 (transparent if contents
    /// have transparency, black otherwise.)
    ///
    /// The `width` and `height` of the new surface are not affected by
    /// the scaling factor of the `self`, or by the `scale` argument; they
    /// are the size of the surface in device pixels. If you wish to create
    /// an image surface capable of holding the contents of `self` you can
    /// use:
    ///
    ///
    ///
    /// **⚠️ The following code is in C ⚠️**
    ///
    /// ```C
    ///   int scale = gdk_window_get_scale_factor (window);
    ///   int width = gdk_window_get_width (window) * scale;
    ///   int height = gdk_window_get_height (window) * scale;
    ///
    ///   // format is set elsewhere
    ///   cairo_surface_t *surface =
    ///     gdk_window_create_similar_image_surface (window,
    ///                                              format,
    ///                                              width, height,
    ///                                              scale);
    /// ```
    ///
    /// Note that unlike `cairo_surface_create_similar_image()`, the new
    /// surface's device scale is set to `scale`, or to the scale factor of
    /// `self` if `scale` is 0.
    /// ## `format`
    /// the format for the new surface
    /// ## `width`
    /// width of the new surface
    /// ## `height`
    /// height of the new surface
    /// ## `scale`
    /// the scale of the new surface, or 0 to use same as `self`
    ///
    /// # Returns
    ///
    /// a pointer to the newly allocated surface. The caller
    /// owns the surface and should call `cairo_surface_destroy()` when done
    /// with it.
    ///
    /// This function always returns a valid pointer, but it will return a
    /// pointer to a “nil” surface if `other` is already in an error state
    /// or any other error occurs.
    #[doc(alias = "gdk_window_create_similar_image_surface")]
    pub fn create_similar_image_surface(
        &self,
        format: cairo::Format,
        width: i32,
        height: i32,
        scale: i32,
    ) -> Option<cairo::Surface> {
        unsafe {
            from_glib_full(ffi::gdk_window_create_similar_image_surface(
                self.to_glib_none().0,
                format.into(),
                width,
                height,
                scale,
            ))
        }
    }
}

pub trait WindowExtManual: 'static {
    #[doc(alias = "gdk_window_set_user_data")]
    unsafe fn set_user_data<T>(&self, user_data: &mut T);

    #[allow(clippy::mut_from_ref)]
    #[doc(alias = "gdk_window_get_user_data")]
    #[doc(alias = "get_user_data")]
    unsafe fn user_data<T>(&self) -> &mut T;

    #[doc(alias = "gdk_get_default_root_window")]
    #[doc(alias = "get_default_root_window")]
    fn default_root_window() -> Window;

    #[doc(alias = "gdk_offscreen_window_set_embedder")]
    fn offscreen_window_set_embedder(&self, embedder: &Window);

    #[doc(alias = "gdk_offscreen_window_get_embedder")]
    fn offscreen_window_get_embedder(&self) -> Option<Window>;

    #[doc(alias = "gdk_offscreen_window_get_surface")]
    fn offscreen_window_get_surface(&self) -> Option<Surface>;

    #[doc(alias = "gdk_pixbuf_get_from_window")]
    #[doc(alias = "get_pixbuf")]
    fn pixbuf(&self, src_x: i32, src_y: i32, width: i32, height: i32)
        -> Option<gdk_pixbuf::Pixbuf>;

    #[doc(alias = "gdk_window_get_background_pattern")]
    #[doc(alias = "get_background_pattern")]
    fn background_pattern(&self) -> Option<cairo::Pattern>;

    #[doc(alias = "gdk_window_set_background_pattern")]
    fn set_background_pattern(&self, pattern: Option<&cairo::Pattern>);
}

impl<O: IsA<Window>> WindowExtManual for O {
    unsafe fn set_user_data<T>(&self, user_data: &mut T) {
        ffi::gdk_window_set_user_data(
            self.as_ref().to_glib_none().0,
            user_data as *mut T as *mut _,
        )
    }

    unsafe fn user_data<T>(&self) -> &mut T {
        let mut pointer = ::std::ptr::null_mut();
        ffi::gdk_window_get_user_data(self.as_ref().to_glib_none().0, &mut pointer);
        &mut *(pointer as *mut T)
    }

    fn default_root_window() -> Window {
        assert_initialized_main_thread!();
        unsafe { from_glib_none(ffi::gdk_get_default_root_window()) }
    }

    fn offscreen_window_set_embedder(&self, embedder: &Window) {
        unsafe {
            ffi::gdk_offscreen_window_set_embedder(
                self.as_ref().to_glib_none().0,
                embedder.to_glib_none().0,
            )
        }
    }

    fn offscreen_window_get_embedder(&self) -> Option<Window> {
        unsafe {
            from_glib_none(ffi::gdk_offscreen_window_get_embedder(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn offscreen_window_get_surface(&self) -> Option<Surface> {
        skip_assert_initialized!();
        unsafe {
            from_glib_none(ffi::gdk_offscreen_window_get_surface(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn pixbuf(
        &self,
        src_x: i32,
        src_y: i32,
        width: i32,
        height: i32,
    ) -> Option<gdk_pixbuf::Pixbuf> {
        skip_assert_initialized!();
        unsafe {
            from_glib_full(ffi::gdk_pixbuf_get_from_window(
                self.as_ref().to_glib_none().0,
                src_x,
                src_y,
                width,
                height,
            ))
        }
    }

    fn background_pattern(&self) -> Option<cairo::Pattern> {
        unsafe {
            let ret = ffi::gdk_window_get_background_pattern(self.as_ref().to_glib_none().0);
            if ret.is_null() {
                None
            } else {
                Some(cairo::Pattern::from_raw_none(ret))
            }
        }
    }

    fn set_background_pattern(&self, pattern: Option<&cairo::Pattern>) {
        unsafe {
            let ptr = if let Some(pattern) = pattern {
                pattern.to_raw_none()
            } else {
                ::std::ptr::null_mut()
            };
            ffi::gdk_window_set_background_pattern(self.as_ref().to_glib_none().0, ptr);
        }
    }
}