gtktreemenu.c 62.4 KB
Newer Older
1 2 3 4 5 6 7
/* gtktreemenu.c
 *
 * Copyright (C) 2010 Openismus GmbH
 *
 * Authors:
 *      Tristan Van Berkom <tristanvb@openismus.com>
 *
8 9
 * Based on some GtkComboBox menu code by Kristian Rietveld <kris@gtk.org>
 *
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

26
/*
27 28 29 30
 * SECTION:gtktreemenu
 * @Short_Description: A GtkMenu automatically created from a #GtkTreeModel
 * @Title: GtkTreeMenu
 *
31
 * The #GtkTreeMenu is used to display a drop-down menu allowing selection
32 33 34 35
 * of every row in the model and is used by the #GtkComboBox for its drop-down
 * menu.
 */

36 37 38
#include "config.h"
#include "gtkintl.h"
#include "gtktreemenu.h"
39
#include "gtkmarshalers.h"
40
#include "gtkmenuitem.h"
41
#include "gtktearoffmenuitem.h"
42 43 44 45 46
#include "gtkseparatormenuitem.h"
#include "gtkcellareabox.h"
#include "gtkcellareacontext.h"
#include "gtkcelllayout.h"
#include "gtkcellview.h"
47
#include "gtkmenushellprivate.h"
48 49 50 51 52
#include "gtkprivate.h"


/* GObjectClass */
static GObject  *gtk_tree_menu_constructor                    (GType                  type,
53 54
                                                               guint                  n_construct_properties,
                                                               GObjectConstructParam *construct_properties);
55 56 57
static void      gtk_tree_menu_dispose                        (GObject            *object);
static void      gtk_tree_menu_finalize                       (GObject            *object);
static void      gtk_tree_menu_set_property                   (GObject            *object,
58 59 60
                                                               guint               prop_id,
                                                               const GValue       *value,
                                                               GParamSpec         *pspec);
61
static void      gtk_tree_menu_get_property                   (GObject            *object,
62 63 64
                                                               guint               prop_id,
                                                               GValue             *value,
                                                               GParamSpec         *pspec);
65 66 67

/* GtkWidgetClass */
static void      gtk_tree_menu_get_preferred_width            (GtkWidget           *widget,
68 69
                                                               gint                *minimum_size,
                                                               gint                *natural_size);
70
static void      gtk_tree_menu_get_preferred_height           (GtkWidget           *widget,
71 72
                                                               gint                *minimum_size,
                                                               gint                *natural_size);
73 74 75 76 77 78 79

/* GtkCellLayoutIface */
static void      gtk_tree_menu_cell_layout_init               (GtkCellLayoutIface  *iface);
static GtkCellArea *gtk_tree_menu_cell_layout_get_area        (GtkCellLayout        *layout);


/* TreeModel/DrawingArea callbacks and building menus/submenus */
80 81
static inline void rebuild_menu                               (GtkTreeMenu          *menu);
static gboolean   menu_occupied                               (GtkTreeMenu          *menu,
82 83 84 85
                                                               guint                 left_attach,
                                                               guint                 right_attach,
                                                               guint                 top_attach,
                                                               guint                 bottom_attach);
86
static void       relayout_item                               (GtkTreeMenu          *menu,
87 88 89
                                                               GtkWidget            *item,
                                                               GtkTreeIter          *iter,
                                                               GtkWidget            *prev);
90
static void       gtk_tree_menu_populate                      (GtkTreeMenu          *menu);
91
static GtkWidget *gtk_tree_menu_create_item                   (GtkTreeMenu          *menu,
92 93
                                                               GtkTreeIter          *iter,
                                                               gboolean              header_item);
94
static void       gtk_tree_menu_create_submenu                (GtkTreeMenu          *menu,
95 96
                                                               GtkWidget            *item,
                                                               GtkTreePath          *path);
97
static void       gtk_tree_menu_set_area                      (GtkTreeMenu          *menu,
98
                                                               GtkCellArea          *area);
99
static GtkWidget *gtk_tree_menu_get_path_item                 (GtkTreeMenu          *menu,
100
                                                               GtkTreePath          *path);
101
static gboolean   gtk_tree_menu_path_in_menu                  (GtkTreeMenu          *menu,
102 103
                                                               GtkTreePath          *path,
                                                               gboolean             *header_item);
104
static void       row_inserted_cb                             (GtkTreeModel         *model,
105 106 107
                                                               GtkTreePath          *path,
                                                               GtkTreeIter          *iter,
                                                               GtkTreeMenu          *menu);
108
static void       row_deleted_cb                              (GtkTreeModel         *model,
109 110
                                                               GtkTreePath          *path,
                                                               GtkTreeMenu          *menu);
111
static void       row_reordered_cb                            (GtkTreeModel         *model,
112 113 114 115
                                                               GtkTreePath          *path,
                                                               GtkTreeIter          *iter,
                                                               gint                 *new_order,
                                                               GtkTreeMenu          *menu);
116
static void       row_changed_cb                              (GtkTreeModel         *model,
117 118 119
                                                               GtkTreePath          *path,
                                                               GtkTreeIter          *iter,
                                                               GtkTreeMenu          *menu);
120
static void       context_size_changed_cb                     (GtkCellAreaContext   *context,
121 122
                                                               GParamSpec           *pspec,
                                                               GtkWidget            *menu);
123
static void       area_apply_attributes_cb                    (GtkCellArea          *area,
124 125 126 127 128
                                                               GtkTreeModel         *tree_model,
                                                               GtkTreeIter          *iter,
                                                               gboolean              is_expander,
                                                               gboolean              is_expanded,
                                                               GtkTreeMenu          *menu);
129
static void       item_activated_cb                           (GtkMenuItem          *item,
130
                                                               GtkTreeMenu          *menu);
131
static void       submenu_activated_cb                        (GtkTreeMenu          *submenu,
132 133
                                                               const gchar          *path,
                                                               GtkTreeMenu          *menu);
134
static void       gtk_tree_menu_set_model_internal            (GtkTreeMenu          *menu,
135
                                                               GtkTreeModel         *model);
136

137 138


139 140 141 142 143 144 145 146 147
struct _GtkTreeMenuPrivate
{
  /* TreeModel and parent for this menu */
  GtkTreeModel        *model;
  GtkTreeRowReference *root;

  /* CellArea and context for this menu */
  GtkCellArea         *area;
  GtkCellAreaContext  *context;
148

149 150
  /* Signals */
  gulong               size_changed_id;
151
  gulong               apply_attributes_id;
152 153 154
  gulong               row_inserted_id;
  gulong               row_deleted_id;
  gulong               row_reordered_id;
155 156 157 158 159 160 161 162 163 164
  gulong               row_changed_id;

  /* Grid menu mode */
  gint                 wrap_width;
  gint                 row_span_col;
  gint                 col_span_col;

  /* Flags */
  guint32              menu_with_header : 1;
  guint32              tearoff     : 1;
165 166 167 168 169

  /* Row separators */
  GtkTreeViewRowSeparatorFunc row_separator_func;
  gpointer                    row_separator_data;
  GDestroyNotify              row_separator_destroy;
170 171 172 173 174

  /* Submenu headers */
  GtkTreeMenuHeaderFunc header_func;
  gpointer              header_data;
  GDestroyNotify        header_destroy;
175 176 177 178 179 180
};

enum {
  PROP_0,
  PROP_MODEL,
  PROP_ROOT,
181
  PROP_CELL_AREA,
182 183 184 185
  PROP_TEAROFF,
  PROP_WRAP_WIDTH,
  PROP_ROW_SPAN_COL,
  PROP_COL_SPAN_COL
186 187
};

188 189 190 191 192
enum {
  SIGNAL_MENU_ACTIVATE,
  N_SIGNALS
};

193 194
static guint   tree_menu_signals[N_SIGNALS] = { 0 };
static GQuark  tree_menu_path_quark = 0;
195

196
G_DEFINE_TYPE_WITH_CODE (GtkTreeMenu, _gtk_tree_menu, GTK_TYPE_MENU,
197 198
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
                                                gtk_tree_menu_cell_layout_init));
199 200

static void
201
_gtk_tree_menu_init (GtkTreeMenu *menu)
202 203 204 205
{
  GtkTreeMenuPrivate *priv;

  menu->priv = G_TYPE_INSTANCE_GET_PRIVATE (menu,
206 207
                                            GTK_TYPE_TREE_MENU,
                                            GtkTreeMenuPrivate);
208
  priv = menu->priv;
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
  priv->model     = NULL;
  priv->root      = NULL;
  priv->area      = NULL;
  priv->context   = NULL;

  priv->size_changed_id  = 0;
  priv->row_inserted_id  = 0;
  priv->row_deleted_id   = 0;
  priv->row_reordered_id = 0;
  priv->row_changed_id   = 0;

  priv->wrap_width   = 0;
  priv->row_span_col = -1;
  priv->col_span_col = -1;

  priv->menu_with_header = FALSE;
  priv->tearoff          = FALSE;

  priv->row_separator_func    = NULL;
  priv->row_separator_data    = NULL;
  priv->row_separator_destroy = NULL;

  priv->header_func    = NULL;
  priv->header_data    = NULL;
  priv->header_destroy = NULL;

236
  gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
237 238
}

239
static void
240
_gtk_tree_menu_class_init (GtkTreeMenuClass *class)
241 242 243 244
{
  GObjectClass   *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);

245 246
  tree_menu_path_quark = g_quark_from_static_string ("gtk-tree-menu-path");

247 248 249 250 251 252
  object_class->constructor  = gtk_tree_menu_constructor;
  object_class->dispose      = gtk_tree_menu_dispose;
  object_class->finalize     = gtk_tree_menu_finalize;
  object_class->set_property = gtk_tree_menu_set_property;
  object_class->get_property = gtk_tree_menu_get_property;

253 254
  widget_class->get_preferred_width  = gtk_tree_menu_get_preferred_width;
  widget_class->get_preferred_height = gtk_tree_menu_get_preferred_height;
255

256
  /*
257 258 259 260 261 262 263 264 265 266 267
   * GtkTreeMenu::menu-activate:
   * @menu: a #GtkTreeMenu
   * @path: the #GtkTreePath string for the item which was activated
   * @user_data: the user data
   *
   * This signal is emitted to notify that a menu item in the #GtkTreeMenu
   * was activated and provides the path string from the #GtkTreeModel
   * to specify which row was selected.
   *
   * Since: 3.0
   */
268 269
  tree_menu_signals[SIGNAL_MENU_ACTIVATE] =
    g_signal_new (I_("menu-activate"),
270 271 272 273 274 275
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST,
                  0, /* No class closure here */
                  NULL, NULL,
                  _gtk_marshal_VOID__STRING,
                  G_TYPE_NONE, 1, G_TYPE_STRING);
276

277
  /*
278 279 280
   * GtkTreeMenu:model:
   *
   * The #GtkTreeModel from which the menu is constructed.
281
   *
282 283
   * Since: 3.0
   */
284 285 286 287 288 289 290 291
  g_object_class_install_property (object_class,
                                   PROP_MODEL,
                                   g_param_spec_object ("model",
                                                        P_("TreeMenu model"),
                                                        P_("The model for the tree menu"),
                                                        GTK_TYPE_TREE_MODEL,
                                                        GTK_PARAM_READWRITE));

292
  /*
293 294 295 296 297 298 299 300 301 302
   * GtkTreeMenu:root:
   *
   * The #GtkTreePath that is the root for this menu, or %NULL.
   *
   * The #GtkTreeMenu recursively creates submenus for #GtkTreeModel
   * rows that have children and the "root" for each menu is provided
   * by the parent menu.
   *
   * If you dont provide a root for the #GtkTreeMenu then the whole
   * model will be added to the menu. Specifying a root allows you
303
   * to build a menu for a given #GtkTreePath and its children.
304 305 306
   * 
   * Since: 3.0
   */
307 308 309
  g_object_class_install_property (object_class,
                                   PROP_ROOT,
                                   g_param_spec_boxed ("root",
310 311 312 313 314
                                                       P_("TreeMenu root row"),
                                                       P_("The TreeMenu will display children of the "
                                                          "specified root"),
                                                       GTK_TYPE_TREE_PATH,
                                                       GTK_PARAM_READWRITE));
315

316
  /*
317 318 319 320 321 322 323 324 325
   * GtkTreeMenu:cell-area:
   *
   * The #GtkCellArea used to render cells in the menu items.
   *
   * You can provide a different cell area at object construction
   * time, otherwise the #GtkTreeMenu will use a #GtkCellAreaBox.
   *
   * Since: 3.0
   */
326
  g_object_class_install_property (object_class,
327 328 329 330 331 332
                                   PROP_CELL_AREA,
                                   g_param_spec_object ("cell-area",
                                                        P_("Cell Area"),
                                                        P_("The GtkCellArea used to layout cells"),
                                                        GTK_TYPE_CELL_AREA,
                                                        GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
333

334
  /*
335 336 337 338 339 340
   * GtkTreeMenu:tearoff:
   *
   * Specifies whether this menu comes with a leading tearoff menu item
   *
   * Since: 3.0
   */
341
  g_object_class_install_property (object_class,
342 343 344 345 346 347
                                   PROP_TEAROFF,
                                   g_param_spec_boolean ("tearoff",
                                                         P_("Tearoff"),
                                                         P_("Whether the menu has a tearoff item"),
                                                         FALSE,
                                                         GTK_PARAM_READWRITE));
348

349
  /*
350 351 352 353 354 355 356 357 358
   * GtkTreeMenu:wrap-width:
   *
   * If wrap-width is set to a positive value, the list will be
   * displayed in multiple columns, the number of columns is
   * determined by wrap-width.
   *
   * Since: 3.0
   */
  g_object_class_install_property (object_class,
359 360 361 362 363 364 365 366
                                   PROP_WRAP_WIDTH,
                                   g_param_spec_int ("wrap-width",
                                                     P_("Wrap Width"),
                                                     P_("Wrap width for laying out items in a grid"),
                                                     0,
                                                     G_MAXINT,
                                                     0,
                                                     GTK_PARAM_READWRITE));
367

368
  /*
369 370
   * GtkTreeMenu:row-span-column:
   *
371 372
   * If this is set to a non-negative value, it must be the index of a column
   * of type %G_TYPE_INT in the model.
373
   *
374 375
   * The values of that column are used to determine how many rows a value in
   * the list will span. Therefore, the values in the model column pointed to
376 377 378 379 380
   * by this property must be greater than zero and not larger than wrap-width.
   *
   * Since: 3.0
   */
  g_object_class_install_property (object_class,
381 382 383 384 385 386 387 388
                                   PROP_ROW_SPAN_COL,
                                   g_param_spec_int ("row-span-column",
                                                     P_("Row span column"),
                                                     P_("TreeModel column containing the row span values"),
                                                     -1,
                                                     G_MAXINT,
                                                     -1,
                                                     GTK_PARAM_READWRITE));
389

390
  /*
391 392
   * GtkTreeMenu:column-span-column:
   *
393 394
   * If this is set to a non-negative value, it must be the index of a column
   * of type %G_TYPE_INT in the model.
395
   *
396 397
   * The values of that column are used to determine how many columns a value
   * in the list will span.
398 399 400 401
   *
   * Since: 3.0
   */
  g_object_class_install_property (object_class,
402 403 404 405 406 407 408 409
                                   PROP_COL_SPAN_COL,
                                   g_param_spec_int ("column-span-column",
                                                     P_("Column span column"),
                                                     P_("TreeModel column containing the column span values"),
                                                     -1,
                                                     G_MAXINT,
                                                     -1,
                                                     GTK_PARAM_READWRITE));
410 411

  g_type_class_add_private (object_class, sizeof (GtkTreeMenuPrivate));
412 413 414 415 416 417 418
}

/****************************************************************
 *                         GObjectClass                         *
 ****************************************************************/
static GObject  *
gtk_tree_menu_constructor (GType                  type,
419 420
                           guint                  n_construct_properties,
                           GObjectConstructParam *construct_properties)
421 422 423 424 425
{
  GObject            *object;
  GtkTreeMenu        *menu;
  GtkTreeMenuPrivate *priv;

426
  object = G_OBJECT_CLASS (_gtk_tree_menu_parent_class)->constructor
427 428 429 430 431 432 433 434 435 436 437 438 439
    (type, n_construct_properties, construct_properties);

  menu = GTK_TREE_MENU (object);
  priv = menu->priv;

  if (!priv->area)
    {
      GtkCellArea *area = gtk_cell_area_box_new ();

      gtk_tree_menu_set_area (menu, area);
    }

  priv->context = gtk_cell_area_create_context (priv->area);
440

441
  priv->size_changed_id =
442
    g_signal_connect (priv->context, "notify",
443
                      G_CALLBACK (context_size_changed_cb), menu);
444 445 446 447 448 449 450 451 452 453 454 455 456

  return object;
}

static void
gtk_tree_menu_dispose (GObject *object)
{
  GtkTreeMenu        *menu;
  GtkTreeMenuPrivate *priv;

  menu = GTK_TREE_MENU (object);
  priv = menu->priv;

457
  _gtk_tree_menu_set_model (menu, NULL);
458 459 460 461 462 463 464 465 466 467 468 469
  gtk_tree_menu_set_area (menu, NULL);

  if (priv->context)
    {
      /* Disconnect signals */
      g_signal_handler_disconnect (priv->context, priv->size_changed_id);

      g_object_unref (priv->context);
      priv->context = NULL;
      priv->size_changed_id = 0;
    }

470
  G_OBJECT_CLASS (_gtk_tree_menu_parent_class)->dispose (object);
471 472 473 474 475
}

static void
gtk_tree_menu_finalize (GObject *object)
{
476 477 478 479 480 481
  GtkTreeMenu        *menu;
  GtkTreeMenuPrivate *priv;

  menu = GTK_TREE_MENU (object);
  priv = menu->priv;

482 483
  _gtk_tree_menu_set_row_separator_func (menu, NULL, NULL, NULL);
  _gtk_tree_menu_set_header_func (menu, NULL, NULL, NULL);
484

485
  if (priv->root)
486
    gtk_tree_row_reference_free (priv->root);
487

488
  G_OBJECT_CLASS (_gtk_tree_menu_parent_class)->finalize (object);
489 490 491 492
}

static void
gtk_tree_menu_set_property (GObject            *object,
493 494 495
                            guint               prop_id,
                            const GValue       *value,
                            GParamSpec         *pspec)
496 497 498 499 500 501
{
  GtkTreeMenu *menu = GTK_TREE_MENU (object);

  switch (prop_id)
    {
    case PROP_MODEL:
502
      _gtk_tree_menu_set_model (menu, g_value_get_object (value));
503 504 505
      break;

    case PROP_ROOT:
506
      _gtk_tree_menu_set_root (menu, g_value_get_boxed (value));
507 508 509 510 511 512 513
      break;

    case PROP_CELL_AREA:
      /* Construct-only, can only be assigned once */
      gtk_tree_menu_set_area (menu, (GtkCellArea *)g_value_get_object (value));
      break;

514
    case PROP_TEAROFF:
515
      _gtk_tree_menu_set_tearoff (menu, g_value_get_boolean (value));
516 517
      break;

518
    case PROP_WRAP_WIDTH:
519
      _gtk_tree_menu_set_wrap_width (menu, g_value_get_int (value));
520 521 522
      break;

     case PROP_ROW_SPAN_COL:
523
      _gtk_tree_menu_set_row_span_column (menu, g_value_get_int (value));
524 525 526
      break;

     case PROP_COL_SPAN_COL:
527
      _gtk_tree_menu_set_column_span_column (menu, g_value_get_int (value));
528 529
      break;

530 531 532 533 534 535 536 537
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_tree_menu_get_property (GObject            *object,
538 539 540
                            guint               prop_id,
                            GValue             *value,
                            GParamSpec         *pspec)
541 542 543 544 545 546
{
  GtkTreeMenu        *menu = GTK_TREE_MENU (object);
  GtkTreeMenuPrivate *priv = menu->priv;

  switch (prop_id)
    {
547 548 549
    case PROP_MODEL:
      g_value_set_object (value, priv->model);
      break;
550

551 552 553
    case PROP_ROOT:
      g_value_set_boxed (value, priv->root);
      break;
554

555 556 557
    case PROP_CELL_AREA:
      g_value_set_object (value, priv->area);
      break;
558

559 560 561
    case PROP_TEAROFF:
      g_value_set_boolean (value, priv->tearoff);
      break;
562

563 564 565
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
    }
}

/****************************************************************
 *                         GtkWidgetClass                       *
 ****************************************************************/

/* We tell all the menu items to reserve space for the submenu
 * indicator if there is at least one submenu, this way we ensure
 * that every internal cell area gets allocated the
 * same width (and requested height for the same appropriate width).
 */
static void
sync_reserve_submenu_size (GtkTreeMenu *menu)
{
  GList              *children, *l;
  gboolean            has_submenu = FALSE;

  children = gtk_container_get_children (GTK_CONTAINER (menu));
  for (l = children; l; l = l->next)
    {
      GtkMenuItem *item = l->data;

      if (gtk_menu_item_get_submenu (item) != NULL)
590 591 592 593
        {
          has_submenu = TRUE;
          break;
        }
594 595 596 597 598 599 600 601 602 603 604 605 606 607
    }

  for (l = children; l; l = l->next)
    {
      GtkMenuItem *item = l->data;

      gtk_menu_item_set_reserve_indicator (item, has_submenu);
    }

  g_list_free (children);
}

static void
gtk_tree_menu_get_preferred_width (GtkWidget           *widget,
608 609
                                   gint                *minimum_size,
                                   gint                *natural_size)
610 611 612 613
{
  GtkTreeMenu        *menu = GTK_TREE_MENU (widget);
  GtkTreeMenuPrivate *priv = menu->priv;

614 615 616 617 618 619
  /* We leave the requesting work up to the cellviews which operate in the same
   * context, reserving space for the submenu indicator if any of the items have
   * submenus ensures that every cellview will receive the same allocated width.
   *
   * Since GtkMenu does hieght-for-width correctly, we know that the width of
   * every cell will be requested before the height-for-widths are requested.
620
   */
621
  g_signal_handler_block (priv->context, priv->size_changed_id);
622 623 624

  sync_reserve_submenu_size (menu);

625
  GTK_WIDGET_CLASS (_gtk_tree_menu_parent_class)->get_preferred_width (widget, minimum_size, natural_size);
626 627 628 629 630 631

  g_signal_handler_unblock (priv->context, priv->size_changed_id);
}

static void
gtk_tree_menu_get_preferred_height (GtkWidget           *widget,
632 633
                                    gint                *minimum_size,
                                    gint                *natural_size)
634 635 636 637 638 639 640 641
{
  GtkTreeMenu        *menu = GTK_TREE_MENU (widget);
  GtkTreeMenuPrivate *priv = menu->priv;

  g_signal_handler_block (priv->context, priv->size_changed_id);

  sync_reserve_submenu_size (menu);

642
  GTK_WIDGET_CLASS (_gtk_tree_menu_parent_class)->get_preferred_height (widget, minimum_size, natural_size);
643 644 645 646 647 648 649 650 651 652

  g_signal_handler_unblock (priv->context, priv->size_changed_id);
}

/****************************************************************
 *                      GtkCellLayoutIface                      *
 ****************************************************************/
static void
gtk_tree_menu_cell_layout_init (GtkCellLayoutIface  *iface)
{
653
  iface->get_area = gtk_tree_menu_cell_layout_get_area;
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
}

static GtkCellArea *
gtk_tree_menu_cell_layout_get_area (GtkCellLayout *layout)
{
  GtkTreeMenu        *menu = GTK_TREE_MENU (layout);
  GtkTreeMenuPrivate *priv = menu->priv;

  return priv->area;
}


/****************************************************************
 *             TreeModel callbacks/populating menus             *
 ****************************************************************/
669 670
static GtkWidget *
gtk_tree_menu_get_path_item (GtkTreeMenu          *menu,
671
                             GtkTreePath          *search)
672 673 674 675 676 677 678 679 680 681 682 683
{
  GtkWidget *item = NULL;
  GList     *children, *l;

  children = gtk_container_get_children (GTK_CONTAINER (menu));

  for (l = children; item == NULL && l != NULL; l = l->next)
    {
      GtkWidget   *child = l->data;
      GtkTreePath *path  = NULL;

      if (GTK_IS_SEPARATOR_MENU_ITEM (child))
684 685 686 687 688 689 690 691 692
        {
          GtkTreeRowReference *row =
            g_object_get_qdata (G_OBJECT (child), tree_menu_path_quark);

          if (row)
            {
              path = gtk_tree_row_reference_get_path (row);

              if (!path)
693
                /* Return any first child where its row-reference became invalid,
694 695 696 697 698 699
                 * this is because row-references get null paths before we recieve
                 * the "row-deleted" signal.
                 */
                item = child;
            }
        }
700
      else if (!GTK_IS_TEAROFF_MENU_ITEM (child))
701 702
        {
          GtkWidget *view = gtk_bin_get_child (GTK_BIN (child));
703

704 705 706
          /* It's always a cellview */
          if (GTK_IS_CELL_VIEW (view))
            path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (view));
707

708
          if (!path)
709
            /* Return any first child where its row-reference became invalid,
710 711 712 713 714
             * this is because row-references get null paths before we recieve
             * the "row-deleted" signal.
             */
            item = child;
        }
715 716

      if (path)
717 718 719
        {
          if (gtk_tree_path_compare (search, path) == 0)
            item = child;
720

721 722
          gtk_tree_path_free (path);
        }
723 724 725 726 727 728 729
    }

  g_list_free (children);

  return item;
}

730 731
static gboolean
gtk_tree_menu_path_in_menu (GtkTreeMenu  *menu,
732 733
                            GtkTreePath  *path,
                            gboolean     *header_item)
734 735
{
  GtkTreeMenuPrivate *priv = menu->priv;
736 737
  gboolean            in_menu = FALSE;
  gboolean            is_header = FALSE;
738

739
  /* Check if the is in root of the model */
740 741
  if (gtk_tree_path_get_depth (path) == 1 && !priv->root)
    in_menu = TRUE;
742
  /* If we are a submenu, compare the parent path */
743
  else if (priv->root)
744
    {
745
      GtkTreePath *root_path   = gtk_tree_row_reference_get_path (priv->root);
746
      GtkTreePath *search_path = gtk_tree_path_copy (path);
747

748
      if (root_path)
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
        {
          if (priv->menu_with_header &&
              gtk_tree_path_compare (root_path, search_path) == 0)
            {
              in_menu   = TRUE;
              is_header = TRUE;
            }
          else if (gtk_tree_path_get_depth (search_path) > 1)
            {
              gtk_tree_path_up (search_path);

              if (gtk_tree_path_compare (root_path, search_path) == 0)
                in_menu = TRUE;
            }
        }
764
      gtk_tree_path_free (root_path);
765
      gtk_tree_path_free (search_path);
766 767
    }

768 769 770 771 772 773
  if (header_item)
    *header_item = is_header;

  return in_menu;
}

774
static GtkWidget *
775 776
gtk_tree_menu_path_needs_submenu (GtkTreeMenu *menu,
                                  GtkTreePath *search)
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
{
  GtkWidget   *item = NULL;
  GList       *children, *l;
  GtkTreePath *parent_path;

  if (gtk_tree_path_get_depth (search) <= 1)
    return NULL;

  parent_path = gtk_tree_path_copy (search);
  gtk_tree_path_up (parent_path);

  children    = gtk_container_get_children (GTK_CONTAINER (menu));

  for (l = children; item == NULL && l != NULL; l = l->next)
    {
      GtkWidget   *child = l->data;
      GtkTreePath *path  = NULL;

      /* Separators dont get submenus, if it already has a submenu then let
       * the submenu handle inserted rows */
797 798 799 800
      if (!GTK_IS_SEPARATOR_MENU_ITEM (child) &&
          !gtk_menu_item_get_submenu (GTK_MENU_ITEM (child)))
        {
          GtkWidget *view = gtk_bin_get_child (GTK_BIN (child));
801

802 803 804 805
          /* It's always a cellview */
          if (GTK_IS_CELL_VIEW (view))
            path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (view));
        }
806 807

      if (path)
808 809 810
        {
          if (gtk_tree_path_compare (parent_path, path) == 0)
            item = child;
811

812 813
          gtk_tree_path_free (path);
        }
814 815 816 817 818 819 820 821
    }

  g_list_free (children);
  gtk_tree_path_free (parent_path);

  return item;
}

822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
static GtkWidget *
find_empty_submenu (GtkTreeMenu  *menu)
{
  GtkTreeMenuPrivate *priv = menu->priv;
  GList              *children, *l;
  GtkWidget          *submenu = NULL;

  children = gtk_container_get_children (GTK_CONTAINER (menu));

  for (l = children; submenu == NULL && l != NULL; l = l->next)
    {
      GtkWidget   *child = l->data;
      GtkTreePath *path  = NULL;
      GtkTreeIter  iter;

      /* Separators dont get submenus, if it already has a submenu then let
       * the submenu handle inserted rows */
      if (!GTK_IS_SEPARATOR_MENU_ITEM (child) && !GTK_IS_TEAROFF_MENU_ITEM (child))
840 841
        {
          GtkWidget *view = gtk_bin_get_child (GTK_BIN (child));
842

843 844 845 846
          /* It's always a cellview */
          if (GTK_IS_CELL_VIEW (view))
            path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (view));
        }
847 848

      if (path)
849 850 851 852
        {
          if (gtk_tree_model_get_iter (priv->model, &iter, path) &&
              !gtk_tree_model_iter_has_child (priv->model, &iter))
            submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (child));
853

854 855
          gtk_tree_path_free (path);
        }
856 857 858 859 860 861 862
    }

  g_list_free (children);

  return submenu;
}

863 864
static void
row_inserted_cb (GtkTreeModel     *model,
865 866 867
                 GtkTreePath      *path,
                 GtkTreeIter      *iter,
                 GtkTreeMenu      *menu)
868 869 870
{
  GtkTreeMenuPrivate *priv = menu->priv;
  gint               *indices, index, depth;
871
  GtkWidget          *item;
872

873
  /* If the iter should be in this menu then go ahead and insert it */
874
  if (gtk_tree_menu_path_in_menu (menu, path, NULL))
875
    {
876
      if (priv->wrap_width > 0)
877
        rebuild_menu (menu);
878
      else
879 880 881 882 883 884
        {
          /* Get the index of the path for this depth */
          indices = gtk_tree_path_get_indices (path);
          depth   = gtk_tree_path_get_depth (path);
          index   = indices[depth -1];

885
          /* Menus with a header include a menuitem for its root node
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
           * and a separator menu item */
          if (priv->menu_with_header)
            index += 2;

          /* Index after the tearoff item for the root menu if
           * there is a tearoff item
           */
          if (priv->root == NULL && priv->tearoff)
            index += 1;

          item = gtk_tree_menu_create_item (menu, iter, FALSE);
          gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, index);

          /* Resize everything */
          gtk_cell_area_context_reset (menu->priv->context);
        }
902
    }
903 904 905 906 907
  else
    {
      /* Create submenus for iters if we need to */
      item = gtk_tree_menu_path_needs_submenu (menu, path);
      if (item)
908 909
        {
          GtkTreePath *item_path = gtk_tree_path_copy (path);
910

911 912 913 914
          gtk_tree_path_up (item_path);
          gtk_tree_menu_create_submenu (menu, item, item_path);
          gtk_tree_path_free (item_path);
        }
915
    }
916 917 918 919
}

static void
row_deleted_cb (GtkTreeModel     *model,
920 921
                GtkTreePath      *path,
                GtkTreeMenu      *menu)
922 923 924 925
{
  GtkTreeMenuPrivate *priv = menu->priv;
  GtkWidget          *item;

926
  /* If it's the header item we leave it to the parent menu
927 928
   * to remove us from its menu
   */
929 930 931
  item = gtk_tree_menu_get_path_item (menu, path);

  if (item)
932
    {
933
      if (priv->wrap_width > 0)
934
        rebuild_menu (menu);
935
      else
936 937 938 939 940 941 942
        {
          /* Get rid of the deleted item */
          gtk_widget_destroy (item);

          /* Resize everything */
          gtk_cell_area_context_reset (menu->priv->context);
        }
943
    }
944
  else
945 946 947 948 949
    {
      /* It's up to the parent menu to destroy a child menu that becomes empty
       * since the topmost menu belongs to the user and is allowed to have no contents */
      GtkWidget *submenu = find_empty_submenu (menu);
      if (submenu)
950
        gtk_widget_destroy (submenu);
951
    }
952 953 954 955
}

static void
row_reordered_cb (GtkTreeModel    *model,
956 957 958 959
                  GtkTreePath     *path,
                  GtkTreeIter     *iter,
                  gint            *new_order,
                  GtkTreeMenu     *menu)
960 961 962 963
{
  GtkTreeMenuPrivate *priv = menu->priv;
  gboolean            this_menu = FALSE;

964
  if (gtk_tree_path_get_depth (path) == 0 && !priv->root)
965 966 967 968
    this_menu = TRUE;
  else if (priv->root)
    {
      GtkTreePath *root_path =
969
        gtk_tree_row_reference_get_path (priv->root);
970 971

      if (gtk_tree_path_compare (root_path, path) == 0)
972
        this_menu = TRUE;
973 974 975 976 977

      gtk_tree_path_free (root_path);
    }

  if (this_menu)
978 979 980
    rebuild_menu (menu);
}

981
static gint
982
menu_item_position (GtkTreeMenu *menu,
983
                    GtkWidget   *item)
984 985 986 987 988 989 990 991 992 993
{
  GList *children, *l;
  gint   position;

  children = gtk_container_get_children (GTK_CONTAINER (menu));
  for (position = 0, l = children; l; position++, l = l->next)
    {
      GtkWidget *iitem = l->data;

      if (item == iitem)
994
        break;
995 996 997 998 999 1000 1001 1002 1003
    }

  g_list_free (children);

  return position;
}

static void
row_changed_cb (GtkTreeModel         *model,
1004 1005 1006
                GtkTreePath          *path,
                GtkTreeIter          *iter,
                GtkTreeMenu          *menu)
1007 1008 1009 1010 1011 1012 1013 1014 1015
{
  GtkTreeMenuPrivate *priv = menu->priv;
  gboolean            is_separator = FALSE;
  gboolean            has_header = FALSE;
  GtkWidget          *item;

  item = gtk_tree_menu_get_path_item (menu, path);

  if (priv->root)
1016
    {
1017
      GtkTreePath *root_path =
1018 1019
        gtk_tree_row_reference_get_path (priv->root);

1020
      if (root_path && gtk_tree_path_compare (root_path, path) == 0)
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
        {
          if (priv->header_func)
            has_header =
              priv->header_func (priv->model, iter, priv->header_data);

          if (has_header && !item)
            {
              item = gtk_separator_menu_item_new ();
              gtk_widget_show (item);
              gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);

              item = gtk_tree_menu_create_item (menu, iter, TRUE);
              gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);

              priv->menu_with_header = TRUE;
            }
          else if (!has_header && item)
            {
              /* Destroy the header item and then the following separator */
              gtk_widget_destroy (item);
              gtk_widget_destroy (GTK_MENU_SHELL (menu)->priv->children->data);

              priv->menu_with_header = FALSE;
            }

          gtk_tree_path_free (root_path);
        }
1048
    }
1049

1050 1051 1052
  if (item)
    {
      if (priv->wrap_width > 0)
1053 1054 1055 1056
        /* Ugly, we need to rebuild the menu here if
         * the row-span/row-column values change
         */
        rebuild_menu (menu);
1057
      else
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
        {
          if (priv->row_separator_func)
            is_separator =
              priv->row_separator_func (model, iter,
                                        priv->row_separator_data);


          if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
            {
              gint position = menu_item_position (menu, item);

              gtk_widget_destroy (item);
              item = gtk_tree_menu_create_item (menu, iter, FALSE);
              gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, position);
            }
        }
1074 1075 1076
    }
}

1077 1078
static void
context_size_changed_cb (GtkCellAreaContext  *context,
1079 1080
                         GParamSpec          *pspec,
                         GtkWidget           *menu)
1081 1082 1083 1084 1085
{
  if (!strcmp (pspec->name, "minimum-width") ||
      !strcmp (pspec->name, "natural-width") ||
      !strcmp (pspec->name, "minimum-height") ||
      !strcmp (pspec->name, "natural-height"))
1086
    gtk_widget_queue_resize (menu);
1087 1088
}

1089 1090 1091 1092 1093
static gboolean
area_is_sensitive (GtkCellArea *area)
{
  GList    *cells, *list;
  gboolean  sensitive = FALSE;
1094

1095 1096 1097 1098 1099
  cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));

  for (list = cells; list; list = list->next)
    {
      g_object_get (list->data, "sensitive", &sensitive, NULL);
1100

1101
      if (sensitive)
1102
        break;
1103 1104 1105 1106 1107 1108 1109 1110
    }
  g_list_free (cells);

  return sensitive;
}

static void
area_apply_attributes_cb (GtkCellArea          *area,
1111 1112 1113 1114 1115
                          GtkTreeModel         *tree_model,
                          GtkTreeIter          *iter,
                          gboolean              is_expander,
                          gboolean              is_expanded,
                          GtkTreeMenu          *menu)
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
{
  /* If the menu for this iter has a submenu */
  GtkTreeMenuPrivate *priv = menu->priv;
  GtkTreePath        *path;
  GtkWidget          *item;
  gboolean            is_header;
  gboolean            sensitive;

  path = gtk_tree_model_get_path (tree_model, iter);

  if (gtk_tree_menu_path_in_menu (menu, path, &is_header))
    {
      item = gtk_tree_menu_get_path_item (menu, path);

      /* If there is no submenu, go ahead and update item sensitivity,
       * items with submenus are always sensitive */
1132
      if (item && !gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)))
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
        {
          sensitive = area_is_sensitive (priv->area);

          gtk_widget_set_sensitive (item, sensitive);

          if (is_header)
            {
              /* For header items we need to set the sensitivity
               * of the following separator item
               */
              if (GTK_MENU_SHELL (menu)->priv->children &&
                  GTK_MENU_SHELL (menu)->priv->children->next)
                {
                  GtkWidget *separator =
                    GTK_MENU_SHELL (menu)->priv->children->next->data;

                  gtk_widget_set_sensitive (separator, sensitive);
                }
            }
        }
1153 1154 1155 1156 1157
    }

  gtk_tree_path_free (path);
}

1158 1159
static void
gtk_tree_menu_set_area (GtkTreeMenu *menu,
1160
                        GtkCellArea *area)
1161 1162 1163 1164
{
  GtkTreeMenuPrivate *priv = menu->priv;

  if (priv->area)
1165 1166
    {
      g_signal_handler_disconnect (priv->area,
1167
                                   priv->apply_attributes_id);
1168 1169 1170 1171
      priv->apply_attributes_id = 0;

      g_object_unref (priv->area);
    }
1172 1173 1174 1175

  priv->area = area;

  if (priv->area)
1176 1177 1178 1179
    {
      g_object_ref_sink (priv->area);

      priv->apply_attributes_id =
1180 1181
        g_signal_connect (priv->area, "apply-attributes",
                          G_CALLBACK (area_apply_attributes_cb), menu);
1182
    }
1183 1184
}

1185 1186 1187 1188 1189 1190 1191 1192 1193
static gboolean
menu_occupied (GtkTreeMenu *menu,
               guint        left_attach,
               guint        right_attach,
               guint        top_attach,
               guint        bottom_attach)
{
  GList *i;

1194
  for (i = GTK_MENU_SHELL (menu)->priv->children; i; i = i->next)
1195 1196 1197
    {
      guint l, r, b, t;

1198 1199
      gtk_container_child_get (GTK_CONTAINER (menu),
                               i->data,
1200 1201 1202 1203 1204 1205 1206 1207
                               "left-attach", &l,
                               "right-attach", &r,
                               "bottom-attach", &b,
                               "top-attach", &t,
                               NULL);

      /* look if this item intersects with the given coordinates */
      if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
1208
        return TRUE;
1209 1210 1211 1212 1213 1214 1215
    }

  return FALSE;
}

static void
relayout_item (GtkTreeMenu *menu,
1216 1217 1218
               GtkWidget   *item,
               GtkTreeIter *iter,
               GtkWidget   *prev)
1219 1220 1221 1222
{
  GtkTreeMenuPrivate *priv = menu->priv;
  gint                current_col = 0, current_row = 0;
  gint                rows = 1, cols = 1;
1223

1224 1225 1226 1227 1228
  if (priv->col_span_col == -1 &&
      priv->row_span_col == -1 &&
      prev)
    {
      gtk_container_child_get (GTK_CONTAINER (menu), prev,
1229 1230 1231
                               "right-attach", &current_col,
                               "top-attach", &current_row,
                               NULL);
1232
      if (current_col + cols > priv->wrap_width)
1233 1234 1235 1236
        {
          current_col = 0;
          current_row++;
        }
1237 1238 1239 1240
    }
  else
    {
      if (priv->col_span_col != -1)
1241 1242 1243
        gtk_tree_model_get (priv->model, iter,
                            priv->col_span_col, &cols,
                            -1);
1244
      if (priv->row_span_col != -1)
1245 1246 1247
        gtk_tree_model_get (priv->model, iter,
                            priv->row_span_col, &rows,
                            -1);
1248 1249

      while (1)
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
        {
          if (current_col + cols > priv->wrap_width)
            {
              current_col = 0;
              current_row++;
            }

          if (!menu_occupied (menu,
                              current_col, current_col + cols,
                              current_row, current_row + rows))
            break;

          current_col++;
        }
1264 1265 1266 1267 1268 1269 1270 1271
    }

  /* set attach props */
  gtk_menu_attach (GTK_MENU (menu), item,
                   current_col, current_col + cols,
                   current_row, current_row + rows);
}

1272 1273
static void
gtk_tree_menu_create_submenu (GtkTreeMenu *menu,
1274 1275
                              GtkWidget   *item,
                              GtkTreePath *path)
1276 1277 1278 1279 1280 1281 1282 1283
{
  GtkTreeMenuPrivate *priv = menu->priv;
  GtkWidget          *view;
  GtkWidget          *submenu;

  view = gtk_bin_get_child (GTK_BIN (item));
  gtk_cell_view_set_draw_sensitive (GTK_CELL_VIEW (view), TRUE);

1284
  submenu = _gtk_tree_menu_new_with_area (priv->area);
1285

1286 1287 1288 1289 1290 1291 1292 1293
  _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (submenu),
                                         priv->row_separator_func,
                                         priv->row_separator_data,
                                         priv->row_separator_destroy);
  _gtk_tree_menu_set_header_func (GTK_TREE_MENU (submenu),
                                  priv->header_func,
                                  priv->header_data,
                                  priv->header_destroy);
1294

1295 1296 1297
  _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (submenu), priv->wrap_width);
  _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (submenu), priv->row_span_col);
  _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (submenu), priv->col_span_col);
1298

1299
  gtk_tree_menu_set_model_internal (GTK_TREE_MENU (submenu), priv->model);
1300
  _gtk_tree_menu_set_root (GTK_TREE_MENU (submenu), path);
1301
  gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
1302 1303 1304

  g_signal_connect (submenu, "menu-activate",
                    G_CALLBACK (submenu_activated_cb), menu);
1305 1306
}

1307 1308
static GtkWidget *
gtk_tree_menu_create_item (GtkTreeMenu *menu,
1309 1310
                           GtkTreeIter *iter,
                           gboolean     header_item)
1311 1312 1313 1314
{
  GtkTreeMenuPrivate *priv = menu->priv;
  GtkWidget          *item, *view;
  GtkTreePath        *path;
1315
  gboolean            is_separator = FALSE;
1316 1317 1318

  path = gtk_tree_model_get_path (priv->model, iter);

1319
  if (priv->row_separator_func)
1320
    is_separator =
1321
      priv->row_separator_func (priv->model, iter,
1322
                                priv->row_separator_data);
1323

1324 1325 1326
  if (is_separator)
    {
      item = gtk_separator_menu_item_new ();
1327
      gtk_widget_show (item);
1328

1329
      g_object_set_qdata_full (G_OBJECT (item),
1330 1331 1332
                               tree_menu_path_quark,
                               gtk_tree_row_reference_new (priv->model, path),
                               (GDestroyNotify)gtk_tree_row_reference_free);
1333 1334 1335 1336 1337 1338 1339