]> saetta.ns0.it Git - libgtkform/commitdiff
Added function GtkForm::fill_from_xml (closes #305).
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Tue, 28 Aug 2012 11:07:13 +0000 (13:07 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Tue, 28 Aug 2012 11:07:13 +0000 (13:07 +0200)
19 files changed:
libgtkform/field.c
libgtkform/field.h
libgtkform/fieldboolean.c
libgtkform/fieldboolean.h
libgtkform/fielddatetime.c
libgtkform/fielddatetime.h
libgtkform/fieldfloat.c
libgtkform/fieldfloat.h
libgtkform/fieldinteger.c
libgtkform/fieldinteger.h
libgtkform/fieldtext.c
libgtkform/fieldtext.h
libgtkform/form.c
libgtkform/form.h
test/from_xml.c
test/main.c
test/test.gui
test/values.xml [new file with mode: 0644]
test/values_single.xml [new file with mode: 0644]

index 1b72ca8d8eb86f2bd84b891d4bfd2e351d1f2b00..b550f464f0c7db3add02fb1d87f2656d8bc27f7f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2010 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2012 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -518,6 +518,29 @@ gtk_form_field_set_from_hashtable (GtkFormField *field, GHashTable *hashtable)
        return ret;
 }
 
+/**
+ * gtk_form_field_set_from_xml:
+ * @field: a #GtkFormField object.
+ * @xnode: an #xmlNode object.
+ *
+ */
+gboolean
+gtk_form_field_set_from_xml (GtkFormField *field, xmlNode *xnode)
+{
+       gboolean ret = FALSE;
+
+       GtkFormFieldPrivate *priv = GTK_FORM_FIELD_GET_PRIVATE (field);
+
+       g_return_val_if_fail (xnode != NULL, FALSE);
+
+       if (IS_GTK_FORM_FIELD (field) && GTK_FORM_FIELD_GET_CLASS (field)->set_from_xml != NULL)
+               {
+                       ret = GTK_FORM_FIELD_GET_CLASS (field)->set_from_xml (field, xnode);
+               }
+
+       return ret;
+}
+
 /**
  * gtk_form_field_set_value:
  * @field: a #GtkFormField object.
index 1e6f68df98690d2fc9ebbfc6556c7ee64f6d487f..0b196476784c18d03ac98bdb8b0f3bc49b54c761 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2010 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2012 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -61,6 +61,7 @@ struct _GtkFormFieldClass
                void (*set_as_origin) (GtkFormField *field);
                gboolean (*set_from_datamodel) (GtkFormField *field, GdaDataModel *dm, gint row);
                gboolean (*set_from_hashtable) (GtkFormField *field, GHashTable *hashtable);
+               gboolean (*set_from_xml) (GtkFormField *field, xmlNode *xnode);
                gboolean (*set_value) (GtkFormField *field, GValue *gvalue);
        };
 
@@ -95,6 +96,7 @@ void gtk_form_field_set_as_origin (GtkFormField *field);
 
 gboolean gtk_form_field_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, gint row);
 gboolean gtk_form_field_set_from_hashtable (GtkFormField *field, GHashTable *hashtable);
+gboolean gtk_form_field_set_from_xml (GtkFormField *field, xmlNode *xnode);
 gboolean gtk_form_field_set_value (GtkFormField *field, GValue *gvalue);
 
 
index d38d706b49436be40bcdc097ab009c315e320d7f..ae7735e52a397465c5e02eae895eaefb87b9b13a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2010 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2012 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,6 +23,7 @@
 #include <string.h>
 
 #include <glib/gi18n-lib.h>
+#include <libxml/xpath.h>
 
 #include "widget.h"
 #include "fieldboolean.h"
@@ -78,6 +79,7 @@ gtk_form_field_boolean_class_init (GtkFormFieldBooleanClass *klass)
        field_class->set_as_origin = gtk_form_field_boolean_set_as_origin;
        field_class->set_from_datamodel = gtk_form_field_boolean_set_from_datamodel;
        field_class->set_from_hashtable = gtk_form_field_boolean_set_from_hashtable;
+       field_class->set_from_xml = gtk_form_field_boolean_set_from_xml;
        field_class->set_value = gtk_form_field_boolean_set_value;
 
        g_object_class_install_property (object_class, PROP_DEFAULT,
@@ -402,6 +404,65 @@ gtk_form_field_boolean_set_from_hashtable (GtkFormField *field, GHashTable *hash
        return ret;
 }
 
+/**
+ * gtk_form_field_boolean_set_from_xml:
+ * @field: a #GtkFormField object.
+ * @xnode: an #xmlNode object.
+ *
+ */
+gboolean
+gtk_form_field_boolean_set_from_xml (GtkFormField *field, xmlNode *xnode)
+{
+       gboolean ret = FALSE;
+       GtkFormFieldBooleanPrivate *priv;
+       gchar *str;
+       gchar *field_name;
+
+       g_return_val_if_fail (IS_GTK_FORM_FIELD_BOOLEAN (field), FALSE);
+       g_return_val_if_fail (xnode != NULL, FALSE);
+
+       priv = GTK_FORM_FIELD_BOOLEAN_GET_PRIVATE (GTK_FORM_FIELD_BOOLEAN (field));
+
+       field_name = gtk_form_field_get_field_name (field);
+       if (field_name != NULL && g_strcmp0 (g_strstrip (field_name), "") != 0)
+               {
+                       xmlDoc *xdoc;
+                       xmlXPathContextPtr xpcontext;
+                       xmlXPathObjectPtr xpresult;
+                       xmlNodeSetPtr xnodeset;
+
+                       xdoc = xmlNewDoc ("1.0");
+                       xmlDocSetRootElement (xdoc, xnode);
+                       xpcontext = xmlXPathNewContext (xdoc);
+
+                       priv->original_value = FALSE;
+                       ret = gtk_form_field_boolean_set_value_stringify (field, "FALSE");
+
+                       xpcontext->node = xnode;
+                       xpresult = xmlXPathEvalExpression ((const xmlChar *)g_strdup_printf ("child::%s", field_name), xpcontext);
+                       if (!xmlXPathNodeSetIsEmpty (xpresult->nodesetval))
+                               {
+                                       xnodeset = xpresult->nodesetval;
+                                       if (xnodeset->nodeNr >= 1)
+                                               {
+                                                       str = (gchar *)xmlNodeGetContent (xnodeset->nodeTab[0]);
+                                                       ret = gtk_form_field_boolean_set_value_stringify (field, str);
+                                                       priv->original_value = gtk_form_field_boolean_str_to_boolean (gtk_form_field_boolean_get_value_stringify (field));
+                                                       xmlFree (str);
+                                               }
+                               }
+
+                       xmlXPathFreeObject (xpresult);
+                       xmlXPathFreeContext (xpcontext);
+                       xmlUnlinkNode (xnode);
+                       xmlFreeDoc (xdoc);
+
+                       g_free (field_name);
+               }
+
+       return ret;
+}
+
 /**
  * gtk_form_field_boolean_set_value:
  * @field: a #GtkFormField object.
index 84ca86048f38786ec7df018ca6de5f51eb9a8503..a972f42fec83b3069c6624db464ed54e80b5b20e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2010 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2012 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -64,6 +64,7 @@ void gtk_form_field_boolean_set_as_origin (GtkFormField *field);
 
 gboolean gtk_form_field_boolean_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, gint row);
 gboolean gtk_form_field_boolean_set_from_hashtable (GtkFormField *field, GHashTable *hashtable);
+gboolean gtk_form_field_boolean_set_from_xml (GtkFormField *field, xmlNode *xnode);
 gboolean gtk_form_field_boolean_set_value (GtkFormField *field, GValue *gvalue);
 
 gboolean gtk_form_field_boolean_str_to_boolean (const gchar *value);
index d92b2443b1d0207bdb8370b436d24a17496122d8..720d51de86a34a7d1d9d2a252debfaad03269550 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2011 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2012 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -25,6 +25,7 @@
 #include <time.h>
 
 #include <glib/gi18n-lib.h>
+#include <libxml/xpath.h>
 
 #include "widget.h"
 #include "fielddatetime.h"
@@ -95,6 +96,7 @@ gtk_form_field_datetime_class_init (GtkFormFieldDateTimeClass *klass)
        field_class->is_valid = gtk_form_field_datetime_is_valid;
        field_class->set_from_datamodel = gtk_form_field_datetime_set_from_datamodel;
        field_class->set_from_hashtable = gtk_form_field_datetime_set_from_hashtable;
+       field_class->set_from_xml = gtk_form_field_datetime_set_from_xml;
        field_class->set_value = gtk_form_field_datetime_set_value;
 
        g_object_class_install_property (object_class, PROP_DEFAULT,
@@ -640,6 +642,69 @@ gtk_form_field_datetime_set_from_hashtable (GtkFormField *field, GHashTable *has
        return ret;
 }
 
+/**
+ * gtk_form_field_boolean_set_from_xml:
+ * @field: a #GtkFormField object.
+ * @xnode: an #xmlNode object.
+ *
+ */
+gboolean
+gtk_form_field_datetime_set_from_xml (GtkFormField *field, xmlNode *xnode)
+{
+       gboolean ret = FALSE;
+       GtkFormFieldDateTimePrivate *priv;
+       gchar *str;
+       gchar *field_name;
+
+       g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), FALSE);
+       g_return_val_if_fail (xnode != NULL, FALSE);
+
+       priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (GTK_FORM_FIELD_DATETIME (field));
+
+       field_name = gtk_form_field_get_field_name (field);
+       if (field_name != NULL && g_strcmp0 (g_strstrip (field_name), "") != 0)
+               {
+                       xmlDoc *xdoc;
+                       xmlXPathContextPtr xpcontext;
+                       xmlXPathObjectPtr xpresult;
+                       xmlNodeSetPtr xnodeset;
+
+                       xdoc = xmlNewDoc ("1.0");
+                       xmlDocSetRootElement (xdoc, xnode);
+                       xpcontext = xmlXPathNewContext (xdoc);
+
+                       if (priv->original_value != NULL)
+                               {
+                                       g_date_time_unref (priv->original_value);
+                               }
+                       priv->original_value = NULL;
+                       ret = gtk_form_field_datetime_set_value_stringify (field, "");
+
+                       xpcontext->node = xnode;
+                       xpresult = xmlXPathEvalExpression ((const xmlChar *)g_strdup_printf ("child::%s", field_name), xpcontext);
+                       if (!xmlXPathNodeSetIsEmpty (xpresult->nodesetval))
+                               {
+                                       xnodeset = xpresult->nodesetval;
+                                       if (xnodeset->nodeNr >= 1)
+                                               {
+                                                       str = (gchar *)xmlNodeGetContent (xnodeset->nodeTab[0]);
+                                                       ret = gtk_form_field_datetime_set_value_stringify (field, str);
+                                                       priv->original_value = gtk_form_field_datetime_get_gdatetime_from_str (gtk_form_field_datetime_get_value_stringify (field), "%Y-%m-%d %H:%M:%S");
+                                                       xmlFree (str);
+                                               }
+                               }
+
+                       xmlXPathFreeObject (xpresult);
+                       xmlXPathFreeContext (xpcontext);
+                       xmlUnlinkNode (xnode);
+                       xmlFreeDoc (xdoc);
+
+                       g_free (field_name);
+               }
+
+       return ret;
+}
+
 /**
  * gtk_form_field_datetime_set_value:
  * @field: a #GtkFormField object.
index bf6ea46b769796e17b5b5d71bca04d55d3406129..dbe4663ab56116493eaf8804b1a9dd6506b50199 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2011 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2012 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -73,6 +73,7 @@ gboolean gtk_form_field_datetime_is_valid (GtkFormField *field);
 
 gboolean gtk_form_field_datetime_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, gint row);
 gboolean gtk_form_field_datetime_set_from_hashtable (GtkFormField *field, GHashTable *hashtable);
+gboolean gtk_form_field_datetime_set_from_xml (GtkFormField *field, xmlNode *xnode);
 gboolean gtk_form_field_datetime_set_value (GtkFormField *field, GValue *gvalue);
 
 struct tm *gtk_form_field_datetime_get_tm_from_str (const gchar *str, const gchar *format);
index a09f011057a1a46f77e65cb2c347b6ed304a79e1..cc426244f68d6df23c01eb3cd1a1873fbf924feb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2011 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2012 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,6 +23,7 @@
 #include <locale.h>
 
 #include <glib/gi18n-lib.h>
+#include <libxml/xpath.h>
 
 #include "widget.h"
 #include "fieldfloat.h"
@@ -83,6 +84,7 @@ gtk_form_field_float_class_init (GtkFormFieldFloatClass *klass)
        field_class->is_valid = gtk_form_field_float_is_valid;
        field_class->set_from_datamodel = gtk_form_field_float_set_from_datamodel;
        field_class->set_from_hashtable = gtk_form_field_float_set_from_hashtable;
+       field_class->set_from_xml = gtk_form_field_float_set_from_xml;
        field_class->set_value = gtk_form_field_float_set_value;
 
        g_object_class_install_property (object_class, PROP_DEFAULT,
@@ -514,6 +516,68 @@ gtk_form_field_float_set_from_hashtable (GtkFormField *field, GHashTable *hashta
        return ret;
 }
 
+/**
+ * gtk_form_field_float_set_from_xml:
+ * @field: a #GtkFormField object.
+ * @xnode: an #xmlNode object.
+ *
+ */
+gboolean
+gtk_form_field_float_set_from_xml (GtkFormField *field, xmlNode *xnode)
+{
+       gboolean ret = FALSE;
+       GtkFormFieldFloatPrivate *priv;
+       gchar *str;
+       gchar *field_name;
+
+       g_return_val_if_fail (IS_GTK_FORM_FIELD_FLOAT (field), FALSE);
+       g_return_val_if_fail (xnode != NULL, FALSE);
+
+       priv = GTK_FORM_FIELD_FLOAT_GET_PRIVATE (GTK_FORM_FIELD_FLOAT (field));
+
+       field_name = gtk_form_field_get_field_name (field);
+       if (field_name != NULL && g_strcmp0 (g_strstrip (field_name), "") != 0)
+               {
+                       xmlDoc *xdoc;
+                       xmlXPathContextPtr xpcontext;
+                       xmlXPathObjectPtr xpresult;
+                       xmlNodeSetPtr xnodeset;
+
+                       xdoc = xmlNewDoc ("1.0");
+                       xmlDocSetRootElement (xdoc, xnode);
+                       xpcontext = xmlXPathNewContext (xdoc);
+
+                       priv->original_value = 0.0f;
+                       ret = gtk_form_field_float_set_value_stringify (field, "");
+
+                       xpcontext->node = xnode;
+                       xpresult = xmlXPathEvalExpression ((const xmlChar *)g_strdup_printf ("child::%s", field_name), xpcontext);
+                       if (!xmlXPathNodeSetIsEmpty (xpresult->nodesetval))
+                               {
+                                       xnodeset = xpresult->nodesetval;
+                                       if (xnodeset->nodeNr >= 1)
+                                               {
+                                                       setlocale (LC_NUMERIC, "");
+                                                       gda_locale_changed ();
+
+                                                       str = (gchar *)xmlNodeGetContent (xnodeset->nodeTab[0]);
+                                                       ret = gtk_form_field_float_set_value_stringify (field, str);
+                                                       priv->original_value = g_strtod (gtk_form_field_float_get_value_stringify (field), NULL);
+                                                       xmlFree (str);
+                                               }
+                               }
+
+                       xmlXPathFreeObject (xpresult);
+                       xmlXPathFreeContext (xpcontext);
+                       xmlUnlinkNode (xnode);
+                       xmlFreeDoc (xdoc);
+
+                       g_free (field_name);
+               }
+
+       return ret;
+}
+
 /**
  * gtk_form_field_float_set_value:
  * @field: a #GtkFormField object.
index 62fcc3b64ed4c829790caba2330d1a5873397661..c940823d0b171c866118c5a5a3d395f6156dd812 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2011 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2012 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -66,6 +66,7 @@ gboolean gtk_form_field_float_is_valid (GtkFormField *field);
 
 gboolean gtk_form_field_float_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, gint row);
 gboolean gtk_form_field_float_set_from_hashtable (GtkFormField *field, GHashTable *hashtable);
+gboolean gtk_form_field_float_set_from_xml (GtkFormField *field, xmlNode *xnode);
 gboolean gtk_form_field_float_set_value (GtkFormField *field, GValue *gvalue);
 
 
index f9b18f34e01f3d97b0acc9c393dda52df89c7512..47718ca5f9330cf48a489cf93ec8724d486052af 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2011 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2012 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -21,6 +21,7 @@
 #endif
 
 #include <glib/gi18n-lib.h>
+#include <libxml/xpath.h>
 
 #include "widget.h"
 #include "widgetspin.h"
@@ -78,6 +79,7 @@ gtk_form_field_integer_class_init (GtkFormFieldIntegerClass *klass)
        field_class->is_valid = gtk_form_field_integer_is_valid;
        field_class->set_from_datamodel = gtk_form_field_integer_set_from_datamodel;
        field_class->set_from_hashtable = gtk_form_field_integer_set_from_hashtable;
+       field_class->set_from_xml = gtk_form_field_integer_set_from_xml;
        field_class->set_value = gtk_form_field_integer_set_value;
 
        g_object_class_install_property (object_class, PROP_DEFAULT,
@@ -439,6 +441,65 @@ gtk_form_field_integer_set_from_hashtable (GtkFormField *field, GHashTable *hash
        return ret;
 }
 
+/**
+ * gtk_form_field_integer_set_from_xml:
+ * @field: a #GtkFormField object.
+ * @xnode: an #xmlNode object.
+ *
+ */
+gboolean
+gtk_form_field_integer_set_from_xml (GtkFormField *field, xmlNode *xnode)
+{
+       gboolean ret = FALSE;
+       GtkFormFieldIntegerPrivate *priv;
+       gchar *str;
+       gchar *field_name;
+
+       g_return_val_if_fail (IS_GTK_FORM_FIELD_INTEGER (field), FALSE);
+       g_return_val_if_fail (xnode != NULL, FALSE);
+
+       priv = GTK_FORM_FIELD_INTEGER_GET_PRIVATE (GTK_FORM_FIELD_INTEGER (field));
+
+       field_name = gtk_form_field_get_field_name (field);
+       if (field_name != NULL && g_strcmp0 (g_strstrip (field_name), "") != 0)
+               {
+                       xmlDoc *xdoc;
+                       xmlXPathContextPtr xpcontext;
+                       xmlXPathObjectPtr xpresult;
+                       xmlNodeSetPtr xnodeset;
+
+                       xdoc = xmlNewDoc ("1.0");
+                       xmlDocSetRootElement (xdoc, xnode);
+                       xpcontext = xmlXPathNewContext (xdoc);
+
+                       priv->original_value = 0;
+                       ret = gtk_form_field_integer_set_value_stringify (field, "");
+
+                       xpcontext->node = xnode;
+                       xpresult = xmlXPathEvalExpression ((const xmlChar *)g_strdup_printf ("child::%s", field_name), xpcontext);
+                       if (!xmlXPathNodeSetIsEmpty (xpresult->nodesetval))
+                               {
+                                       xnodeset = xpresult->nodesetval;
+                                       if (xnodeset->nodeNr >= 1)
+                                               {
+                                                       str = (gchar *)xmlNodeGetContent (xnodeset->nodeTab[0]);
+                                                       ret = gtk_form_field_integer_set_value_stringify (field, str);
+                                                       priv->original_value = strtol (gtk_form_field_integer_get_value_stringify (field), NULL, 10);
+                                                       xmlFree (str);
+                                               }
+                               }
+
+                       xmlXPathFreeObject (xpresult);
+                       xmlXPathFreeContext (xpcontext);
+                       xmlUnlinkNode (xnode);
+                       xmlFreeDoc (xdoc);
+
+                       g_free (field_name);
+               }
+
+       return ret;
+}
+
 /**
  * gtk_form_field_integer_set_value:
  * @field: a #GtkFormField object.
index 4ca3ca74348b488bfac34af84141e62d46d91a45..36d12bfad3a4994b3dce2aa42d6a6ac2c9491d6a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2011 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2012 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -66,6 +66,7 @@ gboolean gtk_form_field_integer_is_valid (GtkFormField *field);
 
 gboolean gtk_form_field_integer_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, gint row);
 gboolean gtk_form_field_integer_set_from_hashtable (GtkFormField *field, GHashTable *hashtable);
+gboolean gtk_form_field_integer_set_from_xml (GtkFormField *field, xmlNode *xml);
 gboolean gtk_form_field_integer_set_value (GtkFormField *field, GValue *gvalue);
 
 
index ae168daba101d709975914899344c292e60843f7..b7629712a44f6b4ff7ceb459ab4b30977857a8cd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2011 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2012 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -21,6 +21,7 @@
 #endif
 
 #include <glib/gi18n-lib.h>
+#include <libxml/xpath.h>
 
 #include "widget.h"
 #include "fieldtext.h"
@@ -76,6 +77,7 @@ gtk_form_field_text_class_init (GtkFormFieldTextClass *klass)
        field_class->set_as_origin = gtk_form_field_text_set_as_origin;
        field_class->set_from_datamodel = gtk_form_field_text_set_from_datamodel;
        field_class->set_from_hashtable = gtk_form_field_text_set_from_hashtable;
+       field_class->set_from_xml = gtk_form_field_text_set_from_xml;
        field_class->set_value = gtk_form_field_text_set_value;
 
        g_object_class_install_property (object_class, PROP_DEFAULT,
@@ -383,6 +385,66 @@ gtk_form_field_text_set_from_hashtable (GtkFormField *field, GHashTable *hashtab
        return ret;
 }
 
+/**
+ * gtk_form_field_text_set_from_xml:
+ * @field: a #GtkFormField object.
+ * @xnode: an #xmlNode object.
+ *
+ */
+gboolean
+gtk_form_field_text_set_from_xml (GtkFormField *field, xmlNode *xnode)
+{
+       gboolean ret = FALSE;
+       GtkFormFieldTextPrivate *priv;
+       gchar *str;
+       gchar *field_name;
+
+       g_return_val_if_fail (IS_GTK_FORM_FIELD_TEXT (field), FALSE);
+       g_return_val_if_fail (xnode != NULL, FALSE);
+
+       priv = GTK_FORM_FIELD_TEXT_GET_PRIVATE (GTK_FORM_FIELD_TEXT (field));
+
+       field_name = gtk_form_field_get_field_name (field);
+       if (field_name != NULL && g_strcmp0 (g_strstrip (field_name), "") != 0)
+               {
+                       xmlDoc *xdoc;
+                       xmlXPathContextPtr xpcontext;
+                       xmlXPathObjectPtr xpresult;
+                       xmlNodeSetPtr xnodeset;
+
+                       xdoc = xmlNewDoc ("1.0");
+                       xmlDocSetRootElement (xdoc, xnode);
+                       xpcontext = xmlXPathNewContext (xdoc);
+
+                       g_free (priv->original_value);
+                       priv->original_value = g_strdup ("");
+                       ret = gtk_form_field_text_set_value_stringify (field, "");
+
+                       xpcontext->node = xnode;
+                       xpresult = xmlXPathEvalExpression ((const xmlChar *)g_strdup_printf ("child::%s", field_name), xpcontext);
+                       if (!xmlXPathNodeSetIsEmpty (xpresult->nodesetval))
+                               {
+                                       xnodeset = xpresult->nodesetval;
+                                       if (xnodeset->nodeNr >= 1)
+                                               {
+                                                       str = (gchar *)xmlNodeGetContent (xnodeset->nodeTab[0]);
+                                                       ret = gtk_form_field_text_set_value_stringify (field, str);
+                                                       priv->original_value = g_strdup (gtk_form_field_text_get_value_stringify (field));
+                                                       xmlFree (str);
+                                               }
+                               }
+
+                       xmlXPathFreeObject (xpresult);
+                       xmlXPathFreeContext (xpcontext);
+                       xmlUnlinkNode (xnode);
+                       xmlFreeDoc (xdoc);
+
+                       g_free (field_name);
+               }
+
+       return ret;
+}
+
 /**
  * gtk_form_field_text_set_value:
  * @field: a #GtkFormField object.
index f005ca5ae66ddf2e7e65400c3f862a1c59227068..e5e7ff9634c36711ba7c372ecd755bccf0b5be89 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2010 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2012 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -64,6 +64,7 @@ void gtk_form_field_text_set_as_origin (GtkFormField *field);
 
 gboolean gtk_form_field_text_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, gint row);
 gboolean gtk_form_field_text_set_from_hashtable (GtkFormField *field, GHashTable *hashtable);
+gboolean gtk_form_field_text_set_from_xml (GtkFormField *field, xmlNode *xnode);
 gboolean gtk_form_field_text_set_value (GtkFormField *field, GValue *gvalue);
 
 
index de8765cf792bcfc91f61ba3fab1c6ecaf82af49c..2177dbe1374438528a8096c9ee33772ba6775ca7 100644 (file)
@@ -2374,7 +2374,7 @@ gtk_form_fill_from_hashtable (GtkForm *form, GHashTable *hashtable)
 }
 
 /**
- * gtk_form_get_original_values_as_xml:
+ * gtk_form_get_values_as_xml:
  * @form:
  *
  */
@@ -2421,6 +2421,43 @@ xmlNode
        return xnode;
 }
 
+/**
+ * gtk_form_fill_from_xml:
+ * @form: a #GtkForm object.
+ * @xnode: an @xmlNode object.
+ *
+ */
+gboolean
+gtk_form_fill_from_xml (GtkForm *form, xmlNode *xnode)
+{
+       GtkFormPrivate *priv;
+       GSList *fields;
+       GtkFormField *field;
+
+       gboolean ret;
+
+       g_return_val_if_fail (IS_GTK_FORM (form), FALSE);
+       g_return_val_if_fail (xnode != NULL, FALSE);
+
+       priv = GTK_FORM_GET_PRIVATE (form);
+
+       fields = priv->fields;
+
+       while (fields != NULL)
+               {
+                       field = (GtkFormField *)fields->data;
+
+                       if (gtk_form_field_is_to_load (field))
+                               {
+                                       gtk_form_field_set_from_xml (field, xnode);
+                               }
+
+                       fields = g_slist_next (fields);
+               }
+
+       return ret;
+}
+
 /**
  * gtk_form_add_group:
  * @form:
index c8e0a1daf61d77fc3cfc112c73942bdea0db1271..ea23150e151604e08bac094a51b34c76bc02df05 100644 (file)
@@ -133,6 +133,7 @@ GHashTable *gtk_form_get_original_values_as_hashtable (GtkForm *form);
 gboolean gtk_form_fill_from_hashtable (GtkForm *form, GHashTable *hashtable);
 
 xmlNode *gtk_form_get_values_as_xml (GtkForm *form);
+gboolean gtk_form_fill_from_xml (GtkForm *form, xmlNode *xnode);
 
 gboolean gtk_form_add_group (GtkForm *form, GtkFormGroup *group);
 GtkFormGroup *gtk_form_get_group_by_name (GtkForm *form, const gchar *group_name);
index 4666882a168ce3415eb6cd6ca789561d0be09cdb..23612eddcf86481349e5853119a78c411a4cb2c0 100644 (file)
@@ -155,6 +155,50 @@ on_btnXml_clicked (GtkButton *button,
        gtk_text_buffer_set_text (buf, str, strlen (str));
 }
 
+G_MODULE_EXPORT void
+on_btnLoadXml_clicked (GtkButton *button,
+                         gpointer user_data)
+{
+       GtkWidget *dlg;
+
+       dlg = gtk_file_chooser_dialog_new ("Open XML",
+                                          GTK_WINDOW (w),
+                                          GTK_FILE_CHOOSER_ACTION_OPEN,
+                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                                          GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+                                          NULL);
+       if (gtk_dialog_run (GTK_DIALOG (dlg)) == GTK_RESPONSE_ACCEPT)
+               {
+                       gchar *filename;
+                       xmlDoc *xdoc;
+                       xmlNode *xnode;
+
+                       gint size;
+                       xmlChar *str;
+
+                       gtk_form_clear (form);
+
+                       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dlg));
+                       xdoc = xmlParseFile (filename);
+                       if (xdoc == NULL)
+                               {
+                                       return;
+                               }
+
+                       xnode = xmlDocGetRootElement (xdoc);
+                       gtk_form_fill_from_xml (form, xnode);
+                       xmlDocSetRootElement (xdoc, xnode);
+
+                       GtkTextBuffer *buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (txtvSql));
+                       xmlDocDumpMemory (xdoc, &str, &size);
+                       gtk_text_buffer_set_text (buf, str, strlen (str));
+
+                       xmlFreeDoc (xdoc);
+                       g_free (filename);
+               }
+       gtk_widget_destroy (dlg);
+}
+
 void
 create_cb_nation (GtkWidget *w)
 {
index 7b2f42de625f713b20c00fec32dd59ac3a5eef21..670915d724ea6451f599f2c4f0440502a052c0f0 100644 (file)
@@ -171,6 +171,50 @@ on_btnXml_clicked (GtkButton *button,
        gtk_text_buffer_set_text (buf, str, strlen (str));
 }
 
+G_MODULE_EXPORT void
+on_btnLoadXml_clicked (GtkButton *button,
+                         gpointer user_data)
+{
+       GtkWidget *dlg;
+
+       dlg = gtk_file_chooser_dialog_new ("Open XML",
+                                          GTK_WINDOW (w),
+                                          GTK_FILE_CHOOSER_ACTION_OPEN,
+                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                                          GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+                                          NULL);
+       if (gtk_dialog_run (GTK_DIALOG (dlg)) == GTK_RESPONSE_ACCEPT)
+               {
+                       gchar *filename;
+                       xmlDoc *xdoc;
+                       xmlNode *xnode;
+
+                       gint size;
+                       xmlChar *str;
+
+                       gtk_form_clear (form);
+
+                       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dlg));
+                       xdoc = xmlParseFile (filename);
+                       if (xdoc == NULL)
+                               {
+                                       return;
+                               }
+
+                       xnode = xmlDocGetRootElement (xdoc);
+                       gtk_form_fill_from_xml (form, xnode);
+                       xmlDocSetRootElement (xdoc, xnode);
+
+                       GtkTextBuffer *buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (txtvSql));
+                       xmlDocDumpMemory (xdoc, &str, &size);
+                       gtk_text_buffer_set_text (buf, str, strlen (str));
+
+                       xmlFreeDoc (xdoc);
+                       g_free (filename);
+               }
+       gtk_widget_destroy (dlg);
+}
+
 void
 create_cb_nation (GtkWidget *w)
 {
index 6183e43d15b5976c10dc0ce00959a6ccc6aef9ad..7c74c4305d0c6123be1be265939df6375b546fa9 100644 (file)
                 <property name="position">4</property>
               </packing>
             </child>
+            <child>
+              <object class="GtkButton" id="button2">
+                <property name="label" translatable="yes">_Load XML</property>
+                <property name="use_action_appearance">False</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_underline">True</property>
+                <signal name="clicked" handler="on_btnLoadXml_clicked" swapped="no"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">5</property>
+              </packing>
+            </child>
           </object>
           <packing>
             <property name="expand">False</property>
diff --git a/test/values.xml b/test/values.xml
new file mode 100644 (file)
index 0000000..fc524a9
--- /dev/null
@@ -0,0 +1,6 @@
+<?xml version="1.0"?>
+<values><id>55</id><name>entry value from XML</name><age>88</age><amount>32,65</amount><id_nation>2</id_nation><married>TRUE</married><description>the default value
+for this text view
+with also line feed
+and    t       a       b
+EDIT FORM XML LOADING TEST</description><now>2012-11-15 12:34:44</now><sex>M</sex><sex>M</sex><sex>M</sex></values>
diff --git a/test/values_single.xml b/test/values_single.xml
new file mode 100644 (file)
index 0000000..6f9b30b
--- /dev/null
@@ -0,0 +1,2 @@
+<?xml version="1.0"?>
+<values><id>33</id></values>