gtkcellrenderertoggle.c 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* gtkcellrenderertoggle.c
 * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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.
 */

#include <stdlib.h>
#include <gtk/gtkcellrenderertoggle.h>
Havoc Pennington's avatar
Havoc Pennington committed
22
#include "gtkintl.h"
23
#include "gtkmarshalers.h"
24
#include "gtktreeprivate.h"
25

26 27 28
static void gtk_cell_renderer_toggle_get_property  (GObject                    *object,
						    guint                       param_id,
						    GValue                     *value,
Tim Janik's avatar
Tim Janik committed
29
						    GParamSpec                 *pspec);
30 31 32
static void gtk_cell_renderer_toggle_set_property  (GObject                    *object,
						    guint                       param_id,
						    const GValue               *value,
Tim Janik's avatar
Tim Janik committed
33
						    GParamSpec                 *pspec);
34 35 36 37
static void gtk_cell_renderer_toggle_init       (GtkCellRendererToggle      *celltext);
static void gtk_cell_renderer_toggle_class_init (GtkCellRendererToggleClass *class);
static void gtk_cell_renderer_toggle_get_size   (GtkCellRenderer            *cell,
						 GtkWidget                  *widget,
38 39 40
 						 GdkRectangle               *cell_area,
						 gint                       *x_offset,
						 gint                       *y_offset,
41 42 43 44 45 46 47 48
						 gint                       *width,
						 gint                       *height);
static void gtk_cell_renderer_toggle_render     (GtkCellRenderer            *cell,
						 GdkWindow                  *window,
						 GtkWidget                  *widget,
						 GdkRectangle               *background_area,
						 GdkRectangle               *cell_area,
						 GdkRectangle               *expose_area,
49
						 GtkCellRendererState        flags);
50 51 52
static gboolean gtk_cell_renderer_toggle_activate  (GtkCellRenderer            *cell,
						    GdkEvent                   *event,
						    GtkWidget                  *widget,
53
						    const gchar                *path,
54 55
						    GdkRectangle               *background_area,
						    GdkRectangle               *cell_area,
56
						    GtkCellRendererState        flags);
57 58 59 60 61 62 63 64 65


enum {
  TOGGLED,
  LAST_SIGNAL
};

enum {
  PROP_ZERO,
66
  PROP_ACTIVATABLE,
Havoc Pennington's avatar
Havoc Pennington committed
67
  PROP_ACTIVE,
68 69
  PROP_RADIO,
  PROP_INCONSISTENT
70 71 72 73 74 75 76
};

#define TOGGLE_WIDTH 12

static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };


Manish Singh's avatar
Manish Singh committed
77
GType
78 79
gtk_cell_renderer_toggle_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
80
  static GType cell_toggle_type = 0;
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

  if (!cell_toggle_type)
    {
      static const GTypeInfo cell_toggle_info =
      {
	sizeof (GtkCellRendererToggleClass),
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_cell_renderer_toggle_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkCellRendererToggle),
	0,              /* n_preallocs */
	(GInstanceInitFunc) gtk_cell_renderer_toggle_init,
      };

Manish Singh's avatar
Manish Singh committed
97 98 99
      cell_toggle_type =
	g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererToggle",
				&cell_toggle_info, 0);
100 101 102 103 104 105 106 107
    }

  return cell_toggle_type;
}

static void
gtk_cell_renderer_toggle_init (GtkCellRendererToggle *celltoggle)
{
108
  celltoggle->activatable = TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
109
  celltoggle->active = FALSE;
110
  celltoggle->radio = FALSE;
111
  GTK_CELL_RENDERER (celltoggle)->mode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
112 113 114 115 116 117 118 119 120 121
  GTK_CELL_RENDERER (celltoggle)->xpad = 2;
  GTK_CELL_RENDERER (celltoggle)->ypad = 2;
}

static void
gtk_cell_renderer_toggle_class_init (GtkCellRendererToggleClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);

122 123
  object_class->get_property = gtk_cell_renderer_toggle_get_property;
  object_class->set_property = gtk_cell_renderer_toggle_set_property;
124 125 126

  cell_class->get_size = gtk_cell_renderer_toggle_get_size;
  cell_class->render = gtk_cell_renderer_toggle_render;
127
  cell_class->activate = gtk_cell_renderer_toggle_activate;
128 129
  
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
130 131
				   PROP_ACTIVE,
				   g_param_spec_boolean ("active",
132
							 _("Toggle state"),
Havoc Pennington's avatar
Havoc Pennington committed
133
							 _("The toggle state of the button"),
134 135 136
							 FALSE,
							 G_PARAM_READABLE |
							 G_PARAM_WRITABLE));
137 138 139 140 141

  g_object_class_install_property (object_class,
		                   PROP_INCONSISTENT,
				   g_param_spec_boolean ("inconsistent",
					                 _("Inconsistent state"),
142
							 _("The inconsistent state of the button"),
143 144 145
							 FALSE,
							 G_PARAM_READABLE |
							 G_PARAM_WRITABLE));
146
  
147 148 149 150 151 152 153 154 155
  g_object_class_install_property (object_class,
				   PROP_ACTIVATABLE,
				   g_param_spec_boolean ("activatable",
							 _("Activatable"),
							 _("The toggle button can be activated"),
							 TRUE,
							 G_PARAM_READABLE |
							 G_PARAM_WRITABLE));

156 157 158 159
  g_object_class_install_property (object_class,
				   PROP_RADIO,
				   g_param_spec_boolean ("radio",
							 _("Radio state"),
Havoc Pennington's avatar
Havoc Pennington committed
160
							 _("Draw the toggle button as a radio button"),
161 162 163
							 FALSE,
							 G_PARAM_READABLE |
							 G_PARAM_WRITABLE));
164 165 166


  toggle_cell_signals[TOGGLED] =
Manish Singh's avatar
Manish Singh committed
167 168 169 170 171 172 173 174
    g_signal_new ("toggled",
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkCellRendererToggleClass, toggled),
		  NULL, NULL,
		  _gtk_marshal_VOID__STRING,
		  G_TYPE_NONE, 1,
		  G_TYPE_STRING);
175 176 177
}

static void
178 179 180
gtk_cell_renderer_toggle_get_property (GObject     *object,
				       guint        param_id,
				       GValue      *value,
Tim Janik's avatar
Tim Janik committed
181
				       GParamSpec  *pspec)
182 183
{
  GtkCellRendererToggle *celltoggle = GTK_CELL_RENDERER_TOGGLE (object);
184
  
185 186
  switch (param_id)
    {
Havoc Pennington's avatar
Havoc Pennington committed
187 188
    case PROP_ACTIVE:
      g_value_set_boolean (value, celltoggle->active);
189
      break;
190 191 192 193 194 195 196 197 198 199 200 201
    case PROP_INCONSISTENT:
      {
	/* Move out of here when more properties start to use the info
	 * thing. I put it here to not affect performance, this property
	 * is not going to be used much.
	 */
	GtkCellRendererInfo *cellinfo;
	cellinfo = g_object_get_data (object, GTK_CELL_RENDERER_INFO_KEY);

        g_value_set_boolean (value, cellinfo->inconsistent);
      }
      break;
202 203 204
    case PROP_ACTIVATABLE:
      g_value_set_boolean (value, celltoggle->activatable);
      break;
205 206 207 208
    case PROP_RADIO:
      g_value_set_boolean (value, celltoggle->radio);
      break;
    default:
209
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
210 211 212 213 214 215
      break;
    }
}


static void
216 217 218
gtk_cell_renderer_toggle_set_property (GObject      *object,
				       guint         param_id,
				       const GValue *value,
Tim Janik's avatar
Tim Janik committed
219
				       GParamSpec   *pspec)
220 221
{
  GtkCellRendererToggle *celltoggle = GTK_CELL_RENDERER_TOGGLE (object);
222
  
223 224
  switch (param_id)
    {
Havoc Pennington's avatar
Havoc Pennington committed
225
    case PROP_ACTIVE:
226
      celltoggle->active = g_value_get_boolean (value);
227
      g_object_notify (G_OBJECT(object), "active");
228
      break;
229 230 231 232 233 234 235 236 237 238
    case PROP_INCONSISTENT:
      {
	/* read comment in _get_property */
	GtkCellRendererInfo *cellinfo;
	cellinfo = g_object_get_data (object, GTK_CELL_RENDERER_INFO_KEY);

	cellinfo->inconsistent = g_value_get_boolean (value);
	g_object_notify (G_OBJECT (object), "inconsistent");
      }
      break;
239 240 241 242
    case PROP_ACTIVATABLE:
      celltoggle->activatable = g_value_get_boolean (value);
      g_object_notify (G_OBJECT(object), "activatable");
      break;
243 244
    case PROP_RADIO:
      celltoggle->radio = g_value_get_boolean (value);
245
      g_object_notify (G_OBJECT(object), "radio");
246 247
      break;
    default:
248
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
249 250 251 252
      break;
    }
}

253 254 255 256 257 258 259 260 261 262 263 264 265
/**
 * gtk_cell_renderer_toggle_new:
 * 
 * Creates a new #GtkCellRendererToggle. Adjust rendering
 * parameters using object properties. Object properties can be set
 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
 * can bind a property to a value in a #GtkTreeModel. For example, you
 * can bind the "active" property on the cell renderer to a boolean value
 * in the model, thus causing the check button to reflect the state of
 * the model.
 * 
 * Return value: the new cell renderer
 **/
266 267 268
GtkCellRenderer *
gtk_cell_renderer_toggle_new (void)
{
Manish Singh's avatar
Manish Singh committed
269
  return g_object_new (GTK_TYPE_CELL_RENDERER_TOGGLE, NULL);
270 271 272 273 274
}

static void
gtk_cell_renderer_toggle_get_size (GtkCellRenderer *cell,
				   GtkWidget       *widget,
275 276 277
				   GdkRectangle    *cell_area,
				   gint            *x_offset,
				   gint            *y_offset,
278 279 280
				   gint            *width,
				   gint            *height)
{
281 282 283 284 285 286
  gint calc_width;
  gint calc_height;

  calc_width = (gint) cell->xpad * 2 + TOGGLE_WIDTH;
  calc_height = (gint) cell->ypad * 2 + TOGGLE_WIDTH;

287
  if (width)
288
    *width = calc_width;
289 290

  if (height)
291
    *height = calc_height;
292

293 294 295 296
  if (cell_area)
    {
      if (x_offset)
	{
297 298
	  *x_offset = cell->xalign * (cell_area->width - calc_width);
	  *x_offset = MAX (*x_offset, 0);
299 300 301
	}
      if (y_offset)
	{
302 303
	  *y_offset = cell->yalign * (cell_area->height - calc_height);
	  *y_offset = MAX (*y_offset, 0);
304 305
	}
    }
306 307 308
}

static void
309 310 311 312 313 314 315
gtk_cell_renderer_toggle_render (GtkCellRenderer      *cell,
				 GdkWindow            *window,
				 GtkWidget            *widget,
				 GdkRectangle         *background_area,
				 GdkRectangle         *cell_area,
				 GdkRectangle         *expose_area,
				 GtkCellRendererState  flags)
316 317
{
  GtkCellRendererToggle *celltoggle = (GtkCellRendererToggle *) cell;
318
  GtkCellRendererInfo *cellinfo;
319
  gint width, height;
320
  gint x_offset, y_offset;
321
  GtkShadowType shadow;
322
  GtkStateType state = 0;
323

324 325 326
  gtk_cell_renderer_toggle_get_size (cell, widget, cell_area,
				     &x_offset, &y_offset,
				     &width, &height);
327 328
  width -= cell->xpad*2;
  height -= cell->ypad*2;
329

330 331 332
  if (width <= 0 || height <= 0)
    return;

333 334 335 336 337 338
  cellinfo = g_object_get_data (G_OBJECT (cell), GTK_CELL_RENDERER_INFO_KEY);

  if (cellinfo->inconsistent)
    shadow = GTK_SHADOW_ETCHED_IN;
  else
    shadow = celltoggle->active ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
339

340
  if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
341 342
    {
      if (GTK_WIDGET_HAS_FOCUS (widget))
343
	state = GTK_STATE_SELECTED;
344
      else
345
	state = GTK_STATE_ACTIVE;
346
    }
347
  else
348
    {
Daniel Elstner's avatar
Daniel Elstner committed
349 350 351 352
      if (celltoggle->activatable)
        state = GTK_STATE_NORMAL;
      else
        state = GTK_STATE_INSENSITIVE;
353
    }
354

355
  if (celltoggle->radio)
356
    {
357 358 359 360
      gtk_paint_option (widget->style,
                        window,
                        state, shadow,
                        cell_area, widget, "cellradio",
361 362
                        cell_area->x + x_offset + cell->xpad,
                        cell_area->y + y_offset + cell->ypad,
363
                        width - 1, height - 1);
364 365 366
    }
  else
    {
367 368 369 370
      gtk_paint_check (widget->style,
                       window,
                       state, shadow,
                       cell_area, widget, "cellcheck",
371 372
                       cell_area->x + x_offset + cell->xpad,
                       cell_area->y + y_offset + cell->ypad,
373
                       width - 1, height - 1);
374 375 376 377
    }
}

static gint
378 379 380 381 382 383 384
gtk_cell_renderer_toggle_activate (GtkCellRenderer      *cell,
				   GdkEvent             *event,
				   GtkWidget            *widget,
				   const gchar          *path,
				   GdkRectangle         *background_area,
				   GdkRectangle         *cell_area,
				   GtkCellRendererState  flags)
385
{
386 387 388
  GtkCellRendererToggle *celltoggle;
  
  celltoggle = GTK_CELL_RENDERER_TOGGLE (cell);
389
  if (celltoggle->activatable)
390
    {
Manish Singh's avatar
Manish Singh committed
391
      g_signal_emit (cell, toggle_cell_signals[TOGGLED], 0, path);
392
      return TRUE;
393
    }
394 395

  return FALSE;
396 397
}

398 399 400 401 402 403 404 405 406 407 408 409 410
/**
 * gtk_cell_renderer_toggle_set_radio:
 * @toggle: a #GtkCellRendererToggle
 * @radio: %TRUE to make the toggle look like a radio button
 * 
 * If @radio is %TRUE, the cell renderer renders a radio toggle
 * (i.e. a toggle in a group of mutually-exclusive toggles).
 * If %FALSE, it renders a check toggle (a standalone boolean option).
 * This can be set globally for the cell renderer, or changed just
 * before rendering each cell in the model (for #GtkTreeView, you set
 * up a per-row setting using #GtkTreeViewColumn to associate model
 * columns with cell renderer properties).
 **/
411 412 413 414 415 416 417 418
void
gtk_cell_renderer_toggle_set_radio (GtkCellRendererToggle *toggle,
				    gboolean               radio)
{
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

  toggle->radio = radio;
}
Havoc Pennington's avatar
Havoc Pennington committed
419

420 421 422
/**
 * gtk_cell_renderer_toggle_get_radio:
 * @toggle: a #GtkCellRendererToggle
Matthias Clasen's avatar
Matthias Clasen committed
423 424
 *
 * Returns wether we're rendering radio toggles rather than checkboxes. 
425 426 427
 * 
 * Return value: %TRUE if we're rendering radio toggles rather than checkboxes
 **/
Havoc Pennington's avatar
Havoc Pennington committed
428
gboolean
429
gtk_cell_renderer_toggle_get_radio (GtkCellRendererToggle *toggle)
Havoc Pennington's avatar
Havoc Pennington committed
430 431 432
{
  g_return_val_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle), FALSE);

433
  return toggle->radio;
Havoc Pennington's avatar
Havoc Pennington committed
434
}
435

Matthias Clasen's avatar
Matthias Clasen committed
436 437
/**
 * gtk_cell_renderer_toggle_get_active:
438
 * @toggle: a #GtkCellRendererToggle
Matthias Clasen's avatar
Matthias Clasen committed
439 440 441 442 443
 *
 * Returns whether the cell renderer is active. See
 * gtk_cell_renderer_toggle_set_active().
 *
 * Return value: %TRUE if the cell renderer is active.
444
 **/
445 446 447 448 449 450 451 452
gboolean
gtk_cell_renderer_toggle_get_active (GtkCellRendererToggle *toggle)
{
  g_return_val_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle), FALSE);

  return toggle->active;
}

Matthias Clasen's avatar
Matthias Clasen committed
453 454 455 456 457 458 459
/**
 * gtk_cell_renderer_toggle_set_active:
 * @toggle: a #GtkCellRendererToggle.
 * @setting: the value to set.
 *
 * Activates or deactivates a cell renderer.
 **/
460 461 462 463 464 465
void
gtk_cell_renderer_toggle_set_active (GtkCellRendererToggle *toggle,
				     gboolean               setting)
{
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

466
  g_object_set (G_OBJECT (toggle), "active", setting?TRUE:FALSE, NULL);
467
}