[ghex] findreplace: Add clipboard keybindings.



commit 078b238bc4679b364ae6aabab4ffd8038210fa51
Author: Logan Rathbone <poprocks gmail com>
Date:   Sat Dec 4 14:20:29 2021 -0500

    findreplace: Add clipboard keybindings.
    
    This patch adds Ctrl+{C,X,V} keybindings to the Find, Replace and
    Advanced Find dialogs of ghex3.
    
    Note that this has been done manually in these dialogs as opposed to
    associating them with the widget per (lib)g(tk)hex4. That is simply
    because some other programs actually use the libgtkhex3 widget
    separately, and I do not want to modify its expected behaviour at this
    time as it is legacy software.
    
    Fixes #36

 src/findreplace.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
---
diff --git a/src/findreplace.c b/src/findreplace.c
index 4be2f4b..1b65baa 100644
--- a/src/findreplace.c
+++ b/src/findreplace.c
@@ -83,6 +83,49 @@ static GtkWidget *create_hex_view(HexDocument *doc)
     return gh;
 }
 
+/* Helper functions to set up copy/paste keybindings */
+static gboolean
+keypress_cb (GtkWidget *dialog, GdkEventKey *event, gpointer user_data)
+{
+       GtkWidget *gh = gtk_window_get_focus (GTK_WINDOW(dialog));
+
+       /* If there isn't a GtkHex widget with focus, bail out. */
+       if (! GTK_IS_HEX (gh)) goto out;
+
+       /* Otherwise, handle clipboard shortcuts. */
+       if (event->state & GDK_CONTROL_MASK)
+       {
+               switch (event->keyval)
+               {
+                       case 'c':
+                               gtk_hex_copy_to_clipboard (GTK_HEX(gh));
+                               return GDK_EVENT_STOP;
+                               break;
+
+                       case 'x':
+                               gtk_hex_cut_to_clipboard (GTK_HEX(gh));
+                               return GDK_EVENT_STOP;
+                               break;
+
+                       case 'v':
+                               gtk_hex_paste_from_clipboard (GTK_HEX(gh));
+                               return GDK_EVENT_STOP;
+                               break;
+               }
+       }
+out:
+       return GDK_EVENT_PROPAGATE;
+}
+
+static void
+setup_clipboard_keybindings (GtkWidget *dialog)
+{
+       /* sanity check: all find/replace/etc. dialogs are GtkDialogs at their core */
+       g_assert (GTK_IS_DIALOG (dialog));
+
+       g_signal_connect (dialog, "key-press-event", G_CALLBACK(keypress_cb), NULL);
+}
+
 FindDialog *create_find_dialog()
 {
        FindDialog *dialog;
@@ -134,6 +177,8 @@ FindDialog *create_find_dialog()
        
gtk_container_set_border_width(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dialog->window))), 2);
        gtk_box_set_spacing(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog->window))), 2);
 
+       setup_clipboard_keybindings (dialog->window);
+
        if (GTK_IS_ACCESSIBLE (gtk_widget_get_accessible (dialog->f_gh))) {
                add_atk_namedesc (dialog->f_gh, _("Find Data"), _("Enter the hex data or ASCII data to search 
for"));
                add_atk_namedesc (dialog->f_next, _("Find Next"), _("Finds the next occurrence of the search 
string"));
@@ -194,6 +239,8 @@ static AdvancedFind_AddDialog *create_advanced_find_add_dialog(AdvancedFindDialo
        gtk_widget_set_can_default(button, TRUE);
        gtk_widget_show(button);
 
+       setup_clipboard_keybindings (dialog->window);
+
        return dialog;
 }
 
@@ -409,6 +456,8 @@ ReplaceDialog *create_replace_dialog()
        
gtk_container_set_border_width(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dialog->window))), 2);
        gtk_box_set_spacing(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog->window))), 2);
 
+       setup_clipboard_keybindings (dialog->window);
+
        if (GTK_IS_ACCESSIBLE(gtk_widget_get_accessible(dialog->f_gh))) {
                add_atk_namedesc (dialog->f_gh, _("Find Data"), _("Enter the hex data or ASCII data to search 
for"));
                add_atk_namedesc (dialog->r_gh, _("Replace Data"), _("Enter the hex data or ASCII data to 
replace with"));


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]