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
// 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::ResponseType;
use crate::Window;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use glib::ToValue;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem::transmute;

glib::wrapper! {
    /// Native dialogs are platform dialogs that don't use [`Dialog`][crate::Dialog] or
    /// [`Window`][crate::Window]. They are used in order to integrate better with a
    /// platform, by looking the same as other native applications and
    /// supporting platform specific features.
    ///
    /// The [`Dialog`][crate::Dialog] functions cannot be used on such objects, but we
    /// need a similar API in order to drive them. The [`NativeDialog`][crate::NativeDialog]
    /// object is an API that allows you to do this. It allows you to set
    /// various common properties on the dialog, as well as show and hide
    /// it and get a `signal::NativeDialog::response` signal when the user finished
    /// with the dialog.
    ///
    /// There is also a [`NativeDialogExt::run()`][crate::prelude::NativeDialogExt::run()] helper that makes it easy
    /// to run any native dialog in a modal way with a recursive mainloop,
    /// similar to [`DialogExt::run()`][crate::prelude::DialogExt::run()].
    ///
    /// This is an Abstract Base Class, you cannot instantiate it.
    ///
    /// # Implements
    ///
    /// [`NativeDialogExt`][trait@crate::prelude::NativeDialogExt], [`trait@glib::ObjectExt`], [`NativeDialogExtManual`][trait@crate::prelude::NativeDialogExtManual]
    #[doc(alias = "GtkNativeDialog")]
    pub struct NativeDialog(Object<ffi::GtkNativeDialog, ffi::GtkNativeDialogClass>);

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

pub const NONE_NATIVE_DIALOG: Option<&NativeDialog> = None;

/// Trait containing all [`struct@NativeDialog`] methods.
///
/// # Implementors
///
/// [`FileChooserNative`][struct@crate::FileChooserNative], [`NativeDialog`][struct@crate::NativeDialog]
pub trait NativeDialogExt: 'static {
    /// Destroys a dialog.
    ///
    /// When a dialog is destroyed, it will break any references it holds
    /// to other objects. If it is visible it will be hidden and any underlying
    /// window system resources will be destroyed.
    ///
    /// Note that this does not release any reference to the object (as opposed to
    /// destroying a GtkWindow) because there is no reference from the windowing
    /// system to the [`NativeDialog`][crate::NativeDialog].
    #[doc(alias = "gtk_native_dialog_destroy")]
    fn destroy(&self);

    /// Returns whether the dialog is modal. See [`set_modal()`][Self::set_modal()].
    ///
    /// # Returns
    ///
    /// [`true`] if the dialog is set to be modal
    #[doc(alias = "gtk_native_dialog_get_modal")]
    #[doc(alias = "get_modal")]
    fn is_modal(&self) -> bool;

    /// Gets the title of the [`NativeDialog`][crate::NativeDialog].
    ///
    /// # Returns
    ///
    /// the title of the dialog, or [`None`] if none has
    ///  been set explicitly. The returned string is owned by the widget
    ///  and must not be modified or freed.
    #[doc(alias = "gtk_native_dialog_get_title")]
    #[doc(alias = "get_title")]
    fn title(&self) -> Option<glib::GString>;

    /// Fetches the transient parent for this window. See
    /// [`set_transient_for()`][Self::set_transient_for()].
    ///
    /// # Returns
    ///
    /// the transient parent for this window,
    /// or [`None`] if no transient parent has been set.
    #[doc(alias = "gtk_native_dialog_get_transient_for")]
    #[doc(alias = "get_transient_for")]
    fn transient_for(&self) -> Option<Window>;

    /// Determines whether the dialog is visible.
    ///
    /// # Returns
    ///
    /// [`true`] if the dialog is visible
    #[doc(alias = "gtk_native_dialog_get_visible")]
    #[doc(alias = "get_visible")]
    fn is_visible(&self) -> bool;

    /// Hides the dialog if it is visilbe, aborting any interaction. Once this
    /// is called the `signal::NativeDialog::response` signal will not be emitted
    /// until after the next call to [`show()`][Self::show()].
    ///
    /// If the dialog is not visible this does nothing.
    #[doc(alias = "gtk_native_dialog_hide")]
    fn hide(&self);

    /// Blocks in a recursive main loop until `self` emits the
    /// `signal::NativeDialog::response` signal. It then returns the response ID
    /// from the ::response signal emission.
    ///
    /// Before entering the recursive main loop, [`run()`][Self::run()]
    /// calls [`show()`][Self::show()] on the dialog for you.
    ///
    /// After [`run()`][Self::run()] returns, then dialog will be hidden.
    ///
    /// Typical usage of this function might be:
    ///
    ///
    /// **⚠️ The following code is in C ⚠️**
    ///
    /// ```C
    ///   gint result = gtk_native_dialog_run (GTK_NATIVE_DIALOG (dialog));
    ///   switch (result)
    ///     {
    ///       case GTK_RESPONSE_ACCEPT:
    ///          do_application_specific_something ();
    ///          break;
    ///       default:
    ///          do_nothing_since_dialog_was_cancelled ();
    ///          break;
    ///     }
    ///   g_object_unref (dialog);
    /// ```
    ///
    /// Note that even though the recursive main loop gives the effect of a
    /// modal dialog (it prevents the user from interacting with other
    /// windows in the same window group while the dialog is run), callbacks
    /// such as timeouts, IO channel watches, DND drops, etc, will
    /// be triggered during a `gtk_nautilus_dialog_run()` call.
    ///
    /// # Returns
    ///
    /// response ID
    #[doc(alias = "gtk_native_dialog_run")]
    fn run(&self) -> ResponseType;

    /// Sets a dialog modal or non-modal. Modal dialogs prevent interaction
    /// with other windows in the same application. To keep modal dialogs
    /// on top of main application windows, use
    /// [`set_transient_for()`][Self::set_transient_for()] to make the dialog transient for the
    /// parent; most [window managers][gtk-X11-arch]
    /// will then disallow lowering the dialog below the parent.
    /// ## `modal`
    /// whether the window is modal
    #[doc(alias = "gtk_native_dialog_set_modal")]
    fn set_modal(&self, modal: bool);

    /// Sets the title of the [`NativeDialog`][crate::NativeDialog].
    /// ## `title`
    /// title of the dialog
    #[doc(alias = "gtk_native_dialog_set_title")]
    fn set_title(&self, title: &str);

    /// Dialog windows should be set transient for the main application
    /// window they were spawned from. This allows
    /// [window managers][gtk-X11-arch] to e.g. keep the
    /// dialog on top of the main window, or center the dialog over the
    /// main window.
    ///
    /// Passing [`None`] for `parent` unsets the current transient window.
    /// ## `parent`
    /// parent window, or [`None`]
    #[doc(alias = "gtk_native_dialog_set_transient_for")]
    fn set_transient_for<P: IsA<Window>>(&self, parent: Option<&P>);

    /// Shows the dialog on the display, allowing the user to interact with
    /// it. When the user accepts the state of the dialog the dialog will
    /// be automatically hidden and the `signal::NativeDialog::response` signal
    /// will be emitted.
    ///
    /// Multiple calls while the dialog is visible will be ignored.
    #[doc(alias = "gtk_native_dialog_show")]
    fn show(&self);

    /// Whether the window is currenlty visible.
    #[cfg(any(feature = "v3_20", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v3_20")))]
    fn set_visible(&self, visible: bool);

    /// Emitted when the user responds to the dialog.
    ///
    /// When this is called the dialog has been hidden.
    ///
    /// If you call [`hide()`][Self::hide()] before the user responds to
    /// the dialog this signal will not be emitted.
    /// ## `response_id`
    /// the response ID
    #[cfg(any(feature = "v3_20", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v3_20")))]
    #[doc(alias = "response")]
    fn connect_response<F: Fn(&Self, ResponseType) + 'static>(&self, f: F) -> SignalHandlerId;

    #[cfg(any(feature = "v3_20", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v3_20")))]
    #[doc(alias = "modal")]
    fn connect_modal_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    #[cfg(any(feature = "v3_20", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v3_20")))]
    #[doc(alias = "title")]
    fn connect_title_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    #[cfg(any(feature = "v3_20", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v3_20")))]
    #[doc(alias = "transient-for")]
    fn connect_transient_for_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    #[cfg(any(feature = "v3_20", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v3_20")))]
    #[doc(alias = "visible")]
    fn connect_visible_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<NativeDialog>> NativeDialogExt for O {
    fn destroy(&self) {
        unsafe {
            ffi::gtk_native_dialog_destroy(self.as_ref().to_glib_none().0);
        }
    }

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

    fn title(&self) -> Option<glib::GString> {
        unsafe {
            from_glib_none(ffi::gtk_native_dialog_get_title(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

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

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

    fn hide(&self) {
        unsafe {
            ffi::gtk_native_dialog_hide(self.as_ref().to_glib_none().0);
        }
    }

    fn run(&self) -> ResponseType {
        unsafe { from_glib(ffi::gtk_native_dialog_run(self.as_ref().to_glib_none().0)) }
    }

    fn set_modal(&self, modal: bool) {
        unsafe {
            ffi::gtk_native_dialog_set_modal(self.as_ref().to_glib_none().0, modal.into_glib());
        }
    }

    fn set_title(&self, title: &str) {
        unsafe {
            ffi::gtk_native_dialog_set_title(
                self.as_ref().to_glib_none().0,
                title.to_glib_none().0,
            );
        }
    }

    fn set_transient_for<P: IsA<Window>>(&self, parent: Option<&P>) {
        unsafe {
            ffi::gtk_native_dialog_set_transient_for(
                self.as_ref().to_glib_none().0,
                parent.map(|p| p.as_ref()).to_glib_none().0,
            );
        }
    }

    fn show(&self) {
        unsafe {
            ffi::gtk_native_dialog_show(self.as_ref().to_glib_none().0);
        }
    }

    #[cfg(any(feature = "v3_20", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v3_20")))]
    fn set_visible(&self, visible: bool) {
        unsafe {
            glib::gobject_ffi::g_object_set_property(
                self.to_glib_none().0 as *mut glib::gobject_ffi::GObject,
                b"visible\0".as_ptr() as *const _,
                visible.to_value().to_glib_none().0,
            );
        }
    }

    #[cfg(any(feature = "v3_20", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v3_20")))]
    fn connect_response<F: Fn(&Self, ResponseType) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn response_trampoline<
            P: IsA<NativeDialog>,
            F: Fn(&P, ResponseType) + 'static,
        >(
            this: *mut ffi::GtkNativeDialog,
            response_id: ffi::GtkResponseType,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                NativeDialog::from_glib_borrow(this).unsafe_cast_ref(),
                from_glib(response_id),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"response\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    response_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[cfg(any(feature = "v3_20", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v3_20")))]
    fn connect_modal_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_modal_trampoline<P: IsA<NativeDialog>, F: Fn(&P) + 'static>(
            this: *mut ffi::GtkNativeDialog,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(NativeDialog::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::modal\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_modal_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[cfg(any(feature = "v3_20", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v3_20")))]
    fn connect_title_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_title_trampoline<P: IsA<NativeDialog>, F: Fn(&P) + 'static>(
            this: *mut ffi::GtkNativeDialog,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(NativeDialog::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::title\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_title_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[cfg(any(feature = "v3_20", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v3_20")))]
    fn connect_transient_for_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_transient_for_trampoline<
            P: IsA<NativeDialog>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::GtkNativeDialog,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(NativeDialog::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::transient-for\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_transient_for_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[cfg(any(feature = "v3_20", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v3_20")))]
    fn connect_visible_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_visible_trampoline<
            P: IsA<NativeDialog>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::GtkNativeDialog,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(NativeDialog::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::visible\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_visible_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

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