]> saetta.ns0.it Git - libgtkform/commitdiff
Bugfixes on GtkForm::check and GtkFormKey::check.
authorAndrea Zagli <azagli@libero.it>
Sun, 30 Aug 2009 09:03:51 +0000 (11:03 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 30 Aug 2009 09:03:51 +0000 (11:03 +0200)
src/form.c
src/form.h
src/key.c
src/key.h
test/from_xml.c
test/main.c

index f2d2310d6b4dece299d32f7cdc47559023f6ae37..865e86bfd4026a1fba632b34803d1514b543a7e9 100644 (file)
@@ -62,6 +62,9 @@ static void gtk_form_get_property (GObject *object,
                                GValue *value,
                                GParamSpec *pspec);
 
+static void gtk_form_show_check_error_dialog (GtkFormWidget *fwidget,
+                                              GtkWidget *parent_window);
+
 #define GTK_FORM_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_GTK_FORM, GtkFormPrivate))
 
 typedef struct _GtkFormPrivate GtkFormPrivate;
@@ -786,6 +789,7 @@ gtk_form_clear (GtkForm *form)
 /**
  * gtk_form_check:
  * @form: a #GtkForm object.
+ * @with_key: whether check also the key or not.
  * @form_widget: a #GtkFormWidget.
  * @show_error_dialog:
  * @set_focus:
@@ -794,7 +798,7 @@ gtk_form_clear (GtkForm *form)
  * if @widget isn't NULL, it is the first #GtkFormWidget empty.
  */
 gboolean
-gtk_form_check (GtkForm *form, GtkFormWidget **form_widget,
+gtk_form_check (GtkForm *form, gboolean with_key, GtkFormWidget **form_widget,
                 gboolean show_error_dialog, GtkWidget *parent_window,
                 gboolean set_focus)
 {
@@ -808,7 +812,6 @@ gtk_form_check (GtkForm *form, GtkFormWidget **form_widget,
        gboolean obl = FALSE;
 
        g_return_val_if_fail (IS_GTK_FORM (form), FALSE);
-       g_return_val_if_fail (form_widget == NULL || *form_widget == NULL, FALSE);
 
        if (show_error_dialog)
                {
@@ -834,34 +837,14 @@ gtk_form_check (GtkForm *form, GtkFormWidget **form_widget,
                                        fwidget = gtk_form_field_get_form_widget (field);
                                        widget = gtk_form_widget_get_widget (fwidget);
 
-                                       if (form_widget != NULL)
-                                               {
+                                       if (form_widget != NULL && *form_widget == NULL)
+                                               {                                               
                                                        *form_widget = fwidget;
                                                }
 
                                        if (show_error_dialog)
                                                {
-                                                       GtkWidget *dialog;
-                                                       GtkWidget *label;
-                                                       const gchar *label_text;
-
-                                                       label = gtk_form_widget_get_label (fwidget);
-                                                       if (GTK_IS_LABEL (label))
-                                                               {
-                                                                       label_text = gtk_label_get_text (GTK_LABEL (label));
-                                                               }
-                                                       else
-                                                               {
-                                                                       label_text = gtk_form_widget_get_widget_name (fwidget);
-                                                               }
-
-                                                       dialog = gtk_message_dialog_new (GTK_WINDOW (parent_window),
-                                                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
-                                                                                        GTK_MESSAGE_WARNING,
-                                                                                        GTK_BUTTONS_OK,
-                                                                                        "The field «%s» is obligatory.", label_text);
-                                                       gtk_dialog_run (GTK_DIALOG (dialog));
-                                                       gtk_widget_destroy (dialog);
+                                                       gtk_form_show_check_error_dialog (fwidget, parent_window);
                                                }
 
                                        if (set_focus)
@@ -874,9 +857,26 @@ gtk_form_check (GtkForm *form, GtkFormWidget **form_widget,
                        fields = g_slist_next (fields);
                }
 
-       if (ret && IS_GTK_FORM_KEY (priv->key))
+       if (with_key && ret && IS_GTK_FORM_KEY (priv->key))
                {
-                       ret = gtk_form_key_check (priv->key);
+                       GtkFormWidget *key_form_widget = NULL;
+
+                       ret = gtk_form_key_check (priv->key, &key_form_widget);
+
+                       if (!ret && form_widget != NULL && *form_widget == NULL && key_form_widget != NULL)
+                               {
+                                       *form_widget = key_form_widget;
+                               }
+
+                       if (!ret && key_form_widget != NULL && show_error_dialog)
+                               {
+                                       gtk_form_show_check_error_dialog (key_form_widget, parent_window);
+                               }
+
+                       if (!ret && key_form_widget != NULL && set_focus)
+                               {
+                                       gtk_widget_grab_focus (gtk_form_widget_get_widget (key_form_widget));
+                               }
                }
 
        return ret;
@@ -1160,3 +1160,29 @@ gtk_form_get_property (GObject *object,
                                break;
          }
 }
+
+static void
+gtk_form_show_check_error_dialog (GtkFormWidget *fwidget, GtkWidget *parent_window)
+{
+       GtkWidget *dialog;
+       GtkWidget *label;
+       const gchar *label_text;
+
+       label = gtk_form_widget_get_label (fwidget);
+       if (GTK_IS_LABEL (label))
+               {
+                       label_text = gtk_label_get_text (GTK_LABEL (label));
+               }
+       else
+               {
+                       label_text = gtk_form_widget_get_widget_name (fwidget);
+               }
+
+       dialog = gtk_message_dialog_new (GTK_WINDOW (parent_window),
+                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                        GTK_MESSAGE_WARNING,
+                                        GTK_BUTTONS_OK,
+                                        "The field «%s» is obligatory.", label_text);
+       gtk_dialog_run (GTK_DIALOG (dialog));
+       gtk_widget_destroy (dialog);
+}
index 672225f13ced0be5057307763573d20bd09ff197..63b632068466f8b33deabad447a4b031ddc006ba 100644 (file)
@@ -79,7 +79,8 @@ GObject **gtk_form_get_objects_by_name (GtkForm *form, ...);
 
 gboolean gtk_form_clear (GtkForm *form);
 
-gboolean gtk_form_check (GtkForm *form, GtkFormWidget **form_widget,
+gboolean gtk_form_check (GtkForm *form, gboolean with_key,
+                         GtkFormWidget **form_widget,
                          gboolean show_error_dialog, GtkWidget *parent_window,
                          gboolean set_focus);
 
index 6d3ab8722081ff80e3509131795a93c5b25040fd..0860e1a0411f4654543861adecda7f862760f26f 100644 (file)
--- a/src/key.c
+++ b/src/key.c
@@ -30,7 +30,7 @@ static void gtk_form_key_init (GtkFormKey *gtk_form_key);
 typedef struct _GtkFormKeyPrivate GtkFormKeyPrivate;
 struct _GtkFormKeyPrivate
        {
-               GList *fields;
+               GSList *fields;
        };
 
 
@@ -78,7 +78,7 @@ gtk_form_key_add_field (GtkFormKey *gtk_form_key, GtkFormField *field)
 
        g_return_val_if_fail (IS_GTK_FORM_FIELD (field), FALSE);
 
-       priv->fields = g_list_append (priv->fields, field);
+       priv->fields = g_slist_append (priv->fields, field);
 
        ret = TRUE;
 
@@ -100,7 +100,7 @@ gtk_form_key_remove_field (GtkFormKey *gtk_form_key, GtkFormField *field)
 
        g_return_val_if_fail (IS_GTK_FORM_FIELD (field), FALSE);
 
-       priv->fields = g_list_remove (priv->fields, field);
+       priv->fields = g_slist_remove (priv->fields, field);
 
        ret = TRUE;
 
@@ -132,18 +132,26 @@ gtk_form_key_add_fields (GtkFormKey *gtk_form_key, ...)
 /**
  * gtk_form_key_check:
  * @gtk_form_key: a #GtkFormKey object.
+ * @form_widget:
  *
  * Returns: TRUE if all obligatory fields are full.
  */
 gboolean
-gtk_form_key_check (GtkFormKey *gtk_form_key)
+gtk_form_key_check (GtkFormKey *gtk_form_key, GtkFormWidget **form_widget)
 {
        GtkFormField *field;
-       gboolean ret = FALSE;
+       gboolean ret;
 
-       GtkFormKeyPrivate *priv = GTK_FORM_KEY_GET_PRIVATE (gtk_form_key);
+       GtkFormKeyPrivate *priv;
+       GSList *fields;
+
+       g_return_val_if_fail (IS_GTK_FORM_KEY (gtk_form_key), FALSE);
+
+       priv = GTK_FORM_KEY_GET_PRIVATE (gtk_form_key);
+
+       ret = TRUE;
 
-       GList *fields = g_list_first (priv->fields);
+       fields = priv->fields;
 
        while (fields != NULL)
                {
@@ -151,11 +159,19 @@ gtk_form_key_check (GtkFormKey *gtk_form_key)
 
                        if (gtk_form_field_is_empty (field))
                                {
-                                       ret = TRUE;
+                                       GtkFormWidget *fwidget;
+
+                                       fwidget = gtk_form_field_get_form_widget (field);
+                                       if (form_widget != NULL && *form_widget == NULL)
+                                               {
+                                                       *form_widget = fwidget;
+                                               }
+
+                                       ret = FALSE;
                                        break;
                                }
 
-                       fields = g_list_next (fields);
+                       fields = g_slist_next (fields);
                }
 
        return ret;
@@ -173,11 +189,14 @@ gchar
        gchar *sql = "";
        gchar *field_name;
 
+       GtkFormKeyPrivate *priv;
+       GSList *fields;
+
        g_return_val_if_fail (IS_GTK_FORM_KEY (gtk_form_key), NULL);
 
-       GtkFormKeyPrivate *priv = GTK_FORM_KEY_GET_PRIVATE (gtk_form_key);
+       priv = GTK_FORM_KEY_GET_PRIVATE (gtk_form_key);
 
-       GList *fields = g_list_first (priv->fields);
+       fields = priv->fields;
 
        while (fields != NULL)
                {
@@ -190,7 +209,7 @@ gchar
                        sql = g_strconcat (sql, " AND ", field_name, " = ",
                                           gtk_form_field_get_value_sql (field), NULL);
 
-                       fields = g_list_next (fields);
+                       fields = g_slist_next (fields);
                }
 
        if (strcmp (sql, "") != 0)
@@ -211,10 +230,12 @@ gchar
 gboolean
 gtk_form_key_field_is_key (GtkFormKey *gtk_form_key, GtkFormField *field)
 {
+       GtkFormKeyPrivate *priv;
+
        g_return_val_if_fail (IS_GTK_FORM_KEY (gtk_form_key), FALSE);
        g_return_val_if_fail (IS_GTK_FORM_FIELD (field), FALSE);
 
-       GtkFormKeyPrivate *priv = GTK_FORM_KEY_GET_PRIVATE (gtk_form_key);
+       priv = GTK_FORM_KEY_GET_PRIVATE (gtk_form_key);
 
-       return (g_list_index (priv->fields, field) >= 0 ? TRUE : FALSE);
+       return (g_slist_index (priv->fields, field) >= 0 ? TRUE : FALSE);
 }
index 6de2300bd028c15be4ff2d82a8b837ffe36bde8c..144c2ecb548560bb95f3b22f27ef799258f88486 100644 (file)
--- a/src/key.h
+++ b/src/key.h
@@ -22,6 +22,7 @@
 #include <glib.h>
 
 #include "field.h"
+#include "widget.h"
 
 
 G_BEGIN_DECLS
@@ -58,7 +59,7 @@ gboolean gtk_form_key_remove_field (GtkFormKey *gtk_form_key, GtkFormField *fiel
 
 gboolean gtk_form_key_add_fields (GtkFormKey *gtk_form_key, ...);
 
-gboolean gtk_form_key_check (GtkFormKey *gtk_form_key);
+gboolean gtk_form_key_check (GtkFormKey *gtk_form_key, GtkFormWidget **form_widget);
 
 gchar *gtk_form_key_get_sql (GtkFormKey *gtk_form_key);
 
index f2e36382ec7a5bd18475a425fd58ca13898ce991..47c133711bae38dd44c7416ae12feb4da64ebe92 100644 (file)
@@ -53,7 +53,7 @@ void
 on_btnCheck_clicked (GtkButton *button,
                      gpointer user_data)
 {
-       if (!gtk_form_check (form, NULL, FALSE, NULL, FALSE))
+       if (!gtk_form_check (form, TRUE, NULL, FALSE, NULL, FALSE))
                {
                        GtkWidget *diag = gtk_message_dialog_new (GTK_WINDOW (w),
                                                                  GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
index 9c572aa972294fd5006a3cae9b6e02b638caedff..ebd0c879f713d861740ecfd67314830c7ea29bf5 100644 (file)
@@ -67,7 +67,7 @@ void
 on_btnCheck_clicked (GtkButton *button,
                      gpointer user_data)
 {
-       if (!gtk_form_check (form, NULL, FALSE, NULL, FALSE))
+       if (!gtk_form_check (form, TRUE, NULL, FALSE, NULL, FALSE))
                {
                        GtkWidget *diag = gtk_message_dialog_new (GTK_WINDOW (w),
                                                                  GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,