]> saetta.ns0.it Git - zakform/gtk/commitdiff
Added ZakFormGtkForm::is_valid.
authorAndrea Zagli <azagli@libero.it>
Sat, 5 Dec 2015 10:39:04 +0000 (11:39 +0100)
committerAndrea Zagli <azagli@libero.it>
Sat, 5 Dec 2015 10:39:04 +0000 (11:39 +0100)
src/form.c
src/form.h

index 64496825db36bdd29bdc1a770089714a0721ab67..8cef05968df72a2346576636bf119d6dff1a728d 100644 (file)
@@ -47,6 +47,7 @@ static void zak_form_gtk_form_element_added (ZakFormGtkForm *form, ZakFormGtkFor
 typedef struct _ZakFormGtkFormPrivate ZakFormGtkFormPrivate;
 struct _ZakFormGtkFormPrivate
        {
+               GPtrArray *ar_elements;
                GtkBuilder *builder;;
        };
 
@@ -73,6 +74,7 @@ zak_form_gtk_form_init (ZakFormGtkForm *zak_form_gtk_form)
 {
        ZakFormGtkFormPrivate *priv = ZAK_FORM_GTK_FORM_GET_PRIVATE (zak_form_gtk_form);
 
+       priv->ar_elements = ZAK_FORM_FORM_CLASS (zak_form_gtk_form_parent_class)->get_elements (ZAK_FORM_FORM (zak_form_gtk_form));
        priv->builder = NULL;
 }
 
@@ -110,6 +112,74 @@ zak_form_gtk_form_set_gtkbuilder (ZakFormGtkForm *form, GtkBuilder *builder)
        priv->builder = builder;
 }
 
+/**
+ * zak_form_gtk_form_is_valid:
+ * @form:
+ * @parent_window:
+ *
+ */
+gboolean
+zak_form_gtk_form_is_valid (ZakFormGtkForm *form, GtkWidget *parent_window)
+{
+       gboolean ret;
+
+       guint i;
+       GString *str;
+       guint m;
+
+       GtkWidget *w_to_focus;
+
+       g_return_val_if_fail (parent_window != NULL ? GTK_IS_WINDOW (parent_window) : TRUE, FALSE);
+
+       ZakFormGtkFormPrivate *priv = ZAK_FORM_GTK_FORM_GET_PRIVATE (form);
+
+       ret = zak_form_form_is_valid (ZAK_FORM_FORM (form));
+
+       if (!ret)
+               {
+                       w_to_focus = NULL;
+                       str = g_string_new ("");
+
+                       /* collect error messages */
+                       for (i = 0; i < priv->ar_elements->len; i++)
+                               {
+                                       GPtrArray *ar_messages = zak_form_element_get_messages ((ZakFormElement *)g_ptr_array_index (priv->ar_elements, i));
+                                       if (ar_messages != NULL)
+                                               {
+                                                       if (w_to_focus == NULL)
+                                                               {
+                                                                       w_to_focus = zak_form_gtk_form_element_get_widget (ZAK_FORM_GTK_FORM_ELEMENT ((ZakFormElement *)g_ptr_array_index (priv->ar_elements, i)));
+                                                               }
+
+                                                       for (m = 0; m < ar_messages->len; m++)
+                                                               {
+                                                                       g_string_append_printf (str, "\n - %s", (gchar *)g_ptr_array_index (ar_messages, m));
+                                                               }
+                                               }
+                               }
+
+                       if (g_strcmp0 (str->str, "") != 0)
+                               {
+                                       GtkWidget *dialog;
+
+                                       dialog = gtk_message_dialog_new (GTK_WINDOW (parent_window),
+                                                                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                                                                        GTK_MESSAGE_WARNING,
+                                                                                                        GTK_BUTTONS_OK,
+                                                                                                        str->str);
+                                       gtk_dialog_run (GTK_DIALOG (dialog));
+                                       gtk_widget_destroy (dialog);
+
+                                       if (w_to_focus != NULL)
+                                               {
+                                                       gtk_widget_grab_focus (w_to_focus);
+                                               }
+                               }
+               }
+
+       return ret;
+}
+
 /* PRIVATE */
 static void
 zak_form_gtk_form_set_property (GObject *object,
index 56ee14dffa5f1e43108e152d600b767787677106..381251b9a3d1d1e6b389fcf16058ff2906451dbb 100644 (file)
@@ -43,6 +43,8 @@ ZakFormGtkForm *zak_form_gtk_form_new (void);
 
 void zak_form_gtk_form_set_gtkbuilder (ZakFormGtkForm *form, GtkBuilder *builder);
 
+gboolean zak_form_gtk_form_is_valid (ZakFormGtkForm *form, GtkWidget *parent_window);
+
 
 G_END_DECLS