[libgda] gdaui-entry-bin: ported to G_DECLARE/G_DEFINE



commit 6a405f7532bae19e7d5265ab4e83deb4381ad5bd
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Sep 20 17:01:06 2018 -0500

    gdaui-entry-bin: ported to G_DECLARE/G_DEFINE

 libgda-ui/data-entries/gdaui-entry-bin.c | 125 ++++++++++---------------------
 libgda-ui/data-entries/gdaui-entry-bin.h |  19 +----
 2 files changed, 40 insertions(+), 104 deletions(-)
---
diff --git a/libgda-ui/data-entries/gdaui-entry-bin.c b/libgda-ui/data-entries/gdaui-entry-bin.c
index 62de30540..64ffdf310 100644
--- a/libgda-ui/data-entries/gdaui-entry-bin.c
+++ b/libgda-ui/data-entries/gdaui-entry-bin.c
@@ -26,10 +26,7 @@
 /* 
  * Main static functions 
  */
-static void gdaui_entry_bin_class_init (GdauiEntryBinClass * class);
-static void gdaui_entry_bin_init (GdauiEntryBin * srv);
 static void gdaui_entry_bin_dispose (GObject   * object);
-static void gdaui_entry_bin_finalize (GObject   * object);
 
 /* virtual functions */
 static GtkWidget *create_entry (GdauiEntryWrapper *mgwrap);
@@ -39,53 +36,25 @@ static void       connect_signals(GdauiEntryWrapper *mgwrap, GCallback modify_cb
 static void       set_editable (GdauiEntryWrapper *mgwrap, gboolean editable);
 static void       grab_focus (GdauiEntryWrapper *mgwrap);
 
-/* get a pointer to the parents to be able to call their destructor */
-static GObjectClass  *parent_class = NULL;
-
 /* private structure */
-struct _GdauiEntryBinPrivate
+typedef struct
 {
        GtkWidget *label;
 
        BinMenu    commonbin;
        GtkWidget *entry_widget;
        gboolean   editable;
-};
+} GdauiEntryBinPrivate;
 
+G_DEFINE_TYPE_WITH_PRIVATE (GdauiEntryBin, gdaui_entry_bin, GDAUI_TYPE_ENTRY_WRAPPER)
 
-GType
-gdaui_entry_bin_get_type (void)
-{
-       static GType type = 0;
-
-       if (G_UNLIKELY (type == 0)) {
-               static const GTypeInfo info = {
-                       sizeof (GdauiEntryBinClass),
-                       (GBaseInitFunc) NULL,
-                       (GBaseFinalizeFunc) NULL,
-                       (GClassInitFunc) gdaui_entry_bin_class_init,
-                       NULL,
-                       NULL,
-                       sizeof (GdauiEntryBin),
-                       0,
-                       (GInstanceInitFunc) gdaui_entry_bin_init,
-                       0
-               };
-               
-               type = g_type_register_static (GDAUI_TYPE_ENTRY_WRAPPER, "GdauiEntryBin", &info, 0);
-       }
-       return type;
-}
 
 static void
 gdaui_entry_bin_class_init (GdauiEntryBinClass *class)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (class);
 
-       parent_class = g_type_class_peek_parent (class);
-
        object_class->dispose = gdaui_entry_bin_dispose;
-       object_class->finalize = gdaui_entry_bin_finalize;
 
        GDAUI_ENTRY_WRAPPER_CLASS (class)->create_entry = create_entry;
        GDAUI_ENTRY_WRAPPER_CLASS (class)->real_set_value = real_set_value;
@@ -96,12 +65,12 @@ gdaui_entry_bin_class_init (GdauiEntryBinClass *class)
 }
 
 static void
-gdaui_entry_bin_init (GdauiEntryBin * gdaui_entry_bin)
+gdaui_entry_bin_init (GdauiEntryBin * dbin)
 {
-       gdaui_entry_bin->priv = g_new0 (GdauiEntryBinPrivate, 1);
-       gdaui_entry_bin->priv->label = NULL;
-       gdaui_entry_bin->priv->editable = TRUE;
-       common_bin_init (&(gdaui_entry_bin->priv->commonbin));
+       GdauiEntryBinPrivate *priv = gdaui_entry_bin_get_instance_private (dbin);
+       priv->label = NULL;
+       priv->editable = TRUE;
+       common_bin_init (&(priv->commonbin));
 }
 
 /**
@@ -133,35 +102,17 @@ gdaui_entry_bin_new (GdaDataHandler *dh, GType type)
 static void
 gdaui_entry_bin_dispose (GObject   * object)
 {
-       GdauiEntryBin *gdaui_entry_bin;
-
-       g_return_if_fail (object != NULL);
-       g_return_if_fail (GDAUI_IS_ENTRY_BIN (object));
-
-       gdaui_entry_bin = GDAUI_ENTRY_BIN (object);
-       if (gdaui_entry_bin->priv)
-               common_bin_reset (&(gdaui_entry_bin->priv->commonbin));
-
-       /* parent class */
-       parent_class->dispose (object);
-}
-
-static void
-gdaui_entry_bin_finalize (GObject   * object)
-{
-       GdauiEntryBin *gdaui_entry_bin;
+       GdauiEntryBin *dbin;
 
        g_return_if_fail (object != NULL);
        g_return_if_fail (GDAUI_IS_ENTRY_BIN (object));
 
-       gdaui_entry_bin = GDAUI_ENTRY_BIN (object);
-       if (gdaui_entry_bin->priv) {
-               g_free (gdaui_entry_bin->priv);
-               gdaui_entry_bin->priv = NULL;
-       }
+       dbin = GDAUI_ENTRY_BIN (object);
+       GdauiEntryBinPrivate *priv = gdaui_entry_bin_get_instance_private (dbin);
+       common_bin_reset (&(priv->commonbin));
 
        /* parent class */
-       parent_class->finalize (object);
+       G_OBJECT_CLASS (gdaui_entry_bin_parent_class)->dispose (object);
 }
 
 /*
@@ -173,10 +124,11 @@ gdaui_entry_bin_finalize (GObject   * object)
 static void
 take_as_current_value (GdauiEntryBin *dbin, GValue *value)
 {
-       common_bin_adjust (&(dbin->priv->commonbin), dbin->priv->editable, value);
+       GdauiEntryBinPrivate *priv = gdaui_entry_bin_get_instance_private (dbin);
+       common_bin_adjust (&(priv->commonbin), priv->editable, value);
 
        gchar *str;
-       str = common_bin_get_description (&(dbin->priv->commonbin));
+       str = common_bin_get_description (&(priv->commonbin));
        gchar *markup;
        if (str) {
                markup = g_markup_printf_escaped ("<a href=''>%s</a>", str);
@@ -184,16 +136,17 @@ take_as_current_value (GdauiEntryBin *dbin, GValue *value)
        }
        else
                markup = g_markup_printf_escaped ("<a href=''><i>%s</i></a>", _("No data"));
-       gtk_label_set_markup (GTK_LABEL (dbin->priv->label), markup);
+       gtk_label_set_markup (GTK_LABEL (priv->label), markup);
        g_free (markup);
 }
 
 static void
 event_after_cb (GtkWidget *widget, GdkEvent *event, GdauiEntryBin *dbin)
 {
+       GdauiEntryBinPrivate *priv = gdaui_entry_bin_get_instance_private (dbin);
        /* don't "forward" event if popover is shown */ 
-       if (!dbin->priv->commonbin.popover || !gtk_widget_is_visible (dbin->priv->commonbin.popover))
-               g_signal_emit_by_name (dbin->priv->entry_widget, "event-after", event);
+       if (!priv->commonbin.popover || !gtk_widget_is_visible (priv->commonbin.popover))
+               g_signal_emit_by_name (priv->entry_widget, "event-after", event);
 }
 
 static GtkWidget *
@@ -205,16 +158,16 @@ create_entry (GdauiEntryWrapper *mgwrap)
 
        g_return_val_if_fail (GDAUI_IS_ENTRY_BIN (mgwrap), NULL);
        dbin = GDAUI_ENTRY_BIN (mgwrap);
-       g_return_val_if_fail (dbin->priv, NULL);
+       GdauiEntryBinPrivate *priv = gdaui_entry_bin_get_instance_private (dbin);
 
        hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
-       dbin->priv->entry_widget = hbox;
+       priv->entry_widget = hbox;
 
        label = gtk_label_new ("");
        gtk_widget_set_halign (label, GTK_ALIGN_START);
        gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
        gtk_widget_show (label);
-       dbin->priv->label = label;
+       priv->label = label;
 
        g_signal_connect (label, "event-after",
                          G_CALLBACK (event_after_cb), dbin);
@@ -231,7 +184,6 @@ real_set_value (GdauiEntryWrapper *mgwrap, const GValue *value)
 
        g_return_if_fail (GDAUI_IS_ENTRY_BIN (mgwrap));
        dbin = GDAUI_ENTRY_BIN (mgwrap);
-       g_return_if_fail (dbin->priv);
 
        take_as_current_value (dbin, value ? gda_value_copy (value) : NULL);
 }
@@ -243,10 +195,10 @@ real_get_value (GdauiEntryWrapper *mgwrap)
 
        g_return_val_if_fail (GDAUI_IS_ENTRY_BIN (mgwrap), NULL);
        dbin = GDAUI_ENTRY_BIN (mgwrap);
-       g_return_val_if_fail (dbin->priv, NULL);
+       GdauiEntryBinPrivate *priv = gdaui_entry_bin_get_instance_private (dbin);
 
-       if (dbin->priv->commonbin.tmpvalue)
-               return gda_value_copy (dbin->priv->commonbin.tmpvalue);
+       if (priv->commonbin.tmpvalue)
+               return gda_value_copy (priv->commonbin.tmpvalue);
        else
                return gda_value_new_null ();
 }
@@ -265,13 +217,14 @@ value_loaded_cb (GdauiEntryBin *dbin, GValue *new_value)
 static void
 link_activated_cb (GdauiEntryBin *dbin, const gchar *uri, GtkWidget *label)
 {
-       if (!dbin->priv->commonbin.popover)
-               common_bin_create_menu (GTK_WIDGET (label), &(dbin->priv->commonbin),
+       GdauiEntryBinPrivate *priv = gdaui_entry_bin_get_instance_private (dbin);
+       if (!priv->commonbin.popover)
+               common_bin_create_menu (GTK_WIDGET (label), &(priv->commonbin),
                                        gdaui_data_entry_get_value_type (GDAUI_DATA_ENTRY (dbin)),
                                        (BinCallback) value_loaded_cb, dbin);
 
-       common_bin_adjust (&(dbin->priv->commonbin), dbin->priv->editable, dbin->priv->commonbin.tmpvalue);
-       gtk_widget_show (dbin->priv->commonbin.popover);
+       common_bin_adjust (&(priv->commonbin), priv->editable, priv->commonbin.tmpvalue);
+       gtk_widget_show (priv->commonbin.popover);
 }
 
 static void
@@ -282,10 +235,10 @@ connect_signals (GdauiEntryWrapper *mgwrap, G_GNUC_UNUSED GCallback modify_cb,
 
        g_return_if_fail (GDAUI_IS_ENTRY_BIN (mgwrap));
        dbin = GDAUI_ENTRY_BIN (mgwrap);
-       g_return_if_fail (dbin->priv);
+       GdauiEntryBinPrivate *priv = gdaui_entry_bin_get_instance_private (dbin);
 
-       g_assert (dbin->priv->label);
-       g_signal_connect_swapped (G_OBJECT (dbin->priv->label), "activate-link",
+       g_assert (priv->label);
+       g_signal_connect_swapped (G_OBJECT (priv->label), "activate-link",
                                  G_CALLBACK (link_activated_cb), dbin);
 }
 
@@ -296,10 +249,10 @@ set_editable (GdauiEntryWrapper *mgwrap, gboolean editable)
 
        g_return_if_fail (GDAUI_IS_ENTRY_BIN (mgwrap));
        dbin = GDAUI_ENTRY_BIN (mgwrap);
-       g_return_if_fail (dbin->priv);
+       GdauiEntryBinPrivate *priv = gdaui_entry_bin_get_instance_private (dbin);
 
-       dbin->priv->editable = editable;
-       common_bin_adjust (&(dbin->priv->commonbin), editable, dbin->priv->commonbin.tmpvalue);
+       priv->editable = editable;
+       common_bin_adjust (&(priv->commonbin), editable, priv->commonbin.tmpvalue);
 }
 
 static void
@@ -309,7 +262,7 @@ grab_focus (GdauiEntryWrapper *mgwrap)
 
        g_return_if_fail (GDAUI_IS_ENTRY_BIN (mgwrap));
        dbin = GDAUI_ENTRY_BIN (mgwrap);
-       g_return_if_fail (dbin->priv);
+       GdauiEntryBinPrivate *priv = gdaui_entry_bin_get_instance_private (dbin);
 
-       gtk_widget_grab_focus (dbin->priv->label);
+       gtk_widget_grab_focus (priv->label);
 }
diff --git a/libgda-ui/data-entries/gdaui-entry-bin.h b/libgda-ui/data-entries/gdaui-entry-bin.h
index b04c3ea4d..5f50740c5 100644
--- a/libgda-ui/data-entries/gdaui-entry-bin.h
+++ b/libgda-ui/data-entries/gdaui-entry-bin.h
@@ -27,30 +27,13 @@
 G_BEGIN_DECLS
 
 #define GDAUI_TYPE_ENTRY_BIN          (gdaui_entry_bin_get_type())
-#define GDAUI_ENTRY_BIN(obj)          G_TYPE_CHECK_INSTANCE_CAST (obj, gdaui_entry_bin_get_type(), 
GdauiEntryBin)
-#define GDAUI_ENTRY_BIN_CLASS(klass)  G_TYPE_CHECK_CLASS_CAST (klass, gdaui_entry_bin_get_type (), 
GdauiEntryBinClass)
-#define GDAUI_IS_ENTRY_BIN(obj)       G_TYPE_CHECK_INSTANCE_TYPE (obj, gdaui_entry_bin_get_type ())
-
-
-typedef struct _GdauiEntryBin GdauiEntryBin;
-typedef struct _GdauiEntryBinClass GdauiEntryBinClass;
-typedef struct _GdauiEntryBinPrivate GdauiEntryBinPrivate;
-
-
-/* struct for the object's data */
-struct _GdauiEntryBin
-{
-       GdauiEntryWrapper           object;
-       GdauiEntryBinPrivate       *priv;
-};
-
+G_DECLARE_DERIVABLE_TYPE(GdauiEntryBin, gdaui_entry_bin, GDAUI, ENTRY_BIN, GdauiEntryWrapper)
 /* struct for the object's class */
 struct _GdauiEntryBinClass
 {
        GdauiEntryWrapperClass      parent_class;
 };
 
-GType        gdaui_entry_bin_get_type        (void) G_GNUC_CONST;
 GtkWidget   *gdaui_entry_bin_new             (GdaDataHandler *dh, GType type);
 
 


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