[ghex] gtkhex: dynamically sized offset column
- From: Logan Rathbone <larathbone src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [ghex] gtkhex: dynamically sized offset column
- Date: Tue, 21 Dec 2021 02:24:23 +0000 (UTC)
commit 4d0792d13b6a239d9e957b6b4d14915f5694c165
Author: Logan Rathbone <poprocks gmail com>
Date: Mon Dec 20 20:32:22 2021 -0500
gtkhex: dynamically sized offset column
src/gtkhex-layout-manager.c | 42 ++++++++++++++++--------------------------
src/gtkhex-layout-manager.h | 3 +++
src/gtkhex.c | 22 ++++++++++++++++++----
3 files changed, 37 insertions(+), 30 deletions(-)
---
diff --git a/src/gtkhex-layout-manager.c b/src/gtkhex-layout-manager.c
index d0adf91..e853d3b 100644
--- a/src/gtkhex-layout-manager.c
+++ b/src/gtkhex-layout-manager.c
@@ -24,10 +24,7 @@
#include "gtkhex-layout-manager.h"
-/* The CPL of the offsets column is constant; easier to just punch it in here
- * than to use config.h. Don't change unless you change code in gtkhex.c as well.
- */
-#define OFFSETS_CPL 8
+#define DEFAULT_OFFSET_CPL 8
struct _GtkHexLayout {
GtkLayoutManager parent_instance;
@@ -37,6 +34,7 @@ struct _GtkHexLayout {
int cpl;
int hex_cpl;
+ int offset_cpl;
int cursor_x, cursor_y;
};
@@ -259,28 +257,7 @@ gtk_hex_layout_allocate (GtkLayoutManager *layout_manager,
break;
case SCROLLBAR_COLUMN: scrollbar = child;
break;
-
case NO_COLUMN:
- {
-#if 0
- GtkRequisition child_req;
- GtkAllocation alloc = {0};
-
- gtk_widget_get_preferred_size (child, &child_req, NULL);
-
- alloc.height = child_req.height;
- alloc.width = child_req.width;
-
- if (GTK_IS_POPOVER (child))
- {
- alloc.x = self->cursor_x;
- alloc.y = self->cursor_y;
- }
-
- gtk_widget_size_allocate (child, &alloc, -1);
- return;
-#endif
- }
break;
/* We won't test for this each loop. */
@@ -306,7 +283,7 @@ gtk_hex_layout_allocate (GtkLayoutManager *layout_manager,
/* nb: offsets always goes at x coordinate 0 so just leave it as it's
* zeroed out anyway. */
- off_alloc.width = OFFSETS_CPL * self->char_width +
+ off_alloc.width = self->offset_cpl * self->char_width +
margins.left + margins.right +
padding.left + padding.right +
borders.left + borders.right;
@@ -467,6 +444,7 @@ gtk_hex_layout_class_init (GtkHexLayoutClass *klass)
static void
gtk_hex_layout_init (GtkHexLayout *self)
{
+ self->offset_cpl = DEFAULT_OFFSET_CPL;
/* FIXME - dumb test initial default */
self->char_width = 20;
self->group_type = GTK_HEX_GROUP_BYTE;
@@ -516,6 +494,18 @@ gtk_hex_layout_set_cursor_pos (GtkHexLayout *layout, int x, int y)
layout->cursor_y = y;
}
+void
+gtk_hex_layout_set_offset_cpl (GtkHexLayout *layout, int offset_cpl)
+{
+ layout->offset_cpl = offset_cpl;
+}
+
+int
+gtk_hex_layout_get_offset_cpl (GtkHexLayout *layout)
+{
+ return layout->offset_cpl;
+}
+
/* GtkHexLayoutChild - Public Methods */
void
diff --git a/src/gtkhex-layout-manager.h b/src/gtkhex-layout-manager.h
index 023955d..df8360c 100644
--- a/src/gtkhex-layout-manager.h
+++ b/src/gtkhex-layout-manager.h
@@ -61,6 +61,9 @@ void gtk_hex_layout_set_group_type (GtkHexLayout *layout,
GtkHexGroupType group_type);
void gtk_hex_layout_set_cursor_pos (GtkHexLayout *layout,
int x, int y);
+void gtk_hex_layout_set_offset_cpl (GtkHexLayout *layout,
+ int offset_cpl);
+int gtk_hex_layout_get_offset_cpl (GtkHexLayout *layout);
G_END_DECLS
diff --git a/src/gtkhex.c b/src/gtkhex.c
index d3e6f61..3b200ce 100644
--- a/src/gtkhex.c
+++ b/src/gtkhex.c
@@ -1058,6 +1058,8 @@ no_more_lines_to_draw:
}
#undef DO_RENDER_CURSOR
+#define BUFLEN 32
+#define MIN_CPL 8
static void
render_offsets (GtkHex *gh,
cairo_t *cr,
@@ -1068,8 +1070,10 @@ render_offsets (GtkHex *gh,
GdkRGBA fg_color;
GtkAllocation allocation;
GtkStyleContext *context;
- /* offset chars (8) + 1 (null terminator) */
- char offstr[9];
+ char buf[BUFLEN] = {0};
+ char fmt[BUFLEN] = {0};
+ char off_str[BUFLEN];
+ int off_cpl;
g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (gh)));
@@ -1087,19 +1091,29 @@ render_offsets (GtkHex *gh,
max_lines = MIN(max_lines, gh->vis_lines);
max_lines = MIN(max_lines, gh->lines - gh->top_line - 1);
+ /* find out how many chars wide our offset column should be based on
+ * how many chars will be in the last offset marker of the screen */
+
+ snprintf (buf, BUFLEN-1, "%lX", (gh->top_line + max_lines - 1) * gh->cpl);
+ off_cpl = MAX (MIN_CPL, strlen (buf));
+ snprintf (fmt, BUFLEN-1, "%%0%dlX", off_cpl);
+ gtk_hex_layout_set_offset_cpl (GTK_HEX_LAYOUT(gh->layout_manager), off_cpl);
+
for (int i = min_lines; i <= max_lines; i++) {
/* generate offset string and place in temporary buffer */
- sprintf(offstr, "%08lX",
+ sprintf(off_str, fmt,
(gh->top_line + i) * gh->cpl + STARTING_OFFSET);
/* build pango layout for offset line; draw line with gtk. */
- pango_layout_set_text (gh->olayout, offstr, 8);
+ pango_layout_set_text (gh->olayout, off_str, off_cpl);
gtk_render_layout (context, cr,
/* x: */ 0,
/* y: */ i * gh->char_height,
gh->olayout);
}
}
+#undef BUFLEN
+#undef MIN_CPL
/* draw_func for the hex drawing area
*/
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]