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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
|
/*
* Copyright © 2013 Canonical Limited
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library 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.1 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
* 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, see <http://www.gnu.org/licenses/>.
*
* Authors: Ryan Lortie <desrt@desrt.ca>
*/
#include "config.h"
#include "gpropertyaction.h"
#include "gsettings-mapping.h"
#include "gaction.h"
#include "glibintl.h"
/**
* SECTION:gpropertyaction
* @title: GPropertyAction
* @short_description: A GAction reflecting a GObject property
* @include: gio/gio.h
*
* A #GPropertyAction is a way to get a #GAction with a state value
* reflecting and controlling the value of a #GObject property.
*
* The state of the action will correspond to the value of the property.
* Changing it will change the property (assuming the requested value
* matches the requirements as specified in the #GParamSpec).
*
* Only the most common types are presently supported. Booleans are
* mapped to booleans, strings to strings, signed/unsigned integers to
* int32/uint32 and floats and doubles to doubles.
*
* If the property is an enum then the state will be string-typed and
* conversion will automatically be performed between the enum value and
* "nick" string as per the #GEnumValue table.
*
* Flags types are not currently supported.
*
* Properties of object types, boxed types and pointer types are not
* supported and probably never will be.
*
* Properties of #GVariant types are not currently supported.
*
* If the property is boolean-valued then the action will have a NULL
* parameter type, and activating the action (with no parameter) will
* toggle the value of the property.
*
* In all other cases, the parameter type will correspond to the type of
* the property.
*
* The general idea here is to reduce the number of locations where a
* particular piece of state is kept (and therefore has to be synchronised
* between). #GPropertyAction does not have a separate state that is kept
* in sync with the property value -- its state is the property value.
*
* For example, it might be useful to create a #GAction corresponding to
* the "visible-child-name" property of a #GtkStack so that the current
* page can be switched from a menu. The active radio indication in the
* menu is then directly determined from the active page of the
* #GtkStack.
*
* An anti-example would be binding the "active-id" property on a
* #GtkComboBox. This is because the state of the combobox itself is
* probably uninteresting and is actually being used to control
* something else.
*
* Another anti-example would be to bind to the "visible-child-name"
* property of a #GtkStack if this value is actually stored in
* #GSettings. In that case, the real source of the value is
* #GSettings. If you want a #GAction to control a setting stored in
* #GSettings, see g_settings_create_action() instead, and possibly
* combine its use with g_settings_bind().
*
* Since: 2.38
**/
struct _GPropertyAction
{
GObject parent_instance;
gchar *name;
gpointer object;
GParamSpec *pspec;
const GVariantType *state_type;
gboolean invert_boolean;
};
/**
* GPropertyAction:
*
* This type is opaque.
*
* Since: 2.38
**/
typedef GObjectClass GPropertyActionClass;
static void g_property_action_iface_init (GActionInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GPropertyAction, g_property_action, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_property_action_iface_init))
enum
{
PROP_NONE,
PROP_NAME,
PROP_PARAMETER_TYPE,
PROP_ENABLED,
PROP_STATE_TYPE,
PROP_STATE,
PROP_OBJECT,
PROP_PROPERTY_NAME,
PROP_INVERT_BOOLEAN
};
static gboolean
g_property_action_get_invert_boolean (GAction *action)
{
GPropertyAction *paction = G_PROPERTY_ACTION (action);
return paction->invert_boolean;
}
static const gchar *
g_property_action_get_name (GAction *action)
{
GPropertyAction *paction = G_PROPERTY_ACTION (action);
return paction->name;
}
static const GVariantType *
g_property_action_get_parameter_type (GAction *action)
{
GPropertyAction *paction = G_PROPERTY_ACTION (action);
return paction->pspec->value_type == G_TYPE_BOOLEAN ? NULL : paction->state_type;
}
static const GVariantType *
g_property_action_get_state_type (GAction *action)
{
GPropertyAction *paction = G_PROPERTY_ACTION (action);
return paction->state_type;
}
static GVariant *
g_property_action_get_state_hint (GAction *action)
{
GPropertyAction *paction = G_PROPERTY_ACTION (action);
if (paction->pspec->value_type == G_TYPE_INT)
{
GParamSpecInt *pspec = (GParamSpecInt *)paction->pspec;
return g_variant_new ("(ii)", pspec->minimum, pspec->maximum);
}
else if (paction->pspec->value_type == G_TYPE_UINT)
{
GParamSpecUInt *pspec = (GParamSpecUInt *)paction->pspec;
return g_variant_new ("(uu)", pspec->minimum, pspec->maximum);
}
else if (paction->pspec->value_type == G_TYPE_FLOAT)
{
GParamSpecFloat *pspec = (GParamSpecFloat *)paction->pspec;
return g_variant_new ("(dd)", (double)pspec->minimum, (double)pspec->maximum);
}
else if (paction->pspec->value_type == G_TYPE_DOUBLE)
{
GParamSpecDouble *pspec = (GParamSpecDouble *)paction->pspec;
return g_variant_new ("(dd)", pspec->minimum, pspec->maximum);
}
return NULL;
}
static gboolean
g_property_action_get_enabled (GAction *action)
{
return TRUE;
}
static void
g_property_action_set_state (GPropertyAction *paction,
GVariant *variant)
{
GValue value = G_VALUE_INIT;
g_value_init (&value, paction->pspec->value_type);
g_settings_get_mapping (&value, variant, NULL);
if (paction->pspec->value_type == G_TYPE_BOOLEAN && paction->invert_boolean)
g_value_set_boolean (&value, !g_value_get_boolean (&value));
g_object_set_property (paction->object, paction->pspec->name, &value);
g_value_unset (&value);
}
static void
g_property_action_change_state (GAction *action,
GVariant *value)
{
GPropertyAction *paction = G_PROPERTY_ACTION (action);
g_return_if_fail (g_variant_is_of_type (value, paction->state_type));
g_property_action_set_state (paction, value);
}
static GVariant *
g_property_action_get_state (GAction *action)
{
GPropertyAction *paction = G_PROPERTY_ACTION (action);
GValue value = G_VALUE_INIT;
GVariant *result;
g_value_init (&value, paction->pspec->value_type);
g_object_get_property (paction->object, paction->pspec->name, &value);
if (paction->pspec->value_type == G_TYPE_BOOLEAN && paction->invert_boolean)
g_value_set_boolean (&value, !g_value_get_boolean (&value));
result = g_settings_set_mapping (&value, paction->state_type, NULL);
g_value_unset (&value);
return g_variant_ref_sink (result);
}
static void
g_property_action_activate (GAction *action,
GVariant *parameter)
{
GPropertyAction *paction = G_PROPERTY_ACTION (action);
if (paction->pspec->value_type == G_TYPE_BOOLEAN)
{
gboolean value;
g_return_if_fail (paction->pspec->value_type == G_TYPE_BOOLEAN && parameter == NULL);
g_object_get (paction->object, paction->pspec->name, &value, NULL);
value = !value;
g_object_set (paction->object, paction->pspec->name, value, NULL);
}
else
{
g_return_if_fail (parameter != NULL && g_variant_is_of_type (parameter, paction->state_type));
g_property_action_set_state (paction, parameter);
}
}
static const GVariantType *
g_property_action_determine_type (GParamSpec *pspec)
{
if (G_TYPE_IS_ENUM (pspec->value_type))
return G_VARIANT_TYPE_STRING;
switch (pspec->value_type)
{
case G_TYPE_BOOLEAN:
return G_VARIANT_TYPE_BOOLEAN;
case G_TYPE_INT:
return G_VARIANT_TYPE_INT32;
case G_TYPE_UINT:
return G_VARIANT_TYPE_UINT32;
case G_TYPE_DOUBLE:
case G_TYPE_FLOAT:
return G_VARIANT_TYPE_DOUBLE;
case G_TYPE_STRING:
return G_VARIANT_TYPE_STRING;
default:
g_critical ("Unable to use GPropertyAction with property '%s::%s' of type '%s'",
g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->value_type));
return NULL;
}
}
static void
g_property_action_notify (GObject *object,
GParamSpec *pspec,
gpointer user_data)
{
GPropertyAction *paction = user_data;
g_assert (object == paction->object);
g_assert (pspec == paction->pspec);
g_object_notify (G_OBJECT (paction), "state");
}
static void
g_property_action_set_property_name (GPropertyAction *paction,
const gchar *property_name)
{
GParamSpec *pspec;
gchar *detailed;
/* In case somebody is constructing GPropertyAction without passing
* a property name
*/
if (G_UNLIKELY (property_name == NULL || property_name[0] == '\0'))
{
g_critical ("Attempted to use an empty property name for GPropertyAction");
return;
}
pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (paction->object), property_name);
if (pspec == NULL)
{
g_critical ("Attempted to use non-existent property '%s::%s' for GPropertyAction",
G_OBJECT_TYPE_NAME (paction->object), property_name);
return;
}
if (~pspec->flags & G_PARAM_READABLE || ~pspec->flags & G_PARAM_WRITABLE || pspec->flags & G_PARAM_CONSTRUCT_ONLY)
{
g_critical ("Property '%s::%s' used with GPropertyAction must be readable, writable, and not construct-only",
G_OBJECT_TYPE_NAME (paction->object), property_name);
return;
}
paction->pspec = pspec;
detailed = g_strconcat ("notify::", paction->pspec->name, NULL);
paction->state_type = g_property_action_determine_type (paction->pspec);
g_signal_connect (paction->object, detailed, G_CALLBACK (g_property_action_notify), paction);
g_free (detailed);
}
static void
g_property_action_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GPropertyAction *paction = G_PROPERTY_ACTION (object);
switch (prop_id)
{
case PROP_NAME:
paction->name = g_value_dup_string (value);
break;
case PROP_OBJECT:
paction->object = g_value_dup_object (value);
break;
case PROP_PROPERTY_NAME:
g_property_action_set_property_name (paction, g_value_get_string (value));
break;
case PROP_INVERT_BOOLEAN:
paction->invert_boolean = g_value_get_boolean (value);
break;
default:
g_assert_not_reached ();
}
}
static void
g_property_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_property_action_get_name (action));
break;
case PROP_PARAMETER_TYPE:
g_value_set_boxed (value, g_property_action_get_parameter_type (action));
break;
case PROP_ENABLED:
g_value_set_boolean (value, g_property_action_get_enabled (action));
break;
case PROP_STATE_TYPE:
g_value_set_boxed (value, g_property_action_get_state_type (action));
break;
case PROP_STATE:
g_value_take_variant (value, g_property_action_get_state (action));
break;
case PROP_INVERT_BOOLEAN:
g_value_set_boolean (value, g_property_action_get_invert_boolean (action));
break;
default:
g_assert_not_reached ();
}
}
static void
g_property_action_dispose (GObject *object)
{
GPropertyAction *paction = G_PROPERTY_ACTION (object);
if (paction->object != NULL)
{
g_signal_handlers_disconnect_by_func (paction->object, g_property_action_notify, paction);
g_clear_object (&paction->object);
}
G_OBJECT_CLASS (g_property_action_parent_class)->dispose (object);
}
static void
g_property_action_finalize (GObject *object)
{
GPropertyAction *paction = G_PROPERTY_ACTION (object);
g_free (paction->name);
G_OBJECT_CLASS (g_property_action_parent_class)
->finalize (object);
}
void
g_property_action_init (GPropertyAction *property)
{
}
void
g_property_action_iface_init (GActionInterface *iface)
{
iface->get_name = g_property_action_get_name;
iface->get_parameter_type = g_property_action_get_parameter_type;
iface->get_state_type = g_property_action_get_state_type;
iface->get_state_hint = g_property_action_get_state_hint;
iface->get_enabled = g_property_action_get_enabled;
iface->get_state = g_property_action_get_state;
iface->change_state = g_property_action_change_state;
iface->activate = g_property_action_activate;
}
void
g_property_action_class_init (GPropertyActionClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->set_property = g_property_action_set_property;
object_class->get_property = g_property_action_get_property;
object_class->dispose = g_property_action_dispose;
object_class->finalize = g_property_action_finalize;
/**
* GPropertyAction:name:
*
* The name of the action. This is mostly meaningful for identifying
* the action once it has been added to a #GActionMap.
*
* Since: 2.38
**/
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,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
/**
* GPropertyAction:parameter-type:
*
* The type of the parameter that must be given when activating the
* action.
*
* Since: 2.38
**/
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,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
/**
* GPropertyAction:enabled:
*
* If @action is currently enabled.
*
* If the action is disabled then calls to g_action_activate() and
* g_action_change_state() have no effect.
*
* Since: 2.38
**/
g_object_class_install_property (object_class, PROP_ENABLED,
g_param_spec_boolean ("enabled",
P_("Enabled"),
P_("If the action can be activated"),
TRUE,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
/**
* GPropertyAction:state-type:
*
* The #GVariantType of the state that the action has, or %NULL if the
* action is stateless.
*
* Since: 2.38
**/
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));
/**
* GPropertyAction:state:
*
* The state of the action, or %NULL if the action is stateless.
*
* Since: 2.38
**/
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,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
/**
* GPropertyAction:object:
*
* The object to wrap a property on.
*
* The object must be a non-%NULL #GObject with properties.
*
* Since: 2.38
**/
g_object_class_install_property (object_class, PROP_OBJECT,
g_param_spec_object ("object",
P_("Object"),
P_("The object with the property to wrap"),
G_TYPE_OBJECT,
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
/**
* GPropertyAction:property-name:
*
* The name of the property to wrap on the object.
*
* The property must exist on the passed-in object and it must be
* readable and writable (and not construct-only).
*
* Since: 2.38
**/
g_object_class_install_property (object_class, PROP_PROPERTY_NAME,
g_param_spec_string ("property-name",
P_("Property name"),
P_("The name of the property to wrap"),
NULL,
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
/**
* GPropertyAction:invert-boolean:
*
* If %TRUE, the state of the action will be the negation of the
* property value, provided the property is boolean.
*
* Since: 2.46
*/
g_object_class_install_property (object_class, PROP_INVERT_BOOLEAN,
g_param_spec_boolean ("invert-boolean",
P_("Invert boolean"),
P_("Whether to invert the value of a boolean property"),
FALSE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
}
/**
* g_property_action_new:
* @name: the name of the action to create
* @object: (type GObject.Object): the object that has the property
* to wrap
* @property_name: the name of the property
*
* Creates a #GAction corresponding to the value of property
* @property_name on @object.
*
* The property must be existent and readable and writable (and not
* construct-only).
*
* This function takes a reference on @object and doesn't release it
* until the action is destroyed.
*
* Returns: a new #GPropertyAction
*
* Since: 2.38
**/
GPropertyAction *
g_property_action_new (const gchar *name,
gpointer object,
const gchar *property_name)
{
g_return_val_if_fail (name != NULL, NULL);
g_return_val_if_fail (G_IS_OBJECT (object), NULL);
g_return_val_if_fail (property_name != NULL, NULL);
return g_object_new (G_TYPE_PROPERTY_ACTION,
"name", name,
"object", object,
"property-name", property_name,
NULL);
}
|