gtkcellrenderertoggle.c 15.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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.
 */

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

28

29 30 31
static void gtk_cell_renderer_toggle_get_property  (GObject                    *object,
						    guint                       param_id,
						    GValue                     *value,
Tim Janik's avatar
Tim Janik committed
32
						    GParamSpec                 *pspec);
33 34 35
static void gtk_cell_renderer_toggle_set_property  (GObject                    *object,
						    guint                       param_id,
						    const GValue               *value,
Tim Janik's avatar
Tim Janik committed
36
						    GParamSpec                 *pspec);
37 38
static void gtk_cell_renderer_toggle_get_size   (GtkCellRenderer            *cell,
						 GtkWidget                  *widget,
39
						 const GdkRectangle         *cell_area,
40 41
						 gint                       *x_offset,
						 gint                       *y_offset,
42 43 44
						 gint                       *width,
						 gint                       *height);
static void gtk_cell_renderer_toggle_render     (GtkCellRenderer            *cell,
45
						 cairo_t                    *cr,
46
						 GtkWidget                  *widget,
47 48
						 const GdkRectangle         *background_area,
						 const GdkRectangle         *cell_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
						    const GdkRectangle         *background_area,
						    const GdkRectangle         *cell_area,
56
						    GtkCellRendererState        flags);
57 58 59 60 61 62 63 64


enum {
  TOGGLED,
  LAST_SIGNAL
};

enum {
65
  PROP_0,
66
  PROP_ACTIVATABLE,
Havoc Pennington's avatar
Havoc Pennington committed
67
  PROP_ACTIVE,
68
  PROP_RADIO,
69 70
  PROP_INCONSISTENT,
  PROP_INDICATOR_SIZE
71 72
};

73
#define TOGGLE_WIDTH 13
74 75 76

static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };

77
struct _GtkCellRendererTogglePrivate
78
{
79 80
  gint indicator_size;

81 82
  guint active       : 1;
  guint activatable  : 1;
83
  guint inconsistent : 1;
84
  guint radio        : 1;
85 86
};

87

Matthias Clasen's avatar
Matthias Clasen committed
88
G_DEFINE_TYPE (GtkCellRendererToggle, gtk_cell_renderer_toggle, GTK_TYPE_CELL_RENDERER)
89

90

91 92 93
static void
gtk_cell_renderer_toggle_init (GtkCellRendererToggle *celltoggle)
{
94
  GtkCellRendererTogglePrivate *priv;
95

96 97
  celltoggle->priv = G_TYPE_INSTANCE_GET_PRIVATE (celltoggle,
                                                  GTK_TYPE_CELL_RENDERER_TOGGLE,
98
                                                  GtkCellRendererTogglePrivate);
99
  priv = celltoggle->priv;
100

101 102 103
  priv->activatable = TRUE;
  priv->active = FALSE;
  priv->radio = FALSE;
104

105 106
  g_object_set (celltoggle, "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
  gtk_cell_renderer_set_padding (GTK_CELL_RENDERER (celltoggle), 2, 2);
107

108
  priv->indicator_size = TOGGLE_WIDTH;
109
  priv->inconsistent = FALSE;
110 111 112 113 114 115 116 117
}

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

118 119
  object_class->get_property = gtk_cell_renderer_toggle_get_property;
  object_class->set_property = gtk_cell_renderer_toggle_set_property;
120 121 122

  cell_class->get_size = gtk_cell_renderer_toggle_get_size;
  cell_class->render = gtk_cell_renderer_toggle_render;
123
  cell_class->activate = gtk_cell_renderer_toggle_activate;
124 125
  
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
126 127
				   PROP_ACTIVE,
				   g_param_spec_boolean ("active",
128 129
							 P_("Toggle state"),
							 P_("The toggle state of the button"),
130
							 FALSE,
131
							 GTK_PARAM_READWRITE));
132 133 134 135

  g_object_class_install_property (object_class,
		                   PROP_INCONSISTENT,
				   g_param_spec_boolean ("inconsistent",
136 137
					                 P_("Inconsistent state"),
							 P_("The inconsistent state of the button"),
138
							 FALSE,
139
							 GTK_PARAM_READWRITE));
140
  
141 142 143
  g_object_class_install_property (object_class,
				   PROP_ACTIVATABLE,
				   g_param_spec_boolean ("activatable",
144 145
							 P_("Activatable"),
							 P_("The toggle button can be activated"),
146
							 TRUE,
147
							 GTK_PARAM_READWRITE));
148

149 150 151
  g_object_class_install_property (object_class,
				   PROP_RADIO,
				   g_param_spec_boolean ("radio",
152 153
							 P_("Radio state"),
							 P_("Draw the toggle button as a radio button"),
154
							 FALSE,
155
							 GTK_PARAM_READWRITE));
156

157 158 159 160 161 162 163 164 165 166
  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));

167 168 169 170 171 172 173 174 175
  
  /**
   * 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. 
   **/
176
  toggle_cell_signals[TOGGLED] =
177
    g_signal_new (I_("toggled"),
Manish Singh's avatar
Manish Singh committed
178 179 180 181 182 183 184
		  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);
185

186
  g_type_class_add_private (object_class, sizeof (GtkCellRendererTogglePrivate));
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
      break;
212 213 214
    case PROP_INDICATOR_SIZE:
      g_value_set_int (value, priv->indicator_size);
      break;
215
    default:
216
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
217 218 219 220 221 222
      break;
    }
}


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

231 232
  switch (param_id)
    {
Havoc Pennington's avatar
Havoc Pennington committed
233
    case PROP_ACTIVE:
234
      priv->active = g_value_get_boolean (value);
235
      break;
236
    case PROP_INCONSISTENT:
237
      priv->inconsistent = g_value_get_boolean (value);
238
      break;
239
    case PROP_ACTIVATABLE:
240
      priv->activatable = g_value_get_boolean (value);
241
      break;
242
    case PROP_RADIO:
243
      priv->radio = g_value_get_boolean (value);
244
      break;
245 246 247
    case PROP_INDICATOR_SIZE:
      priv->indicator_size = g_value_get_int (value);
      break;
248
    default:
249
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
250 251 252 253
      break;
    }
}

254 255
/**
 * gtk_cell_renderer_toggle_new:
256
 *
257 258 259 260 261 262 263
 * 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.
264
 *
265 266
 * Return value: the new cell renderer
 **/
267 268 269
GtkCellRenderer *
gtk_cell_renderer_toggle_new (void)
{
Manish Singh's avatar
Manish Singh committed
270
  return g_object_new (GTK_TYPE_CELL_RENDERER_TOGGLE, NULL);
271 272 273
}

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

287
  priv = GTK_CELL_RENDERER_TOGGLE (cell)->priv;
288

289 290 291
  gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
  calc_width = xpad * 2 + priv->indicator_size;
  calc_height = ypad * 2 + priv->indicator_size;
292

293
  if (width)
294
    *width = calc_width;
295 296

  if (height)
297
    *height = calc_height;
298

299 300
  if (cell_area)
    {
301 302 303 304
      gfloat xalign, yalign;

      gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);

305 306
      if (x_offset)
	{
Jonathan Blandford's avatar
Jonathan Blandford committed
307
	  *x_offset = ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
308
		       (1.0 - xalign) : xalign) * (cell_area->width - calc_width);
309
	  *x_offset = MAX (*x_offset, 0);
310 311 312
	}
      if (y_offset)
	{
313
	  *y_offset = yalign * (cell_area->height - calc_height);
314
	  *y_offset = MAX (*y_offset, 0);
315 316
	}
    }
317 318 319 320 321
  else
    {
      if (x_offset) *x_offset = 0;
      if (y_offset) *y_offset = 0;
    }
322 323 324
}

static void
325
gtk_cell_renderer_toggle_render (GtkCellRenderer      *cell,
326
				 cairo_t              *cr,
327
				 GtkWidget            *widget,
328 329
				 const GdkRectangle   *background_area,
				 const GdkRectangle   *cell_area,
330
				 GtkCellRendererState  flags)
331
{
332
  GtkCellRendererToggle *celltoggle = GTK_CELL_RENDERER_TOGGLE (cell);
333
  GtkCellRendererTogglePrivate *priv = celltoggle->priv;
334
  gint width, height;
335
  gint x_offset, y_offset;
336
  gint xpad, ypad;
337
  GtkShadowType shadow;
338
  GtkStateType state = 0;
339

340
  gtk_cell_renderer_toggle_get_size (cell, widget, cell_area,
341 342
				     &x_offset, &y_offset,
				     &width, &height);
343 344 345
  gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
  width -= xpad * 2;
  height -= ypad * 2;
346

347 348 349
  if (width <= 0 || height <= 0)
    return;

350
  if (priv->inconsistent)
351 352
    shadow = GTK_SHADOW_ETCHED_IN;
  else
353
    shadow = priv->active ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
354

355 356
  if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE ||
      !gtk_cell_renderer_get_sensitive (cell))
357 358 359 360
    {
      state = GTK_STATE_INSENSITIVE;
    }
  else if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
361
    {
362
      if (gtk_widget_has_focus (widget))
363
	state = GTK_STATE_SELECTED;
364
      else
365
	state = GTK_STATE_ACTIVE;
366
    }
367
  else
368
    {
369
      if (priv->activatable)
Daniel Elstner's avatar
Daniel Elstner committed
370 371 372
        state = GTK_STATE_NORMAL;
      else
        state = GTK_STATE_INSENSITIVE;
373
    }
374

375
  if (priv->radio)
376
    {
377
      gtk_paint_option (gtk_widget_get_style (widget),
378 379 380 381 382 383
                              cr,
                              state, shadow,
                              widget, "cellradio",
                              cell_area->x + x_offset + xpad,
                              cell_area->y + y_offset + ypad,
                              width, height);
384 385 386
    }
  else
    {
387
      gtk_paint_check (gtk_widget_get_style (widget),
388 389 390 391 392 393
                             cr,
                             state, shadow,
                             widget, "cellcheck",
                             cell_area->x + x_offset + xpad,
                             cell_area->y + y_offset + ypad,
                             width, height);
394 395 396 397
    }
}

static gint
398 399 400 401
gtk_cell_renderer_toggle_activate (GtkCellRenderer      *cell,
				   GdkEvent             *event,
				   GtkWidget            *widget,
				   const gchar          *path,
402 403
				   const GdkRectangle   *background_area,
				   const GdkRectangle   *cell_area,
404
				   GtkCellRendererState  flags)
405
{
406
  GtkCellRendererTogglePrivate *priv;
407
  GtkCellRendererToggle *celltoggle;
408

409
  celltoggle = GTK_CELL_RENDERER_TOGGLE (cell);
410 411 412
  priv = celltoggle->priv;

  if (priv->activatable)
413
    {
Manish Singh's avatar
Manish Singh committed
414
      g_signal_emit (cell, toggle_cell_signals[TOGGLED], 0, path);
415
      return TRUE;
416
    }
417 418

  return FALSE;
419 420
}

421 422 423 424 425 426 427 428 429 430 431 432 433
/**
 * 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).
 **/
434 435 436 437
void
gtk_cell_renderer_toggle_set_radio (GtkCellRendererToggle *toggle,
				    gboolean               radio)
{
438
  GtkCellRendererTogglePrivate *priv;
439

440 441
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

442 443 444
  priv = toggle->priv;

  priv->radio = radio;
445
}
Havoc Pennington's avatar
Havoc Pennington committed
446

447 448 449
/**
 * gtk_cell_renderer_toggle_get_radio:
 * @toggle: a #GtkCellRendererToggle
Matthias Clasen's avatar
Matthias Clasen committed
450
 *
Matthias Clasen's avatar
Matthias Clasen committed
451
 * Returns whether we're rendering radio toggles rather than checkboxes. 
452 453 454
 * 
 * Return value: %TRUE if we're rendering radio toggles rather than checkboxes
 **/
Havoc Pennington's avatar
Havoc Pennington committed
455
gboolean
456
gtk_cell_renderer_toggle_get_radio (GtkCellRendererToggle *toggle)
Havoc Pennington's avatar
Havoc Pennington committed
457 458 459
{
  g_return_val_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle), FALSE);

460
  return toggle->priv->radio;
Havoc Pennington's avatar
Havoc Pennington committed
461
}
462

Matthias Clasen's avatar
Matthias Clasen committed
463 464
/**
 * gtk_cell_renderer_toggle_get_active:
465
 * @toggle: a #GtkCellRendererToggle
Matthias Clasen's avatar
Matthias Clasen committed
466 467 468 469 470
 *
 * Returns whether the cell renderer is active. See
 * gtk_cell_renderer_toggle_set_active().
 *
 * Return value: %TRUE if the cell renderer is active.
471
 **/
472 473 474 475 476
gboolean
gtk_cell_renderer_toggle_get_active (GtkCellRendererToggle *toggle)
{
  g_return_val_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle), FALSE);

477
  return toggle->priv->active;
478 479
}

Matthias Clasen's avatar
Matthias Clasen committed
480 481 482 483 484 485 486
/**
 * gtk_cell_renderer_toggle_set_active:
 * @toggle: a #GtkCellRendererToggle.
 * @setting: the value to set.
 *
 * Activates or deactivates a cell renderer.
 **/
487 488 489 490 491 492
void
gtk_cell_renderer_toggle_set_active (GtkCellRendererToggle *toggle,
				     gboolean               setting)
{
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

493
  g_object_set (toggle, "active", setting ? TRUE : FALSE, NULL);
494
}
495

496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
/**
 * 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);

512
  return toggle->priv->activatable;
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
}

/**
 * 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)
{
528
  GtkCellRendererTogglePrivate *priv;
529

530 531
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

532 533 534
  priv = toggle->priv;

  if (priv->activatable != setting)
535
    {
536
      priv->activatable = setting ? TRUE : FALSE;
537 538 539
      g_object_notify (G_OBJECT (toggle), "activatable");
    }
}