[ghex: 1/2] Colour autohighlights and impl. more theming




commit 4ad54b7a765bf8de2a7dd8335fa0b4300370f0cd
Author: Logan Rathbone <poprocks gmail com>
Date:   Thu Dec 9 13:27:02 2021 -0500

    Colour autohighlights and impl. more theming

 src/common-macros.h       | 36 +++++++++++++++++++++
 src/findreplace.c         |  7 -----
 src/ghex.css              | 18 +++++++++++
 src/ghex.gresource.xml.in |  3 ++
 src/gtkhex.c              | 80 +++++++++++++++++++++++++----------------------
 src/gtkhex.h              |  7 ++---
 src/paste-special.c       |  6 ----
 src/paste-special.h       |  1 +
 src/preferences.c         | 16 ----------
 src/preferences.h         |  1 +
 10 files changed, 104 insertions(+), 71 deletions(-)
---
diff --git a/src/common-macros.h b/src/common-macros.h
new file mode 100644
index 0000000..1d9f34d
--- /dev/null
+++ b/src/common-macros.h
@@ -0,0 +1,36 @@
+/* vim: colorcolumn=80 ts=4 sw=4
+ */
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
+/* common-macros.h - Common macros for GHex
+
+   Copyright © 2021 Logan Rathbone <poprocks gmail com>
+
+   GHex is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   GHex is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GHex; see the file COPYING.
+   If not, write to the Free Software Foundation, Inc.,
+   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+   Original GHex Author: Jaka Mocnik <jaka gnu org>
+*/
+
+#define GET_WIDGET(X)                                                                                        
                  \
+       X = GTK_WIDGET(gtk_builder_get_object (builder, #X));                                   \
+       g_assert (GTK_IS_WIDGET (X));
+
+#define APPLY_PROVIDER_TO(PROVIDER, WIDGET)                                                                  
  \
+{                                                                                                            
                                          \
+       GtkStyleContext *_context;                                                                            
                  \
+       _context = gtk_widget_get_style_context (GTK_WIDGET(WIDGET));                   \
+       gtk_style_context_add_provider (_context, GTK_STYLE_PROVIDER(PROVIDER), \
+                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);                                             
  \
+}
diff --git a/src/findreplace.c b/src/findreplace.c
index 8fa0e26..7bb5a09 100644
--- a/src/findreplace.c
+++ b/src/findreplace.c
@@ -212,10 +212,6 @@ find_next_cb (GtkButton *button, gpointer user_data)
        priv->auto_highlight = NULL;
        priv->auto_highlight = gtk_hex_insert_autohighlight(priv->gh,
                        str, str_len);
-       /* FIXME - due to the restructuring we lost our ability to add custom
-        * colour - maybe there's a way to replicate this with states. :(
-                       , "red");
-       */
 
        /* Search for requested string */
 
@@ -289,9 +285,6 @@ find_prev_cb (GtkButton *button, gpointer user_data)
        priv->auto_highlight = NULL;
        priv->auto_highlight = gtk_hex_insert_autohighlight(priv->gh,
                        str, str_len);
-       /* FIXME - restore our purdy colours
-                       , "red");
-       */
 
        /* Search for requested string */
        
diff --git a/src/ghex.css b/src/ghex.css
new file mode 100644
index 0000000..78364f4
--- /dev/null
+++ b/src/ghex.css
@@ -0,0 +1,18 @@
+hex {
+       font-family: Monospace;
+       font-size: 12pt;
+       padding-left: 12px;
+       padding-right: 12px;
+}
+
+.view:link {
+       background-color: alpha(red, 0.5);
+}
+
+#offsets {
+       background-color: shade(@theme_bg_color, 0.95);
+}
+
+#hex-display {
+       border-right: dotted 2px @borders;
+}
diff --git a/src/ghex.gresource.xml.in b/src/ghex.gresource.xml.in
index 8153bbb..232a4f0 100644
--- a/src/ghex.gresource.xml.in
+++ b/src/ghex.gresource.xml.in
@@ -30,6 +30,9 @@
                <file preprocess="xml-stripblanks" compressed="true">preferences.ui</file>
                <file preprocess="xml-stripblanks" compressed="true">paste-special.ui</file>
        </gresource>
+       <gresource prefix="@resource_base_path@/css">
+               <file>ghex.css</file>
+       </gresource>
        <gresource prefix="@resource_base_path@/gtk">
                <file preprocess="xml-stripblanks">help-overlay.ui</file>
        </gresource>
diff --git a/src/gtkhex.c b/src/gtkhex.c
index 7ebd8fe..c145cb5 100644
--- a/src/gtkhex.c
+++ b/src/gtkhex.c
@@ -32,6 +32,7 @@
 
 #include "gtkhex.h"
 #include "gtkhex-layout-manager.h"
+#include "common-macros.h"
 
 #include <string.h>
 
@@ -43,7 +44,6 @@
 
 /* DEFINES */
 
-#define CSS_NAME "hex"
 /* default minimum drawing area size (for ascii and hex widgets) in pixels. */
 #define DEFAULT_DA_SIZE 50
 /* default characters per line (cpl) */
@@ -87,10 +87,10 @@ struct _GtkHex_Highlight
 {
        int start, end;
        int start_line, end_line;
-       GdkRGBA *bg_color; /* NULL to use the style color */
 
-       GtkHex_Highlight *prev, *next;
        gboolean valid;
+
+       GtkHex_Highlight *prev, *next;
 };
 
 /* used to automatically highlight all visible occurrences
@@ -123,6 +123,8 @@ struct _GtkHex
 
        GtkLayoutManager *layout_manager;
 
+       GtkCssProvider *provider;
+
        GtkWidget *xdisp, *adisp;       /* DrawingArea */
        GtkWidget *offsets;                     /* DrawingArea */
        GtkWidget *scrollbar;
@@ -699,9 +701,11 @@ render_highlights (GtkHex *gh,
        GtkWidget *widget;              /* shorthand for the hex or ascii drawing area */
        PangoLayout *layout;    /* shorthand for the hex or ascii pango layout */
        GtkStyleContext *context;
+       GtkStateFlags state;
        int hex_cpl;
        cairo_region_t *region;
        int y;
+       gboolean is_autohighlight = FALSE;
 
        if (type == VIEW_HEX)
        {
@@ -717,12 +721,6 @@ render_highlights (GtkHex *gh,
        hex_cpl = gtk_hex_layout_get_hex_cpl (GTK_HEX_LAYOUT(gh->layout_manager));
        y = cursor_line * gh->char_height;
 
-       context = gtk_widget_get_style_context (widget);
-       gtk_style_context_save (context);
-
-       gtk_style_context_set_state (context,
-                       gtk_widget_get_state_flags (widget) | GTK_STATE_FLAG_SELECTED);
-
        while (highlight)
        {
                int cursor_off = 0;
@@ -734,6 +732,17 @@ render_highlights (GtkHex *gh,
                /* Shorthands for readability of this loop */
                int start, end, start_line, end_line;
 
+               context = gtk_widget_get_style_context (widget);
+               gtk_style_context_save (context);
+
+               state = gtk_style_context_get_state (context);
+               if (is_autohighlight)
+                       state |= GTK_STATE_FLAG_LINK;
+               else
+                       state |= GTK_STATE_FLAG_SELECTED;
+
+               gtk_style_context_set_state (context, state);
+
                gtk_hex_validate_highlight (gh, highlight);
 
                start = MIN(highlight->start, highlight->end);
@@ -822,10 +831,10 @@ end_of_loop:
                {
                        highlight = auto_highlight->highlights;
                        auto_highlight = auto_highlight->next;
+                       is_autohighlight = TRUE;
                }
+               gtk_style_context_restore (context);
        }
-       gtk_style_context_restore (context);
-       gtk_widget_queue_draw (GTK_WIDGET(gh));
 }
 
 /* FIXME - Previously, this function was more sophisticated, and only
@@ -1836,7 +1845,6 @@ gtk_hex_insert_highlight (GtkHex *gh,
                GtkHex_AutoHighlight *ahl,
                int start, int end)
 {
-       GdkRGBA rgba;
        int file_size;
 
        g_return_val_if_fail (HEX_IS_DOCUMENT (gh->document), NULL);
@@ -2262,7 +2270,7 @@ gtk_hex_class_init (GtkHexClass *klass)
 
        /* CSS name */
 
-       gtk_widget_class_set_css_name (widget_class, CSS_NAME);
+       gtk_widget_class_set_css_name (widget_class, "hex");
 
        /* SIGNALS */
 
@@ -2422,7 +2430,6 @@ gtk_hex_init (GtkHex *gh)
 
        GtkHexLayoutChild *child_info;
 
-       GtkCssProvider *provider;
        GtkStyleContext *context;
 
        GtkBuilder *builder;
@@ -2453,7 +2460,6 @@ gtk_hex_init (GtkHex *gh)
        gh->selecting = FALSE;
 
        gh->selection.start = gh->selection.end = 0;
-       gh->selection.bg_color = NULL;
        gh->selection.next = gh->selection.prev = NULL;
        gh->selection.valid = FALSE;
 
@@ -2464,23 +2470,15 @@ gtk_hex_init (GtkHex *gh)
 
        /* Init CSS */
 
-       /* Set context var to the widget's context at large. */
-       context = gtk_widget_get_style_context (widget);
+       context = gtk_widget_get_style_context (GTK_WIDGET (widget));
+
+       gh->provider = gtk_css_provider_new ();
+       gtk_css_provider_load_from_resource (GTK_CSS_PROVIDER (gh->provider),
+               RESOURCE_BASE_PATH "/css/ghex.css");
 
-       /* set up a provider so we can feed CSS through C code. */
-       provider = gtk_css_provider_new ();
-       gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (provider),
-                                                                       CSS_NAME " {\n"
-                                                                        "   font-family: Monospace;\n"
-                                                                        "   font-size: 12pt;\n"
-                                        "   padding-left: 12px;\n"
-                                        "   padding-right: 12px;\n"
-                                        "}\n", -1);
-
-       /* add the provider to our widget's style context. */
        gtk_style_context_add_provider (context,
-                                       GTK_STYLE_PROVIDER (provider),
-                                       GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
+                                       GTK_STYLE_PROVIDER (gh->provider),
+                                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
 
        /* Setup offsets widget. */
 
@@ -2498,8 +2496,8 @@ gtk_hex_init (GtkHex *gh)
                        gh,
                        NULL);          /* GDestroyNotify destroy); */
 
-       context = gtk_widget_get_style_context (GTK_WIDGET (gh->offsets));
-       gtk_style_context_add_class (context, "header");
+       gtk_widget_set_name (gh->offsets, "offsets");
+       APPLY_PROVIDER_TO (gh->provider, gh->offsets);
 
        /* hide it by default. */
        gtk_widget_hide (gh->offsets);
@@ -2521,13 +2519,17 @@ gtk_hex_init (GtkHex *gh)
                        gh,
                        NULL);          /* GDestroyNotify destroy); */
 
-       /* Set context var to hex widget's context */
        context = gtk_widget_get_style_context (GTK_WIDGET (gh->xdisp));
-       /* ... and add view class so we get certain theme colours for free. */
+
+       /* Add view class so we get certain theme colours for free. */
        gtk_style_context_add_class (context, "view");
+
        gtk_style_context_add_provider (context,
-                                       GTK_STYLE_PROVIDER (provider),
+                                       GTK_STYLE_PROVIDER (gh->provider),
                                        GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+
+       gtk_widget_set_name (gh->xdisp, "hex-display");
+
        /* Setup our ASCII widget. */
 
        gh->adisp = gtk_drawing_area_new();
@@ -2548,9 +2550,11 @@ gtk_hex_init (GtkHex *gh)
        context = gtk_widget_get_style_context (GTK_WIDGET (gh->adisp));
        gtk_style_context_add_class (context, "view");
        gtk_style_context_add_provider (context,
-                                       GTK_STYLE_PROVIDER (provider),
+                                       GTK_STYLE_PROVIDER (gh->provider),
                                        GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
 
+       gtk_widget_set_name (gh->adisp, "ascii-display");
+
        /* Set a minimum size for hex/ascii drawing areas. */
 
        gtk_widget_set_size_request (gh->adisp,
@@ -3055,7 +3059,7 @@ gtk_hex_set_insert_mode (GtkHex *gh, gboolean insert)
 }
 
 GtkHex_AutoHighlight *
-gtk_hex_insert_autohighlight(GtkHex *gh,
+gtk_hex_insert_autohighlight (GtkHex *gh,
                const char *search,
                int len)
 {
@@ -3074,7 +3078,7 @@ gtk_hex_insert_autohighlight(GtkHex *gh,
        new->view_min = 0;
        new->view_max = 0;
 
-       gtk_hex_update_auto_highlight(gh, new, FALSE, TRUE);
+       gtk_hex_update_auto_highlight (gh, new, FALSE, TRUE);
 
        return new;
 }
diff --git a/src/gtkhex.h b/src/gtkhex.h
index 962af2a..2d555b3 100644
--- a/src/gtkhex.h
+++ b/src/gtkhex.h
@@ -89,10 +89,9 @@ gboolean gtk_hex_get_selection(GtkHex *gh, gint *start, gint *end);
 void gtk_hex_clear_selection(GtkHex *gh);
 void gtk_hex_delete_selection(GtkHex *gh);
 
-GtkHex_AutoHighlight *gtk_hex_insert_autohighlight(GtkHex *gh,
-               const gchar *search,
-               gint len);
-void gtk_hex_delete_autohighlight(GtkHex *gh, GtkHex_AutoHighlight *ahl);
+GtkHex_AutoHighlight *
+gtk_hex_insert_autohighlight (GtkHex *gh, const char *search, int len);
+void gtk_hex_delete_autohighlight (GtkHex *gh, GtkHex_AutoHighlight *ahl);
 
 GtkAdjustment *gtk_hex_get_adjustment(GtkHex *gh);
 HexDocument *gtk_hex_get_document (GtkHex *gh);
diff --git a/src/paste-special.c b/src/paste-special.c
index 020003a..030b537 100644
--- a/src/paste-special.c
+++ b/src/paste-special.c
@@ -31,12 +31,6 @@
 
 #define PASTE_SPECIAL_RESOURCE         RESOURCE_BASE_PATH "/paste-special.ui"
 
-/* MACROS */
-
-#define GET_WIDGET(X) \
-       X = GTK_WIDGET(gtk_builder_get_object (builder, #X)); \
-       g_assert (GTK_IS_WIDGET (X))
-
 /* ENUMS AND DATATYPES */
 
 #define HEX_PASTE_ERROR hex_paste_error_quark ()
diff --git a/src/paste-special.h b/src/paste-special.h
index 30bc57b..c0d28ca 100644
--- a/src/paste-special.h
+++ b/src/paste-special.h
@@ -33,6 +33,7 @@
 #include "gtkhex-paste-data.h"
 #include "ghex-application-window.h"
 #include "common-ui.h"
+#include "common-macros.h"
 
 G_BEGIN_DECLS
 
diff --git a/src/preferences.c b/src/preferences.c
index 7e400b7..a4e68ac 100644
--- a/src/preferences.c
+++ b/src/preferences.c
@@ -39,12 +39,6 @@
 #define SHADED_BOX_MAX         CONFIG_H_SHADED_BOX_MAX
 #define PREFS_RESOURCE         RESOURCE_BASE_PATH "/preferences.ui"
 
-/* MACROS */
-
-#define GET_WIDGET(X) \
-       X = GTK_WIDGET(gtk_builder_get_object (builder, #X)); \
-       g_assert (GTK_IS_WIDGET (X))
-
 /* PRIVATE DATATYPES */
 
 /* The types of fonts that can be set via font choosers. I suppose we could
@@ -90,20 +84,11 @@ static GtkWidget *system_default_chkbtn;
 static GtkWidget *close_button;
 static GtkWidget *help_button;
 
-/* PRIVATE DECLARATIONS */
-
-
-
 /* PRIVATE FUNCTIONS */
 
-#define APPLY_PROVIDER_TO(PROVIDER, WIDGET)                                                                  
  \
-       context = gtk_widget_get_style_context (WIDGET);                                                \
-       gtk_style_context_add_provider (context, GTK_STYLE_PROVIDER(PROVIDER),  \
-                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION)
 static void
 do_css_stuff(void)
 {
-       GtkStyleContext *context;
        GtkCssProvider *box_provider, *frame_provider;
 
        /* Grab layout-oriented widgets and set CSS styling. */
@@ -134,7 +119,6 @@ do_css_stuff(void)
        APPLY_PROVIDER_TO (frame_provider, group_type_frame);
        APPLY_PROVIDER_TO (frame_provider, print_font_frame);
 }
-#undef APPLY_PROVIDER_TO
 
 static void
 help_clicked_cb (GtkButton *button,
diff --git a/src/preferences.h b/src/preferences.h
index de598ad..8510a64 100644
--- a/src/preferences.h
+++ b/src/preferences.h
@@ -41,6 +41,7 @@
 
 #include "configuration.h"
 #include "common-ui.h"
+#include "common-macros.h"
 
 G_BEGIN_DECLS
 


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