276 lines
7.6 KiB
C
276 lines
7.6 KiB
C
/*
|
|
* Copyright (C) 2012 - Juan Ferrer Toribio
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "vn-column-spin.h"
|
|
#include "../vn-grid-model.h"
|
|
|
|
G_DEFINE_TYPE (VnColumnSpin, vn_column_spin, VN_TYPE_COLUMN);
|
|
|
|
static void vn_column_spin_on_edited (GtkCellRendererSpin * cell,
|
|
const gchar * path, gchar * text, VnColumnSpin * obj)
|
|
{
|
|
GValue value = G_VALUE_INIT;
|
|
DbModelStatus status = db_model_get_status (vn_column_get_model (VN_COLUMN (obj)));
|
|
|
|
if (status != DB_MODEL_STATUS_READY)
|
|
return;
|
|
|
|
if (g_strcmp0 (text, ""))
|
|
{
|
|
gchar * err;
|
|
|
|
if (!err || *err == '\0')
|
|
{
|
|
gdouble val, min, max;
|
|
|
|
g_object_get (obj->adjustment, "lower", &min, "upper", &max, NULL);
|
|
val = CLAMP (g_strtod (text, &err), min, max);
|
|
g_value_init (&value, G_TYPE_DOUBLE);
|
|
g_value_set_double (&value, val);
|
|
g_object_set (obj->adjustment, "value", val, NULL);
|
|
}
|
|
else
|
|
return;
|
|
}
|
|
else
|
|
g_value_init (&value, GVN_TYPE_NULL);
|
|
|
|
VN_COLUMN_GET_CLASS (obj)->value_changed (VN_COLUMN (obj), path, &value);
|
|
g_value_unset (&value);
|
|
}
|
|
|
|
void vn_column_spin_on_editing_started (GtkCellRenderer * renderer,
|
|
GtkEntry * entry, gchar * path, GtkTreeViewColumn * obj)
|
|
{
|
|
GtkTreeIter iter;
|
|
GtkTreeModel * model;
|
|
GtkTreeView * tree;
|
|
|
|
if (!GTK_IS_SPIN_BUTTON (entry))
|
|
return;
|
|
|
|
tree = GTK_TREE_VIEW (gtk_tree_view_column_get_tree_view (obj));
|
|
model = gtk_tree_view_get_model (tree);
|
|
|
|
if (gtk_tree_model_get_iter_from_string (model, &iter, path))
|
|
{
|
|
GValue value = G_VALUE_INIT;
|
|
gint col = vn_column_get_column_index (VN_COLUMN (obj));
|
|
|
|
gtk_tree_model_get_value (model, &iter, col, &value);
|
|
|
|
if (gvn_value_is_null (&value))
|
|
gtk_entry_set_text (entry, "");
|
|
}
|
|
}
|
|
|
|
static void vn_column_spin_set_editable (VnColumn * obj, gboolean editable)
|
|
{
|
|
if (editable)
|
|
{
|
|
g_signal_connect (obj->cell, "editing-started",
|
|
G_CALLBACK (vn_column_spin_on_editing_started), obj);
|
|
g_signal_connect (obj->cell, "edited",
|
|
G_CALLBACK (vn_column_spin_on_edited), obj);
|
|
}
|
|
else
|
|
{
|
|
g_signal_handlers_disconnect_by_func (obj->cell,
|
|
vn_column_spin_on_editing_started, obj);
|
|
g_signal_handlers_disconnect_by_func (obj->cell,
|
|
vn_column_spin_on_edited, obj);
|
|
}
|
|
|
|
g_object_set (obj->cell, "editable", editable, NULL);
|
|
}
|
|
|
|
static void vn_column_spin_set_value (VnColumnSpin * obj, GtkTreeModel * model,
|
|
GtkTreeIter * iter, GObject * cell, const GValue * value)
|
|
{
|
|
guint digits;
|
|
GType type = G_VALUE_TYPE (value);
|
|
GValue new_value = G_VALUE_INIT;
|
|
|
|
g_value_init (&new_value, G_TYPE_STRING);
|
|
g_object_get (cell, "digits", &digits, NULL);
|
|
|
|
switch (type)
|
|
{
|
|
case G_TYPE_FLOAT:
|
|
case G_TYPE_DOUBLE:
|
|
{
|
|
gdouble dec;
|
|
gchar buffer [G_ASCII_DTOSTR_BUF_SIZE];
|
|
|
|
if (type == G_TYPE_FLOAT)
|
|
dec = (gdouble) g_value_get_float (value);
|
|
else
|
|
dec = g_value_get_double (value);
|
|
|
|
g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, obj->format, dec);
|
|
g_value_set_string (&new_value, buffer);
|
|
|
|
break;
|
|
}
|
|
default:
|
|
if (!gvn_value_is_null (value))
|
|
g_value_transform (value, &new_value);
|
|
else
|
|
g_value_set_string (&new_value, "");
|
|
}
|
|
|
|
g_object_set_property (cell, "text", &new_value);
|
|
g_value_unset (&new_value);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
enum
|
|
{
|
|
PROP_CLIMB_RATE = 1
|
|
,PROP_DIGITS
|
|
,PROP_LOWER
|
|
,PROP_UPPER
|
|
,PROP_STEP
|
|
};
|
|
|
|
static void vn_column_spin_set_property (VnColumnSpin * obj, guint id,
|
|
const GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_CLIMB_RATE:
|
|
g_object_set (VN_COLUMN (obj)->cell, "climb-rate", g_value_get_double (value), NULL);
|
|
break;
|
|
case PROP_DIGITS:
|
|
{
|
|
guint digits = g_value_get_uint (value);
|
|
g_object_set (VN_COLUMN (obj)->cell, "digits", digits, NULL);
|
|
g_free (obj->format);
|
|
obj->format = g_strdup_printf ("%%.%df", digits);
|
|
break;
|
|
}
|
|
case PROP_LOWER:
|
|
g_object_set (obj->adjustment, "lower", g_value_get_double (value), NULL);
|
|
break;
|
|
case PROP_UPPER:
|
|
g_object_set (obj->adjustment, "upper", g_value_get_double (value), NULL);
|
|
break;
|
|
case PROP_STEP:
|
|
g_object_set (obj->adjustment,
|
|
"step-increment", g_value_get_double (value),
|
|
"page-increment", g_value_get_double (value) * 10,
|
|
NULL);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
|
|
}
|
|
}
|
|
|
|
static void vn_column_spin_get_property (VnColumnSpin * obj, guint id,
|
|
GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_CLIMB_RATE:
|
|
g_object_get_property (G_OBJECT (VN_COLUMN (obj)->cell), "climb-rate", value);
|
|
break;
|
|
case PROP_DIGITS:
|
|
g_object_get_property (G_OBJECT (VN_COLUMN (obj)->cell), "digits", value);
|
|
break;
|
|
case PROP_LOWER:
|
|
g_object_get_property (G_OBJECT (obj->adjustment), "lower", value);
|
|
break;
|
|
case PROP_UPPER:
|
|
g_object_get_property (G_OBJECT (obj->adjustment), "upper", value);
|
|
break;
|
|
case PROP_STEP:
|
|
g_object_get_property (G_OBJECT (obj->adjustment), "step-increment", value);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
|
|
}
|
|
}
|
|
|
|
static void vn_column_spin_init (VnColumnSpin * obj)
|
|
{
|
|
GtkCellRenderer * cell = gtk_cell_renderer_spin_new ();
|
|
|
|
obj->adjustment = gtk_adjustment_new (0.0, G_MININT, G_MAXINT, 0.0, 1.0, 0.0);
|
|
g_object_set (cell, "adjustment", obj->adjustment, NULL);
|
|
|
|
obj->format = NULL;
|
|
|
|
VN_COLUMN_GET_CLASS (obj)->set_renderer (VN_COLUMN (obj), cell);
|
|
}
|
|
|
|
static void vn_column_spin_finalize (VnColumnSpin * obj)
|
|
{
|
|
G_OBJECT_CLASS (vn_column_spin_parent_class)->finalize (G_OBJECT (obj));
|
|
}
|
|
|
|
static void vn_column_spin_class_init (VnColumnSpinClass * klass)
|
|
{
|
|
GObjectClass * k = G_OBJECT_CLASS (klass);
|
|
VnColumnClass * col_k = VN_COLUMN_CLASS (klass);
|
|
k->finalize = (GObjectFinalizeFunc) vn_column_spin_finalize;
|
|
k->set_property = (GObjectSetPropertyFunc) vn_column_spin_set_property;
|
|
k->get_property = (GObjectGetPropertyFunc) vn_column_spin_get_property;
|
|
col_k->set_value = (VnColumnSetValueFunc) vn_column_spin_set_value;
|
|
col_k->set_editable = (VnColumnSetEditableFunc) vn_column_spin_set_editable;
|
|
|
|
g_object_class_install_property (k, PROP_CLIMB_RATE,
|
|
g_param_spec_double ("climb-rate"
|
|
,_("Climb rate")
|
|
,_("The acceleration rate when you hold down a button.")
|
|
,0.0 ,G_MAXDOUBLE ,0.0
|
|
,G_PARAM_CONSTRUCT | G_PARAM_READWRITE
|
|
));
|
|
|
|
g_object_class_install_property (k, PROP_DIGITS,
|
|
g_param_spec_uint ("digits"
|
|
,_("Digits")
|
|
,_("The number of decimal places to display.")
|
|
,0 ,20 ,0
|
|
,G_PARAM_CONSTRUCT | G_PARAM_READWRITE
|
|
));
|
|
|
|
g_object_class_install_property (k, PROP_LOWER,
|
|
g_param_spec_double ("lower"
|
|
,_("Minimum value")
|
|
,_("The minimum value of the internal adjustment.")
|
|
,-G_MAXDOUBLE ,G_MAXDOUBLE ,-G_MAXDOUBLE
|
|
,G_PARAM_CONSTRUCT | G_PARAM_READWRITE
|
|
));
|
|
|
|
g_object_class_install_property (k, PROP_UPPER,
|
|
g_param_spec_double ("upper"
|
|
,_("Maximum value")
|
|
,_("The maximum value of the internal adjustment.")
|
|
,-G_MAXDOUBLE ,G_MAXDOUBLE ,G_MAXDOUBLE
|
|
,G_PARAM_CONSTRUCT | G_PARAM_READWRITE
|
|
));
|
|
|
|
g_object_class_install_property (k, PROP_STEP,
|
|
g_param_spec_double ("step-increment"
|
|
,_("Step Increment")
|
|
,_("The step increment of the internal adjustment")
|
|
,0.00000000000000000001 ,10000000000.0 ,1.0
|
|
,G_PARAM_CONSTRUCT | G_PARAM_READWRITE
|
|
));
|
|
}
|