gtkactionhelper.c 18.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright © 2012 Canonical Limited
 *
 * 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 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, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Ryan Lortie <desrt@desrt.ca>
 */

#include "gtkactionhelper.h"
21
#include "gtkactionobservable.h"
22 23 24

#include "gtkwidget.h"
#include "gtkwidgetprivate.h"
25
#include "gtkdebug.h"
26 27
#include "gtkmodelbutton.h"
#include "gtktypebuiltins.h"
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

#include <string.h>

typedef struct
{
  GActionGroup *group;

  GHashTable *watchers;
} GtkActionHelperGroup;

static void             gtk_action_helper_action_added                  (GtkActionHelper    *helper,
                                                                         gboolean            enabled,
                                                                         const GVariantType *parameter_type,
                                                                         GVariant           *state,
                                                                         gboolean            should_emit_signals);

static void             gtk_action_helper_action_removed                (GtkActionHelper    *helper);

static void             gtk_action_helper_action_enabled_changed        (GtkActionHelper    *helper,
                                                                         gboolean            enabled);

static void             gtk_action_helper_action_state_changed          (GtkActionHelper    *helper,
                                                                         GVariant           *new_state);

typedef GObjectClass GtkActionHelperClass;

struct _GtkActionHelper
{
  GObject parent_instance;

  GtkWidget *widget;

  GtkActionHelperGroup *group;

62
  GtkActionMuxer *action_context;
63 64 65 66 67 68 69 70
  gchar *action_name;

  GVariant *target;

  gboolean can_activate;
  gboolean enabled;
  gboolean active;

71 72
  GtkButtonRole role;

73 74 75 76 77 78 79 80
  gint reporting;
};

enum
{
  PROP_0,
  PROP_ENABLED,
  PROP_ACTIVE,
81
  PROP_ROLE,
82 83 84 85 86
  N_PROPS
};

static GParamSpec *gtk_action_helper_pspecs[N_PROPS];

87
static void gtk_action_helper_observer_iface_init (GtkActionObserverInterface *iface);
88 89

G_DEFINE_TYPE_WITH_CODE (GtkActionHelper, gtk_action_helper, G_TYPE_OBJECT,
90
  G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTION_OBSERVER, gtk_action_helper_observer_iface_init))
91 92 93 94 95 96 97

static void
gtk_action_helper_report_change (GtkActionHelper *helper,
                                 guint            prop_id)
{
  helper->reporting++;

98
  switch (prop_id)
99
    {
100 101 102
    case PROP_ENABLED:
      gtk_widget_set_sensitive (GTK_WIDGET (helper->widget), helper->enabled);
      break;
103

104 105 106
    case PROP_ACTIVE:
      {
        GParamSpec *pspec;
107

108
        pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (helper->widget), "active");
109

110 111 112 113
        if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_BOOLEAN)
          g_object_set (G_OBJECT (helper->widget), "active", helper->active, NULL);
      }
      break;
114

115 116 117 118 119 120 121 122 123 124 125
    case PROP_ROLE:
      {
        GParamSpec *pspec;

        pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (helper->widget), "role");

        if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == GTK_TYPE_BUTTON_ROLE)
          g_object_set (G_OBJECT (helper->widget), "role", helper->role, NULL);
      }
      break;

126 127
    default:
      g_assert_not_reached ();
128 129 130 131 132 133 134 135 136 137 138 139 140
    }

  g_object_notify_by_pspec (G_OBJECT (helper), gtk_action_helper_pspecs[prop_id]);
  helper->reporting--;
}

static void
gtk_action_helper_action_added (GtkActionHelper    *helper,
                                gboolean            enabled,
                                const GVariantType *parameter_type,
                                GVariant           *state,
                                gboolean            should_emit_signals)
{
141 142
  GTK_NOTE(ACTIONS, g_message("actionhelper: %s added", helper->action_name));

143 144 145 146 147 148
  /* we can only activate if we have the correct type of parameter */
  helper->can_activate = (helper->target == NULL && parameter_type == NULL) ||
                          (helper->target != NULL && parameter_type != NULL &&
                          g_variant_is_of_type (helper->target, parameter_type));

  if (!helper->can_activate)
149 150 151 152 153 154 155
    {
      GTK_NOTE(ACTIONS, g_message("actionhelper: %s found, but disabled due to parameter type mismatch",
                                  helper->action_name));
      return;
    }

  GTK_NOTE(ACTIONS, g_message ("actionhelper: %s can be activated", helper->action_name));
156 157 158

  helper->enabled = enabled;

159 160 161 162 163
  if (!enabled)
    GTK_NOTE(ACTIONS, g_message("actionhelper: %s found, but disabled due to disabled action", helper->action_name));
  else
    GTK_NOTE(ACTIONS, g_message("actionhelper: %s found and enabled", helper->action_name));

164
  if (helper->target != NULL && state != NULL)
165 166 167 168
    {
      helper->active = g_variant_equal (state, helper->target);
      helper->role = GTK_BUTTON_ROLE_RADIO;
    }
169
  else if (state != NULL && g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN))
170 171 172 173 174 175 176 177
    {
      helper->active = g_variant_get_boolean (state);
      helper->role = GTK_BUTTON_ROLE_CHECK;
    }
  else
    {
      helper->role = GTK_BUTTON_ROLE_NORMAL;
    }
178 179 180 181 182 183 184 185

  if (should_emit_signals)
    {
      if (helper->enabled)
        gtk_action_helper_report_change (helper, PROP_ENABLED);

      if (helper->active)
        gtk_action_helper_report_change (helper, PROP_ACTIVE);
186 187

      gtk_action_helper_report_change (helper, PROP_ROLE);
188 189 190 191 192 193
    }
}

static void
gtk_action_helper_action_removed (GtkActionHelper *helper)
{
194 195
  GTK_NOTE(ACTIONS, g_message ("actionhelper: %s was removed", helper->action_name));

196 197 198 199 200 201 202 203 204 205 206 207 208
  if (!helper->can_activate)
    return;

  helper->can_activate = FALSE;

  if (helper->enabled)
    {
      helper->enabled = FALSE;
      gtk_action_helper_report_change (helper, PROP_ENABLED);
    }

  if (helper->active)
    {
209
      helper->active = FALSE;
210 211 212 213 214 215 216 217
      gtk_action_helper_report_change (helper, PROP_ACTIVE);
    }
}

static void
gtk_action_helper_action_enabled_changed (GtkActionHelper *helper,
                                          gboolean         enabled)
{
218 219
  GTK_NOTE(ACTIONS, g_message ("actionhelper: %s enabled changed: %d", helper->action_name, enabled));

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  if (!helper->can_activate)
    return;

  if (helper->enabled == enabled)
    return;

  helper->enabled = enabled;
  gtk_action_helper_report_change (helper, PROP_ENABLED);
}

static void
gtk_action_helper_action_state_changed (GtkActionHelper *helper,
                                        GVariant        *new_state)
{
  gboolean was_active;

236 237
  GTK_NOTE(ACTIONS, g_message ("actionhelper: %s state changed", helper->action_name));

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
  if (!helper->can_activate)
    return;

  was_active = helper->active;

  if (helper->target)
    helper->active = g_variant_equal (new_state, helper->target);

  else if (g_variant_is_of_type (new_state, G_VARIANT_TYPE_BOOLEAN))
    helper->active = g_variant_get_boolean (new_state);

  else
    helper->active = FALSE;

  if (helper->active != was_active)
    gtk_action_helper_report_change (helper, PROP_ACTIVE);
}

static void
gtk_action_helper_get_property (GObject *object, guint prop_id,
                                GValue *value, GParamSpec *pspec)
{
  GtkActionHelper *helper = GTK_ACTION_HELPER (object);

  switch (prop_id)
    {
    case PROP_ENABLED:
      g_value_set_boolean (value, helper->enabled);
      break;

    case PROP_ACTIVE:
      g_value_set_boolean (value, helper->active);
      break;

272 273 274 275
    case PROP_ROLE:
      g_value_set_enum (value, helper->role);
      break;

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    default:
      g_assert_not_reached ();
    }
}

static void
gtk_action_helper_finalize (GObject *object)
{
  GtkActionHelper *helper = GTK_ACTION_HELPER (object);

  g_free (helper->action_name);

  if (helper->target)
    g_variant_unref (helper->target);

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

static void
296 297 298 299 300 301
gtk_action_helper_observer_action_added (GtkActionObserver   *observer,
                                         GtkActionObservable *observable,
                                         const gchar         *action_name,
                                         const GVariantType  *parameter_type,
                                         gboolean             enabled,
                                         GVariant            *state)
302 303 304 305 306
{
  gtk_action_helper_action_added (GTK_ACTION_HELPER (observer), enabled, parameter_type, state, TRUE);
}

static void
307 308 309 310
gtk_action_helper_observer_action_enabled_changed (GtkActionObserver   *observer,
                                                   GtkActionObservable *observable,
                                                   const gchar         *action_name,
                                                   gboolean             enabled)
311 312 313 314 315
{
  gtk_action_helper_action_enabled_changed (GTK_ACTION_HELPER (observer), enabled);
}

static void
316 317 318 319
gtk_action_helper_observer_action_state_changed (GtkActionObserver   *observer,
                                                 GtkActionObservable *observable,
                                                 const gchar         *action_name,
                                                 GVariant            *state)
320 321 322 323 324
{
  gtk_action_helper_action_state_changed (GTK_ACTION_HELPER (observer), state);
}

static void
325 326 327
gtk_action_helper_observer_action_removed (GtkActionObserver   *observer,
                                           GtkActionObservable *observable,
                                           const gchar         *action_name)
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
{
  gtk_action_helper_action_removed (GTK_ACTION_HELPER (observer));
}

static void
gtk_action_helper_init (GtkActionHelper *helper)
{
}

static void
gtk_action_helper_class_init (GtkActionHelperClass *class)
{
  class->get_property = gtk_action_helper_get_property;
  class->finalize = gtk_action_helper_finalize;

  gtk_action_helper_pspecs[PROP_ENABLED] = g_param_spec_boolean ("enabled", "enabled", "enabled", FALSE,
                                                                 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  gtk_action_helper_pspecs[PROP_ACTIVE] = g_param_spec_boolean ("active", "active", "active", FALSE,
                                                                G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
347 348 349 350
  gtk_action_helper_pspecs[PROP_ROLE] = g_param_spec_enum ("role", "role", "role",
                                                           GTK_TYPE_BUTTON_ROLE,
                                                           GTK_BUTTON_ROLE_NORMAL,
                                                           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
351 352 353 354
  g_object_class_install_properties (class, N_PROPS, gtk_action_helper_pspecs);
}

static void
355
gtk_action_helper_observer_iface_init (GtkActionObserverInterface *iface)
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
{
  iface->action_added = gtk_action_helper_observer_action_added;
  iface->action_enabled_changed = gtk_action_helper_observer_action_enabled_changed;
  iface->action_state_changed = gtk_action_helper_observer_action_state_changed;
  iface->action_removed = gtk_action_helper_observer_action_removed;
}

/*< private >
 * gtk_action_helper_new:
 * @widget: a #GtkWidget implementing #GtkActionable
 *
 * Creates a helper to track the state of a named action.  This will
 * usually be used by widgets implementing #GtkActionable.
 *
 * This helper class is usually used by @widget itself.  In order to
 * avoid reference cycles, the helper does not hold a reference on
 * @widget, but will assume that it continues to exist for the duration
 * of the life of the helper.  If you are using the helper from outside
 * of the widget, you should take a ref on @widget for each ref you hold
 * on the helper.
 *
 * Returns: a new #GtkActionHelper
 */
GtkActionHelper *
gtk_action_helper_new (GtkActionable *widget)
{
  GtkActionHelper *helper;

  g_return_val_if_fail (GTK_IS_ACTIONABLE (widget), NULL);
  helper = g_object_new (GTK_TYPE_ACTION_HELPER, NULL);

  helper->widget = GTK_WIDGET (widget);

  if (helper->widget)
    {
      GParamSpec *pspec;

      helper->enabled = gtk_widget_get_sensitive (GTK_WIDGET (helper->widget));

      pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (helper->widget), "active");
      if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_BOOLEAN)
        g_object_get (G_OBJECT (helper->widget), "active", &helper->active, NULL);
    }

400
  helper->action_context = _gtk_widget_get_action_muxer (GTK_WIDGET (widget), TRUE);
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

  return helper;
}

void
gtk_action_helper_set_action_name (GtkActionHelper *helper,
                                   const gchar     *action_name)
{
  gboolean was_enabled, was_active;
  const GVariantType *parameter_type;
  gboolean enabled;
  GVariant *state;

  if (g_strcmp0 (action_name, helper->action_name) == 0)
    return;

417 418 419 420 421
  GTK_NOTE(ACTIONS,
           if (!strchr (action_name, '.'))
             g_message ("actionhelper: action name %s doesn't look like 'app.' or 'win.' "
                        "which means that it will probably not work properly.", action_name));

422 423
  if (helper->action_name)
    {
424 425 426
      gtk_action_observable_unregister_observer (GTK_ACTION_OBSERVABLE (helper->action_context),
                                                 helper->action_name,
                                                 GTK_ACTION_OBSERVER (helper));
427 428 429 430 431
      g_free (helper->action_name);
    }

  helper->action_name = g_strdup (action_name);

432 433 434
  gtk_action_observable_register_observer (GTK_ACTION_OBSERVABLE (helper->action_context),
                                           helper->action_name,
                                           GTK_ACTION_OBSERVER (helper));
435 436 437 438 439 440 441 442 443 444

  /* Start by recording the current state of our properties so we know
   * what notify signals we will need to send.
   */
  was_enabled = helper->enabled;
  was_active = helper->active;

  if (g_action_group_query_action (G_ACTION_GROUP (helper->action_context), helper->action_name,
                                   &enabled, &parameter_type, NULL, NULL, &state))
    {
445 446
      GTK_NOTE(ACTIONS, g_message ("actionhelper: %s existed from the start", helper->action_name));

447 448 449 450 451 452
      gtk_action_helper_action_added (helper, enabled, parameter_type, state, FALSE);

      if (state)
        g_variant_unref (state);
    }
  else
453 454 455 456
    {
      GTK_NOTE(ACTIONS, g_message ("actionhelper: %s missing from the start", helper->action_name));
      helper->enabled = FALSE;
    }
457 458 459 460 461 462 463 464 465 466 467 468

  /* Send the notifies for the properties that changed.
   *
   * When called during construction, widget is NULL.  We don't need to
   * report in that case.
   */
  if (helper->enabled != was_enabled)
    gtk_action_helper_report_change (helper, PROP_ENABLED);

  if (helper->active != was_active)
    gtk_action_helper_report_change (helper, PROP_ACTIVE);

469
  g_object_notify (G_OBJECT (helper->widget), "action-name");
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
}

/*< private >
 * gtk_action_helper_set_action_target_value:
 * @helper: a #GtkActionHelper
 * @target_value: an action target, as per #GtkActionable
 *
 * This function consumes @action_target if it is floating.
 */
void
gtk_action_helper_set_action_target_value (GtkActionHelper *helper,
                                           GVariant        *target_value)
{
  gboolean was_enabled;
  gboolean was_active;

  if (target_value == helper->target)
    return;

  if (target_value && helper->target && g_variant_equal (target_value, helper->target))
490 491 492 493
    {
      g_variant_unref (g_variant_ref_sink (target_value));
      return;
    }
494 495 496 497 498 499 500 501 502 503

  if (helper->target)
    {
      g_variant_unref (helper->target);
      helper->target = NULL;
    }

  if (target_value)
    helper->target = g_variant_ref_sink (target_value);

504 505 506 507
  /* The action_name has not yet been set.  Don't do anything yet. */
  if (helper->action_name == NULL)
    return;

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
  was_enabled = helper->enabled;
  was_active = helper->active;

  /* If we are attached to an action group then it is possible that this
   * change of the target value could impact our properties (including
   * changes to 'can_activate' and therefore 'enabled', due to resolving
   * a parameter type mismatch).
   *
   * Start over again by pretending the action gets re-added.
   */
  helper->can_activate = FALSE;
  helper->enabled = FALSE;
  helper->active = FALSE;

  if (helper->action_context)
    {
      const GVariantType *parameter_type;
      gboolean enabled;
      GVariant *state;

      if (g_action_group_query_action (G_ACTION_GROUP (helper->action_context),
                                       helper->action_name, &enabled, &parameter_type,
                                       NULL, NULL, &state))
        {
          gtk_action_helper_action_added (helper, enabled, parameter_type, state, FALSE);

          if (state)
            g_variant_unref (state);
        }
    }

  if (helper->enabled != was_enabled)
    gtk_action_helper_report_change (helper, PROP_ENABLED);

  if (helper->active != was_active)
    gtk_action_helper_report_change (helper, PROP_ACTIVE);

545
  g_object_notify (G_OBJECT (helper->widget), "action-target");
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
}

const gchar *
gtk_action_helper_get_action_name (GtkActionHelper *helper)
{
  if (helper == NULL)
    return NULL;

  return helper->action_name;
}

GVariant *
gtk_action_helper_get_action_target_value (GtkActionHelper *helper)
{
  if (helper == NULL)
    return NULL;

  return helper->target;
}

gboolean
gtk_action_helper_get_enabled (GtkActionHelper *helper)
{
  g_return_val_if_fail (GTK_IS_ACTION_HELPER (helper), FALSE);

  return helper->enabled;
}

gboolean
gtk_action_helper_get_active (GtkActionHelper *helper)
{
  g_return_val_if_fail (GTK_IS_ACTION_HELPER (helper), FALSE);

  return helper->active;
}

void
gtk_action_helper_activate (GtkActionHelper *helper)
{
  g_return_if_fail (GTK_IS_ACTION_HELPER (helper));

  if (!helper->can_activate || helper->reporting)
    return;

  g_action_group_activate_action (G_ACTION_GROUP (helper->action_context),
                                  helper->action_name, helper->target);
}