[ghex/gtk4-port: 28/91] Remove conversions-pane; port hex-dialog to gtk4
- From: Logan Rathbone <larathbone src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [ghex/gtk4-port: 28/91] Remove conversions-pane; port hex-dialog to gtk4
- Date: Thu, 12 Aug 2021 23:35:09 +0000 (UTC)
commit 42d85b3242861d8040fb0175dbf7f827d5303a5b
Author: Logan Rathbone <poprocks gmail com>
Date: Tue Jan 12 23:47:11 2021 -0500
Remove conversions-pane; port hex-dialog to gtk4
Sorry, conversions-pane, we hardly knew ye.
The code in (the unfortunately named) hex-dialog (which is not a dialog
at all) was just too clever.
An array of structs of function pointers? Count me in!
src/Makefile | 2 +-
src/conversions-pane.c | 122 -------------
src/conversions-pane.h | 15 --
src/conversions-pane.ui | 394 ------------------------------------------
src/ghex-application-window.c | 38 +++-
src/hex-dialog.c | 156 ++++++++---------
src/hex-dialog.h | 41 ++---
7 files changed, 116 insertions(+), 652 deletions(-)
---
diff --git a/src/Makefile b/src/Makefile
index 3462c81d..670c9510 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 conversions-pane.o resources.o
+STUB: gtkhex.o hex-document.o ghex-application-window.o hex-dialog.o resources.o
compile-resources:
glib-compile-resources ghex.gresource.xml --target=resources.c --generate-source
diff --git a/src/ghex-application-window.c b/src/ghex-application-window.c
index ef04d8d4..26a11991 100644
--- a/src/ghex-application-window.c
+++ b/src/ghex-application-window.c
@@ -3,13 +3,14 @@
#include <gtkhex.h>
#include "ghex-application-window.h"
-#include "conversions-pane.h"
+#include "hex-dialog.h"
struct _GHexApplicationWindow
{
GtkApplicationWindow parent_instance;
- GtkWidget *conversions_pane;
+ HexDialog *dialog;
+ GtkWidget *dialog_widget;
guint statusbar_id;
GtkAdjustment *adj;
/*
@@ -28,6 +29,27 @@ struct _GHexApplicationWindow
G_DEFINE_TYPE (GHexApplicationWindow, ghex_application_window,
GTK_TYPE_APPLICATION_WINDOW)
+/* CALLBACKS */
+
+static void
+cursor_moved_cb(GtkHex *gtkhex, gpointer user_data)
+{
+ GHexApplicationWindow *self = GHEX_APPLICATION_WINDOW(user_data);
+ int current_pos;
+ HexDialogVal64 val;
+
+ current_pos = gtk_hex_get_cursor(gtkhex);
+// ghex_window_update_status_message(self);
+
+ for (int i = 0; i < 8; i++)
+ {
+ /* returns 0 on buffer overflow, which is what we want */
+ val.v[i] = gtk_hex_get_byte(gtkhex, current_pos+i);
+ }
+ hex_dialog_updateview (self->dialog, &val);
+}
+
+/* --- */
static void
toggle_conversions (GtkWidget *widget,
@@ -69,8 +91,10 @@ ghex_application_window_init(GHexApplicationWindow *self)
gtk_widget_init_template (widget);
/* Setup conversions box and pane */
- self->conversions_pane = conversions_pane_new ();
- gtk_box_append (GTK_BOX(self->conversions_box), self->conversions_pane);
+ self->dialog = hex_dialog_new ();
+ self->dialog_widget = hex_dialog_getview (self->dialog);
+
+ gtk_box_append (GTK_BOX(self->conversions_box), self->dialog_widget);
gtk_widget_set_visible (self->conversions_box, FALSE);
/* CSS - conversions_box */
@@ -88,8 +112,6 @@ ghex_application_window_init(GHexApplicationWindow *self)
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
-
-
// TEST
set_statusbar(self, "Offset: 0x0");
}
@@ -161,6 +183,10 @@ ghex_application_window_add_hex(GHexApplicationWindow *self, GtkHex *gh)
gtk_box_prepend (GTK_BOX(self->child_box), GTK_WIDGET(gh));
+ /* Setup signals */
+ g_signal_connect(G_OBJECT(gh), "cursor-moved",
+ G_CALLBACK(cursor_moved_cb), self);
+
/* Setup scrollbar */
self->adj = gtk_hex_get_adjustment(gh);
gtk_scrollbar_set_adjustment (GTK_SCROLLBAR(self->scrollbar), self->adj);
diff --git a/src/hex-dialog.c b/src/hex-dialog.c
index 8d2d0a5b..29b31f58 100644
--- a/src/hex-dialog.c
+++ b/src/hex-dialog.c
@@ -33,26 +33,33 @@
#include "gtkhex.h"
#include "hex-dialog.h"
-static void hex_dialog_class_init (HexDialogClass *);
-static void hex_dialog_init (HexDialog *);
+struct _HexDialog
+{
+ GObject gobject;
-void hex_dialog_update_entry_sizes(HexDialog *dialog);
-void hex_dialog_updateview(HexDialog *dialog, HexDialogVal64 *val);
+ GtkWidget *entry[ENTRY_MAX];
+ GtkWidget *config_endian;
+ GtkWidget *config_hex;
+ HexConversionProperties properties;
+ HexDialogVal64 val;
+};
+
+static void hex_dialog_update_entry_sizes(HexDialog *dialog);
/* conversion functions */
-char *HexConvert_S8(HexDialogVal64 *val, HexConversionProperties *prop);
-char *HexConvert_US8(HexDialogVal64 *val, HexConversionProperties *prop);
-char *HexConvert_S16(HexDialogVal64 *val, HexConversionProperties *prop);
-char *HexConvert_US16(HexDialogVal64 *val, HexConversionProperties *prop);
-char *HexConvert_S32(HexDialogVal64 *val, HexConversionProperties *prop);
-char *HexConvert_US32(HexDialogVal64 *val, HexConversionProperties *prop);
-char *HexConvert_S64(HexDialogVal64 *val, HexConversionProperties *prop);
-char *HexConvert_US64(HexDialogVal64 *val, HexConversionProperties *prop);
-char *HexConvert_32float(HexDialogVal64 *val, HexConversionProperties *prop);
-char *HexConvert_64float(HexDialogVal64 *val, HexConversionProperties *prop);
-char *HexConvert_hex(HexDialogVal64 *val, HexConversionProperties *prop);
-char *HexConvert_oct(HexDialogVal64 *val, HexConversionProperties *prop);
-char *HexConvert_bin(HexDialogVal64 *val, HexConversionProperties *prop);
+static char *HexConvert_S8(HexDialogVal64 *val, HexConversionProperties *prop);
+static char *HexConvert_US8(HexDialogVal64 *val, HexConversionProperties *prop);
+static char *HexConvert_S16(HexDialogVal64 *val, HexConversionProperties *prop);
+static char *HexConvert_US16(HexDialogVal64 *val, HexConversionProperties *prop);
+static char *HexConvert_S32(HexDialogVal64 *val, HexConversionProperties *prop);
+static char *HexConvert_US32(HexDialogVal64 *val, HexConversionProperties *prop);
+static char *HexConvert_S64(HexDialogVal64 *val, HexConversionProperties *prop);
+static char *HexConvert_US64(HexDialogVal64 *val, HexConversionProperties *prop);
+static char *HexConvert_32float(HexDialogVal64 *val, HexConversionProperties *prop);
+static char *HexConvert_64float(HexDialogVal64 *val, HexConversionProperties *prop);
+static char *HexConvert_hex(HexDialogVal64 *val, HexConversionProperties *prop);
+static char *HexConvert_oct(HexDialogVal64 *val, HexConversionProperties *prop);
+static char *HexConvert_bin(HexDialogVal64 *val, HexConversionProperties *prop);
static struct {
char *name;
@@ -73,64 +80,44 @@ static struct {
{ N_("Binary:"), HexConvert_bin }
};
+G_DEFINE_TYPE (HexDialog, hex_dialog, G_TYPE_OBJECT)
-
-GType hex_dialog_get_type (void)
-{
- static GType doc_type = 0;
-
- if (doc_type == 0)
- {
- static const GTypeInfo doc_info =
- {
- sizeof (HexDialogClass),
- NULL, /* base_init */
- NULL, /* base_finalize */
- (GClassInitFunc) hex_dialog_class_init,
- NULL, /* class_finalize */
- NULL, /* class_data */
- sizeof (HexDialog),
- 0,
- (GInstanceInitFunc) hex_dialog_init
- };
-
- doc_type = g_type_register_static (G_TYPE_OBJECT,
- "HexDialog",
- &doc_info,
- 0);
- }
- return doc_type;
-}
-
-static void hex_dialog_init (HexDialog *dialog)
+static void
+hex_dialog_init (HexDialog *dialog)
{
int i;
+
for (i = 0; i < ENTRY_MAX; i++)
dialog->entry[i] = NULL;
+
dialog->config_endian = NULL;
dialog->config_hex = NULL;
dialog->properties.endian = LITTLE;
dialog->properties.hexHint = FALSE;
dialog->properties.streamBitsHint = 8;
+
for (i = 0; i < 8; i++)
dialog->val.v[i] = 0;
}
-static void hex_dialog_class_init (HexDialogClass *klass)
+static void
+hex_dialog_class_init (HexDialogClass *klass)
{
}
-HexDialog *hex_dialog_new (void)
+HexDialog *
+hex_dialog_new (void)
{
HexDialog *dialog;
- dialog = HEX_DIALOG(g_object_new(HEX_DIALOG_TYPE, NULL));
+ dialog = HEX_DIALOG(g_object_new(HEX_TYPE_DIALOG, NULL));
g_return_val_if_fail (dialog != NULL, NULL);
return dialog;
}
-static void create_dialog_prop(HexDialogEntryTypes type,
+static void
+create_dialog_prop(HexDialogEntryTypes type,
HexDialog *dialog,
GtkWidget *grid,
gint xpos, gint ypos)
@@ -138,7 +125,6 @@ static void create_dialog_prop(HexDialogEntryTypes type,
GtkWidget *label;
label = gtk_label_new(_(HexDialogEntries[type].name));
- gtk_misc_set_alignment (GTK_MISC(label), 1.0, 0.5);
gtk_widget_set_hexpand (label, TRUE);
gtk_grid_attach (GTK_GRID (grid), label,
xpos, ypos, 1, 1);
@@ -152,16 +138,18 @@ static void create_dialog_prop(HexDialogEntryTypes type,
gtk_widget_show(dialog->entry[type]);
}
-static void config_toggled_cb(GtkToggleButton *togglebutton, gpointer user_data)
+static void
+config_toggled_cb(GtkToggleButton *togglebutton, gpointer user_data)
{
HexDialog *dialog = HEX_DIALOG(user_data);
- dialog->properties.endian = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->config_endian)) ?
+ dialog->properties.endian = gtk_check_button_get_active(GTK_CHECK_BUTTON(dialog->config_endian)) ?
LITTLE : BIG;
- dialog->properties.hexHint = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->config_hex));
+ dialog->properties.hexHint = gtk_check_button_get_active(GTK_CHECK_BUTTON(dialog->config_hex));
hex_dialog_updateview(dialog, NULL);
}
-static void config_spinchange_cb(GtkSpinButton *spinbutton, gpointer user_data)
+static void
+config_spinchange_cb(GtkSpinButton *spinbutton, gpointer user_data)
{
HexDialog *dialog = HEX_DIALOG(user_data);
dialog->properties.streamBitsHint = (guchar)gtk_spin_button_get_value(spinbutton);
@@ -170,7 +158,6 @@ static void config_spinchange_cb(GtkSpinButton *spinbutton, gpointer user_data)
GtkWidget *hex_dialog_getview(HexDialog *dialog)
{
-
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *grid;
@@ -185,7 +172,7 @@ GtkWidget *hex_dialog_getview(HexDialog *dialog)
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_widget_set_vexpand (GTK_WIDGET (vbox), FALSE);
- gtk_box_pack_start (GTK_BOX (vbox), grid, TRUE, FALSE, 4);
+ gtk_box_append (GTK_BOX (vbox), grid);
gtk_widget_show (vbox);
create_dialog_prop (S8, dialog, grid, 0, 0);
@@ -208,24 +195,23 @@ GtkWidget *hex_dialog_getview(HexDialog *dialog)
hex_dialog_update_entry_sizes (dialog);
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
- gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 4);
+ gtk_box_append(GTK_BOX(vbox), hbox);
gtk_widget_show(hbox);
dialog->config_endian = gtk_check_button_new_with_label(_("Show little endian decoding"));
- gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->config_endian), TRUE);
+ gtk_check_button_set_active(GTK_CHECK_BUTTON(dialog->config_endian), TRUE);
g_signal_connect(G_OBJECT(dialog->config_endian), "toggled",
G_CALLBACK(config_toggled_cb), dialog);
gtk_widget_show(dialog->config_endian);
- gtk_box_pack_start(GTK_BOX(hbox), dialog->config_endian, TRUE, FALSE, 4);
+ gtk_box_append(GTK_BOX(hbox), dialog->config_endian);
dialog->config_hex = gtk_check_button_new_with_label(_("Show unsigned and float as hexadecimal"));
g_signal_connect(G_OBJECT(dialog->config_hex), "toggled",
G_CALLBACK(config_toggled_cb), dialog);
gtk_widget_show(dialog->config_hex);
- gtk_box_pack_start(GTK_BOX(hbox), dialog->config_hex, TRUE, FALSE, 4);
+ gtk_box_append(GTK_BOX(hbox), dialog->config_hex);
label = gtk_label_new(_("Stream Length:"));
- gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
gtk_grid_attach (GTK_GRID (grid), label, 4, 3, 1, 1);
gtk_widget_show(label);
@@ -239,7 +225,8 @@ GtkWidget *hex_dialog_getview(HexDialog *dialog)
return vbox;
}
-static char *dialog_prop_get_text(HexDialogEntryTypes type,
+static char *
+dialog_prop_get_text(HexDialogEntryTypes type,
HexDialog *dialog,
HexDialogVal64 *val)
{
@@ -256,12 +243,17 @@ static void update_dialog_prop(HexDialogEntryTypes type,
HexDialogVal64 *val)
{
char *buf;
+ GtkEntryBuffer *eb;
+
buf = dialog_prop_get_text (type, dialog, val);
- gtk_entry_set_text (GTK_ENTRY (dialog->entry[type]), buf);
+ eb = gtk_entry_get_buffer (GTK_ENTRY(dialog->entry[type]));
+
+ gtk_entry_buffer_set_text (eb, buf, -1);
}
/* Try to guess the maximum width needed for each entry */
-void hex_dialog_update_entry_sizes(HexDialog *dialog)
+static void
+hex_dialog_update_entry_sizes(HexDialog *dialog)
{
HexDialogVal64 val;
gint i;
@@ -273,7 +265,7 @@ void hex_dialog_update_entry_sizes(HexDialog *dialog)
for (i = 0; i < ENTRY_MAX; i++)
{
width_chars = strlen (dialog_prop_get_text (i, dialog, &val));
- gtk_entry_set_width_chars (GTK_ENTRY (dialog->entry[i]), width_chars);
+ gtk_entry_set_max_length (GTK_ENTRY (dialog->entry[i]), width_chars);
}
}
@@ -309,7 +301,7 @@ static char convbuffer[CONV_BUFSIZE];
* also makes it endian safe (i think)
*/
-static unsigned int pow2(int p)
+inline static unsigned int pow2(int p)
{
unsigned int i = 0, r = 1;
for (i = 0; i < p; i++)
@@ -317,7 +309,7 @@ static unsigned int pow2(int p)
return r;
}
-static unsigned long long int llpow2(int p)
+inline static unsigned long long int llpow2(int p)
{
unsigned int i = 0;
unsigned long long int r = 1;
@@ -326,7 +318,7 @@ static unsigned long long int llpow2(int p)
return r;
}
-char *HexConvert_S8(HexDialogVal64 *val, HexConversionProperties *prop)
+static char *HexConvert_S8(HexDialogVal64 *val, HexConversionProperties *prop)
{
int i, local = 0;
for (i = 0; i < 7; i++)
@@ -337,7 +329,7 @@ char *HexConvert_S8(HexDialogVal64 *val, HexConversionProperties *prop)
return convbuffer;
}
-char *HexConvert_US8(HexDialogVal64 *val, HexConversionProperties *prop)
+static char *HexConvert_US8(HexDialogVal64 *val, HexConversionProperties *prop)
{
int i, local = 0;
for (i = 0; i < 8; i++)
@@ -350,7 +342,7 @@ char *HexConvert_US8(HexDialogVal64 *val, HexConversionProperties *prop)
return convbuffer;
}
-char *HexConvert_S16(HexDialogVal64 *val, HexConversionProperties *prop)
+static char *HexConvert_S16(HexDialogVal64 *val, HexConversionProperties *prop)
{
guchar in[2];
int i, local = 0;
@@ -374,7 +366,7 @@ char *HexConvert_S16(HexDialogVal64 *val, HexConversionProperties *prop)
return convbuffer;
}
-char *HexConvert_US16(HexDialogVal64 *val, HexConversionProperties *prop)
+static char *HexConvert_US16(HexDialogVal64 *val, HexConversionProperties *prop)
{
guchar in[2];
int i, local = 0;
@@ -401,7 +393,7 @@ char *HexConvert_US16(HexDialogVal64 *val, HexConversionProperties *prop)
return convbuffer;
}
-char *HexConvert_S32(HexDialogVal64 *val, HexConversionProperties *prop)
+static char *HexConvert_S32(HexDialogVal64 *val, HexConversionProperties *prop)
{
guchar in[4];
int i, local = 0;
@@ -433,7 +425,7 @@ char *HexConvert_S32(HexDialogVal64 *val, HexConversionProperties *prop)
return convbuffer;
}
-char *HexConvert_US32(HexDialogVal64 *val, HexConversionProperties *prop)
+static char *HexConvert_US32(HexDialogVal64 *val, HexConversionProperties *prop)
{
guchar in[4];
unsigned int i, local = 0;
@@ -467,7 +459,7 @@ char *HexConvert_US32(HexDialogVal64 *val, HexConversionProperties *prop)
return convbuffer;
}
-char *HexConvert_S64(HexDialogVal64 *val, HexConversionProperties *prop)
+static char *HexConvert_S64(HexDialogVal64 *val, HexConversionProperties *prop)
{
guchar in[8];
long long i, local = 0;
@@ -515,7 +507,7 @@ char *HexConvert_S64(HexDialogVal64 *val, HexConversionProperties *prop)
return convbuffer;
}
-char *HexConvert_US64(HexDialogVal64 *val, HexConversionProperties *prop)
+static char *HexConvert_US64(HexDialogVal64 *val, HexConversionProperties *prop)
{
guchar in[8];
long long unsigned i, local = 0;
@@ -568,7 +560,7 @@ char *HexConvert_US64(HexDialogVal64 *val, HexConversionProperties *prop)
/* for floats we just cast them, can't be bothered
* interpretting them properly
*/
-char *HexConvert_32float(HexDialogVal64 *val, HexConversionProperties *prop)
+static char *HexConvert_32float(HexDialogVal64 *val, HexConversionProperties *prop)
{
union
{
@@ -606,7 +598,7 @@ char *HexConvert_32float(HexDialogVal64 *val, HexConversionProperties *prop)
return convbuffer;
}
-char *HexConvert_64float(HexDialogVal64 *val, HexConversionProperties *prop)
+static char *HexConvert_64float(HexDialogVal64 *val, HexConversionProperties *prop)
{
union
{
@@ -653,7 +645,7 @@ char *HexConvert_64float(HexDialogVal64 *val, HexConversionProperties *prop)
}
/* these three care not for endianness, they take the input as a stream */
-char *HexConvert_hex(HexDialogVal64 *val, HexConversionProperties *prop)
+static char *HexConvert_hex(HexDialogVal64 *val, HexConversionProperties *prop)
{
int i;
int local = 0;
@@ -695,7 +687,7 @@ char *HexConvert_hex(HexDialogVal64 *val, HexConversionProperties *prop)
return convbuffer;
}
-char *HexConvert_oct(HexDialogVal64 *val, HexConversionProperties *prop)
+static char *HexConvert_oct(HexDialogVal64 *val, HexConversionProperties *prop)
{
int i;
int local = 0;
@@ -737,7 +729,7 @@ char *HexConvert_oct(HexDialogVal64 *val, HexConversionProperties *prop)
return convbuffer;
}
-char *HexConvert_bin(HexDialogVal64 *val, HexConversionProperties *prop)
+static char *HexConvert_bin(HexDialogVal64 *val, HexConversionProperties *prop)
{
int i;
@@ -757,5 +749,3 @@ char *HexConvert_bin(HexDialogVal64 *val, HexConversionProperties *prop)
convbuffer[i] = '\0';
return convbuffer;
}
-
-
diff --git a/src/hex-dialog.h b/src/hex-dialog.h
index 91365f9c..14b6456a 100644
--- a/src/hex-dialog.h
+++ b/src/hex-dialog.h
@@ -19,19 +19,10 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef __HEX_DIALOG_H__
-#define __HEX_DIALOG_H__
+#ifndef HEX_DIALOG_H
+#define HEX_DIALOG_H
-#include <glib-object.h>
-
-#define HEX_DIALOG_TYPE (hex_dialog_get_type())
-#define HEX_DIALOG(obj) G_TYPE_CHECK_INSTANCE_CAST (obj, hex_dialog_get_type (), HexDialog)
-#define HEX_DIALOG_CLASS(klass) G_TYPE_CHECK_CLASS_CAST (klass, hex_dialog_get_type (), HexDialogClass)
-#define IS_HEX_DIALOG(obj) G_TYPE_CHECK_INSTANCE_TYPE (obj, hex_dialog_get_type ())
-
-
-typedef struct _HexDialog HexDialog;
-typedef struct _HexDialogClass HexDialogClass;
+#include <gtk/gtk.h>
typedef enum
{
@@ -69,27 +60,15 @@ typedef struct
guchar streamBitsHint;
} HexConversionProperties;
-struct _HexDialog
-{
- GObject gobject;
-
- GtkWidget *entry[ENTRY_MAX];
- GtkWidget *config_endian;
- GtkWidget *config_hex;
- HexConversionProperties properties;
- HexDialogVal64 val;
-};
+#define HEX_TYPE_DIALOG (hex_dialog_get_type ())
+G_DECLARE_FINAL_TYPE (HexDialog, hex_dialog, HEX, DIALOG, GObject)
-struct _HexDialogClass
-{
- GObjectClass parent_class;
-};
-GType hex_dialog_get_type(void);
-HexDialog *hex_dialog_new(void);
-GtkWidget *hex_dialog_getview(HexDialog *);
-void hex_dialog_updateview(HexDialog *dialog, HexDialogVal64 *val);
+/* PUBLIC METHOD DECLARATIONS */
+HexDialog *hex_dialog_new (void);
+GtkWidget *hex_dialog_getview (HexDialog *);
+void hex_dialog_updateview (HexDialog *dialog, HexDialogVal64 *val);
-#endif /* __HEX_DIALOG_H__ */
+#endif /* HEX_DIALOG_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]