gtkcellrenderertoggle.c 13.9 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 "gtktreeprivate.h"
26

27 28 29
static void gtk_cell_renderer_toggle_get_property  (GObject                    *object,
						    guint                       param_id,
						    GValue                     *value,
Tim Janik's avatar
Tim Janik committed
30
						    GParamSpec                 *pspec);
31 32 33
static void gtk_cell_renderer_toggle_set_property  (GObject                    *object,
						    guint                       param_id,
						    const GValue               *value,
Tim Janik's avatar
Tim Janik committed
34
						    GParamSpec                 *pspec);
35 36 37 38
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,
39 40 41
 						 GdkRectangle               *cell_area,
						 gint                       *x_offset,
						 gint                       *y_offset,
42 43 44 45 46 47 48 49
						 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,
50
						 GtkCellRendererState        flags);
51 52 53
static gboolean gtk_cell_renderer_toggle_activate  (GtkCellRenderer            *cell,
						    GdkEvent                   *event,
						    GtkWidget                  *widget,
54
						    const gchar                *path,
55 56
						    GdkRectangle               *background_area,
						    GdkRectangle               *cell_area,
57
						    GtkCellRendererState        flags);
58 59 60 61 62 63 64 65 66


enum {
  TOGGLED,
  LAST_SIGNAL
};

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

#define TOGGLE_WIDTH 12

static guint toggle_cell_signals[LAST_SIGNAL] = { 0 };

77 78 79 80 81 82 83 84
#define GTK_CELL_RENDERER_TOGGLE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_TOGGLE, GtkCellRendererTogglePrivate))

typedef struct _GtkCellRendererTogglePrivate GtkCellRendererTogglePrivate;
struct _GtkCellRendererTogglePrivate
{
  guint inconsistent : 1;
};

85

Manish Singh's avatar
Manish Singh committed
86
GType
87 88
gtk_cell_renderer_toggle_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
89
  static GType cell_toggle_type = 0;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

  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
106 107 108
      cell_toggle_type =
	g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererToggle",
				&cell_toggle_info, 0);
109 110 111 112 113 114 115 116
    }

  return cell_toggle_type;
}

static void
gtk_cell_renderer_toggle_init (GtkCellRendererToggle *celltoggle)
{
117
  celltoggle->activatable = TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
118
  celltoggle->active = FALSE;
119
  celltoggle->radio = FALSE;
120
  GTK_CELL_RENDERER (celltoggle)->mode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
121 122 123 124 125 126 127 128 129 130
  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);

131 132
  object_class->get_property = gtk_cell_renderer_toggle_get_property;
  object_class->set_property = gtk_cell_renderer_toggle_set_property;
133 134 135

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

  g_object_class_install_property (object_class,
		                   PROP_INCONSISTENT,
				   g_param_spec_boolean ("inconsistent",
150 151
					                 P_("Inconsistent state"),
							 P_("The inconsistent state of the button"),
152 153 154
							 FALSE,
							 G_PARAM_READABLE |
							 G_PARAM_WRITABLE));
155
  
156 157 158
  g_object_class_install_property (object_class,
				   PROP_ACTIVATABLE,
				   g_param_spec_boolean ("activatable",
159 160
							 P_("Activatable"),
							 P_("The toggle button can be activated"),
161 162 163 164
							 TRUE,
							 G_PARAM_READABLE |
							 G_PARAM_WRITABLE));

165 166 167
  g_object_class_install_property (object_class,
				   PROP_RADIO,
				   g_param_spec_boolean ("radio",
168 169
							 P_("Radio state"),
							 P_("Draw the toggle button as a radio button"),
170 171 172
							 FALSE,
							 G_PARAM_READABLE |
							 G_PARAM_WRITABLE));
173 174 175


  toggle_cell_signals[TOGGLED] =
Manish Singh's avatar
Manish Singh committed
176 177 178 179 180 181 182 183
    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);
184 185

  g_type_class_add_private (object_class, sizeof (GtkCellRendererTogglePrivate));
186 187 188
}

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

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


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

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

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

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

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

289
  if (width)
290
    *width = calc_width;
291 292

  if (height)
293
    *height = calc_height;
Jonathan Blandford's avatar
Jonathan Blandford committed
294

295 296 297 298
  if (cell_area)
    {
      if (x_offset)
	{
Jonathan Blandford's avatar
Jonathan Blandford committed
299 300
	  *x_offset = ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
		       (1.0 - cell->xalign) : cell->xalign) * (cell_area->width - calc_width);
301
	  *x_offset = MAX (*x_offset, 0);
302 303 304
	}
      if (y_offset)
	{
305 306
	  *y_offset = cell->yalign * (cell_area->height - calc_height);
	  *y_offset = MAX (*y_offset, 0);
307 308
	}
    }
309 310 311
}

static void
312
gtk_cell_renderer_toggle_render (GtkCellRenderer      *cell,
313
				 GdkDrawable          *window,
314 315 316 317 318
				 GtkWidget            *widget,
				 GdkRectangle         *background_area,
				 GdkRectangle         *cell_area,
				 GdkRectangle         *expose_area,
				 GtkCellRendererState  flags)
319 320
{
  GtkCellRendererToggle *celltoggle = (GtkCellRendererToggle *) cell;
321
  GtkCellRendererTogglePrivate *priv;
322
  gint width, height;
323
  gint x_offset, y_offset;
324
  GtkShadowType shadow;
325
  GtkStateType state = 0;
326

327 328
  priv = GTK_CELL_RENDERER_TOGGLE_GET_PRIVATE (cell);

329 330 331
  gtk_cell_renderer_toggle_get_size (cell, widget, cell_area,
				     &x_offset, &y_offset,
				     &width, &height);
Jonathan Blandford's avatar
Jonathan Blandford committed
332 333
  width -= cell->xpad*2;
  height -= cell->ypad*2;
334

335 336 337
  if (width <= 0 || height <= 0)
    return;

338
  if (priv->inconsistent)
339 340 341
    shadow = GTK_SHADOW_ETCHED_IN;
  else
    shadow = celltoggle->active ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
342

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

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

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

  return FALSE;
399 400
}

401 402 403 404 405 406 407 408 409 410 411 412 413
/**
 * 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).
 **/
414 415 416 417 418 419 420 421
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
422

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

436
  return toggle->radio;
Havoc Pennington's avatar
Havoc Pennington committed
437
}
438

Matthias Clasen's avatar
Matthias Clasen committed
439 440
/**
 * gtk_cell_renderer_toggle_get_active:
441
 * @toggle: a #GtkCellRendererToggle
Matthias Clasen's avatar
Matthias Clasen committed
442 443 444 445 446
 *
 * Returns whether the cell renderer is active. See
 * gtk_cell_renderer_toggle_set_active().
 *
 * Return value: %TRUE if the cell renderer is active.
447
 **/
448 449 450 451 452 453 454 455
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
456 457 458 459 460 461 462
/**
 * gtk_cell_renderer_toggle_set_active:
 * @toggle: a #GtkCellRendererToggle.
 * @setting: the value to set.
 *
 * Activates or deactivates a cell renderer.
 **/
463 464 465 466 467 468
void
gtk_cell_renderer_toggle_set_active (GtkCellRendererToggle *toggle,
				     gboolean               setting)
{
  g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle));

469
  g_object_set (toggle, "active", setting ? TRUE : FALSE, NULL);
470
}