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



commit bf9f95fc499d5b14986d15a98735a8c1e4b79f71
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Sep 27 12:37:00 2018 -0500

    gdaui-formatted-entry: ported to G_DECLARE/G_DEFINE

 libgda-ui/data-entries/gdaui-formatted-entry.c | 197 +++++++++++--------------
 libgda-ui/data-entries/gdaui-formatted-entry.h |  20 +--
 2 files changed, 91 insertions(+), 126 deletions(-)
---
diff --git a/libgda-ui/data-entries/gdaui-formatted-entry.c b/libgda-ui/data-entries/gdaui-formatted-entry.c
index d0d7c21b4..813572563 100644
--- a/libgda-ui/data-entries/gdaui-formatted-entry.c
+++ b/libgda-ui/data-entries/gdaui-formatted-entry.c
@@ -2,6 +2,7 @@
  * Copyright (C) 2009 - 2012 Vivien Malerba <malerba gnome-db org>
  * Copyright (C) 2010 David King <davidk openismus com>
  * Copyright (C) 2011 Murray Cumming <murrayc murrayc com>
+ * Copyright (C) 2018 Daniel Espinosa <esodan gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -26,7 +27,7 @@
 
 #include "gdaui-formatted-entry.h"
 
-struct _GdauiFormattedEntryPrivate {
+typedef struct {
        gchar   *format; /* UTF-8! */
        gint     format_clen; /* in characters, not gchar */
        gchar   *mask; /* ASCII! */
@@ -34,11 +35,13 @@ struct _GdauiFormattedEntryPrivate {
 
        GdauiFormattedEntryInsertFunc insert_func;
        gpointer                      insert_func_data;
-};
+} GdauiFormattedEntryPrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (GdauiFormattedEntry, gdaui_formatted_entry, GDAUI_TYPE_ENTRY)
 
 static void gdaui_formatted_entry_class_init   (GdauiFormattedEntryClass *klass);
 static void gdaui_formatted_entry_init         (GdauiFormattedEntry *entry);
-static void gdaui_formatted_entry_finalize     (GObject *object);
+static void gdaui_formatted_entry_dispose     (GObject *object);
 static void gdaui_formatted_entry_set_property (GObject *object,
                                                guint param_id,
                                                const GValue *value,
@@ -59,41 +62,12 @@ enum
        PROP_MASK
 };
 
-static GObjectClass *parent_class = NULL;
-
-GType
-gdaui_formatted_entry_get_type (void)
-{
-       static GType type = 0;
-
-       if (G_UNLIKELY (type == 0)) {
-               static const GTypeInfo type_info = {
-                       sizeof (GdauiFormattedEntryClass),
-                       NULL,           /* base_init */
-                       NULL,           /* base_finalize */
-                       (GClassInitFunc) gdaui_formatted_entry_class_init,
-                       NULL,           /* class_finalize */
-                       NULL,           /* class_data */
-                       sizeof (GdauiFormattedEntry),
-                       0,              /* n_preallocs */
-                       (GInstanceInitFunc) gdaui_formatted_entry_init,
-                       0
-               };
-               
-               type = g_type_register_static (GDAUI_TYPE_ENTRY, "GdauiFormattedEntry", &type_info, 0);
-       }
-
-       return type;
-}
-
 static void
 gdaui_formatted_entry_class_init (GdauiFormattedEntryClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-       parent_class = g_type_class_peek_parent (klass);
-
-       object_class->finalize = gdaui_formatted_entry_finalize;
+       object_class->dispose = gdaui_formatted_entry_dispose;
        GDAUI_ENTRY_CLASS (klass)->assume_insert = gdaui_formatted_entry_assume_insert;
        GDAUI_ENTRY_CLASS (klass)->assume_delete = gdaui_formatted_entry_assume_delete;
        GDAUI_ENTRY_CLASS (klass)->get_empty_text = gdaui_formatted_entry_get_empty_text;
@@ -113,15 +87,15 @@ gdaui_formatted_entry_class_init (GdauiFormattedEntryClass *klass)
 static void
 gdaui_formatted_entry_init (GdauiFormattedEntry *entry)
 {
-       entry->priv = g_new0 (GdauiFormattedEntryPrivate, 1);
-       entry->priv->format = NULL;
-       entry->priv->mask = NULL;
-       entry->priv->insert_func = NULL;
-       entry->priv->insert_func_data = NULL;
+       GdauiFormattedEntryPrivate *priv = gdaui_formatted_entry_get_instance_private (entry);
+       priv->format = NULL;
+       priv->mask = NULL;
+       priv->insert_func = NULL;
+       priv->insert_func_data = NULL;
 }
 
 static void 
-gdaui_formatted_entry_finalize (GObject *object)
+gdaui_formatted_entry_dispose (GObject *object)
 {
        GdauiFormattedEntry *entry;
 
@@ -129,15 +103,18 @@ gdaui_formatted_entry_finalize (GObject *object)
         g_return_if_fail (GDAUI_IS_ENTRY (object));
 
         entry = GDAUI_FORMATTED_ENTRY (object);
-        if (entry->priv) {
-               g_free (entry->priv->format);
-               g_free (entry->priv->mask);
-                g_free (entry->priv);
-                entry->priv = NULL;
+        GdauiFormattedEntryPrivate *priv = gdaui_formatted_entry_get_instance_private (entry);
+        if (priv->format) {
+          g_free (priv->format);
+          priv->format = NULL;
+        }
+        if (priv->mask) {
+          g_free (priv->mask);
+          priv->mask = NULL;
         }
 
         /* parent class */
-        parent_class->finalize (object);
+        G_OBJECT_CLASS (gdaui_formatted_entry_parent_class)->dispose (object);
 }
 
 static void 
@@ -150,43 +127,42 @@ gdaui_formatted_entry_set_property (GObject *object,
        const gchar *str;
        gchar *otext;
 
-        entry = GDAUI_FORMATTED_ENTRY (object);
+       entry = GDAUI_FORMATTED_ENTRY (object);
+       GdauiFormattedEntryPrivate *priv = gdaui_formatted_entry_get_instance_private (entry);
        otext = gdaui_entry_get_text (GDAUI_ENTRY (entry));
-        if (entry->priv) {
-                switch (param_id) {
-                case PROP_FORMAT:
-                       g_free (entry->priv->format);
-                       entry->priv->format = NULL;
-                       entry->priv->format_clen = 0;
+       switch (param_id) {
+               case PROP_FORMAT:
+                       g_free (priv->format);
+                       priv->format = NULL;
+                       priv->format_clen = 0;
 
                        str = g_value_get_string (value);
                        if (str) {
                                if (! g_utf8_validate (str, -1, NULL))
                                        g_warning (_("Invalid UTF-8 format!"));
                                else {
-                                       entry->priv->format = g_strdup (str);
-                                       entry->priv->format_clen = g_utf8_strlen (str, -1);
+                                       priv->format = g_strdup (str);
+                                       priv->format_clen = g_utf8_strlen (str, -1);
                                        gdaui_entry_set_width_chars (GDAUI_ENTRY (entry),
-                                                                    entry->priv->format_clen);
+                                                                    priv->format_clen);
                                }
                        }
-                        break;
-                case PROP_MASK:
-                       g_free (entry->priv->mask);
-                       entry->priv->mask = NULL;
-                       entry->priv->mask_len = 0;
+                       break;
+               case PROP_MASK:
+                       g_free (priv->mask);
+                       priv->mask = NULL;
+                       priv->mask_len = 0;
 
                        str = g_value_get_string (value);
                        if (str) {
-                               entry->priv->mask = g_strdup (str);
-                               entry->priv->mask_len = strlen (str);
+                               priv->mask = g_strdup (str);
+                               priv->mask_len = strlen (str);
                        }
-                        break;
-                default:
-                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
-                        break;
-                }
-        }
+                       break;
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+                       break;
+       }
        gdaui_entry_set_text (GDAUI_ENTRY (entry), otext);
        g_free (otext);
 }
@@ -199,43 +175,43 @@ gdaui_formatted_entry_get_property (GObject *object,
 {
        GdauiFormattedEntry *entry;
 
-        entry = GDAUI_FORMATTED_ENTRY (object);
-        if (entry->priv) {
-                switch (param_id) {
-                case PROP_FORMAT:
-                       g_value_set_string (value, entry->priv->format);
-                        break;
-                case PROP_MASK:
-                       g_value_set_string (value, entry->priv->mask);
-                        break;
-                default:
-                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
-                        break;
-                }
-        }
+       entry = GDAUI_FORMATTED_ENTRY (object);
+       GdauiFormattedEntryPrivate *priv = gdaui_formatted_entry_get_instance_private (entry);
+       switch (param_id) {
+       case PROP_FORMAT:
+               g_value_set_string (value, priv->format);
+               break;
+       case PROP_MASK:
+               g_value_set_string (value, priv->mask);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+               break;
+       }
 }
 
 /*
  * is_writable
  * @fentry:
- * @pos: the position (in characters) in @fentry->priv->format
- * @ptr: the character (in @fentry->priv->format)
+ * @pos: the position (in characters) in @fpriv->format
+ * @ptr: the character (in @fpriv->format)
  *
  * Returns: %TRUE if it is a writable loaction
  */
 static gboolean
-is_writable (GdauiFormattedEntry *fentry, gint pos, const gchar *ptr)
+is_writable (GdauiFormattedEntry *entry, gint pos, const gchar *ptr)
 {
+       GdauiFormattedEntryPrivate *priv = gdaui_formatted_entry_get_instance_private (entry);
        if (((*ptr == '0') ||
             (*ptr == '9') ||
             (*ptr == '@') ||
             (*ptr == '^') ||
             (*ptr == '#') ||
             (*ptr == '*')) &&
-           (!fentry->priv->mask ||
-            (fentry->priv->mask &&
-             (pos < fentry->priv->mask_len) &&
-             (fentry->priv->mask [pos] != ' '))))
+           (!priv->mask ||
+            (priv->mask &&
+             (pos < priv->mask_len) &&
+             (priv->mask [pos] != ' '))))
                return TRUE;
        else
                return FALSE;
@@ -244,7 +220,7 @@ is_writable (GdauiFormattedEntry *fentry, gint pos, const gchar *ptr)
 /*
  * is_allowed
  * @fentry:
- * @ptr: the character (in @fentry->priv->format)
+ * @ptr: the character (in @priv->format)
  * @wc: the character to be inserted
  *
  * Returns: %TRUE if @wc can be used to replace @ptr
@@ -286,13 +262,14 @@ gdaui_formatted_entry_get_empty_text (GdauiEntry *entry)
        GdauiFormattedEntry *fentry;
 
        fentry = (GdauiFormattedEntry*) entry;
-       if (fentry->priv->format) {
+       GdauiFormattedEntryPrivate *priv = gdaui_formatted_entry_get_instance_private (fentry);
+       if (priv->format) {
                GString *string;
 
                string = g_string_new ("");
                gchar *ptr;
                gint i;
-               for (ptr = fentry->priv->format, i = 0;
+               for (ptr = priv->format, i = 0;
                     ptr && *ptr;
                     ptr = g_utf8_next_char (ptr), i++) {
                        if (is_writable (fentry, i, ptr))
@@ -317,12 +294,13 @@ gdaui_formatted_entry_assume_insert (GdauiEntry *entry, const gchar *text, G_GNU
        gint i, pos;
 
        fentry = (GdauiFormattedEntry*) entry;
-       if (!fentry->priv->format)
+       GdauiFormattedEntryPrivate *priv = gdaui_formatted_entry_get_instance_private (fentry);
+       if (!priv->format)
                return;
 
        const gchar *ptr, *fptr;
        pos = *virt_pos;
-       for (fptr = fentry->priv->format, i = 0;
+       for (fptr = priv->format, i = 0;
             (i < pos) && fptr && *fptr;
             fptr = g_utf8_next_char (fptr), i++);
        
@@ -333,7 +311,7 @@ gdaui_formatted_entry_assume_insert (GdauiEntry *entry, const gchar *text, G_GNU
        gboolean inserted = FALSE;
        gunichar wc;
        for (ptr = text, i = 0; ptr && *ptr && *fptr; ptr = g_utf8_next_char (ptr)) {
-               while ((pos < fentry->priv->format_clen) &&
+               while ((pos < priv->format_clen) &&
                       !is_writable (fentry, pos, fptr)) {
                        fptr = g_utf8_next_char (fptr);
                        if (!fptr || !*fptr) {
@@ -344,7 +322,7 @@ gdaui_formatted_entry_assume_insert (GdauiEntry *entry, const gchar *text, G_GNU
                }
                
                wc = g_utf8_get_char (ptr);
-               if ((pos < fentry->priv->format_clen) &&
+               if ((pos < priv->format_clen) &&
                    is_allowed (fentry, fptr, wc, &wc)){
                        /* Ok, insert *ptr (<=> text[i] if it was ASCII) */
                        gint rpos = pos + offset;
@@ -362,11 +340,11 @@ gdaui_formatted_entry_assume_insert (GdauiEntry *entry, const gchar *text, G_GNU
        _gdaui_entry_unblock_changes (entry);
        *virt_pos = pos;
 
-       if (!inserted && fentry->priv->insert_func) {
+       if (!inserted && priv->insert_func) {
                ptr = g_utf8_next_char (text);
                if (!*ptr) {
                        wc = g_utf8_get_char (text);
-                       fentry->priv->insert_func (fentry, wc, *virt_pos, fentry->priv->insert_func_data);
+                       priv->insert_func (fentry, wc, *virt_pos, priv->insert_func_data);
                }
        }
 }
@@ -379,7 +357,8 @@ gdaui_formatted_entry_assume_delete (GdauiEntry *entry, gint virt_start_pos, gin
        gint i;
 
        fentry = (GdauiFormattedEntry*) entry;
-       if (!fentry->priv->format)
+       GdauiFormattedEntryPrivate *priv = gdaui_formatted_entry_get_instance_private (fentry);
+       if (!priv->format)
                return;
 
 #ifdef GDA_DEBUG
@@ -388,15 +367,15 @@ gdaui_formatted_entry_assume_delete (GdauiEntry *entry, gint virt_start_pos, gin
        otext = gdaui_entry_get_text (entry);
        if (otext) {
                clen = g_utf8_strlen (otext, -1);
-               g_assert (clen == fentry->priv->format_clen);
+               g_assert (clen == priv->format_clen);
                g_free (otext);
        }
 #endif
 
-       g_assert (virt_end_pos <= fentry->priv->format_clen);
+       g_assert (virt_end_pos <= priv->format_clen);
 
-       /* move fptr to the @virt_start_pos in fentry->priv->format */
-       for (fptr = fentry->priv->format, i = 0;
+       /* move fptr to the @virt_start_pos in priv->format */
+       for (fptr = priv->format, i = 0;
             (i < virt_start_pos) && *fptr;
             fptr = g_utf8_next_char (fptr), i++);
        if (i != virt_start_pos)
@@ -414,7 +393,7 @@ gdaui_formatted_entry_assume_delete (GdauiEntry *entry, gint virt_start_pos, gin
                                        virt_start_pos --;
                                        virt_end_pos --;
                                        i--;
-                                       fptr = g_utf8_find_prev_char (fentry->priv->format, fptr);
+                                       fptr = g_utf8_find_prev_char (priv->format, fptr);
                                        npos --;
                                }
                                if (i < 0) {
@@ -497,11 +476,12 @@ gdaui_formatted_entry_get_text (GdauiFormattedEntry *entry)
        g_return_val_if_fail (GDAUI_IS_FORMATTED_ENTRY (entry), NULL);
        
        text = gdaui_entry_get_text ((GdauiEntry*) entry);
-       if (text && entry->priv->mask) {
+       GdauiFormattedEntryPrivate *priv = gdaui_formatted_entry_get_instance_private (entry);
+       if (text && priv->mask) {
                gchar *tptr, *mptr;
                gint len;
                len = strlen (text);
-               for (tptr = text, mptr = entry->priv->mask;
+               for (tptr = text, mptr = priv->mask;
                     *tptr && *mptr;
                     mptr++) {
                        if ((*mptr == '-') && (*tptr == '_')) {
@@ -530,7 +510,8 @@ gdaui_formatted_entry_set_insert_func (GdauiFormattedEntry *entry, GdauiFormatte
                                       gpointer data)
 {
        g_return_if_fail (GDAUI_IS_FORMATTED_ENTRY (entry));
+       GdauiFormattedEntryPrivate *priv = gdaui_formatted_entry_get_instance_private (entry);
 
-       entry->priv->insert_func = insert_func;
-       entry->priv->insert_func_data = data;
+       priv->insert_func = insert_func;
+       priv->insert_func_data = data;
 }
diff --git a/libgda-ui/data-entries/gdaui-formatted-entry.h b/libgda-ui/data-entries/gdaui-formatted-entry.h
index caff45e8c..1f069cf39 100644
--- a/libgda-ui/data-entries/gdaui-formatted-entry.h
+++ b/libgda-ui/data-entries/gdaui-formatted-entry.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2009 - 2012 Vivien Malerba <malerba gnome-db org>
+ * Copyright (C) 2018 Daniel Espinosa <esodan gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -25,29 +26,12 @@
 G_BEGIN_DECLS
 
 #define GDAUI_TYPE_FORMATTED_ENTRY                 (gdaui_formatted_entry_get_type ())
-#define GDAUI_FORMATTED_ENTRY(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
GDAUI_TYPE_FORMATTED_ENTRY, GdauiFormattedEntry))
-#define GDAUI_FORMATTED_ENTRY_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), 
GDAUI_TYPE_FORMATTED_ENTRY, GdauiFormattedEntry))
-#define GDAUI_IS_FORMATTED_ENTRY(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
GDAUI_TYPE_FORMATTED_ENTRY))
-#define GDAUI_IS_FORMATTED_ENTRY_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass), 
GDAUI_TYPE_FORMATTED_ENTRY))
-#define GDAUI_FORMATTED_ENTRY_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), 
GDAUI_TYPE_FORMATTED_ENTRY, GdauiFormattedEntry))
-
-
-typedef struct _GdauiFormattedEntry        GdauiFormattedEntry;
-typedef struct _GdauiFormattedEntryClass   GdauiFormattedEntryClass;
-typedef struct _GdauiFormattedEntryPrivate GdauiFormattedEntryPrivate;
-
-struct _GdauiFormattedEntry
-{
-       GdauiEntry                  entry;
-       GdauiFormattedEntryPrivate *priv;
-};
-
+G_DECLARE_DERIVABLE_TYPE (GdauiFormattedEntry, gdaui_formatted_entry, GDAUI, FORMATTED_ENTRY, GdauiEntry)
 struct _GdauiFormattedEntryClass
 {
        GdauiEntryClass             parent_class;
 };
 
-GType                 gdaui_formatted_entry_get_type           (void) G_GNUC_CONST;
 GtkWidget            *gdaui_formatted_entry_new                (const gchar *format, const gchar *mask);
 gchar                *gdaui_formatted_entry_get_text           (GdauiFormattedEntry *entry);
 


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