Add a cell renderer which uses a spinbutton to edit the value of a cell.

2006-03-22  Matthias Clasen  <mclasen@redhat.com>

	* gtk/gtk.symbols:
	* gtk/gtkcellrendererspin.h:
	* gtk/gtkcellrendererspin.c: Add a cell renderer which
	uses a spinbutton to edit the value of a cell. (#156023,
	Daniel Kasak, Lorenzo Gil Sanchez, Carlos Garnacho)

	* gtk/gtk.h:
	* gtk/Makefile.am: Glue it in.
This commit is contained in:
Matthias Clasen 2006-03-22 15:28:16 +00:00 committed by Matthias Clasen
parent eb313fc8e4
commit 0047b0b98d
9 changed files with 454 additions and 0 deletions

View File

@ -1,3 +1,19 @@
2006-03-22 Matthias Clasen <mclasen@redhat.com>
* gtk/gtk.symbols:
* gtk/gtkcellrendererspin.h:
* gtk/gtkcellrendererspin.c: Add a cell renderer which
uses a spinbutton to edit the value of a cell. (#156023,
Daniel Kasak, Lorenzo Gil Sanchez, Carlos Garnacho)
* gtk/gtk.h:
* gtk/Makefile.am: Glue it in.
2006-03-21 Matthias Clasen <mclasen@redhat.com>
* tests/testtreeview.c (drag_begin_cb): Test setting up
drag icons for tree dnd.
2006-03-22 Michael Natterer <mitch@imendio.com>
* gtk/gtkentry.c: applied modified patch from maemo-gtk which
@ -106,6 +122,7 @@
(gtk_notebook_destroy): Leave the removal of the children
to the container destroy method. (#319032, Yevgen Muntyan)
>>>>>>> 1.7625
2006-03-20 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkcombobox.c (gtk_combo_box_remove): Use the existing

View File

@ -1,3 +1,19 @@
2006-03-22 Matthias Clasen <mclasen@redhat.com>
* gtk/gtk.symbols:
* gtk/gtkcellrendererspin.h:
* gtk/gtkcellrendererspin.c: Add a cell renderer which
uses a spinbutton to edit the value of a cell. (#156023,
Daniel Kasak, Lorenzo Gil Sanchez, Carlos Garnacho)
* gtk/gtk.h:
* gtk/Makefile.am: Glue it in.
2006-03-21 Matthias Clasen <mclasen@redhat.com>
* tests/testtreeview.c (drag_begin_cb): Test setting up
drag icons for tree dnd.
2006-03-22 Michael Natterer <mitch@imendio.com>
* gtk/gtkentry.c: applied modified patch from maemo-gtk which
@ -106,6 +122,7 @@
(gtk_notebook_destroy): Leave the removal of the children
to the container destroy method. (#319032, Yevgen Muntyan)
>>>>>>> 1.7625
2006-03-20 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkcombobox.c (gtk_combo_box_remove): Use the existing

View File

@ -1,3 +1,7 @@
2006-03-22 Matthias Clasen <mclasen@redhat.com>
* gtk/gtk-sections.txt: Additions
2006-03-20 Matthias Clasen <mclasen@redhat.com>
* gtk/gtk-sections.txt: Updates

View File

@ -4367,6 +4367,22 @@ GTK_CELL_RENDERER_COMBO_GET_CLASS
gtk_cell_renderer_combo_get_type
</SECTION>
<SECTION>
<FILE>gtkcellrendererspin</FILE>
<TITLE>GtkCellRendererSpin</TITLE>
GtkCellRendererSpin
gtk_cell_renderer_spin_new
<SUBSECTION Standard>
GTK_TYPE_CELL_RENDERER_SPIN
GTK_CELL_RENDERER_SPIN
GTK_CELL_RENDERER_SPIN_CLASS
GTK_IS_CELL_RENDERER_SPIN
GTK_IS_CELL_RENDERER_SPIN_CLASS
GTK_CELL_RENDERER_SPIN_GET_CLASS
<SUBSECTION Private>
gtk_cell_renderer_spin_get_type
</SECTION>
<SECTION>
<FILE>gtkcellrendererpixbuf</FILE>
<TITLE>GtkCellRendererPixbuf</TITLE>

View File

@ -135,6 +135,7 @@ gtk_public_h_sources = \
gtkcellrenderercombo.h \
gtkcellrendererpixbuf.h \
gtkcellrendererprogress.h \
gtkcellrendererspin.h \
gtkcellrenderertext.h \
gtkcellrenderertoggle.h \
gtkcellview.h \
@ -352,6 +353,7 @@ gtk_c_sources = \
gtkcellrenderercombo.c \
gtkcellrendererpixbuf.c \
gtkcellrendererprogress.c \
gtkcellrendererspin.c \
gtkcellrenderertext.c \
gtkcellrenderertoggle.c \
gtkcellview.c \

View File

@ -53,6 +53,7 @@
#include <gtk/gtkcellrenderercombo.h>
#include <gtk/gtkcellrendererpixbuf.h>
#include <gtk/gtkcellrendererprogress.h>
#include <gtk/gtkcellrendererspin.h>
#include <gtk/gtkcellrenderertext.h>
#include <gtk/gtkcellrenderertoggle.h>
#include <gtk/gtkcellview.h>

View File

@ -520,6 +520,13 @@ gtk_cell_renderer_pixbuf_new
#endif
#endif
#if IN_HEADER(__GTK_CELL_RENDERER_SPIN_H__)
#if IN_FILE(__GTK_CELL_RENDERER_SPIN_C__)
gtk_cell_renderer_spin_get_type G_GNUC_CONST
gtk_cell_renderer_spin_new
#endif
#endif
#if IN_HEADER(__GTK_CELL_RENDERER_PROGRESS_H__)
#if IN_FILE(__GTK_CELL_RENDERER_PROGRESS_C__)
gtk_cell_renderer_progress_get_type G_GNUC_CONST

336
gtk/gtkcellrendererspin.c Normal file
View File

@ -0,0 +1,336 @@
/* 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
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Lorenzo Gil Sanchez <lgs@sicem.biz>
* Carlos Garnacho Parro <carlosg@gnome.org>
*/
#include <config.h>
#include <gdk/gdkkeysyms.h>
#include "gtkintl.h"
#inlcude "gtkprivate.h"
#include "gtkspinbutton.h"
#include "gtkcellrendererspin.h"
#include "gtkalias.h"
#define GTK_CELL_RENDERER_SPIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_SPIN, GtkCellRendererSpinPrivate))
struct _GtkCellRendererSpinPrivate
{
GtkAdjustment *adjustment;
gdouble climb_rate;
guint digits;
};
static void gtk_cell_renderer_spin_class_init (GtkCellRendererSpinClass *klass);
static void gtk_cell_renderer_spin_init (GtkCellRendererSpin *self);
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);
static GtkCellEditable * gtk_cell_renderer_spin_start_editing (GtkCellRenderer *cell,
GdkEvent *event,
GtkWidget *widget,
const gchar *path,
GdkRectangle *background_area,
GdkRectangle *cell_area,
GtkCellRendererState flags);
enum {
PROP_0,
PROP_ADJUSTMENT,
PROP_CLIMB_RATE,
PROP_DIGITS
};
#define GTK_CELL_RENDERER_SPIN_PATH "gtk-cell-renderer-spin-path"
G_DEFINE_TYPE (GtkCellRendererSpin, gtk_cell_renderer_spin, GTK_TYPE_CELL_RENDERER_TEXT);
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.
*
* Since: 2.10
*/
g_object_class_install_property (object_class,
PROP_ADJUSTMENT,
g_param_spec_object ("adjustment",
P_("Adjustment"),
P_("The adjustment that holds the value of the spinbutton."),
GTK_TYPE_ADJUSTMENT,
GTK_PARAM_READWRITE));
/**
* GtkCellRendererSpin:climb-rate:
*
* The acceleration rate when you hold down a button.
*
* Since: 2.10
*/
g_object_class_install_property (object_class,
PROP_CLIMB_RATE,
g_param_spec_double ("climb-rate",
P_("Climb rate"),
P_("The acceleration rate when you hold down a button"),
0.0, G_MAXDOUBLE, 0.0,
GTK_PARAM_READWRITE));
/**
* GtkCellRendererSpin:digits:
*
* The number of decimal places to display.
*
* Since: 2.10
*/
g_object_class_install_property (object_class,
PROP_DIGITS,
g_param_spec_uint ("digits",
P_("Digits"),
P_("The number of decimal places to display"),
0, 20, 0,
GTK_PARAM_READWRITE));
g_type_class_add_private (object_class, sizeof (GtkCellRendererSpinPrivate));
}
static void
gtk_cell_renderer_spin_init (GtkCellRendererSpin *self)
{
GtkCellRendererSpinPrivate *priv;
priv = GTK_CELL_RENDERER_SPIN_GET_PRIVATE (self);
priv->adjustment = NULL;
priv->climb_rate = 0.0;
priv->digits = 0;
}
static void
gtk_cell_renderer_spin_finalize (GObject *object)
{
GtkCellRendererSpinPrivate *priv;
priv = GTK_CELL_RENDERER_SPIN_GET_PRIVATE (object);
if (priv && priv->adjustment)
g_object_unref (priv->adjustment);
if (G_OBJECT_CLASS (gtk_cell_renderer_spin_parent_class)->finalize)
(* G_OBJECT_CLASS (gtk_cell_renderer_spin_parent_class)->finalize) (object);
}
static void
gtk_cell_renderer_spin_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkCellRendererSpin *renderer;
GtkCellRendererSpinPrivate *priv;
renderer = GTK_CELL_RENDERER_SPIN (object);
priv = GTK_CELL_RENDERER_SPIN_GET_PRIVATE (renderer);
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)
{
GtkCellRendererSpin *renderer;
GtkCellRendererSpinPrivate *priv;
GObject *obj;
renderer = GTK_CELL_RENDERER_SPIN (object);
priv = GTK_CELL_RENDERER_SPIN_GET_PRIVATE (renderer);
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)
priv->adjustment = g_object_ref (obj);
break;
case PROP_CLIMB_RATE:
priv->climb_rate = g_value_get_double (value);
break;
case PROP_DIGITS:
priv->digits = g_value_get_uint (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean
gtk_cell_renderer_spin_focus_out_event (GtkWidget *widget,
GdkEvent *event,
gpointer data)
{
const gchar *path;
const gchar *new_text;
gboolean canceled;
canceled = GTK_ENTRY (widget)->editing_canceled;
g_signal_handlers_disconnect_by_func (widget,
gtk_cell_renderer_spin_focus_out_event,
data);
gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
if (!canceled)
{
path = g_object_get_data (G_OBJECT (widget), GTK_CELL_RENDERER_SPIN_PATH);
new_text = gtk_entry_get_text (GTK_ENTRY (widget));
g_signal_emit_by_name (data, "edited", path, new_text);
}
return FALSE;
}
static gboolean
gtk_cell_renderer_spin_key_press_event (GtkWidget *widget,
GdkEventKey *event,
gpointer data)
{
if (event->state == 0)
{
if (event->keyval == GDK_Up)
{
gtk_spin_button_spin (GTK_SPIN_BUTTON (widget), GTK_SPIN_STEP_FORWARD, 1);
return TRUE;
}
else if (event->keyval == GDK_Down)
{
gtk_spin_button_spin (GTK_SPIN_BUTTON (widget), GTK_SPIN_STEP_BACKWARD, 1);
return TRUE;
}
}
return FALSE;
}
static GtkCellEditable *
gtk_cell_renderer_spin_start_editing (GtkCellRenderer *cell,
GdkEvent *event,
GtkWidget *widget,
const gchar *path,
GdkRectangle *background_area,
GdkRectangle *cell_area,
GtkCellRendererState flags)
{
GtkCellRendererSpinPrivate *priv;
GtkCellRendererText *cell_text;
GtkWidget *spin;
cell_text = GTK_CELL_RENDERER_TEXT (cell);
priv = GTK_CELL_RENDERER_SPIN_GET_PRIVATE (cell);
if (!cell_text->editable)
return NULL;
if (!priv->adjustment)
return NULL;
spin = gtk_spin_button_new (g_object_ref (priv->adjustment),
priv->climb_rate, priv->digits);
if (cell_text->text)
gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin),
g_ascii_strtod (cell_text->text, NULL));
g_object_set_data_full (G_OBJECT (spin), GTK_CELL_RENDERER_SPIN_PATH,
g_strdup (path), g_free);
g_signal_connect (G_OBJECT (spin), "focus_out_event",
G_CALLBACK (gtk_cell_renderer_spin_focus_out_event),
cell);
g_signal_connect (G_OBJECT (spin), "key_press_event",
G_CALLBACK (gtk_cell_renderer_spin_key_press_event),
cell);
gtk_widget_show (spin);
return GTK_CELL_EDITABLE (spin);
}
/**
* gtk_cell_renderer_spin_new:
*
* Creates a new #GtkCellRendererSpin.
*
* Since: 2.10
*/
GtkCellRenderer *
gtk_cell_renderer_spin_new (void)
{
return g_object_new (GTK_TYPE_CELL_RENDERER_SPIN, NULL);
}
#define __GTK_CELL_RENDERER_SPIN_C__
#include "gtkaliasdef.c"

54
gtk/gtkcellrendererspin.h Normal file
View File

@ -0,0 +1,54 @@
/* 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
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GTK_CELL_RENDERER_SPIN_H__
#define __GTK_CELL_RENDERER_SPIN_H__
#include <gtk/gtkcellrenderertext.h>
G_BEGIN_DECLS
#define GTK_TYPE_CELL_RENDERER_SPIN (gtk_cell_renderer_spin_get_type ())
#define GTK_CELL_RENDERER_SPIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_CELL_RENDERER_SPIN, GtkCellRendererSpin))
#define GTK_CELL_RENDERER_SPIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_CELL_RENDERER_SPIN, GtkCellRendererSpinClass))
#define GTK_IS_CELL_RENDERER_SPIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_CELL_RENDERER_SPIN))
#define GTK_IS_CELL_RENDERER_SPIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_CELL_RENDERER_SPIN))
#define GTK_CELL_RENDERER_SPIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_CELL_RENDERER_SPIN, GtkCellRendererTextClass))
typedef struct _GtkCellRendererSpin GtkCellRendererSpin;
typedef struct _GtkCellRendererSpinClass GtkCellRendererSpinClass;
typedef struct _GtkCellRendererSpinPrivate GtkCellRendererSpinPrivate;
struct _GtkCellRendererSpin
{
GtkCellRendererText parent;
};
struct _GtkCellRendererSpinClass
{
GtkCellRendererTextClass parent;
};
GType gtk_cell_renderer_spin_get_type (void);
GtkCellRenderer *gtk_cell_renderer_spin_new (void);
G_END_DECLS
#endif /* __GTK_CELL_RENDERER_SPIN_H__ */