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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
|
/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
/* GTK - The GIMP Toolkit
* gtkfilechoosernative.c: Native File selector dialog
* Copyright (C) 2015, Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gtknativedialogprivate.h"
#include "gtkprivate.h"
#include "gtkfilechooserdialog.h"
#include "gtkfilechooserprivate.h"
#include "gtkfilechooserwidget.h"
#include "gtkfilechooserwidgetprivate.h"
#include "gtkfilechooserutils.h"
#include "gtkfilechooserembed.h"
#include "gtkfilesystem.h"
#include "gtksizerequest.h"
#include "gtktypebuiltins.h"
#include "gtkintl.h"
#include "gtksettings.h"
#include "gtktogglebutton.h"
#include "gtkstylecontext.h"
#include "gtkheaderbar.h"
#include "gtklabel.h"
#include "gtkfilechooserentry.h"
/**
* SECTION:gtknativedialog
* @Short_description: Integrate with native dialogs
* @Title: GtkNativeDialog
* @See_also: #GtkFileChooserNative, #GtkDialog
*
* Native dialogs are platform dialogs that don't use #GtkDialog or
* #GtkWindow. 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 #GtkDialog functions cannot be used on such objects, but we
* need a similar API in order to drive them. The #GtkNativeDialog
* 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 #GtkNativeDialog::response signal when the user finished
* with the dialog.
*
* There is also a gtk_native_dialog_run() helper that makes it easy
* to run any native dialog in a modal way with a recursive mainloop,
* similar to gtk_dialog_run().
*/
typedef struct _GtkNativeDialogPrivate GtkNativeDialogPrivate;
struct _GtkNativeDialogPrivate
{
GtkWindow *transient_for;
char *title;
guint visible : 1;
guint modal : 1;
/* Run state */
gint run_response_id;
GMainLoop *run_loop; /* Non-NULL when in run */
};
enum {
PROP_0,
PROP_TITLE,
PROP_VISIBLE,
PROP_MODAL,
PROP_TRANSIENT_FOR,
LAST_ARG,
};
enum {
RESPONSE,
LAST_SIGNAL
};
static GParamSpec *native_props[LAST_ARG] = { NULL, };
static guint native_signals[LAST_SIGNAL];
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkNativeDialog, gtk_native_dialog, G_TYPE_OBJECT,
G_ADD_PRIVATE (GtkNativeDialog))
static void
gtk_native_dialog_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkNativeDialog *self = GTK_NATIVE_DIALOG (object);
switch (prop_id)
{
case PROP_TITLE:
gtk_native_dialog_set_title (self, g_value_get_string (value));
break;
case PROP_MODAL:
gtk_native_dialog_set_modal (self, g_value_get_boolean (value));
break;
case PROP_VISIBLE:
if (g_value_get_boolean (value))
gtk_native_dialog_show (self);
else
gtk_native_dialog_hide (self);
break;
case PROP_TRANSIENT_FOR:
gtk_native_dialog_set_transient_for (self, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_native_dialog_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkNativeDialog *self = GTK_NATIVE_DIALOG (object);
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
switch (prop_id)
{
case PROP_TITLE:
g_value_set_string (value, priv->title);
break;
case PROP_MODAL:
g_value_set_boolean (value, priv->modal);
break;
case PROP_VISIBLE:
g_value_set_boolean (value, priv->visible);
break;
case PROP_TRANSIENT_FOR:
g_value_set_object (value, priv->transient_for);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_native_dialog_dispose (GObject *object)
{
GtkNativeDialog *self = GTK_NATIVE_DIALOG (object);
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
if (priv->visible)
gtk_native_dialog_hide (self);
G_OBJECT_CLASS (gtk_native_dialog_parent_class)->dispose (object);
}
static void
gtk_native_dialog_finalize (GObject *object)
{
GtkNativeDialog *self = GTK_NATIVE_DIALOG (object);
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
g_clear_pointer (&priv->title, g_free);
g_clear_object (&priv->transient_for);
G_OBJECT_CLASS (gtk_native_dialog_parent_class)->finalize (object);
}
static void
gtk_native_dialog_class_init (GtkNativeDialogClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
gobject_class->set_property = gtk_native_dialog_set_property;
gobject_class->get_property = gtk_native_dialog_get_property;
gobject_class->finalize = gtk_native_dialog_finalize;
gobject_class->dispose = gtk_native_dialog_dispose;
/**
* GtkNativeDialog:title:
*
* The title of the dialog window
*/
native_props[PROP_TITLE] =
g_param_spec_string ("title",
P_("Dialog Title"),
P_("The title of the file chooser dialog"),
NULL,
GTK_PARAM_READWRITE);
/**
* GtkNativeDialog:modal:
*
* Whether the window should be modal with respect to its transient parent.
*/
native_props[PROP_MODAL] =
g_param_spec_boolean ("modal",
P_("Modal"),
P_("If TRUE, the dialog is modal (other windows are not usable while this one is up)"),
FALSE,
GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkNativeDialog:visible:
*
* Whether the window is currenlty visible.
*/
native_props[PROP_VISIBLE] =
g_param_spec_boolean ("visible",
P_("Visible"),
P_("Whether the dialog is currently visible"),
FALSE,
GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkNativeDialog:transient-for:
*
* The transient parent of the dialog, or %NULL for none.
*/
native_props[PROP_TRANSIENT_FOR] =
g_param_spec_object ("transient-for",
P_("Transient for Window"),
P_("The transient parent of the dialog"),
GTK_TYPE_WINDOW,
GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (gobject_class, LAST_ARG, native_props);
/**
* GtkNativeDialog::response:
* @self: the object on which the signal is emitted
* @response_id: the response ID
*
* Emitted when the user responds to the dialog.
*
* When this is called the dialog has been hidden.
*
* If you call gtk_native_dialog_hide() before the user responds to
* the dialog this signal will not be emitted.
*/
native_signals[RESPONSE] =
g_signal_new (I_("response"),
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkNativeDialogClass, response),
NULL, NULL,
g_cclosure_marshal_VOID__INT,
G_TYPE_NONE, 1,
G_TYPE_INT);
}
static void
gtk_native_dialog_init (GtkNativeDialog *self)
{
}
/**
* gtk_native_dialog_show:
* @self: a #GtkNativeDialog
*
* 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 #GtkNativeDialog::response signal
* will be emitted.
*
* Multiple calls while the dialog is visible will be ignored.
**/
void
gtk_native_dialog_show (GtkNativeDialog *self)
{
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
GtkNativeDialogClass *klass;
g_return_if_fail (GTK_IS_NATIVE_DIALOG (self));
if (priv->visible)
return;
klass = GTK_NATIVE_DIALOG_GET_CLASS (self);
g_return_if_fail (klass->show != NULL);
klass->show (self);
priv->visible = TRUE;
g_object_notify_by_pspec (G_OBJECT (self), native_props[PROP_VISIBLE]);
}
/**
* gtk_native_dialog_hide:
* @self: a #GtkNativeDialog
*
* Hides the dialog if it is visilbe, aborting any interaction. Once this
* is called the #GtkNativeDialog::response signal will not be emitted
* until after the next call to gtk_native_dialog_show().
*
* If the dialog is not visible this does nothing.
**/
void
gtk_native_dialog_hide (GtkNativeDialog *self)
{
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
GtkNativeDialogClass *klass;
g_return_if_fail (GTK_IS_NATIVE_DIALOG (self));
if (!priv->visible)
return;
priv->visible = FALSE;
klass = GTK_NATIVE_DIALOG_GET_CLASS (self);
g_return_if_fail (klass->hide != NULL);
klass->hide (self);
if (priv->run_loop && g_main_loop_is_running (priv->run_loop))
g_main_loop_quit (priv->run_loop);
g_object_notify_by_pspec (G_OBJECT (self), native_props[PROP_VISIBLE]);
}
/**
* gtk_native_dialog_destroy:
* @self: a #GtkNativeDialog
*
* 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 #GtkNativeDialog.
**/
void
gtk_native_dialog_destroy (GtkNativeDialog *self)
{
g_return_if_fail (GTK_IS_NATIVE_DIALOG (self));
g_object_run_dispose (G_OBJECT (self));
}
void
_gtk_native_dialog_emit_response (GtkNativeDialog *self,
int response_id)
{
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
priv->visible = FALSE;
g_object_notify_by_pspec (G_OBJECT (self), native_props[PROP_VISIBLE]);
g_signal_emit (self, native_signals[RESPONSE], 0, response_id);
}
/**
* gtk_native_dialog_get_visible:
* @self: a #GtkNativeDialog
*
* Determines whether the dialog is visible.
*
* Returns: %TRUE if the dialog is visible
**/
gboolean
gtk_native_dialog_get_visible (GtkNativeDialog *self)
{
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
g_return_val_if_fail (GTK_IS_NATIVE_DIALOG (self), FALSE);
return priv->visible;
}
/**
* gtk_native_dialog_set_modal:
* @self: a #GtkNativeDialog
* @modal: whether the window is modal
*
* 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
* gtk_native_dialog_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.
**/
void
gtk_native_dialog_set_modal (GtkNativeDialog *self,
gboolean modal)
{
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
g_return_if_fail (GTK_IS_NATIVE_DIALOG (self));
modal = modal != FALSE;
if (priv->modal == modal)
return;
priv->modal = modal;
g_object_notify_by_pspec (G_OBJECT (self), native_props[PROP_MODAL]);
}
/**
* gtk_native_dialog_get_modal:
* @self: a #GtkNativeDialog
*
* Returns whether the dialog is modal. See gtk_native_dialog_set_modal().
*
* Returns: %TRUE if the dialog is set to be modal
**/
gboolean
gtk_native_dialog_get_modal (GtkNativeDialog *self)
{
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
g_return_val_if_fail (GTK_IS_NATIVE_DIALOG (self), FALSE);
return priv->modal;
}
/**
* gtk_native_dialog_set_title:
* @self: a #GtkNativeDialog
* @title: title of the dialog
*
* Sets the title of the #GtkNativeDialog.
**/
void
gtk_native_dialog_set_title (GtkNativeDialog *self,
const char *title)
{
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
g_return_if_fail (GTK_IS_NATIVE_DIALOG (self));
g_free (priv->title);
priv->title = g_strdup (title);
g_object_notify_by_pspec (G_OBJECT (self), native_props[PROP_TITLE]);
}
/**
* gtk_native_dialog_get_title:
* @self: a #GtkNativeDialog
*
* Gets the title of the #GtkNativeDialog.
*
* Returns: (nullable): the title of the dialog, or %NULL if none has
* been set explicitly. The returned string is owned by the widget
* and must not be modified or freed.
**/
const char *
gtk_native_dialog_get_title (GtkNativeDialog *self)
{
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
g_return_val_if_fail (GTK_IS_NATIVE_DIALOG (self), NULL);
return priv->title;
}
/**
* gtk_native_dialog_set_transient_for:
* @self: a #GtkNativeDialog
* @parent: (allow-none): parent window, or %NULL
*
* 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 %NULL for @parent unsets the current transient window.
*/
void
gtk_native_dialog_set_transient_for (GtkNativeDialog *self,
GtkWindow *parent)
{
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
g_return_if_fail (GTK_IS_NATIVE_DIALOG (self));
if (g_set_object (&priv->transient_for, parent))
g_object_notify_by_pspec (G_OBJECT (self), native_props[PROP_TRANSIENT_FOR]);
}
/**
* gtk_native_dialog_get_transient_for:
* @self: a #GtkNativeDialog
*
* Fetches the transient parent for this window. See
* gtk_native_dialog_set_transient_for().
*
* Returns: (nullable) (transfer none): the transient parent for this window,
* or %NULL if no transient parent has been set.
**/
GtkWindow *
gtk_native_dialog_get_transient_for (GtkNativeDialog *self)
{
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
g_return_val_if_fail (GTK_IS_NATIVE_DIALOG (self), NULL);
return priv->transient_for;
}
static void
run_response_cb (GtkNativeDialog *self,
gint response_id,
gpointer data)
{
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
priv->run_response_id = response_id;
if (priv->run_loop && g_main_loop_is_running (priv->run_loop))
g_main_loop_quit (priv->run_loop);
}
/**
* gtk_native_dialog_run:
* @self: a #GtkNativeDialog
*
* Blocks in a recursive main loop until @self emits the
* #GtkNativeDialog::response signal. It then returns the response ID
* from the ::response signal emission.
*
* Before entering the recursive main loop, gtk_native_dialog_run()
* calls gtk_native_dialog_show() on the dialog for you.
*
* After gtk_native_dialog_run() returns, then dialog will be hidden.
*
* Typical usage of this function might be:
* |[<!-- language="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_native_dialog_run() call.
*
* Returns: response ID
**/
gint
gtk_native_dialog_run (GtkNativeDialog *self)
{
GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
gboolean was_modal;
guint response_handler;
g_return_val_if_fail (GTK_IS_NATIVE_DIALOG (self), -1);
g_return_val_if_fail (!priv->visible, -1);
g_return_val_if_fail (priv->run_loop == NULL, -1);
if (priv->visible || priv->run_loop != NULL)
return -1;
g_object_ref (self);
priv->run_response_id = GTK_RESPONSE_NONE;
priv->run_loop = g_main_loop_new (NULL, FALSE);
was_modal = priv->modal;
gtk_native_dialog_set_modal (self, TRUE);
response_handler =
g_signal_connect (self,
"response",
G_CALLBACK (run_response_cb),
NULL);
gtk_native_dialog_show (self);
g_main_loop_run (priv->run_loop);
g_signal_handler_disconnect (self, response_handler);
g_main_loop_unref (priv->run_loop);
priv->run_loop = NULL;
if (!was_modal)
gtk_native_dialog_set_modal (self, FALSE);
g_object_unref (self);
return priv->run_response_id;
}
|