[ghex/gtk4-port: 58/91] Add clear to find/replace; change close button




commit ad9fc03f6003d10a6b35715fded16650b1dec24e
Author: Logan Rathbone <poprocks gmail com>
Date:   Tue Jan 26 08:40:02 2021 -0500

    Add clear to find/replace; change close button

 src/findreplace.c             | 178 ++++++++++++++++++++++++++++++++++--------
 src/findreplace.h             |  12 +--
 src/ghex-application-window.c |  23 ++++--
 src/gtkhex.c                  |   4 +-
 4 files changed, 170 insertions(+), 47 deletions(-)
---
diff --git a/src/findreplace.c b/src/findreplace.c
index c54e6b6c..d7ee785d 100644
--- a/src/findreplace.c
+++ b/src/findreplace.c
@@ -50,6 +50,7 @@ struct _JumpDialog {
        GtkWidget parent_instance;
 
        GtkHex *gh;
+
        GtkWidget *box;
        GtkWidget *label;
        GtkWidget *int_entry;
@@ -64,14 +65,15 @@ struct _FindDialog {
        GtkWidget parent_instance;
 
        GtkHex *gh;
+       GtkHex_AutoHighlight *auto_highlight;
+
+       HexDocument *f_doc;
+       GtkWidget *f_gh;
+
        GtkWidget *frame;
        GtkWidget *vbox;
        GtkWidget *hbox;
-       HexDocument *f_doc;
-       GtkWidget *f_gh;
-       GtkWidget *f_next, *f_prev, *f_close;
-       
-       GtkHex_AutoHighlight *auto_highlight;
+       GtkWidget *f_next, *f_prev, *f_clear, *f_close;
 };
 
 static guint find_signals[LAST_SIGNAL];
@@ -82,14 +84,15 @@ struct _ReplaceDialog {
        GtkWidget parent_instance;
 
        GtkHex *gh;
+       GtkHex_AutoHighlight *auto_highlight;
+
+       GtkWidget *f_gh, *r_gh;
+       HexDocument *f_doc, *r_doc;
+
        GtkWidget *vbox;
        GtkWidget *hbox;
        GtkWidget *f_frame, *r_frame;
-       GtkWidget *f_gh, *r_gh;
-       HexDocument *f_doc, *r_doc;
-       GtkWidget *replace, *replace_all, *next, *close;
-       
-       GtkHex_AutoHighlight *auto_highlight;
+       GtkWidget *replace, *replace_all, *next, *clear, *close;
 }; 
 
 static guint replace_signals[LAST_SIGNAL];
@@ -98,20 +101,22 @@ G_DEFINE_TYPE (ReplaceDialog, replace_dialog, GTK_TYPE_WIDGET)
 
 /* PRIVATE FUNCTION DECLARATIONS */
 
-static void find_cancel_cb(GtkButton *button, gpointer user_data);
+static void find_cancel_cb (GtkButton *button, gpointer user_data);
 static void replace_cancel_cb (GtkButton *button, gpointer user_data);
 static void jump_cancel_cb (GtkButton *button, gpointer user_data);
-static void find_next_cb(GtkButton *button, gpointer user_data);
-static void find_prev_cb(GtkButton *button, gpointer user_data);
-static void replace_next_cb(GtkButton *button, gpointer user_data);
-static void replace_one_cb(GtkButton *button, gpointer user_data);
-static void replace_all_cb(GtkButton *button, gpointer user_data);
-static void goto_byte_cb(GtkButton *button, gpointer user_data);
-static gint get_search_string(HexDocument *doc, gchar **str);
+static void find_next_cb (GtkButton *button, gpointer user_data);
+static void find_prev_cb (GtkButton *button, gpointer user_data);
+static void find_clear_cb (GtkButton *button, gpointer user_data);
+static void replace_next_cb (GtkButton *button, gpointer user_data);
+static void replace_one_cb (GtkButton *button, gpointer user_data);
+static void replace_all_cb (GtkButton *button, gpointer user_data);
+static void replace_clear_cb (GtkButton *button, gpointer user_data);
+static void goto_byte_cb (GtkButton *button, gpointer user_data);
+static gint get_search_string (HexDocument *doc, gchar **str);
 
 
 static GtkWidget *
-create_hex_view(HexDocument *doc)
+create_hex_view (HexDocument *doc)
 {
        /* Not going to bother reffing, since add_view does this internally. */
     GtkWidget *gh = hex_document_add_view (doc);
@@ -137,7 +142,7 @@ static gint get_search_string(HexDocument *doc, gchar **str)
 {
        guint size = doc->file_size;
        
-       if(size > 0)
+       if (size > 0)
                *str = (gchar *)hex_document_get_data(doc, 0, size);
        else
                *str = NULL;
@@ -170,7 +175,7 @@ jump_cancel_cb (GtkButton *button, gpointer user_data)
 }
 
 static void
-find_cancel_cb(GtkButton *button, gpointer user_data)
+find_cancel_cb (GtkButton *button, gpointer user_data)
 {
        FindDialog *self = FIND_DIALOG(user_data);
        GtkWidget *widget = GTK_WIDGET(user_data);
@@ -181,7 +186,7 @@ find_cancel_cb(GtkButton *button, gpointer user_data)
        (void)button;   /* unused */
 
        if (self->auto_highlight)
-               gtk_hex_delete_autohighlight(self->gh, self->auto_highlight);
+               gtk_hex_delete_autohighlight (self->gh, self->auto_highlight);
 
        self->auto_highlight = NULL;
 
@@ -191,7 +196,7 @@ find_cancel_cb(GtkButton *button, gpointer user_data)
 }
 
 static void
-find_next_cb(GtkButton *button, gpointer user_data)
+find_next_cb (GtkButton *button, gpointer user_data)
 {
        FindDialog *self = FIND_DIALOG(user_data);
        GtkWidget *widget = GTK_WIDGET(user_data);
@@ -230,9 +235,9 @@ find_next_cb(GtkButton *button, gpointer user_data)
                        , "red");
        */
 
-       if (hex_document_find_forward(doc,
-                                                                cursor_pos + 1,
-                                                                str, str_len, &offset))
+       if (hex_document_find_forward (doc,
+                               cursor_pos + 1,
+                               str, str_len, &offset))
        {
                gtk_hex_set_cursor(self->gh, offset);
        }
@@ -245,7 +250,7 @@ find_next_cb(GtkButton *button, gpointer user_data)
 }
 
 static void
-find_prev_cb(GtkButton *button, gpointer user_data)
+find_prev_cb (GtkButton *button, gpointer user_data)
 {
        FindDialog *self = FIND_DIALOG(user_data);
        GtkWidget *widget = GTK_WIDGET(user_data);
@@ -295,7 +300,29 @@ find_prev_cb(GtkButton *button, gpointer user_data)
 }
 
 static void
-goto_byte_cb(GtkButton *button, gpointer user_data)
+find_clear_cb (GtkButton *button, gpointer user_data)
+{
+       FindDialog *self = FIND_DIALOG(user_data);
+       GtkWidget *widget = GTK_WIDGET(user_data);
+       GtkWidget *new_gh;
+       HexDocument *new_doc;
+
+       g_return_if_fail (GTK_IS_HEX (self->f_gh));
+       g_return_if_fail (HEX_IS_DOCUMENT (self->f_doc));
+
+       new_doc = hex_document_new ();
+       new_gh = create_hex_view (new_doc);
+
+       gtk_frame_set_child (GTK_FRAME(self->frame), new_gh);
+
+       self->f_doc = new_doc;
+       self->f_gh = new_gh;
+
+       gtk_widget_grab_focus (widget);
+}
+
+static void
+goto_byte_cb (GtkButton *button, gpointer user_data)
 {
        JumpDialog *self = JUMP_DIALOG(user_data);
        GtkWidget *widget = GTK_WIDGET(user_data);
@@ -537,6 +564,37 @@ clean_up:
                g_free(rep_str);
 }
 
+static void
+replace_clear_cb (GtkButton *button, gpointer user_data)
+{
+       ReplaceDialog *self = REPLACE_DIALOG(user_data);
+       GtkWidget *widget = GTK_WIDGET(user_data);
+       
+       GtkWidget *new_f_gh, *new_r_gh;
+       HexDocument *new_f_doc, *new_r_doc;
+
+       g_return_if_fail (GTK_IS_HEX (self->f_gh));
+       g_return_if_fail (GTK_IS_HEX (self->r_gh));
+       g_return_if_fail (HEX_IS_DOCUMENT (self->f_doc));
+       g_return_if_fail (HEX_IS_DOCUMENT (self->r_doc));
+
+       new_f_doc = hex_document_new ();
+       new_r_doc = hex_document_new ();
+       new_f_gh = create_hex_view (new_f_doc);
+       new_r_gh = create_hex_view (new_r_doc);
+
+       gtk_frame_set_child (GTK_FRAME(self->f_frame), new_f_gh);
+       gtk_frame_set_child (GTK_FRAME(self->r_frame), new_r_gh);
+
+       self->f_doc = new_f_doc;
+       self->r_doc = new_r_doc;
+       self->f_gh = new_f_gh;
+       self->r_gh = new_r_gh;
+
+       gtk_widget_grab_focus (widget);
+}
+
+
 /* PRIVATE METHOD DEFINITIONS */
 
 /* FindDialog */
@@ -570,7 +628,15 @@ find_dialog_init (FindDialog *self)
                                          G_CALLBACK(find_prev_cb), self);
        gtk_box_append (GTK_BOX(self->hbox), self->f_prev);
 
-       self->f_close = gtk_button_new_with_mnemonic (_("_Close"));
+       self->f_clear = gtk_button_new_with_mnemonic (_("_Clear"));
+       g_signal_connect (G_OBJECT (self->f_clear), "clicked",
+                                         G_CALLBACK(find_clear_cb), self);
+       gtk_box_append (GTK_BOX(self->hbox), self->f_clear);
+
+       self->f_close = gtk_button_new_from_icon_name ("window-close-symbolic");
+       gtk_button_set_has_frame (GTK_BUTTON(self->f_close), FALSE);
+       gtk_widget_set_hexpand (self->f_close, TRUE);
+       gtk_widget_set_halign (self->f_close, GTK_ALIGN_END);
        g_signal_connect (G_OBJECT (self->f_close), "clicked",
                        G_CALLBACK(find_cancel_cb), self);
        gtk_box_append (GTK_BOX(self->hbox), self->f_close);
@@ -596,6 +662,13 @@ find_dialog_init (FindDialog *self)
        g_debug(_("Closes find data window"));
 }
 
+static gboolean 
+find_dialog_grab_focus (GtkWidget *widget)
+{
+       FindDialog *self = FIND_DIALOG(widget);
+
+       return gtk_widget_grab_focus (self->f_gh);
+}
 
 static void
 find_dialog_dispose(GObject *object)
@@ -632,6 +705,8 @@ find_dialog_class_init(FindDialogClass *klass)
        object_class->finalize = find_dialog_finalize;
        /* </boilerplate> */
 
+       widget_class->grab_focus = find_dialog_grab_focus;
+
        /* set the box-type layout manager for this Find dialog widget.
         */
        gtk_widget_class_set_layout_manager_type (widget_class,
@@ -669,6 +744,12 @@ find_dialog_set_hex (FindDialog *self, GtkHex *gh)
        g_debug("%s: setting GtkHex of FindDialog to: %p",
                        __func__, (void *)gh);
 
+       /* Clear auto-highlight if any.
+        */
+       if (self->auto_highlight)
+               gtk_hex_delete_autohighlight (self->gh, self->auto_highlight);
+       self->auto_highlight = NULL;
+
        self->gh = gh;
 }
 
@@ -709,13 +790,22 @@ replace_dialog_init (ReplaceDialog *self)
                                          self);
        gtk_box_append (GTK_BOX(self->hbox), self->replace);
 
-       self->replace_all= gtk_button_new_with_mnemonic (_("Replace _All"));
+       self->replace_all = gtk_button_new_with_mnemonic (_("Replace _All"));
        g_signal_connect (G_OBJECT (self->replace_all),
                                          "clicked", G_CALLBACK(replace_all_cb),
                                          self);
        gtk_box_append (GTK_BOX(self->hbox), self->replace_all);
 
-       self->close = gtk_button_new_with_mnemonic (_("_Close"));
+       self->clear = gtk_button_new_with_mnemonic (_("_Clear"));
+       g_signal_connect (G_OBJECT (self->clear),
+                                         "clicked", G_CALLBACK(replace_clear_cb),
+                                         self);
+       gtk_box_append (GTK_BOX(self->hbox), self->clear);
+       
+       self->close = gtk_button_new_from_icon_name ("window-close-symbolic");
+       gtk_button_set_has_frame (GTK_BUTTON(self->close), FALSE);
+       gtk_widget_set_hexpand (self->close, TRUE);
+       gtk_widget_set_halign (self->close, GTK_ALIGN_END);
        g_signal_connect (G_OBJECT (self->close),
                                          "clicked", G_CALLBACK(replace_cancel_cb),
                                          self);
@@ -744,6 +834,14 @@ replace_dialog_init (ReplaceDialog *self)
        g_debug(_("Closes find and replace data window"));
 }
 
+static gboolean 
+replace_dialog_grab_focus (GtkWidget *widget)
+{
+       ReplaceDialog *self = REPLACE_DIALOG(widget);
+
+       return gtk_widget_grab_focus (self->f_gh);
+}
+
 static void
 replace_dialog_dispose(GObject *object)
 {
@@ -779,6 +877,8 @@ replace_dialog_class_init(ReplaceDialogClass *klass)
        object_class->finalize = replace_dialog_finalize;
        /* </boilerplate> */
 
+       widget_class->grab_focus = replace_dialog_grab_focus;
+
        /* set the box-type layout manager for this Find dialog widget.
         */
        gtk_widget_class_set_layout_manager_type (widget_class,
@@ -814,6 +914,12 @@ replace_dialog_set_hex (ReplaceDialog *self, GtkHex *gh)
        g_debug("%s: setting GtkHex of ReplaceDialog to: %p",
                        __func__, (void *)gh);
 
+       /* Clear auto-highlight if any.
+        */
+       if (self->auto_highlight)
+               gtk_hex_delete_autohighlight (self->gh, self->auto_highlight);
+       self->auto_highlight = NULL;
+
        self->gh = gh;
 }
 
@@ -883,6 +989,14 @@ jump_dialog_init (JumpDialog *self)
        g_debug(_("Closes jump to byte window"));
 }
 
+static gboolean 
+jump_dialog_grab_focus (GtkWidget *widget)
+{
+       JumpDialog *self = JUMP_DIALOG(widget);
+
+       return gtk_widget_grab_focus (self->int_entry);
+}
+
 static void
 jump_dialog_dispose(GObject *object)
 {
@@ -918,6 +1032,8 @@ jump_dialog_class_init(JumpDialogClass *klass)
        object_class->finalize = jump_dialog_finalize;
        /* </boilerplate> */
 
+       widget_class->grab_focus = jump_dialog_grab_focus;
+
        /* CSS */
        gtk_widget_class_set_css_name (widget_class, JUMP_DIALOG_CSS_NAME);
 
diff --git a/src/findreplace.h b/src/findreplace.h
index c419ae2c..7b4537dc 100644
--- a/src/findreplace.h
+++ b/src/findreplace.h
@@ -55,16 +55,16 @@ G_DECLARE_FINAL_TYPE (ReplaceDialog, replace_dialog, REPLACE, DIALOG,
 /* PUBLIC METHOD DECLARATIONS */
 
 /* FindDialog */
-GtkWidget *find_dialog_new(void);
-void find_dialog_set_hex(FindDialog *self, GtkHex *gh);
+GtkWidget *find_dialog_new (void);
+void find_dialog_set_hex (FindDialog *self, GtkHex *gh);
 
 /* ReplaceDialog */
-GtkWidget *replace_dialog_new(void);
-void replace_dialog_set_hex(ReplaceDialog *self, GtkHex *gh);
+GtkWidget *replace_dialog_new (void);
+void replace_dialog_set_hex (ReplaceDialog *self, GtkHex *gh);
 
 /* JumpDialog */
-GtkWidget *jump_dialog_new(void);
-void jump_dialog_set_hex(JumpDialog *self, GtkHex *gh);
+GtkWidget *jump_dialog_new (void);
+void jump_dialog_set_hex (JumpDialog *self, GtkHex *gh);
 
 G_END_DECLS
 
diff --git a/src/ghex-application-window.c b/src/ghex-application-window.c
index 10e81960..98e3fc31 100644
--- a/src/ghex-application-window.c
+++ b/src/ghex-application-window.c
@@ -157,7 +157,7 @@ static void ghex_application_window_set_can_save (GHexApplicationWindow *self,
                gboolean can_save);
 static void ghex_application_window_remove_tab (GHexApplicationWindow *self,
                GHexNotebookTab *tab);
-GHexNotebookTab * ghex_application_window_get_current_tab (GHexApplicationWindow *self);
+static GHexNotebookTab * ghex_application_window_get_current_tab (GHexApplicationWindow *self);
 
 static void set_statusbar(GHexApplicationWindow *self, const char *str);
 static void update_status_message (GHexApplicationWindow *self);
@@ -436,8 +436,15 @@ settings_group_type_changed_cb (GSettings   *settings,
 
 /* ! settings*changed_cb 's */
 
+static void
+refresh_dialogs (GHexApplicationWindow *self)
+{
+       find_dialog_set_hex (FIND_DIALOG(self->find_dialog), self->gh);
+       replace_dialog_set_hex (REPLACE_DIALOG(self->replace_dialog), self->gh);
+       jump_dialog_set_hex (JUMP_DIALOG(self->jump_dialog), self->gh);
+}
 
-GHexNotebookTab *
+static GHexNotebookTab *
 ghex_application_window_get_current_tab (GHexApplicationWindow *self)
 {
        GtkNotebook *notebook;
@@ -1008,6 +1015,7 @@ ghex_application_window_set_show_ ##WIDGET (GHexApplicationWindow *self,  \
                ghex_application_window_set_show_ ## OTHER1 (self, FALSE);                      \
                ghex_application_window_set_show_ ## OTHER2 (self, FALSE);                      \
                gtk_widget_show (self->WIDGET ## _dialog);                                                    
  \
+               gtk_widget_grab_focus (self->WIDGET ## _dialog);                                        \
        } else {                                                                                              
                                  \
                gtk_widget_hide (self->WIDGET ## _dialog);                                                    
  \
        }                                                                                                     
                                          \
@@ -2116,6 +2124,7 @@ ghex_application_window_activate_tab (GHexApplicationWindow *self,
                        __func__, page_num);
 
        gtk_notebook_set_current_page (notebook, page_num);
+       gtk_widget_grab_focus (GTK_WIDGET(gh));
 }
 
 void
@@ -2131,6 +2140,10 @@ ghex_application_window_set_hex (GHexApplicationWindow *self,
                        __func__, (void *)gh);
 
        self->gh = gh;
+
+       /* Update dialogs: */
+
+       refresh_dialogs (self);
 }
 
 void
@@ -2196,12 +2209,6 @@ ghex_application_window_add_hex (GHexApplicationWindow *self,
 
        g_signal_connect (doc, "file-saved",
                        G_CALLBACK(ghex_application_window_file_saved_cb), self);
-
-       /* Setup find_dialog & friends. */
-
-       find_dialog_set_hex (FIND_DIALOG(self->find_dialog), self->gh);
-       replace_dialog_set_hex (REPLACE_DIALOG(self->replace_dialog), self->gh);
-       jump_dialog_set_hex (JUMP_DIALOG(self->jump_dialog), self->gh);
 }
 
 GList *
diff --git a/src/gtkhex.c b/src/gtkhex.c
index 4284fe17..639f6391 100644
--- a/src/gtkhex.c
+++ b/src/gtkhex.c
@@ -1823,13 +1823,13 @@ key_press_cb (GtkEventControllerKey *controller,
        GtkWidget *widget = GTK_WIDGET(user_data);
        gboolean ret = TRUE;
 
-       hide_cursor(gh);
-
        /* don't trample over Ctrl */
        if (state & GDK_CONTROL_MASK) {
                return FALSE;
        }
 
+       hide_cursor(gh);
+
        /* Figure out if we're holding shift or not. */
        if (! (state & GDK_SHIFT_MASK)) {
                gh->selecting = FALSE;


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