]> saetta.ns0.it Git - libgtkform/commitdiff
Migrated GtkFormFieldDateTime to GDateTime. gdatetime
authorAndrea Zagli <azagli@libero.it>
Fri, 9 Sep 2011 10:08:55 +0000 (12:08 +0200)
committerAndrea Zagli <azagli@libero.it>
Fri, 9 Sep 2011 10:08:55 +0000 (12:08 +0200)
closes #9

libgtkform/fielddatetime.c
libgtkform/form.c
test/test.xml
test/test_db.xml

index 6cb31577689585c5f0f2aa88c6c7d16c7a9dd807..8821b57ccb13ef22fb14e6df5a57ec08d879bafb 100644 (file)
@@ -325,11 +325,9 @@ gtk_form_field_datetime_clear (GtkFormField *field)
 gboolean
 gtk_form_field_datetime_is_empty (GtkFormField *field)
 {
-       gboolean ret;
-
        g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), TRUE);
 
-       return !gtk_form_field_datetime_is_valid_datetime (GTK_FORM_FIELD_DATETIME (field));
+       return (g_strcmp0 (gtk_form_field_datetime_get_value_stringify (GTK_FORM_FIELD (field)), "") == 0);
 }
 
 /**
@@ -386,31 +384,32 @@ gtk_form_field_datetime_is_valid (GtkFormField *field)
 
        GtkFormFieldDateTimePrivate *priv;
 
+       gchar *str_value;
        GDateTime *gdatetime;
 
-       struct tm *tm_value;
-
        gchar *min_value;
-       struct tm *tm_min_value;
+       GDateTime *gdt_min_value;
        gchar *max_value;
-       struct tm *tm_max_value;
+       GDateTime *gdt_max_value;
+       GDateTime *gdt_tmp;
 
        g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), FALSE);
 
        priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (GTK_FORM_FIELD_DATETIME (field));
 
-       gdatetime = gtk_form_field_datetime_get_gdatetime_from_str (gtk_form_field_datetime_get_value_stringify (field), priv->display_format);
-       if (gdatetime == NULL)
+       ret = FALSE;
+
+       str_value = gtk_form_field_datetime_get_value_stringify (field);
+       if (g_strcmp0 (str_value, "") == 0)
                {
-                       return FALSE;
+                       return TRUE;
                }
 
-       ret = TRUE;
-
-       tm_value = gtk_form_field_datetime_get_tm_from_str (gtk_form_field_datetime_get_value_stringify (field), priv->display_format);
+       gdatetime = gtk_form_field_datetime_get_gdatetime_from_str (str_value, priv->display_format);
 
-       if (tm_value != NULL)
+       if (gdatetime != NULL)
                {
+                       ret = TRUE;
                        min_value = NULL;
                        max_value = NULL;
 
@@ -424,41 +423,36 @@ gtk_form_field_datetime_is_valid (GtkFormField *field)
                                {
                                        if (g_strcasecmp (min_value, "now") == 0)
                                                {
-                                                       time_t tt;
-                                                       tt = time (NULL);
-
-                                                       tm_min_value = g_memdup (localtime (&tt), sizeof (struct tm));
+                                                       gdt_tmp = g_date_time_new_now_local ();
 
                                                        if (priv->type == GTK_FORM_FIELD_DATETIME_TYPE_DATE)
                                                                {
-                                                                       tm_min_value->tm_hour = 0;
-                                                                       tm_min_value->tm_min = 0;
-                                                                       tm_min_value->tm_sec = 0;
+                                                                       gdt_min_value = g_date_time_new_local (g_date_time_get_year (gdt_tmp),
+                                                                                                              g_date_time_get_month (gdt_tmp),
+                                                                                                              g_date_time_get_day_of_month (gdt_tmp),
+                                                                                                              0, 0, 0.0);
                                                                }
                                                        else if (priv->type == GTK_FORM_FIELD_DATETIME_TYPE_TIME)
                                                                {
-                                                                       tm_min_value->tm_year = 0;
-                                                                       tm_min_value->tm_mon = 0;
-                                                                       tm_min_value->tm_mday = 0;
+                                                                       gdt_min_value = g_date_time_new_local (1, 1, 1,
+                                                                                                              g_date_time_get_hour (gdt_tmp),
+                                                                                                              g_date_time_get_minute (gdt_tmp),
+                                                                                                              g_date_time_get_seconds (gdt_tmp));
                                                                }
+                                                       gdt_min_value = g_date_time_ref (gdt_tmp);
+                                                       g_date_time_unref (gdt_tmp);
                                                }
                                        else
                                                {
-                                                       tm_min_value = gtk_form_field_datetime_get_tm_from_str (min_value,
+                                                       gdt_min_value = gtk_form_field_datetime_get_gdatetime_from_str (min_value,
                                                               gtk_form_field_datetime_get_str_format (GTK_FORM_FIELD_DATETIME (field)));
                                                }
 
-                                       if (tm_min_value != NULL)
+                                       if (gdt_min_value != NULL)
                                                {
-                                                       time_t tt_min_value;
-                                                       time_t tt_value;
-
-                                                       tt_min_value = mktime (tm_min_value);
-                                                       tt_value = mktime (tm_value);
-
-                                                       ret = (difftime (tt_min_value, tt_value) <= 0);
+                                                       ret = (g_date_time_compare (gdatetime, gdt_min_value) >= 0);
 
-                                                       g_free (tm_min_value);
+                                                       g_date_time_unref (gdt_min_value);
                                                }
                                }
 
@@ -468,45 +462,40 @@ gtk_form_field_datetime_is_valid (GtkFormField *field)
                                {
                                        if (g_strcasecmp (max_value, "now") == 0)
                                                {
-                                                       time_t tt;
-                                                       tt = time (NULL);
-
-                                                       tm_max_value = g_memdup (localtime (&tt), sizeof (struct tm));
+                                                       gdt_tmp = g_date_time_new_now_local ();
 
                                                        if (priv->type == GTK_FORM_FIELD_DATETIME_TYPE_DATE)
                                                                {
-                                                                       tm_max_value->tm_hour = 0;
-                                                                       tm_max_value->tm_min = 0;
-                                                                       tm_max_value->tm_sec = 0;
+                                                                       gdt_max_value = g_date_time_new_local (g_date_time_get_year (gdt_tmp),
+                                                                                                              g_date_time_get_month (gdt_tmp),
+                                                                                                              g_date_time_get_day_of_month (gdt_tmp),
+                                                                                                              0, 0, 0.0);
                                                                }
                                                        else if (priv->type == GTK_FORM_FIELD_DATETIME_TYPE_TIME)
                                                                {
-                                                                       tm_max_value->tm_year = 0;
-                                                                       tm_max_value->tm_mon = 0;
-                                                                       tm_max_value->tm_mday = 0;
+                                                                       gdt_max_value = g_date_time_new_local (1, 1, 1,
+                                                                                                              g_date_time_get_hour (gdt_tmp),
+                                                                                                              g_date_time_get_minute (gdt_tmp),
+                                                                                                              g_date_time_get_seconds (gdt_tmp));
                                                                }
+                                                       gdt_max_value = g_date_time_ref (gdt_tmp);
+                                                       g_date_time_unref (gdt_tmp);
                                                }
                                        else
                                                {
-                                                       tm_max_value = gtk_form_field_datetime_get_tm_from_str (max_value,
+                                                       gdt_max_value = gtk_form_field_datetime_get_gdatetime_from_str (max_value,
                                                               gtk_form_field_datetime_get_str_format (GTK_FORM_FIELD_DATETIME (field)));
                                                }
 
-                                       if (tm_max_value != NULL)
+                                       if (gdt_max_value != NULL)
                                                {
-                                                       time_t tt_max_value;
-                                                       time_t tt_value;
-
-                                                       tt_max_value = mktime (tm_max_value);
-                                                       tt_value = mktime (tm_value);
-
-                                                       ret = (difftime (tt_max_value, tt_value) >= 0);
+                                                       ret = (g_date_time_compare (gdatetime, gdt_max_value) <= 0);
 
-                                                       g_free (tm_max_value);
+                                                       g_date_time_unref (gdt_max_value);
                                                }
                                }
 
-                       g_free (tm_value);
+                       g_date_time_unref (gdatetime);
                }
 
        return ret;
index 5026acabdf479b45c92b2aa796d9b56fe48f5c85..a6354286496f916409f04a821c3c765c3ee52bce 100644 (file)
@@ -1568,7 +1568,7 @@ gtk_form_check (GtkForm *form, gboolean with_key, GtkFormWidget **form_widget,
                                        if (show_error_dialog)
                                                {
                                                        gtk_form_show_check_error_dialog (fwidget, parent_window,
-                                                                                         obl && is_empty ? _(" is obligatory") : _(" isn't valid"));
+                                                                                         !is_valid ? _(" isn't valid") : _(" is obligatory"));
                                                }
 
                                        if (set_focus)
index 1325c1a0fae7978f760e82dbf5fbbd07518bca95..199d95a544e0523f84add045cf6bfa880f775358 100644 (file)
@@ -33,6 +33,7 @@
        <widget type="spin" name="spnAmount">
                <field type="float" name="amount">
                        <default>12.45</default>
+                       <decimals>2</decimals>
                </field>
        </widget>
 
index f50f0345e2ca11741502e6f02331cdcf20d1d31b..b59c79c816f19205a42d88606166a7b11baed08c 100644 (file)
@@ -45,6 +45,7 @@
        <widget type="entry" name="txt_birthday" label="label11">
                <field type="datetime" name="birthday">
                        <obligatory>t</obligatory>
+                       <min-value>1900-01-01</min-value>
                        <max-value>now</max-value>
                        <datetime-type>date</datetime-type>
                        <display-format>%d/%m/%Y</display-format>