From aa7702c8c612bc031672ea3de037569fc1829420 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 19 Jul 2010 11:21:56 +0200 Subject: [PATCH] Added functions GtkForm::get_values_as_hashtable and and GtkForm::fill_from_hashtable. --- configure.ac | 2 +- docs/reference/libgtkform-decl-list.txt | 8 +++ docs/reference/libgtkform-decl.txt | 41 +++++++++++ docs/reference/libgtkform-undocumented.txt | 12 +++- docs/reference/libgtkform-unused.txt | 8 +++ src/field.c | 25 ++++++- src/field.h | 4 +- src/fieldboolean.c | 80 ++++++++++++++++++++-- src/fieldboolean.h | 3 +- src/fielddatetime.c | 58 +++++++++++++++- src/fielddatetime.h | 3 +- src/fieldfloat.c | 57 ++++++++++++++- src/fieldfloat.h | 3 +- src/fieldinteger.c | 49 ++++++++++++- src/fieldinteger.h | 3 +- src/fieldtext.c | 48 ++++++++++++- src/fieldtext.h | 3 +- src/form.c | 80 +++++++++++++++++++++- src/form.h | 5 +- 19 files changed, 469 insertions(+), 23 deletions(-) diff --git a/configure.ac b/configure.ac index 9f539b0..f9198e7 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ(2.59) -AC_INIT([libgtkform], [0.1.2], [azagli@libero.it]) +AC_INIT([libgtkform], [0.1.3], [azagli@libero.it]) AC_CONFIG_SRCDIR([src/form.c]) AC_CONFIG_HEADER([config.h]) diff --git a/docs/reference/libgtkform-decl-list.txt b/docs/reference/libgtkform-decl-list.txt index ae19f43..619bf22 100644 --- a/docs/reference/libgtkform-decl-list.txt +++ b/docs/reference/libgtkform-decl-list.txt @@ -19,6 +19,7 @@ gtk_form_field_float_is_empty gtk_form_field_float_is_changed gtk_form_field_float_set_as_origin gtk_form_field_float_set_from_datamodel +gtk_form_field_float_set_from_hashtable
@@ -63,6 +64,8 @@ GtkFormSqlType gtk_form_get_sql gtk_form_fill_from_datamodel gtk_form_fill_from_table +gtk_form_get_values_as_hashtable +gtk_form_fill_from_hashtable
@@ -104,6 +107,7 @@ gtk_form_field_datetime_is_empty gtk_form_field_datetime_is_changed gtk_form_field_datetime_set_as_origin gtk_form_field_datetime_set_from_datamodel +gtk_form_field_datetime_set_from_hashtable gtk_form_field_datetime_get_tm_from_str gtk_form_field_datetime_get_str_from_tm gtk_form_field_datetime_get_gdate_from_tm @@ -131,6 +135,7 @@ gtk_form_field_boolean_is_empty gtk_form_filed_boolean_is_changed gtk_form_field_boolean_set_as_origin gtk_form_field_boolean_set_from_datamodel +gtk_form_field_boolean_set_from_hashtable gtk_form_field_boolean_str_to_boolean
@@ -233,6 +238,7 @@ gtk_form_field_is_empty gtk_form_field_is_changed gtk_form_field_set_as_origin gtk_form_field_set_from_datamodel +gtk_form_field_set_from_hashtable
@@ -256,6 +262,7 @@ gtk_form_field_text_is_empty gtk_form_field_text_is_changed gtk_form_field_text_set_as_origin gtk_form_field_text_set_from_datamodel +gtk_form_field_text_set_from_hashtable
@@ -320,6 +327,7 @@ gtk_form_field_integer_is_empty gtk_form_field_integer_is_changed gtk_form_field_integer_set_as_origin gtk_form_field_integer_set_from_datamodel +gtk_form_field_integer_set_from_hashtable
diff --git a/docs/reference/libgtkform-decl.txt b/docs/reference/libgtkform-decl.txt index 6cfd4ce..1bbddf2 100644 --- a/docs/reference/libgtkform-decl.txt +++ b/docs/reference/libgtkform-decl.txt @@ -92,6 +92,11 @@ GtkFormField *field gboolean GtkFormField *field, GdaDataModel *dm, gint row + +gtk_form_field_float_set_from_hashtable +gboolean +GtkFormField *field, GHashTable *hashtable + TYPE_GTK_FORM #define TYPE_GTK_FORM (gtk_form_get_type ()) @@ -296,6 +301,16 @@ GtkForm *form, GdaDataModel *dm, gint row gboolean GtkForm *form + +gtk_form_get_values_as_hashtable +GHashTable * +GtkForm *form + + +gtk_form_fill_from_hashtable +gboolean +GtkForm *form, GHashTable *hashtable + TYPE_GTK_FORM_WIDGET_ENTRY #define TYPE_GTK_FORM_WIDGET_ENTRY (gtk_form_widget_entry_get_type ()) @@ -460,6 +475,11 @@ GtkFormField *field GtkFormField *field, GdaDataModel *dm, gint row +gtk_form_field_datetime_set_from_hashtable +gboolean +GtkFormField *field, GHashTable *hashtable + + gtk_form_field_datetime_get_tm_from_str struct tm * const gchar *str, const gchar *format @@ -574,6 +594,11 @@ GtkFormField *field GtkFormField *field, GdaDataModel *dm, gint row +gtk_form_field_boolean_set_from_hashtable +gboolean +GtkFormField *field, GHashTable *hashtable + + gtk_form_field_boolean_str_to_boolean gboolean const gchar *value @@ -911,6 +936,7 @@ struct _GtkFormFieldClass gboolean (*is_changed) (GtkFormField *field); void (*set_as_origin) (GtkFormField *field); gboolean (*set_from_datamodel) (GtkFormField *field, GdaDataModel *dm, gint row); + gboolean (*set_from_hashtable) (GtkFormField *field, GHashTable *hashtable); }; @@ -988,6 +1014,11 @@ GtkFormField *field gboolean GtkFormField *field, GdaDataModel *dm, gint row + +gtk_form_field_set_from_hashtable +gboolean +GtkFormField *field, GHashTable *hashtable + TYPE_GTK_FORM_FIELD_TEXT #define TYPE_GTK_FORM_FIELD_TEXT (gtk_form_field_text_get_type ()) @@ -1082,6 +1113,11 @@ GtkFormField *field gboolean GtkFormField *field, GdaDataModel *dm, gint row + +gtk_form_field_text_set_from_hashtable +gboolean +GtkFormField *field, GHashTable *hashtable + TYPE_GTK_FORM_WIDGET_TEXTVIEW #define TYPE_GTK_FORM_WIDGET_TEXTVIEW (gtk_form_widget_textview_get_type ()) @@ -1345,6 +1381,11 @@ GtkFormField *field gboolean GtkFormField *field, GdaDataModel *dm, gint row + +gtk_form_field_integer_set_from_hashtable +gboolean +GtkFormField *field, GHashTable *hashtable + TYPE_GTK_FORM_WIDGET_RADIO #define TYPE_GTK_FORM_WIDGET_RADIO (gtk_form_widget_radio_get_type ()) diff --git a/docs/reference/libgtkform-undocumented.txt b/docs/reference/libgtkform-undocumented.txt index 972b6db..4e051ae 100644 --- a/docs/reference/libgtkform-undocumented.txt +++ b/docs/reference/libgtkform-undocumented.txt @@ -1,7 +1,7 @@ -8% symbol docs coverage. +7% symbol docs coverage. 24 symbols documented. 2 symbols incomplete. -291 not documented. +299 not documented. GTK_FORM @@ -123,6 +123,7 @@ gtk_form_field_boolean_is_empty gtk_form_field_boolean_new gtk_form_field_boolean_set_as_origin gtk_form_field_boolean_set_from_datamodel +gtk_form_field_boolean_set_from_hashtable gtk_form_field_boolean_str_to_boolean (value) gtk_form_field_clear gtk_form_field_datetime_clear @@ -141,6 +142,7 @@ gtk_form_field_datetime_new gtk_form_field_datetime_replace_str gtk_form_field_datetime_set_as_origin gtk_form_field_datetime_set_from_datamodel +gtk_form_field_datetime_set_from_hashtable gtk_form_field_float_clear gtk_form_field_float_get_type gtk_form_field_float_get_value @@ -151,6 +153,7 @@ gtk_form_field_float_is_empty gtk_form_field_float_new gtk_form_field_float_set_as_origin gtk_form_field_float_set_from_datamodel +gtk_form_field_float_set_from_hashtable gtk_form_field_get_field_name gtk_form_field_get_gtk_form_widget gtk_form_field_get_type @@ -167,6 +170,7 @@ gtk_form_field_integer_is_empty gtk_form_field_integer_new gtk_form_field_integer_set_as_origin gtk_form_field_integer_set_from_datamodel +gtk_form_field_integer_set_from_hashtable gtk_form_field_is_auto_increment gtk_form_field_is_changed gtk_form_field_is_empty @@ -174,6 +178,7 @@ gtk_form_field_is_to_save gtk_form_field_new gtk_form_field_new_from_gtk_form_widget gtk_form_field_set_from_datamodel +gtk_form_field_set_from_hashtable gtk_form_field_text_clear gtk_form_field_text_get_type gtk_form_field_text_get_value @@ -184,8 +189,10 @@ gtk_form_field_text_is_empty gtk_form_field_text_new gtk_form_field_text_set_as_origin gtk_form_field_text_set_from_datamodel +gtk_form_field_text_set_from_hashtable gtk_form_filed_boolean_is_changed gtk_form_fill_from_datamodel +gtk_form_fill_from_hashtable gtk_form_fill_from_table gtk_form_get_field_from_name gtk_form_get_form_field_from_name @@ -199,6 +206,7 @@ gtk_form_get_objects_by_name gtk_form_get_sql gtk_form_get_table gtk_form_get_type +gtk_form_get_values_as_hashtable gtk_form_get_widget_from_name gtk_form_is_changed gtk_form_key_add_field diff --git a/docs/reference/libgtkform-unused.txt b/docs/reference/libgtkform-unused.txt index 86f142b..d6d29f1 100644 --- a/docs/reference/libgtkform-unused.txt +++ b/docs/reference/libgtkform-unused.txt @@ -7,6 +7,7 @@ IS_GTK_FORM_WIDGET_RADIO IS_GTK_FORM_WIDGET_RADIO_CLASS TYPE_GTK_FORM_WIDGET_RADIO gtk_form_field_boolean_set_as_origin +gtk_form_field_boolean_set_from_hashtable gtk_form_field_boolean_str_to_boolean gtk_form_field_datetime_get_gdate_from_tm gtk_form_field_datetime_get_now @@ -14,18 +15,24 @@ gtk_form_field_datetime_get_str_from_tm gtk_form_field_datetime_get_tm_from_str gtk_form_field_datetime_is_changed gtk_form_field_datetime_set_as_origin +gtk_form_field_datetime_set_from_hashtable gtk_form_field_float_is_changed gtk_form_field_float_set_as_origin +gtk_form_field_float_set_from_hashtable gtk_form_field_get_form_widget gtk_form_field_integer_is_changed gtk_form_field_integer_set_as_origin +gtk_form_field_integer_set_from_hashtable gtk_form_field_is_auto_increment gtk_form_field_is_changed gtk_form_field_is_to_save gtk_form_field_set_as_origin +gtk_form_field_set_from_hashtable gtk_form_field_text_is_changed gtk_form_field_text_set_as_origin +gtk_form_field_text_set_from_hashtable gtk_form_filed_boolean_is_changed +gtk_form_fill_from_hashtable gtk_form_fill_from_table gtk_form_get_form_field_from_name gtk_form_get_form_field_from_widget @@ -35,6 +42,7 @@ gtk_form_get_gtkbuilder gtk_form_get_key gtk_form_get_objects_by_name gtk_form_get_table +gtk_form_get_values_as_hashtable gtk_form_get_widgets gtk_form_is_changed gtk_form_load_from_file diff --git a/src/field.c b/src/field.c index a7d7096..7a79147 100644 --- a/src/field.c +++ b/src/field.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2010 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -362,6 +362,29 @@ gtk_form_field_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, gint r return ret; } +/** + * gtk_form_field_set_from_hashtable: + * @field: a #GtkFormField object. + * @hashtable: a #GHashTable object. + * + */ +gboolean +gtk_form_field_set_from_hashtable (GtkFormField *field, GHashTable *hashtable) +{ + gboolean ret = FALSE; + + GtkFormFieldPrivate *priv = GTK_FORM_FIELD_GET_PRIVATE (field); + + g_return_val_if_fail (hashtable != NULL, FALSE); + + if (IS_GTK_FORM_FIELD (field) && GTK_FORM_FIELD_GET_CLASS (field)->set_from_hashtable != NULL) + { + ret = GTK_FORM_FIELD_GET_CLASS (field)->set_from_hashtable (field, hashtable); + } + + return ret; +} + /* PRIVATE */ static void gtk_form_field_set_property (GObject *object, diff --git a/src/field.h b/src/field.h index 7d19f72..1941751 100644 --- a/src/field.h +++ b/src/field.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2010 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -58,6 +58,7 @@ struct _GtkFormFieldClass gboolean (*is_changed) (GtkFormField *field); void (*set_as_origin) (GtkFormField *field); gboolean (*set_from_datamodel) (GtkFormField *field, GdaDataModel *dm, gint row); + gboolean (*set_from_hashtable) (GtkFormField *field, GHashTable *hashtable); }; GType gtk_form_field_get_type (void) G_GNUC_CONST; @@ -84,6 +85,7 @@ gboolean gtk_form_field_is_changed (GtkFormField *field); 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); G_END_DECLS diff --git a/src/fieldboolean.c b/src/fieldboolean.c index e9e5d47..ae34298 100644 --- a/src/fieldboolean.c +++ b/src/fieldboolean.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2010 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -16,8 +16,14 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#ifdef HAVE_CONFIG_H + #include +#endif + #include +#include + #include "widget.h" #include "fieldboolean.h" @@ -272,7 +278,7 @@ gtk_form_field_boolean_set_as_origin (GtkFormField *field) /** * gtk_form_field_boolean_set_from_datamodel: * @field: a #GtkFormField object. - * @dm: + * @dm: a #GdaDataModel object. * @row: * */ @@ -291,17 +297,77 @@ gtk_form_field_boolean_set_from_datamodel (GtkFormField *field, GdaDataModel *dm field_name = gtk_form_field_get_field_name (field); - if (gdaex_data_model_get_field_value_boolean_at (dm, row, field_name)) + if (field_name != NULL && g_strcmp0 (g_strstrip (field_name), "") != 0) { - value = g_strdup_printf ("TRUE"); + priv->original_value = gdaex_data_model_get_field_value_boolean_at (dm, row, field_name); + 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); } else { - value = g_strdup_printf ("FALSE"); + g_warning (_("GtkFormField hasn't a field name.")); } - ret = gtk_form_field_boolean_set_value_stringify (field, value); - priv->original_value = gtk_form_field_boolean_str_to_boolean (value); + g_free (field_name); + g_free (value); + + return ret; +} + +/** + * gtk_form_field_boolean_set_from_hashtable: + * @field: a #GtkFormField object. + * @hashtable: a #GHashTable object. + * + */ +gboolean +gtk_form_field_boolean_set_from_hashtable (GtkFormField *field, GHashTable *hashtable) +{ + gboolean ret = FALSE; + GtkFormFieldBooleanPrivate *priv; + gchar *field_name; + GValue *gval; + gchar *value; + + g_return_val_if_fail (IS_GTK_FORM_FIELD_BOOLEAN (field), FALSE); + g_return_val_if_fail (hashtable != NULL, FALSE); + + priv = GTK_FORM_FIELD_BOOLEAN_GET_PRIVATE (GTK_FORM_FIELD_BOOLEAN (field)); + + field_name = gtk_form_field_get_field_name (field); + if (field_name != NULL && g_strcmp0 (g_strstrip (field_name), "") != 0) + { + gval = (GValue *)g_hash_table_lookup (hashtable, field_name); + if (gval == NULL) + { + priv->original_value = FALSE; + ret = gtk_form_field_boolean_set_value_stringify (field, "FALSE"); + } + else + { + priv->original_value = g_value_get_boolean (gval); + 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); + } + } + else + { + g_warning (_("GtkFormField hasn't a field name.")); + } g_free (field_name); g_free (value); diff --git a/src/fieldboolean.h b/src/fieldboolean.h index c1a9ca4..7485d6d 100644 --- a/src/fieldboolean.h +++ b/src/fieldboolean.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2010 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -64,6 +64,7 @@ gboolean gtk_form_filed_boolean_is_changed (GtkFormField *field); 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_str_to_boolean (const gchar *value); diff --git a/src/fielddatetime.c b/src/fielddatetime.c index 8b5b859..5947858 100644 --- a/src/fielddatetime.c +++ b/src/fielddatetime.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2010 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -92,6 +92,7 @@ gtk_form_field_datetime_class_init (GtkFormFieldDateTimeClass *klass) 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; g_object_class_install_property (object_class, PROP_DEFAULT, g_param_spec_pointer ("default", @@ -405,6 +406,61 @@ gtk_form_field_datetime_set_from_datamodel (GtkFormField *field, GdaDataModel *d return ret; } +/** + * gtk_form_field_datetime_set_from_hashtable: + * @field: a #GtkFormField object. + * @hashtable: a #GHashTable object. + * + */ +gboolean +gtk_form_field_datetime_set_from_hashtable (GtkFormField *field, GHashTable *hashtable) +{ + gboolean ret; + GtkFormFieldDateTimePrivate *priv; + gchar *field_name; + GValue *gval; + struct tm *datetime; + gchar *value; + + g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), FALSE); + g_return_val_if_fail (hashtable != NULL, FALSE); + + ret = FALSE; + + priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (GTK_FORM_FIELD_DATETIME (field)); + + field_name = (gchar *)gtk_form_field_get_field_name (field); + if (field_name != NULL && g_strcmp0 (g_strstrip (field_name), "") != 0) + { + gval = (GValue *)g_hash_table_lookup (hashtable, field_name); + if (gval == NULL) + { + priv->original_value = NULL; + ret = gtk_form_field_datetime_set_value_stringify (field, ""); + } + else + { + datetime = (struct tm *)g_value_get_pointer (gval); + + 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)); + + g_free (value); + } + } + else + { + g_warning (_("GtkFormField hasn't a field name.")); + } + + g_free (field_name); + + 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 09dba45..b5acc9a 100644 --- a/src/fielddatetime.h +++ b/src/fielddatetime.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2010 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -71,6 +71,7 @@ gboolean gtk_form_field_datetime_is_changed (GtkFormField *field); 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); 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 f0f2a1e..b8e0235 100644 --- a/src/fieldfloat.c +++ b/src/fieldfloat.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2010 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -16,8 +16,14 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#ifdef HAVE_CONFIG_H + #include +#endif + #include +#include + #include "widget.h" #include "fieldfloat.h" @@ -70,6 +76,7 @@ gtk_form_field_float_class_init (GtkFormFieldFloatClass *klass) 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; g_object_class_install_property (object_class, PROP_DEFAULT, g_param_spec_float ("default", @@ -315,6 +322,54 @@ gtk_form_field_float_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, return ret; } +/** + * gtk_form_field_float_set_from_hashtable: + * @field: a #GtkFormField object. + * @hashtable: a #GHashTable object. + * + */ +gboolean +gtk_form_field_float_set_from_hashtable (GtkFormField *field, GHashTable *hashtable) +{ + gboolean ret = FALSE; + GtkFormFieldFloatPrivate *priv; + GValue *gval; + gchar *field_name; + + g_return_val_if_fail (IS_GTK_FORM_FIELD_FLOAT (field), FALSE); + g_return_val_if_fail (hashtable != NULL, FALSE); + + priv = GTK_FORM_FIELD_FLOAT_GET_PRIVATE (GTK_FORM_FIELD_FLOAT (field)); + + field_name = gtk_form_field_get_field_name (field); + if (field_name != NULL && g_strcmp0 (g_strstrip (field_name), "") != 0) + { + gval = (GValue *)g_hash_table_lookup (hashtable, field_name); + if (gval == NULL) + { + priv->original_value = 0.0; + + setlocale (LC_NUMERIC, ""); + ret = gtk_form_field_float_set_value_stringify (field, "0.0"); + } + else + { + priv->original_value = g_value_get_float (gval); + + setlocale (LC_NUMERIC, ""); + ret = gtk_form_field_float_set_value_stringify (field, g_strdup_printf ("%f", priv->original_value)); + } + } + else + { + g_warning (_("GtkFormField hasn't a field name.")); + } + + g_free (field_name); + + return ret; +} + /* PRIVATE */ static void gtk_form_field_float_set_property (GObject *object, diff --git a/src/fieldfloat.h b/src/fieldfloat.h index f048571..b1dc528 100644 --- a/src/fieldfloat.h +++ b/src/fieldfloat.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2010 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -64,6 +64,7 @@ gboolean gtk_form_field_float_is_changed (GtkFormField *field); 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); G_END_DECLS diff --git a/src/fieldinteger.c b/src/fieldinteger.c index 06afcb2..611f790 100644 --- a/src/fieldinteger.c +++ b/src/fieldinteger.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2010 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -16,6 +16,12 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#ifdef HAVE_CONFIG_H + #include +#endif + +#include + #include "widget.h" #include "fieldinteger.h" @@ -68,6 +74,7 @@ gtk_form_field_integer_class_init (GtkFormFieldIntegerClass *klass) 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; g_object_class_install_property (object_class, PROP_DEFAULT, g_param_spec_int ("default", @@ -292,6 +299,46 @@ gtk_form_field_integer_set_from_datamodel (GtkFormField *field, GdaDataModel *dm return ret; } +/** + * gtk_form_field_integer_set_from_hashtable: + * @field: a #GtkFormField object. + * @hashtable: a #GHashTable object. + * + */ +gboolean +gtk_form_field_integer_set_from_hashtable (GtkFormField *field, GHashTable *hashtable) +{ + gboolean ret = FALSE; + GtkFormFieldIntegerPrivate *priv; + GValue *gval; + gchar *field_name; + + g_return_val_if_fail (IS_GTK_FORM_FIELD_INTEGER (field), FALSE); + g_return_val_if_fail (hashtable != NULL, FALSE); + + priv = GTK_FORM_FIELD_INTEGER_GET_PRIVATE (GTK_FORM_FIELD_INTEGER (field)); + + field_name = gtk_form_field_get_field_name (field); + if (field_name != NULL && g_strcmp0 (g_strstrip (field_name), "") != 0) + { + gval = (GValue *)g_hash_table_lookup (hashtable, field_name); + if (gval == NULL) + { + priv->original_value = 0; + ret = gtk_form_field_integer_set_value_stringify (field, "0"); + } + else + { + priv->original_value = g_value_get_int (gval); + ret = gtk_form_field_integer_set_value_stringify (field, g_strdup_printf ("%d", priv->original_value)); + } + + g_free (field_name); + } + + return ret; +} + /* PRIVATE */ static void gtk_form_field_integer_set_property (GObject *object, diff --git a/src/fieldinteger.h b/src/fieldinteger.h index e559ac9..0d55392 100644 --- a/src/fieldinteger.h +++ b/src/fieldinteger.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2010 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -64,6 +64,7 @@ gboolean gtk_form_field_integer_is_changed (GtkFormField *field); 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); G_END_DECLS diff --git a/src/fieldtext.c b/src/fieldtext.c index 72db199..c3b03f2 100644 --- a/src/fieldtext.c +++ b/src/fieldtext.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2010 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -16,6 +16,12 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#ifdef HAVE_CONFIG_H + #include +#endif + +#include + #include "widget.h" #include "fieldtext.h" @@ -68,6 +74,7 @@ gtk_form_field_text_class_init (GtkFormFieldTextClass *klass) 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; g_object_class_install_property (object_class, PROP_DEFAULT, g_param_spec_string ("default", @@ -303,6 +310,45 @@ gtk_form_field_text_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, g return ret; } +/** + * gtk_form_field_text_set_from_hashtable: + * @field: a #GtkFormField object. + * @hashtable: a #GHashTable object. + * + */ +gboolean +gtk_form_field_text_set_from_hashtable (GtkFormField *field, GHashTable *hashtable) +{ + gboolean ret = FALSE; + GtkFormFieldTextPrivate *priv; + GValue *gval; + gchar *field_name; + + g_return_val_if_fail (IS_GTK_FORM_FIELD_TEXT (field), FALSE); + g_return_val_if_fail (hashtable != NULL, FALSE); + + priv = GTK_FORM_FIELD_TEXT_GET_PRIVATE (GTK_FORM_FIELD_TEXT (field)); + + field_name = gtk_form_field_get_field_name (field); + if (field_name != NULL && g_strcmp0 (g_strstrip (field_name), "") != 0) + { + gval = (GValue *)g_hash_table_lookup (hashtable, field_name); + if (gval == NULL) + { + priv->original_value = g_strdup (""); + ret = gtk_form_field_text_set_value_stringify (field, ""); + } + else + { + priv->original_value = g_strdup (g_value_get_string (gval)); + ret = gtk_form_field_text_set_value_stringify (field, priv->original_value); + } + g_free (field_name); + } + + return ret; +} + /* PRIVATE */ static void gtk_form_field_text_set_property (GObject *object, diff --git a/src/fieldtext.h b/src/fieldtext.h index 5a96a51..c693aa8 100644 --- a/src/fieldtext.h +++ b/src/fieldtext.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2010 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -64,6 +64,7 @@ gboolean gtk_form_field_text_is_changed (GtkFormField *field); 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); G_END_DECLS diff --git a/src/form.c b/src/form.c index d431340..731c583 100644 --- a/src/form.c +++ b/src/form.c @@ -1441,7 +1441,6 @@ gtk_form_fill_from_datamodel (GtkForm *form, GdaDataModel *dm, gint row) GSList *fields; gboolean ret = TRUE; GtkFormField *field; - gchar *field_name; g_return_val_if_fail (IS_GTK_FORM (form), FALSE); @@ -1494,6 +1493,85 @@ gtk_form_fill_from_table (GtkForm *form) return ret; } +/** + * gtk_form_get_values_as_hashtable: + * @form: a #GtkForm object. + * + */ +GHashTable +*gtk_form_get_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_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. + * @hashtable: a @GHashTable object. + * + */ +gboolean +gtk_form_fill_from_hashtable (GtkForm *form, GHashTable *hashtable) +{ + GtkFormPrivate *priv; + GSList *fields; + GtkFormField *field; + + gboolean ret; + + g_return_val_if_fail (IS_GTK_FORM (form), FALSE); + g_return_val_if_fail (hashtable != NULL, FALSE); + + priv = GTK_FORM_GET_PRIVATE (form); + + fields = priv->fields; + + while (fields != NULL) + { + field = (GtkFormField *)fields->data; + + gtk_form_field_set_from_hashtable (field, hashtable); + + fields = g_slist_next (fields); + } + + return ret; +} + /* PRIVATE */ static void gtk_form_set_property (GObject *object, diff --git a/src/form.h b/src/form.h index 65ea0b9..83bd90c 100644 --- a/src/form.h +++ b/src/form.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2009 Andrea Zagli + * Copyright (C) 2005-2010 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -111,6 +111,9 @@ 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); +GHashTable *gtk_form_get_values_as_hashtable (GtkForm *form); +gboolean gtk_form_fill_from_hashtable (GtkForm *form, GHashTable *hashtable); + G_END_DECLS -- 2.49.0