gtkcellrenderertoggle.c 15.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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
Javier Jardon's avatar
Javier Jardon committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17
 */

18
#include "config.h"
19
#include <stdlib.h>
Kristian Rietveld's avatar
Kristian Rietveld committed
20
#include "gtkcellrenderertoggle.h"
Havoc Pennington's avatar
Havoc Pennington committed
21
#include "gtkintl.h"
22
#include "gtkmarshalers.h"
23
#include "gtkprivate.h"
24
#include "gtktreeprivate.h"
25
#include "a11y/gtkbooleancellaccessible.h"
26

27

28 29 30 31 32 33 34 35 36 37 38 39
/**
 * SECTION:gtkcellrenderertoggle
 * @Short_description: Renders a toggle button in a cell
 * @Title: GtkCellRendererToggle
 *
 * #GtkCellRendererToggle renders a toggle button in a cell. The
 * button is drawn as a radio or a checkbutton, depending on the
 * #GtkCellRendererToggle:radio property.
 * When activated, it emits the #GtkCellRendererToggle::toggled signal.
 */


40 41 42
static void gtk_cell_renderer_toggle_get_property  (GObject                    *object,
						    guint                       param_id,
						    GValue                     *value,
Tim Janik's avatar
Tim Janik committed
43
						    GParamSpec                 *pspec);
44 45 46
static void gtk_cell_renderer_toggle_set_property  (GObject                    *object,
						    guint                       param_id,
						    const GValue               *value,
Tim Janik's avatar
Tim Janik committed
47
						    GParamSpec                 *pspec);
48 49
static void gtk_cell_renderer_toggle_get_size   (GtkCellRenderer            *cell,
						 GtkWidget                  *widget,
50
						 const GdkRectangle         *cell_area,
51 52
						 gint                       *x_offset,
						 gint                       *y_offset,
53 54 55
						 gint                       *width,
						 gint                       *height);
static void gtk_cell_renderer_toggle_render     (GtkCellRenderer            *cell,
56
						 cairo_t                    *cr,
57
						 GtkWidget                  *widget,
58 59
						 const GdkRectangle         *background_area,
						 const GdkRectangle         *cell_area,
60
						 GtkCellRendererState        flags);
61 62 63
static gboolean gtk_cell_renderer_toggle_activate  (GtkCellRenderer            *cell,
						    GdkEvent                   *event,
						    GtkWidget                  *widget,
64
						    const gchar                *path,
65 66
						    const GdkRectangle         *background_area,
						    const GdkRectangle         *cell_area,
67
						    GtkCellRendererState        flags);
68 69 70 71 72 73 74 75


enum {
  TOGGLED,
  LAST_SIGNAL
};

enum {
76
  PROP_0,
77
  PROP_ACTIVATABLE,
Havoc Pennington's avatar
Havoc Pennington committed
78
  PROP_ACTIVE,
79
  PROP_RADIO,
80 81
  PROP_INCONSISTENT,
  PROP_INDICATOR_SIZE
82 83
};

84
#define TOGGLE_WIDTH 16
85 86 87

static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };

88
struct _GtkCellRendererTogglePrivate
89
{
90 91
  gint indicator_size;

92 93
  guint active       : 1;
  guint activatable  : 1;
94
  guint inconsistent : 1;
95
  guint radio        : 1;
96 97
};

98

Matthias Clasen's avatar
Matthias Clasen committed
99
G_DEFINE_TYPE (GtkCellRendererToggle, gtk_cell_renderer_toggle, GTK_TYPE_CELL_RENDERER)
100

101

102 103 104
static void
gtk_cell_renderer_toggle_init (GtkCellRendererToggle *celltoggle)
{
105
  GtkCellRendererTogglePrivate *priv;
106

107 108
  celltoggle->priv = G_TYPE_INSTANCE_GET_PRIVATE (celltoggle,
                                                  GTK_TYPE_CELL_RENDERER_TOGGLE,
109
                                                  GtkCellRendererTogglePrivate);
110
  priv = celltoggle->priv;
111

112 113 114
  priv->activatable = TRUE;
  priv->active = FALSE;
  priv->radio = FALSE;
115

116 117
  g_object_set (celltoggle, "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
  gtk_cell_renderer_set_padding (GTK_CELL_RENDERER (celltoggle), 2, 2);
118

119
  priv->indicator_size = TOGGLE_WIDTH;
120
  priv->inconsistent = FALSE;
121 122 123 124 125 126 127 128
}

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);

129 130
  object_class->get_property = gtk_cell_renderer_toggle_get_property;
  object_class->set_property = gtk_cell_renderer_toggle_set_property;
131 132 133

  cell_class->get_size = gtk_cell_renderer_toggle_get_size;
  cell_class->render = gtk_cell_renderer_toggle_render;
134
  cell_class->activate = gtk_cell_renderer_toggle_activate;
135 136
  
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
137 138
				   PROP_ACTIVE,
				   g_param_spec_boolean ("active",
139 140
							 P_("Toggle state"),
							 P_("The toggle state of the button"),
141
							 FALSE,
142
							 GTK_PARAM_READWRITE));
143 144 145 146

  g_object_class_install_property (object_class,
		                   PROP_INCONSISTENT,
				   g_param_spec_boolean ("inconsistent",
147 148
					                 P_("Inconsistent state"),
							 P_("The inconsistent state of the button"),
149
							 FALSE,
150
							 GTK_PARAM_READWRITE));
151
  
152 153 154
  g_object_class_install_property (object_class,
				   PROP_ACTIVATABLE,
				   g_param_spec_boolean ("activatable",
155 156
							 P_("Activatable"),
							 P_("The toggle button can be activated"),
157
							 TRUE,
158
							 GTK_PARAM_READWRITE));
159

160 161 162
  g_object_class_install_property (object_class,
				   PROP_RADIO,
				   g_param_spec_boolean ("radio",
163 164
							 P_("Radio state"),
							 P_("Draw the toggle button as a radio button"),
165
							 FALSE,
166
							 GTK_PARAM_READWRITE));
167

168 169 170 171 172 173 174 175 176 177
  g_object_class_install_property (object_class,
				   PROP_INDICATOR_SIZE,
				   g_param_spec_int ("indicator-size",
						     P_("Indicator size"),
						     P_("Size of check or radio indicator"),
						     0,
						     G_MAXINT,
						     TOGGLE_WIDTH,
						     GTK_PARAM_READWRITE));

178 179 180 181 182 183 184 185 186
  
  /**
   * GtkCellRendererToggle::toggled:
   * @cell_renderer: the object which received the signal
   * @path: string representation of #GtkTreePath describing the 
   *        event location
   *
   * The ::toggled signal is emitted when the cell is toggled. 
   **/
187
  toggle_cell_signals[TOGGLED] =
188
    g_signal_new (I_("toggled"),
Manish Singh's avatar
Manish Singh committed
189 190 191 192 193 194 195
		  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);
196

197
  g_type_class_add_private (object_class, sizeof (GtkCellRendererTogglePrivate));
198 199

  _gtk_cell_renderer_class_set_accessible_type (cell_class, GTK_TYPE_BOOLEAN_CELL_ACCESSIBLE);
200 201 202
}

static void
203 204 205
gtk_cell_renderer_toggle_get_property (GObject     *object,
				       guint        param_id,
				       GValue      *value,
Tim Janik's avatar
Tim Janik committed
206
				       GParamSpec  *pspec)
207 208
{
  GtkCellRendererToggle *celltoggle = GTK_CELL_RENDERER_TOGGLE (object);
209
  GtkCellRendererTogglePrivate *priv = celltoggle->priv;
210

211 212
  switch (param_id)
    {
Havoc Pennington's avatar
Havoc Pennington committed
213
    case PROP_ACTIVE:
214
      g_value_set_boolean (value, priv->active);
215
      break;
216
    case PROP_INCONSISTENT:
217
      g_value_set_boolean (value, priv->inconsistent);
218
      break;
219
    case PROP_ACTIVATABLE:
220
      g_value_set_boolean (value, priv->activatable);
221
      break;
222
    case PROP_RADIO:
223
      g_value_set_boolean (value, priv->radio);
224
      break;
225 226 227
    case PROP_INDICATOR_SIZE:
      g_value_set_int (value, priv->indicator_size);
      break;
228
    default:
229
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
230 231 232 233 234 235
      break;
    }
}


static void
236 237 238
gtk_cell_renderer_toggle_set_property (GObject      *object,
				       guint         param_id,
				       const GValue *value,
Tim Janik's avatar
Tim Janik committed
239
				       GParamSpec   *pspec)
240 241
{
  GtkCellRendererToggle *celltoggle = GTK_CELL_RENDERER_TOGGLE (object);
242
  GtkCellRendererTogglePrivate *priv = celltoggle->priv;
243

244 245
  switch (param_id)
    {
Havoc Pennington's avatar
Havoc Pennington committed
246
    case PROP_ACTIVE:
247
      priv->active = g_value_get_boolean (value);
248
      break;
249
    case PROP_INCONSISTENT:
250
      priv->inconsistent = g_value_get_boolean (value);
251
      break;
252
    case PROP_ACTIVATABLE:
253
      priv->activatable = g_value_get_boolean (value);
254
      break;
255
    case PROP_RADIO:
256
      priv->radio = g_value_get_boolean (value);
257
      break;
258 259 260
    case PROP_INDICATOR_SIZE:
      priv->indicator_size = g_value_get_int (value);
      break;
261
    default:
262
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
263 264 265 266
      break;
    }
}

267 268
/**
 * gtk_cell_renderer_toggle_new:
269
 *
270 271 272 273 274 275 276
 * 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.
277
 *
278 279
 * Return value: the new cell renderer
 **/
280 281 282
GtkCellRenderer *
gtk_cell_renderer_toggle_new (void)
{
Manish Singh's avatar
Manish Singh committed
283
  return g_object_new (GTK_TYPE_CELL_RENDERER_TOGGLE, NULL);
284 285 286
}

static void
287 288 289 290 291 292 293
gtk_cell_renderer_toggle_get_size (GtkCellRenderer    *cell,
				   GtkWidget          *widget,
				   const GdkRectangle *cell_area,
				   gint               *x_offset,
				   gint               *y_offset,
				   gint               *width,
				   gint               *height)
294
{
295
  GtkCellRendererTogglePrivate *priv;
296 297
  gint calc_width;
  gint calc_height;
298
  gint xpad, ypad;
299

300
  priv = GTK_CELL_RENDERER_TOGGLE (cell)->priv;
301

302 303 304
  gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
  calc_width = xpad * 2 + priv->indicator_size;
  calc_height = ypad * 2 + priv->indicator_size;
305

306
  if (width)
307
    *width = calc_width;
308 309

  if (height)
310
    *height = calc_height;
311

312 313
  if (cell_area)
    {
314 315 316 317
      gfloat xalign, yalign;

      gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);

318 319
      if (x_offset)
	{
Jonathan Blandford's avatar
Jonathan Blandford committed
320
	  *x_offset = ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
321
		       (1.0 - xalign) : xalign) * (cell_area->width - calc_width);
322
	  *x_offset = MAX (*x_offset, 0);
323 324 325
	}
      if (y_offset)
	{
326
	  *y_offset = yalign * (cell_area->height - calc_height);
327
	  *y_offset = MAX (*y_offset, 0);
328 329
	}
    }
330 331 332 333 334
  else
    {
      if (x_offset) *x_offset = 0;
      if (y_offset) *y_offset = 0;
    }
335 336 337
}

static void
338
gtk_cell_renderer_toggle_render (GtkCellRenderer      *cell,
339
				 cairo_t              *cr,
340
				 GtkWidget            *widget,
341 342
				 const GdkRectangle   *background_area,
				 const GdkRectangle   *cell_area,
343
				 GtkCellRendererState  flags)
344
{
345
  GtkCellRendererToggle *celltoggle = GTK_CELL_RENDERER_TOGGLE (cell);
346
  GtkCellRendererTogglePrivate *priv = celltoggle->priv;
347
  GtkStyleContext *context;
348
  gint width, height;
349
  gint x_offset, y_offset;
350
  gint xpad, ypad;
351
  GtkStateFlags state;
352

353
  context = gtk_widget_get_style_context (widget);
354
  gtk_cell_renderer_toggle_get_size (cell, widget, cell_area,
355 356
				     &x_offset, &y_offset,
				     &width, &height);
357 358 359
  gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
  width -= xpad * 2;
  height -= ypad * 2;
360

361 362 363
  if (width <= 0 || height <= 0)
    return;

364 365
  state = gtk_cell_renderer_get_state (cell, widget, flags);

366
  if (!priv->activatable)
367 368 369
    state |= GTK_STATE_FLAG_INSENSITIVE;

  state &= ~(GTK_STATE_FLAG_INCONSISTENT | GTK_STATE_FLAG_ACTIVE);
370

371 372 373 374
  if (priv->inconsistent)
    state |= GTK_STATE_FLAG_INCONSISTENT;
  else if (priv->active)
    state |= GTK_STATE_FLAG_ACTIVE;
375

376 377 378 379 380
  cairo_save (cr);

  gdk_cairo_rectangle (cr, cell_area);
  cairo_clip (cr);

381 382 383
  gtk_style_context_save (context);
  gtk_style_context_set_state (context, state);

384
  if (priv->radio)
385
    {
386 387 388 389 390
      gtk_style_context_add_class (context, GTK_STYLE_CLASS_RADIO);
      gtk_render_option (context, cr,
                         cell_area->x + x_offset + xpad,
                         cell_area->y + y_offset + ypad,
                         width, height);
391 392 393
    }
  else
    {
394 395 396 397 398
      gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
      gtk_render_check (context, cr,
                        cell_area->x + x_offset + xpad,
                        cell_area->y + y_offset + ypad,
                        width, height);
399
    }
400

401
  gtk_style_context_restore (context);
402
  cairo_restore (cr);
403 404 405
}

static gint
406 407 408 409
gtk_cell_renderer_toggle_activate (GtkCellRenderer      *cell,
				   GdkEvent             *event,
				   GtkWidget            *widget,
				   const gchar          *path,
410 411
				   const GdkRectangle   *background_area,
				   const GdkRectangle   *cell_area,
412
				   GtkCellRendererState  flags)
413
{
414
  GtkCellRendererTogglePrivate *priv;
415
  GtkCellRendererToggle *celltoggle;
416

417
  celltoggle = GTK_CELL_RENDERER_TOGGLE (cell);
418 419 420
  priv = celltoggle->priv;

  if (priv->activatable)
421
    {
Manish Singh's avatar
Manish Singh committed
422
      g_signal_emit (cell, toggle_cell_signals[TOGGLED], 0, path);
423
      return TRUE;
424
    }
425 426

  return FALSE;
427 428
}

429 430 431 432 433 434 435 436 437 438 439 440 441
/**
 * 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).
 **/
442 443 444 445
void
gtk_cell_renderer_toggle_set_radio (GtkCellRendererToggle *toggle,
				    gboolean               radio)
{
446
  GtkCellRendererTogglePrivate *priv;
447

448 449
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

450 451 452
  priv = toggle->priv;

  priv->radio = radio;
453
}
Havoc Pennington's avatar
Havoc Pennington committed
454

455 456 457
/**
 * gtk_cell_renderer_toggle_get_radio:
 * @toggle: a #GtkCellRendererToggle
Matthias Clasen's avatar
Matthias Clasen committed
458
 *
Matthias Clasen's avatar
Matthias Clasen committed
459
 * Returns whether we're rendering radio toggles rather than checkboxes. 
460 461 462
 * 
 * Return value: %TRUE if we're rendering radio toggles rather than checkboxes
 **/
Havoc Pennington's avatar
Havoc Pennington committed
463
gboolean
464
gtk_cell_renderer_toggle_get_radio (GtkCellRendererToggle *toggle)
Havoc Pennington's avatar
Havoc Pennington committed
465 466 467
{
  g_return_val_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle), FALSE);

468
  return toggle->priv->radio;
Havoc Pennington's avatar
Havoc Pennington committed
469
}
470

Matthias Clasen's avatar
Matthias Clasen committed
471 472
/**
 * gtk_cell_renderer_toggle_get_active:
473
 * @toggle: a #GtkCellRendererToggle
Matthias Clasen's avatar
Matthias Clasen committed
474 475 476 477 478
 *
 * Returns whether the cell renderer is active. See
 * gtk_cell_renderer_toggle_set_active().
 *
 * Return value: %TRUE if the cell renderer is active.
479
 **/
480 481 482 483 484
gboolean
gtk_cell_renderer_toggle_get_active (GtkCellRendererToggle *toggle)
{
  g_return_val_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle), FALSE);

485
  return toggle->priv->active;
486 487
}

Matthias Clasen's avatar
Matthias Clasen committed
488 489 490 491 492 493 494
/**
 * gtk_cell_renderer_toggle_set_active:
 * @toggle: a #GtkCellRendererToggle.
 * @setting: the value to set.
 *
 * Activates or deactivates a cell renderer.
 **/
495 496 497 498 499 500
void
gtk_cell_renderer_toggle_set_active (GtkCellRendererToggle *toggle,
				     gboolean               setting)
{
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

501
  g_object_set (toggle, "active", setting ? TRUE : FALSE, NULL);
502
}
503

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
/**
 * gtk_cell_renderer_toggle_get_activatable:
 * @toggle: a #GtkCellRendererToggle
 *
 * Returns whether the cell renderer is activatable. See
 * gtk_cell_renderer_toggle_set_activatable().
 *
 * Return value: %TRUE if the cell renderer is activatable.
 *
 * Since: 2.18
 **/
gboolean
gtk_cell_renderer_toggle_get_activatable (GtkCellRendererToggle *toggle)
{
  g_return_val_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle), FALSE);

520
  return toggle->priv->activatable;
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
}

/**
 * gtk_cell_renderer_toggle_set_activatable:
 * @toggle: a #GtkCellRendererToggle.
 * @setting: the value to set.
 *
 * Makes the cell renderer activatable.
 *
 * Since: 2.18
 **/
void
gtk_cell_renderer_toggle_set_activatable (GtkCellRendererToggle *toggle,
                                          gboolean               setting)
{
536
  GtkCellRendererTogglePrivate *priv;
537

538 539
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

540 541 542
  priv = toggle->priv;

  if (priv->activatable != setting)
543
    {
544
      priv->activatable = setting ? TRUE : FALSE;
545 546 547
      g_object_notify (G_OBJECT (toggle), "activatable");
    }
}