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
use crate::{Actor, GridPosition, LayoutManager, Orientation};
use glib::{
    object as gobject,
    object::{Cast, IsA},
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;
use std::{fmt, mem::transmute};

glib_wrapper! {
    pub struct GridLayout(Object<ffi::ClutterGridLayout, ffi::ClutterGridLayoutClass, GridLayoutClass>) @extends LayoutManager, gobject::InitiallyUnowned;

    match fn {
        get_type => || ffi::clutter_grid_layout_get_type(),
    }
}

impl GridLayout {
    /// Creates a new `GridLayout`
    ///
    /// # Returns
    ///
    /// the new `GridLayout`
    pub fn new() -> GridLayout {
        unsafe { LayoutManager::from_glib_none(ffi::clutter_grid_layout_new()).unsafe_cast() }
    }
}

impl Default for GridLayout {
    fn default() -> Self {
        Self::new()
    }
}

/// Trait containing all `GridLayout` methods.
///
/// # Implementors
///
/// [`GridLayout`](struct.GridLayout.html)
pub trait GridLayoutExt: 'static {
    /// Adds a widget to the grid.
    ///
    /// The position of `child` is determined by `left` and `top`. The
    /// number of 'cells' that `child` will occupy is determined by
    /// `width` and `height`.
    /// ## `child`
    /// the `Actor` to add
    /// ## `left`
    /// the column number to attach the left side of `child` to
    /// ## `top`
    /// the row number to attach the top side of `child` to
    /// ## `width`
    /// the number of columns that `child` will span
    /// ## `height`
    /// the number of rows that `child` will span
    fn attach<P: IsA<Actor>>(&self, child: &P, left: i32, top: i32, width: i32, height: i32);

    /// Adds a actor to the grid.
    ///
    /// The actor is placed next to `sibling`, on the side determined by
    /// `side`. When `sibling` is `None`, the actor is placed in row (for
    /// left or right placement) or column 0 (for top or bottom placement),
    /// at the end indicated by `side`.
    ///
    /// Attaching widgets labeled [1], [2], [3] with `sibling` == `None` and
    /// `side` == `GridPosition::Left` yields a layout of [3][2][1].
    /// ## `child`
    /// the actor to add
    /// ## `sibling`
    /// the child of `self` that `child` will be placed
    ///  next to, or `None` to place `child` at the beginning or end
    /// ## `side`
    /// the side of `sibling` that `child` is positioned next to
    /// ## `width`
    /// the number of columns that `child` will span
    /// ## `height`
    /// the number of rows that `child` will span
    fn attach_next_to<P: IsA<Actor>, Q: IsA<Actor>>(
        &self,
        child: &P,
        sibling: Option<&Q>,
        side: GridPosition,
        width: i32,
        height: i32,
    );

    /// Gets the child of `self` whose area covers the grid
    /// cell whose upper left corner is at `left`, `top`.
    /// ## `left`
    /// the left edge of the cell
    /// ## `top`
    /// the top edge of the cell
    ///
    /// # Returns
    ///
    /// the child at the given position, or `None`
    fn get_child_at(&self, left: i32, top: i32) -> Option<Actor>;

    /// Returns whether all columns of `self` have the same width.
    ///
    /// # Returns
    ///
    /// whether all columns of `self` have the same width.
    fn get_column_homogeneous(&self) -> bool;

    /// Retrieves the spacing set using `GridLayoutExt::set_column_spacing`
    ///
    /// # Returns
    ///
    /// the spacing between coluns of `self`
    fn get_column_spacing(&self) -> u32;

    /// Retrieves the orientation of the `self`.
    ///
    /// # Returns
    ///
    /// the orientation of the layout
    fn get_orientation(&self) -> Orientation;

    /// Returns whether all rows of `self` have the same height.
    ///
    /// # Returns
    ///
    /// whether all rows of `self` have the same height.
    fn get_row_homogeneous(&self) -> bool;

    /// Retrieves the spacing set using `GridLayoutExt::set_row_spacing`
    ///
    /// # Returns
    ///
    /// the spacing between rows of `self`
    fn get_row_spacing(&self) -> u32;

    /// Inserts a column at the specified position.
    ///
    /// Children which are attached at or to the right of this position
    /// are moved one column to the right. Children which span across this
    /// position are grown to span the new column.
    /// ## `position`
    /// the position to insert the column at
    fn insert_column(&self, position: i32);

    /// Inserts a row or column at the specified position.
    ///
    /// The new row or column is placed next to `sibling`, on the side
    /// determined by `side`. If `side` is `GridPosition::Left` or
    /// `GridPosition::Bottom`, a row is inserted. If `side` is
    /// `GridPosition::Left` of `GridPosition::Right`,
    /// a column is inserted.
    /// ## `sibling`
    /// the child of `self` that the new row or column will be
    ///  placed next to
    /// ## `side`
    /// the side of `sibling` that `child` is positioned next to
    fn insert_next_to<P: IsA<Actor>>(&self, sibling: &P, side: GridPosition);

    /// Inserts a row at the specified position.
    ///
    /// Children which are attached at or below this position
    /// are moved one row down. Children which span across this
    /// position are grown to span the new row.
    /// ## `position`
    /// the position to insert the row at
    fn insert_row(&self, position: i32);

    /// Sets whether all columns of `self` will have the same width.
    /// ## `homogeneous`
    /// `true` to make columns homogeneous
    fn set_column_homogeneous(&self, homogeneous: bool);

    /// Sets the spacing between columns of `self`
    /// ## `spacing`
    /// the spacing between columns of the layout, in pixels
    fn set_column_spacing(&self, spacing: u32);

    /// Sets the orientation of the `self`.
    ///
    /// `GridLayout` uses the orientation as a hint when adding
    /// children to the `Actor` using it as a layout manager via
    /// `ActorExt::add_child`; changing this value will not have
    /// any effect on children that are already part of the layout.
    /// ## `orientation`
    /// the orientation of the `GridLayout`
    fn set_orientation(&self, orientation: Orientation);

    /// Sets whether all rows of `self` will have the same height.
    /// ## `homogeneous`
    /// `true` to make rows homogeneous
    fn set_row_homogeneous(&self, homogeneous: bool);

    /// Sets the spacing between rows of `self`
    /// ## `spacing`
    /// the spacing between rows of the layout, in pixels
    fn set_row_spacing(&self, spacing: u32);

    fn connect_property_column_homogeneous_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    fn connect_property_column_spacing_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    fn connect_property_orientation_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    fn connect_property_row_homogeneous_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    fn connect_property_row_spacing_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<GridLayout>> GridLayoutExt for O {
    fn attach<P: IsA<Actor>>(&self, child: &P, left: i32, top: i32, width: i32, height: i32) {
        unsafe {
            ffi::clutter_grid_layout_attach(
                self.as_ref().to_glib_none().0,
                child.as_ref().to_glib_none().0,
                left,
                top,
                width,
                height,
            );
        }
    }

    fn attach_next_to<P: IsA<Actor>, Q: IsA<Actor>>(
        &self,
        child: &P,
        sibling: Option<&Q>,
        side: GridPosition,
        width: i32,
        height: i32,
    ) {
        unsafe {
            ffi::clutter_grid_layout_attach_next_to(
                self.as_ref().to_glib_none().0,
                child.as_ref().to_glib_none().0,
                sibling.map(|p| p.as_ref()).to_glib_none().0,
                side.to_glib(),
                width,
                height,
            );
        }
    }

    fn get_child_at(&self, left: i32, top: i32) -> Option<Actor> {
        unsafe {
            from_glib_none(ffi::clutter_grid_layout_get_child_at(
                self.as_ref().to_glib_none().0,
                left,
                top,
            ))
        }
    }

    fn get_column_homogeneous(&self) -> bool {
        unsafe {
            from_glib(ffi::clutter_grid_layout_get_column_homogeneous(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_column_spacing(&self) -> u32 {
        unsafe { ffi::clutter_grid_layout_get_column_spacing(self.as_ref().to_glib_none().0) }
    }

    fn get_orientation(&self) -> Orientation {
        unsafe {
            from_glib(ffi::clutter_grid_layout_get_orientation(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_row_homogeneous(&self) -> bool {
        unsafe {
            from_glib(ffi::clutter_grid_layout_get_row_homogeneous(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_row_spacing(&self) -> u32 {
        unsafe { ffi::clutter_grid_layout_get_row_spacing(self.as_ref().to_glib_none().0) }
    }

    fn insert_column(&self, position: i32) {
        unsafe {
            ffi::clutter_grid_layout_insert_column(self.as_ref().to_glib_none().0, position);
        }
    }

    fn insert_next_to<P: IsA<Actor>>(&self, sibling: &P, side: GridPosition) {
        unsafe {
            ffi::clutter_grid_layout_insert_next_to(
                self.as_ref().to_glib_none().0,
                sibling.as_ref().to_glib_none().0,
                side.to_glib(),
            );
        }
    }

    fn insert_row(&self, position: i32) {
        unsafe {
            ffi::clutter_grid_layout_insert_row(self.as_ref().to_glib_none().0, position);
        }
    }

    fn set_column_homogeneous(&self, homogeneous: bool) {
        unsafe {
            ffi::clutter_grid_layout_set_column_homogeneous(
                self.as_ref().to_glib_none().0,
                homogeneous.to_glib(),
            );
        }
    }

    fn set_column_spacing(&self, spacing: u32) {
        unsafe {
            ffi::clutter_grid_layout_set_column_spacing(self.as_ref().to_glib_none().0, spacing);
        }
    }

    fn set_orientation(&self, orientation: Orientation) {
        unsafe {
            ffi::clutter_grid_layout_set_orientation(
                self.as_ref().to_glib_none().0,
                orientation.to_glib(),
            );
        }
    }

    fn set_row_homogeneous(&self, homogeneous: bool) {
        unsafe {
            ffi::clutter_grid_layout_set_row_homogeneous(
                self.as_ref().to_glib_none().0,
                homogeneous.to_glib(),
            );
        }
    }

    fn set_row_spacing(&self, spacing: u32) {
        unsafe {
            ffi::clutter_grid_layout_set_row_spacing(self.as_ref().to_glib_none().0, spacing);
        }
    }

    fn connect_property_column_homogeneous_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn notify_column_homogeneous_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ffi::ClutterGridLayout,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<GridLayout>,
        {
            let f: &F = &*(f as *const F);
            f(&GridLayout::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::column-homogeneous\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_column_homogeneous_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_property_column_spacing_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn notify_column_spacing_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ffi::ClutterGridLayout,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<GridLayout>,
        {
            let f: &F = &*(f as *const F);
            f(&GridLayout::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::column-spacing\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_column_spacing_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_property_orientation_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_orientation_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ffi::ClutterGridLayout,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<GridLayout>,
        {
            let f: &F = &*(f as *const F);
            f(&GridLayout::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::orientation\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_orientation_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_property_row_homogeneous_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn notify_row_homogeneous_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ffi::ClutterGridLayout,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<GridLayout>,
        {
            let f: &F = &*(f as *const F);
            f(&GridLayout::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::row-homogeneous\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_row_homogeneous_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_property_row_spacing_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_row_spacing_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ffi::ClutterGridLayout,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<GridLayout>,
        {
            let f: &F = &*(f as *const F);
            f(&GridLayout::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::row-spacing\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_row_spacing_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for GridLayout {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "GridLayout")
    }
}