* parameter type (which is given at construction time).
*
* An action may optionally have a state, in which case the state may be
- * set with g_action_set_state(). This call takes a #GVariant. The
+ * set with g_action_change_state(). This call takes a #GVariant. The
* correct type for the state is determined by a static state type
* (which is given at construction time).
*
* name of the action, the parameter type, the enabled state, the
* optional state type and the state and emitting the appropriate
* signals when these change. The implementor responsible for filtering
- * calls to g_action_activate() and g_action_set_state() for type safety
- * and for the state being enabled.
+ * calls to g_action_activate() and g_action_change_state() for type
+ * safety and for the state being enabled.
*
* Probably the only useful thing to do with a #GAction is to put it
* inside of a #GSimpleActionGroup.
* If @action is currently enabled.
*
* If the action is disabled then calls to g_action_activate() and
- * g_action_set_state() have no effect.
+ * g_action_change_state() have no effect.
*
* Since: 2.28
**/
P_("The state the action is in"),
G_VARIANT_TYPE_ANY,
NULL,
- G_PARAM_CONSTRUCT |
- G_PARAM_READWRITE |
+ G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
}
/**
- * g_action_set_state:
+ * g_action_change_state:
* @action: a #GAction
* @value: the new state
*
*
* If the @value GVariant is floating, it is consumed.
*
- * Since: 2.28
+ * Since: 2.30
**/
void
-g_action_set_state (GAction *action,
- GVariant *value)
+g_action_change_state (GAction *action,
+ GVariant *value)
{
const GVariantType *state_type;
g_variant_ref_sink (value);
G_ACTION_GET_IFACE (action)
- ->set_state (action, value);
+ ->change_state (action, value);
g_variant_unref (value);
}
* If the action is stateful (e.g. created with
* g_simple_action_new_stateful()) then this function returns the
* #GVariantType of the state. This is the type of the initial value
- * given as the state. All calls to g_action_set_state() must give a
+ * given as the state. All calls to g_action_change_state() must give a
* #GVariant of this type and g_action_get_state() will return a
* #GVariant of the same type.
*
* If the action is not stateful (e.g. created with g_simple_action_new())
* then this function will return %NULL. In that case, g_action_get_state()
- * will return %NULL and you must not call g_action_set_state().
+ * will return %NULL and you must not call g_action_change_state().
*
* Returns: (allow-none): the state type, if the action is stateful
*
#include "gaction.h"
#include "glibintl.h"
-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))
-
/**
* SECTION:gsimpleaction
* @title: GSimpleAction
*
* See also #GtkAction.
**/
-
-struct _GSimpleActionPrivate
+struct _GSimpleAction
{
+ GObject parent_instance;
+
gchar *name;
GVariantType *parameter_type;
- guint enabled : 1;
- guint state_set : 1;
+ gboolean enabled;
GVariant *state;
};
+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))
+
enum
{
PROP_NONE,
{
GSimpleAction *simple = G_SIMPLE_ACTION (action);
- return simple->priv->name;
+ return simple->name;
}
const GVariantType *
{
GSimpleAction *simple = G_SIMPLE_ACTION (action);
- return simple->priv->parameter_type;
+ return simple->parameter_type;
}
static const GVariantType *
{
GSimpleAction *simple = G_SIMPLE_ACTION (action);
- if (simple->priv->state != NULL)
- return g_variant_get_type (simple->priv->state);
+ if (simple->state != NULL)
+ return g_variant_get_type (simple->state);
else
return NULL;
}
{
GSimpleAction *simple = G_SIMPLE_ACTION (action);
- return simple->priv->enabled;
+ return simple->enabled;
}
static void
-g_simple_action_set_state (GAction *action,
- GVariant *value)
+g_simple_action_change_state (GAction *action,
+ GVariant *value)
{
GSimpleAction *simple = G_SIMPLE_ACTION (action);
+ g_simple_action_set_state (simple, value);
+}
+
+/**
+ * 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));
g_return_if_fail (value != NULL);
{
const GVariantType *state_type;
- state_type = simple->priv->state ?
- g_variant_get_type (simple->priv->state) : NULL;
+ state_type = simple->state ?
+ g_variant_get_type (simple->state) : NULL;
g_return_if_fail (state_type != NULL);
g_return_if_fail (g_variant_is_of_type (value, state_type));
}
g_variant_ref_sink (value);
- if (!g_variant_equal (simple->priv->state, value))
+ if (!simple->state || !g_variant_equal (simple->state, value))
{
- if (simple->priv->state)
- g_variant_unref (simple->priv->state);
+ if (simple->state)
+ g_variant_unref (simple->state);
- simple->priv->state = g_variant_ref (value);
+ simple->state = g_variant_ref (value);
g_object_notify (G_OBJECT (simple), "state");
}
{
GSimpleAction *simple = G_SIMPLE_ACTION (action);
- return simple->priv->state ? g_variant_ref (simple->priv->state) : NULL;
+ return simple->state ? g_variant_ref (simple->state) : NULL;
}
static void
{
GSimpleAction *simple = G_SIMPLE_ACTION (action);
- g_return_if_fail (simple->priv->parameter_type == NULL ?
+ g_return_if_fail (simple->parameter_type == NULL ?
parameter == NULL :
(parameter != NULL &&
g_variant_is_of_type (parameter,
- simple->priv->parameter_type)));
+ simple->parameter_type)));
if (parameter != NULL)
g_variant_ref_sink (parameter);
- if (simple->priv->enabled)
+ if (simple->enabled)
g_signal_emit (simple, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);
if (parameter != NULL)
switch (prop_id)
{
case PROP_NAME:
- g_assert (simple->priv->name == NULL);
- simple->priv->name = g_value_dup_string (value);
+ g_assert (simple->name == NULL);
+ simple->name = g_value_dup_string (value);
break;
case PROP_PARAMETER_TYPE:
- g_assert (simple->priv->parameter_type == NULL);
- simple->priv->parameter_type = g_value_dup_boxed (value);
+ g_assert (simple->parameter_type == NULL);
+ simple->parameter_type = g_value_dup_boxed (value);
break;
case PROP_ENABLED:
g_simple_action_set_enabled (simple, g_value_get_boolean (value));
break;
- case PROP_STATE:
- /* PROP_STATE is marked as G_PARAM_CONSTRUCT so we always get a
- * call during object construction, even if it is NULL. We treat
- * that first call differently, for a number of reasons.
- *
- * First, we don't want the value to be rejected by the
- * possibly-overridden .set_state() function. Second, we don't
- * want to be tripped by the assertions in g_simple_action_set_state()
- * that would enforce the catch22 that we only provide a value of
- * the same type as the existing value (when there is not yet an
- * existing value).
- */
- if (simple->priv->state_set)
- g_simple_action_set_state (G_ACTION (simple),
- g_value_get_variant (value));
-
- else /* this is the special case */
- {
- /* only do it the first time. */
- simple->priv->state_set = TRUE;
-
- /* blindly set it. */
- simple->priv->state = g_value_dup_variant (value);
- }
- break;
-
default:
g_assert_not_reached ();
}
{
GSimpleAction *simple = G_SIMPLE_ACTION (object);
- g_free (simple->priv->name);
- if (simple->priv->parameter_type)
- g_variant_type_free (simple->priv->parameter_type);
- if (simple->priv->state)
- g_variant_unref (simple->priv->state);
+ g_free (simple->name);
+ if (simple->parameter_type)
+ g_variant_type_free (simple->parameter_type);
+ if (simple->state)
+ g_variant_unref (simple->state);
G_OBJECT_CLASS (g_simple_action_parent_class)
->finalize (object);
void
g_simple_action_init (GSimpleAction *simple)
{
- simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (simple,
- G_TYPE_SIMPLE_ACTION,
- GSimpleActionPrivate);
}
void
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;
- iface->set_state = g_simple_action_set_state;
+ iface->change_state = g_simple_action_change_state;
iface->activate = g_simple_action_activate;
}
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
-
object_class->get_property = g_simple_action_get_property;
object_class->set_property = g_simple_action_set_property;
object_class->finalize = g_simple_action_finalize;
g_signal_new (I_("activate"),
G_TYPE_SIMPLE_ACTION,
G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
- G_STRUCT_OFFSET (GSimpleActionClass, activate),
- NULL, NULL,
+ 0, NULL, NULL,
g_cclosure_marshal_VOID__VARIANT,
G_TYPE_NONE, 1,
G_TYPE_VARIANT);
* If @action is currently enabled.
*
* If the action is disabled then calls to g_simple_action_activate() and
- * g_simple_action_set_state() have no effect.
+ * g_simple_action_change_state() have no effect.
*
* Since: 2.28
**/
P_("The state the action is in"),
G_VARIANT_TYPE_ANY,
NULL,
- G_PARAM_CONSTRUCT |
- G_PARAM_READWRITE |
+ G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
-
- g_type_class_add_private (class, sizeof (GSimpleActionPrivate));
}
/**
* An action must be enabled in order to be activated or in order to
* have its state changed from outside callers.
*
+ * This should only be called by the implementor of the action. Users
+ * of the action should not attempt to modify its enabled flag.
+ *
* Since: 2.28
**/
void
enabled = !!enabled;
- if (simple->priv->enabled != enabled)
+ if (simple->enabled != enabled)
{
- simple->priv->enabled = enabled;
+ simple->enabled = enabled;
g_object_notify (G_OBJECT (simple), "enabled");
}
}
const GVariantType *parameter_type,
GVariant *state)
{
- return g_object_new (G_TYPE_SIMPLE_ACTION,
- "name", name,
- "parameter-type", parameter_type,
- "state", state,
- NULL);
+ GSimpleAction *simple;
+
+ simple = g_object_new (G_TYPE_SIMPLE_ACTION,
+ "name", name,
+ "parameter-type", parameter_type,
+ NULL);
+
+ simple->state = g_variant_ref_sink (state);
+
+ return simple;
}