gtktogglebutton.c 14.2 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

Elliot Lee's avatar
Elliot Lee committed
27 28
#include "gtklabel.h"
#include "gtkmain.h"
29
#include "gtkmarshalers.h"
Elliot Lee's avatar
Elliot Lee committed
30
#include "gtktogglebutton.h"
Owen Taylor's avatar
Owen Taylor committed
31
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
32 33 34 35 36 37 38 39 40 41

#define DEFAULT_LEFT_POS  4
#define DEFAULT_TOP_POS   4
#define DEFAULT_SPACING   7

enum {
  TOGGLED,
  LAST_SIGNAL
};

42
enum {
Owen Taylor's avatar
Owen Taylor committed
43 44 45 46
  PROP_0,
  PROP_ACTIVE,
  PROP_INCONSISTENT,
  PROP_DRAW_INDICATOR
47 48
};

Elliot Lee's avatar
Elliot Lee committed
49

Owen Taylor's avatar
Owen Taylor committed
50 51 52 53
static void gtk_toggle_button_class_init    (GtkToggleButtonClass *klass);
static void gtk_toggle_button_init          (GtkToggleButton      *toggle_button);
static gint gtk_toggle_button_expose        (GtkWidget            *widget,
					     GdkEventExpose       *event);
54 55
static gboolean gtk_toggle_button_mnemonic_activate  (GtkWidget            *widget,
                                                      gboolean              group_cycling);
Owen Taylor's avatar
Owen Taylor committed
56 57 58 59 60 61 62 63 64 65 66
static void gtk_toggle_button_pressed       (GtkButton            *button);
static void gtk_toggle_button_released      (GtkButton            *button);
static void gtk_toggle_button_clicked       (GtkButton            *button);
static void gtk_toggle_button_set_property  (GObject              *object,
					     guint                 prop_id,
					     const GValue         *value,
					     GParamSpec           *pspec);
static void gtk_toggle_button_get_property  (GObject              *object,
					     guint                 prop_id,
					     GValue               *value,
					     GParamSpec           *pspec);
67
static void gtk_toggle_button_update_state  (GtkButton            *button);
Elliot Lee's avatar
Elliot Lee committed
68

69
static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
70
static GtkContainerClass *parent_class = NULL;
Elliot Lee's avatar
Elliot Lee committed
71

Manish Singh's avatar
Manish Singh committed
72
GType
73
gtk_toggle_button_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
74
{
Manish Singh's avatar
Manish Singh committed
75
  static GType toggle_button_type = 0;
Elliot Lee's avatar
Elliot Lee committed
76 77 78

  if (!toggle_button_type)
    {
Manish Singh's avatar
Manish Singh committed
79
      static const GTypeInfo toggle_button_info =
Elliot Lee's avatar
Elliot Lee committed
80 81
      {
	sizeof (GtkToggleButtonClass),
Manish Singh's avatar
Manish Singh committed
82 83 84 85 86 87 88 89
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_toggle_button_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkToggleButton),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_toggle_button_init,
Elliot Lee's avatar
Elliot Lee committed
90 91
      };

Manish Singh's avatar
Manish Singh committed
92 93 94
      toggle_button_type =
	g_type_register_static (GTK_TYPE_BUTTON, "GtkToggleButton",
				&toggle_button_info, 0);
Elliot Lee's avatar
Elliot Lee committed
95 96 97 98 99 100 101 102
    }

  return toggle_button_type;
}

static void
gtk_toggle_button_class_init (GtkToggleButtonClass *class)
{
Manish Singh's avatar
Manish Singh committed
103
  GObjectClass *gobject_class;
Elliot Lee's avatar
Elliot Lee committed
104 105 106 107
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;
  GtkButtonClass *button_class;

Owen Taylor's avatar
Owen Taylor committed
108
  gobject_class = G_OBJECT_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
109 110 111 112
  widget_class = (GtkWidgetClass*) class;
  container_class = (GtkContainerClass*) class;
  button_class = (GtkButtonClass*) class;

Manish Singh's avatar
Manish Singh committed
113
  parent_class = g_type_class_peek_parent (class);
Elliot Lee's avatar
Elliot Lee committed
114

Owen Taylor's avatar
Owen Taylor committed
115 116
  gobject_class->set_property = gtk_toggle_button_set_property;
  gobject_class->get_property = gtk_toggle_button_get_property;
117

118
  widget_class->expose_event = gtk_toggle_button_expose;
119
  widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate;
Elliot Lee's avatar
Elliot Lee committed
120 121 122 123

  button_class->pressed = gtk_toggle_button_pressed;
  button_class->released = gtk_toggle_button_released;
  button_class->clicked = gtk_toggle_button_clicked;
124 125
  button_class->enter = gtk_toggle_button_update_state;
  button_class->leave = gtk_toggle_button_update_state;
Elliot Lee's avatar
Elliot Lee committed
126 127

  class->toggled = NULL;
128

Owen Taylor's avatar
Owen Taylor committed
129 130 131 132 133 134 135 136 137 138 139 140
  g_object_class_install_property (gobject_class,
                                   PROP_ACTIVE,
                                   g_param_spec_boolean ("active",
							 _("Active"),
							 _("If the toggle button should be pressed in or not"),
							 FALSE,
							 G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class,
                                   PROP_INCONSISTENT,
                                   g_param_spec_boolean ("inconsistent",
							 _("Inconsistent"),
Soren Sandmann's avatar
Soren Sandmann committed
141
							 _("If the toggle button is in an \"in between\" state"),
Owen Taylor's avatar
Owen Taylor committed
142 143 144 145 146 147 148 149 150 151
							 FALSE,
							 G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class,
                                   PROP_DRAW_INDICATOR,
                                   g_param_spec_boolean ("draw_indicator",
							 _("Draw Indicator"),
							 _("If the toggle part of the button is displayed"),
							 FALSE,
							 G_PARAM_READWRITE));
152 153

  toggle_button_signals[TOGGLED] =
Manish Singh's avatar
Manish Singh committed
154 155 156 157 158 159 160
    g_signal_new ("toggled",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkToggleButtonClass, toggled),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
Elliot Lee's avatar
Elliot Lee committed
161 162 163 164 165 166 167
}

static void
gtk_toggle_button_init (GtkToggleButton *toggle_button)
{
  toggle_button->active = FALSE;
  toggle_button->draw_indicator = FALSE;
168
  GTK_BUTTON (toggle_button)->depress_on_activate = TRUE;
Elliot Lee's avatar
Elliot Lee committed
169 170 171 172
}


GtkWidget*
173
gtk_toggle_button_new (void)
Elliot Lee's avatar
Elliot Lee committed
174
{
Manish Singh's avatar
Manish Singh committed
175
  return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
Elliot Lee's avatar
Elliot Lee committed
176 177 178 179 180
}

GtkWidget*
gtk_toggle_button_new_with_label (const gchar *label)
{
181
  return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
Elliot Lee's avatar
Elliot Lee committed
182 183
}

184 185 186 187 188 189 190 191 192 193 194 195 196
/**
 * gtk_toggle_button_new_with_mnemonic:
 * @label: the text of the button, with an underscore in front of the
 *         mnemonic character
 * @returns: a new #GtkToggleButton
 *
 * Creates a new #GtkToggleButton containing a label. The label
 * will be created using gtk_label_new_with_mnemonic(), so underscores
 * in @label indicate the mnemonic for the button.
 **/
GtkWidget*
gtk_toggle_button_new_with_mnemonic (const gchar *label)
{
197
  return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, "use_underline", TRUE, NULL);
198 199
}

200
static void
Owen Taylor's avatar
Owen Taylor committed
201 202 203 204
gtk_toggle_button_set_property (GObject      *object,
				guint         prop_id,
				const GValue *value,
				GParamSpec   *pspec)
205 206 207 208 209
{
  GtkToggleButton *tb;

  tb = GTK_TOGGLE_BUTTON (object);

Owen Taylor's avatar
Owen Taylor committed
210
  switch (prop_id)
211
    {
Owen Taylor's avatar
Owen Taylor committed
212 213 214 215 216
    case PROP_ACTIVE:
      gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
      break;
    case PROP_INCONSISTENT:
      gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
217
      break;
Owen Taylor's avatar
Owen Taylor committed
218 219
    case PROP_DRAW_INDICATOR:
      gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
220 221 222 223 224 225 226
      break;
    default:
      break;
    }
}

static void
Owen Taylor's avatar
Owen Taylor committed
227 228 229 230
gtk_toggle_button_get_property (GObject      *object,
				guint         prop_id,
				GValue       *value,
				GParamSpec   *pspec)
231 232 233 234 235
{
  GtkToggleButton *tb;

  tb = GTK_TOGGLE_BUTTON (object);

Owen Taylor's avatar
Owen Taylor committed
236
  switch (prop_id)
237
    {
Owen Taylor's avatar
Owen Taylor committed
238 239 240 241 242
    case PROP_ACTIVE:
      g_value_set_boolean (value, tb->active);
      break;
    case PROP_INCONSISTENT:
      g_value_set_boolean (value, tb->inconsistent);
243
      break;
Owen Taylor's avatar
Owen Taylor committed
244 245
    case PROP_DRAW_INDICATOR:
      g_value_set_boolean (value, tb->draw_indicator);
246 247
      break;
    default:
Owen Taylor's avatar
Owen Taylor committed
248
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249 250 251 252
      break;
    }
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266
/**
 * gtk_toggle_button_set_mode:
 * @toggle_button: a #GtkToggleButton
 * @draw_indicator: if %TRUE, draw the button as a separate indicator
 * and label; if %FALSE, draw the button like a normal button
 *
 * Sets whether the button is displayed as a separate indicator and label.
 * You can call this function on a checkbutton or a radiobutton with
 * @draw_indicator = %FALSE to make the button look like a normal button
 *
 * This function only effects instances of classes like #GtkCheckButton
 * and #GtkRadioButton that derive from #GtkToggleButton,
 * not instances of #GtkToggleButton itself.
 */
Elliot Lee's avatar
Elliot Lee committed
267 268
void
gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
269
			    gboolean         draw_indicator)
Elliot Lee's avatar
Elliot Lee committed
270
{
271 272
  GtkWidget *widget;

Elliot Lee's avatar
Elliot Lee committed
273 274
  g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));

275 276
  widget = GTK_WIDGET (toggle_button);

Elliot Lee's avatar
Elliot Lee committed
277 278 279 280
  draw_indicator = draw_indicator ? TRUE : FALSE;

  if (toggle_button->draw_indicator != draw_indicator)
    {
281
      toggle_button->draw_indicator = draw_indicator;
282
      GTK_BUTTON (toggle_button)->depress_on_activate = !draw_indicator;
283
      
Elliot Lee's avatar
Elliot Lee committed
284 285
      if (GTK_WIDGET_VISIBLE (toggle_button))
	gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
Owen Taylor's avatar
Owen Taylor committed
286 287

      g_object_notify (G_OBJECT (toggle_button), "draw_indicator");
Elliot Lee's avatar
Elliot Lee committed
288 289 290
    }
}

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
/**
 * gtk_toggle_button_get_mode:
 * @toggle_button: a #GtkToggleButton
 *
 * Retrieves whether the button is displayed as a separate indicator
 * and label. See gtk_toggle_button_set_mode().
 *
 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
 *   and label.
 **/
gboolean
gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
{
  g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);

  return toggle_button->draw_indicator;
}
308

Elliot Lee's avatar
Elliot Lee committed
309
void
310 311
gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
			      gboolean         is_active)
Elliot Lee's avatar
Elliot Lee committed
312 313 314
{
  g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));

Owen Taylor's avatar
Owen Taylor committed
315
  is_active = is_active != FALSE;
316 317

  if (toggle_button->active != is_active)
Elliot Lee's avatar
Elliot Lee committed
318 319 320
    gtk_button_clicked (GTK_BUTTON (toggle_button));
}

321 322 323 324 325 326 327 328 329 330

gboolean
gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
{
  g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);

  return (toggle_button->active) ? TRUE : FALSE;
}


Elliot Lee's avatar
Elliot Lee committed
331 332 333
void
gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
{
334 335
  g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));

Manish Singh's avatar
Manish Singh committed
336
  g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
Elliot Lee's avatar
Elliot Lee committed
337 338
}

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
/**
 * gtk_toggle_button_set_inconsistent:
 * @toggle_button: a #GtkToggleButton
 * @setting: %TRUE if state is inconsistent
 *
 * If the user has selected a range of elements (such as some text or
 * spreadsheet cells) that are affected by a toggle button, and the
 * current values in that range are inconsistent, you may want to
 * display the toggle in an "in between" state. This function turns on
 * "in between" display.  Normally you would turn off the inconsistent
 * state again if the user toggles the toggle button. This has to be
 * done manually, gtk_toggle_button_set_inconsistent() only affects
 * visual appearance, it doesn't affect the semantics of the button.
 * 
 **/
void
gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
                                    gboolean         setting)
{
  g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
  
  setting = setting != FALSE;

  if (setting != toggle_button->inconsistent)
    {
      toggle_button->inconsistent = setting;
365 366
      
      gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
367
      gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
Owen Taylor's avatar
Owen Taylor committed
368 369

      g_object_notify (G_OBJECT (toggle_button), "inconsistent");      
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    }
}

/**
 * gtk_toggle_button_get_inconsistent:
 * @toggle_button: a #GtkToggleButton
 * 
 * Gets the value set by gtk_toggle_button_set_inconsistent().
 * 
 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
 **/
gboolean
gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
{
  g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);

  return toggle_button->inconsistent;
}
Elliot Lee's avatar
Elliot Lee committed
388

389 390 391
static gint
gtk_toggle_button_expose (GtkWidget      *widget,
			  GdkEventExpose *event)
Elliot Lee's avatar
Elliot Lee committed
392
{
393
  if (GTK_WIDGET_DRAWABLE (widget))
Elliot Lee's avatar
Elliot Lee committed
394
    {
395 396 397 398
      GtkWidget *child = GTK_BIN (widget)->child;
      GtkButton *button = GTK_BUTTON (widget);
      GtkStateType state_type;
      GtkShadowType shadow_type;
Elliot Lee's avatar
Elliot Lee committed
399

400 401
      state_type = GTK_WIDGET_STATE (widget);
      
402
      if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
403 404 405 406 407
        {
          if (state_type == GTK_STATE_ACTIVE)
            state_type = GTK_STATE_NORMAL;
          shadow_type = GTK_SHADOW_ETCHED_IN;
        }
Elliot Lee's avatar
Elliot Lee committed
408
      else
409
	shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
410

411 412
      _gtk_button_paint (button, &event->area, state_type, shadow_type,
			 "togglebutton", "togglebuttondefault");
413 414 415

      if (child)
	gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
416 417
    }
  
418
  return FALSE;
419 420
}

421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
static gboolean
gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
                                     gboolean   group_cycling)
{
  /*
   * We override the standard implementation in 
   * gtk_widget_real_mnemonic_activate() in order to focus the widget even
   * if there is no mnemonic conflict.
   */
  if (GTK_WIDGET_CAN_FOCUS (widget))
    gtk_widget_grab_focus (widget);

  if (!group_cycling)
    gtk_widget_activate (widget);

  return TRUE;
}

Elliot Lee's avatar
Elliot Lee committed
439 440 441 442 443
static void
gtk_toggle_button_pressed (GtkButton *button)
{
  button->button_down = TRUE;

444
  gtk_toggle_button_update_state (button);
445
  gtk_widget_queue_draw (GTK_WIDGET (button));
Elliot Lee's avatar
Elliot Lee committed
446 447 448 449 450 451 452 453 454 455
}

static void
gtk_toggle_button_released (GtkButton *button)
{
  if (button->button_down)
    {
      button->button_down = FALSE;

      if (button->in_button)
456
	gtk_button_clicked (button);
Elliot Lee's avatar
Elliot Lee committed
457

458
      gtk_toggle_button_update_state (button);
459
      gtk_widget_queue_draw (GTK_WIDGET (button));
Elliot Lee's avatar
Elliot Lee committed
460 461 462 463 464 465
    }
}

static void
gtk_toggle_button_clicked (GtkButton *button)
{
466
  GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
Elliot Lee's avatar
Elliot Lee committed
467 468 469 470
  toggle_button->active = !toggle_button->active;

  gtk_toggle_button_toggled (toggle_button);

471
  gtk_toggle_button_update_state (button);
Owen Taylor's avatar
Owen Taylor committed
472 473

  g_object_notify (G_OBJECT (toggle_button), "active");
Elliot Lee's avatar
Elliot Lee committed
474 475
}

476 477 478 479 480 481 482 483 484 485
static void
gtk_toggle_button_update_state (GtkButton *button)
{
  GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
  gboolean depressed;
  GtkStateType new_state;

  if (toggle_button->inconsistent)
    depressed = FALSE;
  else if (button->in_button && button->button_down)
486
    depressed = TRUE;
487 488 489
  else
    depressed = toggle_button->active;
      
490
  if (button->in_button && (!button->button_down || toggle_button->draw_indicator))
491 492
    new_state = GTK_STATE_PRELIGHT;
  else
493
    new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
494 495 496 497

  _gtk_button_set_depressed (button, depressed); 
  gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);
}