From 0d04f3f9f85c8e0c919a2c77402ac8e3db22d302 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 6 Apr 2011 17:34:29 +0200 Subject: [PATCH] Added property decimals to GtkForFieldFloat. --- data/gtkform.dtd | 3 ++- src/fieldfloat.c | 54 ++++++++++++++++++++++++++++++++++++----------- src/form.c | 21 +++++++++++------- test/database.db | Bin 2048 -> 2048 bytes test/test_db.gui | 29 ++++++++++++++++++++++++- test/test_db.xml | 13 +++++++++--- 6 files changed, 95 insertions(+), 25 deletions(-) diff --git a/data/gtkform.dtd b/data/gtkform.dtd index cc91902..caac165 100644 --- a/data/gtkform.dtd +++ b/data/gtkform.dtd @@ -31,7 +31,7 @@ - + + diff --git a/src/fieldfloat.c b/src/fieldfloat.c index d2c5bab..046ff56 100644 --- a/src/fieldfloat.c +++ b/src/fieldfloat.c @@ -30,7 +30,8 @@ enum { PROP_0, - PROP_DEFAULT + PROP_DEFAULT, + PROP_DECIMALS }; static void gtk_form_field_float_class_init (GtkFormFieldFloatClass *klass); @@ -46,6 +47,7 @@ static void gtk_form_field_float_get_property (GObject *object, GParamSpec *pspec); static gboolean gtk_form_field_float_set_value_stringify (GtkFormField *field, const gchar *value); +static const gchar *gtk_form_field_float_get_str_format (GtkFormFieldFloat *field); #define GTK_FORM_FIELD_FLOAT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_GTK_FORM_FIELD_FLOAT, GtkFormFieldFloatPrivate)) @@ -54,6 +56,8 @@ struct _GtkFormFieldFloatPrivate { gfloat default_value; gfloat original_value; + + guint decimals; }; @@ -88,6 +92,13 @@ gtk_form_field_float_class_init (GtkFormFieldFloatClass *klass) -G_MAXFLOAT, G_MAXFLOAT, 0.0f, G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + g_object_class_install_property (object_class, PROP_DECIMALS, + g_param_spec_uint ("decimals", + "Decimals", + "Number of decimals to show", + 0, G_MAXUINT, 0, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + g_type_class_add_private (object_class, sizeof (GtkFormFieldFloatPrivate)); } @@ -118,10 +129,13 @@ gchar gchar *ret; GtkFormWidget *fw; + GtkFormFieldFloatPrivate *priv; - ret = g_strdup (""); + g_return_val_if_fail (IS_GTK_FORM_FIELD_FLOAT (field), ""); - g_return_val_if_fail (IS_GTK_FORM_FIELD_FLOAT (field), ret); + priv = GTK_FORM_FIELD_FLOAT_GET_PRIVATE (field); + + ret = g_strdup (""); g_object_get (field, "form-widget", &fw, @@ -135,7 +149,7 @@ gchar if (value != NULL) { - ret = g_strdup_printf ("%f", strtod (value, NULL)); + ret = g_strdup_printf (gtk_form_field_float_get_str_format (GTK_FORM_FIELD_FLOAT (field)), g_strtod (value, NULL)); g_free (value); } @@ -163,7 +177,7 @@ GValue { ret = g_malloc0 (sizeof (GValue)); g_value_init (ret, G_TYPE_FLOAT); - g_value_set_float (ret, strtod (value, NULL)); + g_value_set_float (ret, g_strtod (value, NULL)); g_free (value); } @@ -190,7 +204,7 @@ gchar { char *cur = g_strdup (setlocale (LC_NUMERIC, NULL)); gda_locale_changed (); - gfloat fval = strtod (value, NULL); + gfloat fval = g_strtod (value, NULL); setlocale (LC_NUMERIC, "C"); gda_locale_changed (); @@ -239,7 +253,7 @@ gtk_form_field_float_clear (GtkFormField *field) GtkFormFieldFloatPrivate *priv = GTK_FORM_FIELD_FLOAT_GET_PRIVATE (field); - ret = gtk_form_field_float_set_value_stringify (field, g_strdup_printf ("%f", priv->default_value)); + ret = gtk_form_field_float_set_value_stringify (field, g_strdup_printf (gtk_form_field_float_get_str_format (GTK_FORM_FIELD_FLOAT (field)), priv->default_value)); priv->original_value = priv->default_value; @@ -349,7 +363,7 @@ gtk_form_field_float_is_valid (GtkFormField *field) NULL); if (min_value != NULL - && g_strcmp0 (min_value, "") != 0) + && g_strcmp0 (min_value, "") != 0) { ret = (g_strtod (min_value, NULL) <= value); } @@ -390,7 +404,7 @@ gtk_form_field_float_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, setlocale (LC_NUMERIC, ""); gda_locale_changed (); - ret = gtk_form_field_float_set_value_stringify (field, g_strdup_printf ("%f", value)); + ret = gtk_form_field_float_set_value_stringify (field, g_strdup_printf (gtk_form_field_float_get_str_format (GTK_FORM_FIELD_FLOAT (field)), value)); priv->original_value = value; @@ -428,7 +442,7 @@ gtk_form_field_float_set_from_hashtable (GtkFormField *field, GHashTable *hashta setlocale (LC_NUMERIC, ""); gda_locale_changed (); - ret = gtk_form_field_float_set_value_stringify (field, "0.0"); + ret = gtk_form_field_float_set_value_stringify (field, g_strdup_printf (gtk_form_field_float_get_str_format (GTK_FORM_FIELD_FLOAT (field)), 0.0)); } else { @@ -436,7 +450,7 @@ gtk_form_field_float_set_from_hashtable (GtkFormField *field, GHashTable *hashta setlocale (LC_NUMERIC, ""); gda_locale_changed (); - ret = gtk_form_field_float_set_value_stringify (field, g_strdup_printf ("%f", priv->original_value)); + ret = gtk_form_field_float_set_value_stringify (field, g_strdup_printf (gtk_form_field_float_get_str_format (GTK_FORM_FIELD_FLOAT (field)), priv->original_value)); } } else @@ -469,7 +483,7 @@ gtk_form_field_float_set_value (GtkFormField *field, GValue *gvalue) priv->original_value = g_value_get_float (gvalue); setlocale (LC_NUMERIC, ""); gda_locale_changed (); - ret = gtk_form_field_float_set_value_stringify (field, g_strdup_printf ("%f", priv->original_value)); + ret = gtk_form_field_float_set_value_stringify (field, g_strdup_printf (gtk_form_field_float_get_str_format (GTK_FORM_FIELD_FLOAT (field)), priv->original_value)); return ret; } @@ -491,6 +505,10 @@ gtk_form_field_float_set_property (GObject *object, priv->default_value = g_value_get_float (value); break; + case PROP_DECIMALS: + priv->decimals = g_value_get_uint (value); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -513,6 +531,10 @@ gtk_form_field_float_get_property (GObject *object, g_value_set_float (value, priv->default_value); break; + case PROP_DECIMALS: + g_value_set_uint (value, priv->decimals); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -533,3 +555,11 @@ gtk_form_field_float_set_value_stringify (GtkFormField *field, const gchar *valu return ret; } + +static const gchar +*gtk_form_field_float_get_str_format (GtkFormFieldFloat *field) +{ + GtkFormFieldFloatPrivate *priv = GTK_FORM_FIELD_FLOAT_GET_PRIVATE (field); + + return g_strdup_printf ("%%0%sf", priv->decimals == 0 ? ".0" : (priv->decimals < 0 ? ".2" : g_strdup_printf (".%d", priv->decimals))); +} diff --git a/src/form.c b/src/form.c index e799090..9302be9 100644 --- a/src/form.c +++ b/src/form.c @@ -650,26 +650,26 @@ gtk_form_load_from_xml (GtkForm *form, xmlDoc *xmldoc, GtkBuilder *gtkbuilder) if (xmlStrcmp (node_field->name, (const xmlChar *)"long-name") == 0) { g_object_set (G_OBJECT (field), - "long-name", prop, - NULL); + "long-name", prop, + NULL); } else if (xmlStrcmp (node_field->name, (const xmlChar *)"to-load") == 0) { g_object_set (G_OBJECT (field), - "to-load", gtk_form_field_boolean_str_to_boolean (prop), - NULL); + "to-load", gtk_form_field_boolean_str_to_boolean (prop), + NULL); } else if (xmlStrcmp (node_field->name, (const xmlChar *)"to-save") == 0) { g_object_set (G_OBJECT (field), - "to-save", gtk_form_field_boolean_str_to_boolean (prop), - NULL); + "to-save", gtk_form_field_boolean_str_to_boolean (prop), + NULL); } else if (xmlStrcmp (node_field->name, (const xmlChar *)"obligatory") == 0) { g_object_set (G_OBJECT (field), - "obligatory", gtk_form_field_boolean_str_to_boolean (prop), - NULL); + "obligatory", gtk_form_field_boolean_str_to_boolean (prop), + NULL); } else if (xmlStrcmp (node_field->name, (const xmlChar *)"default") == 0) { @@ -770,6 +770,11 @@ gtk_form_load_from_xml (GtkForm *form, xmlDoc *xmldoc, GtkBuilder *gtkbuilder) { g_object_set (G_OBJECT (field), "max-value", prop, NULL); } + else if (xmlStrcmp (node_field->name, (const xmlChar *)"decimals") == 0 + && g_strcmp0 (type, "float") == 0) + { + g_object_set (G_OBJECT (field), "decimals", strtol (prop, NULL, 10), NULL); + } node_field = node_field->next; } diff --git a/test/database.db b/test/database.db index 8b04301b2e4008704f8113f444c51dd3d3e829e1..c0668b95f803381fe8bdb9c89eb016e01171e97e 100644 GIT binary patch delta 346 zcmZn=Xb_knE$Gg`z`z8=Fu*)f$C%%pLHCgqFHne)={Ez@?~R4MOk7P&jO^m_@{G;F zljWIrN@V6G=jWy>xcT@yhA6nWx;cjWgeVy38BD&$tS!oPiGiIdjDcw$QyB9ZW+rAo z=GjblHx@cDg}5*Tc5!)m z#%B4+tjs$n-(c1hWlmvWXS&3|^p*KE^Gs$(W_9K)rb}S;0ql{?QyKhJ85tQJjd{yb zi&8Qb(lZnD@-mIt81wSEQc{bPi-6j7gn=q#71=o%APnA=loSzW2G0_O%wjz~JtG4{ zLtO(9GGOFn0GVdZz(08vo4Tq5D<^{}NG}6#84xfa^eD1$GN?mjCiAgN3NSFRsWR%a naWZIvt>T}&oLz<6fEi)j4R&=2V True 3 - 8 + 9 2 3 3 @@ -285,6 +285,33 @@ 8 + + + True + 0 + Income + + + 8 + 9 + GTK_FILL + GTK_FILL + + + + + True + True + + + + 1 + 2 + 8 + 9 + GTK_FILL + + False diff --git a/test/test_db.xml b/test/test_db.xml index 1c234fd..f50f034 100644 --- a/test/test_db.xml +++ b/test/test_db.xml @@ -51,22 +51,29 @@ - + - + M - + F + + + 0 + 2 + + + -- 2.49.0