Blame gio/gsimpleaction.c

Packit ae235b
/*
Packit ae235b
 * Copyright © 2010 Codethink Limited
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Authors: Ryan Lortie <desrt@desrt.ca>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gsimpleaction.h"
Packit ae235b
Packit ae235b
#include "gaction.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gsimpleaction
Packit ae235b
 * @title: GSimpleAction
Packit ae235b
 * @short_description: A simple GAction implementation
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * A #GSimpleAction is the obvious simple implementation of the #GAction
Packit ae235b
 * interface. This is the easiest way to create an action for purposes of
Packit ae235b
 * adding it to a #GSimpleActionGroup.
Packit ae235b
 *
Packit ae235b
 * See also #GtkAction.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GSimpleAction:
Packit ae235b
 *
Packit ae235b
 * #GSimpleAction is an opaque data structure and can only be accessed
Packit ae235b
 * using the following functions.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
struct _GSimpleAction
Packit ae235b
{
Packit ae235b
  GObject       parent_instance;
Packit ae235b
Packit ae235b
  gchar        *name;
Packit ae235b
  GVariantType *parameter_type;
Packit ae235b
  gboolean      enabled;
Packit ae235b
  GVariant     *state;
Packit ae235b
  GVariant     *state_hint;
Packit ae235b
  gboolean      state_set_already;
Packit ae235b
};
Packit ae235b
Packit ae235b
typedef GObjectClass GSimpleActionClass;
Packit ae235b
Packit ae235b
static void g_simple_action_iface_init (GActionInterface *iface);
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GSimpleAction, g_simple_action, G_TYPE_OBJECT,
Packit ae235b
  G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_simple_action_iface_init))
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  PROP_NONE,
Packit ae235b
  PROP_NAME,
Packit ae235b
  PROP_PARAMETER_TYPE,
Packit ae235b
  PROP_ENABLED,
Packit ae235b
  PROP_STATE_TYPE,
Packit ae235b
  PROP_STATE
Packit ae235b
};
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  SIGNAL_CHANGE_STATE,
Packit ae235b
  SIGNAL_ACTIVATE,
Packit ae235b
  NR_SIGNALS
Packit ae235b
};
Packit ae235b
Packit ae235b
static guint g_simple_action_signals[NR_SIGNALS];
Packit ae235b
Packit ae235b
static const gchar *
Packit ae235b
g_simple_action_get_name (GAction *action)
Packit ae235b
{
Packit ae235b
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
Packit ae235b
Packit ae235b
  return simple->name;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const GVariantType *
Packit ae235b
g_simple_action_get_parameter_type (GAction *action)
Packit ae235b
{
Packit ae235b
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
Packit ae235b
Packit ae235b
  return simple->parameter_type;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const GVariantType *
Packit ae235b
g_simple_action_get_state_type (GAction *action)
Packit ae235b
{
Packit ae235b
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
Packit ae235b
Packit ae235b
  if (simple->state != NULL)
Packit ae235b
    return g_variant_get_type (simple->state);
Packit ae235b
  else
Packit ae235b
    return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GVariant *
Packit ae235b
g_simple_action_get_state_hint (GAction *action)
Packit ae235b
{
Packit ae235b
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
Packit ae235b
Packit ae235b
  if (simple->state_hint != NULL)
Packit ae235b
    return g_variant_ref (simple->state_hint);
Packit ae235b
  else
Packit ae235b
    return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_simple_action_get_enabled (GAction *action)
Packit ae235b
{
Packit ae235b
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
Packit ae235b
Packit ae235b
  return simple->enabled;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_simple_action_change_state (GAction  *action,
Packit ae235b
                              GVariant *value)
Packit ae235b
{
Packit ae235b
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
Packit ae235b
Packit ae235b
  /* If the user connected a signal handler then they are responsible
Packit ae235b
   * for handling state changes.
Packit ae235b
   */
Packit ae235b
  if (g_signal_has_handler_pending (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, TRUE))
Packit ae235b
    g_signal_emit (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, value);
Packit ae235b
Packit ae235b
  /* If not, then the default behaviour is to just set the state. */
Packit ae235b
  else
Packit ae235b
    g_simple_action_set_state (simple, value);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_action_set_state:
Packit ae235b
 * @simple: a #GSimpleAction
Packit ae235b
 * @value: the new #GVariant for the state
Packit ae235b
 *
Packit ae235b
 * Sets the state of the action.
Packit ae235b
 *
Packit ae235b
 * This directly updates the 'state' property to the given value.
Packit ae235b
 *
Packit ae235b
 * This should only be called by the implementor of the action.  Users
Packit ae235b
 * of the action should not attempt to directly modify the 'state'
Packit ae235b
 * property.  Instead, they should call g_action_change_state() to
Packit ae235b
 * request the change.
Packit ae235b
 *
Packit ae235b
 * If the @value GVariant is floating, it is consumed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_action_set_state (GSimpleAction *simple,
Packit ae235b
                           GVariant      *value)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
Packit ae235b
  g_return_if_fail (value != NULL);
Packit ae235b
Packit ae235b
  {
Packit ae235b
    const GVariantType *state_type;
Packit ae235b
Packit ae235b
    state_type = simple->state ?
Packit ae235b
                   g_variant_get_type (simple->state) : NULL;
Packit ae235b
    g_return_if_fail (state_type != NULL);
Packit ae235b
    g_return_if_fail (g_variant_is_of_type (value, state_type));
Packit ae235b
  }
Packit ae235b
Packit ae235b
  g_variant_ref_sink (value);
Packit ae235b
Packit ae235b
  if (!simple->state || !g_variant_equal (simple->state, value))
Packit ae235b
    {
Packit ae235b
      if (simple->state)
Packit ae235b
        g_variant_unref (simple->state);
Packit ae235b
Packit ae235b
      simple->state = g_variant_ref (value);
Packit ae235b
Packit ae235b
      g_object_notify (G_OBJECT (simple), "state");
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_variant_unref (value);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GVariant *
Packit ae235b
g_simple_action_get_state (GAction *action)
Packit ae235b
{
Packit ae235b
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
Packit ae235b
Packit ae235b
  return simple->state ? g_variant_ref (simple->state) : NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_simple_action_activate (GAction  *action,
Packit ae235b
                          GVariant *parameter)
Packit ae235b
{
Packit ae235b
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
Packit ae235b
Packit ae235b
  g_return_if_fail (simple->parameter_type == NULL ?
Packit ae235b
                      parameter == NULL :
Packit ae235b
                    (parameter != NULL &&
Packit ae235b
                     g_variant_is_of_type (parameter,
Packit ae235b
                                           simple->parameter_type)));
Packit ae235b
Packit ae235b
  if (parameter != NULL)
Packit ae235b
    g_variant_ref_sink (parameter);
Packit ae235b
Packit ae235b
  if (simple->enabled)
Packit ae235b
    {
Packit ae235b
      /* If the user connected a signal handler then they are responsible
Packit ae235b
       * for handling activation.
Packit ae235b
       */
Packit ae235b
      if (g_signal_has_handler_pending (action, g_simple_action_signals[SIGNAL_ACTIVATE], 0, TRUE))
Packit ae235b
        g_signal_emit (action, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);
Packit ae235b
Packit ae235b
      /* If not, do some reasonable defaults for stateful actions. */
Packit ae235b
      else if (simple->state)
Packit ae235b
        {
Packit ae235b
          /* If we have no parameter and this is a boolean action, toggle. */
Packit ae235b
          if (parameter == NULL && g_variant_is_of_type (simple->state, G_VARIANT_TYPE_BOOLEAN))
Packit ae235b
            {
Packit ae235b
              gboolean was_enabled = g_variant_get_boolean (simple->state);
Packit ae235b
              g_simple_action_change_state (action, g_variant_new_boolean (!was_enabled));
Packit ae235b
            }
Packit ae235b
Packit ae235b
          /* else, if the parameter and state type are the same, do a change-state */
Packit ae235b
          else if (g_variant_is_of_type (simple->state, g_variant_get_type (parameter)))
Packit ae235b
            g_simple_action_change_state (action, parameter);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (parameter != NULL)
Packit ae235b
    g_variant_unref (parameter);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_simple_action_set_property (GObject    *object,
Packit ae235b
                              guint       prop_id,
Packit ae235b
                              const GValue     *value,
Packit ae235b
                              GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  GSimpleAction *action = G_SIMPLE_ACTION (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_NAME:
Packit ae235b
      action->name = g_strdup (g_value_get_string (value));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_PARAMETER_TYPE:
Packit ae235b
      action->parameter_type = g_value_dup_boxed (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_ENABLED:
Packit ae235b
      action->enabled = g_value_get_boolean (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_STATE:
Packit ae235b
      /* The first time we see this (during construct) we should just
Packit ae235b
       * take the state as it was handed to us.
Packit ae235b
       *
Packit ae235b
       * After that, we should make sure we go through the same checks
Packit ae235b
       * as the C API.
Packit ae235b
       */
Packit ae235b
      if (!action->state_set_already)
Packit ae235b
        {
Packit ae235b
          action->state = g_value_dup_variant (value);
Packit ae235b
          action->state_set_already = TRUE;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        g_simple_action_set_state (action, g_value_get_variant (value));
Packit ae235b
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_simple_action_get_property (GObject    *object,
Packit ae235b
                              guint       prop_id,
Packit ae235b
                              GValue     *value,
Packit ae235b
                              GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  GAction *action = G_ACTION (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_NAME:
Packit ae235b
      g_value_set_string (value, g_simple_action_get_name (action));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_PARAMETER_TYPE:
Packit ae235b
      g_value_set_boxed (value, g_simple_action_get_parameter_type (action));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_ENABLED:
Packit ae235b
      g_value_set_boolean (value, g_simple_action_get_enabled (action));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_STATE_TYPE:
Packit ae235b
      g_value_set_boxed (value, g_simple_action_get_state_type (action));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_STATE:
Packit ae235b
      g_value_take_variant (value, g_simple_action_get_state (action));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_simple_action_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GSimpleAction *simple = G_SIMPLE_ACTION (object);
Packit ae235b
Packit ae235b
  g_free (simple->name);
Packit ae235b
  if (simple->parameter_type)
Packit ae235b
    g_variant_type_free (simple->parameter_type);
Packit ae235b
  if (simple->state)
Packit ae235b
    g_variant_unref (simple->state);
Packit ae235b
  if (simple->state_hint)
Packit ae235b
    g_variant_unref (simple->state_hint);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_simple_action_parent_class)
Packit ae235b
    ->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_simple_action_init (GSimpleAction *simple)
Packit ae235b
{
Packit ae235b
  simple->enabled = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_simple_action_iface_init (GActionInterface *iface)
Packit ae235b
{
Packit ae235b
  iface->get_name = g_simple_action_get_name;
Packit ae235b
  iface->get_parameter_type = g_simple_action_get_parameter_type;
Packit ae235b
  iface->get_state_type = g_simple_action_get_state_type;
Packit ae235b
  iface->get_state_hint = g_simple_action_get_state_hint;
Packit ae235b
  iface->get_enabled = g_simple_action_get_enabled;
Packit ae235b
  iface->get_state = g_simple_action_get_state;
Packit ae235b
  iface->change_state = g_simple_action_change_state;
Packit ae235b
  iface->activate = g_simple_action_activate;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_simple_action_class_init (GSimpleActionClass *class)
Packit ae235b
{
Packit ae235b
  GObjectClass *object_class = G_OBJECT_CLASS (class);
Packit ae235b
Packit ae235b
  object_class->set_property = g_simple_action_set_property;
Packit ae235b
  object_class->get_property = g_simple_action_get_property;
Packit ae235b
  object_class->finalize = g_simple_action_finalize;
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GSimpleAction::activate:
Packit ae235b
   * @simple: the #GSimpleAction
Packit ae235b
   * @parameter: (nullable): the parameter to the activation
Packit ae235b
   *
Packit ae235b
   * Indicates that the action was just activated.
Packit ae235b
   *
Packit ae235b
   * @parameter will always be of the expected type.  In the event that
Packit ae235b
   * an incorrect type was given, no signal will be emitted.
Packit ae235b
   *
Packit ae235b
   * Since GLib 2.40, if no handler is connected to this signal then the
Packit ae235b
   * default behaviour for boolean-stated actions with a %NULL parameter
Packit ae235b
   * type is to toggle them via the #GSimpleAction::change-state signal.
Packit ae235b
   * For stateful actions where the state type is equal to the parameter
Packit ae235b
   * type, the default is to forward them directly to
Packit ae235b
   * #GSimpleAction::change-state.  This should allow almost all users
Packit ae235b
   * of #GSimpleAction to connect only one handler or the other.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_simple_action_signals[SIGNAL_ACTIVATE] =
Packit ae235b
    g_signal_new (I_("activate"),
Packit ae235b
                  G_TYPE_SIMPLE_ACTION,
Packit ae235b
                  G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
Packit ae235b
                  0, NULL, NULL,
Packit ae235b
                  g_cclosure_marshal_VOID__VARIANT,
Packit ae235b
                  G_TYPE_NONE, 1,
Packit ae235b
                  G_TYPE_VARIANT);
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GSimpleAction::change-state:
Packit ae235b
   * @simple: the #GSimpleAction
Packit ae235b
   * @value: (nullable): the requested value for the state
Packit ae235b
   *
Packit ae235b
   * Indicates that the action just received a request to change its
Packit ae235b
   * state.
Packit ae235b
   *
Packit ae235b
   * @value will always be of the correct state type.  In the event that
Packit ae235b
   * an incorrect type was given, no signal will be emitted.
Packit ae235b
   *
Packit ae235b
   * If no handler is connected to this signal then the default
Packit ae235b
   * behaviour is to call g_simple_action_set_state() to set the state
Packit ae235b
   * to the requested value. If you connect a signal handler then no
Packit ae235b
   * default action is taken. If the state should change then you must
Packit ae235b
   * call g_simple_action_set_state() from the handler.
Packit ae235b
   *
Packit ae235b
   * An example of a 'change-state' handler:
Packit ae235b
   * |[
Packit ae235b
   * static void
Packit ae235b
   * change_volume_state (GSimpleAction *action,
Packit ae235b
   *                      GVariant      *value,
Packit ae235b
   *                      gpointer       user_data)
Packit ae235b
   * {
Packit ae235b
   *   gint requested;
Packit ae235b
   *
Packit ae235b
   *   requested = g_variant_get_int32 (value);
Packit ae235b
   *
Packit ae235b
   *   // Volume only goes from 0 to 10
Packit ae235b
   *   if (0 <= requested && requested <= 10)
Packit ae235b
   *     g_simple_action_set_state (action, value);
Packit ae235b
   * }
Packit ae235b
   * ]|
Packit ae235b
   *
Packit ae235b
   * The handler need not set the state to the requested value.
Packit ae235b
   * It could set it to any value at all, or take some other action.
Packit ae235b
   *
Packit ae235b
   * Since: 2.30
Packit ae235b
   */
Packit ae235b
  g_simple_action_signals[SIGNAL_CHANGE_STATE] =
Packit ae235b
    g_signal_new (I_("change-state"),
Packit ae235b
                  G_TYPE_SIMPLE_ACTION,
Packit ae235b
                  G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
Packit ae235b
                  0, NULL, NULL,
Packit ae235b
                  g_cclosure_marshal_VOID__VARIANT,
Packit ae235b
                  G_TYPE_NONE, 1,
Packit ae235b
                  G_TYPE_VARIANT);
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GSimpleAction:name:
Packit ae235b
   *
Packit ae235b
   * The name of the action. This is mostly meaningful for identifying
Packit ae235b
   * the action once it has been added to a #GSimpleActionGroup.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   **/
Packit ae235b
  g_object_class_install_property (object_class, PROP_NAME,
Packit ae235b
                                   g_param_spec_string ("name",
Packit ae235b
                                                        P_("Action Name"),
Packit ae235b
                                                        P_("The name used to invoke the action"),
Packit ae235b
                                                        NULL,
Packit ae235b
                                                        G_PARAM_READWRITE |
Packit ae235b
                                                        G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
                                                        G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GSimpleAction:parameter-type:
Packit ae235b
   *
Packit ae235b
   * The type of the parameter that must be given when activating the
Packit ae235b
   * action.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   **/
Packit ae235b
  g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
Packit ae235b
                                   g_param_spec_boxed ("parameter-type",
Packit ae235b
                                                       P_("Parameter Type"),
Packit ae235b
                                                       P_("The type of GVariant passed to activate()"),
Packit ae235b
                                                       G_TYPE_VARIANT_TYPE,
Packit ae235b
                                                       G_PARAM_READWRITE |
Packit ae235b
                                                       G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
                                                       G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GSimpleAction:enabled:
Packit ae235b
   *
Packit ae235b
   * If @action is currently enabled.
Packit ae235b
   *
Packit ae235b
   * If the action is disabled then calls to g_action_activate() and
Packit ae235b
   * g_action_change_state() have no effect.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   **/
Packit ae235b
  g_object_class_install_property (object_class, PROP_ENABLED,
Packit ae235b
                                   g_param_spec_boolean ("enabled",
Packit ae235b
                                                         P_("Enabled"),
Packit ae235b
                                                         P_("If the action can be activated"),
Packit ae235b
                                                         TRUE,
Packit ae235b
                                                         G_PARAM_READWRITE |
Packit ae235b
                                                         G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GSimpleAction:state-type:
Packit ae235b
   *
Packit ae235b
   * The #GVariantType of the state that the action has, or %NULL if the
Packit ae235b
   * action is stateless.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   **/
Packit ae235b
  g_object_class_install_property (object_class, PROP_STATE_TYPE,
Packit ae235b
                                   g_param_spec_boxed ("state-type",
Packit ae235b
                                                       P_("State Type"),
Packit ae235b
                                                       P_("The type of the state kept by the action"),
Packit ae235b
                                                       G_TYPE_VARIANT_TYPE,
Packit ae235b
                                                       G_PARAM_READABLE |
Packit ae235b
                                                       G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GSimpleAction:state:
Packit ae235b
   *
Packit ae235b
   * The state of the action, or %NULL if the action is stateless.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   **/
Packit ae235b
  g_object_class_install_property (object_class, PROP_STATE,
Packit ae235b
                                   g_param_spec_variant ("state",
Packit ae235b
                                                         P_("State"),
Packit ae235b
                                                         P_("The state the action is in"),
Packit ae235b
                                                         G_VARIANT_TYPE_ANY,
Packit ae235b
                                                         NULL,
Packit ae235b
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
Packit ae235b
                                                         G_PARAM_STATIC_STRINGS));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_action_set_enabled:
Packit ae235b
 * @simple: a #GSimpleAction
Packit ae235b
 * @enabled: whether the action is enabled
Packit ae235b
 *
Packit ae235b
 * Sets the action as enabled or not.
Packit ae235b
 *
Packit ae235b
 * An action must be enabled in order to be activated or in order to
Packit ae235b
 * have its state changed from outside callers.
Packit ae235b
 *
Packit ae235b
 * This should only be called by the implementor of the action.  Users
Packit ae235b
 * of the action should not attempt to modify its enabled flag.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_action_set_enabled (GSimpleAction *simple,
Packit ae235b
                             gboolean       enabled)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
Packit ae235b
Packit ae235b
  enabled = !!enabled;
Packit ae235b
Packit ae235b
  if (simple->enabled != enabled)
Packit ae235b
    {
Packit ae235b
      simple->enabled = enabled;
Packit ae235b
      g_object_notify (G_OBJECT (simple), "enabled");
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_action_set_state_hint:
Packit ae235b
 * @simple: a #GSimpleAction
Packit ae235b
 * @state_hint: (nullable): a #GVariant representing the state hint
Packit ae235b
 *
Packit ae235b
 * Sets the state hint for the action.
Packit ae235b
 *
Packit ae235b
 * See g_action_get_state_hint() for more information about
Packit ae235b
 * action state hints.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_action_set_state_hint (GSimpleAction *simple,
Packit ae235b
                                GVariant      *state_hint)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
Packit ae235b
Packit ae235b
  if (simple->state_hint != NULL)
Packit ae235b
    {
Packit ae235b
      g_variant_unref (simple->state_hint);
Packit ae235b
      simple->state_hint = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (state_hint != NULL)
Packit ae235b
    simple->state_hint = g_variant_ref (state_hint);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_action_new:
Packit ae235b
 * @name: the name of the action
Packit ae235b
 * @parameter_type: (nullable): the type of parameter to the activate function
Packit ae235b
 *
Packit ae235b
 * Creates a new action.
Packit ae235b
 *
Packit ae235b
 * The created action is stateless.  See g_simple_action_new_stateful().
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GSimpleAction
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
GSimpleAction *
Packit ae235b
g_simple_action_new (const gchar        *name,
Packit ae235b
                     const GVariantType *parameter_type)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (name != NULL, NULL);
Packit ae235b
Packit ae235b
  return g_object_new (G_TYPE_SIMPLE_ACTION,
Packit ae235b
                       "name", name,
Packit ae235b
                       "parameter-type", parameter_type,
Packit ae235b
                       NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_action_new_stateful:
Packit ae235b
 * @name: the name of the action
Packit ae235b
 * @parameter_type: (nullable): the type of the parameter to the activate function
Packit ae235b
 * @state: the initial state of the action
Packit ae235b
 *
Packit ae235b
 * Creates a new stateful action.
Packit ae235b
 *
Packit ae235b
 * @state is the initial state of the action.  All future state values
Packit ae235b
 * must have the same #GVariantType as the initial state.
Packit ae235b
 *
Packit ae235b
 * If the @state GVariant is floating, it is consumed.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GSimpleAction
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 **/
Packit ae235b
GSimpleAction *
Packit ae235b
g_simple_action_new_stateful (const gchar        *name,
Packit ae235b
                              const GVariantType *parameter_type,
Packit ae235b
                              GVariant           *state)
Packit ae235b
{
Packit ae235b
  return g_object_new (G_TYPE_SIMPLE_ACTION,
Packit ae235b
                       "name", name,
Packit ae235b
                       "parameter-type", parameter_type,
Packit ae235b
                       "state", state,
Packit ae235b
                       NULL);
Packit ae235b
}