[ghex/gtk4-port: 34/91] Quick and dirty port of chartable.




commit 0968026d6c218fafa9f67e570576e5b051bb0607
Author: Logan Rathbone <poprocks gmail com>
Date:   Thu Jan 14 19:54:55 2021 -0500

    Quick and dirty port of chartable.
    
    Needs some work. Committing so I can pick this up on another machine.

 src/Makefile                   |   2 +-
 src/chartable.c                | 106 ++++++++++++++++++++++++++++-------------
 src/chartable.h                |   9 ++--
 src/ghex-application-window.c  |  33 +++++++++++++
 src/ghex-application-window.ui |   2 +-
 src/gtkhex.c                   |   8 ++++
 src/gtkhex.h                   |   1 +
 7 files changed, 123 insertions(+), 38 deletions(-)
---
diff --git a/src/Makefile b/src/Makefile
index 2415ecd2..a5ded9a6 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -10,7 +10,7 @@ CFLAGS=-Wall -Wextra -std=c11 -pedantic \
 
 .PHONY: clean compile-resources
 
-STUB: gtkhex.o hex-document.o ghex-application-window.o hex-dialog.o findreplace.o resources.o
+STUB: gtkhex.o hex-document.o ghex-application-window.o hex-dialog.o findreplace.o chartable.o resources.o
 
 compile-resources:
        glib-compile-resources ghex.gresource.xml --target=resources.c --generate-source
diff --git a/src/chartable.c b/src/chartable.c
index 9306b406..af136d0f 100644
--- a/src/chartable.c
+++ b/src/chartable.c
@@ -1,3 +1,5 @@
+/* vim: colorcolumn=80 ts=4 sw=4
+ */
 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
 /* chartable.c - a window with a character table
 
@@ -31,12 +33,14 @@
 #include <gdk/gdkkeysyms.h>
 #include <glib/gi18n.h>
 
+#include <gtkhex.h>
 #include "chartable.h"
-#include "ghex-window.h"
-#include "ui.h"
+//#include "ghex-window.h"
+//#include "ui.h"
 
 GtkWidget *char_table = NULL;
 static GtkTreeSelection *sel_row = NULL;
+static GtkHex *gh_glob = NULL;
 
 static char *ascii_non_printable_label[] = {
        "NUL",
@@ -76,27 +80,34 @@ static char *ascii_non_printable_label[] = {
 static void
 insert_char(GtkTreeView *treeview, GtkTreeModel *model)
 {
-       GHexWindow *win;
        GtkTreeIter iter;
        GtkTreeSelection *selection;
        GValue value = { 0 };
+       HexDocument *doc;
+
+       g_return_if_fail (GTK_IS_HEX(gh_glob));
+
+       doc = gtk_hex_get_document (gh_glob);
 
        selection = gtk_tree_view_get_selection(treeview);
-       if(!gtk_tree_selection_get_selected(selection, &model, &iter))
+       if (! gtk_tree_selection_get_selected (selection, &model, &iter))
                return;
+
        gtk_tree_model_get_value(model, &iter, 2, &value);
-       if(selection == sel_row) {
-               win = ghex_window_get_active();
-               if(win->gh) {
-                       hex_document_set_byte(win->gh->document, (guchar)atoi(g_value_get_string(&value)), 
win->gh->cursor_pos,
-                                                                 win->gh->insert, TRUE);
-                       gtk_hex_set_cursor(win->gh, win->gh->cursor_pos + 1);
-               }
+       if (selection == sel_row) {
+               hex_document_set_byte (doc,
+                               (guchar)atoi(g_value_get_string(&value)),
+                               gtk_hex_get_cursor (gh_glob),
+                               gtk_hex_get_insert_mode (gh_glob),
+                               TRUE);  // undoable
+
+               gtk_hex_set_cursor (gh_glob, gtk_hex_get_cursor (gh_glob) + 1);
        }
        g_value_unset(&value);
        sel_row = selection;
 }
 
+#if 0
 static gboolean select_chartable_row_cb(GtkTreeView *treeview, GdkEventButton *event, gpointer data)
 {
        GtkTreeModel *model = GTK_TREE_MODEL(data);
@@ -105,14 +116,18 @@ static gboolean select_chartable_row_cb(GtkTreeView *treeview, GdkEventButton *e
                insert_char(treeview, model);
        return FALSE;
 }
+#endif
 
 static void hide_chartable_cb (GtkWidget *widget, GtkWidget *win)
 {
     /* widget may be NULL if called from keypress cb! */
-       ghex_window_sync_char_table_item(NULL, FALSE);
-       gtk_widget_hide(win);
+       // FIXME - commenting out for now to get to build.
+//     ghex_window_sync_char_table_item (NULL, FALSE);
+       gtk_widget_hide (win);
 }
 
+// REWRITE FOR EVENT CONTROLLERS
+#if 0
 static gint char_table_key_press_cb (GtkWindow *w, GdkEventKey *e, gpointer data)
 {
        if (e->keyval == GDK_KEY_Escape) {
@@ -140,12 +155,13 @@ char_table_delete_event_cb(GtkWidget *widget, GdkEventAny *e, gpointer user_data
        gtk_widget_hide(widget);
        return TRUE;
 }
+#endif
 
-GtkWidget *create_char_table()
+GtkWidget *create_char_table(GtkWindow *parent_win, GtkHex *gh)
 {
        static gchar *fmt[] = { NULL, "%02X", "%03d", "%03o" };
        static gchar *titles[] = {  N_("ASCII"), N_("Hex"), N_("Decimal"),
-                                                               N_("Octal"), N_("Binary") };
+               N_("Octal"), N_("Binary") };
        gchar *real_titles[5];
        GtkWidget *ct, *sw, *ctv, *cbtn, *vbox, *hbox, *lbl;
        GtkListStore *store;
@@ -156,19 +172,41 @@ GtkWidget *create_char_table()
        int i, col;
        gchar *label, ascii_printable_label[2], bin_label[9], *row[5];
 
-       ct = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-       g_signal_connect(G_OBJECT(ct), "delete_event",
-                                        G_CALLBACK(char_table_delete_event_cb), NULL);
-       g_signal_connect(G_OBJECT(ct), "key_press_event",
-                                        G_CALLBACK(char_table_key_press_cb), NULL);
+       /* set global GtkHex widget */
+       g_assert (GTK_IS_HEX(gh));
+       gh_glob = gh;
+
+       /* Create our char table window and set as child of parent window,
+        * if requested.
+        */
+       ct = gtk_window_new ();
+
+       if (parent_win) {
+               g_assert (GTK_IS_WINDOW (parent_win));
+
+               gtk_window_set_transient_for (GTK_WINDOW(ct), parent_win);
+       }
+
+       // TODO
+//     g_signal_connect(G_OBJECT(ct), "close-request",
+//                                      G_CALLBACK(char_table_close_request_cb), NULL);
+       // handles esc - possibly rewrite to use Event Controllers. 
+//     g_signal_connect(G_OBJECT(ct), "key_press_event",
+//                                      G_CALLBACK(char_table_key_press_cb), NULL);
+//
        gtk_window_set_title(GTK_WINDOW(ct), _("Character table"));
-       sw = gtk_scrolled_window_new(NULL, NULL);
-       gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+
+       sw = gtk_scrolled_window_new ();
+       gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
+                       GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+
        for(i = 0; i < 5; i++)
                real_titles[i] = _(titles[i]);
+
        store = gtk_list_store_new(5, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, 
G_TYPE_STRING);
        cell_renderer = gtk_cell_renderer_text_new();
        ctv = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
+
        for (i = 0; i < 5; i++) {
                column = gtk_tree_view_column_new_with_attributes (real_titles[i], cell_renderer, "text", i, 
NULL);
                gtk_tree_view_append_column(GTK_TREE_VIEW(ctv), column);
@@ -217,37 +255,41 @@ GtkWidget *create_char_table()
        selection = gtk_tree_view_get_selection(GTK_TREE_VIEW (ctv));
        gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
 
+       // REWRITE
+#if 0
        g_signal_connect(G_OBJECT(ct), "delete-event",
                                         G_CALLBACK(delete_event_cb), ct);
        g_signal_connect(G_OBJECT(ctv), "button_press_event",
                                         G_CALLBACK(select_chartable_row_cb), GTK_TREE_MODEL(store));
        g_signal_connect(G_OBJECT(ctv), "key_press_event",
                                         G_CALLBACK(key_press_cb), GTK_TREE_MODEL(store));
+#endif
+
        gtk_widget_grab_focus(ctv);
 
-       cbtn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
+       cbtn = gtk_button_new_with_mnemonic (_("_Close"));
        gtk_widget_show(cbtn);
        g_signal_connect(G_OBJECT (cbtn), "clicked",
                                        G_CALLBACK(hide_chartable_cb), ct);
 
        lbl = gtk_label_new ("");
-       gtk_widget_show(lbl);
-
        vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 4);
-       gtk_widget_show(vbox);
-
        hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
-       gtk_widget_show(hbox);
 
+       gtk_box_append (GTK_BOX(vbox), sw);
+       gtk_box_append (GTK_BOX(hbox), lbl);
+       gtk_box_append (GTK_BOX(hbox), cbtn);
+       gtk_box_append (GTK_BOX(vbox), hbox);
+
+#if 0
        gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(hbox), lbl, TRUE, TRUE, 4);
        gtk_box_pack_start(GTK_BOX(hbox), cbtn, FALSE, TRUE, 12);
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
+#endif
 
-       gtk_container_add(GTK_CONTAINER(sw), ctv);
-       gtk_container_add(GTK_CONTAINER(ct), vbox);
-       gtk_widget_show(ctv);
-       gtk_widget_show(sw);
+       gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW(sw), ctv);
+       gtk_window_set_child (GTK_WINDOW (ct), vbox);
 
        gtk_widget_set_size_request(ct, 320, 256);
 
diff --git a/src/chartable.h b/src/chartable.h
index a8cd5fc6..3e47927b 100644
--- a/src/chartable.h
+++ b/src/chartable.h
@@ -21,17 +21,18 @@
    Author: Jaka Mocnik <jaka gnu org>
 */
 
-#ifndef __GHEX_CHARTABLE_H__
-#define __GHEX_CHARTABLE_H__
+#ifndef GHEX_CHARTABLE_H
+#define GHEX_CHARTABLE_H
 
 #include <gtk/gtk.h>
 
 G_BEGIN_DECLS
 
-GtkWidget *create_char_table(void);
+GtkWidget *create_char_table (GtkWindow *parent_win, /* can-NULL */
+               GtkHex *gh);
 
 extern GtkWidget *char_table;
 
 G_END_DECLS
 
-#endif /* !__GHEX_CHARTABLE_H__ */
+#endif /* GHEX_CHARTABLE_H */
diff --git a/src/ghex-application-window.c b/src/ghex-application-window.c
index 167bc228..2c787531 100644
--- a/src/ghex-application-window.c
+++ b/src/ghex-application-window.c
@@ -5,6 +5,7 @@
 #include "ghex-application-window.h"
 #include "hex-dialog.h"
 #include "findreplace.h"
+#include "chartable.h"
 
 struct _GHexApplicationWindow
 {
@@ -20,6 +21,7 @@ struct _GHexApplicationWindow
        GtkWidget *find_dialog;
        GtkWidget *replace_dialog;
        GtkWidget *jump_dialog;
+       GtkWidget *chartable;
 
 /*
  * for i in `cat ghex-application-window.ui |grep -i 'id=' |sed -e 's,^\s*,,g' |sed -e 's,.*id=",,' |sed -e 
's,">,,'`; do echo $i >> tmp.txt; done
@@ -62,6 +64,31 @@ cursor_moved_cb(GtkHex *gtkhex, gpointer user_data)
 
 /* ACTIONS */
 
+static void
+show_chartable (GtkWidget *widget,
+               const char *action_name,
+               GVariant *parameter)
+{
+       GHexApplicationWindow *self = GHEX_APPLICATION_WINDOW(widget);
+
+       g_return_if_fail (GTK_IS_HEX(self->gh));
+       g_return_if_fail (GTK_IS_WIDGET(self->find_dialog));
+
+       (void)parameter, (void)action_name;             /* unused */
+
+       if (! self->chartable) {
+               self->chartable = create_char_table (GTK_WINDOW(self), self->gh);
+               gtk_widget_show (self->chartable);
+       }
+       else if (gtk_widget_is_visible (self->chartable)) {
+               gtk_widget_hide (self->chartable);
+       }
+       else {
+               gtk_widget_show (self->chartable);
+       }
+}
+
+
 static void
 show_find_pane (GtkWidget *widget,
                const char *action_name,
@@ -277,6 +304,12 @@ ghex_application_window_class_init(GHexApplicationWindowClass *klass)
        gtk_widget_class_install_action (widget_class, "ghex.jump",
                        NULL,   // GVariant string param_type
                        show_jump_pane);
+
+       gtk_widget_class_install_action (widget_class, "ghex.chartable",
+                       NULL,   // GVariant string param_type
+                       show_chartable);
+
+
        /* 
         * for i in `cat tmp.txt`; do echo "gtk_widget_class_bind_template_child (widget_class, 
GHexApplicationWindow, ${i});"; done
         */
diff --git a/src/ghex-application-window.ui b/src/ghex-application-window.ui
index 9576ac89..5eed23ad 100644
--- a/src/ghex-application-window.ui
+++ b/src/ghex-application-window.ui
@@ -48,7 +48,7 @@
                                <attribute name="label" translatable="yes">_Tools</attribute>
                                <item>
                                        <attribute name="label" translatable="yes">_Character 
Table</attribute>
-                                       <attribute name="action">ghex.char-table</attribute>
+                                       <attribute name="action">ghex.chartable</attribute>
                                </item>
                                <item>
                                        <attribute name="label" translatable="yes">_Base Converter</attribute>
diff --git a/src/gtkhex.c b/src/gtkhex.c
index b68fd853..3ee3cab3 100644
--- a/src/gtkhex.c
+++ b/src/gtkhex.c
@@ -3529,3 +3529,11 @@ gtk_hex_get_document (GtkHex *gh)
 
        return gh->document;
 }
+
+gboolean
+gtk_hex_get_insert_mode (GtkHex *gh)
+{
+       g_assert (GTK_IS_HEX (gh));
+
+       return gh->insert;
+}
diff --git a/src/gtkhex.h b/src/gtkhex.h
index ecc6a003..283a08dc 100644
--- a/src/gtkhex.h
+++ b/src/gtkhex.h
@@ -69,6 +69,7 @@ void gtk_hex_show_offsets(GtkHex *, gboolean);
 void gtk_hex_set_font(GtkHex *, PangoFontMetrics *,
                const PangoFontDescription *);
 
+gboolean gtk_hex_get_insert_mode(GtkHex *gh);
 void gtk_hex_set_insert_mode(GtkHex *, gboolean);
 
 void gtk_hex_set_geometry(GtkHex *gh, gint cpl, gint vis_lines);


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