gtktoggleaction.c 10.6 KB
Newer Older
1
/*
Cody Russell's avatar
Cody Russell committed
2
 * GTK - The GIMP Toolkit
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * Copyright (C) 1998, 1999 Red Hat, Inc.
 * All rights reserved.
 *
 * 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
Javier Jardon's avatar
Javier Jardon committed
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 19 20 21 22 23 24 25 26 27 28
 */

/*
 * Author: James Henstridge <james@daa.com.au>
 *
 * Modified by the GTK+ Team and others 2003.  See the AUTHORS
 * 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/. 
 */

29
#include "config.h"
30

31
#include "gtkintl.h"
32 33
#include "gtktoggleaction.h"
#include "gtktoggletoolbutton.h"
34
#include "gtktogglebutton.h"
35
#include "gtkcheckmenuitem.h"
36
#include "gtkprivate.h"
37

38 39 40 41 42 43 44 45 46 47

/**
 * SECTION:gtktoggleaction
 * @Short_description: An action which can be toggled between two states
 * @Title: GtkToggleAction
 *
 * A #GtkToggleAction corresponds roughly to a #GtkCheckMenuItem. It has an
 * "active" state specifying whether the action has been checked or not.
 */

48 49 50 51 52
struct _GtkToggleActionPrivate
{
  guint active        : 1;
  guint draw_as_radio : 1;
};
53

54 55 56 57 58 59
enum 
{
  TOGGLED,
  LAST_SIGNAL
};

60 61
enum {
  PROP_0,
62 63
  PROP_DRAW_AS_RADIO,
  PROP_ACTIVE
64 65
};

Matthias Clasen's avatar
Matthias Clasen committed
66
G_DEFINE_TYPE (GtkToggleAction, gtk_toggle_action, GTK_TYPE_ACTION)
67

68 69 70 71 72 73 74 75 76 77 78
static void gtk_toggle_action_activate     (GtkAction       *action);
static void set_property                   (GObject         *object,
					    guint            prop_id,
					    const GValue    *value,
					    GParamSpec      *pspec);
static void get_property                   (GObject         *object,
					    guint            prop_id,
					    GValue          *value,
					    GParamSpec      *pspec);
static GtkWidget *create_menu_item         (GtkAction       *action);

79 80 81 82 83 84 85 86 87 88 89 90 91 92

static GObjectClass *parent_class = NULL;
static guint         action_signals[LAST_SIGNAL] = { 0 };

static void
gtk_toggle_action_class_init (GtkToggleActionClass *klass)
{
  GObjectClass *gobject_class;
  GtkActionClass *action_class;

  parent_class = g_type_class_peek_parent (klass);
  gobject_class = G_OBJECT_CLASS (klass);
  action_class = GTK_ACTION_CLASS (klass);

93 94 95
  gobject_class->set_property = set_property;
  gobject_class->get_property = get_property;

96 97 98 99 100
  action_class->activate = gtk_toggle_action_activate;

  action_class->menu_item_type = GTK_TYPE_CHECK_MENU_ITEM;
  action_class->toolbar_item_type = GTK_TYPE_TOGGLE_TOOL_BUTTON;

101 102
  action_class->create_menu_item = create_menu_item;

103
  klass->toggled = NULL;
104

105 106 107 108 109 110 111 112
  /**
   * GtkToggleAction:draw-as-radio:
   *
   * Whether the proxies for this action look like radio action proxies.
   *
   * This is an appearance property and thus only applies if 
   * #GtkActivatable:use-action-appearance is %TRUE.
   */
113 114
  g_object_class_install_property (gobject_class,
                                   PROP_DRAW_AS_RADIO,
115
                                   g_param_spec_boolean ("draw-as-radio",
116 117
                                                         P_("Create the same proxies as a radio action"),
                                                         P_("Whether the proxies for this action look like radio action proxies"),
118
                                                         FALSE,
119
                                                         GTK_PARAM_READWRITE));
120

121 122 123
  /**
   * GtkToggleAction:active:
   *
124
   * Whether the toggle action should be active.
125 126 127 128 129 130 131
   *
   * Since: 2.10
   */
  g_object_class_install_property (gobject_class,
                                   PROP_ACTIVE,
                                   g_param_spec_boolean ("active",
                                                         P_("Active"),
132
                                                         P_("Whether the toggle action should be active"),
133 134
                                                         FALSE,
                                                         GTK_PARAM_READWRITE));
135 136 137 138 139 140 141
  /**
   * GtkToggleAction::toggled:
   * @toggleaction: the object which received the signal.
   *
   * Should be connected if you wish to perform an action
   * whenever the #GtkToggleAction state is changed.
   */
142
  action_signals[TOGGLED] =
Matthias Clasen's avatar
Matthias Clasen committed
143
    g_signal_new (I_("toggled"),
144 145 146 147 148 149 150 151 152 153 154 155 156
                  G_OBJECT_CLASS_TYPE (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkToggleActionClass, toggled),
		  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  g_type_class_add_private (gobject_class, sizeof (GtkToggleActionPrivate));
}

static void
gtk_toggle_action_init (GtkToggleAction *action)
{
157 158 159
  action->private_data = G_TYPE_INSTANCE_GET_PRIVATE (action,
                                                      GTK_TYPE_TOGGLE_ACTION,
                                                      GtkToggleActionPrivate);
160
  action->private_data->active = FALSE;
161 162 163
  action->private_data->draw_as_radio = FALSE;
}

164 165 166
/**
 * gtk_toggle_action_new:
 * @name: A unique name for the action
167 168
 * @label: (allow-none): The label displayed in menu items and on buttons,
 *         or %NULL
Johan Dahlin's avatar
Johan Dahlin committed
169
 * @tooltip: (allow-none): A tooltip for the action, or %NULL
170 171
 * @stock_id: (allow-none): The stock icon to display in widgets representing
 *            the action, or %NULL
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
 *
 * Creates a new #GtkToggleAction object. To add the action to
 * a #GtkActionGroup and set the accelerator for the action,
 * call gtk_action_group_add_action_with_accel().
 *
 * Return value: a new #GtkToggleAction
 *
 * Since: 2.4
 */
GtkToggleAction *
gtk_toggle_action_new (const gchar *name,
		       const gchar *label,
		       const gchar *tooltip,
		       const gchar *stock_id)
{
187 188 189 190 191 192 193 194
  g_return_val_if_fail (name != NULL, NULL);

  return g_object_new (GTK_TYPE_TOGGLE_ACTION,
		       "name", name,
		       "label", label,
		       "tooltip", tooltip,
		       "stock-id", stock_id,
		       NULL);
195 196
}

197 198 199 200 201 202 203 204 205 206 207 208 209
static void
get_property (GObject     *object,
	      guint        prop_id,
	      GValue      *value,
	      GParamSpec  *pspec)
{
  GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
  
  switch (prop_id)
    {
    case PROP_DRAW_AS_RADIO:
      g_value_set_boolean (value, gtk_toggle_action_get_draw_as_radio (action));
      break;
210 211 212
    case PROP_ACTIVE:
      g_value_set_boolean (value, gtk_toggle_action_get_active (action));
      break;
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
set_property (GObject      *object,
	      guint         prop_id,
	      const GValue *value,
	      GParamSpec   *pspec)
{
  GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
  
  switch (prop_id)
    {
    case PROP_DRAW_AS_RADIO:
      gtk_toggle_action_set_draw_as_radio (action, g_value_get_boolean (value));
      break;
232 233 234
    case PROP_ACTIVE:
      gtk_toggle_action_set_active (action, g_value_get_boolean (value));
      break;
235 236 237 238
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
239 240 241 242 243 244 245 246 247 248 249 250 251
}

static void
gtk_toggle_action_activate (GtkAction *action)
{
  GtkToggleAction *toggle_action;

  g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));

  toggle_action = GTK_TOGGLE_ACTION (action);

  toggle_action->private_data->active = !toggle_action->private_data->active;

252
  g_object_notify (G_OBJECT (action), "active");
253

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  gtk_toggle_action_toggled (toggle_action);
}

/**
 * gtk_toggle_action_toggled:
 * @action: the action object
 *
 * Emits the "toggled" signal on the toggle action.
 *
 * Since: 2.4
 */
void
gtk_toggle_action_toggled (GtkToggleAction *action)
{
  g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));

  g_signal_emit (action, action_signals[TOGGLED], 0);
}

/**
 * gtk_toggle_action_set_active:
 * @action: the action object
 * @is_active: whether the action should be checked or not
 *
 * Sets the checked state on the toggle action.
 *
 * Since: 2.4
 */
void
gtk_toggle_action_set_active (GtkToggleAction *action, 
			      gboolean         is_active)
{
  g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));

  is_active = is_active != FALSE;

  if (action->private_data->active != is_active)
291
    _gtk_action_emit_activate (GTK_ACTION (action));
292 293 294 295 296 297
}

/**
 * gtk_toggle_action_get_active:
 * @action: the action object
 *
Matthias Clasen's avatar
Matthias Clasen committed
298
 * Returns the checked state of the toggle action.
299
 *
300 301 302 303 304 305 306 307 308 309 310
 * Returns: the checked state of the toggle action
 *
 * Since: 2.4
 */
gboolean
gtk_toggle_action_get_active (GtkToggleAction *action)
{
  g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);

  return action->private_data->active;
}
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334


/**
 * gtk_toggle_action_set_draw_as_radio:
 * @action: the action object
 * @draw_as_radio: whether the action should have proxies like a radio 
 *    action
 *
 * Sets whether the action should have proxies like a radio action.
 *
 * Since: 2.4
 */
void
gtk_toggle_action_set_draw_as_radio (GtkToggleAction *action, 
				     gboolean         draw_as_radio)
{
  g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));

  draw_as_radio = draw_as_radio != FALSE;

  if (action->private_data->draw_as_radio != draw_as_radio)
    {
      action->private_data->draw_as_radio = draw_as_radio;
      
335
      g_object_notify (G_OBJECT (action), "draw-as-radio");      
336 337 338 339 340 341 342
    }
}

/**
 * gtk_toggle_action_get_draw_as_radio:
 * @action: the action object
 *
Matthias Clasen's avatar
Matthias Clasen committed
343 344
 * Returns whether the action should have proxies like a radio action.
 *
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
 * Returns: whether the action should have proxies like a radio action.
 *
 * Since: 2.4
 */
gboolean
gtk_toggle_action_get_draw_as_radio (GtkToggleAction *action)
{
  g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);

  return action->private_data->draw_as_radio;
}

static GtkWidget *
create_menu_item (GtkAction *action)
{
  GtkToggleAction *toggle_action = GTK_TOGGLE_ACTION (action);

  return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, 
363
		       "draw-as-radio", toggle_action->private_data->draw_as_radio,
364 365
		       NULL);
}
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385


/* Private */

/*
 * _gtk_toggle_action_set_active:
 * @toggle_action: a #GtkToggleAction
 * @is_active: whether the action is active or not
 *
 * Sets the #GtkToggleAction:active property directly. This function does
 * not emit signals or notifications: it is left to the caller to do so.
 */
void
_gtk_toggle_action_set_active (GtkToggleAction *toggle_action,
                               gboolean         is_active)
{
  GtkToggleActionPrivate *priv = toggle_action->private_data;

  priv->active = is_active;
}