gsimpleaction.c 16.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Copyright © 2010 Codethink Limited
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 of the licence 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.
 *
 * Authors: Ryan Lortie <desrt@desrt.ca>
 */

#include "config.h"

#include "gsimpleaction.h"

#include "gaction.h"
#include "glibintl.h"

/**
 * SECTION:gsimpleaction
 * @title: GSimpleAction
Matthias Clasen's avatar
Matthias Clasen committed
32
 * @short_description: A simple GAction implementation
33
 *
Claudio Saavedra's avatar
Claudio Saavedra committed
34
 * A #GSimpleAction is the obvious simple implementation of the #GAction
Matthias Clasen's avatar
Matthias Clasen committed
35
 * interface. This is the easiest way to create an action for purposes of
36 37 38
 * adding it to a #GSimpleActionGroup.
 *
 * See also #GtkAction.
Matthias Clasen's avatar
Matthias Clasen committed
39
 */
40
struct _GSimpleAction
41
{
42 43
  GObject       parent_instance;

44 45
  gchar        *name;
  GVariantType *parameter_type;
46
  gboolean      enabled;
47 48 49
  GVariant     *state;
};

50 51 52 53 54 55
typedef GObjectClass GSimpleActionClass;

static void g_simple_action_iface_init (GActionInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GSimpleAction, g_simple_action, G_TYPE_OBJECT,
  G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_simple_action_iface_init))

56 57 58 59 60 61 62 63 64 65 66 67
enum
{
  PROP_NONE,
  PROP_NAME,
  PROP_PARAMETER_TYPE,
  PROP_ENABLED,
  PROP_STATE_TYPE,
  PROP_STATE
};

enum
{
68
  SIGNAL_CHANGE_STATE,
69 70 71 72 73 74 75 76 77 78 79
  SIGNAL_ACTIVATE,
  NR_SIGNALS
};

static guint g_simple_action_signals[NR_SIGNALS];

static const gchar *
g_simple_action_get_name (GAction *action)
{
  GSimpleAction *simple = G_SIMPLE_ACTION (action);

80
  return simple->name;
81 82
}

Allison Karlitskaya's avatar
Allison Karlitskaya committed
83
static const GVariantType *
84 85 86 87
g_simple_action_get_parameter_type (GAction *action)
{
  GSimpleAction *simple = G_SIMPLE_ACTION (action);

88
  return simple->parameter_type;
89 90 91 92 93 94 95
}

static const GVariantType *
g_simple_action_get_state_type (GAction *action)
{
  GSimpleAction *simple = G_SIMPLE_ACTION (action);

96 97
  if (simple->state != NULL)
    return g_variant_get_type (simple->state);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  else
    return NULL;
}

static GVariant *
g_simple_action_get_state_hint (GAction *action)
{
  return NULL;
}

static gboolean
g_simple_action_get_enabled (GAction *action)
{
  GSimpleAction *simple = G_SIMPLE_ACTION (action);

113
  return simple->enabled;
114 115 116
}

static void
117 118
g_simple_action_change_state (GAction  *action,
                              GVariant *value)
119 120 121
{
  GSimpleAction *simple = G_SIMPLE_ACTION (action);

122 123 124 125 126 127 128 129 130
  /* If the user connected a signal handler then they are responsible
   * for handling state changes.
   */
  if (g_signal_has_handler_pending (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, TRUE))
    g_signal_emit (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, value);

  /* If not, then the default behaviour is to just set the state. */
  else
    g_simple_action_set_state (simple, value);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
}

/**
 * g_simple_action_set_state:
 * @simple: a #GSimpleAction
 * @value: the new #GVariant for the state
 *
 * Sets the state of the action.
 *
 * This directly updates the 'state' property to the given value.
 *
 * This should only be called by the implementor of the action.  Users
 * of the action should not attempt to directly modify the 'state'
 * property.  Instead, they should call g_action_change_state() to
 * request the change.
 *
 * Since: 2.30
 **/
void
g_simple_action_set_state (GSimpleAction *simple,
                           GVariant      *value)
{
  g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
154 155 156 157 158
  g_return_if_fail (value != NULL);

  {
    const GVariantType *state_type;

159 160
    state_type = simple->state ?
                   g_variant_get_type (simple->state) : NULL;
161 162 163 164 165 166
    g_return_if_fail (state_type != NULL);
    g_return_if_fail (g_variant_is_of_type (value, state_type));
  }

  g_variant_ref_sink (value);

167
  if (!simple->state || !g_variant_equal (simple->state, value))
168
    {
169 170
      if (simple->state)
        g_variant_unref (simple->state);
171

172
      simple->state = g_variant_ref (value);
173 174 175 176 177 178 179 180 181 182 183 184

      g_object_notify (G_OBJECT (simple), "state");
    }

  g_variant_unref (value);
}

static GVariant *
g_simple_action_get_state (GAction *action)
{
  GSimpleAction *simple = G_SIMPLE_ACTION (action);

185
  return simple->state ? g_variant_ref (simple->state) : NULL;
186 187 188 189 190 191 192 193
}

static void
g_simple_action_activate (GAction  *action,
                          GVariant *parameter)
{
  GSimpleAction *simple = G_SIMPLE_ACTION (action);

194
  g_return_if_fail (simple->parameter_type == NULL ?
195 196 197
                      parameter == NULL :
                    (parameter != NULL &&
                     g_variant_is_of_type (parameter,
198
                                           simple->parameter_type)));
199 200 201 202

  if (parameter != NULL)
    g_variant_ref_sink (parameter);

203
  if (simple->enabled)
204 205 206 207 208 209
    g_signal_emit (simple, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);

  if (parameter != NULL)
    g_variant_unref (parameter);
}

210 211 212 213 214 215 216 217 218 219 220 221 222
static void
g_simple_action_set_property (GObject    *object,
                              guint       prop_id,
                              const GValue     *value,
                              GParamSpec *pspec)
{
  GSimpleAction *action = G_SIMPLE_ACTION (object);

  switch (prop_id)
    {
    case PROP_NAME:
      action->name = g_strdup (g_value_get_string (value));
      break;
223

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    case PROP_PARAMETER_TYPE:
      action->parameter_type = g_value_dup_boxed (value);
      break;

    case PROP_ENABLED:
      action->enabled = g_value_get_boolean (value);
      break;

    case PROP_STATE:
      action->state = g_value_dup_variant (value);
      break;

    default:
      g_assert_not_reached ();
    }
}

241 242 243 244 245 246 247 248 249 250 251 252 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
static void
g_simple_action_get_property (GObject    *object,
                              guint       prop_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  GAction *action = G_ACTION (object);

  switch (prop_id)
    {
    case PROP_NAME:
      g_value_set_string (value, g_simple_action_get_name (action));
      break;

    case PROP_PARAMETER_TYPE:
      g_value_set_boxed (value, g_simple_action_get_parameter_type (action));
      break;

    case PROP_ENABLED:
      g_value_set_boolean (value, g_simple_action_get_enabled (action));
      break;

    case PROP_STATE_TYPE:
      g_value_set_boxed (value, g_simple_action_get_state_type (action));
      break;

    case PROP_STATE:
      g_value_take_variant (value, g_simple_action_get_state (action));
      break;

    default:
      g_assert_not_reached ();
    }
}

static void
g_simple_action_finalize (GObject *object)
{
  GSimpleAction *simple = G_SIMPLE_ACTION (object);

281 282 283 284 285
  g_free (simple->name);
  if (simple->parameter_type)
    g_variant_type_free (simple->parameter_type);
  if (simple->state)
    g_variant_unref (simple->state);
286 287 288 289 290 291 292 293

  G_OBJECT_CLASS (g_simple_action_parent_class)
    ->finalize (object);
}

void
g_simple_action_init (GSimpleAction *simple)
{
294
  simple->enabled = TRUE;
295 296 297 298 299 300 301 302 303 304 305
}

void
g_simple_action_iface_init (GActionInterface *iface)
{
  iface->get_name = g_simple_action_get_name;
  iface->get_parameter_type = g_simple_action_get_parameter_type;
  iface->get_state_type = g_simple_action_get_state_type;
  iface->get_state_hint = g_simple_action_get_state_hint;
  iface->get_enabled = g_simple_action_get_enabled;
  iface->get_state = g_simple_action_get_state;
306
  iface->change_state = g_simple_action_change_state;
307 308 309 310 311 312 313 314
  iface->activate = g_simple_action_activate;
}

void
g_simple_action_class_init (GSimpleActionClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

315
  object_class->set_property = g_simple_action_set_property;
316 317 318 319 320 321 322 323 324 325 326 327 328
  object_class->get_property = g_simple_action_get_property;
  object_class->finalize = g_simple_action_finalize;

  /**
   * GSimpleAction::activate:
   * @simple: the #GSimpleAction
   * @parameter: (allow-none): the parameter to the activation
   *
   * Indicates that the action was just activated.
   *
   * @parameter will always be of the expected type.  In the event that
   * an incorrect type was given, no signal will be emitted.
   *
329
   * Since: 2.28
330 331 332 333
   */
  g_simple_action_signals[SIGNAL_ACTIVATE] =
    g_signal_new (I_("activate"),
                  G_TYPE_SIMPLE_ACTION,
334
                  G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
335
                  0, NULL, NULL,
336 337 338 339
                  g_cclosure_marshal_VOID__VARIANT,
                  G_TYPE_NONE, 1,
                  G_TYPE_VARIANT);

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
  /**
   * GSimpleAction::change-state:
   * @simple: the #GSimpleAction
   * @value: (allow-none): the requested value for the state
   *
   * Indicates that the action just received a request to change its
   * state.
   *
   * @value will always be of the correct state type.  In the event that
   * an incorrect type was given, no signal will be emitted.
   *
   * If no handler is connected to this signal then the default
   * behaviour is to call g_simple_action_set_state() to set the state
   * to the requested value.  If you connect a signal handler then no
   * default action is taken.  If the state should change then you must
   * call g_simple_action_set_state() from the handler.
   *
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
   * <example>
   * <title>Example 'change-state' handler</title>
   * <programlisting>
   * static void
   * change_volume_state (GSimpleAction *action,
   *                      GVariant      *value,
   *                      gpointer       user_data)
   * {
   *   gint requested;
   *
   *   requested = g_variant_get_int32 (value);
   *
   *   // Volume only goes from 0 to 10
   *   if (0 <= requested && requested <= 10)
   *     g_simple_action_set_state (action, value);
   * }
   * </programlisting>
   * </example>
   *
   * The handler need not set the state to the requested value.  It
   * could set it to any value at all, or take some other action.
   *
379 380 381 382 383 384 385 386 387 388 389
   * Since: 2.30
   */
  g_simple_action_signals[SIGNAL_CHANGE_STATE] =
    g_signal_new (I_("change-state"),
                  G_TYPE_SIMPLE_ACTION,
                  G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
                  0, NULL, NULL,
                  g_cclosure_marshal_VOID__VARIANT,
                  G_TYPE_NONE, 1,
                  G_TYPE_VARIANT);

390 391 392 393 394 395
  /**
   * GSimpleAction:name:
   *
   * The name of the action.  This is mostly meaningful for identifying
   * the action once it has been added to a #GSimpleActionGroup.
   *
396
   * Since: 2.28
397 398 399 400 401 402
   **/
  g_object_class_install_property (object_class, PROP_NAME,
                                   g_param_spec_string ("name",
                                                        P_("Action Name"),
                                                        P_("The name used to invoke the action"),
                                                        NULL,
403
                                                        G_PARAM_READWRITE |
404
                                                        G_PARAM_CONSTRUCT_ONLY |
405 406 407 408 409 410 411 412
                                                        G_PARAM_STATIC_STRINGS));

  /**
   * GSimpleAction:parameter-type:
   *
   * The type of the parameter that must be given when activating the
   * action.
   *
413
   * Since: 2.28
414 415 416 417 418 419
   **/
  g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
                                   g_param_spec_boxed ("parameter-type",
                                                       P_("Parameter Type"),
                                                       P_("The type of GVariant passed to activate()"),
                                                       G_TYPE_VARIANT_TYPE,
420
                                                       G_PARAM_READWRITE |
421
                                                       G_PARAM_CONSTRUCT_ONLY |
422 423 424 425 426 427 428
                                                       G_PARAM_STATIC_STRINGS));

  /**
   * GSimpleAction:enabled:
   *
   * If @action is currently enabled.
   *
429 430
   * If the action is disabled then calls to g_action_activate() and
   * g_action_change_state() have no effect.
431
   *
432
   * Since: 2.28
433 434 435 436 437 438
   **/
  g_object_class_install_property (object_class, PROP_ENABLED,
                                   g_param_spec_boolean ("enabled",
                                                         P_("Enabled"),
                                                         P_("If the action can be activated"),
                                                         TRUE,
439
                                                         G_PARAM_READWRITE |
440 441 442 443 444 445 446 447
                                                         G_PARAM_STATIC_STRINGS));

  /**
   * GSimpleAction:state-type:
   *
   * The #GVariantType of the state that the action has, or %NULL if the
   * action is stateless.
   *
448
   * Since: 2.28
449 450 451 452 453 454 455 456 457 458 459 460 461 462
   **/
  g_object_class_install_property (object_class, PROP_STATE_TYPE,
                                   g_param_spec_boxed ("state-type",
                                                       P_("State Type"),
                                                       P_("The type of the state kept by the action"),
                                                       G_TYPE_VARIANT_TYPE,
                                                       G_PARAM_READABLE |
                                                       G_PARAM_STATIC_STRINGS));

  /**
   * GSimpleAction:state:
   *
   * The state of the action, or %NULL if the action is stateless.
   *
463
   * Since: 2.28
464 465 466 467 468 469 470
   **/
  g_object_class_install_property (object_class, PROP_STATE,
                                   g_param_spec_variant ("state",
                                                         P_("State"),
                                                         P_("The state the action is in"),
                                                         G_VARIANT_TYPE_ANY,
                                                         NULL,
471
                                                         G_PARAM_READWRITE |
472 473 474 475 476 477 478 479 480 481 482 483 484
                                                         G_PARAM_STATIC_STRINGS));
}

/**
 * g_simple_action_set_enabled:
 * @simple: a #GSimpleAction
 * @enabled: whether the action is enabled
 *
 * Sets the action as enabled or not.
 *
 * An action must be enabled in order to be activated or in order to
 * have its state changed from outside callers.
 *
485 486 487
 * This should only be called by the implementor of the action.  Users
 * of the action should not attempt to modify its enabled flag.
 *
488
 * Since: 2.28
489 490 491 492 493 494 495 496 497
 **/
void
g_simple_action_set_enabled (GSimpleAction *simple,
                             gboolean       enabled)
{
  g_return_if_fail (G_IS_SIMPLE_ACTION (simple));

  enabled = !!enabled;

498
  if (simple->enabled != enabled)
499
    {
500
      simple->enabled = enabled;
501 502 503 504 505 506 507 508 509 510 511 512 513 514
      g_object_notify (G_OBJECT (simple), "enabled");
    }
}
/**
 * g_simple_action_new:
 * @name: the name of the action
 * @parameter_type: (allow-none): the type of parameter to the activate function
 *
 * Creates a new action.
 *
 * The created action is stateless.  See g_simple_action_new_stateful().
 *
 * Returns: a new #GSimpleAction
 *
515
 * Since: 2.28
516 517 518 519 520
 **/
GSimpleAction *
g_simple_action_new (const gchar        *name,
                     const GVariantType *parameter_type)
{
521 522 523 524
  return g_object_new (G_TYPE_SIMPLE_ACTION,
                       "name", name,
                       "parameter-type", parameter_type,
                       NULL);
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
}

/**
 * g_simple_action_new_stateful:
 * @name: the name of the action
 * @parameter_type: (allow-none): the type of the parameter to the activate function
 * @state: the initial state of the action
 *
 * Creates a new stateful action.
 *
 * @state is the initial state of the action.  All future state values
 * must have the same #GVariantType as the initial state.
 *
 * If the @state GVariant is floating, it is consumed.
 *
 * Returns: a new #GSimpleAction
 *
542
 * Since: 2.28
543 544 545 546 547 548
 **/
GSimpleAction *
g_simple_action_new_stateful (const gchar        *name,
                              const GVariantType *parameter_type,
                              GVariant           *state)
{
549 550 551 552 553
  return g_object_new (G_TYPE_SIMPLE_ACTION,
                       "name", name,
                       "parameter-type", parameter_type,
                       "state", state,
                       NULL);
554
}