diff --git a/gtk/Makefile.am b/gtk/Makefile.am index b1fc2a8dd..d8ad19e9a 100644 --- a/gtk/Makefile.am +++ b/gtk/Makefile.am @@ -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 \ diff --git a/gtk/gtk.h b/gtk/gtk.h index 94e0b61f9..8ba2502f6 100644 --- a/gtk/gtk.h +++ b/gtk/gtk.h @@ -88,6 +88,7 @@ #include #include #include +#include #include #include #include @@ -128,6 +129,7 @@ #include #include #include +#include #include #include #include diff --git a/gtk/gtk.symbols b/gtk/gtk.symbols index 06f87a7c0..134ddb314 100644 --- a/gtk/gtk.symbols +++ b/gtk/gtk.symbols @@ -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 diff --git a/gtk/gtkfilechooser.c b/gtk/gtkfilechooser.c index e91887be5..a8e0fbf5c 100644 --- a/gtk/gtkfilechooser.c +++ b/gtk/gtkfilechooser.c @@ -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; diff --git a/gtk/gtkfilechoosernative.c b/gtk/gtkfilechoosernative.c new file mode 100644 index 000000000..28e1f5169 --- /dev/null +++ b/gtk/gtkfilechoosernative.c @@ -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 + +#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" diff --git a/gtk/gtkfilechoosernative.h b/gtk/gtkfilechoosernative.h new file mode 100644 index 000000000..fb8f852ca --- /dev/null +++ b/gtk/gtkfilechoosernative.h @@ -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 can be included directly." +#endif + +#include +#include + +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__ */ diff --git a/gtk/gtkfilechoosernativeportal.c b/gtk/gtkfilechoosernativeportal.c new file mode 100644 index 000000000..bf13d85a8 --- /dev/null +++ b/gtk/gtkfilechoosernativeportal.c @@ -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 + +#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" diff --git a/gtk/gtkfilechoosernativeprivate.h b/gtk/gtkfilechoosernativeprivate.h new file mode 100644 index 000000000..ca5e083a8 --- /dev/null +++ b/gtk/gtkfilechoosernativeprivate.h @@ -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 + +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__ */ diff --git a/gtk/gtkfilechoosernativequartz.c b/gtk/gtkfilechoosernativequartz.c new file mode 100644 index 000000000..7c9262cd3 --- /dev/null +++ b/gtk/gtkfilechoosernativequartz.c @@ -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 + +#import + +#include "gtkfilechoosernativeprivate.h" +#include "gtknativedialogprivate.h" +#include "gtkfilechooser.h" +#include "gtkfilefilterprivate.h" +#include "gtkprivate.h" +#include + +#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" diff --git a/gtk/gtkfilechoosernativewin32.c b/gtk/gtkfilechoosernativewin32.c new file mode 100644 index 000000000..ed305c2cd --- /dev/null +++ b/gtk/gtkfilechoosernativewin32.c @@ -0,0 +1,1055 @@ +/* GTK - The GIMP Toolkit + * gtkfilechoosernativewin32.c: Win32 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. + */ + +/* The file dialog of Windows for GtkFileChooserNative: IFileDialog + * from Vista on, as GTK+ 3 has it, and the common dialogs of Windows + * 2000, XP and ReactOS (GetOpenFileName, GetSaveFileName and + * SHBrowseForFolder) before. The dialog runs in a thread of its own, + * so that the windows of the program go on drawing; the answer comes + * back to the main loop. */ + +#include "config.h" + +/* the declarations of Vista, which is looked for at run time */ +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x0600 +#undef WINVER +#define WINVER _WIN32_WINNT +#define COBJMACROS + +#include + +#include "gtkfilechoosernativeprivate.h" +#include "gtknativedialogprivate.h" +#include "gtkfilechooser.h" +#include "gtkfilechooserprivate.h" +#include "gtkfilefilterprivate.h" +#include "gtkinvisible.h" +#include "gtkmain.h" +#include "gtkdialog.h" +#include "gtkprivate.h" + +#include +#include +#include +#include +#include + +#include "gtkalias.h" + +typedef struct { + GtkFileChooserNative *self; + + GtkWidget *grab_widget; + + IFileDialogEvents *events; /* Vista and newer */ + HWND parent; + gboolean skip_response; + gboolean save; + gboolean folder; + gboolean modal; + gboolean overwrite_confirmation; + gboolean select_multiple; + gboolean show_hidden; + + char *accept_label; + char *cancel_label; + char *title; + + GSList *shortcut_uris; + + GFile *current_folder; + GFile *current_file; + char *current_name; + + COMDLG_FILTERSPEC *filters; + int n_filters; + int filter_index; /* 1-based, of the filter shown first */ + int chosen_filter; /* 1-based, 0 for unknown */ + + /* the dialog of the older Windows, for hiding it */ + HWND old_dialog; + gboolean old_close; + + GSList *files; + int response; +} FilechooserWin32ThreadData; + +/* protects the window handles of the dialogs and their "close" flags */ +G_LOCK_DEFINE_STATIC (FileDialogEvents); + +/* {3CAFD12E-82AE-4184-8309-848C0104B4DC} */ +static const GUID myIID_IFileDialogEvents = +{ 0x3cafd12e, 0x82ae, 0x4184, { 0x83, 0x9, 0x84, 0x8c, 0x1, 0x4, 0xb4, 0xdc } }; + +typedef struct { + IFileDialogEvents iFileDialogEvents; + int ref_count; + gboolean enable_owner; + gboolean got_hwnd; + HWND dialog_hwnd; + gboolean do_close; /* hide came before dialog_hwnd */ + FilechooserWin32ThreadData *data; +} FileDialogEvents; + +static ULONG STDMETHODCALLTYPE +ifiledialogevents_AddRef (IFileDialogEvents *self) +{ + FileDialogEvents *events = (FileDialogEvents *) self; + ULONG ref_count; + + G_LOCK (FileDialogEvents); + ref_count = ++events->ref_count; + G_UNLOCK (FileDialogEvents); + + return ref_count; +} + +static ULONG STDMETHODCALLTYPE +ifiledialogevents_Release (IFileDialogEvents *self) +{ + FileDialogEvents *events = (FileDialogEvents *) self; + int ref_count; + + G_LOCK (FileDialogEvents); + ref_count = --events->ref_count; + G_UNLOCK (FileDialogEvents); + + if (ref_count == 0) + g_free (self); + + return ref_count; +} + +static HRESULT STDMETHODCALLTYPE +ifiledialogevents_QueryInterface (IFileDialogEvents *self, + REFIID riid, + LPVOID *ppvObject) +{ + if (IsEqualIID (riid, &IID_IUnknown) || + IsEqualIID (riid, &myIID_IFileDialogEvents)) + { + *ppvObject = self; + IUnknown_AddRef ((IUnknown *) self); + return NOERROR; + } + + *ppvObject = NULL; + return E_NOINTERFACE; +} + +static HRESULT STDMETHODCALLTYPE +ifiledialogevents_OnFileOk (IFileDialogEvents *self, + IFileDialog *pfd) +{ + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE +ifiledialogevents_OnFolderChanging (IFileDialogEvents *self, + IFileDialog *pfd, + IShellItem *psiFolder) +{ + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE +ifiledialogevents_OnFolderChange (IFileDialogEvents *self, + IFileDialog *pfd) +{ + FileDialogEvents *events = (FileDialogEvents *) self; + IOleWindow *olew = NULL; + HWND dialog_hwnd; + HRESULT hr; + + if (!events->got_hwnd) + { + events->got_hwnd = TRUE; + + hr = IFileDialog_QueryInterface (pfd, &IID_IOleWindow, (LPVOID *) &olew); + if (SUCCEEDED (hr)) + { + hr = IOleWindow_GetWindow (olew, &dialog_hwnd); + if (SUCCEEDED (hr)) + { + G_LOCK (FileDialogEvents); + events->dialog_hwnd = dialog_hwnd; + if (events->do_close) + SendMessage (events->dialog_hwnd, WM_CLOSE, 0, 0); + G_UNLOCK (FileDialogEvents); + } + IOleWindow_Release (olew); + } + + /* not modal: the parent stays usable */ + if (events->enable_owner && events->dialog_hwnd) + { + HWND owner = GetWindow (events->dialog_hwnd, GW_OWNER); + + if (owner) + EnableWindow (owner, TRUE); + } + } + + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE +ifiledialogevents_OnSelectionChange (IFileDialogEvents *self, + IFileDialog *pfd) +{ + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE +ifiledialogevents_OnShareViolation (IFileDialogEvents *self, + IFileDialog *pfd, + IShellItem *psi, + FDE_SHAREVIOLATION_RESPONSE *pResponse) +{ + return E_NOTIMPL; +} + +static HRESULT STDMETHODCALLTYPE +ifiledialogevents_OnTypeChange (IFileDialogEvents *self, + IFileDialog *pfd) +{ + FileDialogEvents *events = (FileDialogEvents *) self; + UINT file_type; + + /* read in the main thread, with the files */ + if (SUCCEEDED (IFileDialog_GetFileTypeIndex (pfd, &file_type))) + events->data->chosen_filter = file_type; + + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE +ifiledialogevents_OnOverwrite (IFileDialogEvents *self, + IFileDialog *pfd, + IShellItem *psi, + FDE_OVERWRITE_RESPONSE *pResponse) +{ + return E_NOTIMPL; +} + +static IFileDialogEventsVtbl ifde_vtbl = { + ifiledialogevents_QueryInterface, + ifiledialogevents_AddRef, + ifiledialogevents_Release, + ifiledialogevents_OnFileOk, + ifiledialogevents_OnFolderChanging, + ifiledialogevents_OnFolderChange, + ifiledialogevents_OnSelectionChange, + ifiledialogevents_OnShareViolation, + ifiledialogevents_OnTypeChange, + ifiledialogevents_OnOverwrite +}; + +static IFileDialogEvents * +file_dialog_events_new (gboolean enable_owner, + FilechooserWin32ThreadData *data) +{ + FileDialogEvents *events; + + events = g_new0 (FileDialogEvents, 1); + events->iFileDialogEvents.lpVtbl = &ifde_vtbl; + events->ref_count = 1; + events->enable_owner = enable_owner; + events->data = data; + + return &events->iFileDialogEvents; +} + +static void +filechooser_win32_send_close (FilechooserWin32ThreadData *data) +{ + G_LOCK (FileDialogEvents); + + if (data->events) + { + FileDialogEvents *events = (FileDialogEvents *) data->events; + + if (events->dialog_hwnd) + PostMessage (events->dialog_hwnd, WM_CLOSE, 0, 0); + else + events->do_close = TRUE; + } + if (data->old_dialog) + PostMessage (data->old_dialog, WM_CLOSE, 0, 0); + else + data->old_close = TRUE; + + G_UNLOCK (FileDialogEvents); +} + +static void +filechooser_win32_thread_data_free (FilechooserWin32ThreadData *data) +{ + int i; + + if (data->filters) + { + for (i = 0; data->filters[i].pszName != NULL; i++) + { + g_free ((char *) data->filters[i].pszName); + g_free ((char *) data->filters[i].pszSpec); + } + g_free (data->filters); + } + + if (data->events) + IFileDialogEvents_Release (data->events); + + if (data->grab_widget) + { + gtk_grab_remove (data->grab_widget); + gtk_widget_destroy (data->grab_widget); + } + + if (data->current_folder) + g_object_unref (data->current_folder); + if (data->current_file) + g_object_unref (data->current_file); + g_free (data->current_name); + + g_slist_foreach (data->shortcut_uris, (GFunc) g_free, NULL); + g_slist_free (data->shortcut_uris); + g_slist_foreach (data->files, (GFunc) g_object_unref, NULL); + g_slist_free (data->files); + if (data->self) + g_object_unref (data->self); + g_free (data->accept_label); + g_free (data->cancel_label); + g_free (data->title); + g_free (data); +} + +static gboolean +filechooser_win32_thread_done (gpointer _data) +{ + FilechooserWin32ThreadData *data = _data; + GtkFileChooserNative *self = data->self; + + self->mode_data = NULL; + + if (!data->skip_response) + { + if (data->chosen_filter > 0) + { + GSList *filters = gtk_file_chooser_list_filters (GTK_FILE_CHOOSER (self)); + GtkFileFilter *filter = g_slist_nth_data (filters, data->chosen_filter - 1); + + if (filter) + gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (self), filter); + g_slist_free (filters); + } + + g_slist_foreach (self->custom_files, (GFunc) g_object_unref, NULL); + g_slist_free (self->custom_files); + self->custom_files = g_slist_reverse (data->files); + data->files = NULL; + + _gtk_native_dialog_emit_response (GTK_NATIVE_DIALOG (data->self), + data->response); + } + + filechooser_win32_thread_data_free (data); + + return FALSE; +} + +static void +data_add_path_w (FilechooserWin32ThreadData *data, + const wchar_t *pathw) +{ + char *path = g_utf16_to_utf8 (pathw, -1, NULL, NULL, NULL); + + if (path != NULL) + { + data->files = g_slist_prepend (data->files, g_file_new_for_path (path)); + data->response = GTK_RESPONSE_ACCEPT; + g_free (path); + } +} + +static void +data_add_shell_item (FilechooserWin32ThreadData *data, + IShellItem *item) +{ + PWSTR pathw = NULL; + + if (SUCCEEDED (IShellItem_GetDisplayName (item, SIGDN_FILESYSPATH, &pathw))) + { + data_add_path_w (data, pathw); + CoTaskMemFree (pathw); + } +} + +/* SHCreateItemFromParsingName is from Vista on: looked up, so that + * the DLL loads on XP */ +typedef HRESULT (WINAPI *CreateItemFunc) (PCWSTR, IBindCtx *, REFIID, void **); + +static IShellItem * +get_shell_item_for_path (const char *path) +{ + static CreateItemFunc create_item; + static gboolean looked_up; + IShellItem *item = NULL; + gunichar2 *path_w; + + if (!looked_up) + { + create_item = (CreateItemFunc) GetProcAddress (GetModuleHandleW (L"shell32.dll"), + "SHCreateItemFromParsingName"); + looked_up = TRUE; + } + if (create_item == NULL || path == NULL) + return NULL; + + path_w = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL); + if (path_w == NULL || FAILED (create_item (path_w, NULL, &IID_IShellItem, (void **) &item))) + item = NULL; + g_free (path_w); + + return item; +} + +static IShellItem * +get_shell_item_for_file (GFile *file) +{ + char *path = g_file_get_path (file); + IShellItem *item = get_shell_item_for_path (path); + + g_free (path); + return item; +} + +/* The dialog of Vista and newer; FALSE if there is none */ +static gboolean +run_file_dialog (FilechooserWin32ThreadData *data) +{ + HRESULT hr; + IFileDialog *pfd = NULL; + IFileDialog2 *pfd2 = NULL; + DWORD flags; + DWORD cookie; + GSList *l; + + /* the dialogs of XP also where there is the newer one, for trying them */ + if (g_getenv ("GTK_WIN32_OLD_FILE_DIALOGS")) + return FALSE; + + if (data->save && !data->folder) + hr = CoCreateInstance (&CLSID_FileSaveDialog, + NULL, CLSCTX_INPROC_SERVER, + &IID_IFileSaveDialog, (LPVOID *) &pfd); + else + hr = CoCreateInstance (&CLSID_FileOpenDialog, + NULL, CLSCTX_INPROC_SERVER, + &IID_IFileOpenDialog, (LPVOID *) &pfd); + if (FAILED (hr)) + return FALSE; + + if (FAILED (IFileDialog_GetOptions (pfd, &flags))) + flags = 0; + + flags |= FOS_FORCEFILESYSTEM; + if (data->folder) + flags |= FOS_PICKFOLDERS; + if (data->folder && data->save) + flags &= ~(FOS_FILEMUSTEXIST); + if (data->select_multiple) + flags |= FOS_ALLOWMULTISELECT; + if (data->show_hidden) + flags |= FOS_FORCESHOWHIDDEN; + if (data->overwrite_confirmation) + flags |= FOS_OVERWRITEPROMPT; + else + flags &= ~(FOS_OVERWRITEPROMPT); + + IFileDialog_SetOptions (pfd, flags); + + if (data->title) + { + gunichar2 *label = g_utf8_to_utf16 (data->title, -1, NULL, NULL, NULL); + IFileDialog_SetTitle (pfd, label); + g_free (label); + } + + if (data->accept_label) + { + gunichar2 *label = g_utf8_to_utf16 (data->accept_label, -1, NULL, NULL, NULL); + IFileDialog_SetOkButtonLabel (pfd, label); + g_free (label); + } + + if (data->cancel_label) + { + gunichar2 *label = g_utf8_to_utf16 (data->cancel_label, -1, NULL, NULL, NULL); + + if (SUCCEEDED (IFileDialog_QueryInterface (pfd, &IID_IFileDialog2, (LPVOID *) &pfd2))) + { + IFileDialog2_SetCancelButtonLabel (pfd2, label); + IFileDialog2_Release (pfd2); + } + g_free (label); + } + + for (l = data->shortcut_uris; l != NULL; l = l->next) + { + char *path = g_filename_from_uri (l->data, NULL, NULL); + IShellItem *item = get_shell_item_for_path (path); + + g_free (path); + if (item) + { + IFileDialog_AddPlace (pfd, item, FDAP_BOTTOM); + IShellItem_Release (item); + } + } + + if (data->current_file) + { + IFileSaveDialog *pfsd; + + if (SUCCEEDED (IFileDialog_QueryInterface (pfd, &IID_IFileSaveDialog, (LPVOID *) &pfsd))) + { + IShellItem *item = get_shell_item_for_file (data->current_file); + + if (item) + { + IFileSaveDialog_SetSaveAsItem (pfsd, item); + IShellItem_Release (item); + } + IFileSaveDialog_Release (pfsd); + } + } + + if (data->current_folder) + { + IShellItem *item = get_shell_item_for_file (data->current_folder); + + if (item) + { + IFileDialog_SetFolder (pfd, item); + IShellItem_Release (item); + } + } + + if (data->current_name) + { + gunichar2 *name = g_utf8_to_utf16 (data->current_name, -1, NULL, NULL, NULL); + IFileDialog_SetFileName (pfd, name); + g_free (name); + } + + if (data->n_filters > 0) + { + IFileDialog_SetFileTypes (pfd, data->n_filters, data->filters); + IFileDialog_SetDefaultExtension (pfd, L""); + IFileDialog_SetFileTypeIndex (pfd, data->filter_index); + } + + data->response = GTK_RESPONSE_CANCEL; + + data->events = file_dialog_events_new (!data->modal, data); + if (FAILED (IFileDialog_Advise (pfd, data->events, &cookie))) + cookie = 0; + + hr = IFileDialog_Show (pfd, data->parent); + if (SUCCEEDED (hr)) + { + IFileOpenDialog *pfod = NULL; + + if (SUCCEEDED (IFileDialog_QueryInterface (pfd, &IID_IFileOpenDialog, (LPVOID *) &pfod))) + { + IShellItemArray *res; + DWORD i, count; + + if (SUCCEEDED (IFileOpenDialog_GetResults (pfod, &res))) + { + if (SUCCEEDED (IShellItemArray_GetCount (res, &count))) + for (i = 0; i < count; i++) + { + IShellItem *item; + + if (SUCCEEDED (IShellItemArray_GetItemAt (res, i, &item))) + { + data_add_shell_item (data, item); + IShellItem_Release (item); + } + } + IShellItemArray_Release (res); + } + IFileOpenDialog_Release (pfod); + } + else + { + IShellItem *item; + + if (SUCCEEDED (IFileDialog_GetResult (pfd, &item))) + { + data_add_shell_item (data, item); + IShellItem_Release (item); + } + } + } + + if (cookie) + IFileDialog_Unadvise (pfd, cookie); + + IFileDialog_Release (pfd); + + return TRUE; +} + +/* The dialogs of Windows 2000, XP and ReactOS */ + +static UINT_PTR CALLBACK +old_dialog_hook (HWND hdlg, + UINT msg, + WPARAM wparam, + LPARAM lparam) +{ + if (msg == WM_INITDIALOG) + { + OPENFILENAMEW *ofn = (OPENFILENAMEW *) lparam; + FilechooserWin32ThreadData *data = (FilechooserWin32ThreadData *) ofn->lCustData; + HWND dialog = GetParent (hdlg); /* the hook has a child of the dialog */ + + G_LOCK (FileDialogEvents); + data->old_dialog = dialog; + if (data->old_close) + PostMessage (dialog, WM_CLOSE, 0, 0); + G_UNLOCK (FileDialogEvents); + + if (data->accept_label) + { + gunichar2 *label = g_utf8_to_utf16 (data->accept_label, -1, NULL, NULL, NULL); + SendMessageW (dialog, CDM_SETCONTROLTEXT, IDOK, (LPARAM) label); + g_free (label); + } + if (data->cancel_label) + { + gunichar2 *label = g_utf8_to_utf16 (data->cancel_label, -1, NULL, NULL, NULL); + SendMessageW (dialog, CDM_SETCONTROLTEXT, IDCANCEL, (LPARAM) label); + g_free (label); + } + } + + return 0; +} + +static int CALLBACK +browse_callback (HWND hwnd, + UINT msg, + LPARAM lparam, + LPARAM lpdata) +{ + FilechooserWin32ThreadData *data = (FilechooserWin32ThreadData *) lpdata; + + if (msg == BFFM_INITIALIZED) + { + G_LOCK (FileDialogEvents); + data->old_dialog = hwnd; + if (data->old_close) + PostMessage (hwnd, WM_CLOSE, 0, 0); + G_UNLOCK (FileDialogEvents); + + /* the title goes above the tree; the window gets it too */ + if (data->title) + { + gunichar2 *title = g_utf8_to_utf16 (data->title, -1, NULL, NULL, NULL); + SetWindowTextW (hwnd, title); + g_free (title); + } + + if (data->current_folder) + { + char *path = g_file_get_path (data->current_folder); + gunichar2 *path_w = path ? g_utf8_to_utf16 (path, -1, NULL, NULL, NULL) : NULL; + + if (path_w) + SendMessageW (hwnd, BFFM_SETSELECTIONW, TRUE, (LPARAM) path_w); + g_free (path_w); + g_free (path); + } + } + + return 0; +} + +static void +run_old_folder_dialog (FilechooserWin32ThreadData *data) +{ + BROWSEINFOW bi; + LPITEMIDLIST pidl; + gunichar2 *title = data->title ? g_utf8_to_utf16 (data->title, -1, NULL, NULL, NULL) : NULL; + wchar_t path[MAX_PATH]; + + memset (&bi, 0, sizeof bi); + bi.hwndOwner = data->parent; + bi.lpszTitle = title; + bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE; + bi.lpfn = browse_callback; + bi.lParam = (LPARAM) data; + + data->response = GTK_RESPONSE_CANCEL; + pidl = SHBrowseForFolderW (&bi); + if (pidl) + { + if (SHGetPathFromIDListW (pidl, path)) + data_add_path_w (data, path); + CoTaskMemFree (pidl); + } + g_free (title); +} + +static void +run_old_file_dialog (FilechooserWin32ThreadData *data) +{ + OPENFILENAMEW ofn; + GString *filter = NULL; + gunichar2 *filter_w = NULL, *title = NULL, *initial_dir = NULL; + const int buffer_len = 32768; + wchar_t *buffer = g_new0 (wchar_t, buffer_len); + BOOL ok; + int i; + + memset (&ofn, 0, sizeof ofn); + ofn.lStructSize = sizeof ofn; + ofn.hwndOwner = data->parent; + + /* "name\0pattern;pattern\0...\0\0", as UTF-16 */ + if (data->n_filters > 0) + { + glong len = 0; + gunichar2 *p; + + for (i = 0; i < data->n_filters; i++) + len += wcslen (data->filters[i].pszName) + 1 + wcslen (data->filters[i].pszSpec) + 1; + filter_w = p = g_new0 (gunichar2, len + 1); + for (i = 0; i < data->n_filters; i++) + { + wcscpy ((wchar_t *) p, data->filters[i].pszName); + p += wcslen (data->filters[i].pszName) + 1; + wcscpy ((wchar_t *) p, data->filters[i].pszSpec); + p += wcslen (data->filters[i].pszSpec) + 1; + } + ofn.lpstrFilter = filter_w; + ofn.nFilterIndex = data->filter_index; + ofn.lpstrDefExt = L""; /* the extension of the filter where none is typed */ + } + (void) filter; + + if (data->current_file) + { + char *path = g_file_get_path (data->current_file); + gunichar2 *path_w = path ? g_utf8_to_utf16 (path, -1, NULL, NULL, NULL) : NULL; + + if (path_w && wcslen (path_w) < (size_t) buffer_len) + wcscpy (buffer, path_w); + g_free (path_w); + g_free (path); + } + else + { + if (data->current_folder) + { + char *path = g_file_get_path (data->current_folder); + + if (path) + initial_dir = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL); + g_free (path); + } + if (data->current_name) + { + gunichar2 *name = g_utf8_to_utf16 (data->current_name, -1, NULL, NULL, NULL); + + if (name && wcslen (name) < (size_t) buffer_len) + wcscpy (buffer, name); + g_free (name); + } + } + + ofn.lpstrFile = buffer; + ofn.nMaxFile = buffer_len; + ofn.lpstrInitialDir = initial_dir; + if (data->title) + title = g_utf8_to_utf16 (data->title, -1, NULL, NULL, NULL); + ofn.lpstrTitle = title; + ofn.Flags = OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLESIZING | OFN_NOCHANGEDIR | + OFN_HIDEREADONLY | OFN_PATHMUSTEXIST; + if (!data->save) + ofn.Flags |= OFN_FILEMUSTEXIST; + if (data->select_multiple) + ofn.Flags |= OFN_ALLOWMULTISELECT; + if (data->save && data->overwrite_confirmation) + ofn.Flags |= OFN_OVERWRITEPROMPT; + ofn.lpfnHook = old_dialog_hook; + ofn.lCustData = (LPARAM) data; + + data->response = GTK_RESPONSE_CANCEL; + ok = data->save ? GetSaveFileNameW (&ofn) : GetOpenFileNameW (&ofn); + if (ok) + { + /* one path, or the folder and the names, each ended by a 0 */ + wchar_t *first = buffer, *name = buffer + wcslen (buffer) + 1; + + if (!data->select_multiple || *name == 0) + data_add_path_w (data, first); + else + for (; *name; name += wcslen (name) + 1) + { + wchar_t *full = g_new (wchar_t, wcslen (first) + 1 + wcslen (name) + 1); + + wcscpy (full, first); + wcscat (full, L"\\"); + wcscat (full, name); + data_add_path_w (data, full); + g_free (full); + } + data->chosen_filter = ofn.nFilterIndex; + } + + g_free (buffer); + g_free (filter_w); + g_free (initial_dir); + g_free (title); +} + +static gpointer +filechooser_win32_thread (gpointer _data) +{ + FilechooserWin32ThreadData *data = _data; + + /* OLE, not just COM: the folder dialog of XP wants it for its + * newer style */ + OleInitialize (NULL); + + if (!run_file_dialog (data)) + { + if (data->folder) + run_old_folder_dialog (data); + else + run_old_file_dialog (data); + } + + OleUninitialize (); + + g_main_context_invoke (NULL, filechooser_win32_thread_done, data); + + return NULL; +} + +static gboolean +file_filter_to_win32 (GtkFileFilter *filter, + COMDLG_FILTERSPEC *spec) +{ + const char *name; + char **patterns; + char *pattern_list; + + patterns = _gtk_file_filter_get_as_patterns (filter); + if (patterns == NULL) + return FALSE; + + pattern_list = g_strjoinv (";", patterns); + g_strfreev (patterns); + + name = gtk_file_filter_get_name (filter); + if (name == NULL) + name = pattern_list; + spec->pszName = g_utf8_to_utf16 (name, -1, NULL, NULL, NULL); + spec->pszSpec = g_utf8_to_utf16 (pattern_list, -1, NULL, NULL, NULL); + + g_free (pattern_list); + + return TRUE; +} + +/* "_Save" -> "&Save", "__" -> "_", "&" -> "&&" */ +static char * +translate_mnemonics (const char *src) +{ + GString *s; + const char *p; + + if (src == NULL) + return NULL; + + s = g_string_sized_new (strlen (src)); + + for (p = src; *p; p++) + { + switch (*p) + { + case '_': + if (p[1] == '_') + { + g_string_append_c (s, '_'); + p++; + } + else + g_string_append_c (s, '&'); + break; + case '&': + g_string_append (s, "&&"); + break; + default: + g_string_append_c (s, *p); + break; + } + } + + return g_string_free (s, FALSE); +} + +gboolean +_gtk_file_chooser_native_win32_show (GtkFileChooserNative *self) +{ + GThread *thread; + FilechooserWin32ThreadData *data; + GtkWindow *transient_for; + GtkFileChooserAction action; + guint update_preview_signal; + GSList *filters, *l; + int n_filters, i; + + /* 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; + + data = g_new0 (FilechooserWin32ThreadData, 1); + data->filter_index = 1; + + filters = gtk_file_chooser_list_filters (GTK_FILE_CHOOSER (self)); + n_filters = g_slist_length (filters); + if (n_filters > 0) + { + GtkFileFilter *current = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (self)); + + data->filters = g_new0 (COMDLG_FILTERSPEC, n_filters + 1); + data->n_filters = n_filters; + + for (l = filters, i = 0; l != NULL; l = l->next, i++) + { + if (!file_filter_to_win32 (l->data, &data->filters[i])) + { + g_slist_free (filters); + filechooser_win32_thread_data_free (data); + return FALSE; + } + if (l->data == current) + data->filter_index = i + 1; + } + } + g_slist_free (filters); + + self->mode_data = data; + data->self = g_object_ref (self); + + data->shortcut_uris = + gtk_file_chooser_list_shortcut_folder_uris (GTK_FILE_CHOOSER (self->dialog)); + + data->accept_label = translate_mnemonics (self->accept_label); + data->cancel_label = translate_mnemonics (self->cancel_label); + + action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (self->dialog)); + if (action == GTK_FILE_CHOOSER_ACTION_SAVE || + action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER) + data->save = TRUE; + + if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER || + action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER) + data->folder = TRUE; + + if ((action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER || + action == GTK_FILE_CHOOSER_ACTION_OPEN) && + gtk_file_chooser_get_select_multiple (GTK_FILE_CHOOSER (self->dialog))) + data->select_multiple = TRUE; + + if (gtk_file_chooser_get_do_overwrite_confirmation (GTK_FILE_CHOOSER (self->dialog))) + data->overwrite_confirmation = TRUE; + + if (gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (self->dialog))) + data->show_hidden = TRUE; + + transient_for = gtk_native_dialog_get_transient_for (GTK_NATIVE_DIALOG (self)); + if (transient_for) + { + gtk_widget_realize (GTK_WIDGET (transient_for)); + data->parent = GDK_WINDOW_HWND (gtk_widget_get_window (GTK_WIDGET (transient_for))); + + if (gtk_native_dialog_get_modal (GTK_NATIVE_DIALOG (self))) + data->modal = TRUE; + } + + data->title = g_strdup (gtk_native_dialog_get_title (GTK_NATIVE_DIALOG (self))); + + if (self->current_file) + data->current_file = g_object_ref (self->current_file); + else + { + if (self->current_folder) + data->current_folder = g_object_ref (self->current_folder); + + if (action == GTK_FILE_CHOOSER_ACTION_SAVE || + action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER) + data->current_name = g_strdup (self->current_name); + } + + thread = g_thread_try_new ("win32 filechooser", filechooser_win32_thread, data, NULL); + if (thread == NULL) + { + self->mode_data = NULL; + filechooser_win32_thread_data_free (data); + return FALSE; + } + g_thread_unref (thread); + + if (gtk_native_dialog_get_modal (GTK_NATIVE_DIALOG (self))) + { + data->grab_widget = gtk_invisible_new (); + gtk_grab_add (data->grab_widget); + } + + return TRUE; +} + +void +_gtk_file_chooser_native_win32_hide (GtkFileChooserNative *self) +{ + FilechooserWin32ThreadData *data = self->mode_data; + + /* set while the dialog is visible */ + g_return_if_fail (data != NULL); + + data->skip_response = TRUE; + filechooser_win32_send_close (data); +} + +#define __GTK_FILE_CHOOSER_NATIVE_WIN32_C__ +#include "gtkaliasdef.c" diff --git a/gtk/gtkfilefilter.c b/gtk/gtkfilefilter.c index 1c8f4e8c7..1e62e67a2 100644 --- a/gtk/gtkfilefilter.c +++ b/gtk/gtkfilefilter.c @@ -20,8 +20,10 @@ #include "config.h" #include +#include #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" diff --git a/gtk/gtkfilefilter.h b/gtk/gtkfilefilter.h index 696c7b510..caaa29e37 100644 --- a/gtk/gtkfilefilter.h +++ b/gtk/gtkfilefilter.h @@ -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); diff --git a/gtk/gtkfilefilterprivate.h b/gtk/gtkfilefilterprivate.h new file mode 100644 index 000000000..4cec69fae --- /dev/null +++ b/gtk/gtkfilefilterprivate.h @@ -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 + +G_BEGIN_DECLS + +char **_gtk_file_filter_get_as_patterns (GtkFileFilter *filter); + +G_END_DECLS + +#endif /* __GTK_FILE_FILTER_PRIVATE_H__ */ diff --git a/gtk/gtknativedialog.c b/gtk/gtknativedialog.c new file mode 100644 index 000000000..81ddfbfeb --- /dev/null +++ b/gtk/gtknativedialog.c @@ -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" diff --git a/gtk/gtknativedialog.h b/gtk/gtknativedialog.h new file mode 100644 index 000000000..11120aa1e --- /dev/null +++ b/gtk/gtknativedialog.h @@ -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 can be included directly." +#endif + +#include + +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); + + /* */ + 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__ */ diff --git a/gtk/gtknativedialogprivate.h b/gtk/gtknativedialogprivate.h new file mode 100644 index 000000000..89573eafd --- /dev/null +++ b/gtk/gtknativedialogprivate.h @@ -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 + +G_BEGIN_DECLS + +void _gtk_native_dialog_emit_response (GtkNativeDialog *self, + int response_id); + +G_END_DECLS + +#endif /* __GTK_NATIVE_DIALOG_PRIVATE_H__ */