]> saetta.ns0.it Git - libgtkform/commitdiff
Added property decimals to GtkForFieldFloat.
authorAndrea Zagli <azagli@libero.it>
Wed, 6 Apr 2011 15:34:29 +0000 (17:34 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 6 Apr 2011 15:34:29 +0000 (17:34 +0200)
data/gtkform.dtd
src/fieldfloat.c
src/form.c
test/database.db
test/test_db.gui
test/test_db.xml

index cc919025c3d3c1c4e7250f2e4bd6066f60ca69e2..caac1654a842a949c859dd26093ff4b18a723c3a 100644 (file)
@@ -31,7 +31,7 @@
 
 <!ELEMENT widget-name (#PCDATA)>
 
-<!ELEMENT field (long-name?, to-load?, to-save?, obligatory?, default?, is-key?, auto-increment?, datetime-type?, display-format?, trim?, min-value?, max-value?)>
+<!ELEMENT field (long-name?, to-load?, to-save?, obligatory?, default?, is-key?, auto-increment?, datetime-type?, display-format?, trim?, min-value?, max-value?, decimals?)>
 
 <!ATTLIST field
        type  (boolean | datetime | float | integer | text)  #REQUIRED
@@ -50,6 +50,7 @@
 <!ELEMENT trim (#PCDATA)>
 <!ELEMENT min-value (#PCDATA)>
 <!ELEMENT max-value (#PCDATA)>
+<!ELEMENT decimals (#PCDATA)>
 
 <!ELEMENT group (widget*, field*)>
 
index d2c5baba60961d2de0a542055a3dbed55606e02b..046ff5679195e63ed0fd25f8759b03a4389676ff 100644 (file)
@@ -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)));
+}
index e799090d382b5ff2f1674abfc3e971c658eee20d..9302be91799c58e9873ef2e4cd265241e6bfc2c5 100644 (file)
@@ -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;
                                                                                                                                                }
index 8b04301b2e4008704f8113f444c51dd3d3e829e1..c0668b95f803381fe8bdb9c89eb016e01171e97e 100644 (file)
Binary files a/test/database.db and b/test/database.db differ
index 194dd96ded9970a9d2dcefdadbfa28bb115b4641..bdeaf3f17311b256089161718812290ecf20dc86 100644 (file)
@@ -17,7 +17,7 @@
           <object class="GtkTable" id="table1">
             <property name="visible">True</property>
             <property name="border_width">3</property>
-            <property name="n_rows">8</property>
+            <property name="n_rows">9</property>
             <property name="n_columns">2</property>
             <property name="column_spacing">3</property>
             <property name="row_spacing">3</property>
                 <property name="bottom_attach">8</property>
               </packing>
             </child>
+            <child>
+              <object class="GtkLabel" id="label5">
+                <property name="visible">True</property>
+                <property name="xalign">0</property>
+                <property name="label" translatable="yes">Income</property>
+              </object>
+              <packing>
+                <property name="top_attach">8</property>
+                <property name="bottom_attach">9</property>
+                <property name="x_options">GTK_FILL</property>
+                <property name="y_options">GTK_FILL</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkEntry" id="txt_income">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="invisible_char">&#x2022;</property>
+              </object>
+              <packing>
+                <property name="left_attach">1</property>
+                <property name="right_attach">2</property>
+                <property name="top_attach">8</property>
+                <property name="bottom_attach">9</property>
+                <property name="y_options">GTK_FILL</property>
+              </packing>
+            </child>
           </object>
           <packing>
             <property name="expand">False</property>
index 1c234fd1829659caf2c084a16b006876cdbf5c58..f50f0345e2ca11741502e6f02331cdcf20d1d31b 100644 (file)
                </field>
        </widget>
 
-       <widget type="radio" name="rbtnNone">
+       <widget type="radio" name="rbtnNone" label="label3">
                <return-value />
                <field type="text" name="sex">
                </field>
        </widget>
 
-       <widget type="radio" name="rbtnMale">
+       <widget type="radio" name="rbtnMale" label="label3">
                <return-value>M</return-value>
                <field type="text" name="sex">
                </field>
        </widget>
 
-       <widget type="radio" name="rbtnFemale">
+       <widget type="radio" name="rbtnFemale" label="label3">
                <return-value>F</return-value>
                <field type="text" name="sex">
                </field>
        </widget>
 
+       <widget type="entry" name="txt_income" label="label5">
+               <field type="float" name="income">
+                       <min-value>0</min-value>
+                       <decimals>2</decimals>
+               </field>
+       </widget>
+
 </gtkform>