2006-03-22 15:28:16 +00:00
|
|
|
/* GtkCellRendererSpin
|
|
|
|
* Copyright (C) 2004 Lorenzo Gil Sanchez
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2006-03-22 15:28:16 +00:00
|
|
|
*
|
|
|
|
* Authors: Lorenzo Gil Sanchez <lgs@sicem.biz>
|
|
|
|
* Carlos Garnacho Parro <carlosg@gnome.org>
|
|
|
|
*/
|
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
#include "config.h"
|
2011-01-04 17:21:41 +00:00
|
|
|
|
2011-11-02 05:15:21 +00:00
|
|
|
#include "gtkcellrendererspin.h"
|
|
|
|
|
2012-03-03 18:41:55 +00:00
|
|
|
#include "gtkadjustment.h"
|
2006-03-22 15:28:16 +00:00
|
|
|
#include "gtkintl.h"
|
2006-03-22 16:17:52 +00:00
|
|
|
#include "gtkprivate.h"
|
2006-03-22 15:28:16 +00:00
|
|
|
#include "gtkspinbutton.h"
|
2017-05-04 16:33:27 +00:00
|
|
|
#include "gtkentry.h"
|
2018-03-11 12:56:02 +00:00
|
|
|
#include "gtkeventcontrollerkey.h"
|
2006-03-22 15:28:16 +00:00
|
|
|
|
|
|
|
|
2011-04-11 21:14:35 +00:00
|
|
|
/**
|
2021-03-02 03:38:34 +00:00
|
|
|
* GtkCellRendererSpin:
|
|
|
|
*
|
|
|
|
* Renders a spin button in a cell
|
2011-04-11 21:14:35 +00:00
|
|
|
*
|
2021-05-20 13:17:04 +00:00
|
|
|
* `GtkCellRendererSpin` renders text in a cell like `GtkCellRendererText` from
|
|
|
|
* which it is derived. But while `GtkCellRendererText` offers a simple entry to
|
|
|
|
* edit the text, `GtkCellRendererSpin` offers a `GtkSpinButton` widget. Of course,
|
2011-04-11 21:14:35 +00:00
|
|
|
* that means that the text has to be parseable as a floating point number.
|
|
|
|
*
|
|
|
|
* The range of the spinbutton is taken from the adjustment property of the
|
|
|
|
* cell renderer, which can be set explicitly or mapped to a column in the
|
2021-05-20 13:17:04 +00:00
|
|
|
* tree model, like all properties of cell renders. `GtkCellRendererSpin`
|
|
|
|
* also has properties for the `GtkCellRendererSpin:climb-rate` and the number
|
|
|
|
* of `GtkCellRendererSpin:digits` to display. Other `GtkSpinButton` properties
|
|
|
|
* can be set in a handler for the `GtkCellRenderer::editing-started` signal.
|
2011-04-11 21:14:35 +00:00
|
|
|
*
|
2021-05-20 13:17:04 +00:00
|
|
|
* The `GtkCellRendererSpin` cell renderer was added in GTK 2.10.
|
2011-04-11 21:14:35 +00:00
|
|
|
*/
|
|
|
|
|
2019-05-27 00:35:26 +00:00
|
|
|
typedef struct _GtkCellRendererSpinClass GtkCellRendererSpinClass;
|
|
|
|
typedef struct _GtkCellRendererSpinPrivate GtkCellRendererSpinPrivate;
|
|
|
|
|
|
|
|
struct _GtkCellRendererSpin
|
|
|
|
{
|
|
|
|
GtkCellRendererText parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkCellRendererSpinClass
|
|
|
|
{
|
|
|
|
GtkCellRendererTextClass parent;
|
|
|
|
};
|
2011-04-11 21:14:35 +00:00
|
|
|
|
2006-03-22 15:28:16 +00:00
|
|
|
struct _GtkCellRendererSpinPrivate
|
|
|
|
{
|
2019-12-24 08:33:02 +00:00
|
|
|
GtkWidget *spin;
|
2006-03-22 15:28:16 +00:00
|
|
|
GtkAdjustment *adjustment;
|
2020-07-24 20:32:16 +00:00
|
|
|
double climb_rate;
|
2006-03-22 15:28:16 +00:00
|
|
|
guint digits;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void gtk_cell_renderer_spin_finalize (GObject *object);
|
|
|
|
|
|
|
|
static void gtk_cell_renderer_spin_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *spec);
|
|
|
|
static void gtk_cell_renderer_spin_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *spec);
|
|
|
|
|
2018-03-11 12:56:02 +00:00
|
|
|
static gboolean gtk_cell_renderer_spin_key_pressed (GtkEventControllerKey *controller,
|
|
|
|
guint keyval,
|
|
|
|
guint keycode,
|
|
|
|
GdkModifierType state,
|
|
|
|
GtkWidget *widget);
|
|
|
|
|
2006-03-22 15:28:16 +00:00
|
|
|
static GtkCellEditable * gtk_cell_renderer_spin_start_editing (GtkCellRenderer *cell,
|
|
|
|
GdkEvent *event,
|
|
|
|
GtkWidget *widget,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *path,
|
2010-10-04 14:22:09 +00:00
|
|
|
const GdkRectangle *background_area,
|
|
|
|
const GdkRectangle *cell_area,
|
2006-03-22 15:28:16 +00:00
|
|
|
GtkCellRendererState flags);
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_ADJUSTMENT,
|
|
|
|
PROP_CLIMB_RATE,
|
|
|
|
PROP_DIGITS
|
|
|
|
};
|
|
|
|
|
|
|
|
#define GTK_CELL_RENDERER_SPIN_PATH "gtk-cell-renderer-spin-path"
|
|
|
|
|
2013-06-27 19:02:52 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GtkCellRendererSpin, gtk_cell_renderer_spin, GTK_TYPE_CELL_RENDERER_TEXT)
|
2006-03-22 15:28:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_cell_renderer_spin_class_init (GtkCellRendererSpinClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->finalize = gtk_cell_renderer_spin_finalize;
|
|
|
|
object_class->get_property = gtk_cell_renderer_spin_get_property;
|
|
|
|
object_class->set_property = gtk_cell_renderer_spin_set_property;
|
|
|
|
|
|
|
|
cell_class->start_editing = gtk_cell_renderer_spin_start_editing;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkCellRendererSpin:adjustment:
|
|
|
|
*
|
|
|
|
* The adjustment that holds the value of the spinbutton.
|
|
|
|
* This must be non-%NULL for the cell renderer to be editable.
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_ADJUSTMENT,
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_object ("adjustment", NULL, NULL,
|
2006-03-22 15:28:16 +00:00
|
|
|
GTK_TYPE_ADJUSTMENT,
|
|
|
|
GTK_PARAM_READWRITE));
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkCellRendererSpin:climb-rate:
|
|
|
|
*
|
|
|
|
* The acceleration rate when you hold down a button.
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_CLIMB_RATE,
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_double ("climb-rate", NULL, NULL,
|
2006-03-22 15:28:16 +00:00
|
|
|
0.0, G_MAXDOUBLE, 0.0,
|
|
|
|
GTK_PARAM_READWRITE));
|
|
|
|
/**
|
|
|
|
* GtkCellRendererSpin:digits:
|
|
|
|
*
|
|
|
|
* The number of decimal places to display.
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_DIGITS,
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_uint ("digits", NULL, NULL,
|
2006-03-22 15:28:16 +00:00
|
|
|
0, 20, 0,
|
2014-06-08 15:27:41 +00:00
|
|
|
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
|
2006-03-22 15:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_cell_renderer_spin_init (GtkCellRendererSpin *self)
|
|
|
|
{
|
2019-05-27 00:37:42 +00:00
|
|
|
GtkCellRendererSpinPrivate *priv = gtk_cell_renderer_spin_get_instance_private (self);
|
2006-03-22 15:28:16 +00:00
|
|
|
|
|
|
|
priv->adjustment = NULL;
|
|
|
|
priv->climb_rate = 0.0;
|
|
|
|
priv->digits = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_cell_renderer_spin_finalize (GObject *object)
|
|
|
|
{
|
2019-05-27 00:37:42 +00:00
|
|
|
GtkCellRendererSpinPrivate *priv = gtk_cell_renderer_spin_get_instance_private (GTK_CELL_RENDERER_SPIN (object));
|
2006-03-22 15:28:16 +00:00
|
|
|
|
2019-12-24 08:33:02 +00:00
|
|
|
g_clear_object (&priv->adjustment);
|
|
|
|
g_clear_object (&priv->spin);
|
2006-03-22 15:28:16 +00:00
|
|
|
|
2008-08-07 14:21:47 +00:00
|
|
|
G_OBJECT_CLASS (gtk_cell_renderer_spin_parent_class)->finalize (object);
|
2006-03-22 15:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_cell_renderer_spin_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2019-05-27 00:37:42 +00:00
|
|
|
GtkCellRendererSpinPrivate *priv = gtk_cell_renderer_spin_get_instance_private (GTK_CELL_RENDERER_SPIN (object));
|
2006-03-22 15:28:16 +00:00
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_ADJUSTMENT:
|
|
|
|
g_value_set_object (value, priv->adjustment);
|
|
|
|
break;
|
|
|
|
case PROP_CLIMB_RATE:
|
|
|
|
g_value_set_double (value, priv->climb_rate);
|
|
|
|
break;
|
|
|
|
case PROP_DIGITS:
|
|
|
|
g_value_set_uint (value, priv->digits);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_cell_renderer_spin_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2019-05-27 00:37:42 +00:00
|
|
|
GtkCellRendererSpinPrivate *priv = gtk_cell_renderer_spin_get_instance_private (GTK_CELL_RENDERER_SPIN (object));
|
2006-03-22 15:28:16 +00:00
|
|
|
GObject *obj;
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_ADJUSTMENT:
|
|
|
|
obj = g_value_get_object (value);
|
|
|
|
|
|
|
|
if (priv->adjustment)
|
|
|
|
{
|
|
|
|
g_object_unref (priv->adjustment);
|
|
|
|
priv->adjustment = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj)
|
2017-12-08 22:48:47 +00:00
|
|
|
priv->adjustment = GTK_ADJUSTMENT (g_object_ref_sink (obj));
|
|
|
|
|
2006-03-22 15:28:16 +00:00
|
|
|
break;
|
|
|
|
case PROP_CLIMB_RATE:
|
|
|
|
priv->climb_rate = g_value_get_double (value);
|
|
|
|
break;
|
|
|
|
case PROP_DIGITS:
|
2014-06-08 15:27:41 +00:00
|
|
|
if (priv->digits != g_value_get_uint (value))
|
|
|
|
{
|
|
|
|
priv->digits = g_value_get_uint (value);
|
|
|
|
g_object_notify_by_pspec (object, pspec);
|
|
|
|
}
|
2006-03-22 15:28:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 20:09:26 +00:00
|
|
|
static void
|
|
|
|
gtk_cell_renderer_spin_focus_changed (GtkWidget *widget,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
gpointer data)
|
2006-03-22 15:28:16 +00:00
|
|
|
{
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *path;
|
|
|
|
const char *new_text;
|
2006-03-22 15:28:16 +00:00
|
|
|
gboolean canceled;
|
|
|
|
|
2018-01-06 20:09:26 +00:00
|
|
|
if (gtk_widget_has_focus (widget))
|
|
|
|
return;
|
|
|
|
|
2019-12-24 08:33:02 +00:00
|
|
|
g_object_get (widget, "editing-canceled", &canceled, NULL);
|
2006-03-22 15:28:16 +00:00
|
|
|
|
|
|
|
g_signal_handlers_disconnect_by_func (widget,
|
2018-01-06 20:09:26 +00:00
|
|
|
gtk_cell_renderer_spin_focus_changed,
|
2006-03-22 15:28:16 +00:00
|
|
|
data);
|
|
|
|
|
|
|
|
gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
|
|
|
|
|
2019-12-24 08:33:02 +00:00
|
|
|
if (canceled)
|
|
|
|
return;
|
2006-03-22 15:28:16 +00:00
|
|
|
|
2019-12-24 08:33:02 +00:00
|
|
|
path = g_object_get_data (G_OBJECT (widget), GTK_CELL_RENDERER_SPIN_PATH);
|
|
|
|
new_text = gtk_editable_get_text (GTK_EDITABLE (widget));
|
|
|
|
g_signal_emit_by_name (data, "edited", path, new_text);
|
2006-03-22 15:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2018-03-11 12:56:02 +00:00
|
|
|
gtk_cell_renderer_spin_key_pressed (GtkEventControllerKey *controller,
|
|
|
|
guint keyval,
|
|
|
|
guint keycode,
|
|
|
|
GdkModifierType state,
|
|
|
|
GtkWidget *widget)
|
2006-03-22 15:28:16 +00:00
|
|
|
{
|
2017-08-25 14:44:04 +00:00
|
|
|
if (state == 0)
|
2006-03-22 15:28:16 +00:00
|
|
|
{
|
2017-08-25 14:44:04 +00:00
|
|
|
if (keyval == GDK_KEY_Up)
|
2006-03-22 15:28:16 +00:00
|
|
|
{
|
|
|
|
gtk_spin_button_spin (GTK_SPIN_BUTTON (widget), GTK_SPIN_STEP_FORWARD, 1);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2017-08-25 14:44:04 +00:00
|
|
|
else if (keyval == GDK_KEY_Down)
|
2006-03-22 15:28:16 +00:00
|
|
|
{
|
|
|
|
gtk_spin_button_spin (GTK_SPIN_BUTTON (widget), GTK_SPIN_STEP_BACKWARD, 1);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-12-24 08:33:02 +00:00
|
|
|
static void
|
|
|
|
gtk_cell_renderer_spin_editing_done (GtkSpinButton *spin,
|
|
|
|
GtkCellRendererSpin *cell)
|
|
|
|
{
|
|
|
|
GtkCellRendererSpinPrivate *priv = gtk_cell_renderer_spin_get_instance_private (GTK_CELL_RENDERER_SPIN (cell));
|
|
|
|
gboolean canceled;
|
|
|
|
const char *path;
|
|
|
|
const char *new_text;
|
|
|
|
|
|
|
|
g_clear_object (&priv->spin);
|
|
|
|
|
|
|
|
g_object_get (spin, "editing-canceled", &canceled, NULL);
|
|
|
|
gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (cell), canceled);
|
|
|
|
|
|
|
|
if (canceled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
path = g_object_get_data (G_OBJECT (spin), GTK_CELL_RENDERER_SPIN_PATH);
|
|
|
|
new_text = gtk_editable_get_text (GTK_EDITABLE (spin));
|
|
|
|
g_signal_emit_by_name (cell, "edited", path, new_text);
|
|
|
|
}
|
|
|
|
|
2006-03-22 15:28:16 +00:00
|
|
|
static GtkCellEditable *
|
2010-10-04 14:22:09 +00:00
|
|
|
gtk_cell_renderer_spin_start_editing (GtkCellRenderer *cell,
|
|
|
|
GdkEvent *event,
|
|
|
|
GtkWidget *widget,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *path,
|
2010-10-04 14:22:09 +00:00
|
|
|
const GdkRectangle *background_area,
|
|
|
|
const GdkRectangle *cell_area,
|
|
|
|
GtkCellRendererState flags)
|
2006-03-22 15:28:16 +00:00
|
|
|
{
|
2019-05-27 00:37:42 +00:00
|
|
|
GtkCellRendererSpinPrivate *priv = gtk_cell_renderer_spin_get_instance_private (GTK_CELL_RENDERER_SPIN (cell));
|
|
|
|
GtkCellRendererText *cell_text = GTK_CELL_RENDERER_TEXT (cell);
|
2018-03-11 12:56:02 +00:00
|
|
|
GtkEventController *key_controller;
|
2010-06-01 19:01:33 +00:00
|
|
|
gboolean editable;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *text;
|
2006-03-22 15:28:16 +00:00
|
|
|
|
2010-06-01 19:01:33 +00:00
|
|
|
g_object_get (cell_text, "editable", &editable, NULL);
|
|
|
|
if (!editable)
|
2006-03-22 15:28:16 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!priv->adjustment)
|
|
|
|
return NULL;
|
|
|
|
|
2019-12-24 08:33:02 +00:00
|
|
|
priv->spin = gtk_spin_button_new (priv->adjustment, priv->climb_rate, priv->digits);
|
|
|
|
g_object_ref_sink (priv->spin);
|
2006-03-22 15:28:16 +00:00
|
|
|
|
2010-06-01 19:01:33 +00:00
|
|
|
g_object_get (cell_text, "text", &text, NULL);
|
|
|
|
if (text)
|
2010-12-20 15:02:00 +00:00
|
|
|
{
|
2019-12-24 08:33:02 +00:00
|
|
|
gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->spin), g_strtod (text, NULL));
|
2010-12-20 15:02:00 +00:00
|
|
|
g_free (text);
|
|
|
|
}
|
2006-03-22 15:28:16 +00:00
|
|
|
|
2018-04-20 17:58:06 +00:00
|
|
|
key_controller = gtk_event_controller_key_new ();
|
2018-03-11 12:56:02 +00:00
|
|
|
g_signal_connect (key_controller, "key-pressed",
|
2019-12-24 08:33:02 +00:00
|
|
|
G_CALLBACK (gtk_cell_renderer_spin_key_pressed), priv->spin);
|
|
|
|
gtk_widget_add_controller (priv->spin, key_controller);
|
2018-03-11 12:56:02 +00:00
|
|
|
|
2019-12-24 08:33:02 +00:00
|
|
|
g_object_set_data_full (G_OBJECT (priv->spin), GTK_CELL_RENDERER_SPIN_PATH,
|
2006-03-22 15:28:16 +00:00
|
|
|
g_strdup (path), g_free);
|
|
|
|
|
2019-12-24 08:33:02 +00:00
|
|
|
g_signal_connect (priv->spin, "editing-done",
|
|
|
|
G_CALLBACK (gtk_cell_renderer_spin_editing_done), cell);
|
2006-03-22 15:28:16 +00:00
|
|
|
|
2019-12-24 08:33:02 +00:00
|
|
|
g_signal_connect (priv->spin, "notify::has-focus",
|
|
|
|
G_CALLBACK (gtk_cell_renderer_spin_focus_changed), cell);
|
2006-03-22 15:28:16 +00:00
|
|
|
|
2019-12-24 08:33:02 +00:00
|
|
|
return GTK_CELL_EDITABLE (priv->spin);
|
2006-03-22 15:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_cell_renderer_spin_new:
|
|
|
|
*
|
2021-05-20 13:17:04 +00:00
|
|
|
* Creates a new `GtkCellRendererSpin`.
|
2006-03-22 15:28:16 +00:00
|
|
|
*
|
2021-05-20 13:17:04 +00:00
|
|
|
* Returns: a new `GtkCellRendererSpin`
|
2006-03-22 15:28:16 +00:00
|
|
|
*/
|
|
|
|
GtkCellRenderer *
|
|
|
|
gtk_cell_renderer_spin_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_CELL_RENDERER_SPIN, NULL);
|
|
|
|
}
|