<!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
<!ELEMENT trim (#PCDATA)>
<!ELEMENT min-value (#PCDATA)>
<!ELEMENT max-value (#PCDATA)>
+<!ELEMENT decimals (#PCDATA)>
<!ELEMENT group (widget*, field*)>
enum
{
PROP_0,
- PROP_DEFAULT
+ PROP_DEFAULT,
+ PROP_DECIMALS
};
static void gtk_form_field_float_class_init (GtkFormFieldFloatClass *klass);
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))
{
gfloat default_value;
gfloat original_value;
+
+ guint decimals;
};
-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));
}
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,
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);
}
{
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);
}
{
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 ();
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;
NULL);
if (min_value != NULL
- && g_strcmp0 (min_value, "") != 0)
+ && g_strcmp0 (min_value, "") != 0)
{
ret = (g_strtod (min_value, NULL) <= value);
}
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;
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
{
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
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;
}
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;
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;
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)));
+}
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)
{
{
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;
}
<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">•</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>
</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>