diff --git a/docs/reference/gtk/gtk3-sections.txt b/docs/reference/gtk/gtk3-sections.txt index de228f33c0..f48b20bc20 100644 --- a/docs/reference/gtk/gtk3-sections.txt +++ b/docs/reference/gtk/gtk3-sections.txt @@ -1316,6 +1316,7 @@ gtk_file_chooser_get_do_overwrite_confirmation gtk_file_chooser_set_create_folders gtk_file_chooser_get_create_folders gtk_file_chooser_set_current_name +gtk_file_chooser_get_current_name gtk_file_chooser_get_filename gtk_file_chooser_set_filename gtk_file_chooser_select_filename diff --git a/gtk/gtkfilechooser.c b/gtk/gtkfilechooser.c index 42bc0d22be..e199d3e070 100644 --- a/gtk/gtkfilechooser.c +++ b/gtk/gtkfilechooser.c @@ -1352,6 +1352,34 @@ gtk_file_chooser_set_current_name (GtkFileChooser *chooser, GTK_FILE_CHOOSER_GET_IFACE (chooser)->set_current_name (chooser, name); } +/** + * gtk_file_chooser_get_current_name: + * @chooser: a #GtkFileChooser + * + * Gets the current name in the file selector, as entered by the user in the + * text entry for "Name". + * + * This is meant to be used in save dialogs, to get the currently typed filename + * when the file itself does not exist yet. For example, an application that + * adds a custom extra widget to the file chooser for "file format" may want to + * change the extension of the typed filename based on the chosen format, say, + * from ".jpg" to ".png". + * + * Returns: The raw text from the file chooser's "Name" entry. Free this with + * g_free(). Note that this string is not a full pathname or URI; it is + * whatever the contents of the entry are. Note also that this string is in + * UTF-8 encoding, which is not necessarily the system's encoding for filenames. + * + * Since: 3.10 + **/ +gchar * +gtk_file_chooser_get_current_name (GtkFileChooser *chooser) +{ + g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser)); + + return GTK_FILE_CHOOSER_GET_IFACE (chooser)->get_current_name (chooser); +} + /** * gtk_file_chooser_get_uri: * @chooser: a #GtkFileChooser diff --git a/gtk/gtkfilechooser.h b/gtk/gtkfilechooser.h index 0705737cd8..6dff84ec2b 100644 --- a/gtk/gtkfilechooser.h +++ b/gtk/gtkfilechooser.h @@ -153,8 +153,10 @@ gboolean gtk_file_chooser_get_create_folders (GtkFileChooser *choose /* Suggested name for the Save-type actions */ GDK_AVAILABLE_IN_ALL -void gtk_file_chooser_set_current_name (GtkFileChooser *chooser, - const gchar *name); +void gtk_file_chooser_set_current_name (GtkFileChooser *chooser, + const gchar *name); +GDK_AVAILABLE_IN_3_10 +gchar *gtk_file_chooser_get_current_name (GtkFileChooser *chooser); /* Filename manipulation */ diff --git a/gtk/gtkfilechooserdefault.c b/gtk/gtkfilechooserdefault.c index 1f67175e9a..e0dee0e859 100644 --- a/gtk/gtkfilechooserdefault.c +++ b/gtk/gtkfilechooserdefault.c @@ -436,6 +436,7 @@ static gboolean gtk_file_chooser_default_update_current_folder (GtkFil static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser); static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser, const gchar *name); +static gchar * gtk_file_chooser_default_get_current_name (GtkFileChooser *chooser); static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser, GFile *file, GError **error); @@ -563,6 +564,7 @@ gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface) iface->set_current_folder = gtk_file_chooser_default_set_current_folder; iface->get_current_folder = gtk_file_chooser_default_get_current_folder; iface->set_current_name = gtk_file_chooser_default_set_current_name; + iface->get_current_name = gtk_file_chooser_default_get_current_name; iface->add_filter = gtk_file_chooser_default_add_filter; iface->remove_filter = gtk_file_chooser_default_remove_filter; iface->list_filters = gtk_file_chooser_default_list_filters; @@ -4755,6 +4757,19 @@ gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser, gtk_entry_set_text (GTK_ENTRY (priv->location_entry), name); } +static gchar * +gtk_file_chooser_default_get_current_name (GtkFileChooser *chooser) +{ + GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser); + GtkFileChooserDefaultPrivate *priv = impl->priv; + + g_return_val_if_fail (priv->action == GTK_FILE_CHOOSER_ACTION_SAVE || + priv->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER, + NULL); + + return g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->location_entry))); +} + static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser, GFile *file, diff --git a/gtk/gtkfilechooserprivate.h b/gtk/gtkfilechooserprivate.h index 61a3aac6db..58bfc9dbc9 100644 --- a/gtk/gtkfilechooserprivate.h +++ b/gtk/gtkfilechooserprivate.h @@ -61,6 +61,7 @@ struct _GtkFileChooserIface GFile * (*get_current_folder) (GtkFileChooser *chooser); void (*set_current_name) (GtkFileChooser *chooser, const gchar *name); + gchar * (*get_current_name) (GtkFileChooser *chooser); gboolean (*select_file) (GtkFileChooser *chooser, GFile *file, GError **error); diff --git a/gtk/gtkfilechooserutils.c b/gtk/gtkfilechooserutils.c index 43148aa9c3..2b2542cd7b 100644 --- a/gtk/gtkfilechooserutils.c +++ b/gtk/gtkfilechooserutils.c @@ -31,6 +31,7 @@ static gboolean delegate_set_current_folder (GtkFileChooser *choose static GFile * delegate_get_current_folder (GtkFileChooser *chooser); static void delegate_set_current_name (GtkFileChooser *chooser, const gchar *name); +static gchar * delegate_get_current_name (GtkFileChooser *chooser); static gboolean delegate_select_file (GtkFileChooser *chooser, GFile *file, GError **error); @@ -134,6 +135,7 @@ _gtk_file_chooser_delegate_iface_init (GtkFileChooserIface *iface) iface->set_current_folder = delegate_set_current_folder; iface->get_current_folder = delegate_get_current_folder; iface->set_current_name = delegate_set_current_name; + iface->get_current_name = delegate_get_current_name; iface->select_file = delegate_select_file; iface->unselect_file = delegate_unselect_file; iface->select_all = delegate_select_all; @@ -308,6 +310,12 @@ delegate_set_current_name (GtkFileChooser *chooser, gtk_file_chooser_set_current_name (get_delegate (chooser), name); } +static gchar * +delegate_get_current_name (GtkFileChooser *chooser) +{ + return gtk_file_chooser_get_current_name (get_delegate (chooser)); +} + static void delegate_notify (GObject *object, GParamSpec *pspec,