[ghex] lib: Property-ize some items to make lib more bindable



commit f9b42f9e7db3f4ab441d0646feb2f52fc243c7f0
Author: Logan Rathbone <poprocks gmail com>
Date:   Sun Jun 12 13:42:53 2022 -0400

    lib: Property-ize some items to make lib more bindable

 src/gtkhex.c       | 151 +++++++++++++++++++++++++++++++++++++++--------------
 src/hex-document.c |  88 ++++++++++++++++++++++++++++++-
 2 files changed, 198 insertions(+), 41 deletions(-)
---
diff --git a/src/gtkhex.c b/src/gtkhex.c
index 156ca72..6ddb3b3 100644
--- a/src/gtkhex.c
+++ b/src/gtkhex.c
@@ -67,16 +67,6 @@ typedef enum {
        VIEW_ASCII
 } HexWidgetViewType;
 
-enum {
-       CURSOR_MOVED_SIGNAL,
-       DATA_CHANGED_SIGNAL,
-       CUT_CLIPBOARD_SIGNAL,
-       COPY_CLIPBOARD_SIGNAL,
-       PASTE_CLIPBOARD_SIGNAL,
-       DRAW_COMPLETE_SIGNAL,
-       LAST_SIGNAL
-};
-
 /* highlighting information.
  */
 typedef struct _HexWidget_Highlight HexWidget_Highlight;
@@ -129,6 +119,30 @@ G_DEFINE_BOXED_TYPE (HexWidgetAutoHighlight, hex_widget_autohighlight,
  * ------------------------------
  */
 
+/* PROPERTIES */
+
+enum
+{
+       DOCUMENT = 1,
+       N_PROPERTIES
+};
+
+static GParamSpec *properties[N_PROPERTIES];
+
+/* SIGNALS */
+
+enum {
+       CURSOR_MOVED_SIGNAL,
+       DATA_CHANGED_SIGNAL,
+       CUT_CLIPBOARD_SIGNAL,
+       COPY_CLIPBOARD_SIGNAL,
+       PASTE_CLIPBOARD_SIGNAL,
+       DRAW_COMPLETE_SIGNAL,
+       LAST_SIGNAL
+};
+
+static guint gtkhex_signals[LAST_SIGNAL] = { 0 };
+
 /**
  * HexWidget:
  *
@@ -212,8 +226,6 @@ G_DEFINE_TYPE (HexWidget, hex_widget, GTK_TYPE_WIDGET)
 
 /* STATIC FORWARD DECLARATIONS */
 
-static guint gtkhex_signals[LAST_SIGNAL] = { 0 };
-
 static char *char_widths = NULL;
 
 static void render_highlights (HexWidget *self, cairo_t *cr, gint64 cursor_line,
@@ -240,6 +252,51 @@ static void recalc_displays (HexWidget *self);
 
 static void show_cursor (HexWidget *self, gboolean show);
 
+
+/* PROPERTIES - GETTERS AND SETTERS */
+
+static void
+hex_widget_set_property (GObject *object,
+               guint property_id,
+               const GValue *value,
+               GParamSpec *pspec)
+{
+       HexWidget *self = HEX_WIDGET(object);
+
+       switch (property_id)
+       {
+               case DOCUMENT:
+                       self->document = g_value_get_object (value);
+                       break;
+
+               default:
+                       /* We don't have any other property... */
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                       break;
+       }
+}
+
+static void
+hex_widget_get_property (GObject *object,
+               guint property_id,
+               GValue *value,
+               GParamSpec *pspec)
+{
+       HexWidget *self = HEX_WIDGET(object);
+
+       switch (property_id)
+       {
+               case DOCUMENT:
+                       g_value_set_object (value, self->document);
+                       break;
+
+               default:
+                       /* We don't have any other property... */
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                       break;
+       }
+}
+
 /* HexWidget - Method Definitions */
 
 /* ACTIONS */
@@ -2320,6 +2377,32 @@ document_changed_cb (HexDocument* doc, gpointer change_data,
                        "gtkhex.redo", hex_document_can_redo (doc));
 }
 
+static void
+hex_widget_constructed (GObject *object)
+{
+       HexWidget *self = HEX_WIDGET(object);
+
+       /* Setup document signals */
+
+       g_signal_connect (self->document, "file-read-started",
+                       G_CALLBACK (file_read_started_cb), self);
+
+       g_signal_connect (self->document, "file-loaded",
+                       G_CALLBACK (file_loaded_cb), self);
+
+       g_signal_connect (self->document, "document-changed",
+                       G_CALLBACK (document_changed_cb), self);
+
+       g_signal_connect (self->document, "undo",
+                       G_CALLBACK (doc_undo_redo_cb), self);
+
+       g_signal_connect (self->document, "redo",
+                       G_CALLBACK (doc_undo_redo_cb), self);
+
+       /* Chain up */
+       G_OBJECT_CLASS(hex_widget_parent_class)->constructed (object);
+}
+
 static void
 hex_widget_dispose (GObject *object)
 {
@@ -2375,11 +2458,23 @@ hex_widget_class_init (HexWidgetClass *klass)
 
        /* vfuncs */
 
+       object_class->constructed = hex_widget_constructed;
        object_class->dispose = hex_widget_dispose;
        object_class->finalize = hex_widget_finalize;
+       object_class->set_property = hex_widget_set_property;
+       object_class->get_property = hex_widget_get_property;
+
        widget_class->snapshot = hex_widget_snapshot;
 
-       /* Layout manager: box-style layout. */
+       /* PROPERTIES */
+
+       properties[DOCUMENT] = g_param_spec_object ("document", NULL, NULL,
+                       HEX_TYPE_DOCUMENT,
+                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
+
+       g_object_class_install_properties (object_class, N_PROPERTIES, properties);
+
+       /* Layout manager */
 
        gtk_widget_class_set_layout_manager_type (widget_class,
                        HEX_TYPE_WIDGET_LAYOUT);
@@ -2746,8 +2841,6 @@ hex_widget_init (HexWidget *self)
        gtk_widget_set_visible (self->busy_spinner, FALSE);
        gtk_widget_set_parent (self->busy_spinner, widget);
 
-       /* SETUP SIGNALS */
-
        /* GESTURES */
 
        /* Whole HexWidget widget (for right-click context menu) */
@@ -2881,31 +2974,9 @@ hex_widget_init (HexWidget *self)
 GtkWidget *
 hex_widget_new (HexDocument *owner)
 {
-       HexWidget *self;
-
-       self = HEX_WIDGET (g_object_new (HEX_TYPE_WIDGET, NULL));
-       g_return_val_if_fail (self != NULL, NULL);
-
-       self->document = owner;
-
-       /* Setup document signals */
-
-    g_signal_connect (G_OBJECT (self->document), "file-read-started",
-            G_CALLBACK (file_read_started_cb), self);
-
-    g_signal_connect (G_OBJECT (self->document), "file-loaded",
-            G_CALLBACK (file_loaded_cb), self);
-
-    g_signal_connect (G_OBJECT (self->document), "document-changed",
-            G_CALLBACK (document_changed_cb), self);
-
-    g_signal_connect (G_OBJECT (self->document), "undo",
-            G_CALLBACK (doc_undo_redo_cb), self);
-       
-    g_signal_connect (G_OBJECT (self->document), "redo",
-            G_CALLBACK (doc_undo_redo_cb), self);
-       
-       return GTK_WIDGET(self);
+       return g_object_new (HEX_TYPE_WIDGET,
+                       "document", owner,
+                       NULL);
 }
 
 /**
diff --git a/src/hex-document.c b/src/hex-document.c
index f42840a..854d989 100644
--- a/src/hex-document.c
+++ b/src/hex-document.c
@@ -54,6 +54,8 @@ static void undo_stack_free             (HexDocument *doc);
 #define DEFAULT_UNDO_DEPTH 1024
 #define REGEX_SEARCH_LEN 1024  /* FIXME/TODO: This is kind of lazy. Stopgap? */
 
+/* SIGNALS */
+
 enum {
        DOCUMENT_CHANGED,
        UNDO,
@@ -68,6 +70,17 @@ enum {
 
 static guint hex_signals[LAST_SIGNAL];
 
+/* PROPERTIES */
+
+enum
+{
+       GFILE = 1,
+       BUFFER,
+       N_PROPERTIES
+};
+
+static GParamSpec *properties[N_PROPERTIES];
+
 
 /* HexDocumentFindData GType Definitions */
 
@@ -145,6 +158,58 @@ struct _HexDocument
 
 G_DEFINE_TYPE (HexDocument, hex_document, G_TYPE_OBJECT)
 
+
+/* PROPERTIES - GETTERS AND SETTERS */
+
+static void
+hex_document_set_property (GObject *object,
+               guint property_id,
+               const GValue *value,
+               GParamSpec *pspec)
+{
+       HexDocument *doc = HEX_DOCUMENT(object);
+
+       switch (property_id)
+       {
+               case GFILE:
+                       hex_document_set_file (doc, g_value_get_object (value));
+                       break;
+
+               case BUFFER:
+                       hex_document_set_buffer (doc, g_value_get_object (value));
+                       break;
+
+               default:
+                       /* We don't have any other property... */
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                       break;
+       }
+}
+
+static void
+hex_document_get_property (GObject *object,
+               guint property_id,
+               GValue *value,
+               GParamSpec *pspec)
+{
+       HexDocument *doc = HEX_DOCUMENT(object);
+
+       switch (property_id)
+       {
+               case GFILE:
+                       /* --- */
+                       break;
+
+               case BUFFER:
+                       break;
+
+               default:
+                       /* We don't have any other property... */
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                       break;
+       }
+}
+
 /* ---- */
 
 static void
@@ -276,9 +341,27 @@ static void
 hex_document_class_init (HexDocumentClass *klass)
 {
        GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
+       GParamFlags default_flags = G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
+               G_PARAM_EXPLICIT_NOTIFY;
        
        gobject_class->finalize = hex_document_finalize;
-       gobject_class->dispose= hex_document_dispose;
+       gobject_class->dispose = hex_document_dispose;
+       gobject_class->set_property = hex_document_set_property;
+       gobject_class->get_property = hex_document_get_property;
+
+       /* PROPERTIES */
+
+       properties[GFILE] = g_param_spec_object ("file", NULL, NULL, 
+                       G_TYPE_FILE,
+                       default_flags);
+
+       properties[BUFFER] = g_param_spec_object ("buffer", NULL, NULL, 
+                       HEX_TYPE_BUFFER,
+                       default_flags);
+
+       g_object_class_install_properties (gobject_class, N_PROPERTIES, properties);
+
+       /* SIGNALS */
        
        hex_signals[DOCUMENT_CHANGED] =
                g_signal_new_class_handler ("document-changed",
@@ -409,7 +492,9 @@ hex_document_set_file (HexDocument *doc, GFile *file)
        }
 
        doc->file = g_object_ref (file);
+
        g_signal_emit (G_OBJECT(doc), hex_signals[FILE_NAME_CHANGED], 0);
+       g_object_notify_by_pspec (G_OBJECT(doc), properties[BUFFER]);
 
        return TRUE;
 }
@@ -1846,5 +1931,6 @@ hex_document_set_buffer (HexDocument *doc, HexBuffer *buf)
        g_clear_object (&doc->buffer);
        doc->buffer = buf;
 
+       g_object_notify_by_pspec (G_OBJECT(doc), properties[BUFFER]);
        return TRUE;
 }


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