gtkcheckmenuitem.c 21.7 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * 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
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

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

25
#include "config.h"
Elliot Lee's avatar
Elliot Lee committed
26
#include "gtkcheckmenuitem.h"
27
#include "gtkmenuitemprivate.h"
Tim Janik's avatar
Tim Janik committed
28
#include "gtkaccellabel.h"
29 30
#include "gtkactivatable.h"
#include "gtktoggleaction.h"
31
#include "gtkmarshalers.h"
32 33
#include "gtkprivate.h"
#include "gtkintl.h"
34
#include "a11y/gtkcheckmenuitemaccessible.h"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

/**
 * SECTION:gtkcheckmenuitem
 * @Short_description: A menu item with a check box
 * @Title: GtkCheckMenuItem
 *
 * A #GtkCheckMenuItem is a menu item that maintains the state of a boolean
 * value in addition to a #GtkMenuItem usual role in activating application
 * code.
 *
 * A check box indicating the state of the boolean value is displayed
 * at the left side of the #GtkMenuItem.  Activating the #GtkMenuItem
 * toggles the value.
 */


51
#define INDICATOR_SIZE 16
52

53
struct _GtkCheckMenuItemPrivate
54 55 56 57 58 59 60
{
  guint active             : 1;
  guint always_show_toggle : 1;
  guint draw_as_radio      : 1;
  guint inconsistent       : 1;
};

Elliot Lee's avatar
Elliot Lee committed
61 62 63 64 65
enum {
  TOGGLED,
  LAST_SIGNAL
};

66 67 68
enum {
  PROP_0,
  PROP_ACTIVE,
69 70
  PROP_INCONSISTENT,
  PROP_DRAW_AS_RADIO
71
};
Elliot Lee's avatar
Elliot Lee committed
72

73 74
static gint gtk_check_menu_item_draw                 (GtkWidget             *widget,
                                                      cairo_t               *cr);
75 76
static void gtk_check_menu_item_activate             (GtkMenuItem           *menu_item);
static void gtk_check_menu_item_toggle_size_request  (GtkMenuItem           *menu_item,
77
                                                      gint                  *requisition);
78
static void gtk_real_check_menu_item_draw_indicator  (GtkCheckMenuItem      *check_menu_item,
79
                                                      cairo_t               *cr);
80
static void gtk_check_menu_item_set_property         (GObject               *object,
81 82 83
                                                      guint                  prop_id,
                                                      const GValue          *value,
                                                      GParamSpec            *pspec);
84
static void gtk_check_menu_item_get_property         (GObject               *object,
85 86 87
                                                      guint                  prop_id,
                                                      GValue                *value,
                                                      GParamSpec            *pspec);
88 89

static void gtk_check_menu_item_activatable_interface_init (GtkActivatableIface  *iface);
90
static void gtk_check_menu_item_update                     (GtkActivatable       *activatable,
91 92
                                                            GtkAction            *action,
                                                            const gchar          *property_name);
93
static void gtk_check_menu_item_sync_action_properties     (GtkActivatable       *activatable,
94
                                                            GtkAction            *action);
95 96 97 98 99

static GtkActivatableIface *parent_activatable_iface;
static guint                check_menu_item_signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE_WITH_CODE (GtkCheckMenuItem, gtk_check_menu_item, GTK_TYPE_MENU_ITEM,
100 101
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
                                                gtk_check_menu_item_activatable_interface_init))
Elliot Lee's avatar
Elliot Lee committed
102

103 104 105
static void
gtk_check_menu_item_class_init (GtkCheckMenuItemClass *klass)
{
Manish Singh's avatar
Manish Singh committed
106
  GObjectClass *gobject_class;
107 108 109
  GtkWidgetClass *widget_class;
  GtkMenuItemClass *menu_item_class;
  
Manish Singh's avatar
Manish Singh committed
110
  gobject_class = G_OBJECT_CLASS (klass);
111 112 113
  widget_class = (GtkWidgetClass*) klass;
  menu_item_class = (GtkMenuItemClass*) klass;
  
Manish Singh's avatar
Manish Singh committed
114 115
  gobject_class->set_property = gtk_check_menu_item_set_property;
  gobject_class->get_property = gtk_check_menu_item_get_property;
116

Manish Singh's avatar
Manish Singh committed
117
  g_object_class_install_property (gobject_class,
118 119
                                   PROP_ACTIVE,
                                   g_param_spec_boolean ("active",
120 121
                                                         P_("Active"),
                                                         P_("Whether the menu item is checked"),
122
                                                         FALSE,
123
                                                         GTK_PARAM_READWRITE));
124
  
Manish Singh's avatar
Manish Singh committed
125
  g_object_class_install_property (gobject_class,
126
                                   PROP_INCONSISTENT,
127
                                   g_param_spec_boolean ("inconsistent",
128 129
                                                         P_("Inconsistent"),
                                                         P_("Whether to display an \"inconsistent\" state"),
130
                                                         FALSE,
131
                                                         GTK_PARAM_READWRITE));
132
  
133
  g_object_class_install_property (gobject_class,
134
                                   PROP_DRAW_AS_RADIO,
135
                                   g_param_spec_boolean ("draw-as-radio",
136 137
                                                         P_("Draw as radio menu item"),
                                                         P_("Whether the menu item looks like a radio menu item"),
138
                                                         FALSE,
139
                                                         GTK_PARAM_READWRITE));
140
  
141
  gtk_widget_class_install_style_property (widget_class,
142
                                           g_param_spec_int ("indicator-size",
143
                                                             P_("Indicator Size"),
144 145 146
                                                             P_("Size of check or radio indicator"),
                                                             0,
                                                             G_MAXINT,
147
                                                             INDICATOR_SIZE,
148
                                                             GTK_PARAM_READABLE));
149

150
  widget_class->draw = gtk_check_menu_item_draw;
151

152
  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_CHECK_MENU_ITEM_ACCESSIBLE);
153

154 155
  menu_item_class->activate = gtk_check_menu_item_activate;
  menu_item_class->hide_on_activate = FALSE;
156
  menu_item_class->toggle_size_request = gtk_check_menu_item_toggle_size_request;
157 158 159 160
  
  klass->toggled = NULL;
  klass->draw_indicator = gtk_real_check_menu_item_draw_indicator;

161 162 163 164 165 166 167 168 169
  /**
   * GtkCheckMenuItem::toggled:
   * @checkmenuitem: the object which received the signal.
   *
   * This signal is emitted when the state of the check box is changed.
   *
   * A signal handler can use gtk_check_menu_item_get_active()
   * to discover the new state.
   */
170
  check_menu_item_signals[TOGGLED] =
171
    g_signal_new (I_("toggled"),
172 173 174 175 176 177
                  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);
178

179
  g_type_class_add_private (klass, sizeof (GtkCheckMenuItemPrivate));
180 181
}

182 183 184 185
static void 
gtk_check_menu_item_activatable_interface_init (GtkActivatableIface  *iface)
{
  parent_activatable_iface = g_type_interface_peek_parent (iface);
186 187
  iface->update = gtk_check_menu_item_update;
  iface->sync_action_properties = gtk_check_menu_item_sync_action_properties;
188 189
}

190 191
static void
gtk_check_menu_item_update (GtkActivatable *activatable,
192 193
                            GtkAction      *action,
                            const gchar    *property_name)
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
{
  GtkCheckMenuItem *check_menu_item;

  check_menu_item = GTK_CHECK_MENU_ITEM (activatable);

  parent_activatable_iface->update (activatable, action, property_name);

  if (strcmp (property_name, "active") == 0)
    {
      gtk_action_block_activate (action);
      gtk_check_menu_item_set_active (check_menu_item, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
      gtk_action_unblock_activate (action);
    }

  if (!gtk_activatable_get_use_action_appearance (activatable))
    return;

  if (strcmp (property_name, "draw-as-radio") == 0)
212
    gtk_check_menu_item_set_draw_as_radio (check_menu_item,
213
                                           gtk_toggle_action_get_draw_as_radio (GTK_TOGGLE_ACTION (action)));
214 215
}

216 217
static void
gtk_check_menu_item_sync_action_properties (GtkActivatable *activatable,
218
                                            GtkAction      *action)
219 220 221 222 223
{
  GtkCheckMenuItem *check_menu_item;

  check_menu_item = GTK_CHECK_MENU_ITEM (activatable);

224
  parent_activatable_iface->sync_action_properties (activatable, action);
225

226
  if (!GTK_IS_TOGGLE_ACTION (action))
227 228 229 230 231 232 233 234 235
    return;

  gtk_action_block_activate (action);
  gtk_check_menu_item_set_active (check_menu_item, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
  gtk_action_unblock_activate (action);
  
  if (!gtk_activatable_get_use_action_appearance (activatable))
    return;

236
  gtk_check_menu_item_set_draw_as_radio (check_menu_item,
237
                                         gtk_toggle_action_get_draw_as_radio (GTK_TOGGLE_ACTION (action)));
238 239
}

240 241 242 243 244 245 246
/**
 * gtk_check_menu_item_new:
 *
 * Creates a new #GtkCheckMenuItem.
 *
 * Returns: a new #GtkCheckMenuItem.
 */
Elliot Lee's avatar
Elliot Lee committed
247
GtkWidget*
248
gtk_check_menu_item_new (void)
Elliot Lee's avatar
Elliot Lee committed
249
{
Manish Singh's avatar
Manish Singh committed
250
  return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, NULL);
Elliot Lee's avatar
Elliot Lee committed
251 252
}

253 254 255 256 257 258 259 260
/**
 * gtk_check_menu_item_new_with_label:
 * @label: the string to use for the label.
 *
 * Creates a new #GtkCheckMenuItem with a label.
 *
 * Returns: a new #GtkCheckMenuItem.
 */
Elliot Lee's avatar
Elliot Lee committed
261 262 263
GtkWidget*
gtk_check_menu_item_new_with_label (const gchar *label)
{
264
  return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, 
265 266
                       "label", label,
                       NULL);
267 268 269 270 271 272
}


/**
 * gtk_check_menu_item_new_with_mnemonic:
 * @label: The text of the button, with an underscore in front of the
Matthias Clasen's avatar
Matthias Clasen committed
273
 *     character
274 275 276 277
 *
 * 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.
Matthias Clasen's avatar
Matthias Clasen committed
278 279 280
 *
 * Returns: a new #GtkCheckMenuItem
 */
281 282 283
GtkWidget*
gtk_check_menu_item_new_with_mnemonic (const gchar *label)
{
284
  return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, 
285 286 287
                       "label", label,
                       "use-underline", TRUE,
                       NULL);
Elliot Lee's avatar
Elliot Lee committed
288 289
}

290 291 292 293 294 295 296
/**
 * gtk_check_menu_item_set_active:
 * @check_menu_item: a #GtkCheckMenuItem.
 * @is_active: boolean value indicating whether the check box is active.
 *
 * Sets the active state of the menu item's check box.
 */
297 298
void
gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
299
                                gboolean          is_active)
Elliot Lee's avatar
Elliot Lee committed
300
{
301
  GtkCheckMenuItemPrivate *priv;
302

Elliot Lee's avatar
Elliot Lee committed
303 304
  g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));

305 306
  priv = check_menu_item->priv;

307 308
  is_active = is_active != 0;

309
  if (priv->active != is_active)
310
    gtk_menu_item_activate (GTK_MENU_ITEM (check_menu_item));
Elliot Lee's avatar
Elliot Lee committed
311 312
}

313 314 315 316 317 318 319 320 321 322 323 324 325 326
/**
 * 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);

327
  return check_menu_item->priv->active;
328 329
}

330 331
static void
gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
332
                                         gint        *requisition)
333
{
334
  guint toggle_spacing;
335
  guint indicator_size;
336
  
337
  g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
338 339
  
  gtk_widget_style_get (GTK_WIDGET (menu_item),
340 341 342
                        "toggle-spacing", &toggle_spacing,
                        "indicator-size", &indicator_size,
                        NULL);
343 344

  *requisition = indicator_size + toggle_spacing;
345 346
}

347 348 349 350 351 352
/**
 * gtk_check_menu_item_toggled:
 * @check_menu_item: a #GtkCheckMenuItem.
 *
 * Emits the #GtkCheckMenuItem::toggled signal.
 */
Elliot Lee's avatar
Elliot Lee committed
353 354 355
void
gtk_check_menu_item_toggled (GtkCheckMenuItem *check_menu_item)
{
Manish Singh's avatar
Manish Singh committed
356
  g_signal_emit (check_menu_item, check_menu_item_signals[TOGGLED], 0);
Elliot Lee's avatar
Elliot Lee committed
357 358
}

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
/**
 * 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)
{
378
  GtkCheckMenuItemPrivate *priv;
379

380
  g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
381 382

  priv = check_menu_item->priv;
383 384 385
  
  setting = setting != FALSE;

386
  if (setting != priv->inconsistent)
387
    {
388
      priv->inconsistent = setting;
389
      gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
390
      g_object_notify (G_OBJECT (check_menu_item), "inconsistent");
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
    }
}

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

407
  return check_menu_item->priv->inconsistent;
408 409
}

410 411 412 413 414 415 416
/**
 * 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
417
 * Since: 2.4
418 419 420
 **/
void
gtk_check_menu_item_set_draw_as_radio (GtkCheckMenuItem *check_menu_item,
421
                                       gboolean          draw_as_radio)
422
{
423
  GtkCheckMenuItemPrivate *priv;
424

425
  g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
426 427 428

  priv = check_menu_item->priv;

429 430
  draw_as_radio = draw_as_radio != FALSE;

431
  if (draw_as_radio != priv->draw_as_radio)
432
    {
433
      priv->draw_as_radio = draw_as_radio;
434 435 436

      gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));

437
      g_object_notify (G_OBJECT (check_menu_item), "draw-as-radio");
438 439 440 441 442 443 444 445 446 447
    }
}

/**
 * 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
448 449
 * 
 * Since: 2.4
450 451 452 453 454 455
 **/
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);
  
456
  return check_menu_item->priv->draw_as_radio;
457 458
}

Elliot Lee's avatar
Elliot Lee committed
459 460 461
static void
gtk_check_menu_item_init (GtkCheckMenuItem *check_menu_item)
{
462
  GtkCheckMenuItemPrivate *priv;
463 464 465

  check_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (check_menu_item,
                                                       GTK_TYPE_CHECK_MENU_ITEM,
466
                                                       GtkCheckMenuItemPrivate);
467 468 469 470
  priv = check_menu_item->priv; 

  priv->active = FALSE;
  priv->always_show_toggle = TRUE;
Elliot Lee's avatar
Elliot Lee committed
471 472 473
}

static gint
474 475
gtk_check_menu_item_draw (GtkWidget *widget,
                          cairo_t   *cr)
Elliot Lee's avatar
Elliot Lee committed
476
{
477
  GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (widget);
Elliot Lee's avatar
Elliot Lee committed
478

479 480 481 482 483
  if (GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->draw)
    GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->draw (widget, cr);

  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, cr);
Elliot Lee's avatar
Elliot Lee committed
484 485 486 487 488 489 490

  return FALSE;
}

static void
gtk_check_menu_item_activate (GtkMenuItem *menu_item)
{
491
  GtkCheckMenuItemPrivate *priv;
492

493
  GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
494 495 496
  priv = check_menu_item->priv;

  priv->active = !priv->active;
Elliot Lee's avatar
Elliot Lee committed
497 498 499

  gtk_check_menu_item_toggled (check_menu_item);
  gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
500

501 502
  GTK_MENU_ITEM_CLASS (gtk_check_menu_item_parent_class)->activate (menu_item);

503
  g_object_notify (G_OBJECT (check_menu_item), "active");
Elliot Lee's avatar
Elliot Lee committed
504 505 506 507
}

static void
gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
508
                                         cairo_t          *cr)
Elliot Lee's avatar
Elliot Lee committed
509
{
510
  GtkCheckMenuItemPrivate *priv = check_menu_item->priv;
Elliot Lee's avatar
Elliot Lee committed
511 512 513
  GtkWidget *widget;
  gint x, y;

514 515 516
  widget = GTK_WIDGET (check_menu_item);

  if (gtk_widget_is_drawable (widget))
Elliot Lee's avatar
Elliot Lee committed
517
    {
518
      GtkAllocation allocation;
519
      GtkStyleContext *context;
520
      guint border_width;
521 522 523 524
      guint offset;
      guint toggle_size;
      guint toggle_spacing;
      guint horizontal_padding;
525
      guint indicator_size;
526 527 528 529 530 531
      GtkStateFlags state;
      GtkBorder padding;

      context = gtk_widget_get_style_context (widget);
      state = gtk_widget_get_state_flags (widget);
      gtk_style_context_get_padding (context, state, &padding);
Elliot Lee's avatar
Elliot Lee committed
532

533 534
      gtk_widget_get_allocation (widget, &allocation);

535
      gtk_widget_style_get (widget,
536 537 538 539
                            "toggle-spacing", &toggle_spacing,
                            "horizontal-padding", &horizontal_padding,
                            "indicator-size", &indicator_size,
                            NULL);
540

541
      toggle_size = GTK_MENU_ITEM (check_menu_item)->priv->toggle_size;
542
      border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
543
      offset = border_width + padding.left + 2;
544 545

      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
546
        {
547
          x = offset + horizontal_padding +
548 549
            (toggle_size - toggle_spacing - indicator_size) / 2;
        }
550
      else
551
        {
552
          x = allocation.width -
553 554 555
            offset - horizontal_padding - toggle_size + toggle_spacing +
            (toggle_size - toggle_spacing - indicator_size) / 2;
        }
556

557
      y = (allocation.height - indicator_size) / 2;
Elliot Lee's avatar
Elliot Lee committed
558

559
      if (priv->active ||
560
          priv->always_show_toggle ||
561
          (state & GTK_STATE_FLAG_PRELIGHT))
562
        {
563
          gtk_style_context_save (context);
564 565

          if (priv->inconsistent)
566
            state |= GTK_STATE_FLAG_INCONSISTENT;
567
          else if (priv->active)
568
            state |= GTK_STATE_FLAG_ACTIVE;
569

570
          gtk_style_context_set_state (context, state);
571 572 573

          if (priv->draw_as_radio)
            {
574 575 576
              gtk_style_context_add_class (context, GTK_STYLE_CLASS_RADIO);
              gtk_render_option (context, cr, x, y,
                                 indicator_size, indicator_size);
577 578 579
            }
          else
            {
580 581 582
              gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
              gtk_render_check (context, cr, x, y,
                                indicator_size, indicator_size);
583
            }
584 585

          gtk_style_context_restore (context);
586
        }
Elliot Lee's avatar
Elliot Lee committed
587 588
    }
}
589 590 591 592


static void
gtk_check_menu_item_get_property (GObject     *object,
593 594 595
                                  guint        prop_id,
                                  GValue      *value,
                                  GParamSpec  *pspec)
596 597
{
  GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
598
  GtkCheckMenuItemPrivate *priv = checkitem->priv;
599 600 601 602
  
  switch (prop_id)
    {
    case PROP_ACTIVE:
603
      g_value_set_boolean (value, priv->active);
604 605
      break;
    case PROP_INCONSISTENT:
606
      g_value_set_boolean (value, priv->inconsistent);
607
      break;
608
    case PROP_DRAW_AS_RADIO:
609
      g_value_set_boolean (value, priv->draw_as_radio);
610
      break;
611 612 613 614 615 616 617 618 619
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}


static void
gtk_check_menu_item_set_property (GObject      *object,
620 621 622
                                  guint         prop_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
623 624 625 626 627 628 629 630 631 632 633
{
  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;
634 635 636
    case PROP_DRAW_AS_RADIO:
      gtk_check_menu_item_set_draw_as_radio (checkitem, g_value_get_boolean (value));
      break;
637 638 639 640 641
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657


/* Private */

/*
 * _gtk_check_menu_item_set_active:
 * @check_menu_item: a #GtkCheckMenuItem
 * @is_active: whether the action is active or not
 *
 * Sets the #GtkCheckMenuItem:active property directly. This function does
 * not emit signals or notifications: it is left to the caller to do so.
 */
void
_gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
                                 gboolean          is_active)
{
658
  GtkCheckMenuItemPrivate *priv = check_menu_item->priv;
659 660 661

  priv->active = is_active;
}