From a4207256c4d04b644142aa0b7c8a2038425498a7 Mon Sep 17 00:00:00 2001 From: Andre Miranda Date: Fri, 14 Dec 2018 22:00:19 -0300 Subject: [PATCH] Add support for CTRL+Z in rename dialog (Bug #14956) --- thunar/thunar-dialogs.c | 90 ++++++++++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 18 deletions(-) diff --git a/thunar/thunar-dialogs.c b/thunar/thunar-dialogs.c index d5c02253..efa2ca12 100644 --- a/thunar/thunar-dialogs.c +++ b/thunar/thunar-dialogs.c @@ -43,6 +43,14 @@ +static void thunar_dialogs_select_filename (GtkWidget *entry, + ThunarFile *file); +static gboolean thunar_dialogs_entry_undo (GtkWidget *widget, + GdkEvent *event, + ThunarFile *file); + + + /** * thunar_dialogs_show_rename_file: * @parent : a #GtkWidget on which the error dialog should be shown, or a #GdkScreen @@ -72,7 +80,6 @@ thunar_dialogs_show_rename_file (gpointer parent, GtkWindow *window; GdkPixbuf *icon; GdkScreen *screen; - glong offset; gchar *title; gint response; PangoLayout *layout; @@ -142,24 +149,12 @@ thunar_dialogs_show_rename_file (gpointer parent, /* setup the old filename */ gtk_entry_set_text (GTK_ENTRY (entry), filename); - /* check if we don't have a directory here */ - if (!thunar_file_is_directory (file)) - { - /* check if the filename contains an extension */ - text = thunar_util_str_get_extension (filename); - if (G_LIKELY (text != NULL)) - { - /* grab focus to the entry first, else the selection will be altered later */ - gtk_widget_grab_focus (entry); - - /* determine the UTF-8 char offset */ - offset = g_utf8_pointer_to_offset (filename, text); + /* allow reverting the filename with ctrl + z */ + g_signal_connect (entry, "key-press-event", + G_CALLBACK (thunar_dialogs_entry_undo), file); - /* select the text prior to the dot */ - if (G_LIKELY (offset > 0)) - gtk_editable_select_region (GTK_EDITABLE (entry), 0, offset); - } - } + /* select the filename without the extension */ + thunar_dialogs_select_filename (entry, file); /* get the size the entry requires to render the full text */ layout = gtk_entry_get_layout (GTK_ENTRY (entry)); @@ -855,3 +850,62 @@ thunar_dialogs_show_insecure_program (gpointer parent, return (response == GTK_RESPONSE_OK); } + + + +static void thunar_dialogs_select_filename (GtkWidget *entry, + ThunarFile *file) +{ + const gchar *filename; + const gchar *ext; + glong offset; + + /* check if we don't have a directory here */ + if (thunar_file_is_directory (file)) + { + gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1); + return; + } + + filename = thunar_file_get_display_name (file); + + /* check if the filename contains an extension */ + ext = thunar_util_str_get_extension (filename); + if (G_UNLIKELY (ext == NULL)) + return; + + /* grab focus to the entry first, else the selection will be altered later */ + gtk_widget_grab_focus (entry); + + /* determine the UTF-8 char offset */ + offset = g_utf8_pointer_to_offset (filename, ext); + + /* select the text prior to the dot */ + if (G_LIKELY (offset > 0)) + gtk_editable_select_region (GTK_EDITABLE (entry), 0, offset); +} + + + +static gboolean thunar_dialogs_entry_undo (GtkWidget *widget, + GdkEvent *event, + ThunarFile *file) +{ + guint keyval; + GdkModifierType state; + + if (G_UNLIKELY (!gdk_event_get_keyval (event, &keyval) && + !gdk_event_get_state (event, &state))) + return GDK_EVENT_PROPAGATE; + + if ((state & GDK_CONTROL_MASK) != 0 && keyval == GDK_KEY_z) + { + gtk_entry_set_text (GTK_ENTRY (widget), + thunar_file_get_display_name (file)); + thunar_dialogs_select_filename (widget, file); + + return GDK_EVENT_STOP; + } + + return GDK_EVENT_PROPAGATE; +} -- 2.20.0