gtk: add GtkFileChooserNative and GtkNativeDialog

Same API as GTK 3.20. Uses IFileDialog on Windows (the old common
dialogs on XP and ReactOS), NSOpenPanel/NSSavePanel on Mac OS X 10.4
and later, and the portal under Flatpak or with GTK_USE_PORTAL=1.
Otherwise it falls back to the GTK dialog. Also adds
gtk_file_filter_to_gvariant() and _new_from_gvariant() from 3.22.

Backported from GTK 3:

- 5094900180 (Alexander Larsson)
- 81cef0091e (Alexander Larsson)
- 01d0bac83e (Alexander Larsson)
- 693db082a1 (Alexander Larsson)
- 35181e793d (Matthias Clasen)
- 15e361e793 (Matthias Clasen)
- 0acb6a0e98 (Matthias Clasen)
- ce4c9a9ad6 (Tom Schoonjans)
- 32d87ac907 (Emilio Cobos Álvarez)
This commit is contained in:
Daemonratte 2026-10-17 18:04:49 +02:00
commit 94bf53e488
16 changed files with 3761 additions and 1 deletions

View file

@ -224,6 +224,7 @@ gtk_public_h_sources = \
gtkfilechooser.h \
gtkfilechooserbutton.h \
gtkfilechooserdialog.h \
gtkfilechoosernative.h \
gtkfilechooserwidget.h \
gtkfilefilter.h \
gtkfixed.h \
@ -266,6 +267,7 @@ gtk_public_h_sources = \
gtkmisc.h \
gtkmodules.h \
gtkmountoperation.h \
gtknativedialog.h \
gtknotebook.h \
gtkobject.h \
gtkoffscreenwindow.h \
@ -386,6 +388,9 @@ gtk_private_h_sources = \
gtkfilechooserentry.h \
gtkfilechooserprivate.h \
gtkfilechoosersettings.h \
gtkfilechoosernativeprivate.h \
gtkfilefilterprivate.h \
gtknativedialogprivate.h \
gtkfilechooserutils.h \
gtkfilesystem.h \
gtkfilesystemmodel.h \
@ -485,6 +490,8 @@ gtk_base_c_sources = \
gtkfilechooserbutton.c \
gtkfilechooserdefault.c \
gtkfilechooserdialog.c \
gtkfilechoosernative.c \
gtkfilechoosernativeportal.c \
gtkfilechooserembed.c \
gtkfilechooserentry.c \
gtkfilechoosersettings.c \
@ -541,6 +548,7 @@ gtk_base_c_sources = \
gtkmnemonichash.c \
gtkmodules.c \
gtkmountoperation.c \
gtknativedialog.c \
gtknotebook.c \
gtkobject.c \
gtkoffscreenwindow.c \
@ -744,12 +752,14 @@ gtk_use_x11_c_sources = \
gtktrayicon-x11.c \
gtkmountoperation-x11.c
gtk_use_win32_c_sources = \
gtkfilechoosernativewin32.c \
gtkplug-win32.c \
gtksocket-win32.c \
gtkwin32embed.c \
gtkwin32embedwidget.c \
gtkmountoperation-stub.c
gtk_use_quartz_c_sources = \
gtkfilechoosernativequartz.c \
gtksearchenginequartz.c \
gtkplug-stub.c \
gtksocket-stub.c \

View file

@ -88,6 +88,7 @@
#include <gtk/gtkfilechooser.h>
#include <gtk/gtkfilechooserbutton.h>
#include <gtk/gtkfilechooserdialog.h>
#include <gtk/gtkfilechoosernative.h>
#include <gtk/gtkfilechooserwidget.h>
#include <gtk/gtkfilefilter.h>
#include <gtk/gtkfontbutton.h>
@ -128,6 +129,7 @@
#include <gtk/gtkmisc.h>
#include <gtk/gtkmodules.h>
#include <gtk/gtkmountoperation.h>
#include <gtk/gtknativedialog.h>
#include <gtk/gtknotebook.h>
#include <gtk/gtkobject.h>
#include <gtk/gtkoffscreenwindow.h>

View file

@ -1667,6 +1667,34 @@ gtk_file_chooser_dialog_new_with_backend
#endif
#endif
#if IN_HEADER(__GTK_FILE_CHOOSER_NATIVE_H__)
#if IN_FILE(__GTK_FILE_CHOOSER_NATIVE_C__)
gtk_file_chooser_native_get_accept_label
gtk_file_chooser_native_get_cancel_label
gtk_file_chooser_native_get_type
gtk_file_chooser_native_new
gtk_file_chooser_native_set_accept_label
gtk_file_chooser_native_set_cancel_label
#endif
#endif
#if IN_HEADER(__GTK_NATIVE_DIALOG_H__)
#if IN_FILE(__GTK_NATIVE_DIALOG_C__)
gtk_native_dialog_destroy
gtk_native_dialog_get_modal
gtk_native_dialog_get_title
gtk_native_dialog_get_transient_for
gtk_native_dialog_get_type
gtk_native_dialog_get_visible
gtk_native_dialog_hide
gtk_native_dialog_run
gtk_native_dialog_set_modal
gtk_native_dialog_set_title
gtk_native_dialog_set_transient_for
gtk_native_dialog_show
#endif
#endif
#if IN_HEADER(__GTK_FILE_CHOOSER_WIDGET_H__)
#if IN_FILE(__GTK_FILE_CHOOSER_WIDGET_C__)
gtk_file_chooser_widget_get_type
@ -1705,7 +1733,9 @@ gtk_file_filter_get_name
gtk_file_filter_get_needed
gtk_file_filter_get_type
gtk_file_filter_new
gtk_file_filter_new_from_gvariant
gtk_file_filter_set_name
gtk_file_filter_to_gvariant
#endif
#endif

View file

@ -639,7 +639,8 @@ gtk_file_chooser_get_type (void)
(GClassInitFunc) gtk_file_chooser_class_init,
0, NULL, 0);
g_type_interface_add_prerequisite (file_chooser_type, GTK_TYPE_WIDGET);
/* G_TYPE_OBJECT, as in GTK+ 3: GtkFileChooserNative is no widget */
g_type_interface_add_prerequisite (file_chooser_type, G_TYPE_OBJECT);
}
return file_chooser_type;

657
gtk/gtkfilechoosernative.c Normal file
View file

@ -0,0 +1,657 @@
/* 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* GtkFileChooserNative as in GTK+ 3.20: the file dialog of Windows (also
* XP and ReactOS) and Mac OS X, the one of the desktop portal where
* GTK_USE_PORTAL=1 or in Flatpak, and a GtkFileChooserDialog
* elsewhere. It keeps its GtkFileChooser state in a hidden
* GtkFileChooserDialog, which is the fallback too. Pale Moon and
* Firefox look for it at run time (widget.allow-gtk-native-file-chooser). */
#include "config.h"
#include <string.h>
#include "gtkfilechoosernative.h"
#include "gtkfilechoosernativeprivate.h"
#include "gtknativedialogprivate.h"
#include "gtkfilechooserdialog.h"
#include "gtkfilechooserprivate.h"
#include "gtkfilechooserutils.h"
#include "gtkbutton.h"
#include "gtkstock.h"
#include "gtkintl.h"
#include "gtkprivate.h"
#include "gtkalias.h"
/**
* SECTION:gtkfilechoosernative
* @Short_description: A native file chooser dialog, suitable for "File/Open" or "File/Save" commands
* @Title: GtkFileChooserNative
* @See_also: #GtkFileChooser, #GtkNativeDialog, #GtkFileChooserDialog
*
* #GtkFileChooserNative is an abstraction of a dialog box suitable
* for use with "File/Open" or "File/Save as" commands. By default, this
* just uses a #GtkFileChooserDialog to implement the actual dialog.
* However, on Windows and Mac OS X, it uses the native file chooser
* dialog of the system, and where GTK_USE_PORTAL=1 is set or in a
* Flatpak sandbox, the file chooser of the desktop portal.
*
* As with GTK+ 3.20, preview widgets and extra widgets are not shown
* by the native dialogs, and a filter with custom rules makes the
* Windows and Mac OS X dialogs fall back to the #GtkFileChooserDialog.
*/
enum {
MODE_FALLBACK,
MODE_WIN32,
MODE_QUARTZ,
MODE_PORTAL
};
enum {
PROP_0,
PROP_ACCEPT_LABEL,
PROP_CANCEL_LABEL,
LAST_ARG
};
static GParamSpec *native_props[LAST_ARG] = { NULL, };
static void _gtk_file_chooser_native_iface_init (GtkFileChooserIface *iface);
G_DEFINE_TYPE_WITH_CODE (GtkFileChooserNative, gtk_file_chooser_native, GTK_TYPE_NATIVE_DIALOG,
G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
_gtk_file_chooser_native_iface_init))
/**
* gtk_file_chooser_native_get_accept_label:
* @self: a #GtkFileChooserNative
*
* Retrieves the custom label text for the accept button.
*
* Returns: (nullable): The custom label, or %NULL for the default. This string
* is owned by GTK+ and should not be modified or freed
*
* Since: 2.24
**/
const char *
gtk_file_chooser_native_get_accept_label (GtkFileChooserNative *self)
{
g_return_val_if_fail (GTK_IS_FILE_CHOOSER_NATIVE (self), NULL);
return self->accept_label;
}
/**
* gtk_file_chooser_native_set_accept_label:
* @self: a #GtkFileChooserNative
* @accept_label: (allow-none): custom label or %NULL for the default
*
* Sets the custom label text for the accept button.
*
* If characters in @label are preceded by an underscore, they are underlined.
* If you need a literal underscore character in a label, use "__" (two
* underscores). The first underlined character represents a keyboard
* accelerator called a mnemonic.
* Pressing Alt and that key activates the button.
*
* Since: 2.24
**/
void
gtk_file_chooser_native_set_accept_label (GtkFileChooserNative *self,
const char *accept_label)
{
g_return_if_fail (GTK_IS_FILE_CHOOSER_NATIVE (self));
g_free (self->accept_label);
self->accept_label = g_strdup (accept_label);
g_object_notify_by_pspec (G_OBJECT (self), native_props[PROP_ACCEPT_LABEL]);
}
/**
* gtk_file_chooser_native_get_cancel_label:
* @self: a #GtkFileChooserNative
*
* Retrieves the custom label text for the cancel button.
*
* Returns: (nullable): The custom label, or %NULL for the default. This string
* is owned by GTK+ and should not be modified or freed
*
* Since: 2.24
**/
const char *
gtk_file_chooser_native_get_cancel_label (GtkFileChooserNative *self)
{
g_return_val_if_fail (GTK_IS_FILE_CHOOSER_NATIVE (self), NULL);
return self->cancel_label;
}
/**
* gtk_file_chooser_native_set_cancel_label:
* @self: a #GtkFileChooserNative
* @cancel_label: (allow-none): custom label or %NULL for the default
*
* Sets the custom label text for the cancel button.
*
* If characters in @label are preceded by an underscore, they are underlined.
* If you need a literal underscore character in a label, use "__" (two
* underscores). The first underlined character represents a keyboard
* accelerator called a mnemonic.
* Pressing Alt and that key activates the button.
*
* Since: 2.24
**/
void
gtk_file_chooser_native_set_cancel_label (GtkFileChooserNative *self,
const char *cancel_label)
{
g_return_if_fail (GTK_IS_FILE_CHOOSER_NATIVE (self));
g_free (self->cancel_label);
self->cancel_label = g_strdup (cancel_label);
g_object_notify_by_pspec (G_OBJECT (self), native_props[PROP_CANCEL_LABEL]);
}
/* "_Save" -> "Save", "__" -> "_" */
gchar *
_gtk_file_chooser_native_strip_mnemonic (const gchar *label)
{
GString *s;
const gchar *p;
if (label == NULL)
return NULL;
s = g_string_new (NULL);
for (p = label; *p; p++)
{
if (*p == '_')
{
if (p[1] == '_')
p++;
else
continue;
}
g_string_append_c (s, *p);
}
return g_string_free (s, FALSE);
}
static void
gtk_file_chooser_native_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkFileChooserNative *self = GTK_FILE_CHOOSER_NATIVE (object);
switch (prop_id)
{
case PROP_ACCEPT_LABEL:
gtk_file_chooser_native_set_accept_label (self, g_value_get_string (value));
break;
case PROP_CANCEL_LABEL:
gtk_file_chooser_native_set_cancel_label (self, g_value_get_string (value));
break;
case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
/* construct-only, and the dialog is made already */
break;
case GTK_FILE_CHOOSER_PROP_FILTER:
self->current_filter = g_value_get_object (value);
gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (self->dialog), self->current_filter);
g_object_notify (G_OBJECT (self), "filter");
break;
default:
g_object_set_property (G_OBJECT (self->dialog), pspec->name, value);
break;
}
}
static void
gtk_file_chooser_native_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkFileChooserNative *self = GTK_FILE_CHOOSER_NATIVE (object);
switch (prop_id)
{
case PROP_ACCEPT_LABEL:
g_value_set_string (value, self->accept_label);
break;
case PROP_CANCEL_LABEL:
g_value_set_string (value, self->cancel_label);
break;
case GTK_FILE_CHOOSER_PROP_FILTER:
self->current_filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (self->dialog));
g_value_set_object (value, self->current_filter);
break;
default:
g_object_get_property (G_OBJECT (self->dialog), pspec->name, value);
break;
}
}
static void
gtk_file_chooser_native_finalize (GObject *object)
{
GtkFileChooserNative *self = GTK_FILE_CHOOSER_NATIVE (object);
g_free (self->current_name);
if (self->current_file)
g_object_unref (self->current_file);
if (self->current_folder)
g_object_unref (self->current_folder);
g_free (self->accept_label);
g_free (self->cancel_label);
gtk_widget_destroy (self->dialog);
g_object_unref (self->dialog);
g_slist_foreach (self->custom_files, (GFunc) g_object_unref, NULL);
g_slist_free (self->custom_files);
G_OBJECT_CLASS (gtk_file_chooser_native_parent_class)->finalize (object);
}
static gint
override_delete_handler (GtkDialog *dialog,
GdkEventAny *event,
gpointer data)
{
return TRUE; /* Do not destroy */
}
static void
gtk_file_chooser_native_init (GtkFileChooserNative *self)
{
/* All properties go to a file chooser dialog: it keeps the state, and
* it is there when the native one cannot be used */
self->dialog = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG, NULL);
g_object_ref_sink (self->dialog);
self->cancel_button = gtk_dialog_add_button (GTK_DIALOG (self->dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
self->accept_button = gtk_dialog_add_button (GTK_DIALOG (self->dialog), GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT);
gtk_dialog_set_default_response (GTK_DIALOG (self->dialog),
GTK_RESPONSE_ACCEPT);
gtk_dialog_set_alternative_button_order (GTK_DIALOG (self->dialog),
GTK_RESPONSE_ACCEPT,
GTK_RESPONSE_CANCEL,
-1);
/* not destroyed on delete events, but hidden in the response handler */
g_signal_connect (self->dialog,
"delete-event",
G_CALLBACK (override_delete_handler),
NULL);
/* The delegate of the methods, set without _gtk_file_chooser_set_delegate()
* so that its signals do not come twice */
g_object_set_qdata (G_OBJECT (self), GTK_FILE_CHOOSER_DELEGATE_QUARK, self->dialog);
}
/**
* gtk_file_chooser_native_new:
* @title: (allow-none): Title of the native, or %NULL
* @parent: (allow-none): Transient parent of the native, or %NULL
* @action: Open or save mode for the dialog
* @accept_label: (allow-none): text to go in the accept button, or %NULL for the default
* @cancel_label: (allow-none): text to go in the cancel button, or %NULL for the default
*
* Creates a new #GtkFileChooserNative.
*
* Returns: a new #GtkFileChooserNative
*
* Since: 2.24
**/
GtkFileChooserNative *
gtk_file_chooser_native_new (const gchar *title,
GtkWindow *parent,
GtkFileChooserAction action,
const gchar *accept_label,
const gchar *cancel_label)
{
return g_object_new (GTK_TYPE_FILE_CHOOSER_NATIVE,
"title", title,
"action", action,
"transient-for", parent,
"accept-label", accept_label,
"cancel-label", cancel_label,
NULL);
}
static void
dialog_response_cb (GtkDialog *dialog,
gint response_id,
gpointer data)
{
GtkFileChooserNative *self = data;
g_signal_handlers_disconnect_by_func (self->dialog, dialog_response_cb, self);
gtk_widget_hide (self->dialog);
_gtk_native_dialog_emit_response (GTK_NATIVE_DIALOG (self), response_id);
}
static void
dialog_update_preview_cb (GtkFileChooser *file_chooser,
gpointer data)
{
g_signal_emit_by_name (data, "update-preview");
}
static void
set_button_label (GtkWidget *button,
const gchar *label,
const gchar *stock_id)
{
if (label)
{
gtk_button_set_use_stock (GTK_BUTTON (button), FALSE);
gtk_button_set_image (GTK_BUTTON (button), NULL);
gtk_button_set_use_underline (GTK_BUTTON (button), TRUE);
gtk_button_set_label (GTK_BUTTON (button), label);
}
else
{
gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
gtk_button_set_label (GTK_BUTTON (button), stock_id);
}
}
static void
show_dialog (GtkFileChooserNative *self)
{
GtkFileChooserAction action;
action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (self->dialog));
set_button_label (self->accept_button, self->accept_label,
action == GTK_FILE_CHOOSER_ACTION_SAVE ? GTK_STOCK_SAVE : GTK_STOCK_OPEN);
set_button_label (self->cancel_button, self->cancel_label, GTK_STOCK_CANCEL);
gtk_window_set_title (GTK_WINDOW (self->dialog),
gtk_native_dialog_get_title (GTK_NATIVE_DIALOG (self)));
gtk_window_set_transient_for (GTK_WINDOW (self->dialog),
gtk_native_dialog_get_transient_for (GTK_NATIVE_DIALOG (self)));
gtk_window_set_modal (GTK_WINDOW (self->dialog),
gtk_native_dialog_get_modal (GTK_NATIVE_DIALOG (self)));
g_signal_connect (self->dialog,
"response",
G_CALLBACK (dialog_response_cb),
self);
g_signal_connect (self->dialog,
"update-preview",
G_CALLBACK (dialog_update_preview_cb),
self);
gtk_window_present (GTK_WINDOW (self->dialog));
}
void
_gtk_file_chooser_native_show_fallback (GtkFileChooserNative *self)
{
self->mode = MODE_FALLBACK;
show_dialog (self);
}
static void
hide_dialog (GtkFileChooserNative *self)
{
g_signal_handlers_disconnect_by_func (self->dialog, dialog_response_cb, self);
g_signal_handlers_disconnect_by_func (self->dialog, dialog_update_preview_cb, self);
gtk_widget_hide (self->dialog);
}
static void
set_object (GFile **where,
GFile *file)
{
if (file)
g_object_ref (file);
if (*where)
g_object_unref (*where);
*where = file;
}
static gboolean
gtk_file_chooser_native_set_current_folder (GtkFileChooser *chooser,
GFile *file,
GError **error)
{
GtkFileChooserNative *self = GTK_FILE_CHOOSER_NATIVE (chooser);
gboolean res;
res = gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (self->dialog),
file, error);
if (res)
{
set_object (&self->current_folder, file);
set_object (&self->current_file, NULL);
}
return res;
}
static gboolean
gtk_file_chooser_native_select_file (GtkFileChooser *chooser,
GFile *file,
GError **error)
{
GtkFileChooserNative *self = GTK_FILE_CHOOSER_NATIVE (chooser);
gboolean res;
res = gtk_file_chooser_select_file (GTK_FILE_CHOOSER (self->dialog),
file, error);
if (res)
{
set_object (&self->current_file, file);
set_object (&self->current_folder, NULL);
g_free (self->current_name);
self->current_name = NULL;
}
return res;
}
static void
gtk_file_chooser_native_set_current_name (GtkFileChooser *chooser,
const gchar *name)
{
GtkFileChooserNative *self = GTK_FILE_CHOOSER_NATIVE (chooser);
gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (self->dialog), name);
g_free (self->current_name);
self->current_name = g_strdup (name);
set_object (&self->current_file, NULL);
}
static GSList *
gtk_file_chooser_native_get_files (GtkFileChooser *chooser)
{
GtkFileChooserNative *self = GTK_FILE_CHOOSER_NATIVE (chooser);
GSList *l, *copy = NULL;
switch (self->mode)
{
case MODE_PORTAL:
case MODE_WIN32:
case MODE_QUARTZ:
for (l = self->custom_files; l; l = l->next)
copy = g_slist_prepend (copy, g_object_ref (l->data));
return g_slist_reverse (copy);
case MODE_FALLBACK:
default:
return gtk_file_chooser_get_files (GTK_FILE_CHOOSER (self->dialog));
}
}
static GFile *
gtk_file_chooser_native_get_current_folder (GtkFileChooser *chooser)
{
GtkFileChooserNative *self = GTK_FILE_CHOOSER_NATIVE (chooser);
/* after a native dialog, the folder of what was chosen */
if (self->mode != MODE_FALLBACK && self->custom_files)
{
GFile *first = self->custom_files->data;
if (gtk_file_chooser_get_action (chooser) == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
gtk_file_chooser_get_action (chooser) == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
return g_object_ref (first);
return g_file_get_parent (first);
}
return gtk_file_chooser_get_current_folder_file (GTK_FILE_CHOOSER (self->dialog));
}
static void
gtk_file_chooser_native_show (GtkNativeDialog *native)
{
GtkFileChooserNative *self = GTK_FILE_CHOOSER_NATIVE (native);
self->mode = MODE_FALLBACK;
#ifdef GDK_WINDOWING_WIN32
if (_gtk_file_chooser_native_win32_show (self))
self->mode = MODE_WIN32;
#endif
#ifdef GDK_WINDOWING_QUARTZ
if (_gtk_file_chooser_native_quartz_show (self))
self->mode = MODE_QUARTZ;
#endif
if (self->mode == MODE_FALLBACK &&
_gtk_file_chooser_native_portal_show (self))
self->mode = MODE_PORTAL;
if (self->mode == MODE_FALLBACK)
show_dialog (self);
}
static void
gtk_file_chooser_native_hide (GtkNativeDialog *native)
{
GtkFileChooserNative *self = GTK_FILE_CHOOSER_NATIVE (native);
switch (self->mode)
{
case MODE_FALLBACK:
hide_dialog (self);
break;
case MODE_WIN32:
#ifdef GDK_WINDOWING_WIN32
_gtk_file_chooser_native_win32_hide (self);
#endif
break;
case MODE_QUARTZ:
#ifdef GDK_WINDOWING_QUARTZ
_gtk_file_chooser_native_quartz_hide (self);
#endif
break;
case MODE_PORTAL:
_gtk_file_chooser_native_portal_hide (self);
break;
}
}
static void
gtk_file_chooser_native_class_init (GtkFileChooserNativeClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
GtkNativeDialogClass *native_dialog_class = GTK_NATIVE_DIALOG_CLASS (class);
gobject_class->finalize = gtk_file_chooser_native_finalize;
gobject_class->set_property = gtk_file_chooser_native_set_property;
gobject_class->get_property = gtk_file_chooser_native_get_property;
native_dialog_class->show = gtk_file_chooser_native_show;
native_dialog_class->hide = gtk_file_chooser_native_hide;
_gtk_file_chooser_install_properties (gobject_class);
/**
* GtkFileChooserNative:accept-label:
*
* The text used for the label on the accept button in the dialog, or
* %NULL to use the default text.
*
* Since: 2.24
*/
native_props[PROP_ACCEPT_LABEL] =
g_param_spec_string ("accept-label",
P_("Accept label"),
P_("The label on the accept button"),
NULL,
GTK_PARAM_READWRITE);
/**
* GtkFileChooserNative:cancel-label:
*
* The text used for the label on the cancel button in the dialog, or
* %NULL to use the default text.
*
* Since: 2.24
*/
native_props[PROP_CANCEL_LABEL] =
g_param_spec_string ("cancel-label",
P_("Cancel label"),
P_("The label on the cancel button"),
NULL,
GTK_PARAM_READWRITE);
g_object_class_install_property (gobject_class, PROP_ACCEPT_LABEL, native_props[PROP_ACCEPT_LABEL]);
g_object_class_install_property (gobject_class, PROP_CANCEL_LABEL, native_props[PROP_CANCEL_LABEL]);
}
static void
_gtk_file_chooser_native_iface_init (GtkFileChooserIface *iface)
{
_gtk_file_chooser_delegate_iface_init (iface);
iface->select_file = gtk_file_chooser_native_select_file;
iface->set_current_name = gtk_file_chooser_native_set_current_name;
iface->set_current_folder = gtk_file_chooser_native_set_current_folder;
iface->get_current_folder = gtk_file_chooser_native_get_current_folder;
iface->get_files = gtk_file_chooser_native_get_files;
}
#define __GTK_FILE_CHOOSER_NATIVE_C__
#include "gtkaliasdef.c"

View file

@ -0,0 +1,68 @@
/* GTK - The GIMP Toolkit
* gtkfilechoosernative.h: 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GTK_FILE_CHOOSER_NATIVE_H__
#define __GTK_FILE_CHOOSER_NATIVE_H__
#if defined(GTK_DISABLE_SINGLE_INCLUDES) && !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gtk/gtk.h> can be included directly."
#endif
#include <gtk/gtkfilechooser.h>
#include <gtk/gtknativedialog.h>
G_BEGIN_DECLS
#define GTK_TYPE_FILE_CHOOSER_NATIVE (gtk_file_chooser_native_get_type ())
#define GTK_FILE_CHOOSER_NATIVE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_FILE_CHOOSER_NATIVE, GtkFileChooserNative))
#define GTK_FILE_CHOOSER_NATIVE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_NATIVE, GtkFileChooserNativeClass))
#define GTK_IS_FILE_CHOOSER_NATIVE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_FILE_CHOOSER_NATIVE))
#define GTK_IS_FILE_CHOOSER_NATIVE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_NATIVE))
#define GTK_FILE_CHOOSER_NATIVE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_NATIVE, GtkFileChooserNativeClass))
/* The file dialog of the system - Windows, Mac OS X, or the desktop
* portal where GTK_USE_PORTAL=1 or in Flatpak - with the GtkFileChooser
* interface and the API of GTK+ 3.20; elsewhere a GtkFileChooserDialog */
typedef struct _GtkFileChooserNative GtkFileChooserNative;
typedef struct _GtkFileChooserNativeClass GtkFileChooserNativeClass;
struct _GtkFileChooserNativeClass
{
GtkNativeDialogClass parent_class;
};
GType gtk_file_chooser_native_get_type (void);
GtkFileChooserNative *gtk_file_chooser_native_new (const gchar *title,
GtkWindow *parent,
GtkFileChooserAction action,
const gchar *accept_label,
const gchar *cancel_label);
const char * gtk_file_chooser_native_get_accept_label (GtkFileChooserNative *self);
void gtk_file_chooser_native_set_accept_label (GtkFileChooserNative *self,
const char *accept_label);
const char * gtk_file_chooser_native_get_cancel_label (GtkFileChooserNative *self);
void gtk_file_chooser_native_set_cancel_label (GtkFileChooserNative *self,
const char *cancel_label);
G_END_DECLS
#endif /* __GTK_FILE_CHOOSER_NATIVE_H__ */

View file

@ -0,0 +1,585 @@
/* GTK - The GIMP Toolkit
* gtkfilechoosernativeportal.c: Portal 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* The file chooser of the desktop portal (org.freedesktop.portal.FileChooser,
* from xdg-desktop-portal): the dialog of the desktop (KDE, GNOME, ...),
* and the way out of a Flatpak sandbox. Used where GTK_USE_PORTAL=1 is
* set or in Flatpak, as GTK+ 3 does; on X11 only, with the window of the
* program as "x11:XID". */
#include "config.h"
#include <string.h>
#include "gtkfilechoosernativeprivate.h"
#include "gtknativedialogprivate.h"
#include "gtkfilechooser.h"
#include "gtkfilefilter.h"
#include "gtkinvisible.h"
#include "gtkmain.h"
#include "gtkdialog.h"
#include "gtkprivate.h"
#include "gtkalias.h"
#ifdef GDK_WINDOWING_X11
#include "x11/gdkx.h"
#endif
typedef struct {
GtkFileChooserNative *self;
GtkWidget *grab_widget;
GDBusConnection *connection;
char *portal_handle;
guint portal_response_signal_id;
gboolean modal;
gboolean hidden;
gboolean call_pending; /* the callback of the call frees the data */
const char *method_name;
} FilechooserPortalData;
/* as GTK+ 3: the portal in a Flatpak sandbox, or where asked for */
static gboolean
should_use_portal (void)
{
static const char *use_portal = NULL;
if (G_UNLIKELY (use_portal == NULL))
{
if (g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS))
use_portal = "1";
else
{
use_portal = g_getenv ("GTK_USE_PORTAL");
if (!use_portal)
use_portal = "";
}
}
return use_portal[0] == '1';
}
static guint
get_portal_interface_version (GDBusConnection *connection,
const char *interface_name)
{
GError *error = NULL;
GVariant *ret;
guint version = 0;
ret = g_dbus_connection_call_sync (connection,
"org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.DBus.Properties",
"Get",
g_variant_new ("(ss)", interface_name, "version"),
G_VARIANT_TYPE ("(v)"),
G_DBUS_CALL_FLAGS_NONE,
5000,
NULL,
&error);
if (ret == NULL)
{
g_error_free (error);
return 0;
}
else
{
GVariant *v;
g_variant_get (ret, "(v)", &v);
if (g_variant_is_of_type (v, G_VARIANT_TYPE_UINT32))
version = g_variant_get_uint32 (v);
g_variant_unref (v);
g_variant_unref (ret);
}
return version;
}
/* where the portal answers a request with @token */
static char *
get_portal_request_path (GDBusConnection *connection,
char **token)
{
static int counter = 0;
char *sender, *path;
int i;
*token = g_strdup_printf ("gtk%d", counter++);
sender = g_strdup (g_dbus_connection_get_unique_name (connection) + 1);
for (i = 0; sender[i]; i++)
if (sender[i] == '.')
sender[i] = '_';
path = g_strconcat ("/org/freedesktop/portal/desktop/request/", sender, "/", *token, NULL);
g_free (sender);
return path;
}
static void
filechooser_portal_data_clear (FilechooserPortalData *data)
{
if (data->portal_response_signal_id != 0)
{
g_dbus_connection_signal_unsubscribe (data->connection,
data->portal_response_signal_id);
data->portal_response_signal_id = 0;
}
if (data->connection)
{
g_object_unref (data->connection);
data->connection = NULL;
}
if (data->grab_widget)
{
gtk_grab_remove (data->grab_widget);
gtk_widget_destroy (data->grab_widget);
data->grab_widget = NULL;
}
if (data->self)
{
g_object_unref (data->self);
data->self = NULL;
}
g_free (data->portal_handle);
data->portal_handle = NULL;
}
static void
filechooser_portal_data_free (FilechooserPortalData *data)
{
if (data != NULL)
{
filechooser_portal_data_clear (data);
g_free (data);
}
}
static void
response_cb (GDBusConnection *connection,
const char *sender_name,
const char *object_path,
const char *interface_name,
const char *signal_name,
GVariant *parameters,
gpointer user_data)
{
GtkFileChooserNative *self = user_data;
FilechooserPortalData *data = self->mode_data;
guint32 portal_response;
int gtk_response;
const char **uris = NULL;
int i;
GVariant *response_data;
GVariant *current_filter;
g_variant_get (parameters, "(u@a{sv})", &portal_response, &response_data);
if (!g_variant_lookup (response_data, "uris", "^a&s", &uris))
uris = NULL;
current_filter = g_variant_lookup_value (response_data, "current_filter", G_VARIANT_TYPE ("(sa(us))"));
if (current_filter)
{
GtkFileFilter *filter = gtk_file_filter_new_from_gvariant (current_filter);
const char *current_filter_name = gtk_file_filter_get_name (filter);
GtkFileFilter *filter_to_select = filter;
GSList *filters, *l;
/* Filters are compared by pointer: the one of the list with the
* same name is the one chosen */
filters = gtk_file_chooser_list_filters (GTK_FILE_CHOOSER (self));
for (l = filters; l; l = l->next)
{
GtkFileFilter *f = l->data;
if (g_strcmp0 (gtk_file_filter_get_name (f), current_filter_name) == 0)
{
filter_to_select = f;
break;
}
}
g_slist_free (filters);
g_object_ref_sink (filter);
gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (self), filter_to_select);
g_object_unref (filter);
g_variant_unref (current_filter);
}
g_slist_foreach (self->custom_files, (GFunc) g_object_unref, NULL);
g_slist_free (self->custom_files);
self->custom_files = NULL;
for (i = 0; uris && uris[i]; i++)
self->custom_files = g_slist_prepend (self->custom_files, g_file_new_for_uri (uris[i]));
self->custom_files = g_slist_reverse (self->custom_files);
g_free (uris);
g_variant_unref (response_data);
switch (portal_response)
{
case 0:
gtk_response = GTK_RESPONSE_ACCEPT;
break;
case 1:
gtk_response = GTK_RESPONSE_CANCEL;
break;
case 2:
default:
gtk_response = GTK_RESPONSE_DELETE_EVENT;
break;
}
/* the native dialog lives until the response is out; the data held
* a reference too */
g_object_ref (self);
filechooser_portal_data_free (data);
self->mode_data = NULL;
_gtk_native_dialog_emit_response (GTK_NATIVE_DIALOG (self), gtk_response);
g_object_unref (self);
}
static void
send_close (FilechooserPortalData *data)
{
GDBusMessage *message;
GError *error = NULL;
message = g_dbus_message_new_method_call ("org.freedesktop.portal.Desktop",
data->portal_handle,
"org.freedesktop.portal.Request",
"Close");
if (!g_dbus_connection_send_message (data->connection,
message,
G_DBUS_SEND_MESSAGE_FLAGS_NONE,
NULL, &error))
{
g_warning ("unable to send FileChooser Close message: %s", error->message);
g_error_free (error);
}
g_object_unref (message);
}
static void
open_file_msg_cb (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
FilechooserPortalData *data = user_data;
GtkFileChooserNative *self = data->self;
GDBusMessage *reply;
GError *error = NULL;
char *handle = NULL;
data->call_pending = FALSE;
reply = g_dbus_connection_send_message_with_reply_finish (G_DBUS_CONNECTION (source_object), res, &error);
if (reply && g_dbus_message_to_gerror (reply, &error))
{
g_object_unref (reply);
reply = NULL;
}
if (reply == NULL)
{
if (!data->hidden)
{
/* no portal after all: the user still gets a dialog */
g_object_ref (self);
filechooser_portal_data_free (data);
self->mode_data = NULL;
_gtk_file_chooser_native_show_fallback (self);
g_object_unref (self);
}
else
g_free (data);
if (error)
g_error_free (error);
return;
}
g_variant_get_child (g_dbus_message_get_body (reply), 0, "o", &handle);
if (data->hidden)
{
/* hidden before the handle came: closed now; the data was
* cleared then */
g_free (data);
}
else if (strcmp (handle, data->portal_handle) != 0)
{
/* an older portal that does not know handle_token */
g_free (data->portal_handle);
data->portal_handle = handle;
handle = NULL;
g_dbus_connection_signal_unsubscribe (data->connection,
data->portal_response_signal_id);
data->portal_response_signal_id =
g_dbus_connection_signal_subscribe (data->connection,
"org.freedesktop.portal.Desktop",
"org.freedesktop.portal.Request",
"Response",
data->portal_handle,
NULL,
G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
response_cb,
self, NULL);
}
g_object_unref (reply);
g_free (handle);
}
static GVariant *
get_filters (GtkFileChooser *self)
{
GSList *list, *l;
GVariantBuilder builder;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(sa(us))"));
list = gtk_file_chooser_list_filters (self);
for (l = list; l; l = l->next)
{
GtkFileFilter *filter = l->data;
g_variant_builder_add (&builder, "@(sa(us))", gtk_file_filter_to_gvariant (filter));
}
g_slist_free (list);
return g_variant_builder_end (&builder);
}
static void
show_portal_file_chooser (GtkFileChooserNative *self,
const char *parent_window_str)
{
FilechooserPortalData *data = self->mode_data;
GDBusMessage *message;
GVariantBuilder opt_builder;
gboolean multiple;
gboolean directory;
const char *title;
char *token;
GtkFileFilter *current_filter;
message = g_dbus_message_new_method_call ("org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.FileChooser",
data->method_name);
data->portal_handle = get_portal_request_path (data->connection, &token);
data->portal_response_signal_id =
g_dbus_connection_signal_subscribe (data->connection,
"org.freedesktop.portal.Desktop",
"org.freedesktop.portal.Request",
"Response",
data->portal_handle,
NULL,
G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
response_cb,
self, NULL);
multiple = gtk_file_chooser_get_select_multiple (GTK_FILE_CHOOSER (self));
directory = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (self)) == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add (&opt_builder, "{sv}", "handle_token",
g_variant_new_string (token));
g_free (token);
g_variant_builder_add (&opt_builder, "{sv}", "multiple",
g_variant_new_boolean (multiple));
g_variant_builder_add (&opt_builder, "{sv}", "directory",
g_variant_new_boolean (directory));
if (self->accept_label)
g_variant_builder_add (&opt_builder, "{sv}", "accept_label",
g_variant_new_string (self->accept_label));
if (self->cancel_label)
g_variant_builder_add (&opt_builder, "{sv}", "cancel_label",
g_variant_new_string (self->cancel_label));
g_variant_builder_add (&opt_builder, "{sv}", "modal",
g_variant_new_boolean (data->modal));
g_variant_builder_add (&opt_builder, "{sv}", "filters", get_filters (GTK_FILE_CHOOSER (self)));
current_filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (self->dialog));
if (current_filter)
g_variant_builder_add (&opt_builder, "{sv}", "current_filter",
gtk_file_filter_to_gvariant (current_filter));
if (self->current_name)
g_variant_builder_add (&opt_builder, "{sv}", "current_name",
g_variant_new_string (self->current_name));
if (self->current_folder)
{
char *path = g_file_get_path (self->current_folder);
if (path)
g_variant_builder_add (&opt_builder, "{sv}", "current_folder",
g_variant_new_bytestring (path));
g_free (path);
}
if (self->current_file)
{
char *path = g_file_get_path (self->current_file);
if (path)
g_variant_builder_add (&opt_builder, "{sv}", "current_file",
g_variant_new_bytestring (path));
g_free (path);
}
title = gtk_native_dialog_get_title (GTK_NATIVE_DIALOG (self));
g_dbus_message_set_body (message,
g_variant_new ("(ss@a{sv})",
parent_window_str ? parent_window_str : "",
title ? title : "",
g_variant_builder_end (&opt_builder)));
data->call_pending = TRUE;
g_dbus_connection_send_message_with_reply (data->connection,
message,
G_DBUS_SEND_MESSAGE_FLAGS_NONE,
G_MAXINT,
NULL,
NULL,
open_file_msg_cb,
data);
g_object_unref (message);
}
gboolean
_gtk_file_chooser_native_portal_show (GtkFileChooserNative *self)
{
FilechooserPortalData *data;
GtkWindow *transient_for;
GDBusConnection *connection;
GtkFileChooserAction action;
const char *method_name;
char *handle_str = NULL;
if (!should_use_portal ())
return FALSE;
/* The session bus is kept once used: the bus connection is shared and
* goes away with the last reference, and with it a Close not sent yet */
{
static GDBusConnection *session_bus;
if (session_bus == NULL)
session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
if (session_bus == NULL || g_dbus_connection_is_closed (session_bus))
return FALSE;
connection = g_object_ref (session_bus);
}
action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (self));
if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
method_name = "OpenFile";
else if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
method_name = "SaveFile";
else if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
{
if (get_portal_interface_version (connection, "org.freedesktop.portal.FileChooser") < 3)
{
g_object_unref (connection);
return FALSE;
}
method_name = "OpenFile";
}
else
{
/* CREATE_FOLDER: the portal has nothing for it */
g_object_unref (connection);
return FALSE;
}
data = g_new0 (FilechooserPortalData, 1);
data->self = g_object_ref (self);
data->connection = connection;
data->method_name = method_name;
data->modal = gtk_native_dialog_get_modal (GTK_NATIVE_DIALOG (self));
self->mode_data = data;
transient_for = gtk_native_dialog_get_transient_for (GTK_NATIVE_DIALOG (self));
#ifdef GDK_WINDOWING_X11
if (transient_for != NULL && gtk_widget_get_realized (GTK_WIDGET (transient_for)) &&
GDK_IS_WINDOW (gtk_widget_get_window (GTK_WIDGET (transient_for))))
handle_str = g_strdup_printf ("x11:%lx",
(gulong) GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (transient_for))));
#endif
/* modal: the windows of the program take no input meanwhile */
if (data->modal)
{
GdkScreen *screen = transient_for ? gtk_widget_get_screen (GTK_WIDGET (transient_for))
: gdk_screen_get_default ();
data->grab_widget = gtk_invisible_new_for_screen (screen);
gtk_grab_add (data->grab_widget);
}
show_portal_file_chooser (self, handle_str);
g_free (handle_str);
return TRUE;
}
void
_gtk_file_chooser_native_portal_hide (GtkFileChooserNative *self)
{
FilechooserPortalData *data = self->mode_data;
/* set while the dialog is visible */
g_return_if_fail (data != NULL);
data->hidden = TRUE;
if (data->portal_handle)
send_close (data);
/* The call may still be on its way: then its callback frees the
* data, which is cleared here */
if (data->call_pending)
filechooser_portal_data_clear (data);
else
filechooser_portal_data_free (data);
self->mode_data = NULL;
}
#define __GTK_FILE_CHOOSER_NATIVE_PORTAL_C__
#include "gtkaliasdef.c"

View file

@ -0,0 +1,68 @@
/* GTK - The GIMP Toolkit
* gtkfilechoosernativeprivate.h: 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GTK_FILE_CHOOSER_NATIVE_PRIVATE_H__
#define __GTK_FILE_CHOOSER_NATIVE_PRIVATE_H__
#include <gtk/gtkfilechoosernative.h>
G_BEGIN_DECLS
struct _GtkFileChooserNative
{
GtkNativeDialog parent_instance;
char *accept_label;
char *cancel_label;
int mode;
GSList *custom_files;
GFile *current_folder;
GFile *current_file;
char *current_name;
GtkFileFilter *current_filter;
/* Fallback mode, and where the GtkFileChooser state is kept */
GtkWidget *dialog;
GtkWidget *accept_button;
GtkWidget *cancel_button;
gpointer mode_data;
};
gboolean _gtk_file_chooser_native_win32_show (GtkFileChooserNative *self);
void _gtk_file_chooser_native_win32_hide (GtkFileChooserNative *self);
gboolean _gtk_file_chooser_native_quartz_show (GtkFileChooserNative *self);
void _gtk_file_chooser_native_quartz_hide (GtkFileChooserNative *self);
gboolean _gtk_file_chooser_native_portal_show (GtkFileChooserNative *self);
void _gtk_file_chooser_native_portal_hide (GtkFileChooserNative *self);
/* the GtkFileChooserDialog after all, when a system dialog fails */
void _gtk_file_chooser_native_show_fallback (GtkFileChooserNative *self);
/* the labels without mnemonics, for the system dialogs */
gchar *_gtk_file_chooser_native_strip_mnemonic (const gchar *label);
G_END_DECLS
#endif /* __GTK_FILE_CHOOSER_NATIVE_PRIVATE_H__ */

View file

@ -0,0 +1,321 @@
/* GTK - The GIMP Toolkit
* gtkfilechoosernativequartz.c: Quartz Native File selector dialog
*
* 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* NSOpenPanel and NSSavePanel for GtkFileChooserNative, from Mac OS X
* 10.4 on: run modal (GTK+ 3 has sheets, which need the blocks of
* 10.6), from the main loop once gtk_native_dialog_show() has
* returned. The types of the current filter are the ones offered;
* a filter with rules that are not "*.ext" leaves the GTK+ dialog. */
#include "config.h"
#include <string.h>
#import <Cocoa/Cocoa.h>
#include "gtkfilechoosernativeprivate.h"
#include "gtknativedialogprivate.h"
#include "gtkfilechooser.h"
#include "gtkfilefilterprivate.h"
#include "gtkprivate.h"
#include <gdk/quartz/gdkquartz.h>
#include "gtkalias.h"
typedef struct {
GtkFileChooserNative *self;
gboolean skip_response;
gboolean save;
gboolean folder;
gboolean select_multiple;
gboolean show_hidden;
gboolean create_folders;
NSWindow *parent;
char *accept_label;
char *title;
char *directory;
char *name;
NSArray *types; /* extensions, nil for all */
guint idle;
} FileChooserQuartzData;
static void
filechooser_quartz_data_free (FileChooserQuartzData *data)
{
if (data->types)
[data->types release];
if (data->self)
g_object_unref (data->self);
g_free (data->accept_label);
g_free (data->title);
g_free (data->directory);
g_free (data->name);
g_free (data);
}
static GFile *
file_for_nsstring (NSString *path)
{
if (path == nil)
return NULL;
return g_file_new_for_path ([path fileSystemRepresentation]);
}
/* "*.png" -> "png"; nil if a pattern is anything else */
static NSArray *
types_of_filter (GtkFileFilter *filter,
gboolean *ok)
{
NSMutableArray *types;
char **patterns;
int i;
*ok = TRUE;
if (filter == NULL)
return nil;
patterns = _gtk_file_filter_get_as_patterns (filter);
if (patterns == NULL)
{
*ok = FALSE;
return nil;
}
types = [[NSMutableArray alloc] init];
for (i = 0; patterns[i]; i++)
{
const char *p = patterns[i];
if (strcmp (p, "*") == 0)
{
[types release];
g_strfreev (patterns);
return nil; /* all files */
}
if (strncmp (p, "*.", 2) != 0 || strpbrk (p + 2, "*?[") != NULL)
{
[types release];
g_strfreev (patterns);
*ok = FALSE;
return nil;
}
[types addObject:[NSString stringWithUTF8String:p + 2]];
}
g_strfreev (patterns);
return types;
}
static gboolean
filechooser_quartz_run (gpointer user_data)
{
FileChooserQuartzData *data = user_data;
GtkFileChooserNative *self = data->self;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSSavePanel *panel;
NSString *directory = data->directory ? [NSString stringWithUTF8String:data->directory] : nil;
NSString *name = data->name ? [NSString stringWithUTF8String:data->name] : nil;
NSInteger result;
GSList *files = NULL;
int response = GTK_RESPONSE_CANCEL;
data->idle = 0;
if (data->save && !data->folder)
{
panel = [NSSavePanel savePanel];
if ([panel respondsToSelector:@selector(setCanCreateDirectories:)])
[panel setCanCreateDirectories:data->create_folders];
if (data->types)
[panel setAllowedFileTypes:data->types];
}
else
{
NSOpenPanel *open = [NSOpenPanel openPanel];
[open setAllowsMultipleSelection:data->select_multiple];
[open setCanChooseDirectories:data->folder];
[open setCanChooseFiles:!data->folder];
if (data->folder && [open respondsToSelector:@selector(setCanCreateDirectories:)])
[open setCanCreateDirectories:YES];
panel = open;
}
if (data->show_hidden && [panel respondsToSelector:@selector(setShowsHiddenFiles:)])
[panel setShowsHiddenFiles:YES];
if (data->accept_label)
[panel setPrompt:[NSString stringWithUTF8String:data->accept_label]];
if (data->title)
[panel setTitle:[NSString stringWithUTF8String:data->title]];
if ([panel respondsToSelector:@selector(setDirectoryURL:)])
{
/* 10.6 and newer */
if (directory)
[panel setDirectoryURL:[NSURL fileURLWithPath:directory]];
if (name && [panel respondsToSelector:@selector(setNameFieldStringValue:)])
[panel setNameFieldStringValue:name];
if (!data->save && data->types)
[panel setAllowedFileTypes:data->types];
result = [panel runModal];
}
else if (data->save && !data->folder)
result = [panel runModalForDirectory:directory file:name];
else
result = [(NSOpenPanel *) panel runModalForDirectory:directory file:name types:data->types];
if (result == NSOKButton)
{
response = GTK_RESPONSE_ACCEPT;
if (data->save && !data->folder)
{
GFile *file = file_for_nsstring ([panel filename]);
if (file)
files = g_slist_prepend (files, file);
}
else
{
NSArray *names = [(NSOpenPanel *) panel filenames];
NSUInteger i;
for (i = 0; i < [names count]; i++)
{
GFile *file = file_for_nsstring ([names objectAtIndex:i]);
if (file)
files = g_slist_prepend (files, file);
}
}
}
if (data->parent)
[data->parent makeKeyAndOrderFront:nil];
[pool release];
self->mode_data = NULL;
if (!data->skip_response)
{
g_slist_foreach (self->custom_files, (GFunc) g_object_unref, NULL);
g_slist_free (self->custom_files);
self->custom_files = g_slist_reverse (files);
files = NULL;
_gtk_native_dialog_emit_response (GTK_NATIVE_DIALOG (self), response);
}
g_slist_foreach (files, (GFunc) g_object_unref, NULL);
g_slist_free (files);
filechooser_quartz_data_free (data);
return FALSE;
}
gboolean
_gtk_file_chooser_native_quartz_show (GtkFileChooserNative *self)
{
FileChooserQuartzData *data;
GtkFileChooserAction action;
GtkWindow *transient_for;
guint update_preview_signal;
NSArray *types;
gboolean ok;
/* what only the GTK+ dialog can show */
if (gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (self)) != NULL)
return FALSE;
update_preview_signal = g_signal_lookup ("update-preview", GTK_TYPE_FILE_CHOOSER);
if (g_signal_has_handler_pending (self, update_preview_signal, 0, TRUE))
return FALSE;
types = types_of_filter (gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (self)), &ok);
if (!ok)
return FALSE;
data = g_new0 (FileChooserQuartzData, 1);
data->self = g_object_ref (self);
data->types = types;
action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (self->dialog));
data->save = action == GTK_FILE_CHOOSER_ACTION_SAVE || action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
data->folder = action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER || action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
data->select_multiple = !data->save &&
gtk_file_chooser_get_select_multiple (GTK_FILE_CHOOSER (self->dialog));
data->show_hidden = gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (self->dialog));
data->create_folders = gtk_file_chooser_get_create_folders (GTK_FILE_CHOOSER (self->dialog));
data->accept_label = _gtk_file_chooser_native_strip_mnemonic (self->accept_label);
data->title = g_strdup (gtk_native_dialog_get_title (GTK_NATIVE_DIALOG (self)));
if (self->current_file)
{
GFile *parent = g_file_get_parent (self->current_file);
data->directory = parent ? g_file_get_path (parent) : NULL;
data->name = g_file_get_basename (self->current_file);
if (parent)
g_object_unref (parent);
}
else
{
if (self->current_folder)
data->directory = g_file_get_path (self->current_folder);
if (data->save)
data->name = g_strdup (self->current_name);
}
transient_for = gtk_native_dialog_get_transient_for (GTK_NATIVE_DIALOG (self));
if (transient_for && gtk_widget_get_realized (GTK_WIDGET (transient_for)))
data->parent = gdk_quartz_window_get_nswindow (gtk_widget_get_window (GTK_WIDGET (transient_for)));
self->mode_data = data;
/* after gtk_native_dialog_show() has returned, which the response
* must not come before */
data->idle = gdk_threads_add_idle (filechooser_quartz_run, data);
return TRUE;
}
void
_gtk_file_chooser_native_quartz_hide (GtkFileChooserNative *self)
{
FileChooserQuartzData *data = self->mode_data;
g_return_if_fail (data != NULL);
data->skip_response = TRUE;
if (data->idle)
{
/* not shown yet */
g_source_remove (data->idle);
self->mode_data = NULL;
filechooser_quartz_data_free (data);
}
else
[NSApp abortModal];
}
#define __GTK_FILE_CHOOSER_NATIVE_QUARTZ_C__
#include "gtkaliasdef.c"

File diff suppressed because it is too large Load diff

View file

@ -20,8 +20,10 @@
#include "config.h"
#include <string.h>
#include <gio/gio.h>
#include "gtkfilefilter.h"
#include "gtkfilefilterprivate.h"
#include "gtkintl.h"
#include "gtkprivate.h"
@ -433,5 +435,174 @@ gtk_file_filter_filter (GtkFileFilter *filter,
return FALSE;
}
/**
* gtk_file_filter_to_gvariant:
* @filter: a #GtkFileFilter
*
* Serializes a file filter to a GVariant of the type (sa(us)) that
* the file chooser of the desktop portal takes: the name, and the
* patterns (0) and MIME types (1) of the filter. Custom rules cannot
* be serialized and are left out.
*
* Returns: (transfer none): a new, floating, #GVariant
*
* Since: 2.24 (as in GTK+ 3.22)
*/
GVariant *
gtk_file_filter_to_gvariant (GtkFileFilter *filter)
{
GVariantBuilder builder;
GSList *l;
g_return_val_if_fail (GTK_IS_FILE_FILTER (filter), NULL);
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(us)"));
for (l = filter->rules; l; l = l->next)
{
FilterRule *rule = l->data;
switch (rule->type)
{
case FILTER_RULE_PATTERN:
g_variant_builder_add (&builder, "(us)", 0, rule->u.pattern);
break;
case FILTER_RULE_MIME_TYPE:
g_variant_builder_add (&builder, "(us)", 1, rule->u.mime_type);
break;
case FILTER_RULE_PIXBUF_FORMATS:
{
GSList *f;
for (f = rule->u.pixbuf_formats; f; f = f->next)
{
gchar **mime_types = gdk_pixbuf_format_get_mime_types (f->data);
int i;
for (i = 0; mime_types && mime_types[i]; i++)
g_variant_builder_add (&builder, "(us)", 1, mime_types[i]);
g_strfreev (mime_types);
}
}
break;
case FILTER_RULE_CUSTOM:
default:
break;
}
}
return g_variant_new ("(s@a(us))", filter->name ? filter->name : "",
g_variant_builder_end (&builder));
}
/**
* gtk_file_filter_new_from_gvariant:
* @variant: a #GVariant of the type (sa(us))
*
* Makes a file filter of a GVariant in the form that
* gtk_file_filter_to_gvariant() gives.
*
* Returns: a new #GtkFileFilter, as gtk_file_filter_new()
*
* Since: 2.24 (as in GTK+ 3.22)
*/
GtkFileFilter *
gtk_file_filter_new_from_gvariant (GVariant *variant)
{
GtkFileFilter *filter;
GVariantIter *iter;
const char *name;
guint32 type;
char *tmp;
g_return_val_if_fail (variant != NULL &&
g_variant_is_of_type (variant, G_VARIANT_TYPE ("(sa(us))")), NULL);
filter = gtk_file_filter_new ();
g_variant_get (variant, "(&sa(us))", &name, &iter);
gtk_file_filter_set_name (filter, name);
while (g_variant_iter_next (iter, "(u&s)", &type, &tmp))
{
switch (type)
{
case 0:
gtk_file_filter_add_pattern (filter, tmp);
break;
case 1:
gtk_file_filter_add_mime_type (filter, tmp);
break;
default:
break;
}
}
g_variant_iter_free (iter);
return filter;
}
/* The filter as patterns, for the file dialogs of Windows and Mac OS X;
* NULL if a rule cannot be one. MIME types become the extension the
* system knows for them, where it knows one. */
char **
_gtk_file_filter_get_as_patterns (GtkFileFilter *filter)
{
GPtrArray *array = g_ptr_array_new ();
GSList *l;
for (l = filter->rules; l; l = l->next)
{
FilterRule *rule = l->data;
switch (rule->type)
{
case FILTER_RULE_PATTERN:
g_ptr_array_add (array, g_strdup (rule->u.pattern));
break;
case FILTER_RULE_MIME_TYPE:
{
gchar *type = g_content_type_from_mime_type (rule->u.mime_type);
/* GIO on Windows has extensions for content types */
if (type == NULL || type[0] != '.')
{
g_free (type);
goto none;
}
g_ptr_array_add (array, g_strconcat ("*", type, NULL));
g_free (type);
}
break;
case FILTER_RULE_PIXBUF_FORMATS:
{
GSList *f;
for (f = rule->u.pixbuf_formats; f; f = f->next)
{
gchar **extensions = gdk_pixbuf_format_get_extensions (f->data);
int i;
for (i = 0; extensions && extensions[i]; i++)
g_ptr_array_add (array, g_strdup_printf ("*.%s", extensions[i]));
g_strfreev (extensions);
}
}
break;
case FILTER_RULE_CUSTOM:
default:
goto none;
}
}
g_ptr_array_add (array, NULL);
return (char **) g_ptr_array_free (array, FALSE);
none:
g_ptr_array_foreach (array, (GFunc) g_free, NULL);
g_ptr_array_free (array, TRUE);
return NULL;
}
#define __GTK_FILE_FILTER_C__
#include "gtkaliasdef.c"

View file

@ -75,6 +75,9 @@ void gtk_file_filter_add_custom (GtkFileFilter *filter,
GDestroyNotify notify);
GtkFileFilterFlags gtk_file_filter_get_needed (GtkFileFilter *filter);
GVariant *gtk_file_filter_to_gvariant (GtkFileFilter *filter);
GtkFileFilter *gtk_file_filter_new_from_gvariant (GVariant *variant);
gboolean gtk_file_filter_filter (GtkFileFilter *filter,
const GtkFileFilterInfo *filter_info);

View file

@ -0,0 +1,31 @@
/* GTK - The GIMP Toolkit
* gtkfilefilterprivate.h
*
* 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GTK_FILE_FILTER_PRIVATE_H__
#define __GTK_FILE_FILTER_PRIVATE_H__
#include <gtk/gtkfilefilter.h>
G_BEGIN_DECLS
char **_gtk_file_filter_get_as_patterns (GtkFileFilter *filter);
G_END_DECLS
#endif /* __GTK_FILE_FILTER_PRIVATE_H__ */

639
gtk/gtknativedialog.c Normal file
View file

@ -0,0 +1,639 @@
/* GTK - The GIMP Toolkit
* gtknativedialog.c: Native 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* The base class of dialogs that are not GTK+ windows - the file
* dialogs of Windows and Mac OS X, and of the desktop portal - with
* the API of GTK+ 3.20, so that programs written for it, or looking
* for it at run time as Pale Moon and Firefox do, find it here. */
#include "config.h"
#include "gtknativedialog.h"
#include "gtknativedialogprivate.h"
#include "gtkdialog.h"
#include "gtkintl.h"
#include "gtkprivate.h"
#include "gtkmarshalers.h"
#include "gtkalias.h"
typedef struct _GtkNativeDialogPrivate GtkNativeDialogPrivate;
struct _GtkNativeDialogPrivate
{
GtkWindow *transient_for;
char *title;
guint visible : 1;
guint modal : 1;
/* while in gtk_native_dialog_run() */
gint run_response_id;
GMainLoop *run_loop;
};
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_free (priv->title);
if (priv->transient_for)
g_object_unref (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
*
* Since: 2.24
*/
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.
*
* Since: 2.24
*/
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);
/**
* GtkNativeDialog:visible:
*
* Whether the window is currently visible.
*
* Since: 2.24
*/
native_props[PROP_VISIBLE] =
g_param_spec_boolean ("visible",
P_("Visible"),
P_("Whether the dialog is currently visible"),
FALSE,
GTK_PARAM_READWRITE);
/**
* GtkNativeDialog:transient-for:
*
* The transient parent of the dialog, or %NULL for none.
*
* Since: 2.24
*/
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_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.
*
* Since: 2.24
*/
native_signals[RESPONSE] =
g_signal_new (I_("response"),
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkNativeDialogClass, response),
NULL, NULL,
_gtk_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.
*
* Since: 2.24
**/
void
gtk_native_dialog_show (GtkNativeDialog *self)
{
GtkNativeDialogPrivate *priv;
GtkNativeDialogClass *klass;
g_return_if_fail (GTK_IS_NATIVE_DIALOG (self));
priv = gtk_native_dialog_get_instance_private (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.
*
* Since: 2.24
**/
void
gtk_native_dialog_hide (GtkNativeDialog *self)
{
GtkNativeDialogPrivate *priv;
GtkNativeDialogClass *klass;
g_return_if_fail (GTK_IS_NATIVE_DIALOG (self));
priv = gtk_native_dialog_get_instance_private (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.
*
* Since: 2.24
**/
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
*
* Since: 2.24
**/
gboolean
gtk_native_dialog_get_visible (GtkNativeDialog *self)
{
GtkNativeDialogPrivate *priv;
g_return_val_if_fail (GTK_IS_NATIVE_DIALOG (self), FALSE);
priv = gtk_native_dialog_get_instance_private (self);
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.
*
* Since: 2.24
**/
void
gtk_native_dialog_set_modal (GtkNativeDialog *self,
gboolean modal)
{
GtkNativeDialogPrivate *priv;
g_return_if_fail (GTK_IS_NATIVE_DIALOG (self));
priv = gtk_native_dialog_get_instance_private (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
*
* Since: 2.24
**/
gboolean
gtk_native_dialog_get_modal (GtkNativeDialog *self)
{
GtkNativeDialogPrivate *priv;
g_return_val_if_fail (GTK_IS_NATIVE_DIALOG (self), FALSE);
priv = gtk_native_dialog_get_instance_private (self);
return priv->modal;
}
/**
* gtk_native_dialog_set_title:
* @self: a #GtkNativeDialog
* @title: title of the dialog
*
* Sets the title of the #GtkNativeDialog.
*
* Since: 2.24
**/
void
gtk_native_dialog_set_title (GtkNativeDialog *self,
const char *title)
{
GtkNativeDialogPrivate *priv;
g_return_if_fail (GTK_IS_NATIVE_DIALOG (self));
priv = gtk_native_dialog_get_instance_private (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.
*
* Since: 2.24
**/
const char *
gtk_native_dialog_get_title (GtkNativeDialog *self)
{
GtkNativeDialogPrivate *priv;
g_return_val_if_fail (GTK_IS_NATIVE_DIALOG (self), NULL);
priv = gtk_native_dialog_get_instance_private (self);
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.
*
* Since: 2.24
*/
void
gtk_native_dialog_set_transient_for (GtkNativeDialog *self,
GtkWindow *parent)
{
GtkNativeDialogPrivate *priv;
g_return_if_fail (GTK_IS_NATIVE_DIALOG (self));
priv = gtk_native_dialog_get_instance_private (self);
if (priv->transient_for == parent)
return;
if (parent)
g_object_ref (parent);
if (priv->transient_for)
g_object_unref (priv->transient_for);
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.
*
* Since: 2.24
**/
GtkWindow *
gtk_native_dialog_get_transient_for (GtkNativeDialog *self)
{
GtkNativeDialogPrivate *priv;
g_return_val_if_fail (GTK_IS_NATIVE_DIALOG (self), NULL);
priv = gtk_native_dialog_get_instance_private (self);
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.
*
* 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
*
* Since: 2.24
**/
gint
gtk_native_dialog_run (GtkNativeDialog *self)
{
GtkNativeDialogPrivate *priv;
gboolean was_modal;
gulong response_handler;
g_return_val_if_fail (GTK_IS_NATIVE_DIALOG (self), -1);
priv = gtk_native_dialog_get_instance_private (self);
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);
if (priv->visible)
{
GDK_THREADS_LEAVE ();
g_main_loop_run (priv->run_loop);
GDK_THREADS_ENTER ();
}
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;
}
#define __GTK_NATIVE_DIALOG_C__
#include "gtkaliasdef.c"

86
gtk/gtknativedialog.h Normal file
View file

@ -0,0 +1,86 @@
/* GTK - The GIMP Toolkit
* gtknativedialog.h: Native 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GTK_NATIVE_DIALOG_H__
#define __GTK_NATIVE_DIALOG_H__
#if defined(GTK_DISABLE_SINGLE_INCLUDES) && !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gtk/gtk.h> can be included directly."
#endif
#include <gtk/gtkwindow.h>
G_BEGIN_DECLS
#define GTK_TYPE_NATIVE_DIALOG (gtk_native_dialog_get_type ())
#define GTK_NATIVE_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_NATIVE_DIALOG, GtkNativeDialog))
#define GTK_NATIVE_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_NATIVE_DIALOG, GtkNativeDialogClass))
#define GTK_IS_NATIVE_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_NATIVE_DIALOG))
#define GTK_IS_NATIVE_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_NATIVE_DIALOG))
#define GTK_NATIVE_DIALOG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_NATIVE_DIALOG, GtkNativeDialogClass))
typedef struct _GtkNativeDialog GtkNativeDialog;
typedef struct _GtkNativeDialogClass GtkNativeDialogClass;
/* A dialog of the system, or of the desktop portal, with the API of
* GTK+ 3.20; see GtkFileChooserNative */
struct _GtkNativeDialog
{
GObject parent_instance;
};
struct _GtkNativeDialogClass
{
GObjectClass parent_class;
void (* response) (GtkNativeDialog *self, gint response_id);
/* <private> */
void (* show) (GtkNativeDialog *self);
void (* hide) (GtkNativeDialog *self);
/* Padding for future expansion */
void (*_gtk_reserved1) (void);
void (*_gtk_reserved2) (void);
void (*_gtk_reserved3) (void);
void (*_gtk_reserved4) (void);
};
GType gtk_native_dialog_get_type (void);
void gtk_native_dialog_show (GtkNativeDialog *self);
void gtk_native_dialog_hide (GtkNativeDialog *self);
void gtk_native_dialog_destroy (GtkNativeDialog *self);
gboolean gtk_native_dialog_get_visible (GtkNativeDialog *self);
void gtk_native_dialog_set_modal (GtkNativeDialog *self,
gboolean modal);
gboolean gtk_native_dialog_get_modal (GtkNativeDialog *self);
void gtk_native_dialog_set_title (GtkNativeDialog *self,
const char *title);
const char *gtk_native_dialog_get_title (GtkNativeDialog *self);
void gtk_native_dialog_set_transient_for (GtkNativeDialog *self,
GtkWindow *parent);
GtkWindow * gtk_native_dialog_get_transient_for (GtkNativeDialog *self);
gint gtk_native_dialog_run (GtkNativeDialog *self);
G_END_DECLS
#endif /* __GTK_NATIVE_DIALOG_H__ */

View file

@ -0,0 +1,33 @@
/* GTK - The GIMP Toolkit
* gtknativedialogprivate.h: Native 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GTK_NATIVE_DIALOG_PRIVATE_H__
#define __GTK_NATIVE_DIALOG_PRIVATE_H__
#include <gtk/gtknativedialog.h>
G_BEGIN_DECLS
void _gtk_native_dialog_emit_response (GtkNativeDialog *self,
int response_id);
G_END_DECLS
#endif /* __GTK_NATIVE_DIALOG_PRIVATE_H__ */