From 1835a7761e06210215bf2f049901ed059dd07260 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 9 Oct 2010 20:59:50 +0200 Subject: [PATCH] Added functions GtkForm::reset and GtkForm::get_original_values_as_hashtable, GtkFormField::get_original_value and GtkFormField::set_value and each corresponding functions in every fields type. --- src/field.c | 45 +++++++++++++++++++++++ src/field.h | 4 +++ src/fieldboolean.c | 58 ++++++++++++++++++++++++++++++ src/fieldboolean.h | 2 ++ src/fielddatetime.c | 56 +++++++++++++++++++++++++++++ src/fielddatetime.h | 2 ++ src/fieldfloat.c | 47 ++++++++++++++++++++++++ src/fieldfloat.h | 2 ++ src/fieldinteger.c | 50 +++++++++++++++++++++++++- src/fieldinteger.h | 2 ++ src/fieldtext.c | 46 ++++++++++++++++++++++++ src/fieldtext.h | 2 ++ src/form.c | 79 +++++++++++++++++++++++++++++++++++++++++ src/form.h | 3 ++ test/from_xml_with_db.c | 11 +++++- test/test_db.gui | 15 ++++++++ 16 files changed, 422 insertions(+), 2 deletions(-) diff --git a/src/field.c b/src/field.c index 449bdfd..4500e24 100644 --- a/src/field.c +++ b/src/field.c @@ -261,6 +261,30 @@ gchar return ret; } +/** + * gtk_form_field_get_origin_value: + * @field: a #GtkFormField object. + * + */ +GValue +*gtk_form_field_get_original_value (GtkFormField *field) +{ + GValue *ret; + + ret = NULL; + + if (IS_GTK_FORM_FIELD (field) && GTK_FORM_FIELD_GET_CLASS (field)->get_original_value != NULL) + { + ret = GTK_FORM_FIELD_GET_CLASS (field)->get_original_value (field); + if (G_VALUE_HOLDS_STRING (ret)) + { + g_value_set_string (ret, g_strstrip (g_strdup (g_value_get_string (ret)))); + } + } + + return ret; +} + /** * gtk_form_field_clear: * @field: a #GtkFormField object. @@ -434,6 +458,27 @@ gtk_form_field_set_from_hashtable (GtkFormField *field, GHashTable *hashtable) return ret; } +/** + * gtk_form_field_set_value: + * @field: a #GtkFormField object. + * @gvalue: a #GValue. + * + */ +gboolean +gtk_form_field_set_value (GtkFormField *field, GValue *gvalue) +{ + gboolean ret = FALSE; + + GtkFormFieldPrivate *priv = GTK_FORM_FIELD_GET_PRIVATE (field); + + if (IS_GTK_FORM_FIELD (field) && GTK_FORM_FIELD_GET_CLASS (field)->set_value != NULL) + { + ret = GTK_FORM_FIELD_GET_CLASS (field)->set_value (field, gvalue); + } + + return ret; +} + /* PRIVATE */ static void gtk_form_field_set_property (GObject *object, diff --git a/src/field.h b/src/field.h index 6c129bf..2b7bf99 100644 --- a/src/field.h +++ b/src/field.h @@ -52,6 +52,7 @@ struct _GtkFormFieldClass gchar *(*get_value_stringify) (GtkFormField *field); GValue *(*get_value) (GtkFormField *field); gchar *(*get_value_sql) (GtkFormField *field); + GValue *(*get_original_value) (GtkFormField *field); gboolean (*clear) (GtkFormField *field); gboolean (*is_empty) (GtkFormField *field); @@ -59,6 +60,7 @@ struct _GtkFormFieldClass void (*set_as_origin) (GtkFormField *field); gboolean (*set_from_datamodel) (GtkFormField *field, GdaDataModel *dm, gint row); gboolean (*set_from_hashtable) (GtkFormField *field, GHashTable *hashtable); + gboolean (*set_value) (GtkFormField *field, GValue *gvalue); }; GType gtk_form_field_get_type (void) G_GNUC_CONST; @@ -72,6 +74,7 @@ GtkFormWidget *gtk_form_field_get_form_widget (GtkFormField *field); gchar *gtk_form_field_get_value_stringify (GtkFormField *field); GValue *gtk_form_field_get_value (GtkFormField *field); gchar *gtk_form_field_get_value_sql (GtkFormField *field); +GValue *gtk_form_field_get_original_value (GtkFormField *field); gboolean gtk_form_field_clear (GtkFormField *field); @@ -87,6 +90,7 @@ void gtk_form_field_set_as_origin (GtkFormField *field); gboolean gtk_form_field_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, gint row); gboolean gtk_form_field_set_from_hashtable (GtkFormField *field, GHashTable *hashtable); +gboolean gtk_form_field_set_value (GtkFormField *field, GValue *gvalue); G_END_DECLS diff --git a/src/fieldboolean.c b/src/fieldboolean.c index 47a23ae..d38d706 100644 --- a/src/fieldboolean.c +++ b/src/fieldboolean.c @@ -71,12 +71,14 @@ gtk_form_field_boolean_class_init (GtkFormFieldBooleanClass *klass) field_class->get_value_stringify = gtk_form_field_boolean_get_value_stringify; field_class->get_value = gtk_form_field_boolean_get_value; field_class->get_value_sql = gtk_form_field_boolean_get_value_sql; + field_class->get_original_value = gtk_form_field_boolean_get_original_value; field_class->clear = gtk_form_field_boolean_clear; field_class->is_empty = gtk_form_field_boolean_is_empty; field_class->is_changed = gtk_form_filed_boolean_is_changed; field_class->set_as_origin = gtk_form_field_boolean_set_as_origin; field_class->set_from_datamodel = gtk_form_field_boolean_set_from_datamodel; field_class->set_from_hashtable = gtk_form_field_boolean_set_from_hashtable; + field_class->set_value = gtk_form_field_boolean_set_value; g_object_class_install_property (object_class, PROP_DEFAULT, g_param_spec_boolean ("default", @@ -191,6 +193,30 @@ gchar return ret; } +/** + * gtk_form_field_boolean_get_original_value: + * @field: a #GtkFormField object. + * + */ +GValue +*gtk_form_field_boolean_get_original_value (GtkFormField *field) +{ + GValue *ret; + + g_return_val_if_fail (IS_GTK_FORM_FIELD_BOOLEAN (field), NULL); + + GtkFormFieldBooleanPrivate *priv = GTK_FORM_FIELD_BOOLEAN_GET_PRIVATE (field); + + ret = NULL; + + ret = g_malloc0 (sizeof (GValue)); + g_value_init (ret, G_TYPE_BOOLEAN); + g_value_set_boolean (ret, priv->original_value + ); + + return ret; +} + /** * gtk_form_field_boolean_clear: * @field: a #GtkFormField object. @@ -376,6 +402,38 @@ gtk_form_field_boolean_set_from_hashtable (GtkFormField *field, GHashTable *hash return ret; } +/** + * gtk_form_field_boolean_set_value: + * @field: a #GtkFormField object. + * @gvalue: a #GValue. + * + */ +gboolean +gtk_form_field_boolean_set_value (GtkFormField *field, GValue *gvalue) +{ + gboolean ret = FALSE; + GtkFormFieldBooleanPrivate *priv; + gchar *value; + + g_return_val_if_fail (IS_GTK_FORM_FIELD_BOOLEAN (field), FALSE); + g_return_val_if_fail (G_VALUE_HOLDS_BOOLEAN (gvalue), FALSE); + + priv = GTK_FORM_FIELD_BOOLEAN_GET_PRIVATE (GTK_FORM_FIELD_BOOLEAN (field)); + + priv->original_value = g_value_get_boolean (gvalue); + if (priv->original_value) + { + value = g_strdup_printf ("TRUE"); + } + else + { + value = g_strdup_printf ("FALSE"); + } + ret = gtk_form_field_boolean_set_value_stringify (field, value); + + return ret; +} + /** * gtk_form_field_boolean_str_to_boolean: * @value: diff --git a/src/fieldboolean.h b/src/fieldboolean.h index 7485d6d..303be5b 100644 --- a/src/fieldboolean.h +++ b/src/fieldboolean.h @@ -55,6 +55,7 @@ GtkFormField *gtk_form_field_boolean_new (void); gchar *gtk_form_field_boolean_get_value_stringify (GtkFormField *field); GValue *gtk_form_field_boolean_get_value (GtkFormField *field); gchar *gtk_form_field_boolean_get_value_sql (GtkFormField *field); +GValue *gtk_form_field_boolean_get_original_value (GtkFormField *field); gboolean gtk_form_field_boolean_clear (GtkFormField *field); @@ -65,6 +66,7 @@ void gtk_form_field_boolean_set_as_origin (GtkFormField *field); gboolean gtk_form_field_boolean_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, gint row); gboolean gtk_form_field_boolean_set_from_hashtable (GtkFormField *field, GHashTable *hashtable); +gboolean gtk_form_field_boolean_set_value (GtkFormField *field, GValue *gvalue); gboolean gtk_form_field_boolean_str_to_boolean (const gchar *value); diff --git a/src/fielddatetime.c b/src/fielddatetime.c index 4ce5139..c7108f1 100644 --- a/src/fielddatetime.c +++ b/src/fielddatetime.c @@ -87,12 +87,14 @@ gtk_form_field_datetime_class_init (GtkFormFieldDateTimeClass *klass) field_class->get_value_stringify = gtk_form_field_datetime_get_value_stringify; field_class->get_value = gtk_form_field_datetime_get_value; field_class->get_value_sql = gtk_form_field_datetime_get_value_sql; + field_class->get_original_value = gtk_form_field_datetime_get_original_value; field_class->clear = gtk_form_field_datetime_clear; field_class->is_empty = gtk_form_field_datetime_is_empty; field_class->is_changed = gtk_form_field_datetime_is_changed; field_class->set_as_origin = gtk_form_field_datetime_set_as_origin; field_class->set_from_datamodel = gtk_form_field_datetime_set_from_datamodel; field_class->set_from_hashtable = gtk_form_field_datetime_set_from_hashtable; + field_class->set_value = gtk_form_field_datetime_set_value; g_object_class_install_property (object_class, PROP_DEFAULT, g_param_spec_pointer ("default", @@ -247,6 +249,31 @@ gchar return ret; } +/** + * gtk_form_field_datetime_get_original_value: + * @field: a #GtkFormField object. + * + * Returns: a #GValue of type G_TYPE_POINTER that point to a struct tm. + */ +GValue +*gtk_form_field_datetime_get_original_value (GtkFormField *field) +{ + GValue *ret; + GtkFormFieldDateTimePrivate *priv; + + g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), NULL); + + priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (GTK_FORM_FIELD_DATETIME (field)); + + ret = NULL; + + ret = g_malloc0 (sizeof (GValue)); + g_value_init (ret, G_TYPE_POINTER); + g_value_set_pointer (ret, g_memdup (priv->original_value, sizeof (struct tm))); + + return ret; +} + /** * gtk_form_field_datetime_clear: * @field: a #GtkFormField object. @@ -461,6 +488,35 @@ gtk_form_field_datetime_set_from_hashtable (GtkFormField *field, GHashTable *has return ret; } +/** + * gtk_form_field_datetime_set_value: + * @field: a #GtkFormField object. + * @value: a #GValue. + * + */ +gboolean +gtk_form_field_datetime_set_value (GtkFormField *field, GValue *gvalue) +{ + gboolean ret; + GtkFormFieldDateTimePrivate *priv; + struct tm *datetime; + gchar *value; + + g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), FALSE); + g_return_val_if_fail (G_VALUE_HOLDS_POINTER (gvalue), FALSE); + + ret = FALSE; + + priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (GTK_FORM_FIELD_DATETIME (field)); + + datetime = (struct tm *)g_value_get_pointer (gvalue); + value = gtk_form_field_datetime_get_str_from_tm (datetime, priv->display_format); + ret = gtk_form_field_datetime_set_value_stringify (field, value); + priv->original_value = g_memdup (datetime, sizeof (struct tm)); + + return ret; +} + /** * gtk_form_field_datetime_get_tm_from_str: * @str: the #gchar to be parsed. diff --git a/src/fielddatetime.h b/src/fielddatetime.h index b5acc9a..6acfeac 100644 --- a/src/fielddatetime.h +++ b/src/fielddatetime.h @@ -62,6 +62,7 @@ GtkFormField *gtk_form_field_datetime_new (void); gchar *gtk_form_field_datetime_get_value_stringify (GtkFormField *field); GValue *gtk_form_field_datetime_get_value (GtkFormField *field); gchar *gtk_form_field_datetime_get_value_sql (GtkFormField *field); +GValue *gtk_form_field_datetime_get_original_value (GtkFormField *field); gboolean gtk_form_field_datetime_clear (GtkFormField *field); @@ -72,6 +73,7 @@ void gtk_form_field_datetime_set_as_origin (GtkFormField *field); gboolean gtk_form_field_datetime_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, gint row); gboolean gtk_form_field_datetime_set_from_hashtable (GtkFormField *field, GHashTable *hashtable); +gboolean gtk_form_field_datetime_set_value (GtkFormField *field, GValue *gvalue); struct tm *gtk_form_field_datetime_get_tm_from_str (const gchar *str, const gchar *format); gchar *gtk_form_field_datetime_get_str_from_tm (struct tm *datetime, const gchar *format); diff --git a/src/fieldfloat.c b/src/fieldfloat.c index b8e0235..59cfb5b 100644 --- a/src/fieldfloat.c +++ b/src/fieldfloat.c @@ -71,12 +71,14 @@ gtk_form_field_float_class_init (GtkFormFieldFloatClass *klass) field_class->get_value_stringify = gtk_form_field_float_get_value_stringify; field_class->get_value = gtk_form_field_float_get_value; field_class->get_value_sql = gtk_form_field_float_get_value_sql; + field_class->get_original_value = gtk_form_field_float_get_original_value; field_class->clear = gtk_form_field_float_clear; field_class->is_empty = gtk_form_field_float_is_empty; field_class->is_changed = gtk_form_field_float_is_changed; field_class->set_as_origin = gtk_form_field_float_set_as_origin; field_class->set_from_datamodel = gtk_form_field_float_set_from_datamodel; field_class->set_from_hashtable = gtk_form_field_float_set_from_hashtable; + field_class->set_value = gtk_form_field_float_set_value; g_object_class_install_property (object_class, PROP_DEFAULT, g_param_spec_float ("default", @@ -200,6 +202,27 @@ gchar return ret; } +/** + * gtk_form_field_float_get_original_value: + * @field: a #GtkFormField object. + * + */ +GValue +*gtk_form_field_float_get_original_value (GtkFormField *field) +{ + GValue *ret; + + ret = NULL; + + GtkFormFieldFloatPrivate *priv = GTK_FORM_FIELD_FLOAT_GET_PRIVATE (field); + + ret = g_malloc0 (sizeof (GValue)); + g_value_init (ret, G_TYPE_FLOAT); + g_value_set_float (ret, priv->original_value); + + return ret; +} + /** * gtk_form_field_float_clear: * @field: a #GtkFormField object. @@ -370,6 +393,30 @@ gtk_form_field_float_set_from_hashtable (GtkFormField *field, GHashTable *hashta return ret; } +/** + * gtk_form_field_float_set_value: + * @field: a #GtkFormField object. + * @value: a #GValue. + * + */ +gboolean +gtk_form_field_float_set_value (GtkFormField *field, GValue *gvalue) +{ + gboolean ret = FALSE; + GtkFormFieldFloatPrivate *priv; + + g_return_val_if_fail (IS_GTK_FORM_FIELD_FLOAT (field), FALSE); + g_return_val_if_fail (G_VALUE_HOLDS_FLOAT (gvalue), FALSE); + + priv = GTK_FORM_FIELD_FLOAT_GET_PRIVATE (GTK_FORM_FIELD_FLOAT (field)); + + priv->original_value = g_value_get_float (gvalue); + setlocale (LC_NUMERIC, ""); + ret = gtk_form_field_float_set_value_stringify (field, g_strdup_printf ("%f", priv->original_value)); + + return ret; +} + /* PRIVATE */ static void gtk_form_field_float_set_property (GObject *object, diff --git a/src/fieldfloat.h b/src/fieldfloat.h index b1dc528..6816ffb 100644 --- a/src/fieldfloat.h +++ b/src/fieldfloat.h @@ -55,6 +55,7 @@ GtkFormField *gtk_form_field_float_new (void); gchar *gtk_form_field_float_get_value_stringify (GtkFormField *field); GValue *gtk_form_field_float_get_value (GtkFormField *field); gchar *gtk_form_field_float_get_value_sql (GtkFormField *field); +GValue *gtk_form_field_float_get_original_value (GtkFormField *field); gboolean gtk_form_field_float_clear (GtkFormField *field); @@ -65,6 +66,7 @@ void gtk_form_field_float_set_as_origin (GtkFormField *field); gboolean gtk_form_field_float_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, gint row); gboolean gtk_form_field_float_set_from_hashtable (GtkFormField *field, GHashTable *hashtable); +gboolean gtk_form_field_float_set_value (GtkFormField *field, GValue *gvalue); G_END_DECLS diff --git a/src/fieldinteger.c b/src/fieldinteger.c index 611f790..e422e36 100644 --- a/src/fieldinteger.c +++ b/src/fieldinteger.c @@ -69,12 +69,14 @@ gtk_form_field_integer_class_init (GtkFormFieldIntegerClass *klass) field_class->get_value_stringify = gtk_form_field_integer_get_value_stringify; field_class->get_value = gtk_form_field_integer_get_value; field_class->get_value_sql = gtk_form_field_integer_get_value_sql; + field_class->get_original_value = gtk_form_field_integer_get_original_value; field_class->clear = gtk_form_field_integer_clear; field_class->is_empty = gtk_form_field_integer_is_empty; field_class->is_changed = gtk_form_field_integer_is_changed; field_class->set_as_origin = gtk_form_field_integer_set_as_origin; field_class->set_from_datamodel = gtk_form_field_integer_set_from_datamodel; field_class->set_from_hashtable = gtk_form_field_integer_set_from_hashtable; + field_class->set_value = gtk_form_field_integer_set_value; g_object_class_install_property (object_class, PROP_DEFAULT, g_param_spec_int ("default", @@ -158,7 +160,7 @@ GValue { ret = g_malloc0 (sizeof (GValue)); g_value_init (ret, G_TYPE_INT); - g_value_set_int (ret, atol (value)); + g_value_set_int (ret, strtol (value, NULL, 10)); g_free (value); } @@ -179,6 +181,29 @@ gchar return gtk_form_field_integer_get_value_stringify (field); } +/** + * gtk_form_field_integer_get_original_value: + * @field: a #GtkFormField object. + * + */ +GValue +*gtk_form_field_integer_get_original_value (GtkFormField *field) +{ + GValue *ret; + + g_return_val_if_fail (IS_GTK_FORM_FIELD_INTEGER (field), NULL); + + GtkFormFieldIntegerPrivate *priv = GTK_FORM_FIELD_INTEGER_GET_PRIVATE (field); + + ret = NULL; + + ret = g_malloc0 (sizeof (GValue)); + g_value_init (ret, G_TYPE_INT); + g_value_set_int (ret, priv->original_value); + + return ret; +} + /** * gtk_form_field_integer_clear: * @field: a #GtkFormField object. @@ -339,6 +364,29 @@ gtk_form_field_integer_set_from_hashtable (GtkFormField *field, GHashTable *hash return ret; } +/** + * gtk_form_field_integer_set_value: + * @field: a #GtkFormField object. + * @gvalue: a #Gvalue. + * + */ +gboolean +gtk_form_field_integer_set_value (GtkFormField *field, GValue *gvalue) +{ + gboolean ret = FALSE; + GtkFormFieldIntegerPrivate *priv; + + g_return_val_if_fail (IS_GTK_FORM_FIELD_INTEGER (field), FALSE); + g_return_val_if_fail (G_VALUE_HOLDS_INT (gvalue), FALSE); + + priv = GTK_FORM_FIELD_INTEGER_GET_PRIVATE (GTK_FORM_FIELD_INTEGER (field)); + + priv->original_value = g_value_get_int (gvalue); + ret = gtk_form_field_integer_set_value_stringify (field, g_strdup_printf ("%d", priv->original_value)); + + return ret; +} + /* PRIVATE */ static void gtk_form_field_integer_set_property (GObject *object, diff --git a/src/fieldinteger.h b/src/fieldinteger.h index 0d55392..3caa6de 100644 --- a/src/fieldinteger.h +++ b/src/fieldinteger.h @@ -55,6 +55,7 @@ GtkFormField *gtk_form_field_integer_new (void); gchar *gtk_form_field_integer_get_value_stringify (GtkFormField *field); GValue *gtk_form_field_integer_get_value (GtkFormField *field); gchar *gtk_form_field_integer_get_value_sql (GtkFormField *field); +GValue *gtk_form_field_integer_get_original_value (GtkFormField *field); gboolean gtk_form_field_integer_clear (GtkFormField *field); @@ -65,6 +66,7 @@ void gtk_form_field_integer_set_as_origin (GtkFormField *field); gboolean gtk_form_field_integer_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, gint row); gboolean gtk_form_field_integer_set_from_hashtable (GtkFormField *field, GHashTable *hashtable); +gboolean gtk_form_field_integer_set_value (GtkFormField *field, GValue *gvalue); G_END_DECLS diff --git a/src/fieldtext.c b/src/fieldtext.c index e6d4517..5b8b5e2 100644 --- a/src/fieldtext.c +++ b/src/fieldtext.c @@ -69,12 +69,14 @@ gtk_form_field_text_class_init (GtkFormFieldTextClass *klass) field_class->get_value_stringify = gtk_form_field_text_get_value_stringify; field_class->get_value = gtk_form_field_text_get_value; field_class->get_value_sql = gtk_form_field_text_get_value_sql; + field_class->get_original_value = gtk_form_field_text_get_original_value; field_class->clear = gtk_form_field_text_clear; field_class->is_empty = gtk_form_field_text_is_empty; field_class->is_changed = gtk_form_field_text_is_changed; field_class->set_as_origin = gtk_form_field_text_set_as_origin; field_class->set_from_datamodel = gtk_form_field_text_set_from_datamodel; field_class->set_from_hashtable = gtk_form_field_text_set_from_hashtable; + field_class->set_value = gtk_form_field_text_set_value; g_object_class_install_property (object_class, PROP_DEFAULT, g_param_spec_string ("default", @@ -191,6 +193,27 @@ gchar return ret; } +/** + * gtk_form_field_text_get_original_value: + * @field: a #GtkFormField object. + * + */ +GValue +*gtk_form_field_text_get_original_value (GtkFormField *field) +{ + GValue *ret; + + GtkFormFieldTextPrivate *priv = GTK_FORM_FIELD_TEXT_GET_PRIVATE (field); + + ret = NULL; + + ret = g_malloc0 (sizeof (GValue)); + g_value_init (ret, G_TYPE_STRING); + g_value_set_string (ret, priv->original_value); + + return ret; +} + /** * gtk_form_field_text_clear: * @field: a #GtkFormField object. @@ -357,6 +380,29 @@ gtk_form_field_text_set_from_hashtable (GtkFormField *field, GHashTable *hashtab return ret; } +/** + * gtk_form_field_text_set_value: + * @field: a #GtkFormField object. + * @gvalue: a #GValue. + * + */ +gboolean +gtk_form_field_text_set_value (GtkFormField *field, GValue *gvalue) +{ + gboolean ret = FALSE; + GtkFormFieldTextPrivate *priv; + + g_return_val_if_fail (IS_GTK_FORM_FIELD_TEXT (field), FALSE); + g_return_val_if_fail (G_VALUE_HOLDS_STRING (gvalue), FALSE); + + priv = GTK_FORM_FIELD_TEXT_GET_PRIVATE (GTK_FORM_FIELD_TEXT (field)); + + ret = gtk_form_field_text_set_value_stringify (field, g_value_get_string (gvalue)); + priv->original_value = gtk_form_field_text_get_value_stringify (field); + + return ret; +} + /* PRIVATE */ static void gtk_form_field_text_set_property (GObject *object, diff --git a/src/fieldtext.h b/src/fieldtext.h index c693aa8..885eded 100644 --- a/src/fieldtext.h +++ b/src/fieldtext.h @@ -55,6 +55,7 @@ GtkFormField *gtk_form_field_text_new (void); gchar *gtk_form_field_text_get_value_stringify (GtkFormField *field); GValue *gtk_form_field_text_get_value (GtkFormField *field); gchar *gtk_form_field_text_get_value_sql (GtkFormField *field); +GValue *gtk_form_field_text_get_original_value (GtkFormField *field); gboolean gtk_form_field_text_clear (GtkFormField *field); @@ -65,6 +66,7 @@ void gtk_form_field_text_set_as_origin (GtkFormField *field); gboolean gtk_form_field_text_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, gint row); gboolean gtk_form_field_text_set_from_hashtable (GtkFormField *field, GHashTable *hashtable); +gboolean gtk_form_field_text_set_value (GtkFormField *field, GValue *gvalue); G_END_DECLS diff --git a/src/form.c b/src/form.c index 1249f41..1620f68 100644 --- a/src/form.c +++ b/src/form.c @@ -1929,6 +1929,40 @@ gtk_form_fill_from_table (GtkForm *form) return ret; } +/** + * gtk_form_reset: + * @form: + * + */ +gboolean +gtk_form_reset (GtkForm *form) +{ + GtkFormPrivate *priv; + GSList *fields; + gboolean ret = TRUE; + GtkFormField *field; + GtkFormWidget *widget; + GValue *gval; + + g_return_val_if_fail (IS_GTK_FORM (form), FALSE); + + priv = GTK_FORM_GET_PRIVATE (form); + + fields = priv->fields; + + while (fields != NULL) + { + field = (GtkFormField *)fields->data; + + gval = gtk_form_field_get_original_value (field); + gtk_form_field_set_value (field, gval); + + fields = g_slist_next (fields); + } + + return ret; +} + /** * gtk_form_get_values_as_hashtable: * @form: a #GtkForm object. @@ -1974,6 +2008,51 @@ GHashTable return ht; } +/** + * gtk_form_get_original_values_as_hashtable: + * @form: + * + */ +GHashTable +*gtk_form_get_original_values_as_hashtable (GtkForm *form) +{ + GtkFormPrivate *priv; + GSList *fields; + GtkFormField *field; + gchar *field_name; + GValue *gval; + GHashTable *ht; + + g_return_val_if_fail (IS_GTK_FORM (form), NULL); + + priv = GTK_FORM_GET_PRIVATE (form); + + fields = priv->fields; + + if (fields != NULL) + { + ht = g_hash_table_new (g_str_hash, g_str_equal); + } + + while (fields != NULL) + { + field = (GtkFormField *)fields->data; + + field_name = gtk_form_field_get_field_name (field); + + gval = gtk_form_field_get_original_value (field); + if (gval != NULL) + { + /* TODO check if the field already exists */ + g_hash_table_insert (ht, field_name, gval); + } + + fields = g_slist_next (fields); + } + + return ht; +} + /** * gtk_form_fill_from_hashtable: * @form: a #GtkForm object. diff --git a/src/form.h b/src/form.h index f42ae37..a7cbf7b 100644 --- a/src/form.h +++ b/src/form.h @@ -124,7 +124,10 @@ gchar *gtk_form_get_sql (GtkForm *form, GtkFormSqlType type); gboolean gtk_form_fill_from_datamodel (GtkForm *form, GdaDataModel *dm, gint row); gboolean gtk_form_fill_from_table (GtkForm *form); +gboolean gtk_form_reset (GtkForm *form); + GHashTable *gtk_form_get_values_as_hashtable (GtkForm *form); +GHashTable *gtk_form_get_original_values_as_hashtable (GtkForm *form); gboolean gtk_form_fill_from_hashtable (GtkForm *form, GHashTable *hashtable); gboolean gtk_form_add_group (GtkForm *form, GtkFormGroup *group); diff --git a/test/from_xml_with_db.c b/test/from_xml_with_db.c index b080629..f4ec536 100644 --- a/test/from_xml_with_db.c +++ b/test/from_xml_with_db.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2010 Andrea Zagli * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -49,6 +49,7 @@ GtkWidget *btn_edit; GtkWidget *btn_delete; GtkWidget *cb_customers; GtkWidget *btn_open; +GtkWidget *btn_cancel; GtkListStore *lstore_customers; @@ -157,6 +158,13 @@ on_btn_open_clicked (GtkButton *button, current_id = id; } +G_MODULE_EXPORT void +on_btn_cancel_clicked (GtkButton *button, + gpointer user_data) +{ + gtk_form_reset (form); +} + void fill_cb_customers (void) { @@ -211,6 +219,7 @@ main (int argc, char **argv) btn_delete = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "btn_delete")); cb_customers = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "cb_customers")); btn_open = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "btn_open")); + btn_cancel = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "btn_cancel")); lstore_customers = GTK_LIST_STORE (gtk_builder_get_object (gtkbuilder, "lstore_customers")); diff --git a/test/test_db.gui b/test/test_db.gui index 4765b94..dc974ef 100644 --- a/test/test_db.gui +++ b/test/test_db.gui @@ -318,6 +318,21 @@ 4 + + + gtk-undo + True + True + True + True + + + + False + False + 5 + + False -- 2.49.0