[libgda] GdaUI: new entry plugin to specify a formatted entry



commit 9e15dd81753623497bf364be6605c712535d6bf8
Author: Vivien Malerba <malerba gnome-db org>
Date:   Tue Jan 3 22:27:50 2012 +0100

    GdaUI: new entry plugin to specify a formatted entry

 libgda-ui/data-entries/plugins/.gitignore          |    1 +
 libgda-ui/data-entries/plugins/Makefile.am         |    5 +-
 .../plugins/gdaui-entry-format-spec.xml.in         |    7 +
 .../data-entries/plugins/gdaui-entry-format.c      |  264 ++++++++++++++++++++
 .../data-entries/plugins/gdaui-entry-format.h      |   58 +++++
 libgda-ui/data-entries/plugins/libmain.c           |   32 +++
 6 files changed, 366 insertions(+), 1 deletions(-)
---
diff --git a/libgda-ui/data-entries/plugins/.gitignore b/libgda-ui/data-entries/plugins/.gitignore
index 9994c59..af68ed2 100644
--- a/libgda-ui/data-entries/plugins/.gitignore
+++ b/libgda-ui/data-entries/plugins/.gitignore
@@ -3,3 +3,4 @@ gdaui-entry-password.xml
 gdaui-entry-pict-spec.xml
 gdaui-entry-pict-spec_string.xml
 gdaui-entry-text-spec.xml
+gdaui-entry-format-spec.xml
diff --git a/libgda-ui/data-entries/plugins/Makefile.am b/libgda-ui/data-entries/plugins/Makefile.am
index 96ef981..944aec1 100644
--- a/libgda-ui/data-entries/plugins/Makefile.am
+++ b/libgda-ui/data-entries/plugins/Makefile.am
@@ -29,6 +29,7 @@ plugins_headers = \
 	gdaui-data-cell-renderer-pict.h \
 	gdaui-entry-filesel.h \
 	gdaui-entry-cidr.h \
+	gdaui-entry-format.h \
 	gdaui-entry-text.h \
 	gdaui-entry-pict.h \
 	gdaui-entry-rt.h \
@@ -45,6 +46,7 @@ libgda_ui_plugins_la_SOURCES = \
 	gdaui-data-cell-renderer-pict.c \
 	gdaui-entry-filesel.c \
 	gdaui-entry-cidr.c \
+	gdaui-entry-format.c \
 	gdaui-entry-text.c \
 	gdaui-entry-pict.c \
 	gdaui-entry-rt.c \
@@ -65,7 +67,8 @@ xml_in_files = \
 	gdaui-entry-pict-spec_string.xml.in \
 	gdaui-entry-filesel-spec.xml.in \
 	gdaui-entry-password.xml.in \
-	gdaui-entry-text-spec.xml.in
+	gdaui-entry-text-spec.xml.in \
+	gdaui-entry-format-spec.xml.in
 
 xml_DATA = $(xml_in_files:.xml.in=.xml)
 
diff --git a/libgda-ui/data-entries/plugins/gdaui-entry-format-spec.xml.in b/libgda-ui/data-entries/plugins/gdaui-entry-format-spec.xml.in
new file mode 100644
index 0000000..762f8da
--- /dev/null
+++ b/libgda-ui/data-entries/plugins/gdaui-entry-format-spec.xml.in
@@ -0,0 +1,7 @@
+<?xml version="1.0"?>
+<data-set-spec>
+  <parameters>
+    <parameter id="FORMAT" _name="Format" _descr="Allowed characters at each position('0':digit, '9': digit except 0, '@':alpha, '^':upper cased alpha, '#':alphanumeric, '*':any character, any other character will be left unmodified)" gdatype="string"/>
+    <parameter id="MASK" _name="Mask" _descr="Writing mask for corresponding characters in format ('_': writable, '-':writable, removed from actual value, ' ': not writable)" gdatype="string"/>
+  </parameters>
+</data-set-spec>
diff --git a/libgda-ui/data-entries/plugins/gdaui-entry-format.c b/libgda-ui/data-entries/plugins/gdaui-entry-format.c
new file mode 100644
index 0000000..867f28c
--- /dev/null
+++ b/libgda-ui/data-entries/plugins/gdaui-entry-format.c
@@ -0,0 +1,264 @@
+/*
+ * Copyright (C) 2009 - 2010 Vivien Malerba <malerba gnome-db org>
+ * Copyright (C) 2010 David King <davidk openismus com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#include <glib/gi18n-lib.h>
+#include "gdaui-entry-format.h"
+#include <libgda/gda-data-handler.h>
+#include <string.h>
+#include "gdaui-formatted-entry.h"
+
+/* 
+ * Main static functions 
+ */
+static void gdaui_entry_format_class_init (GdauiEntryFormatClass * class);
+static void gdaui_entry_format_init (GdauiEntryFormat * srv);
+static void gdaui_entry_format_dispose (GObject   * object);
+static void gdaui_entry_format_finalize (GObject   * object);
+
+/* virtual functions */
+static GtkWidget *create_entry (GdauiEntryWrapper *mgwrap);
+static void       real_set_value (GdauiEntryWrapper *mgwrap, const GValue *value);
+static GValue    *real_get_value (GdauiEntryWrapper *mgwrap);
+static void       connect_signals(GdauiEntryWrapper *mgwrap, GCallback modify_cb, GCallback activate_cb);
+static gboolean   can_expand (GdauiEntryWrapper *mgwrap, gboolean horiz);
+
+/* get a pointer to the parents to be able to call their destructor */
+static GObjectClass  *parent_class = NULL;
+
+/* private structure */
+struct _GdauiEntryFormatPrivate
+{
+	GtkWidget *entry;
+	gchar     *format;
+	gchar     *mask;
+};
+
+
+GType
+gdaui_entry_format_get_type (void)
+{
+	static GType type = 0;
+
+	if (G_UNLIKELY (type == 0)) {
+		static const GTypeInfo info = {
+			sizeof (GdauiEntryFormatClass),
+			(GBaseInitFunc) NULL,
+			(GBaseFinalizeFunc) NULL,
+			(GClassInitFunc) gdaui_entry_format_class_init,
+			NULL,
+			NULL,
+			sizeof (GdauiEntryFormat),
+			0,
+			(GInstanceInitFunc) gdaui_entry_format_init,
+			0
+		};
+		
+		type = g_type_register_static (GDAUI_TYPE_ENTRY_WRAPPER, "GdauiEntryFormat", &info, 0);
+	}
+	return type;
+}
+
+static void
+gdaui_entry_format_class_init (GdauiEntryFormatClass * class)
+{
+	GObjectClass   *object_class = G_OBJECT_CLASS (class);
+
+	parent_class = g_type_class_peek_parent (class);
+
+	object_class->dispose = gdaui_entry_format_dispose;
+	object_class->finalize = gdaui_entry_format_finalize;
+
+	GDAUI_ENTRY_WRAPPER_CLASS (class)->create_entry = create_entry;
+	GDAUI_ENTRY_WRAPPER_CLASS (class)->real_set_value = real_set_value;
+	GDAUI_ENTRY_WRAPPER_CLASS (class)->real_get_value = real_get_value;
+	GDAUI_ENTRY_WRAPPER_CLASS (class)->connect_signals = connect_signals;
+	GDAUI_ENTRY_WRAPPER_CLASS (class)->can_expand = can_expand;
+}
+
+static void
+gdaui_entry_format_init (GdauiEntryFormat * gdaui_entry_format)
+{
+	gdaui_entry_format->priv = g_new0 (GdauiEntryFormatPrivate, 1);
+	gdaui_entry_format->priv->entry = NULL;
+	gdaui_entry_format->priv->format = NULL;
+	gdaui_entry_format->priv->mask = NULL;
+}
+
+/**
+ * gdaui_entry_format_new
+ * @dh: the data handler to be used by the new widget
+ * @type: the requested data type (compatible with @dh)
+ * @options: the options
+ *
+ * Creates a new widget which is mainly a GtkEntry
+ *
+ * Returns: the new widget
+ */
+GtkWidget *
+gdaui_entry_format_new (GdaDataHandler *dh, GType type, const gchar *options)
+{
+	GObject *obj;
+	GdauiEntryFormat *mgformat;
+
+	g_return_val_if_fail (dh && GDA_IS_DATA_HANDLER (dh), NULL);
+	g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
+	g_return_val_if_fail (gda_data_handler_accepts_g_type (dh, type), NULL);
+
+	obj = g_object_new (GDAUI_TYPE_ENTRY_FORMAT, "handler", dh, NULL);
+	mgformat = GDAUI_ENTRY_FORMAT (obj);
+	if (options && *options) {
+                GdaQuarkList *params;
+                const gchar *str;
+
+                params = gda_quark_list_new_from_string (options);
+                str = gda_quark_list_find (params, "FORMAT");
+                if (str)
+                        mgformat->priv->format = g_strdup (str);
+                str = gda_quark_list_find (params, "MASK");
+                if (str)
+                        mgformat->priv->mask = g_strdup (str);
+
+                gda_quark_list_free (params);
+        }
+
+	gdaui_data_entry_set_value_type (GDAUI_DATA_ENTRY (mgformat), type);
+
+	return GTK_WIDGET (obj);
+}
+
+
+static void
+gdaui_entry_format_dispose (GObject   * object)
+{
+	GdauiEntryFormat *gdaui_entry_format;
+
+	g_return_if_fail (object != NULL);
+	g_return_if_fail (GDAUI_IS_ENTRY_FORMAT (object));
+
+	gdaui_entry_format = GDAUI_ENTRY_FORMAT (object);
+	if (gdaui_entry_format->priv) {
+		g_free (gdaui_entry_format->priv->format);
+		g_free (gdaui_entry_format->priv->mask);
+		g_free (gdaui_entry_format->priv);
+		gdaui_entry_format->priv = NULL;
+	}
+
+	/* parent class */
+	parent_class->dispose (object);
+}
+
+static void
+gdaui_entry_format_finalize (GObject   * object)
+{
+	GdauiEntryFormat *gdaui_entry_format;
+
+	g_return_if_fail (object != NULL);
+	g_return_if_fail (GDAUI_IS_ENTRY_FORMAT (object));
+
+	gdaui_entry_format = GDAUI_ENTRY_FORMAT (object);
+	if (gdaui_entry_format->priv) {
+
+		g_free (gdaui_entry_format->priv);
+		gdaui_entry_format->priv = NULL;
+	}
+
+	/* parent class */
+	parent_class->finalize (object);
+}
+
+static GtkWidget *
+create_entry (GdauiEntryWrapper *mgwrap)
+{
+	GtkWidget *entry;
+	GdauiEntryFormat *mgformat;
+
+	g_return_val_if_fail (mgwrap && GDAUI_IS_ENTRY_FORMAT (mgwrap), NULL);
+	mgformat = GDAUI_ENTRY_FORMAT (mgwrap);
+	g_return_val_if_fail (mgformat->priv, NULL);
+
+	entry = gdaui_formatted_entry_new (mgformat->priv->format, mgformat->priv->mask);
+
+	mgformat->priv->entry = entry;
+	if (mgformat->priv->format)
+		gtk_entry_set_width_chars (GTK_ENTRY (entry), g_utf8_strlen (mgformat->priv->format, -1));
+
+	return entry;
+}
+
+static void
+real_set_value (GdauiEntryWrapper *mgwrap, const GValue *value)
+{
+	GdauiEntryFormat *mgformat;
+
+	g_return_if_fail (mgwrap && GDAUI_IS_ENTRY_FORMAT (mgwrap));
+	mgformat = GDAUI_ENTRY_FORMAT (mgwrap);
+	g_return_if_fail (mgformat->priv);
+
+	if (value) {
+		if (gda_value_is_null ((GValue *) value))
+			gdaui_entry_set_text (GDAUI_ENTRY (mgformat->priv->entry), NULL);
+		else
+			gdaui_entry_set_text (GDAUI_ENTRY (mgformat->priv->entry),
+					      g_value_get_string (value));
+	}
+	else
+		gdaui_entry_set_text (GDAUI_ENTRY (mgformat->priv->entry), NULL);
+}
+
+static GValue *
+real_get_value (GdauiEntryWrapper *mgwrap)
+{
+	GValue *value = NULL;
+	GdauiEntryFormat *mgformat;
+	gchar *tmp;
+
+	g_return_val_if_fail (mgwrap && GDAUI_IS_ENTRY_FORMAT (mgwrap), NULL);
+	mgformat = GDAUI_ENTRY_FORMAT (mgwrap);
+	g_return_val_if_fail (mgformat->priv, NULL);
+
+	tmp = gdaui_entry_get_text (GDAUI_ENTRY (mgformat->priv->entry));
+	if (tmp && *tmp)
+		g_value_take_string ((value = gda_value_new (G_TYPE_STRING)), tmp);
+	else {
+		g_free (tmp);
+		value = gda_value_new_null ();
+	}
+
+	return value;
+}
+
+static void
+connect_signals (GdauiEntryWrapper *mgwrap, GCallback modify_cb, GCallback activate_cb)
+{
+	GdauiEntryFormat *mgformat;
+
+	g_return_if_fail (mgwrap && GDAUI_IS_ENTRY_FORMAT (mgwrap));
+	mgformat = GDAUI_ENTRY_FORMAT (mgwrap);
+	g_return_if_fail (mgformat->priv);
+
+	g_signal_connect (G_OBJECT (mgformat->priv->entry), "changed", modify_cb, mgwrap);
+	g_signal_connect (G_OBJECT (mgformat->priv->entry), "activate", activate_cb, mgwrap);
+}
+
+static gboolean
+can_expand (G_GNUC_UNUSED GdauiEntryWrapper *mgwrap, G_GNUC_UNUSED gboolean horiz)
+{
+	return FALSE;
+}
diff --git a/libgda-ui/data-entries/plugins/gdaui-entry-format.h b/libgda-ui/data-entries/plugins/gdaui-entry-format.h
new file mode 100644
index 0000000..3bd852b
--- /dev/null
+++ b/libgda-ui/data-entries/plugins/gdaui-entry-format.h
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2009 Vivien Malerba <malerba gnome-db org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+
+#ifndef __GDAUI_ENTRY_FORMAT_H_
+#define __GDAUI_ENTRY_FORMAT_H_
+
+#include <libgda-ui/data-entries/gdaui-entry-wrapper.h>
+
+G_BEGIN_DECLS
+
+#define GDAUI_TYPE_ENTRY_FORMAT          (gdaui_entry_format_get_type())
+#define GDAUI_ENTRY_FORMAT(obj)          G_TYPE_CHECK_INSTANCE_CAST (obj, gdaui_entry_format_get_type(), GdauiEntryFormat)
+#define GDAUI_ENTRY_FORMAT_CLASS(klass)  G_TYPE_CHECK_CLASS_CAST (klass, gdaui_entry_format_get_type (), GdauiEntryFormatClass)
+#define GDAUI_IS_ENTRY_FORMAT(obj)       G_TYPE_CHECK_INSTANCE_TYPE (obj, gdaui_entry_format_get_type ())
+
+
+typedef struct _GdauiEntryFormat GdauiEntryFormat;
+typedef struct _GdauiEntryFormatClass GdauiEntryFormatClass;
+typedef struct _GdauiEntryFormatPrivate GdauiEntryFormatPrivate;
+
+
+/* struct for the object's data */
+struct _GdauiEntryFormat
+{
+	GdauiEntryWrapper              object;
+	GdauiEntryFormatPrivate    *priv;
+};
+
+/* struct for the object's class */
+struct _GdauiEntryFormatClass
+{
+	GdauiEntryWrapperClass         parent_class;
+};
+
+GType        gdaui_entry_format_get_type        (void) G_GNUC_CONST;
+GtkWidget   *gdaui_entry_format_new             (GdaDataHandler *dh, GType type, const gchar *options);
+
+
+G_END_DECLS
+
+#endif
diff --git a/libgda-ui/data-entries/plugins/libmain.c b/libgda-ui/data-entries/plugins/libmain.c
index 52241f3..3fddc46 100644
--- a/libgda-ui/data-entries/plugins/libmain.c
+++ b/libgda-ui/data-entries/plugins/libmain.c
@@ -31,6 +31,7 @@
 #include "gdaui-entry-pict.h"
 #include "gdaui-entry-rt.h"
 #include "gdaui-data-cell-renderer-pict.h"
+#include "gdaui-entry-format.h"
 
 #ifdef HAVE_LIBGCRYPT
 #include "gdaui-entry-password.h"
@@ -51,6 +52,7 @@
 
 static GdauiDataEntry *plugin_entry_filesel_create_func (GdaDataHandler *handler, GType type, const gchar *options);
 static GdauiDataEntry *plugin_entry_cidr_create_func (GdaDataHandler *handler, GType type, const gchar *options);
+static GdauiDataEntry *plugin_entry_format_create_func (GdaDataHandler *handler, GType type, const gchar *options);
 static GdauiDataEntry *plugin_entry_text_create_func (GdaDataHandler *handler, GType type, const gchar *options);
 static GdauiDataEntry *plugin_entry_rt_create_func (GdaDataHandler *handler, GType type, const gchar *options);
 static GdauiDataEntry *plugin_entry_pict_create_func (GdaDataHandler *handler, GType type, const gchar *options);
@@ -105,6 +107,30 @@ plugin_init (GError **error)
 	plugin->cell_create_func = NULL;
 	retlist = g_slist_append (retlist, plugin);
 
+	/* FORMAT */
+	plugin = g_new0 (GdauiPlugin, 1);
+	plugin->plugin_name = "format";
+	plugin->plugin_descr = "Text entry with specific format";
+	plugin->plugin_file = NULL; /* always leave NULL */
+	plugin->nb_g_types = 1;
+	plugin->valid_g_types = g_new (GType, plugin->nb_g_types);
+	plugin->valid_g_types [0] = G_TYPE_STRING;
+	plugin->options_xml_spec = NULL;
+	plugin->entry_create_func = plugin_entry_format_create_func;
+	plugin->cell_create_func = NULL;
+	retlist = g_slist_append (retlist, plugin);
+	file = gda_gbr_get_file_path (GDA_LIB_DIR, LIBGDA_ABI_NAME, "plugins", "gdaui-entry-format-spec.xml", NULL);
+	if (! g_file_test (file, G_FILE_TEST_EXISTS)) {
+		if (error && !*error)
+			g_set_error (error, GDAUI_DATA_ENTRY_ERROR, GDAUI_DATA_ENTRY_FILE_NOT_FOUND_ERROR,
+				     _("Missing spec. file '%s'"), file);
+        }
+	else {
+		gsize len;
+		g_file_get_contents (file, &(plugin->options_xml_spec), &len, error);
+	}
+	g_free (file);
+
 #ifdef HAVE_LIBGCRYPT
 	/* PASSWORD */
 	plugin = g_new0 (GdauiPlugin, 1);
@@ -302,6 +328,12 @@ plugin_entry_cidr_create_func (GdaDataHandler *handler, GType type, G_GNUC_UNUSE
 }
 
 static GdauiDataEntry *
+plugin_entry_format_create_func (GdaDataHandler *handler, GType type, const gchar *options)
+{
+	return (GdauiDataEntry *) gdaui_entry_format_new (handler, type, options);
+}
+
+static GdauiDataEntry *
 plugin_entry_text_create_func (GdaDataHandler *handler, GType type, const gchar *options)
 {
 	return (GdauiDataEntry *) gdaui_entry_text_new (handler, type, options);



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