From f38722f83fcdd2edb15d100c2907e59398ebf6a2 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Tue, 28 Aug 2012 13:07:13 +0200 Subject: [PATCH] Added function GtkForm::fill_from_xml (closes #305). --- libgtkform/field.c | 25 +++++++++++++- libgtkform/field.h | 4 ++- libgtkform/fieldboolean.c | 63 ++++++++++++++++++++++++++++++++++- libgtkform/fieldboolean.h | 3 +- libgtkform/fielddatetime.c | 67 +++++++++++++++++++++++++++++++++++++- libgtkform/fielddatetime.h | 3 +- libgtkform/fieldfloat.c | 66 ++++++++++++++++++++++++++++++++++++- libgtkform/fieldfloat.h | 3 +- libgtkform/fieldinteger.c | 63 ++++++++++++++++++++++++++++++++++- libgtkform/fieldinteger.h | 3 +- libgtkform/fieldtext.c | 64 +++++++++++++++++++++++++++++++++++- libgtkform/fieldtext.h | 3 +- libgtkform/form.c | 39 +++++++++++++++++++++- libgtkform/form.h | 1 + test/from_xml.c | 44 +++++++++++++++++++++++++ test/main.c | 44 +++++++++++++++++++++++++ test/test.gui | 16 +++++++++ test/values.xml | 6 ++++ test/values_single.xml | 2 ++ 19 files changed, 506 insertions(+), 13 deletions(-) create mode 100644 test/values.xml create mode 100644 test/values_single.xml diff --git a/libgtkform/field.c b/libgtkform/field.c index 1b72ca8..b550f46 100644 --- a/libgtkform/field.c +++ b/libgtkform/field.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Andrea Zagli + * Copyright (C) 2005-2012 Andrea Zagli * * 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. diff --git a/libgtkform/field.h b/libgtkform/field.h index 1e6f68d..0b19647 100644 --- a/libgtkform/field.h +++ b/libgtkform/field.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Andrea Zagli + * Copyright (C) 2005-2012 Andrea Zagli * * 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); diff --git a/libgtkform/fieldboolean.c b/libgtkform/fieldboolean.c index d38d706..ae7735e 100644 --- a/libgtkform/fieldboolean.c +++ b/libgtkform/fieldboolean.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Andrea Zagli + * Copyright (C) 2005-2012 Andrea Zagli * * 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 #include +#include #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. diff --git a/libgtkform/fieldboolean.h b/libgtkform/fieldboolean.h index 84ca860..a972f42 100644 --- a/libgtkform/fieldboolean.h +++ b/libgtkform/fieldboolean.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Andrea Zagli + * Copyright (C) 2005-2012 Andrea Zagli * * 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); diff --git a/libgtkform/fielddatetime.c b/libgtkform/fielddatetime.c index d92b244..720d51d 100644 --- a/libgtkform/fielddatetime.c +++ b/libgtkform/fielddatetime.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2011 Andrea Zagli + * Copyright (C) 2005-2012 Andrea Zagli * * 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 #include +#include #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. diff --git a/libgtkform/fielddatetime.h b/libgtkform/fielddatetime.h index bf6ea46..dbe4663 100644 --- a/libgtkform/fielddatetime.h +++ b/libgtkform/fielddatetime.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2011 Andrea Zagli + * Copyright (C) 2005-2012 Andrea Zagli * * 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); diff --git a/libgtkform/fieldfloat.c b/libgtkform/fieldfloat.c index a09f011..cc42624 100644 --- a/libgtkform/fieldfloat.c +++ b/libgtkform/fieldfloat.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2011 Andrea Zagli + * Copyright (C) 2005-2012 Andrea Zagli * * 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 #include +#include #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. diff --git a/libgtkform/fieldfloat.h b/libgtkform/fieldfloat.h index 62fcc3b..c940823 100644 --- a/libgtkform/fieldfloat.h +++ b/libgtkform/fieldfloat.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2011 Andrea Zagli + * Copyright (C) 2005-2012 Andrea Zagli * * 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); diff --git a/libgtkform/fieldinteger.c b/libgtkform/fieldinteger.c index f9b18f3..47718ca 100644 --- a/libgtkform/fieldinteger.c +++ b/libgtkform/fieldinteger.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2011 Andrea Zagli + * Copyright (C) 2005-2012 Andrea Zagli * * 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 +#include #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. diff --git a/libgtkform/fieldinteger.h b/libgtkform/fieldinteger.h index 4ca3ca7..36d12bf 100644 --- a/libgtkform/fieldinteger.h +++ b/libgtkform/fieldinteger.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2011 Andrea Zagli + * Copyright (C) 2005-2012 Andrea Zagli * * 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); diff --git a/libgtkform/fieldtext.c b/libgtkform/fieldtext.c index ae168da..b762971 100644 --- a/libgtkform/fieldtext.c +++ b/libgtkform/fieldtext.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2011 Andrea Zagli + * Copyright (C) 2005-2012 Andrea Zagli * * 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 +#include #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. diff --git a/libgtkform/fieldtext.h b/libgtkform/fieldtext.h index f005ca5..e5e7ff9 100644 --- a/libgtkform/fieldtext.h +++ b/libgtkform/fieldtext.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Andrea Zagli + * Copyright (C) 2005-2012 Andrea Zagli * * 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); diff --git a/libgtkform/form.c b/libgtkform/form.c index de8765c..2177dbe 100644 --- a/libgtkform/form.c +++ b/libgtkform/form.c @@ -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: diff --git a/libgtkform/form.h b/libgtkform/form.h index c8e0a1d..ea23150 100644 --- a/libgtkform/form.h +++ b/libgtkform/form.h @@ -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); diff --git a/test/from_xml.c b/test/from_xml.c index 4666882..23612ed 100644 --- a/test/from_xml.c +++ b/test/from_xml.c @@ -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) { diff --git a/test/main.c b/test/main.c index 7b2f42d..670915d 100644 --- a/test/main.c +++ b/test/main.c @@ -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) { diff --git a/test/test.gui b/test/test.gui index 6183e43..7c74c43 100644 --- a/test/test.gui +++ b/test/test.gui @@ -557,6 +557,22 @@ 4 + + + _Load XML + False + True + True + True + True + + + + False + False + 5 + + False diff --git a/test/values.xml b/test/values.xml new file mode 100644 index 0000000..fc524a9 --- /dev/null +++ b/test/values.xml @@ -0,0 +1,6 @@ + +55entry value from XML8832,652TRUEthe default value +for this text view +with also line feed +and t a b +EDIT FORM XML LOADING TEST2012-11-15 12:34:44MMM diff --git a/test/values_single.xml b/test/values_single.xml new file mode 100644 index 0000000..6f9b30b --- /dev/null +++ b/test/values_single.xml @@ -0,0 +1,2 @@ + +33 -- 2.49.0