From: Andrea Zagli Date: Fri, 15 Nov 2013 12:18:41 +0000 (+0100) Subject: Now max and min value can be a field name. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=382c766dd5e34d679d49401a9e2d414954e34cfb;p=libgtkform Now max and min value can be a field name. --- diff --git a/libgtkform/fielddatetime.c b/libgtkform/fielddatetime.c index e3e7d61..048a501 100644 --- a/libgtkform/fielddatetime.c +++ b/libgtkform/fielddatetime.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2012 Andrea Zagli + * Copyright (C) 2005-2013 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -27,6 +27,7 @@ #include #include +#include "form.h" #include "widget.h" #include "fielddatetime.h" @@ -449,6 +450,30 @@ gtk_form_field_datetime_is_valid (GtkFormField *field) } g_date_time_unref (gdt_tmp); } + else if (min_value[0] == '[' && min_value[strlen (min_value) - 1] == ']') + { + /* is a widget name */ + GtkFormWidget *fw; + GtkForm *form; + + gchar *wname; + const gchar *str; + gchar *display_format; + + g_object_get (field, + "form-widget", &fw, + NULL); + + g_object_get (fw, + "form", &form, + NULL); + + wname = g_strndup (min_value + 1, strlen (min_value) - 2); + str = gtk_form_get_form_widget_value_stringify_by_name (form, (const gchar *)wname); + g_free (wname); + + gdt_min_value = gtk_form_field_datetime_get_gdatetime_from_str (str, priv->display_format); + } else { gdt_min_value = gtk_form_field_datetime_get_gdatetime_from_str (min_value, @@ -493,6 +518,29 @@ gtk_form_field_datetime_is_valid (GtkFormField *field) } g_date_time_unref (gdt_tmp); } + else if (max_value[0] == '[' && max_value[strlen (max_value) - 1] == ']') + { + /* is a widget name */ + GtkFormWidget *fw; + GtkForm *form; + + gchar *wname; + const gchar *str; + + g_object_get (field, + "form-widget", &fw, + NULL); + + g_object_get (fw, + "form", &form, + NULL); + + wname = g_strndup (max_value + 1, strlen (max_value) - 2); + str = gtk_form_get_form_widget_value_stringify_by_name (form, (const gchar *)wname); + g_free (wname); + + gdt_max_value = gtk_form_field_datetime_get_gdatetime_from_str (str, priv->display_format); + } else { gdt_max_value = gtk_form_field_datetime_get_gdatetime_from_str (max_value, @@ -1040,9 +1088,9 @@ GDateTime if (datetime != NULL) { - ret = g_date_time_new_local (datetime->tm_mday, + ret = g_date_time_new_local (datetime->tm_year + 1900, datetime->tm_mon + 1, - datetime->tm_year + 1900, + datetime->tm_mday, datetime->tm_hour, datetime->tm_min, (gdouble)datetime->tm_sec); diff --git a/libgtkform/fieldfloat.c b/libgtkform/fieldfloat.c index 4379104..2fb54d5 100644 --- a/libgtkform/fieldfloat.c +++ b/libgtkform/fieldfloat.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2012 Andrea Zagli + * Copyright (C) 2005-2013 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 "form.h" #include "widget.h" #include "fieldfloat.h" @@ -418,14 +419,66 @@ gtk_form_field_float_is_valid (GtkFormField *field) if (min_value != NULL && g_strcmp0 (min_value, "") != 0) { - ret = (g_strtod (min_value, NULL) <= value); + if (min_value[0] == '[' && min_value[strlen (min_value) - 1] == ']') + { + /* is a widget name */ + GtkFormWidget *fw; + GtkForm *form; + + gchar *wname; + const gchar *str; + + g_object_get (field, + "form-widget", &fw, + NULL); + + g_object_get (fw, + "form", &form, + NULL); + + wname = g_strndup (min_value + 1, strlen (min_value) - 2); + str = gtk_form_get_form_widget_value_stringify_by_name (form, (const gchar *)wname); + g_free (wname); + + ret = (g_strtod (str, NULL) <= value); + } + else + { + ret = (g_strtod (min_value, NULL) <= value); + } } if (ret && max_value != NULL && g_strcmp0 (max_value, "") != 0) { - ret = (g_strtod (max_value, NULL) >= value); + if (min_value[0] == '[' && min_value[strlen (min_value) - 1] == ']') + { + /* is a widget name */ + GtkFormWidget *fw; + GtkForm *form; + + gchar *wname; + const gchar *str; + + g_object_get (field, + "form-widget", &fw, + NULL); + + g_object_get (fw, + "form", &form, + NULL); + + wname = g_strndup (min_value + 1, strlen (min_value) - 2); + str = gtk_form_get_form_widget_value_stringify_by_name (form, (const gchar *)wname); + g_free (wname); + + ret = (g_strtod (str, NULL) >= value); + } + else + { + ret = (g_strtod (max_value, NULL) >= value); + } } return ret; diff --git a/libgtkform/fieldinteger.c b/libgtkform/fieldinteger.c index ccd9552..46a7570 100644 --- a/libgtkform/fieldinteger.c +++ b/libgtkform/fieldinteger.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2012 Andrea Zagli + * Copyright (C) 2005-2013 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 "form.h" #include "widget.h" #include "widgetspin.h" #include "fieldinteger.h" @@ -320,8 +321,7 @@ gtk_form_field_integer_is_valid (GtkFormField *field) if (g_strcmp0 (g_strconcat (g_strdup_printf ("%d", value), IS_GTK_FORM_WIDGET_SPIN (fw) ? ",000000" : "", NULL), str_value) != 0) - {g_message ("%s %s", g_strconcat (g_strdup_printf ("%d", value), - IS_GTK_FORM_WIDGET_SPIN (fw) ? ",000000" : "", NULL), str_value); + { g_free (str_value); return FALSE; } @@ -340,14 +340,66 @@ gtk_form_field_integer_is_valid (GtkFormField *field) if (min_value != NULL && g_strcmp0 (min_value, "") != 0) { - ret = (strtol (min_value, NULL, 10) <= value); + if (min_value[0] == '[' && min_value[strlen (min_value) - 1] == ']') + { + /* is a widget name */ + GtkFormWidget *fw; + GtkForm *form; + + gchar *wname; + const gchar *str; + + g_object_get (field, + "form-widget", &fw, + NULL); + + g_object_get (fw, + "form", &form, + NULL); + + wname = g_strndup (min_value + 1, strlen (min_value) - 2); + str = gtk_form_get_form_widget_value_stringify_by_name (form, (const gchar *)wname); + g_free (wname); + + ret = (strtol (str, NULL, 10) <= value); + } + else + { + ret = (strtol (min_value, NULL, 10) <= value); + } } if (ret && max_value != NULL && g_strcmp0 (max_value, "") != 0) { - ret = (strtol (max_value, NULL, 10) >= value); + if (max_value[0] == '[' && max_value[strlen (max_value) - 1] == ']') + { + /* is a widget name */ + GtkFormWidget *fw; + GtkForm *form; + + gchar *wname; + const gchar *str; + + g_object_get (field, + "form-widget", &fw, + NULL); + + g_object_get (fw, + "form", &form, + NULL); + + wname = g_strndup (max_value + 1, strlen (max_value) - 2); + str = gtk_form_get_form_widget_value_stringify_by_name (form, (const gchar *)wname); + g_free (wname); + + ret = (strtol (str, NULL, 10) >= value); + } + else + { + ret = (strtol (max_value, NULL, 10) >= value); + } } return ret; diff --git a/test/main.c b/test/main.c index 670915d..4650248 100644 --- a/test/main.c +++ b/test/main.c @@ -406,13 +406,7 @@ main (int argc, char **argv) "field", "now", "form-widget", wlabel, "type", GTK_FORM_FIELD_DATETIME_TYPE_DATE, - "default", g_strdup_printf ("%04d-%02d-%02d %02d:%02d:%02d", - now->tm_year + 1900, - now->tm_mon + 1, - now->tm_mday, - now->tm_hour, - now->tm_min, - now->tm_sec), + "default", gtk_form_field_datetime_get_gdatetime_from_tm (now), "display-format", "%d/%m/%Y", NULL); gtk_form_add_field (form, fdatetime);