gtkcellrenderertoggle.c 16.3 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 Jardón's avatar
Javier Jardón 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

99
G_DEFINE_TYPE_WITH_PRIVATE (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
  celltoggle->priv = gtk_cell_renderer_toggle_get_instance_private (celltoggle);
108
  priv = celltoggle->priv;
109

110 111 112
  priv->activatable = TRUE;
  priv->active = FALSE;
  priv->radio = FALSE;
113

114 115
  g_object_set (celltoggle, "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
  gtk_cell_renderer_set_padding (GTK_CELL_RENDERER (celltoggle), 2, 2);
116

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

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

127 128
  object_class->get_property = gtk_cell_renderer_toggle_get_property;
  object_class->set_property = gtk_cell_renderer_toggle_set_property;
129 130 131

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

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

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

166 167 168 169 170 171 172 173
  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,
174
						     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
175

176 177 178 179 180 181 182 183
  
  /**
   * 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. 
184 185
   *
   * It is the responsibility of the application to update the model
186 187
   * with the correct value to store at @path.  Often this is simply the
   * opposite of the value currently stored at @path.
188
   **/
189
  toggle_cell_signals[TOGGLED] =
190
    g_signal_new (I_("toggled"),
Manish Singh's avatar
Manish Singh committed
191 192 193 194 195 196 197
		  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);
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 248 249 250 251
      if (priv->active != g_value_get_boolean (value))
        {
          priv->active = g_value_get_boolean (value);
          g_object_notify_by_pspec (object, pspec);
        }
252
      break;
253
    case PROP_INCONSISTENT:
254 255 256 257 258
      if (priv->inconsistent != g_value_get_boolean (value))
        {
          priv->inconsistent = g_value_get_boolean (value);
          g_object_notify_by_pspec (object, pspec);
        }
259
      break;
260
    case PROP_ACTIVATABLE:
261 262 263 264 265
      if (priv->activatable != g_value_get_boolean (value))
        {
          priv->activatable = g_value_get_boolean (value);
          g_object_notify_by_pspec (object, pspec);
        }
266
      break;
267
    case PROP_RADIO:
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
      if (priv->radio != g_value_get_boolean (value))
        {
          priv->radio = g_value_get_boolean (value);
          g_object_notify_by_pspec (object, pspec);
        }
      break;
    case PROP_INDICATOR_SIZE:
      if (priv->indicator_size != g_value_get_int (value))
        {
          priv->indicator_size = g_value_get_int (value);
          g_object_notify_by_pspec (object, pspec);
        }
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
    }
}

287 288
/**
 * gtk_cell_renderer_toggle_new:
289
 *
290 291 292 293
 * 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
294
 * can bind the “active” property on the cell renderer to a boolean value
295 296
 * in the model, thus causing the check button to reflect the state of
 * the model.
297
 *
298
 * Returns: the new cell renderer
299
 **/
300 301 302
GtkCellRenderer *
gtk_cell_renderer_toggle_new (void)
{
Manish Singh's avatar
Manish Singh committed
303
  return g_object_new (GTK_TYPE_CELL_RENDERER_TOGGLE, NULL);
304 305 306
}

static void
307 308 309 310 311 312 313
gtk_cell_renderer_toggle_get_size (GtkCellRenderer    *cell,
				   GtkWidget          *widget,
				   const GdkRectangle *cell_area,
				   gint               *x_offset,
				   gint               *y_offset,
				   gint               *width,
				   gint               *height)
314
{
315
  GtkCellRendererTogglePrivate *priv;
316 317
  gint calc_width;
  gint calc_height;
318
  gint xpad, ypad;
319

320
  priv = GTK_CELL_RENDERER_TOGGLE (cell)->priv;
321

322 323 324
  gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
  calc_width = xpad * 2 + priv->indicator_size;
  calc_height = ypad * 2 + priv->indicator_size;
325

326
  if (width)
327
    *width = calc_width;
328 329

  if (height)
330
    *height = calc_height;
331

332 333
  if (cell_area)
    {
334 335 336 337
      gfloat xalign, yalign;

      gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);

338 339
      if (x_offset)
	{
Jonathan Blandford's avatar
Jonathan Blandford committed
340
	  *x_offset = ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
341
		       (1.0 - xalign) : xalign) * (cell_area->width - calc_width);
342
	  *x_offset = MAX (*x_offset, 0);
343 344 345
	}
      if (y_offset)
	{
346
	  *y_offset = yalign * (cell_area->height - calc_height);
347
	  *y_offset = MAX (*y_offset, 0);
348 349
	}
    }
350 351 352 353 354
  else
    {
      if (x_offset) *x_offset = 0;
      if (y_offset) *y_offset = 0;
    }
355 356 357
}

static void
358
gtk_cell_renderer_toggle_render (GtkCellRenderer      *cell,
359
				 cairo_t              *cr,
360
				 GtkWidget            *widget,
361 362
				 const GdkRectangle   *background_area,
				 const GdkRectangle   *cell_area,
363
				 GtkCellRendererState  flags)
364
{
365
  GtkCellRendererToggle *celltoggle = GTK_CELL_RENDERER_TOGGLE (cell);
366
  GtkCellRendererTogglePrivate *priv = celltoggle->priv;
367
  GtkStyleContext *context;
368
  gint width, height;
369
  gint x_offset, y_offset;
370
  gint xpad, ypad;
371
  GtkStateFlags state;
372

373
  context = gtk_widget_get_style_context (widget);
374
  gtk_cell_renderer_toggle_get_size (cell, widget, cell_area,
375 376
				     &x_offset, &y_offset,
				     &width, &height);
377 378 379
  gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
  width -= xpad * 2;
  height -= ypad * 2;
380

381 382 383
  if (width <= 0 || height <= 0)
    return;

384 385
  state = gtk_cell_renderer_get_state (cell, widget, flags);

386
  if (!priv->activatable)
387 388
    state |= GTK_STATE_FLAG_INSENSITIVE;

389
  state &= ~(GTK_STATE_FLAG_INCONSISTENT | GTK_STATE_FLAG_CHECKED);
390

391 392
  if (priv->inconsistent)
    state |= GTK_STATE_FLAG_INCONSISTENT;
393 394
  
  if (priv->active)
395
    state |= GTK_STATE_FLAG_CHECKED;
396

397 398 399 400 401
  cairo_save (cr);

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

402 403 404
  gtk_style_context_save (context);
  gtk_style_context_set_state (context, state);

405
  if (priv->radio)
406
    {
407 408 409 410 411
      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);
412 413 414
    }
  else
    {
415 416 417 418 419
      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);
420
    }
421

422
  gtk_style_context_restore (context);
423
  cairo_restore (cr);
424 425 426
}

static gint
427 428 429 430
gtk_cell_renderer_toggle_activate (GtkCellRenderer      *cell,
				   GdkEvent             *event,
				   GtkWidget            *widget,
				   const gchar          *path,
431 432
				   const GdkRectangle   *background_area,
				   const GdkRectangle   *cell_area,
433
				   GtkCellRendererState  flags)
434
{
435
  GtkCellRendererTogglePrivate *priv;
436
  GtkCellRendererToggle *celltoggle;
437

438
  celltoggle = GTK_CELL_RENDERER_TOGGLE (cell);
439 440 441
  priv = celltoggle->priv;

  if (priv->activatable)
442
    {
Manish Singh's avatar
Manish Singh committed
443
      g_signal_emit (cell, toggle_cell_signals[TOGGLED], 0, path);
444
      return TRUE;
445
    }
446 447

  return FALSE;
448 449
}

450 451 452 453 454 455 456 457 458 459 460 461 462
/**
 * 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).
 **/
463 464 465 466
void
gtk_cell_renderer_toggle_set_radio (GtkCellRendererToggle *toggle,
				    gboolean               radio)
{
467
  GtkCellRendererTogglePrivate *priv;
468

469 470
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

471 472 473
  priv = toggle->priv;

  priv->radio = radio;
474
}
Havoc Pennington's avatar
Havoc Pennington committed
475

476 477 478
/**
 * gtk_cell_renderer_toggle_get_radio:
 * @toggle: a #GtkCellRendererToggle
Matthias Clasen's avatar
Matthias Clasen committed
479
 *
480
 * Returns whether we’re rendering radio toggles rather than checkboxes. 
481
 * 
482
 * Returns: %TRUE if we’re rendering radio toggles rather than checkboxes
483
 **/
Havoc Pennington's avatar
Havoc Pennington committed
484
gboolean
485
gtk_cell_renderer_toggle_get_radio (GtkCellRendererToggle *toggle)
Havoc Pennington's avatar
Havoc Pennington committed
486 487 488
{
  g_return_val_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle), FALSE);

489
  return toggle->priv->radio;
Havoc Pennington's avatar
Havoc Pennington committed
490
}
491

Matthias Clasen's avatar
Matthias Clasen committed
492 493
/**
 * gtk_cell_renderer_toggle_get_active:
494
 * @toggle: a #GtkCellRendererToggle
Matthias Clasen's avatar
Matthias Clasen committed
495 496 497 498
 *
 * Returns whether the cell renderer is active. See
 * gtk_cell_renderer_toggle_set_active().
 *
499
 * Returns: %TRUE if the cell renderer is active.
500
 **/
501 502 503 504 505
gboolean
gtk_cell_renderer_toggle_get_active (GtkCellRendererToggle *toggle)
{
  g_return_val_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle), FALSE);

506
  return toggle->priv->active;
507 508
}

Matthias Clasen's avatar
Matthias Clasen committed
509 510 511 512 513 514 515
/**
 * gtk_cell_renderer_toggle_set_active:
 * @toggle: a #GtkCellRendererToggle.
 * @setting: the value to set.
 *
 * Activates or deactivates a cell renderer.
 **/
516 517 518 519 520 521
void
gtk_cell_renderer_toggle_set_active (GtkCellRendererToggle *toggle,
				     gboolean               setting)
{
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

522
  g_object_set (toggle, "active", setting ? TRUE : FALSE, NULL);
523
}
524

525 526 527 528 529 530 531
/**
 * gtk_cell_renderer_toggle_get_activatable:
 * @toggle: a #GtkCellRendererToggle
 *
 * Returns whether the cell renderer is activatable. See
 * gtk_cell_renderer_toggle_set_activatable().
 *
532
 * Returns: %TRUE if the cell renderer is activatable.
533 534 535 536 537 538 539 540
 *
 * Since: 2.18
 **/
gboolean
gtk_cell_renderer_toggle_get_activatable (GtkCellRendererToggle *toggle)
{
  g_return_val_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle), FALSE);

541
  return toggle->priv->activatable;
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
}

/**
 * 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)
{
557
  GtkCellRendererTogglePrivate *priv;
558

559 560
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

561 562 563
  priv = toggle->priv;

  if (priv->activatable != setting)
564
    {
565
      priv->activatable = setting ? TRUE : FALSE;
566 567 568
      g_object_notify (G_OBJECT (toggle), "activatable");
    }
}