gtkcheckmenuitem.c 16.1 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-2001.  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
#include "gtkcheckmenuitem.h"
Tim Janik's avatar
Tim Janik committed
28
#include "gtkaccellabel.h"
29
#include "gtkintl.h"
30
#include "gtkmarshalers.h"
Elliot Lee's avatar
Elliot Lee committed
31

32
#define CHECK_TOGGLE_SIZE 12
Elliot Lee's avatar
Elliot Lee committed
33 34 35 36 37 38

enum {
  TOGGLED,
  LAST_SIGNAL
};

39 40 41
enum {
  PROP_0,
  PROP_ACTIVE,
42 43
  PROP_INCONSISTENT,
  PROP_DRAW_AS_RADIO
44
};
Elliot Lee's avatar
Elliot Lee committed
45

46 47 48 49 50 51
static void gtk_check_menu_item_class_init           (GtkCheckMenuItemClass *klass);
static void gtk_check_menu_item_init                 (GtkCheckMenuItem      *check_menu_item);
static gint gtk_check_menu_item_expose               (GtkWidget             *widget,
						      GdkEventExpose        *event);
static void gtk_check_menu_item_activate             (GtkMenuItem           *menu_item);
static void gtk_check_menu_item_toggle_size_request  (GtkMenuItem           *menu_item,
52
						      gint                  *requisition);
53 54 55 56
static void gtk_check_menu_item_draw_indicator       (GtkCheckMenuItem      *check_menu_item,
						      GdkRectangle          *area);
static void gtk_real_check_menu_item_draw_indicator  (GtkCheckMenuItem      *check_menu_item,
						      GdkRectangle          *area);
57 58 59 60 61 62 63 64
static void gtk_check_menu_item_set_property (GObject         *object,
					      guint            prop_id,
					      const GValue    *value,
					      GParamSpec      *pspec);
static void gtk_check_menu_item_get_property (GObject         *object,
					      guint            prop_id,
					      GValue          *value,
					      GParamSpec      *pspec);
Elliot Lee's avatar
Elliot Lee committed
65 66 67


static GtkMenuItemClass *parent_class = NULL;
68
static guint check_menu_item_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
69 70


Manish Singh's avatar
Manish Singh committed
71
GType
72
gtk_check_menu_item_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
73
{
Manish Singh's avatar
Manish Singh committed
74
  static GType check_menu_item_type = 0;
Elliot Lee's avatar
Elliot Lee committed
75 76 77

  if (!check_menu_item_type)
    {
Manish Singh's avatar
Manish Singh committed
78
      static const GTypeInfo check_menu_item_info =
Elliot Lee's avatar
Elliot Lee committed
79 80
      {
        sizeof (GtkCheckMenuItemClass),
Manish Singh's avatar
Manish Singh committed
81 82 83 84 85 86 87 88
	NULL,		/* base_init */
	NULL,		/* base_finalize */
        (GClassInitFunc) gtk_check_menu_item_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkCheckMenuItem),
	0,		/* n_preallocs */
        (GInstanceInitFunc) gtk_check_menu_item_init,
Elliot Lee's avatar
Elliot Lee committed
89 90
      };

Manish Singh's avatar
Manish Singh committed
91 92 93
      check_menu_item_type =
	g_type_register_static (GTK_TYPE_MENU_ITEM, "GtkCheckMenuItem",
				&check_menu_item_info, 0);
Elliot Lee's avatar
Elliot Lee committed
94 95 96 97 98
    }

  return check_menu_item_type;
}

99 100 101
static void
gtk_check_menu_item_class_init (GtkCheckMenuItemClass *klass)
{
Manish Singh's avatar
Manish Singh committed
102
  GObjectClass *gobject_class;
103 104 105
  GtkWidgetClass *widget_class;
  GtkMenuItemClass *menu_item_class;
  
Manish Singh's avatar
Manish Singh committed
106
  gobject_class = G_OBJECT_CLASS (klass);
107 108 109
  widget_class = (GtkWidgetClass*) klass;
  menu_item_class = (GtkMenuItemClass*) klass;
  
Manish Singh's avatar
Manish Singh committed
110
  parent_class = g_type_class_peek_parent (klass);
111

Manish Singh's avatar
Manish Singh committed
112 113
  gobject_class->set_property = gtk_check_menu_item_set_property;
  gobject_class->get_property = gtk_check_menu_item_get_property;
114

Manish Singh's avatar
Manish Singh committed
115
  g_object_class_install_property (gobject_class,
116 117 118
                                   PROP_ACTIVE,
                                   g_param_spec_boolean ("active",
                                                         _("Active"),
Soren Sandmann's avatar
Soren Sandmann committed
119
                                                         _("Whether the menu item is checked"),
120
                                                         FALSE,
121
                                                         G_PARAM_READWRITE));
122
  
Manish Singh's avatar
Manish Singh committed
123
  g_object_class_install_property (gobject_class,
Owen Taylor's avatar
Owen Taylor committed
124
                                   PROP_INCONSISTENT,
125 126
                                   g_param_spec_boolean ("inconsistent",
                                                         _("Inconsistent"),
Soren Sandmann's avatar
Soren Sandmann committed
127
                                                         _("Whether to display an \"inconsistent\" state"),
128
                                                         FALSE,
129
                                                         G_PARAM_READWRITE));
130
  
131
  g_object_class_install_property (gobject_class,
132
                                   PROP_DRAW_AS_RADIO,
133 134 135 136 137 138
                                   g_param_spec_boolean ("draw_as_radio",
                                                         _("Draw as radio menu item"),
                                                         _("Whether the menu item looks like a radio menu item"),
                                                         FALSE,
                                                         G_PARAM_READWRITE));
  
139 140 141 142
  widget_class->expose_event = gtk_check_menu_item_expose;
  
  menu_item_class->activate = gtk_check_menu_item_activate;
  menu_item_class->hide_on_activate = FALSE;
143
  menu_item_class->toggle_size_request = gtk_check_menu_item_toggle_size_request;
144 145 146 147 148
  
  klass->toggled = NULL;
  klass->draw_indicator = gtk_real_check_menu_item_draw_indicator;

  check_menu_item_signals[TOGGLED] =
Manish Singh's avatar
Manish Singh committed
149 150 151 152 153 154 155
    g_signal_new ("toggled",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkCheckMenuItemClass, toggled),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
156 157
}

Elliot Lee's avatar
Elliot Lee committed
158
GtkWidget*
159
gtk_check_menu_item_new (void)
Elliot Lee's avatar
Elliot Lee committed
160
{
Manish Singh's avatar
Manish Singh committed
161
  return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, NULL);
Elliot Lee's avatar
Elliot Lee committed
162 163 164 165 166 167
}

GtkWidget*
gtk_check_menu_item_new_with_label (const gchar *label)
{
  GtkWidget *check_menu_item;
Tim Janik's avatar
Tim Janik committed
168
  GtkWidget *accel_label;
Elliot Lee's avatar
Elliot Lee committed
169 170

  check_menu_item = gtk_check_menu_item_new ();
Tim Janik's avatar
Tim Janik committed
171 172
  accel_label = gtk_accel_label_new (label);
  gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

  gtk_container_add (GTK_CONTAINER (check_menu_item), accel_label);
  gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), check_menu_item);
  gtk_widget_show (accel_label);

  return check_menu_item;
}


/**
 * gtk_check_menu_item_new_with_mnemonic:
 * @label: The text of the button, with an underscore in front of the
 *         mnemonic character
 * @returns: a new #GtkCheckMenuItem
 *
 * Creates a new #GtkCheckMenuItem containing a label. The label
 * will be created using gtk_label_new_with_mnemonic(), so underscores
 * in @label indicate the mnemonic for the menu item.
 **/
GtkWidget*
gtk_check_menu_item_new_with_mnemonic (const gchar *label)
{
  GtkWidget *check_menu_item;
  GtkWidget *accel_label;

  check_menu_item = gtk_check_menu_item_new ();
Manish Singh's avatar
Manish Singh committed
199
  accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
200 201
  gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
  gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
Elliot Lee's avatar
Elliot Lee committed
202

Tim Janik's avatar
Tim Janik committed
203 204 205
  gtk_container_add (GTK_CONTAINER (check_menu_item), accel_label);
  gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), check_menu_item);
  gtk_widget_show (accel_label);
Elliot Lee's avatar
Elliot Lee committed
206 207 208 209

  return check_menu_item;
}

210 211 212
void
gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
				gboolean          is_active)
Elliot Lee's avatar
Elliot Lee committed
213 214 215
{
  g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));

216 217 218
  is_active = is_active != 0;

  if (check_menu_item->active != is_active)
219
    gtk_menu_item_activate (GTK_MENU_ITEM (check_menu_item));
Elliot Lee's avatar
Elliot Lee committed
220 221
}

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
/**
 * gtk_check_menu_item_get_active:
 * @check_menu_item: a #GtkCheckMenuItem
 * 
 * Returns whether the check menu item is active. See
 * gtk_check_menu_item_set_active ().
 * 
 * Return value: %TRUE if the menu item is checked.
 */
gboolean
gtk_check_menu_item_get_active (GtkCheckMenuItem *check_menu_item)
{
  g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);

  return check_menu_item->active;
}

239 240
static void
gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
241
					 gint        *requisition)
242
{
243 244
  guint toggle_spacing;
  
245
  g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
246 247 248 249 250 251
  
  gtk_widget_style_get (GTK_WIDGET (menu_item),
			"toggle_spacing", &toggle_spacing,
			NULL);
  
  *requisition = CHECK_TOGGLE_SIZE + toggle_spacing;
252 253
}

254 255 256 257 258
void
gtk_check_menu_item_set_show_toggle (GtkCheckMenuItem *menu_item,
				     gboolean          always)
{
  g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
259 260

#if 0
261
  menu_item->always_show_toggle = always != FALSE;
262
#endif  
263 264
}

Elliot Lee's avatar
Elliot Lee committed
265 266 267
void
gtk_check_menu_item_toggled (GtkCheckMenuItem *check_menu_item)
{
Manish Singh's avatar
Manish Singh committed
268
  g_signal_emit (check_menu_item, check_menu_item_signals[TOGGLED], 0);
Elliot Lee's avatar
Elliot Lee committed
269 270
}

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
/**
 * gtk_check_menu_item_set_inconsistent:
 * @check_menu_item: a #GtkCheckMenuItem
 * @setting: %TRUE to display an "inconsistent" third state check
 *
 * If the user has selected a range of elements (such as some text or
 * spreadsheet cells) that are affected by a boolean setting, and the
 * current values in that range are inconsistent, you may want to
 * display the check in an "in between" state. This function turns on
 * "in between" display.  Normally you would turn off the inconsistent
 * state again if the user explicitly selects a setting. This has to be
 * done manually, gtk_check_menu_item_set_inconsistent() only affects
 * visual appearance, it doesn't affect the semantics of the widget.
 * 
 **/
void
gtk_check_menu_item_set_inconsistent (GtkCheckMenuItem *check_menu_item,
                                      gboolean          setting)
{
  g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
  
  setting = setting != FALSE;

  if (setting != check_menu_item->inconsistent)
    {
      check_menu_item->inconsistent = setting;
      gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
298
      g_object_notify (G_OBJECT (check_menu_item), "inconsistent");
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    }
}

/**
 * gtk_check_menu_item_get_inconsistent:
 * @check_menu_item: a #GtkCheckMenuItem
 * 
 * Retrieves the value set by gtk_check_menu_item_set_inconsistent().
 * 
 * Return value: %TRUE if inconsistent
 **/
gboolean
gtk_check_menu_item_get_inconsistent (GtkCheckMenuItem *check_menu_item)
{
  g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);

  return check_menu_item->inconsistent;
}

318 319 320 321 322 323 324
/**
 * gtk_check_menu_item_set_draw_as_radio:
 * @check_menu_item: a #GtkCheckMenuItem
 * @draw_as_radio: whether @check_menu_item is drawn like a #GtkRadioMenuItem
 *
 * Sets whether @check_menu_item is drawn like a #GtkRadioMenuItem
 *
Matthias Clasen's avatar
Matthias Clasen committed
325
 * Since: 2.4
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
 **/
void
gtk_check_menu_item_set_draw_as_radio (GtkCheckMenuItem *check_menu_item,
				       gboolean          draw_as_radio)
{
  g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
  
  draw_as_radio = draw_as_radio != FALSE;

  if (draw_as_radio != check_menu_item->draw_as_radio)
    {
      check_menu_item->draw_as_radio = draw_as_radio;

      gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));

      g_object_notify (G_OBJECT (check_menu_item), "draw_as_radio");
    }
}

/**
 * gtk_check_menu_item_get_draw_as_radio:
 * @check_menu_item: a #GtkCheckMenuItem
 * 
 * Returns whether @check_menu_item looks like a #GtkRadioMenuItem
 * 
 * Return value: Whether @check_menu_item looks like a #GtkRadioMenuItem
Matthias Clasen's avatar
Matthias Clasen committed
352 353
 * 
 * Since: 2.4
354 355 356 357 358 359 360 361 362
 **/
gboolean
gtk_check_menu_item_get_draw_as_radio (GtkCheckMenuItem *check_menu_item)
{
  g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
  
  return check_menu_item->draw_as_radio;
}

Elliot Lee's avatar
Elliot Lee committed
363 364 365 366
static void
gtk_check_menu_item_init (GtkCheckMenuItem *check_menu_item)
{
  check_menu_item->active = FALSE;
367
  check_menu_item->always_show_toggle = TRUE;
Elliot Lee's avatar
Elliot Lee committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
}

static gint
gtk_check_menu_item_expose (GtkWidget      *widget,
			    GdkEventExpose *event)
{
  if (GTK_WIDGET_CLASS (parent_class)->expose_event)
    (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);

  gtk_check_menu_item_draw_indicator (GTK_CHECK_MENU_ITEM (widget), &event->area);

  return FALSE;
}

static void
gtk_check_menu_item_activate (GtkMenuItem *menu_item)
{
385
  GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
Elliot Lee's avatar
Elliot Lee committed
386 387 388 389
  check_menu_item->active = !check_menu_item->active;

  gtk_check_menu_item_toggled (check_menu_item);
  gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
390

391
  g_object_notify (G_OBJECT (check_menu_item), "active");
Elliot Lee's avatar
Elliot Lee committed
392 393 394 395 396 397
}

static void
gtk_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
				    GdkRectangle     *area)
{
398 399
  if (GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator)
    (* GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator) (check_menu_item, area);
Elliot Lee's avatar
Elliot Lee committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413
}

static void
gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
					 GdkRectangle     *area)
{
  GtkWidget *widget;
  GtkStateType state_type;
  GtkShadowType shadow_type;
  gint width, height;
  gint x, y;

  if (GTK_WIDGET_DRAWABLE (check_menu_item))
    {
414 415 416 417 418
      guint offset;
      guint toggle_size;
      guint toggle_spacing;
      guint horizontal_padding;
      
Elliot Lee's avatar
Elliot Lee committed
419 420
      widget = GTK_WIDGET (check_menu_item);

421 422 423 424 425
      gtk_widget_style_get (GTK_WIDGET (check_menu_item),
 			    "toggle_spacing", &toggle_spacing,
 			    "horizontal_padding", &horizontal_padding,
 			    NULL);

Elliot Lee's avatar
Elliot Lee committed
426 427
      width = 8;
      height = 8;
428 429 430 431

      toggle_size = GTK_MENU_ITEM (check_menu_item)->toggle_size;
      offset = GTK_CONTAINER (check_menu_item)->border_width + widget->style->xthickness;
      
432
      offset = GTK_CONTAINER (check_menu_item)->border_width +
433 434 435 436 437 438 439
	widget->style->xthickness + 2; 

      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
	{
	  x = widget->allocation.x + offset + horizontal_padding +
	    (toggle_size - toggle_spacing - width) / 2;
	}
440
      else 
441 442 443 444 445 446
	{
	  x = widget->allocation.x + widget->allocation.width -
	    offset - horizontal_padding - toggle_size + toggle_spacing +
	    (toggle_size - toggle_spacing - width) / 2;
	}
      
447
      y = widget->allocation.y + (widget->allocation.height - height) / 2;
Elliot Lee's avatar
Elliot Lee committed
448 449

      if (check_menu_item->active ||
450
	  check_menu_item->always_show_toggle ||
Elliot Lee's avatar
Elliot Lee committed
451 452 453
	  (GTK_WIDGET_STATE (check_menu_item) == GTK_STATE_PRELIGHT))
	{
	  state_type = GTK_WIDGET_STATE (widget);
454
	  
455 456 457 458 459 460 461 462 463 464 465
	  if (check_menu_item->inconsistent)
	    shadow_type = GTK_SHADOW_ETCHED_IN;
	  else if (check_menu_item->active)
	    shadow_type = GTK_SHADOW_IN;
	  else 
	    shadow_type = GTK_SHADOW_OUT;
	  
	  if (!GTK_WIDGET_IS_SENSITIVE (widget))
	    state_type = GTK_STATE_INSENSITIVE;

	  if (check_menu_item->draw_as_radio)
466
	    {
467 468 469 470
	      gtk_paint_option (widget->style, widget->window,
				state_type, shadow_type,
				area, widget, "option",
				x, y, width, height);
471 472 473
	    }
	  else
	    {
474 475 476 477
	      gtk_paint_check (widget->style, widget->window,
			       state_type, shadow_type,
			       area, widget, "check",
			       x, y, width, height);
478
	    }
Elliot Lee's avatar
Elliot Lee committed
479 480 481
	}
    }
}
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499


static void
gtk_check_menu_item_get_property (GObject     *object,
				  guint        prop_id,
				  GValue      *value,
				  GParamSpec  *pspec)
{
  GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
  
  switch (prop_id)
    {
    case PROP_ACTIVE:
      g_value_set_boolean (value, checkitem->active);
      break;
    case PROP_INCONSISTENT:
      g_value_set_boolean (value, checkitem->inconsistent);
      break;
500 501 502
    case PROP_DRAW_AS_RADIO:
      g_value_set_boolean (value, checkitem->draw_as_radio);
      break;
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}


static void
gtk_check_menu_item_set_property (GObject      *object,
				  guint         prop_id,
				  const GValue *value,
				  GParamSpec   *pspec)
{
  GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
  
  switch (prop_id)
    {
    case PROP_ACTIVE:
      gtk_check_menu_item_set_active (checkitem, g_value_get_boolean (value));
      break;
    case PROP_INCONSISTENT:
      gtk_check_menu_item_set_inconsistent (checkitem, g_value_get_boolean (value));
      break;
526 527 528
    case PROP_DRAW_AS_RADIO:
      gtk_check_menu_item_set_draw_as_radio (checkitem, g_value_get_boolean (value));
      break;
529 530 531 532 533
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}