Commit 540f025c authored by Juan Pablo Ugarte's avatar Juan Pablo Ugarte

GladeGtkAdjustment: add digits virtual property

Let the user choose the values precision to use.

Closes issue #18 "Scale Properties Adjustment precision doesn't match Scale Properties Digits"
parent 6a26af57
......@@ -26,15 +26,15 @@
#include <gladeui/glade.h>
void
glade_gtk_adjustment_write_widget (GladeWidgetAdaptor * adaptor,
GladeWidget * widget,
GladeXmlContext * context,
GladeXmlNode * node)
glade_gtk_adjustment_write_widget (GladeWidgetAdaptor *adaptor,
GladeWidget *widget,
GladeXmlContext *context,
GladeXmlNode *node)
{
GladeProperty *prop;
if (!(glade_xml_node_verify_silent (node, GLADE_XML_TAG_WIDGET) ||
glade_xml_node_verify_silent (node, GLADE_XML_TAG_TEMPLATE)))
glade_xml_node_verify_silent (node, GLADE_XML_TAG_TEMPLATE)))
return;
/* Ensure proper order of adjustment properties by writing them here. */
......@@ -49,3 +49,112 @@ glade_gtk_adjustment_write_widget (GladeWidgetAdaptor * adaptor,
GWA_GET_CLASS (G_TYPE_OBJECT)->write_widget (adaptor, widget, context, node);
}
static gint
double_get_precision (double number, gdouble epsilon)
{
char str[G_ASCII_DTOSTR_BUF_SIZE];
int i;
for (i = 0; i <= 20; i++)
{
snprintf (str, G_ASCII_DTOSTR_BUF_SIZE, "%.*f", i, number);
if (ABS (g_ascii_strtod (str, NULL) - number) <= epsilon)
return i;
}
return 20;
}
static gint
get_prop_precision (GladeWidget *widget, gchar *property)
{
GladeProperty *prop = glade_widget_get_property (widget, property);
GladePropertyClass *klass = glade_property_get_class (prop);
GParamSpec *pspec = glade_property_class_get_pspec (klass);
GValue value = G_VALUE_INIT;
glade_property_get_value (prop, &value);
if (G_IS_PARAM_SPEC_FLOAT (pspec))
return double_get_precision (g_value_get_float (&value),
((GParamSpecFloat *)pspec)->epsilon);
else if (G_IS_PARAM_SPEC_DOUBLE (pspec))
return double_get_precision (g_value_get_double (&value),
((GParamSpecDouble *)pspec)->epsilon);
return 0;
}
static gint
get_digits (GladeWidget *widget)
{
gint digits = 2;
digits = MAX (digits, get_prop_precision (widget, "value"));
digits = MAX (digits, get_prop_precision (widget, "lower"));
digits = MAX (digits, get_prop_precision (widget, "upper"));
digits = MAX (digits, get_prop_precision (widget, "page-increment"));
digits = MAX (digits, get_prop_precision (widget, "step-increment"));
return MAX (digits, get_prop_precision (widget, "page-size"));
}
void
glade_gtk_adjustment_read_widget (GladeWidgetAdaptor *adaptor,
GladeWidget *widget,
GladeXmlNode *node)
{
if (!(glade_xml_node_verify_silent (node, GLADE_XML_TAG_WIDGET) ||
glade_xml_node_verify_silent (node, GLADE_XML_TAG_TEMPLATE)))
return;
/* First chain up and read in all the normal properties.. */
GWA_GET_CLASS (GTK_TYPE_WIDGET)->read_widget (adaptor, widget, node);
glade_widget_property_set (widget, "glade-digits", get_digits (widget), NULL);
}
void
glade_gtk_adjustment_set_property (GladeWidgetAdaptor *adaptor,
GObject *object,
const gchar *property_name,
const GValue *value)
{
if (!strcmp (property_name, "glade-digits"))
{
GladeWidget *adjustment = glade_widget_get_from_gobject (object);
gint digits = g_value_get_int (value);
g_object_set (glade_widget_get_property (adjustment, "value"),
"precision", digits, NULL);
g_object_set (glade_widget_get_property (adjustment, "lower"),
"precision", digits, NULL);
g_object_set (glade_widget_get_property (adjustment, "upper"),
"precision", digits, NULL);
g_object_set (glade_widget_get_property (adjustment, "page-increment"),
"precision", digits, NULL);
g_object_set (glade_widget_get_property (adjustment, "step-increment"),
"precision", digits, NULL);
g_object_set (glade_widget_get_property (adjustment, "page-size"),
"precision", digits, NULL);
}
else
GWA_GET_CLASS (G_TYPE_OBJECT)->set_property (adaptor, object,
property_name, value);
}
gboolean
glade_gtk_adjustment_verify_property (GladeWidgetAdaptor *adaptor,
GObject *object,
const gchar *id,
const GValue *value)
{
if (!strcmp (id, "glade-digits"))
{
gint digits = get_digits (glade_widget_get_from_gobject (object));
return g_value_get_int (value) >= digits;
}
return TRUE;
}
......@@ -3697,8 +3697,18 @@
_title="Accel Group" toplevel="True"/>
<glade-widget-class name="GtkAdjustment" generic-name="adjustment"
_title="Adjustment" toplevel="True">
<read-widget-function>glade_gtk_adjustment_read_widget</read-widget-function>
<write-widget-function>glade_gtk_adjustment_write_widget</write-widget-function>
<set-property-function>glade_gtk_adjustment_set_property</set-property-function>
<verify-function>glade_gtk_adjustment_verify_property</verify-function>
<properties>
<property id="glade-digits" _name="Digits" save="False" weight="1.0">
<parameter-spec>
<type>GParamInt</type>
<min>2</min>
<max>20</max>
</parameter-spec>
</property>
<property id="value" default="0.0" save="False"/>
<property id="lower" default="0.0" save="False"/>
<property id="upper" default="100.0" save="False"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment