Merge branch 'wip/cdavis/file-dialog-accept-label' into 'main'

gtkfiledialog: Allow devs to set custom accept labels

Closes #5421

See merge request GNOME/gtk!5339
This commit is contained in:
Matthias Clasen 2022-12-16 17:04:00 +00:00
commit c81a793f63
3 changed files with 97 additions and 4 deletions

View File

@ -23,4 +23,23 @@ void gdk_source_set_static_name_by_id (guint tag,
#define I_(string) g_intern_static_string (string) #define I_(string) g_intern_static_string (string)
#endif #endif
#if !GLIB_CHECK_VERSION (2, 76, 0)
static inline gboolean
g_set_str (char **str_pointer,
const char *new_str)
{
char *copy;
if (*str_pointer == new_str ||
(*str_pointer && new_str && strcmp (*str_pointer, new_str) == 0))
return FALSE;
copy = g_strdup (new_str);
g_free (*str_pointer);
*str_pointer = copy;
return TRUE;
}
#endif
#endif /* __GDK__PRIVATE_H__ */ #endif /* __GDK__PRIVATE_H__ */

View File

@ -25,6 +25,7 @@
#include "gtkfilechoosernativeprivate.h" #include "gtkfilechoosernativeprivate.h"
#include "gtkdialogerror.h" #include "gtkdialogerror.h"
#include <glib/gi18n-lib.h> #include <glib/gi18n-lib.h>
#include "gdk/gdkprivate.h"
#include "gdk/gdkdebugprivate.h" #include "gdk/gdkdebugprivate.h"
/** /**
@ -51,6 +52,7 @@ struct _GtkFileDialog
GObject parent_instance; GObject parent_instance;
char *title; char *title;
char *accept_label;
unsigned int modal : 1; unsigned int modal : 1;
GListModel *filters; GListModel *filters;
@ -67,6 +69,7 @@ enum
PROP_SHORTCUT_FOLDERS, PROP_SHORTCUT_FOLDERS,
PROP_CURRENT_FILTER, PROP_CURRENT_FILTER,
PROP_CURRENT_FOLDER, PROP_CURRENT_FOLDER,
PROP_ACCEPT_LABEL,
NUM_PROPERTIES NUM_PROPERTIES
}; };
@ -87,6 +90,7 @@ gtk_file_dialog_finalize (GObject *object)
GtkFileDialog *self = GTK_FILE_DIALOG (object); GtkFileDialog *self = GTK_FILE_DIALOG (object);
g_free (self->title); g_free (self->title);
g_free (self->accept_label);
g_clear_object (&self->filters); g_clear_object (&self->filters);
g_clear_object (&self->shortcut_folders); g_clear_object (&self->shortcut_folders);
g_clear_object (&self->current_filter); g_clear_object (&self->current_filter);
@ -129,6 +133,10 @@ gtk_file_dialog_get_property (GObject *object,
g_value_set_object (value, self->current_folder); g_value_set_object (value, self->current_folder);
break; break;
case PROP_ACCEPT_LABEL:
g_value_set_string (value, self->accept_label);
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break; break;
@ -169,6 +177,10 @@ gtk_file_dialog_set_property (GObject *object,
gtk_file_dialog_set_current_folder (self, g_value_get_object (value)); gtk_file_dialog_set_current_folder (self, g_value_get_object (value));
break; break;
case PROP_ACCEPT_LABEL:
gtk_file_dialog_set_accept_label (self, g_value_get_string (value));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
@ -259,6 +271,18 @@ gtk_file_dialog_class_init (GtkFileDialogClass *class)
G_TYPE_FILE, G_TYPE_FILE,
G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS|G_PARAM_EXPLICIT_NOTIFY); G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS|G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkFileDialog:accept-label: (attributes org.gtk.Property.get=gtk_file_dialog_get_accept_label org.gtk.Property.set=gtk_file_dialog_set_accept_label)
*
* Label for the file chooser's accept button.
*
* Since: 4.10
*/
properties[PROP_ACCEPT_LABEL] =
g_param_spec_string ("accept-label", NULL, NULL,
NULL,
G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS|G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (object_class, NUM_PROPERTIES, properties); g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
} }
@ -656,24 +680,24 @@ create_file_chooser (GtkFileDialog *self,
gboolean select_multiple) gboolean select_multiple)
{ {
GtkFileChooserNative *chooser; GtkFileChooserNative *chooser;
const char *accept; const char *default_accept_label, *accept;
const char *default_title, *title; const char *default_title, *title;
GdkDisplay *display G_GNUC_UNUSED; GdkDisplay *display G_GNUC_UNUSED;
switch (action) switch (action)
{ {
case GTK_FILE_CHOOSER_ACTION_OPEN: case GTK_FILE_CHOOSER_ACTION_OPEN:
accept = _("_Open"); default_accept_label = _("_Open");
default_title = select_multiple ? _("Pick Files") : _("Pick a File"); default_title = select_multiple ? _("Pick Files") : _("Pick a File");
break; break;
case GTK_FILE_CHOOSER_ACTION_SAVE: case GTK_FILE_CHOOSER_ACTION_SAVE:
accept = _("_Save"); default_accept_label = _("_Save");
default_title = _("Save a File"); default_title = _("Save a File");
break; break;
case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER: case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
accept = _("_Select"); default_accept_label = _("_Select");
default_title = select_multiple ? _("Select Folders") : _("Select a Folder"); default_title = select_multiple ? _("Select Folders") : _("Select a Folder");
break; break;
@ -686,6 +710,11 @@ create_file_chooser (GtkFileDialog *self,
else else
title = default_title; title = default_title;
if (self->accept_label)
accept = self->accept_label;
else
accept = default_accept_label;
chooser = gtk_file_chooser_native_new (title, parent, action, accept, _("_Cancel")); chooser = gtk_file_chooser_native_new (title, parent, action, accept, _("_Cancel"));
if (parent) if (parent)
@ -1136,6 +1165,44 @@ gtk_file_dialog_select_multiple_folders_finish (GtkFileDialog *self,
return finish_multiple_files_op (self, G_TASK (result), error); return finish_multiple_files_op (self, G_TASK (result), error);
} }
/**
* gtk_file_dialog_get_accept_label:
* @self: a `GtkFileDialog`
*
* Returns: (nullable): the label shown on the file chooser's accept button.
*
* Since: 4.10
*/
const char *
gtk_file_dialog_get_accept_label (GtkFileDialog *self)
{
g_return_val_if_fail (GTK_IS_FILE_DIALOG (self), NULL);
return self->accept_label;
}
/**
* gtk_file_dialog_set_accept_label:
* @self: a `GtkFileDialog`
* @accept_label: (nullable): the new accept label
*
* Sets the label shown on the file chooser's accept button.
*
* Leaving the accept label unset or setting it as `NULL` will fall back to
* a default label, depending on what API is used to launch the file dialog.
*
* Since: 4.10
*/
void
gtk_file_dialog_set_accept_label (GtkFileDialog *self,
const char *accept_label)
{
g_return_if_fail (GTK_IS_FILE_DIALOG (self));
if (g_set_str (&self->accept_label, accept_label))
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACCEPT_LABEL]);
}
/* }}} */ /* }}} */
/* vim:set foldmethod=marker expandtab: */ /* vim:set foldmethod=marker expandtab: */

View File

@ -148,4 +148,11 @@ GListModel * gtk_file_dialog_select_multiple_folders_finish
GAsyncResult *result, GAsyncResult *result,
GError **error); GError **error);
GDK_AVAILABLE_IN_4_10
const char * gtk_file_dialog_get_accept_label (GtkFileDialog *self);
GDK_AVAILABLE_IN_4_10
void gtk_file_dialog_set_accept_label (GtkFileDialog *self,
const char *accept_label);
G_END_DECLS G_END_DECLS