gtkcellrenderertoggle.c 17.5 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 "gtkstylecontextprivate.h"
25
#include "gtktreeprivate.h"
26
#include "a11y/gtkbooleancellaccessible.h"
27

28

29 30 31 32 33 34 35 36 37 38 39 40
/**
 * 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.
 */


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


enum {
  TOGGLED,
  LAST_SIGNAL
};

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

84
#define TOGGLE_WIDTH 16
85 86 87

static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };

88
struct _GtkCellRendererTogglePrivate
89
{
90 91
  guint active       : 1;
  guint activatable  : 1;
92
  guint inconsistent : 1;
93
  guint radio        : 1;
94 95
};

96

97
G_DEFINE_TYPE_WITH_PRIVATE (GtkCellRendererToggle, gtk_cell_renderer_toggle, GTK_TYPE_CELL_RENDERER)
98

99

100 101 102
static void
gtk_cell_renderer_toggle_init (GtkCellRendererToggle *celltoggle)
{
103
  GtkCellRendererTogglePrivate *priv;
104

105
  celltoggle->priv = gtk_cell_renderer_toggle_get_instance_private (celltoggle);
106
  priv = celltoggle->priv;
107

108 109 110
  priv->activatable = TRUE;
  priv->active = FALSE;
  priv->radio = FALSE;
111

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

  priv->inconsistent = FALSE;
116 117 118 119 120 121 122 123
}

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

124 125
  object_class->get_property = gtk_cell_renderer_toggle_get_property;
  object_class->set_property = gtk_cell_renderer_toggle_set_property;
126 127 128

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

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

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

163 164 165 166 167 168 169 170
  
  /**
   * 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. 
171 172
   *
   * It is the responsibility of the application to update the model
173 174
   * with the correct value to store at @path.  Often this is simply the
   * opposite of the value currently stored at @path.
175
   **/
176
  toggle_cell_signals[TOGGLED] =
177
    g_signal_new (I_("toggled"),
Manish Singh's avatar
Manish Singh committed
178 179 180 181
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkCellRendererToggleClass, toggled),
		  NULL, NULL,
182
		  NULL,
Manish Singh's avatar
Manish Singh committed
183 184
		  G_TYPE_NONE, 1,
		  G_TYPE_STRING);
185

186
  gtk_cell_renderer_class_set_accessible_type (cell_class, GTK_TYPE_BOOLEAN_CELL_ACCESSIBLE);
187 188 189
}

static void
190 191 192
gtk_cell_renderer_toggle_get_property (GObject     *object,
				       guint        param_id,
				       GValue      *value,
Tim Janik's avatar
Tim Janik committed
193
				       GParamSpec  *pspec)
194 195
{
  GtkCellRendererToggle *celltoggle = GTK_CELL_RENDERER_TOGGLE (object);
196
  GtkCellRendererTogglePrivate *priv = celltoggle->priv;
197

198 199
  switch (param_id)
    {
Havoc Pennington's avatar
Havoc Pennington committed
200
    case PROP_ACTIVE:
201
      g_value_set_boolean (value, priv->active);
202
      break;
203
    case PROP_INCONSISTENT:
204
      g_value_set_boolean (value, priv->inconsistent);
205
      break;
206
    case PROP_ACTIVATABLE:
207
      g_value_set_boolean (value, priv->activatable);
208
      break;
209
    case PROP_RADIO:
210
      g_value_set_boolean (value, priv->radio);
211 212
      break;
    default:
213
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
214 215 216 217 218 219
      break;
    }
}


static void
220 221 222
gtk_cell_renderer_toggle_set_property (GObject      *object,
				       guint         param_id,
				       const GValue *value,
Tim Janik's avatar
Tim Janik committed
223
				       GParamSpec   *pspec)
224 225
{
  GtkCellRendererToggle *celltoggle = GTK_CELL_RENDERER_TOGGLE (object);
226
  GtkCellRendererTogglePrivate *priv = celltoggle->priv;
227

228 229
  switch (param_id)
    {
Havoc Pennington's avatar
Havoc Pennington committed
230
    case PROP_ACTIVE:
231 232 233 234 235
      if (priv->active != g_value_get_boolean (value))
        {
          priv->active = g_value_get_boolean (value);
          g_object_notify_by_pspec (object, pspec);
        }
236
      break;
237
    case PROP_INCONSISTENT:
238 239 240 241 242
      if (priv->inconsistent != g_value_get_boolean (value))
        {
          priv->inconsistent = g_value_get_boolean (value);
          g_object_notify_by_pspec (object, pspec);
        }
243
      break;
244
    case PROP_ACTIVATABLE:
245 246 247 248 249
      if (priv->activatable != g_value_get_boolean (value))
        {
          priv->activatable = g_value_get_boolean (value);
          g_object_notify_by_pspec (object, pspec);
        }
250
      break;
251
    case PROP_RADIO:
252 253 254 255 256 257 258 259 260 261 262 263
      if (priv->radio != g_value_get_boolean (value))
        {
          priv->radio = g_value_get_boolean (value);
          g_object_notify_by_pspec (object, pspec);
        }
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
    }
}

264 265
/**
 * gtk_cell_renderer_toggle_new:
266
 *
267 268 269 270
 * 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
271
 * can bind the “active” property on the cell renderer to a boolean value
272 273
 * in the model, thus causing the check button to reflect the state of
 * the model.
274
 *
275
 * Returns: the new cell renderer
276
 **/
277 278 279
GtkCellRenderer *
gtk_cell_renderer_toggle_new (void)
{
Manish Singh's avatar
Manish Singh committed
280
  return g_object_new (GTK_TYPE_CELL_RENDERER_TOGGLE, NULL);
281 282
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
static GtkStyleContext *
gtk_cell_renderer_toggle_save_context (GtkCellRenderer *cell,
				       GtkWidget       *widget)
{
  GtkCellRendererTogglePrivate *priv = GTK_CELL_RENDERER_TOGGLE (cell)->priv;

  GtkStyleContext *context;

  context = gtk_widget_get_style_context (widget);

  if (priv->radio)
    gtk_style_context_save_named (context, "radio");
  else
    gtk_style_context_save_named (context, "check");

  return context;
}
300 301 302 303 304 305
 
static void
calc_indicator_size (GtkStyleContext *context,
                     gint            *width,
                     gint            *height)
{
306
  gtk_style_context_get (context, 
307 308 309 310 311 312 313 314 315 316
                         "min-width", width,
                         "min-height", height,
                         NULL);

  if (*width == 0)
    *width = TOGGLE_WIDTH;
  if (*height == 0)
    *height = TOGGLE_WIDTH;
}

317
static void
318 319 320 321 322 323 324
gtk_cell_renderer_toggle_get_size (GtkCellRenderer    *cell,
				   GtkWidget          *widget,
				   const GdkRectangle *cell_area,
				   gint               *x_offset,
				   gint               *y_offset,
				   gint               *width,
				   gint               *height)
325
{
326 327
  gint calc_width;
  gint calc_height;
328
  gint xpad, ypad;
329 330
  GtkStyleContext *context;
  GtkBorder border, padding;
331

332
  gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
333 334

  context = gtk_cell_renderer_toggle_save_context (cell, widget);
335 336
  gtk_style_context_get_padding (context, &padding);
  gtk_style_context_get_border (context, &border);
337

338
  calc_indicator_size (context, &calc_width, &calc_height);
339 340
  calc_width += xpad * 2 + padding.left + padding.right + border.left + border.right;
  calc_height += ypad * 2 + padding.top + padding.bottom + border.top + border.bottom;
341 342

  gtk_style_context_restore (context);
343

344
  if (width)
345
    *width = calc_width;
346 347

  if (height)
348
    *height = calc_height;
349

350 351
  if (cell_area)
    {
352 353 354 355
      gfloat xalign, yalign;

      gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);

356 357
      if (x_offset)
	{
Jonathan Blandford's avatar
Jonathan Blandford committed
358
	  *x_offset = ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
359
		       (1.0 - xalign) : xalign) * (cell_area->width - calc_width);
360
	  *x_offset = MAX (*x_offset, 0);
361 362 363
	}
      if (y_offset)
	{
364
	  *y_offset = yalign * (cell_area->height - calc_height);
365
	  *y_offset = MAX (*y_offset, 0);
366 367
	}
    }
368 369 370 371 372
  else
    {
      if (x_offset) *x_offset = 0;
      if (y_offset) *y_offset = 0;
    }
373 374 375
}

static void
376
gtk_cell_renderer_toggle_render (GtkCellRenderer      *cell,
377
				 cairo_t              *cr,
378
				 GtkWidget            *widget,
379 380
				 const GdkRectangle   *background_area,
				 const GdkRectangle   *cell_area,
381
				 GtkCellRendererState  flags)
382
{
383
  GtkCellRendererToggle *celltoggle = GTK_CELL_RENDERER_TOGGLE (cell);
384
  GtkCellRendererTogglePrivate *priv = celltoggle->priv;
385
  GtkStyleContext *context;
386
  gint width, height;
387
  gint x_offset, y_offset;
388
  gint xpad, ypad;
389
  GtkStateFlags state;
390
  GtkBorder padding, border;
391

392
  context = gtk_widget_get_style_context (widget);
393
  gtk_cell_renderer_toggle_get_size (cell, widget, cell_area,
394 395
				     &x_offset, &y_offset,
				     &width, &height);
396 397 398
  gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
  width -= xpad * 2;
  height -= ypad * 2;
399

400 401 402
  if (width <= 0 || height <= 0)
    return;

403 404
  state = gtk_cell_renderer_get_state (cell, widget, flags);

405
  if (!priv->activatable)
406 407
    state |= GTK_STATE_FLAG_INSENSITIVE;

408
  state &= ~(GTK_STATE_FLAG_INCONSISTENT | GTK_STATE_FLAG_CHECKED);
409

410 411
  if (priv->inconsistent)
    state |= GTK_STATE_FLAG_INCONSISTENT;
412 413
  
  if (priv->active)
414
    state |= GTK_STATE_FLAG_CHECKED;
415

416 417 418 419 420
  cairo_save (cr);

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

421 422
  context = gtk_cell_renderer_toggle_save_context (cell, widget);
  gtk_style_context_set_state (context, state);
423

424
  gtk_render_background (context, cr,
425 426 427
                         cell_area->x + x_offset + xpad,
                         cell_area->y + y_offset + ypad,
                         width, height);
428 429 430 431 432
  gtk_render_frame (context, cr,
                    cell_area->x + x_offset + xpad,
                    cell_area->y + y_offset + ypad,
                    width, height);

433 434
  gtk_style_context_get_padding (context, &padding);
  gtk_style_context_get_border (context, &border);
435 436 437 438 439 440 441 442

  if (priv->radio)
    {
      gtk_render_option (context, cr,
                         cell_area->x + x_offset + xpad + padding.left + border.left,
                         cell_area->y + y_offset + ypad + padding.top + border.top,
                         width - padding.left - padding.right - border.left - border.right,
                         height - padding.top - padding.bottom - border.top - border.bottom);
443 444 445
    }
  else
    {
446
      gtk_render_check (context, cr,
447 448 449 450
                        cell_area->x + x_offset + xpad + padding.left + border.left,
                        cell_area->y + y_offset + ypad + padding.top + border.top,
                        width - padding.left - padding.right - border.left - border.right,
                        height - padding.top - padding.bottom - border.top - border.bottom);
451
    }
452

453
  gtk_style_context_restore (context);
454
  cairo_restore (cr);
455 456 457
}

static gint
458 459 460 461
gtk_cell_renderer_toggle_activate (GtkCellRenderer      *cell,
				   GdkEvent             *event,
				   GtkWidget            *widget,
				   const gchar          *path,
462 463
				   const GdkRectangle   *background_area,
				   const GdkRectangle   *cell_area,
464
				   GtkCellRendererState  flags)
465
{
466
  GtkCellRendererTogglePrivate *priv;
467
  GtkCellRendererToggle *celltoggle;
468

469
  celltoggle = GTK_CELL_RENDERER_TOGGLE (cell);
470 471 472
  priv = celltoggle->priv;

  if (priv->activatable)
473
    {
Manish Singh's avatar
Manish Singh committed
474
      g_signal_emit (cell, toggle_cell_signals[TOGGLED], 0, path);
475
      return TRUE;
476
    }
477 478

  return FALSE;
479 480
}

481 482 483 484 485 486 487 488 489 490 491 492 493
/**
 * 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).
 **/
494 495 496 497
void
gtk_cell_renderer_toggle_set_radio (GtkCellRendererToggle *toggle,
				    gboolean               radio)
{
498
  GtkCellRendererTogglePrivate *priv;
499

500 501
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

502 503 504
  priv = toggle->priv;

  priv->radio = radio;
505
}
Havoc Pennington's avatar
Havoc Pennington committed
506

507 508 509
/**
 * gtk_cell_renderer_toggle_get_radio:
 * @toggle: a #GtkCellRendererToggle
Matthias Clasen's avatar
Matthias Clasen committed
510
 *
511
 * Returns whether we’re rendering radio toggles rather than checkboxes. 
512
 * 
513
 * Returns: %TRUE if we’re rendering radio toggles rather than checkboxes
514
 **/
Havoc Pennington's avatar
Havoc Pennington committed
515
gboolean
516
gtk_cell_renderer_toggle_get_radio (GtkCellRendererToggle *toggle)
Havoc Pennington's avatar
Havoc Pennington committed
517 518 519
{
  g_return_val_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle), FALSE);

520
  return toggle->priv->radio;
Havoc Pennington's avatar
Havoc Pennington committed
521
}
522

Matthias Clasen's avatar
Matthias Clasen committed
523 524
/**
 * gtk_cell_renderer_toggle_get_active:
525
 * @toggle: a #GtkCellRendererToggle
Matthias Clasen's avatar
Matthias Clasen committed
526 527 528 529
 *
 * Returns whether the cell renderer is active. See
 * gtk_cell_renderer_toggle_set_active().
 *
530
 * Returns: %TRUE if the cell renderer is active.
531
 **/
532 533 534 535 536
gboolean
gtk_cell_renderer_toggle_get_active (GtkCellRendererToggle *toggle)
{
  g_return_val_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle), FALSE);

537
  return toggle->priv->active;
538 539
}

Matthias Clasen's avatar
Matthias Clasen committed
540 541 542 543 544 545 546
/**
 * gtk_cell_renderer_toggle_set_active:
 * @toggle: a #GtkCellRendererToggle.
 * @setting: the value to set.
 *
 * Activates or deactivates a cell renderer.
 **/
547 548 549 550 551 552
void
gtk_cell_renderer_toggle_set_active (GtkCellRendererToggle *toggle,
				     gboolean               setting)
{
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

553
  g_object_set (toggle, "active", setting ? TRUE : FALSE, NULL);
554
}
555

556 557 558 559 560 561 562
/**
 * gtk_cell_renderer_toggle_get_activatable:
 * @toggle: a #GtkCellRendererToggle
 *
 * Returns whether the cell renderer is activatable. See
 * gtk_cell_renderer_toggle_set_activatable().
 *
563
 * Returns: %TRUE if the cell renderer is activatable.
564 565 566 567 568 569 570 571
 *
 * Since: 2.18
 **/
gboolean
gtk_cell_renderer_toggle_get_activatable (GtkCellRendererToggle *toggle)
{
  g_return_val_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle), FALSE);

572
  return toggle->priv->activatable;
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
}

/**
 * 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)
{
588
  GtkCellRendererTogglePrivate *priv;
589

590 591
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

592 593 594
  priv = toggle->priv;

  if (priv->activatable != setting)
595
    {
596
      priv->activatable = setting ? TRUE : FALSE;
597 598 599
      g_object_notify (G_OBJECT (toggle), "activatable");
    }
}