filedialog: Add gtk_file_dialog_set_initial_name()

This commit is contained in:
Benjamin Otte 2022-12-23 03:43:01 +01:00 committed by Matthias Clasen
parent 26cec4a021
commit 8a2d35b9fb
8 changed files with 97 additions and 26 deletions

View File

@ -342,7 +342,6 @@ save_cb (GtkWidget *button,
gtk_file_dialog_save (dialog,
GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (button))),
NULL, NULL,
NULL,
save_response_cb, self);
g_object_unref (dialog);
}

View File

@ -634,11 +634,11 @@ save_cb (GtkWidget *button,
gtk_file_dialog_set_title (dialog, "Save node");
cwd = g_file_new_for_path (".");
gtk_file_dialog_set_initial_folder (dialog, cwd);
gtk_file_dialog_set_initial_name (dialog, "demo.node");
g_object_unref (cwd);
gtk_file_dialog_save (dialog,
GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (button))),
NULL,
"demo.node",
NULL,
save_response_cb, self);
g_object_unref (dialog);
@ -742,10 +742,10 @@ export_image_cb (GtkWidget *button,
dialog = gtk_file_dialog_new ();
gtk_file_dialog_set_title (dialog, "");
gtk_file_dialog_set_initial_name (dialog, "example.png");
gtk_file_dialog_save (dialog,
GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (button))),
NULL,
"example.png",
NULL,
export_image_response_cb, texture);
g_object_unref (dialog);

View File

@ -509,7 +509,6 @@ activate_save_as (GSimpleAction *action,
GTK_WINDOW (main_window),
NULL,
NULL,
NULL,
on_save_response, NULL);
g_object_unref (dialog);
}

View File

@ -59,17 +59,20 @@ struct _GtkFileDialog
GListModel *shortcut_folders;
GtkFileFilter *current_filter;
GFile *initial_folder;
char *initial_name;
};
enum
{
PROP_TITLE = 1,
PROP_MODAL,
PROP_FILTERS,
PROP_SHORTCUT_FOLDERS,
PROP_CURRENT_FILTER,
PROP_INITIAL_FOLDER,
PROP_0,
PROP_ACCEPT_LABEL,
PROP_CURRENT_FILTER,
PROP_FILTERS,
PROP_INITIAL_FOLDER,
PROP_INITIAL_NAME,
PROP_MODAL,
PROP_SHORTCUT_FOLDERS,
PROP_TITLE,
NUM_PROPERTIES
};
@ -95,6 +98,7 @@ gtk_file_dialog_finalize (GObject *object)
g_clear_object (&self->shortcut_folders);
g_clear_object (&self->current_filter);
g_clear_object (&self->initial_folder);
g_free (self->initial_name);
G_OBJECT_CLASS (gtk_file_dialog_parent_class)->finalize (object);
}
@ -133,6 +137,10 @@ gtk_file_dialog_get_property (GObject *object,
g_value_set_object (value, self->initial_folder);
break;
case PROP_INITIAL_NAME:
g_value_set_string (value, self->initial_name);
break;
case PROP_ACCEPT_LABEL:
g_value_set_string (value, self->accept_label);
break;
@ -177,6 +185,10 @@ gtk_file_dialog_set_property (GObject *object,
gtk_file_dialog_set_initial_folder (self, g_value_get_object (value));
break;
case PROP_INITIAL_NAME:
gtk_file_dialog_set_initial_name (self, g_value_get_string (value));
break;
case PROP_ACCEPT_LABEL:
gtk_file_dialog_set_accept_label (self, g_value_get_string (value));
break;
@ -271,6 +283,19 @@ gtk_file_dialog_class_init (GtkFileDialogClass *class)
G_TYPE_FILE,
G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS|G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkFileDialog:initial-name: (attributes org.gtk.Property.get=gtk_file_dialog_get_initial_name org.gtk.Property.set=gtk_file_dialog_set_initial_name)
*
* The inital name, that is, the filename that is initially
* selected in the file chooser dialog.
*
* Since: 4.10
*/
properties[PROP_INITIAL_NAME] =
g_param_spec_string ("initial-name", NULL, NULL,
NULL,
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)
*
@ -616,6 +641,49 @@ gtk_file_dialog_set_initial_folder (GtkFileDialog *self,
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_INITIAL_FOLDER]);
}
/**
* gtk_file_dialog_get_initial_name:
* @self: a `GtkFileDialog`
*
* Gets the name for the file that should be initially set.
*
* Returns: (nullable) (transfer none): the name
*
* Since: 4.10
*/
const char *
gtk_file_dialog_get_initial_name (GtkFileDialog *self)
{
g_return_val_if_fail (GTK_IS_FILE_DIALOG (self), NULL);
return self->initial_name;
}
/**
* gtk_file_dialog_set_initial_name:
* @self: a `GtkFileDialog`
* @name: (nullable): a UTF8 string
*
* Sets the name for the file that should be initially set.
* For saving dialogs, this will usually be pre-entered into the name field.
*
* If a file with this name already exists in the directory set via
* [property@Gtk.FileDialog:initial-folder], the dialog should preselect it.
*
* Since: 4.10
*/
void
gtk_file_dialog_set_initial_name (GtkFileDialog *self,
const char *name)
{
g_return_if_fail (GTK_IS_FILE_DIALOG (self));
if (!g_set_str (&self->initial_name, name))
return;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_INITIAL_NAME]);
}
/* }}} */
/* {{{ Async implementation */
@ -676,7 +744,6 @@ create_file_chooser (GtkFileDialog *self,
GtkWindow *parent,
GtkFileChooserAction action,
GFile *current_file,
const char *current_name,
gboolean select_multiple)
{
GtkFileChooserNative *chooser;
@ -738,8 +805,8 @@ create_file_chooser (GtkFileDialog *self,
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser), self->initial_folder, NULL);
if (current_file)
gtk_file_chooser_set_file (GTK_FILE_CHOOSER (chooser), current_file, NULL);
else if (current_name)
gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (chooser), current_name);
else if (self->initial_name)
gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (chooser), self->initial_name);
return chooser;
}
@ -815,7 +882,7 @@ gtk_file_dialog_open (GtkFileDialog *self,
g_return_if_fail (GTK_IS_FILE_DIALOG (self));
chooser = create_file_chooser (self, parent, GTK_FILE_CHOOSER_ACTION_OPEN,
current_file, NULL, FALSE);
current_file, FALSE);
task = g_task_new (self, cancellable, callback, user_data);
g_task_set_check_cancellable (task, FALSE);
@ -892,7 +959,7 @@ gtk_file_dialog_select_folder (GtkFileDialog *self,
g_return_if_fail (GTK_IS_FILE_DIALOG (self));
chooser = create_file_chooser (self, parent, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
initial_folder, NULL, FALSE);
initial_folder, FALSE);
task = g_task_new (self, cancellable, callback, user_data);
g_task_set_check_cancellable (task, FALSE);
@ -938,7 +1005,6 @@ gtk_file_dialog_select_folder_finish (GtkFileDialog *self,
* @self: a `GtkFileDialog`
* @parent: (nullable): the parent `GtkWindow`
* @current_file: (nullable): the initial file
* @current_name: (nullable): the initial filename to offer
* @cancellable: (nullable): a `GCancellable` to cancel the operation
* @callback: (scope async): a callback to call when the operation is complete
* @user_data: (closure callback): data to pass to @callback
@ -963,7 +1029,6 @@ void
gtk_file_dialog_save (GtkFileDialog *self,
GtkWindow *parent,
GFile *current_file,
const char *current_name,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
@ -974,7 +1039,7 @@ gtk_file_dialog_save (GtkFileDialog *self,
g_return_if_fail (GTK_IS_FILE_DIALOG (self));
chooser = create_file_chooser (self, parent, GTK_FILE_CHOOSER_ACTION_SAVE,
current_file, current_name, FALSE);
current_file, FALSE);
task = g_task_new (self, cancellable, callback, user_data);
g_task_set_check_cancellable (task, FALSE);
@ -1048,7 +1113,7 @@ gtk_file_dialog_open_multiple (GtkFileDialog *self,
g_return_if_fail (GTK_IS_FILE_DIALOG (self));
chooser = create_file_chooser (self, parent, GTK_FILE_CHOOSER_ACTION_OPEN,
NULL, NULL, TRUE);
NULL, TRUE);
task = g_task_new (self, cancellable, callback, user_data);
g_task_set_check_cancellable (task, FALSE);
@ -1123,7 +1188,7 @@ gtk_file_dialog_select_multiple_folders (GtkFileDialog *self,
g_return_if_fail (GTK_IS_FILE_DIALOG (self));
chooser = create_file_chooser (self, parent, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
NULL, NULL, TRUE);
NULL, TRUE);
task = g_task_new (self, cancellable, callback, user_data);
g_task_set_check_cancellable (task, FALSE);

View File

@ -80,6 +80,13 @@ GDK_AVAILABLE_IN_4_10
void gtk_file_dialog_set_initial_folder (GtkFileDialog *self,
GFile *folder);
GDK_AVAILABLE_IN_4_10
const char * gtk_file_dialog_get_initial_name (GtkFileDialog *self);
GDK_AVAILABLE_IN_4_10
void gtk_file_dialog_set_initial_name (GtkFileDialog *self,
const char *name);
GDK_AVAILABLE_IN_4_10
void gtk_file_dialog_open (GtkFileDialog *self,
GtkWindow *parent,
@ -111,7 +118,6 @@ GDK_AVAILABLE_IN_4_10
void gtk_file_dialog_save (GtkFileDialog *self,
GtkWindow *parent,
GFile *current_file,
const char *current_name,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);

View File

@ -737,10 +737,10 @@ filesave_choose_cb (GtkWidget *button,
dialog = gtk_file_dialog_new ();
gtk_file_dialog_set_title (dialog, _("Select a filename"));
gtk_file_dialog_set_initial_name (dialog, current_name);
gtk_file_dialog_save (dialog,
GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (widget))),
current_folder,
current_name,
NULL,
dialog_response_callback, widget);

View File

@ -243,10 +243,10 @@ save_clicked (GtkButton *button,
GtkFileDialog *dialog;
dialog = gtk_file_dialog_new ();
gtk_file_dialog_set_initial_name (dialog, "custom.css");
gtk_file_dialog_save (dialog,
GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (ce))),
NULL, "custom.css",
NULL,
NULL, NULL,
save_response, ce);
g_object_unref (dialog);
}

View File

@ -1859,12 +1859,14 @@ render_node_save (GtkButton *button,
filename = g_strdup_printf ("%s.node", nodename);
dialog = gtk_file_dialog_new ();
gtk_file_dialog_set_initial_name (dialog, filename);
gtk_file_dialog_save (dialog,
GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (recorder))),
NULL, filename,
NULL,
NULL, NULL,
render_node_save_response, node);
g_object_unref (dialog);
g_free (filename);
g_free (nodename);
}
static void