gtkmenutracker.c 23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright © 2013 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
15
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

#include "config.h"

#include "gtkmenutracker.h"

24
/*< private >
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 * SECTION:gtkmenutracker
 * @Title: GtkMenuTracker
 * @Short_description: A helper class for interpreting #GMenuModel
 *
 * #GtkMenuTracker is a simple object to ease implementations of #GMenuModel.
 * Given a #GtkActionObservable (usually a #GActionMuxer) along with a
 * #GMenuModel, it will tell you which menu items to create and where to place
 * them. If a menu item is removed, it will tell you the position of the menu
 * item to remove.
 *
 * Using #GtkMenuTracker is fairly simple. The only guarantee you must make
 * to #GtkMenuTracker is that you must obey all insert signals and track the
 * position of items that #GtkMenuTracker gives you. That is, #GtkMenuTracker
 * expects positions of all the latter items to change when it calls your
 * insertion callback with an early position, as it may ask you to remove
 * an item with a readjusted position later.
 *
 * #GtkMenuTracker will give you a #GtkMenuTrackerItem in your callback. You
 * must hold onto this object until a remove signal is emitted. This item
 * represents a single menu item, which can be one of three classes: normal item,
 * separator, or submenu.
 *
 * Certain properties on the #GtkMenuTrackerItem are mutable, and you must
 * listen for changes in the item. For more details, see the documentation
49
 * for #GtkMenuTrackerItem along with https://wiki.gnome.org/Projects/GLib/GApplication/GMenuModel.
50 51 52
 *
 * The idea of @with_separators is for special cases where menu models may
 * be tracked in places where separators are not available, like in toplevel
53
 * "File", “Edit” menu bars. Ignoring separator items is wrong, as #GtkMenuTracker
54 55 56 57
 * expects the position to change, so we must tell #GtkMenuTracker to ignore
 * separators itself.
 */

58 59 60 61
typedef struct _GtkMenuTrackerSection GtkMenuTrackerSection;

struct _GtkMenuTracker
{
62
  GtkActionObservable      *observable;
63 64
  guint                     merge_sections : 1;
  guint                     mac_os_mode    : 1;
65 66 67 68 69 70 71 72 73
  GtkMenuTrackerInsertFunc  insert_func;
  GtkMenuTrackerRemoveFunc  remove_func;
  gpointer                  user_data;

  GtkMenuTrackerSection    *toplevel;
};

struct _GtkMenuTrackerSection
{
74
  gpointer    model;   /* may be a GtkMenuTrackerItem or a GMenuModel */
75 76 77
  GSList     *items;
  gchar      *action_namespace;

78
  guint       separator_label : 1;
79 80
  guint       with_separators : 1;
  guint       has_separator   : 1;
81
  guint       is_fake         : 1;
82 83 84 85 86 87 88

  gulong      handler;
};

static GtkMenuTrackerSection *  gtk_menu_tracker_section_new    (GtkMenuTracker        *tracker,
                                                                 GMenuModel            *model,
                                                                 gboolean               with_separators,
89
                                                                 gboolean               separator_label,
90 91 92 93 94 95
                                                                 gint                   offset,
                                                                 const gchar           *action_namespace);
static void                    gtk_menu_tracker_section_free    (GtkMenuTrackerSection *section);

static GtkMenuTrackerSection *
gtk_menu_tracker_section_find_model (GtkMenuTrackerSection *section,
96
                                     gpointer               model,
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
                                     gint                  *offset)
{
  GSList *item;

  if (section->has_separator)
    (*offset)++;

  if (section->model == model)
    return section;

  for (item = section->items; item; item = item->next)
    {
      GtkMenuTrackerSection *subsection = item->data;

      if (subsection)
        {
          GtkMenuTrackerSection *found_section;

          found_section = gtk_menu_tracker_section_find_model (subsection, model, offset);

          if (found_section)
            return found_section;
        }
      else
        (*offset)++;
    }

124
  return NULL;
125 126 127 128 129 130 131
}

/* this is responsible for syncing the showing of a separator for a
 * single subsection (and its children).
 *
 * we only ever show separators if we have _actual_ children (ie: we do
 * not show a separator if the section contains only empty child
132
 * sections).  it’s difficult to determine this on-the-fly, so we have
133 134 135 136 137 138 139 140 141 142 143 144 145 146
 * this separate function to come back later and figure it out.
 *
 * 'section' is that section.
 *
 * 'tracker' is passed in so that we can emit callbacks when we decide
 * to add/remove separators.
 *
 * 'offset' is passed in so we know which position to emit in our
 * callbacks.  ie: if we add a separator right at the top of this
 * section then we would emit it with this offset.  deeper inside, we
 * adjust accordingly.
 *
 * could_have_separator is true in two situations:
 *
147 148
 *  - our parent section had with_separators defined and there are items
 *    before us (ie: we should add a separator if we have content in
149 150
 *    order to divide us from the items above)
 *
151
 *  - if we had a “label” attribute set for this section
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
 *
 * parent_model and parent_index are passed in so that we can give them
 * to the insertion callback so that it can see the label (and anything
 * else that happens to be defined on the section).
 *
 * we iterate each item in ourselves.  for subsections, we recursively
 * run ourselves to sync separators.  after we are done, we notice if we
 * have any items in us or if we are completely empty and sync if our
 * separator is shown or not.
 */
static gint
gtk_menu_tracker_section_sync_separators (GtkMenuTrackerSection *section,
                                          GtkMenuTracker        *tracker,
                                          gint                   offset,
                                          gboolean               could_have_separator,
                                          GMenuModel            *parent_model,
                                          gint                   parent_index)
{
  gboolean should_have_separator;
  gint n_items = 0;
  GSList *item;
  gint i = 0;

  for (item = section->items; item; item = item->next)
    {
      GtkMenuTrackerSection *subsection = item->data;

      if (subsection)
        {
181
          gboolean separator;
182

183
          separator = (section->with_separators && n_items > 0) || subsection->separator_label;
184

185
          /* Only pass the parent_model and parent_index in case they may be used to create the separator. */
186
          n_items += gtk_menu_tracker_section_sync_separators (subsection, tracker, offset + n_items,
187 188 189
                                                               separator,
                                                               separator ? section->model : NULL,
                                                               separator ? i : 0);
190 191 192 193 194 195 196
        }
      else
        n_items++;

      i++;
    }

197
  should_have_separator = !section->is_fake && could_have_separator && n_items != 0;
198 199 200 201

  if (should_have_separator > section->has_separator)
    {
      /* Add a separator */
202
      GtkMenuTrackerItem *separator;
203

204 205 206
      separator = _gtk_menu_tracker_item_new (tracker->observable, parent_model, parent_index, FALSE, NULL, TRUE);
      (* tracker->insert_func) (separator, offset, tracker->user_data);
      g_object_unref (separator);
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221
      section->has_separator = TRUE;
    }
  else if (should_have_separator < section->has_separator)
    {
      /* Remove a separator */
      (* tracker->remove_func) (offset, tracker->user_data);
      section->has_separator = FALSE;
    }

  n_items += section->has_separator;

  return n_items;
}

222 223
static void
gtk_menu_tracker_item_visibility_changed (GtkMenuTrackerItem *item,
224
                                          GParamSpec         *pspec,
225 226 227 228
                                          gpointer            user_data)
{
  GtkMenuTracker *tracker = user_data;
  GtkMenuTrackerSection *section;
229
  gboolean is_now_visible;
230 231 232
  gboolean was_visible;
  gint offset = 0;

233
  is_now_visible = gtk_menu_tracker_item_get_is_visible (item);
234

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
  /* remember: the item is our model */
  section = gtk_menu_tracker_section_find_model (tracker->toplevel, item, &offset);

  was_visible = section->items != NULL;

  if (is_now_visible == was_visible)
    return;

  if (is_now_visible)
    {
      section->items = g_slist_prepend (NULL, NULL);
      (* tracker->insert_func) (section->model, offset, tracker->user_data);
    }
  else
    {
      section->items = g_slist_delete_link (section->items, section->items);
      (* tracker->remove_func) (offset, tracker->user_data);
    }

  gtk_menu_tracker_section_sync_separators (tracker->toplevel, tracker, 0, FALSE, NULL, 0);
}

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
static gint
gtk_menu_tracker_section_measure (GtkMenuTrackerSection *section)
{
  GSList *item;
  gint n_items;

  if (section == NULL)
    return 1;

  n_items = 0;

  if (section->has_separator)
    n_items++;

  for (item = section->items; item; item = item->next)
    n_items += gtk_menu_tracker_section_measure (item->data);

  return n_items;
}

static void
gtk_menu_tracker_remove_items (GtkMenuTracker  *tracker,
                               GSList         **change_point,
                               gint             offset,
                               gint             n_items)
{
  gint i;

  for (i = 0; i < n_items; i++)
    {
      GtkMenuTrackerSection *subsection;
      gint n;

      subsection = (*change_point)->data;
      *change_point = g_slist_delete_link (*change_point, *change_point);

      n = gtk_menu_tracker_section_measure (subsection);
      gtk_menu_tracker_section_free (subsection);

      while (n--)
        (* tracker->remove_func) (offset, tracker->user_data);
    }
}

static void
gtk_menu_tracker_add_items (GtkMenuTracker         *tracker,
                            GtkMenuTrackerSection  *section,
                            GSList                **change_point,
                            gint                    offset,
                            GMenuModel             *model,
                            gint                    position,
                            gint                    n_items)
{
  while (n_items--)
    {
      GMenuModel *submenu;

      submenu = g_menu_model_get_item_link (model, position + n_items, G_MENU_LINK_SECTION);
      g_assert (submenu != model);
316 317

      if (submenu != NULL && tracker->merge_sections)
318 319 320
        {
          GtkMenuTrackerSection *subsection;
          gchar *action_namespace = NULL;
321 322 323 324
          gboolean has_label;

          has_label = g_menu_model_get_item_attribute (model, position + n_items,
                                                       G_MENU_ATTRIBUTE_LABEL, "s", NULL);
325 326 327 328 329 330 331 332 333

          g_menu_model_get_item_attribute (model, position + n_items,
                                           G_MENU_ATTRIBUTE_ACTION_NAMESPACE, "s", &action_namespace);

          if (section->action_namespace)
            {
              gchar *namespace;

              namespace = g_strjoin (".", section->action_namespace, action_namespace, NULL);
334
              subsection = gtk_menu_tracker_section_new (tracker, submenu, FALSE, has_label, offset, namespace);
335 336 337
              g_free (namespace);
            }
          else
338
            subsection = gtk_menu_tracker_section_new (tracker, submenu, FALSE, has_label, offset, action_namespace);
339 340 341 342 343 344 345

          *change_point = g_slist_prepend (*change_point, subsection);
          g_free (action_namespace);
          g_object_unref (submenu);
        }
      else
        {
346 347 348
          GtkMenuTrackerItem *item;

          item = _gtk_menu_tracker_item_new (tracker->observable, model, position + n_items,
349
                                             tracker->mac_os_mode,
350
                                             section->action_namespace, submenu != NULL);
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
          /* In the case that the item may disappear we handle that by
           * treating the item that we just created as being its own
           * subsection.  This happens as so:
           *
           *  - the subsection is created without the possibility of
           *    showing a separator
           *
           *  - the subsection will have either 0 or 1 item in it at all
           *    times: either the shown item or not (in the case it is
           *    hidden)
           *
           *  - the created item acts as the "model" for this section
           *    and we use its "visiblity-changed" signal in the same
           *    way that we use the "items-changed" signal from a real
           *    GMenuModel
           *
           * We almost never use the '->model' stored in the section for
           * anything other than lookups and for dropped the ref and
           * disconnecting the signal when we destroy the menu, and we
           * need to do exactly those things in this case as well.
           *
           * The only other thing that '->model' is used for is in the
           * case that we want to show a separator, but we will never do
           * that because separators are not shown for this fake section.
           */
377
          if (gtk_menu_tracker_item_may_disappear (item))
378 379 380 381
            {
              GtkMenuTrackerSection *fake_section;

              fake_section = g_slice_new0 (GtkMenuTrackerSection);
382
              fake_section->is_fake = TRUE;
383
              fake_section->model = g_object_ref (item);
384
              fake_section->handler = g_signal_connect (item, "notify::is-visible",
385 386 387 388
                                                        G_CALLBACK (gtk_menu_tracker_item_visibility_changed),
                                                        tracker);
              *change_point = g_slist_prepend (*change_point, fake_section);

389
              if (gtk_menu_tracker_item_get_is_visible (item))
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
                {
                  (* tracker->insert_func) (item, offset, tracker->user_data);
                  fake_section->items = g_slist_prepend (NULL, NULL);
                }
            }
          else
            {
              /* In the normal case, we store NULL in the linked list.
               * The measurement and lookup code count NULL always as
               * exactly 1: an item that will always be there.
               */
              (* tracker->insert_func) (item, offset, tracker->user_data);
              *change_point = g_slist_prepend (*change_point, NULL);
            }

          g_object_unref (item);
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
        }
    }
}

static void
gtk_menu_tracker_model_changed (GMenuModel *model,
                                gint        position,
                                gint        removed,
                                gint        added,
                                gpointer    user_data)
{
  GtkMenuTracker *tracker = user_data;
  GtkMenuTrackerSection *section;
  GSList **change_point;
  gint offset = 0;
  gint i;

  /* First find which section the changed model corresponds to, and the
   * position of that section within the overall menu.
   */
  section = gtk_menu_tracker_section_find_model (tracker->toplevel, model, &offset);

  /* Next, seek through that section to the change point.  This gives us
   * the correct GSList** to make the change to and also finds the final
   * offset at which we will make the changes (by measuring the number
   * of items within each item of the section before the change point).
   */
  change_point = &section->items;
  for (i = 0; i < position; i++)
    {
      offset += gtk_menu_tracker_section_measure ((*change_point)->data);
      change_point = &(*change_point)->next;
    }

  /* We remove items in order and add items in reverse order.  This
   * means that the offset used for all inserts and removes caused by a
   * single change will be the same.
   *
   * This also has a performance advantage: GtkMenuShell stores the
   * menu items in a linked list.  In the case where we are creating a
   * menu for the first time, adding the items in reverse order means
   * that we only ever insert at index zero, prepending the list.  This
   * means that we can populate in O(n) time instead of O(n^2) that we
   * would do by appending.
   */
  gtk_menu_tracker_remove_items (tracker, change_point, offset, removed);
  gtk_menu_tracker_add_items (tracker, section, change_point, offset, model, position, added);

  /* The offsets for insertion/removal of separators will be all over
   * the place, however...
   */
  gtk_menu_tracker_section_sync_separators (tracker->toplevel, tracker, 0, FALSE, NULL, 0);
}

static void
gtk_menu_tracker_section_free (GtkMenuTrackerSection *section)
{
  if (section == NULL)
    return;

  g_signal_handler_disconnect (section->model, section->handler);
  g_slist_free_full (section->items, (GDestroyNotify) gtk_menu_tracker_section_free);
  g_free (section->action_namespace);
  g_object_unref (section->model);
  g_slice_free (GtkMenuTrackerSection, section);
}

static GtkMenuTrackerSection *
gtk_menu_tracker_section_new (GtkMenuTracker *tracker,
                              GMenuModel     *model,
                              gboolean        with_separators,
477
                              gboolean        separator_label,
478 479 480 481 482 483 484 485 486
                              gint            offset,
                              const gchar    *action_namespace)
{
  GtkMenuTrackerSection *section;

  section = g_slice_new0 (GtkMenuTrackerSection);
  section->model = g_object_ref (model);
  section->with_separators = with_separators;
  section->action_namespace = g_strdup (action_namespace);
487
  section->separator_label = separator_label;
488 489 490 491 492 493 494 495 496 497 498 499

  gtk_menu_tracker_add_items (tracker, section, &section->items, offset, model, 0, g_menu_model_get_n_items (model));
  section->handler = g_signal_connect (model, "items-changed", G_CALLBACK (gtk_menu_tracker_model_changed), tracker);

  return section;
}

/*< private >
 * gtk_menu_tracker_new:
 * @model: the model to flatten
 * @with_separators: if the toplevel should have separators (ie: TRUE
 *   for menus, FALSE for menubars)
500 501 502
 * @merge_sections: if sections should have their items merged in the
 *   usual way or reported only as separators (which can be queried to
 *   manually handle the items)
503
 * @mac_os_mode: if this is on behalf of the Mac OS menubar
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
 * @action_namespace: the passed-in action namespace
 * @insert_func: insert callback
 * @remove_func: remove callback
 * @user_data user data for callbacks
 *
 * Creates a GtkMenuTracker for @model, holding a ref on @model for as
 * long as the tracker is alive.
 *
 * This flattens out the model, merging sections and inserting
 * separators where appropriate.  It monitors for changes and performs
 * updates on the fly.  It also handles action_namespace for subsections
 * (but you will need to handle it yourself for submenus).
 *
 * When the tracker is first created, @insert_func will be called many
 * times to populate the menu with the initial contents of @model
 * (unless it is empty), before gtk_menu_tracker_new() returns.  For
 * this reason, the menu that is using the tracker ought to be empty
 * when it creates the tracker.
 *
 * Future changes to @model will result in more calls to @insert_func
 * and @remove_func.
 *
 * The position argument to both functions is the linear 0-based
 * position in the menu at which the item in question should be inserted
 * or removed.
 *
 * For @insert_func, @model and @item_index are used to get the
 * information about the menu item to insert.  @action_namespace is the
 * action namespace that actions referred to from that item should place
 * themselves in.  Note that if the item is a submenu and the
534
 * “action-namespace” attribute is defined on the item, it will _not_ be
535 536 537 538 539 540 541
 * applied to the @action_namespace argument as it is meant for the
 * items inside of the submenu, not the submenu item itself.
 *
 * @is_separator is set to %TRUE in case the item being added is a
 * separator.  @model and @item_index will still be meaningfully set in
 * this case -- to the section menu item corresponding to the separator.
 * This is useful if the section specifies a label, for example.  If
542
 * there is an “action-namespace” attribute on this menu item then it
543 544 545 546 547 548 549 550
 * should be ignored by the consumer because #GtkMenuTracker has already
 * handled it.
 *
 * When using #GtkMenuTracker there is no need to hold onto @model or
 * monitor it for changes.  The model will be unreffed when
 * gtk_menu_tracker_free() is called.
 */
GtkMenuTracker *
551 552
gtk_menu_tracker_new (GtkActionObservable      *observable,
                      GMenuModel               *model,
553
                      gboolean                  with_separators,
554
                      gboolean                  merge_sections,
555
                      gboolean                  mac_os_mode,
556 557 558 559 560 561 562 563
                      const gchar              *action_namespace,
                      GtkMenuTrackerInsertFunc  insert_func,
                      GtkMenuTrackerRemoveFunc  remove_func,
                      gpointer                  user_data)
{
  GtkMenuTracker *tracker;

  tracker = g_slice_new (GtkMenuTracker);
564
  tracker->merge_sections = merge_sections;
565
  tracker->mac_os_mode = mac_os_mode;
566
  tracker->observable = g_object_ref (observable);
567 568 569 570
  tracker->insert_func = insert_func;
  tracker->remove_func = remove_func;
  tracker->user_data = user_data;

571
  tracker->toplevel = gtk_menu_tracker_section_new (tracker, model, with_separators, FALSE, 0, action_namespace);
572 573 574 575 576
  gtk_menu_tracker_section_sync_separators (tracker->toplevel, tracker, 0, FALSE, NULL, 0);

  return tracker;
}

577
GtkMenuTracker *
578 579
gtk_menu_tracker_new_for_item_link (GtkMenuTrackerItem       *item,
                                    const gchar              *link_name,
580
                                    gboolean                  merge_sections,
581
                                    gboolean                  mac_os_mode,
582 583 584
                                    GtkMenuTrackerInsertFunc  insert_func,
                                    GtkMenuTrackerRemoveFunc  remove_func,
                                    gpointer                  user_data)
585
{
586 587 588 589
  GtkMenuTracker *tracker;
  GMenuModel *submenu;
  gchar *namespace;

590 591
  submenu = _gtk_menu_tracker_item_get_link (item, link_name);
  namespace = _gtk_menu_tracker_item_get_link_namespace (item);
592 593

  tracker = gtk_menu_tracker_new (_gtk_menu_tracker_item_get_observable (item), submenu,
594 595
                                  TRUE, merge_sections, mac_os_mode,
                                  namespace, insert_func, remove_func, user_data);
596 597 598 599 600

  g_object_unref (submenu);
  g_free (namespace);

  return tracker;
601 602
}

603 604 605 606 607 608 609 610 611 612
/*< private >
 * gtk_menu_tracker_free:
 * @tracker: a #GtkMenuTracker
 *
 * Frees the tracker, ...
 */
void
gtk_menu_tracker_free (GtkMenuTracker *tracker)
{
  gtk_menu_tracker_section_free (tracker->toplevel);
613
  g_object_unref (tracker->observable);
614 615
  g_slice_free (GtkMenuTracker, tracker);
}