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
// 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::CoordType;
use crate::Layer;
use crate::Object;
use crate::Rectangle;
#[cfg(any(feature = "v2_30", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_30")))]
use crate::ScrollType;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem;
use std::mem::transmute;

glib::wrapper! {
    /// The ATK interface provided by UI components
    /// which occupy a physical area on the screen.
    /// which the user can activate/interact with.
    ///
    /// [`Component`][crate::Component] should be implemented by most if not all UI elements
    /// with an actual on-screen presence, i.e. components which can be
    /// said to have a screen-coordinate bounding box. Virtually all
    /// widgets will need to have [`Component`][crate::Component] implementations provided
    /// for their corresponding [`Object`][crate::Object] class. In short, only UI
    /// elements which are *not* GUI elements will omit this ATK interface.
    ///
    /// A possible exception might be textual information with a
    /// transparent background, in which case text glyph bounding box
    /// information is provided by [`Text`][crate::Text].
    ///
    /// # Implements
    ///
    /// [`ComponentExt`][trait@crate::prelude::ComponentExt]
    #[doc(alias = "AtkComponent")]
    pub struct Component(Interface<ffi::AtkComponent, ffi::AtkComponentIface>);

    match fn {
        type_ => || ffi::atk_component_get_type(),
    }
}

impl Component {
    pub const NONE: Option<&'static Component> = None;
}

/// Trait containing all [`struct@Component`] methods.
///
/// # Implementors
///
/// [`Component`][struct@crate::Component], [`NoOpObject`][struct@crate::NoOpObject], [`Plug`][struct@crate::Plug], [`Socket`][struct@crate::Socket]
pub trait ComponentExt: 'static {
    /// Checks whether the specified point is within the extent of the `self`.
    ///
    /// Toolkit implementor note: ATK provides a default implementation for
    /// this virtual method. In general there are little reason to
    /// re-implement it.
    /// ## `x`
    /// x coordinate
    /// ## `y`
    /// y coordinate
    /// ## `coord_type`
    /// specifies whether the coordinates are relative to the screen
    /// or to the components top level window
    ///
    /// # Returns
    ///
    /// [`true`] or [`false`] indicating whether the specified point is within
    /// the extent of the `self` or not
    #[doc(alias = "atk_component_contains")]
    fn contains(&self, x: i32, y: i32, coord_type: CoordType) -> bool;

    /// Returns the alpha value (i.e. the opacity) for this
    /// `self`, on a scale from 0 (fully transparent) to 1.0
    /// (fully opaque).
    ///
    /// # Returns
    ///
    /// An alpha value from 0 to 1.0, inclusive.
    #[doc(alias = "atk_component_get_alpha")]
    #[doc(alias = "get_alpha")]
    fn alpha(&self) -> f64;

    /// Gets the rectangle which gives the extent of the `self`.
    ///
    /// If the extent can not be obtained (e.g. a non-embedded plug or missing
    /// support), all of x, y, width, height are set to -1.
    /// ## `coord_type`
    /// specifies whether the coordinates are relative to the screen
    /// or to the components top level window
    ///
    /// # Returns
    ///
    ///
    /// ## `x`
    /// address of `gint` to put x coordinate
    ///
    /// ## `y`
    /// address of `gint` to put y coordinate
    ///
    /// ## `width`
    /// address of `gint` to put width
    ///
    /// ## `height`
    /// address of `gint` to put height
    #[doc(alias = "atk_component_get_extents")]
    #[doc(alias = "get_extents")]
    fn extents(&self, coord_type: CoordType) -> (i32, i32, i32, i32);

    /// Gets the layer of the component.
    ///
    /// # Returns
    ///
    /// an [`Layer`][crate::Layer] which is the layer of the component
    #[doc(alias = "atk_component_get_layer")]
    #[doc(alias = "get_layer")]
    fn layer(&self) -> Layer;

    /// Gets the zorder of the component. The value G_MININT will be returned
    /// if the layer of the component is not ATK_LAYER_MDI or ATK_LAYER_WINDOW.
    ///
    /// # Returns
    ///
    /// a gint which is the zorder of the component, i.e. the depth at
    /// which the component is shown in relation to other components in the same
    /// container.
    #[doc(alias = "atk_component_get_mdi_zorder")]
    #[doc(alias = "get_mdi_zorder")]
    fn mdi_zorder(&self) -> i32;

    /// Gets the position of `self` in the form of
    /// a point specifying `self`'s top-left corner.
    ///
    /// If the position can not be obtained (e.g. a non-embedded plug or missing
    /// support), x and y are set to -1.
    ///
    /// # Deprecated
    ///
    /// Since 2.12. Use [`extents()`][Self::extents()] instead.
    /// ## `coord_type`
    /// specifies whether the coordinates are relative to the screen
    /// or to the components top level window
    ///
    /// # Returns
    ///
    ///
    /// ## `x`
    /// address of `gint` to put x coordinate position
    ///
    /// ## `y`
    /// address of `gint` to put y coordinate position
    #[doc(alias = "atk_component_get_position")]
    #[doc(alias = "get_position")]
    fn position(&self, coord_type: CoordType) -> (i32, i32);

    /// Gets the size of the `self` in terms of width and height.
    ///
    /// If the size can not be obtained (e.g. a non-embedded plug or missing
    /// support), width and height are set to -1.
    ///
    /// # Deprecated
    ///
    /// Since 2.12. Use [`extents()`][Self::extents()] instead.
    ///
    /// # Returns
    ///
    ///
    /// ## `width`
    /// address of `gint` to put width of `self`
    ///
    /// ## `height`
    /// address of `gint` to put height of `self`
    #[doc(alias = "atk_component_get_size")]
    #[doc(alias = "get_size")]
    fn size(&self) -> (i32, i32);

    /// Grabs focus for this `self`.
    ///
    /// # Returns
    ///
    /// [`true`] if successful, [`false`] otherwise.
    #[doc(alias = "atk_component_grab_focus")]
    fn grab_focus(&self) -> bool;

    /// Gets a reference to the accessible child, if one exists, at the
    /// coordinate point specified by `x` and `y`.
    /// ## `x`
    /// x coordinate
    /// ## `y`
    /// y coordinate
    /// ## `coord_type`
    /// specifies whether the coordinates are relative to the screen
    /// or to the components top level window
    ///
    /// # Returns
    ///
    /// a reference to the accessible
    /// child, if one exists
    #[doc(alias = "atk_component_ref_accessible_at_point")]
    fn ref_accessible_at_point(&self, x: i32, y: i32, coord_type: CoordType) -> Option<Object>;

    /// Makes `self` visible on the screen by scrolling all necessary parents.
    ///
    /// Contrary to atk_component_set_position, this does not actually move
    /// `self` in its parent, this only makes the parents scroll so that the
    /// object shows up on the screen, given its current position within the parents.
    /// ## `type_`
    /// specify where the object should be made visible.
    ///
    /// # Returns
    ///
    /// whether scrolling was successful.
    #[cfg(any(feature = "v2_30", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_30")))]
    #[doc(alias = "atk_component_scroll_to")]
    fn scroll_to(&self, type_: ScrollType) -> bool;

    /// Move the top-left of `self` to a given position of the screen by
    /// scrolling all necessary parents.
    /// ## `coords`
    /// specify whether coordinates are relative to the screen or to the
    /// parent object.
    /// ## `x`
    /// x-position where to scroll to
    /// ## `y`
    /// y-position where to scroll to
    ///
    /// # Returns
    ///
    /// whether scrolling was successful.
    #[cfg(any(feature = "v2_30", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_30")))]
    #[doc(alias = "atk_component_scroll_to_point")]
    fn scroll_to_point(&self, coords: CoordType, x: i32, y: i32) -> bool;

    /// Sets the extents of `self`.
    /// ## `x`
    /// x coordinate
    /// ## `y`
    /// y coordinate
    /// ## `width`
    /// width to set for `self`
    /// ## `height`
    /// height to set for `self`
    /// ## `coord_type`
    /// specifies whether the coordinates are relative to the screen
    /// or to the components top level window
    ///
    /// # Returns
    ///
    /// [`true`] or [`false`] whether the extents were set or not
    #[doc(alias = "atk_component_set_extents")]
    fn set_extents(&self, x: i32, y: i32, width: i32, height: i32, coord_type: CoordType) -> bool;

    /// Sets the position of `self`.
    ///
    /// Contrary to atk_component_scroll_to, this does not trigger any scrolling,
    /// this just moves `self` in its parent.
    /// ## `x`
    /// x coordinate
    /// ## `y`
    /// y coordinate
    /// ## `coord_type`
    /// specifies whether the coordinates are relative to the screen
    /// or to the component's top level window
    ///
    /// # Returns
    ///
    /// [`true`] or [`false`] whether or not the position was set or not
    #[doc(alias = "atk_component_set_position")]
    fn set_position(&self, x: i32, y: i32, coord_type: CoordType) -> bool;

    /// Set the size of the `self` in terms of width and height.
    /// ## `width`
    /// width to set for `self`
    /// ## `height`
    /// height to set for `self`
    ///
    /// # Returns
    ///
    /// [`true`] or [`false`] whether the size was set or not
    #[doc(alias = "atk_component_set_size")]
    fn set_size(&self, width: i32, height: i32) -> bool;

    /// The 'bounds-changed" signal is emitted when the bposition or
    /// size of the component changes.
    /// ## `arg1`
    /// The AtkRectangle giving the new position and size.
    #[doc(alias = "bounds-changed")]
    fn connect_bounds_changed<F: Fn(&Self, &Rectangle) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<Component>> ComponentExt for O {
    fn contains(&self, x: i32, y: i32, coord_type: CoordType) -> bool {
        unsafe {
            from_glib(ffi::atk_component_contains(
                self.as_ref().to_glib_none().0,
                x,
                y,
                coord_type.into_glib(),
            ))
        }
    }

    fn alpha(&self) -> f64 {
        unsafe { ffi::atk_component_get_alpha(self.as_ref().to_glib_none().0) }
    }

    fn extents(&self, coord_type: CoordType) -> (i32, i32, i32, i32) {
        unsafe {
            let mut x = mem::MaybeUninit::uninit();
            let mut y = mem::MaybeUninit::uninit();
            let mut width = mem::MaybeUninit::uninit();
            let mut height = mem::MaybeUninit::uninit();
            ffi::atk_component_get_extents(
                self.as_ref().to_glib_none().0,
                x.as_mut_ptr(),
                y.as_mut_ptr(),
                width.as_mut_ptr(),
                height.as_mut_ptr(),
                coord_type.into_glib(),
            );
            (
                x.assume_init(),
                y.assume_init(),
                width.assume_init(),
                height.assume_init(),
            )
        }
    }

    fn layer(&self) -> Layer {
        unsafe { from_glib(ffi::atk_component_get_layer(self.as_ref().to_glib_none().0)) }
    }

    fn mdi_zorder(&self) -> i32 {
        unsafe { ffi::atk_component_get_mdi_zorder(self.as_ref().to_glib_none().0) }
    }

    fn position(&self, coord_type: CoordType) -> (i32, i32) {
        unsafe {
            let mut x = mem::MaybeUninit::uninit();
            let mut y = mem::MaybeUninit::uninit();
            ffi::atk_component_get_position(
                self.as_ref().to_glib_none().0,
                x.as_mut_ptr(),
                y.as_mut_ptr(),
                coord_type.into_glib(),
            );
            (x.assume_init(), y.assume_init())
        }
    }

    fn size(&self) -> (i32, i32) {
        unsafe {
            let mut width = mem::MaybeUninit::uninit();
            let mut height = mem::MaybeUninit::uninit();
            ffi::atk_component_get_size(
                self.as_ref().to_glib_none().0,
                width.as_mut_ptr(),
                height.as_mut_ptr(),
            );
            (width.assume_init(), height.assume_init())
        }
    }

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

    fn ref_accessible_at_point(&self, x: i32, y: i32, coord_type: CoordType) -> Option<Object> {
        unsafe {
            from_glib_full(ffi::atk_component_ref_accessible_at_point(
                self.as_ref().to_glib_none().0,
                x,
                y,
                coord_type.into_glib(),
            ))
        }
    }

    #[cfg(any(feature = "v2_30", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_30")))]
    fn scroll_to(&self, type_: ScrollType) -> bool {
        unsafe {
            from_glib(ffi::atk_component_scroll_to(
                self.as_ref().to_glib_none().0,
                type_.into_glib(),
            ))
        }
    }

    #[cfg(any(feature = "v2_30", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_30")))]
    fn scroll_to_point(&self, coords: CoordType, x: i32, y: i32) -> bool {
        unsafe {
            from_glib(ffi::atk_component_scroll_to_point(
                self.as_ref().to_glib_none().0,
                coords.into_glib(),
                x,
                y,
            ))
        }
    }

    fn set_extents(&self, x: i32, y: i32, width: i32, height: i32, coord_type: CoordType) -> bool {
        unsafe {
            from_glib(ffi::atk_component_set_extents(
                self.as_ref().to_glib_none().0,
                x,
                y,
                width,
                height,
                coord_type.into_glib(),
            ))
        }
    }

    fn set_position(&self, x: i32, y: i32, coord_type: CoordType) -> bool {
        unsafe {
            from_glib(ffi::atk_component_set_position(
                self.as_ref().to_glib_none().0,
                x,
                y,
                coord_type.into_glib(),
            ))
        }
    }

    fn set_size(&self, width: i32, height: i32) -> bool {
        unsafe {
            from_glib(ffi::atk_component_set_size(
                self.as_ref().to_glib_none().0,
                width,
                height,
            ))
        }
    }

    fn connect_bounds_changed<F: Fn(&Self, &Rectangle) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn bounds_changed_trampoline<
            P: IsA<Component>,
            F: Fn(&P, &Rectangle) + 'static,
        >(
            this: *mut ffi::AtkComponent,
            arg1: *mut ffi::AtkRectangle,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                Component::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(arg1),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"bounds-changed\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    bounds_changed_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for Component {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("Component")
    }
}