From: Andrea Zagli Date: Fri, 30 Sep 2011 07:57:13 +0000 (+0200) Subject: Added check on validity of float and integer fields. X-Git-Tag: 0.4.0~6 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=c56845315596fcddce110b108682cf3e22dbab8c;p=libgtkform Added check on validity of float and integer fields. Setted default decimals to 2 for float fields. --- diff --git a/libgtkform/fieldfloat.c b/libgtkform/fieldfloat.c index 19379ef..3f0ec29 100644 --- a/libgtkform/fieldfloat.c +++ b/libgtkform/fieldfloat.c @@ -96,7 +96,7 @@ gtk_form_field_float_class_init (GtkFormFieldFloatClass *klass) g_param_spec_uint ("decimals", "Decimals", "Number of decimals to show", - 0, G_MAXUINT, 0, + 0, G_MAXUINT, 2, G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); g_type_class_add_private (object_class, sizeof (GtkFormFieldFloatPrivate)); @@ -340,6 +340,7 @@ gtk_form_field_float_is_valid (GtkFormField *field) gboolean ret; GtkFormFieldFloatPrivate *priv; + GtkFormWidget *fw; gdouble value; @@ -354,6 +355,38 @@ gtk_form_field_float_is_valid (GtkFormField *field) value = g_strtod (gtk_form_field_float_get_value_stringify (field), NULL); + g_object_get (field, + "form-widget", &fw, + NULL); + + if (IS_GTK_FORM_WIDGET (fw)) + { + gdouble dbl_value; + gchar *str_value; + gchar *str_comma; + gchar *str_format; + + str_value = gtk_form_widget_get_value_stringify (fw); + dbl_value = g_strtod (str_value, NULL); + str_comma = g_strstr_len (str_value, -1, ","); + + if (str_value != NULL && g_strcmp0 (g_strstrip (str_value), "") != 0) + { + str_format = g_strdup_printf ("%%0%dd", 6 - (str_comma != NULL ? strlen (str_comma) - 1 : 0)); + if (g_strcmp0 (g_strdup_printf ("%f", dbl_value), g_strdup_printf (g_strconcat ("%s", (str_comma == NULL ? "," : ""), str_format, NULL), str_value, 0)) != 0) + { + g_free (str_value); + g_free (str_comma); + g_free (str_format); + return FALSE; + } + + g_free (str_value); + g_free (str_comma); + g_free (str_format); + } + } + min_value = NULL; max_value = NULL; diff --git a/libgtkform/fieldinteger.c b/libgtkform/fieldinteger.c index 00c1260..0d3519a 100644 --- a/libgtkform/fieldinteger.c +++ b/libgtkform/fieldinteger.c @@ -287,6 +287,7 @@ gtk_form_field_integer_is_valid (GtkFormField *field) gboolean ret; GtkFormFieldIntegerPrivate *priv; + GtkFormWidget *fw; gint value; @@ -301,6 +302,27 @@ gtk_form_field_integer_is_valid (GtkFormField *field) value = strtol (gtk_form_field_integer_get_value_stringify (field), NULL, 10); + g_object_get (field, + "form-widget", &fw, + NULL); + + if (IS_GTK_FORM_WIDGET (fw)) + { + gchar *str_value; + + str_value = gtk_form_widget_get_value_stringify (fw); + + if (str_value != NULL && g_strcmp0 (g_strstrip (str_value), "") != 0) + { + if (g_strcmp0 (g_strdup_printf ("%d", value), str_value) != 0) + { + g_free (str_value); + return FALSE; + } + g_free (str_value); + } + } + min_value = NULL; max_value = NULL;