]> saetta.ns0.it Git - libgtkform/commitdiff
Added gtkbuilder-file to xml defition and implemented in code.
authorAndrea Zagli <azagli@libero.it>
Thu, 30 Jul 2009 08:37:31 +0000 (10:37 +0200)
committerAndrea Zagli <azagli@libero.it>
Thu, 30 Jul 2009 08:37:31 +0000 (10:37 +0200)
Added function GtkForm::get_gtkbuilder.
Fixed some bugs.
Changed GtkForm::fields container from GList to GSList.

src/form.c
src/form.h
test/from_xml.c
test/test.xml

index d24dc7eabca6936a9eeada9b27ebdb7145bfbeb1..d4e4423dfb12296d8d233b8d6639e691377e67ce 100644 (file)
@@ -22,6 +22,8 @@
 
 #include <string.h>
 
+#include <libxml/xpath.h>
+
 #include "form.h"
 
 #include "fieldboolean.h"
@@ -62,8 +64,10 @@ static void gtk_form_get_property (GObject *object,
 typedef struct _GtkFormPrivate GtkFormPrivate;
 struct _GtkFormPrivate
        {
+               GtkBuilder *gtkbuilder;
+
                gchar *table;
-               GList *fields;
+               GSList *fields;
                GtkFormKey *key;
        };
 
@@ -127,20 +131,11 @@ GtkForm
 *gtk_form_new_from_xml (xmlDoc *xmldoc, GtkBuilder *gtkbuilder)
 {
        GtkForm *form;
+       GtkFormPrivate *priv;
        xmlNode *cur;
-       GtkBuilder *new_gtkbuilder;
 
        form = NULL;
 
-       if (GTK_IS_BUILDER (gtkbuilder))
-               {
-                       new_gtkbuilder = gtkbuilder;
-               }
-       else
-               {
-                       new_gtkbuilder = NULL;
-               }
-
        cur = xmlDocGetRootElement (xmldoc);
        if (cur != NULL)
                {
@@ -157,9 +152,50 @@ GtkForm
 
                                        form = gtk_form_new ();
 
-                                       /* TO DO
-                                        * search gtkguilder-file node
-                                        */
+                                       priv = GTK_FORM_GET_PRIVATE (form);
+
+                                       if (GTK_IS_BUILDER (gtkbuilder))
+                                               {
+                                                       priv->gtkbuilder = gtkbuilder;
+                                               }
+                                       else
+                                               {
+                                                       priv->gtkbuilder = NULL;
+                                               }
+
+                                       /* search gtkguilder-file node */
+                                       if (!GTK_IS_BUILDER (priv->gtkbuilder))
+                                               {
+                                                       xmlXPathContextPtr xpcontext;
+                                                       xmlXPathObjectPtr xpresult;
+                                                       xmlNodeSetPtr xnodeset;
+
+                                                       xpcontext = xmlXPathNewContext (xmldoc);
+
+                                                       xpcontext->node = cur;
+                                                       xpresult = xmlXPathEvalExpression ((const xmlChar *)"child::gtkbuilder-file", xpcontext);
+                                                       if (!xmlXPathNodeSetIsEmpty (xpresult->nodesetval))
+                                                               {
+                                                                       xnodeset = xpresult->nodesetval;
+                                                                       if (xnodeset->nodeNr == 1)
+                                                                               {
+                                                                                       GError *error;
+                                                                                       gchar *filename;
+
+                                                                                       filename = (gchar *)xmlNodeGetContent (xnodeset->nodeTab[0]);
+
+                                                                                       priv->gtkbuilder = gtk_builder_new ();
+
+                                                                                       error = NULL;
+                                                                                       if (gtk_builder_add_from_file (priv->gtkbuilder, filename, &error) <= 0)
+                                                                                               {
+                                                                                                       priv->gtkbuilder = NULL;
+                                                                                                       g_warning ("Error on loading GtkBuilder file: %s\n",
+                                                                                                                  error->message);
+                                                                                               }
+                                                                               }
+                                                               }
+                                               }
 
                                        cur = cur->children;
                                        while (cur)
@@ -200,7 +236,10 @@ GtkForm
                                                                                {
                                                                                        name = xmlGetProp (cur, (const xmlChar *)"name");
 
-                                                                                       gtk_form_widget_set_from_gtkbuilder (widget, new_gtkbuilder, name);
+                                                                                       if (GTK_IS_BUILDER (priv->gtkbuilder))
+                                                                                               {
+                                                                                                       gtk_form_widget_set_from_gtkbuilder (widget, priv->gtkbuilder, name);
+                                                                                               }
 
                                                                                        node_widget = cur->children;
                                                                                        while (node_widget != NULL)
@@ -317,6 +356,23 @@ GtkForm
        return form;
 }
 
+/**
+ * gtk_form_get_gtkbuilder:
+ * @form: a #GtkForm object.
+ *
+ */
+GtkBuilder
+*gtk_form_get_gtkbuilder (GtkForm *form)
+{
+       GtkFormPrivate *priv;
+
+       g_return_val_if_fail (IS_GTK_FORM (form), NULL);
+
+       priv = GTK_FORM_GET_PRIVATE (form);
+
+       return priv->gtkbuilder;
+}
+
 /**
  * gtk_form_get_table:
  * @form: a #GtkForm object.
@@ -410,7 +466,7 @@ gtk_form_add_field (GtkForm *form, GtkFormField *field)
 
        priv = GTK_FORM_GET_PRIVATE (form);
 
-       priv->fields = g_list_append (priv->fields, field);
+       priv->fields = g_slist_append (priv->fields, field);
 
        ret = TRUE;
 
@@ -434,7 +490,7 @@ gtk_form_remove_field (GtkForm *form, GtkFormField *field)
 
        priv = GTK_FORM_GET_PRIVATE (form);
 
-       priv->fields = g_list_remove (priv->fields, field);
+       priv->fields = g_slist_remove (priv->fields, field);
 
        ret = TRUE;
 
@@ -482,7 +538,7 @@ GtkFormField
 
        priv = GTK_FORM_GET_PRIVATE (form);
 
-       GList *fields = g_list_first (priv->fields);
+       GSList *fields = priv->fields;
        while (fields != NULL)
                {
                        name = gtk_form_field_get_field_name ((GtkFormField *)fields->data);
@@ -492,7 +548,7 @@ GtkFormField
                                        break;
                                }
 
-                       fields = g_list_next (fields);
+                       fields = g_slist_next (fields);
                }
 
        return field;
@@ -517,7 +573,7 @@ GtkFormWidget
 
        priv = GTK_FORM_GET_PRIVATE (form);
 
-       GList *fields = g_list_first (priv->fields);
+       GSList *fields = priv->fields;
        while (fields != NULL)
                {
                        widget = gtk_form_field_get_gtk_form_widget ((GtkFormField *)fields->data);
@@ -528,7 +584,7 @@ GtkFormWidget
                                        break;
                                }
 
-                       fields = g_list_next (fields);
+                       fields = g_slist_next (fields);
                }
 
        return widget_ret;
@@ -550,7 +606,7 @@ gtk_form_clear (GtkForm *form)
 
        priv = GTK_FORM_GET_PRIVATE (form);
 
-       GList *fields = g_list_first (priv->fields);
+       GSList *fields = priv->fields;
 
        while (fields != NULL)
                {
@@ -558,7 +614,7 @@ gtk_form_clear (GtkForm *form)
 
                        gtk_form_field_clear (field);
 
-                       fields = g_list_next (fields);
+                       fields = g_slist_next (fields);
                }
 
        ret = TRUE;
@@ -583,7 +639,7 @@ gtk_form_check (GtkForm *form)
 
        GtkFormPrivate *priv = GTK_FORM_GET_PRIVATE (form);
 
-       GList *fields = g_list_first (priv->fields);
+       GSList *fields = priv->fields;
 
        while (fields != NULL)
                {
@@ -599,10 +655,10 @@ gtk_form_check (GtkForm *form)
                                        break;
                                }
 
-                       fields = g_list_next (fields);
+                       fields = g_slist_next (fields);
                }
 
-       if (ret)
+       if (ret && IS_GTK_FORM_KEY (priv->key))
                {
                        ret = gtk_form_key_check (priv->key);
                }
@@ -625,7 +681,7 @@ gtk_form_set_sensitive (GtkForm *form, gboolean sensitive)
 
        g_return_if_fail (IS_GTK_FORM (form));
 
-       GList *fields = g_list_first (priv->fields);
+       GSList *fields = priv->fields;
        while (fields != NULL)
                {
                        fwidget = gtk_form_field_get_gtk_form_widget ((GtkFormField *)fields->data);
@@ -633,7 +689,7 @@ gtk_form_set_sensitive (GtkForm *form, gboolean sensitive)
                        widget = gtk_form_widget_get_widget (fwidget);
                        gtk_widget_set_sensitive (widget, sensitive);
 
-                       fields = g_list_next (fields);
+                       fields = g_slist_next (fields);
                }
 }
 
@@ -651,14 +707,14 @@ gtk_form_set_editable (GtkForm *form, gboolean editable)
 
        g_return_if_fail (IS_GTK_FORM (form));
 
-       GList *fields = g_list_first (priv->fields);
+       GSList *fields = priv->fields;
        while (fields != NULL)
                {
                        fwidget = gtk_form_field_get_gtk_form_widget ((GtkFormField *)fields->data);
 
                        gtk_form_widget_set_editable (fwidget, editable);
 
-                       fields = g_list_next (fields);
+                       fields = g_slist_next (fields);
                }
 }
 
@@ -684,7 +740,7 @@ gchar
 
        GtkFormPrivate *priv = GTK_FORM_GET_PRIVATE (form);
 
-       GList *fields = g_list_first (priv->fields);
+       GSList *fields = priv->fields;
 
        while (fields != NULL)
                {
@@ -710,7 +766,8 @@ gchar
                                                                break;
 
                                                        case GTK_FORM_SQL_UPDATE:
-                                                               if (!gtk_form_key_field_is_key (priv->key, field))
+                                                               if (!IS_GTK_FORM_KEY (priv->key) ||
+                                                                   !gtk_form_key_field_is_key (priv->key, field))
                                                                        {
                                                                                value = gtk_form_field_get_value_sql (field);
                                                                                if (value != NULL)
@@ -722,7 +779,7 @@ gchar
                                                }
                                }
 
-                       fields = g_list_next (fields);
+                       fields = g_slist_next (fields);
                }
 
        if (g_str_has_suffix (fields_names, ", "))
@@ -753,11 +810,14 @@ gchar
                                break;
                }
 
-       where = gtk_form_key_get_sql (priv->key);
-       if ((type == GTK_FORM_SQL_SELECT || type == GTK_FORM_SQL_UPDATE || type == GTK_FORM_SQL_DELETE)
-           && where != NULL && strcmp (where, "") != 0)
+       if (IS_GTK_FORM_KEY (priv->key))
                {
-                       sql = g_strconcat (sql, where, NULL);
+                       where = gtk_form_key_get_sql (priv->key);
+                       if ((type == GTK_FORM_SQL_SELECT || type == GTK_FORM_SQL_UPDATE || type == GTK_FORM_SQL_DELETE)
+                               && where != NULL && strcmp (where, "") != 0)
+                               {
+                                       sql = g_strconcat (sql, where, NULL);
+                               }
                }
 
        return sql;
@@ -781,7 +841,7 @@ gtk_form_fill_from_datamodel (GtkForm *form, GdaDataModel *dm, gint row)
 
        GtkFormPrivate *priv = GTK_FORM_GET_PRIVATE (form);
 
-       GList *fields = g_list_first (priv->fields);
+       GSList *fields = priv->fields;
 
        while (fields != NULL)
                {
@@ -789,7 +849,7 @@ gtk_form_fill_from_datamodel (GtkForm *form, GdaDataModel *dm, gint row)
 
                        gtk_form_field_set_from_datamodel (field, dm, row);
 
-                       fields = g_list_next (fields);
+                       fields = g_slist_next (fields);
                }
 
        return ret;
index 456574de6f433a828e409d11204fe1ab66114711..8c7c142ba3e1acd073cd46c2f2fd17c340995bf4 100644 (file)
@@ -59,6 +59,8 @@ GtkForm *gtk_form_new (void);
 GtkForm *gtk_form_new_from_xml (xmlDoc *xmldoc, GtkBuilder *gtkbuilder);
 GtkForm *gtk_form_new_from_file (const gchar *filename, GtkBuilder *gtkbuilder);
 
+GtkBuilder *gtk_form_get_gtkbuilder (GtkForm *form);
+
 const gchar *gtk_form_get_table (GtkForm *form);
 void gtk_form_set_table (GtkForm *form, const gchar *table);
 
index 74cca128a45eaceed0e4901bfa0550a1263246b3..2a1ab1f1d2d42a846a869b2cfb2e26c3b0dbffae 100644 (file)
@@ -193,18 +193,16 @@ main (int argc, char **argv)
 
        gtk_init (&argc, &argv);
 
-       gtkbuilder = gtk_builder_new ();
+       form = gtk_form_new_from_file (GUIDIR "/test.xml", NULL);
+       if (form == NULL) return 0;
 
-       gtk_builder_add_from_file (gtkbuilder, GUIDIR "/test.gui", NULL);
+       gtkbuilder = gtk_form_get_gtkbuilder (form);
 
        w = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "wMain"));
 
        tbtnEditable = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "tbtnEditable"));
        tbtnSensitive = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "tbtnSensitive"));
 
-       form = gtk_form_new_from_file (GUIDIR "/test.xml", gtkbuilder);
-       if (form == NULL) return 0;
-
        create_cb_nation (GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "cbNation")));
 
        txtvSql = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "txtvSql"));
index 21f7a9fc47ec71408db8b3d59e45240321c7730c..82d046c8120f52414f2b964e524ea81537ab8072 100644 (file)
@@ -1,5 +1,7 @@
 <gtkform>
 
+       <gtkbuilder-file>test/test.gui</gtkbuilder-file>
+
        <table>names</table>
 
        <widget type="label" name="lblID">