gtkmenubar.c 29.9 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21 22 23 24
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

25 26 27 28 29 30 31 32 33 34 35
/**
 * SECTION:gtkmenubar
 * @Title: GtkMenuBar
 * @Short_description: A subclass of GtkMenuShell which holds GtkMenuItem widgets
 * @See_also: #GtkMenuShell, #GtkMenu, #GtkMenuItem
 *
 * The #GtkMenuBar is a subclass of #GtkMenuShell which contains one or
 * more #GtkMenuItems. The result is a standard menu bar which can hold
 * many menu items.
 */

36
#include "config.h"
37 38 39

#include "gtkmenubar.h"

40
#include "gtkbindings.h"
Elliot Lee's avatar
Elliot Lee committed
41
#include "gtkmain.h"
42
#include "gtkmarshalers.h"
43
#include "gtkmenuitemprivate.h"
44
#include "gtkmenuprivate.h"
45
#include "gtkmenushellprivate.h"
46
#include "gtkrender.h"
Havoc Pennington's avatar
Havoc Pennington committed
47
#include "gtksettings.h"
48
#include "gtksizerequest.h"
Havoc Pennington's avatar
Havoc Pennington committed
49
#include "gtkwindow.h"
50
#include "gtkcontainerprivate.h"
51
#include "gtkintl.h"
52
#include "gtkprivate.h"
53
#include "gtktypebuiltins.h"
54
#include "gtkwidgetprivate.h"
Elliot Lee's avatar
Elliot Lee committed
55

56 57
#define MENU_BAR_POPUP_DELAY 0

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/* Properties */
enum {
  PROP_0,
  PROP_PACK_DIRECTION,
  PROP_CHILD_PACK_DIRECTION
};

struct _GtkMenuBarPrivate
{
  GtkPackDirection pack_direction;
  GtkPackDirection child_pack_direction;
};


static void gtk_menu_bar_set_property      (GObject             *object,
					    guint                prop_id,
					    const GValue        *value,
					    GParamSpec          *pspec);
static void gtk_menu_bar_get_property      (GObject             *object,
					    guint                prop_id,
					    GValue              *value,
					    GParamSpec          *pspec);
80 81 82 83 84 85
static void gtk_menu_bar_get_preferred_width (GtkWidget     *widget,
					      gint          *minimum,
					      gint          *natural);
static void gtk_menu_bar_get_preferred_height (GtkWidget    *widget,
					       gint         *minimum,
					       gint         *natural);
86 87 88 89 90 91 92 93
static void gtk_menu_bar_get_preferred_width_for_height (GtkWidget    *widget,
                                                         gint          height,
                                                         gint         *minimum,
                                                         gint         *natural);
static void gtk_menu_bar_get_preferred_height_for_width (GtkWidget    *widget,
                                                         gint          width,
                                                         gint         *minimum,
                                                         gint         *natural);
94 95
static void gtk_menu_bar_size_allocate     (GtkWidget       *widget,
					    GtkAllocation   *allocation);
96 97
static gint gtk_menu_bar_draw              (GtkWidget       *widget,
                                            cairo_t         *cr);
98 99
static void gtk_menu_bar_hierarchy_changed (GtkWidget       *widget,
					    GtkWidget       *old_toplevel);
100 101 102
static gint gtk_menu_bar_get_popup_delay   (GtkMenuShell    *menu_shell);
static void gtk_menu_bar_move_current      (GtkMenuShell     *menu_shell,
                                            GtkMenuDirectionType direction);
103

Havoc Pennington's avatar
Havoc Pennington committed
104
static GtkShadowType get_shadow_type   (GtkMenuBar      *menubar);
Elliot Lee's avatar
Elliot Lee committed
105

106
G_DEFINE_TYPE_WITH_PRIVATE (GtkMenuBar, gtk_menu_bar, GTK_TYPE_MENU_SHELL)
107

Elliot Lee's avatar
Elliot Lee committed
108 109 110
static void
gtk_menu_bar_class_init (GtkMenuBarClass *class)
{
111
  GObjectClass *gobject_class;
Elliot Lee's avatar
Elliot Lee committed
112 113 114
  GtkWidgetClass *widget_class;
  GtkMenuShellClass *menu_shell_class;

115 116
  GtkBindingSet *binding_set;

117
  gobject_class = (GObjectClass*) class;
Elliot Lee's avatar
Elliot Lee committed
118 119 120
  widget_class = (GtkWidgetClass*) class;
  menu_shell_class = (GtkMenuShellClass*) class;

121 122 123
  gobject_class->get_property = gtk_menu_bar_get_property;
  gobject_class->set_property = gtk_menu_bar_set_property;

124 125
  widget_class->get_preferred_width = gtk_menu_bar_get_preferred_width;
  widget_class->get_preferred_height = gtk_menu_bar_get_preferred_height;
126 127
  widget_class->get_preferred_width_for_height = gtk_menu_bar_get_preferred_width_for_height;
  widget_class->get_preferred_height_for_width = gtk_menu_bar_get_preferred_height_for_width;
Elliot Lee's avatar
Elliot Lee committed
128
  widget_class->size_allocate = gtk_menu_bar_size_allocate;
129
  widget_class->draw = gtk_menu_bar_draw;
Havoc Pennington's avatar
Havoc Pennington committed
130 131
  widget_class->hierarchy_changed = gtk_menu_bar_hierarchy_changed;
  
132 133
  gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_MENU_BAR);

Elliot Lee's avatar
Elliot Lee committed
134
  menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
135
  menu_shell_class->get_popup_delay = gtk_menu_bar_get_popup_delay;
136
  menu_shell_class->move_current = gtk_menu_bar_move_current;
137 138 139

  binding_set = gtk_binding_set_by_class (class);
  gtk_binding_entry_add_signal (binding_set,
140
				GDK_KEY_Left, 0,
141
				"move-current", 1,
142 143
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_PREV);
144
  gtk_binding_entry_add_signal (binding_set,
145
				GDK_KEY_KP_Left, 0,
146
				"move-current", 1,
147 148
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_PREV);
149
  gtk_binding_entry_add_signal (binding_set,
150
				GDK_KEY_Right, 0,
151
				"move-current", 1,
152 153
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_NEXT);
154
  gtk_binding_entry_add_signal (binding_set,
155
				GDK_KEY_KP_Right, 0,
156
				"move-current", 1,
157 158
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_NEXT);
159
  gtk_binding_entry_add_signal (binding_set,
160
				GDK_KEY_Up, 0,
161
				"move-current", 1,
162 163
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_PARENT);
164
  gtk_binding_entry_add_signal (binding_set,
165
				GDK_KEY_KP_Up, 0,
166
				"move-current", 1,
167 168
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_PARENT);
169
  gtk_binding_entry_add_signal (binding_set,
170
				GDK_KEY_Down, 0,
171
				"move-current", 1,
172 173
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_CHILD);
174
  gtk_binding_entry_add_signal (binding_set,
175
				GDK_KEY_KP_Down, 0,
176
				"move-current", 1,
177 178
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_CHILD);
Havoc Pennington's avatar
Havoc Pennington committed
179

180 181 182 183 184 185 186 187 188
  /**
   * GtkMenuBar:pack-direction:
   *
   * The pack direction of the menubar. It determines how
   * menuitems are arranged in the menubar.
   *
   * Since: 2.8
   */
  g_object_class_install_property (gobject_class,
189 190 191 192 193 194 195
                                   PROP_PACK_DIRECTION,
                                   g_param_spec_enum ("pack-direction",
                                                      P_("Pack direction"),
                                                      P_("The pack direction of the menubar"),
                                                      GTK_TYPE_PACK_DIRECTION,
                                                      GTK_PACK_DIRECTION_LTR,
                                                      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
196 197 198 199
  
  /**
   * GtkMenuBar:child-pack-direction:
   *
Matthias Clasen's avatar
Matthias Clasen committed
200
   * The child pack direction of the menubar. It determines how
201 202 203 204 205
   * the widgets contained in child menuitems are arranged.
   *
   * Since: 2.8
   */
  g_object_class_install_property (gobject_class,
206 207 208 209 210 211 212
                                   PROP_CHILD_PACK_DIRECTION,
                                   g_param_spec_enum ("child-pack-direction",
                                                      P_("Child Pack direction"),
                                                      P_("The child pack direction of the menubar"),
                                                      GTK_TYPE_PACK_DIRECTION,
                                                      GTK_PACK_DIRECTION_LTR,
                                                      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
213 214
  

Havoc Pennington's avatar
Havoc Pennington committed
215
  gtk_widget_class_install_style_property (widget_class,
216
					   g_param_spec_enum ("shadow-type",
217 218
                                                              P_("Shadow type"),
                                                              P_("Style of bevel around the menubar"),
Havoc Pennington's avatar
Havoc Pennington committed
219 220
                                                              GTK_TYPE_SHADOW_TYPE,
                                                              GTK_SHADOW_OUT,
221
                                                              GTK_PARAM_READABLE));
222

223 224 225 226 227 228 229 230 231
  /**
   * GtkMenuBar:internal-padding:
   *
   * Amount of border space between the menubar shadow and the menu items
   *
   * Deprecated: 3.8: use the standard padding CSS property (through objects
   *   like #GtkStyleContext and #GtkCssProvider); the value of this style
   *   property is ignored.
   */
232
  gtk_widget_class_install_style_property (widget_class,
233
					   g_param_spec_int ("internal-padding",
234 235
							     P_("Internal padding"),
							     P_("Amount of border space between the menubar shadow and the menu items"),
236 237
							     0,
							     G_MAXINT,
238 239 240
                                                             0,
                                                             GTK_PARAM_READABLE |
                                                             G_PARAM_DEPRECATED));
241
}
242 243

static void
244
gtk_menu_bar_init (GtkMenuBar *menu_bar)
245
{
246 247
  GtkStyleContext *context;

248
  menu_bar->priv = gtk_menu_bar_get_instance_private (menu_bar);
249 250 251

  context = gtk_widget_get_style_context (GTK_WIDGET (menu_bar));
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUBAR);
252 253
}

254 255 256 257 258 259 260
/**
 * gtk_menu_bar_new:
 *
 * Creates a new #GtkMenuBar
 *
 * Returns: the new menu bar, as a #GtkWidget
 */
Elliot Lee's avatar
Elliot Lee committed
261
GtkWidget*
262
gtk_menu_bar_new (void)
Elliot Lee's avatar
Elliot Lee committed
263
{
Manish Singh's avatar
Manish Singh committed
264
  return g_object_new (GTK_TYPE_MENU_BAR, NULL);
Elliot Lee's avatar
Elliot Lee committed
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
static void
gtk_menu_bar_set_property (GObject      *object,
			   guint         prop_id,
			   const GValue *value,
			   GParamSpec   *pspec)
{
  GtkMenuBar *menubar = GTK_MENU_BAR (object);
  
  switch (prop_id)
    {
    case PROP_PACK_DIRECTION:
      gtk_menu_bar_set_pack_direction (menubar, g_value_get_enum (value));
      break;
    case PROP_CHILD_PACK_DIRECTION:
      gtk_menu_bar_set_child_pack_direction (menubar, g_value_get_enum (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_menu_bar_get_property (GObject    *object,
			   guint       prop_id,
			   GValue     *value,
			   GParamSpec *pspec)
{
  GtkMenuBar *menubar = GTK_MENU_BAR (object);
  
  switch (prop_id)
    {
    case PROP_PACK_DIRECTION:
      g_value_set_enum (value, gtk_menu_bar_get_pack_direction (menubar));
      break;
    case PROP_CHILD_PACK_DIRECTION:
      g_value_set_enum (value, gtk_menu_bar_get_child_pack_direction (menubar));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

Elliot Lee's avatar
Elliot Lee committed
311 312
static void
gtk_menu_bar_size_request (GtkWidget      *widget,
313
                           GtkOrientation  orientation,
314
                           gint            size,
315 316
                           gint           *minimum,
                           gint           *natural)
Elliot Lee's avatar
Elliot Lee committed
317 318
{
  GtkMenuBar *menu_bar;
319
  GtkMenuBarPrivate *priv;
Elliot Lee's avatar
Elliot Lee committed
320 321 322
  GtkMenuShell *menu_shell;
  GtkWidget *child;
  GList *children;
323
  guint border_width;
324
  gboolean use_toggle_size, use_maximize;
325
  gint child_minimum, child_natural;
326 327 328
  GtkStyleContext *context;
  GtkBorder border;
  GtkStateFlags flags;
329 330 331

  *minimum = 0;
  *natural = 0;
Elliot Lee's avatar
Elliot Lee committed
332

333 334 335 336 337 338
  menu_bar = GTK_MENU_BAR (widget);
  menu_shell = GTK_MENU_SHELL (widget);
  priv = menu_bar->priv;

  children = menu_shell->priv->children;

339 340 341 342 343 344
  if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
      priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
    use_toggle_size = (orientation == GTK_ORIENTATION_HORIZONTAL);
  else
    use_toggle_size = (orientation == GTK_ORIENTATION_VERTICAL);

345 346 347 348 349 350
  if (priv->pack_direction == GTK_PACK_DIRECTION_LTR ||
      priv->pack_direction == GTK_PACK_DIRECTION_RTL)
    use_maximize = (orientation == GTK_ORIENTATION_VERTICAL);
  else
    use_maximize = (orientation == GTK_ORIENTATION_HORIZONTAL);

351 352 353 354 355 356 357
  while (children)
    {
      child = children->data;
      children = children->next;

      if (gtk_widget_get_visible (child))
        {
358
          _gtk_widget_get_preferred_size_for_size (child, orientation, size, &child_minimum, &child_natural, NULL, NULL);
359

360 361 362 363 364 365 366
          if (use_toggle_size)
            {
              gint toggle_size;

              gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
                                                 &toggle_size);

367 368
              child_minimum += toggle_size;
              child_natural += toggle_size;
369
            }
370

371
          if (use_maximize)
372 373 374 375
            {
              *minimum = MAX (*minimum, child_minimum);
              *natural = MAX (*natural, child_natural);
            }
376
          else
377 378 379 380
            {
              *minimum += child_minimum;
              *natural += child_natural;
            }
381 382 383
        }
    }

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
  context = gtk_widget_get_style_context (widget);
  flags = gtk_widget_get_state_flags (widget);
  gtk_style_context_get_padding (context, flags, &border);

  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      *minimum += border.left + border.right;
      *natural += border.left + border.right;
    }
  else
    {
      *minimum += border.top + border.bottom;
      *natural += border.top + border.bottom;
    }

399
  border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_bar));
400 401
  *minimum += border_width * 2;
  *natural += border_width * 2;
402 403

  if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
Elliot Lee's avatar
Elliot Lee committed
404
    {
405
      gtk_style_context_get_border (context, flags, &border);
406

407
      if (orientation == GTK_ORIENTATION_HORIZONTAL)
408
        {
409 410
          *minimum += border.left + border.right;
          *natural += border.left + border.right;
411
        }
412
      else
413
        {
414 415
          *minimum += border.top + border.bottom;
          *natural += border.top + border.bottom;
416
        }
Elliot Lee's avatar
Elliot Lee committed
417 418 419
    }
}

420 421 422 423 424
static void
gtk_menu_bar_get_preferred_width (GtkWidget *widget,
				  gint      *minimum,
				  gint      *natural)
{
425
  gtk_menu_bar_size_request (widget, GTK_ORIENTATION_HORIZONTAL, -1, minimum, natural);
426 427 428 429 430 431 432
}

static void
gtk_menu_bar_get_preferred_height (GtkWidget *widget,
				   gint      *minimum,
				   gint      *natural)
{
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
  gtk_menu_bar_size_request (widget, GTK_ORIENTATION_VERTICAL, -1, minimum, natural);
}

static void
gtk_menu_bar_get_preferred_width_for_height (GtkWidget *widget,
                                             gint       height,
                                             gint      *minimum,
                                             gint      *natural)
{
  gtk_menu_bar_size_request (widget, GTK_ORIENTATION_HORIZONTAL, height, minimum, natural);
}

static void
gtk_menu_bar_get_preferred_height_for_width (GtkWidget *widget,
                                             gint       width,
                                             gint      *minimum,
                                             gint      *natural)
{
  gtk_menu_bar_size_request (widget, GTK_ORIENTATION_VERTICAL, width, minimum, natural);
452 453
}

Elliot Lee's avatar
Elliot Lee committed
454 455 456 457 458 459
static void
gtk_menu_bar_size_allocate (GtkWidget     *widget,
			    GtkAllocation *allocation)
{
  GtkMenuBar *menu_bar;
  GtkMenuShell *menu_shell;
460
  GtkMenuBarPrivate *priv;
Elliot Lee's avatar
Elliot Lee committed
461 462
  GtkWidget *child;
  GList *children;
463
  GtkAllocation remaining_space;
464
  guint border_width;
465 466 467
  GArray *requested_sizes;
  gint toggle_size;
  guint i;
468

Elliot Lee's avatar
Elliot Lee committed
469 470 471 472 473
  g_return_if_fail (GTK_IS_MENU_BAR (widget));
  g_return_if_fail (allocation != NULL);

  menu_bar = GTK_MENU_BAR (widget);
  menu_shell = GTK_MENU_SHELL (widget);
474
  priv = menu_bar->priv;
Elliot Lee's avatar
Elliot Lee committed
475

476 477
  gtk_widget_set_allocation (widget, allocation);

478
  if (gtk_widget_get_realized (widget))
479
    gdk_window_move_resize (gtk_widget_get_window (widget),
Elliot Lee's avatar
Elliot Lee committed
480 481 482
			    allocation->x, allocation->y,
			    allocation->width, allocation->height);

483
  if (menu_shell->priv->children)
Elliot Lee's avatar
Elliot Lee committed
484
    {
485 486 487 488 489 490 491 492
      GtkStyleContext *context;
      GtkStateFlags flags;
      GtkBorder border;

      context = gtk_widget_get_style_context (widget);
      flags = gtk_widget_get_state_flags (widget);
      gtk_style_context_get_padding (context, flags, &border);

493
      border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_bar));
494

495 496
      remaining_space.x = (border_width + border.left);
      remaining_space.y = (border_width + border.top);
497
      remaining_space.width = allocation->width -
Matthias Clasen's avatar
Matthias Clasen committed
498
        2 * border_width - border.left - border.right;
499
      remaining_space.height = allocation->height -
Matthias Clasen's avatar
Matthias Clasen committed
500
        2 * border_width - border.top - border.bottom;
501

502 503
      if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
	{
504
          gtk_style_context_get_border (context, flags, &border);
505

506 507 508 509
          remaining_space.x += border.left;
          remaining_space.y += border.top;
          remaining_space.width -= border.left + border.right;
          remaining_space.height -= border.top + border.bottom;
510 511
	}
      
512 513
      requested_sizes = g_array_new (FALSE, FALSE, sizeof (GtkRequestedSize));

514 515
      if (priv->pack_direction == GTK_PACK_DIRECTION_LTR ||
	  priv->pack_direction == GTK_PACK_DIRECTION_RTL)
Elliot Lee's avatar
Elliot Lee committed
516
	{
517 518
          int size = remaining_space.width;
          gboolean ltr = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) == (priv->pack_direction == GTK_PACK_DIRECTION_LTR);
519

520
          for (children = menu_shell->priv->children; children; children = children->next)
521
            {
522
              GtkRequestedSize request;
523
	      child = children->data;
524 525 526 527 528 529 530 531 532

	      if (!gtk_widget_get_visible (child))
                continue;

              request.data = child;
              gtk_widget_get_preferred_width_for_height (child, 
                                                         remaining_space.height,
                                                         &request.minimum_size, 
                                                         &request.natural_size);
533 534
	      gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
						 &toggle_size);
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
              request.minimum_size += toggle_size;
              request.natural_size += toggle_size;

	      gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child), toggle_size);

              g_array_append_val (requested_sizes, request);

              size -= request.minimum_size;
            }

          size = gtk_distribute_natural_allocation (size,
                                                    requested_sizes->len,
                                                    (GtkRequestedSize *) requested_sizes->data);

          for (i = 0; i < requested_sizes->len; i++)
            {
              GtkAllocation child_allocation = remaining_space;
              GtkRequestedSize *request = &g_array_index (requested_sizes, GtkRequestedSize, i);

              child_allocation.width = request->minimum_size;
              remaining_space.width -= request->minimum_size;

	      if (i + 1 == requested_sizes->len && GTK_IS_MENU_ITEM (request->data) &&
558
                  GTK_MENU_ITEM (request->data)->priv->right_justify)
559 560 561 562 563 564 565 566 567
                ltr = !ltr;

              if (ltr)
                remaining_space.x += request->minimum_size;
              else
                child_allocation.x += remaining_space.width;

              gtk_widget_size_allocate (request->data, &child_allocation);
            }
568 569 570
	}
      else
	{
571 572
          int size = remaining_space.height;
          gboolean ttb = (priv->pack_direction == GTK_PACK_DIRECTION_TTB);
573

574
          for (children = menu_shell->priv->children; children; children = children->next)
575
            {
576
              GtkRequestedSize request;
577
	      child = children->data;
578 579 580 581 582 583 584 585 586

	      if (!gtk_widget_get_visible (child))
                continue;

              request.data = child;
              gtk_widget_get_preferred_height_for_width (child, 
                                                         remaining_space.width,
                                                         &request.minimum_size, 
                                                         &request.natural_size);
587 588
	      gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
						 &toggle_size);
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
              request.minimum_size += toggle_size;
              request.natural_size += toggle_size;

	      gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child), toggle_size);

              g_array_append_val (requested_sizes, request);

              size -= request.minimum_size;
            }

          size = gtk_distribute_natural_allocation (size,
                                                    requested_sizes->len,
                                                    (GtkRequestedSize *) requested_sizes->data);

          for (i = 0; i < requested_sizes->len; i++)
            {
              GtkAllocation child_allocation = remaining_space;
              GtkRequestedSize *request = &g_array_index (requested_sizes, GtkRequestedSize, i);

              child_allocation.height = request->minimum_size;
              remaining_space.height -= request->minimum_size;

	      if (i + 1 == requested_sizes->len && GTK_IS_MENU_ITEM (request->data) &&
612
                  GTK_MENU_ITEM (request->data)->priv->right_justify)
613 614 615 616 617 618 619 620 621
                ttb = !ttb;

              if (ttb)
                remaining_space.y += request->minimum_size;
              else
                child_allocation.y += remaining_space.height;

              gtk_widget_size_allocate (request->data, &child_allocation);
            }
Elliot Lee's avatar
Elliot Lee committed
622
	}
623 624

      g_array_free (requested_sizes, TRUE);
Elliot Lee's avatar
Elliot Lee committed
625 626 627 628
    }
}

static gint
629 630
gtk_menu_bar_draw (GtkWidget *widget,
		   cairo_t   *cr)
Elliot Lee's avatar
Elliot Lee committed
631
{
632
  GtkStyleContext *context;
633
  int border;
Elliot Lee's avatar
Elliot Lee committed
634

635
  border = gtk_container_get_border_width (GTK_CONTAINER (widget));
636 637
  context = gtk_widget_get_style_context (widget);

638 639 640 641 642
  gtk_render_background (context, cr,
                         border, border,
                         gtk_widget_get_allocated_width (widget) - border * 2,
                         gtk_widget_get_allocated_height (widget) - border * 2);

643
  if (get_shadow_type (GTK_MENU_BAR (widget)) != GTK_SHADOW_NONE)
644 645 646 647
    gtk_render_frame (context, cr,
                      border, border,
                      gtk_widget_get_allocated_width (widget) - border * 2,
                      gtk_widget_get_allocated_height (widget) - border * 2);
648 649

  GTK_WIDGET_CLASS (gtk_menu_bar_parent_class)->draw (widget, cr);
Elliot Lee's avatar
Elliot Lee committed
650 651 652

  return FALSE;
}
Havoc Pennington's avatar
Havoc Pennington committed
653

654 655 656 657 658 659
static GList *
get_menu_bars (GtkWindow *window)
{
  return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
}

660 661
GList *
_gtk_menu_bar_get_viewable_menu_bars (GtkWindow *window)
662 663 664 665 666 667 668 669 670 671 672 673 674
{
  GList *menu_bars;
  GList *viewable_menu_bars = NULL;

  for (menu_bars = get_menu_bars (window);
       menu_bars;
       menu_bars = menu_bars->next)
    {
      GtkWidget *widget = menu_bars->data;
      gboolean viewable = TRUE;
      
      while (widget)
	{
675
	  if (!gtk_widget_get_mapped (widget))
676
	    viewable = FALSE;
677 678

          widget = gtk_widget_get_parent (widget);
679 680 681 682 683 684 685 686 687
	}

      if (viewable)
	viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
    }

  return g_list_reverse (viewable_menu_bars);
}

688 689 690 691
static void
set_menu_bars (GtkWindow *window,
	       GList     *menubars)
{
692
  g_object_set_data (G_OBJECT (window), I_("gtk-menu-bar-list"), menubars);
693 694
}

Havoc Pennington's avatar
Havoc Pennington committed
695 696 697 698
static void
add_to_window (GtkWindow  *window,
               GtkMenuBar *menubar)
{
699
  GList *menubars = get_menu_bars (window);
Havoc Pennington's avatar
Havoc Pennington committed
700

701
  set_menu_bars (window, g_list_prepend (menubars, menubar));
Havoc Pennington's avatar
Havoc Pennington committed
702 703 704 705 706 707
}

static void
remove_from_window (GtkWindow  *window,
                    GtkMenuBar *menubar)
{
708 709 710 711
  GList *menubars = get_menu_bars (window);

  menubars = g_list_remove (menubars, menubar);
  set_menu_bars (window, menubars);
Havoc Pennington's avatar
Havoc Pennington committed
712 713 714
}

static void
715 716
gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
				GtkWidget *old_toplevel)
Havoc Pennington's avatar
Havoc Pennington committed
717 718 719 720 721 722 723 724
{
  GtkWidget *toplevel;  
  GtkMenuBar *menubar;

  menubar = GTK_MENU_BAR (widget);

  toplevel = gtk_widget_get_toplevel (widget);

725
  if (old_toplevel)
726
    remove_from_window (GTK_WINDOW (old_toplevel), menubar);
Havoc Pennington's avatar
Havoc Pennington committed
727
  
728
  if (gtk_widget_is_toplevel (toplevel))
729
    add_to_window (GTK_WINDOW (toplevel), menubar);
Havoc Pennington's avatar
Havoc Pennington committed
730 731
}

732 733 734 735 736 737 738 739 740 741
/**
 * _gtk_menu_bar_cycle_focus:
 * @menubar: a #GtkMenuBar
 * @dir: direction in which to cycle the focus
 * 
 * Move the focus between menubars in the toplevel.
 **/
void
_gtk_menu_bar_cycle_focus (GtkMenuBar       *menubar,
			   GtkDirectionType  dir)
742 743
{
  GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
744
  GtkMenuItem *to_activate = NULL;
745

746
  if (gtk_widget_is_toplevel (toplevel))
747
    {
748
      GList *tmp_menubars = _gtk_menu_bar_get_viewable_menu_bars (GTK_WINDOW (toplevel));
749
      GList *menubars;
750 751
      GList *current;

752
      menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
753
					    dir, GTK_WIDGET (menubar));
754
      g_list_free (tmp_menubars);
755 756 757 758

      if (menubars)
	{
	  current = g_list_find (menubars, menubar);
759

760 761
	  if (current && current->next)
	    {
762
	      GtkMenuShell *new_menushell = GTK_MENU_SHELL (current->next->data);
763 764
	      if (new_menushell->priv->children)
		to_activate = new_menushell->priv->children->data;
765 766 767 768 769
	    }
	}
	  
      g_list_free (menubars);
    }
770

771
  gtk_menu_shell_cancel (GTK_MENU_SHELL (menubar));
772 773 774

  if (to_activate)
    g_signal_emit_by_name (to_activate, "activate_item");
775 776
}

Havoc Pennington's avatar
Havoc Pennington committed
777 778 779 780 781 782
static GtkShadowType
get_shadow_type (GtkMenuBar *menubar)
{
  GtkShadowType shadow_type = GTK_SHADOW_OUT;
  
  gtk_widget_style_get (GTK_WIDGET (menubar),
783
			"shadow-type", &shadow_type,
Havoc Pennington's avatar
Havoc Pennington committed
784 785 786 787
			NULL);

  return shadow_type;
}
788 789 790 791

static gint
gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell)
{
792
  return MENU_BAR_POPUP_DELAY;
793
}
794 795 796

static void
gtk_menu_bar_move_current (GtkMenuShell         *menu_shell,
797
			   GtkMenuDirectionType  direction)
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
{
  GtkMenuBar *menubar = GTK_MENU_BAR (menu_shell);
  GtkTextDirection text_dir;
  GtkPackDirection pack_dir;

  text_dir = gtk_widget_get_direction (GTK_WIDGET (menubar));
  pack_dir = gtk_menu_bar_get_pack_direction (menubar);
  
  if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
     {
      if ((text_dir == GTK_TEXT_DIR_RTL) == (pack_dir == GTK_PACK_DIRECTION_LTR))
	{
	  switch (direction) 
	    {      
	    case GTK_MENU_DIR_PREV:
	      direction = GTK_MENU_DIR_NEXT;
	      break;
	    case GTK_MENU_DIR_NEXT:
	      direction = GTK_MENU_DIR_PREV;
	      break;
	    default: ;
	    }
	}
    }
  else
    {
      switch (direction) 
	{
	case GTK_MENU_DIR_PARENT:
	  if ((text_dir == GTK_TEXT_DIR_LTR) == (pack_dir == GTK_PACK_DIRECTION_TTB))
	    direction = GTK_MENU_DIR_PREV;
	  else
	    direction = GTK_MENU_DIR_NEXT;
	  break;
	case GTK_MENU_DIR_CHILD:
	  if ((text_dir == GTK_TEXT_DIR_LTR) == (pack_dir == GTK_PACK_DIRECTION_TTB))
	    direction = GTK_MENU_DIR_NEXT;
	  else
	    direction = GTK_MENU_DIR_PREV;
	  break;
	case GTK_MENU_DIR_PREV:
	  if (text_dir == GTK_TEXT_DIR_RTL)	  
	    direction = GTK_MENU_DIR_CHILD;
	  else
	    direction = GTK_MENU_DIR_PARENT;
	  break;
	case GTK_MENU_DIR_NEXT:
	  if (text_dir == GTK_TEXT_DIR_RTL)	  
	    direction = GTK_MENU_DIR_PARENT;
	  else
	    direction = GTK_MENU_DIR_CHILD;
	  break;
	default: ;
	}
    }
  
854
  GTK_MENU_SHELL_CLASS (gtk_menu_bar_parent_class)->move_current (menu_shell, direction);
855 856 857 858 859 860
}

/**
 * gtk_menu_bar_get_pack_direction:
 * @menubar: a #GtkMenuBar
 * 
Matthias Clasen's avatar
Matthias Clasen committed
861 862
 * Retrieves the current pack direction of the menubar. 
 * See gtk_menu_bar_set_pack_direction().
863
 *
864
 * Returns: the pack direction
865 866
 *
 * Since: 2.8
Matthias Clasen's avatar
Matthias Clasen committed
867
 */
868 869 870 871 872 873
GtkPackDirection
gtk_menu_bar_get_pack_direction (GtkMenuBar *menubar)
{
  g_return_val_if_fail (GTK_IS_MENU_BAR (menubar), 
			GTK_PACK_DIRECTION_LTR);

874
  return menubar->priv->pack_direction;
875 876 877 878
}

/**
 * gtk_menu_bar_set_pack_direction:
Matthias Clasen's avatar
Matthias Clasen committed
879 880
 * @menubar: a #GtkMenuBar
 * @pack_dir: a new #GtkPackDirection
881 882 883 884
 * 
 * Sets how items should be packed inside a menubar.
 * 
 * Since: 2.8
Matthias Clasen's avatar
Matthias Clasen committed
885
 */
886 887 888
void
gtk_menu_bar_set_pack_direction (GtkMenuBar       *menubar,
                                 GtkPackDirection  pack_dir)
889 890
{
  GtkMenuBarPrivate *priv;
891
  GList *l;
892 893 894

  g_return_if_fail (GTK_IS_MENU_BAR (menubar));

895
  priv = menubar->priv;
896 897 898 899 900 901

  if (priv->pack_direction != pack_dir)
    {
      priv->pack_direction = pack_dir;

      gtk_widget_queue_resize (GTK_WIDGET (menubar));
902

903
      for (l = GTK_MENU_SHELL (menubar)->priv->children; l; l = l->next)
904 905
	gtk_widget_queue_resize (GTK_WIDGET (l->data));

906 907 908 909 910 911 912 913
      g_object_notify (G_OBJECT (menubar), "pack-direction");
    }
}

/**
 * gtk_menu_bar_get_child_pack_direction:
 * @menubar: a #GtkMenuBar
 * 
Matthias Clasen's avatar
Matthias Clasen committed
914 915
 * Retrieves the current child pack direction of the menubar.
 * See gtk_menu_bar_set_child_pack_direction().
916
 *
917
 * Returns: the child pack direction
918 919
 *
 * Since: 2.8
Matthias Clasen's avatar
Matthias Clasen committed
920
 */
921 922 923 924 925 926
GtkPackDirection
gtk_menu_bar_get_child_pack_direction (GtkMenuBar *menubar)
{
  g_return_val_if_fail (GTK_IS_MENU_BAR (menubar), 
			GTK_PACK_DIRECTION_LTR);

927
  return menubar->priv->child_pack_direction;
928 929 930 931
}

/**
 * gtk_menu_bar_set_child_pack_direction:
Matthias Clasen's avatar
Matthias Clasen committed
932 933
 * @menubar: a #GtkMenuBar
 * @child_pack_dir: a new #GtkPackDirection
934 935 936 937
 * 
 * Sets how widgets should be packed inside the children of a menubar.
 * 
 * Since: 2.8
Matthias Clasen's avatar
Matthias Clasen committed
938
 */
939 940 941
void
gtk_menu_bar_set_child_pack_direction (GtkMenuBar       *menubar,
                                       GtkPackDirection  child_pack_dir)
942 943
{
  GtkMenuBarPrivate *priv;
944
  GList *l;
945 946 947

  g_return_if_fail (GTK_IS_MENU_BAR (menubar));

948
  priv = menubar->priv;
949 950 951 952 953 954

  if (priv->child_pack_direction != child_pack_dir)
    {
      priv->child_pack_direction = child_pack_dir;

      gtk_widget_queue_resize (GTK_WIDGET (menubar));
955

956
      for (l = GTK_MENU_SHELL (menubar)->priv->children; l; l = l->next)
957 958
	gtk_widget_queue_resize (GTK_WIDGET (l->data));

959 960 961
      g_object_notify (G_OBJECT (menubar), "child-pack-direction");
    }
}
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990

/**
 * gtk_menu_bar_new_from_model:
 * @model: a #GMenuModel
 *
 * Creates a new #GtkMenuBar and populates it with menu items
 * and submenus according to @model.
 *
 * The created menu items are connected to actions found in the
 * #GtkApplicationWindow to which the menu bar belongs - typically
 * by means of being contained within the #GtkApplicationWindows
 * widget hierarchy.
 *
 * Returns: a new #GtkMenuBar
 *
 * Since: 3.4
 */
GtkWidget *
gtk_menu_bar_new_from_model (GMenuModel *model)
{
  GtkWidget *menubar;

  g_return_val_if_fail (G_IS_MENU_MODEL (model), NULL);

  menubar = gtk_menu_bar_new ();
  gtk_menu_shell_bind_model (GTK_MENU_SHELL (menubar), model, NULL, FALSE);

  return menubar;
}