gtkmenubutton.c 28.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* GTK - The GIMP Toolkit
 *
 * Copyright (C) 2003 Ricardo Fernandez Pascual
 * Copyright (C) 2004 Paolo Borelli
 * Copyright (C) 2012 Bastien Nocera
 *
 * 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 License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * SECTION:gtkmenubutton
Matthias Clasen's avatar
Matthias Clasen committed
23 24
 * @short_description: A widget that shows a menu when clicked on
 * @title: GtkMenuButton
25 26 27 28
 *
 * The #GtkMenuButton widget is used to display a menu when clicked on.
 * This menu can be provided either as a #GtkMenu, or an abstract #GMenuModel.
 *
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
 * The #GtkMenuButton widget can hold any valid child widget. That is, it
 * can hold almost any other standard #GtkWidget. The most commonly used
 * child is the provided #GtkArrow.
 *
 * The positioning of the menu is determined by the #GtkMenuButton:direction
 * property of the menu button and the #GtkWidget:halign or #GtkWidget:valign
 * properties of the menu. For example, when the direction is %GTK_ARROW_DOWN
 * and the horizontal alignment is %GTK_ALIGN_START, the menu will be
 * positioned below the button, with the starting edge (depending on the
 * text direction) of the menu aligned with the starting edge of the button.
 * If there is not enough space below the button, the menu is popped up above
 * the button instead. If the alignment would move part of the menu offscreen,
 * it is 'pushed in'.
 *
 * <informaltable>
 *   <tgroup cols="4">
 *     <tbody>
 *       <row>
 *         <entry></entry>
 *         <entry>halign = start</entry>
 *         <entry>halign = center</entry>
 *         <entry>halign = end</entry>
 *       </row>
 *     <row>
 *       <entry>direction = down</entry>
 *       <entry>
 *         <inlinemediaobject>
 *           <imageobject><imagedata fileref="down-start.png" format="PNG"/></imageobject>
 *         </inlinemediaobject>
 *       </entry>
 *       <entry>
 *         <inlinemediaobject>
 *           <imageobject><imagedata fileref="down-center.png" format="PNG"/></imageobject>
 *         </inlinemediaobject>
 *       </entry>
 *       <entry>
 *         <inlinemediaobject>
 *           <imageobject><imagedata fileref="down-end.png" format="PNG"/></imageobject>
 *         </inlinemediaobject>
 *       </entry>
 *     </row>
 *     <row>
 *       <entry>direction = up</entry>
 *       <entry>
 *         <inlinemediaobject>
 *           <imageobject><imagedata fileref="up-start.png" format="PNG"/></imageobject>
 *         </inlinemediaobject>
 *       </entry>
 *       <entry>
 *         <inlinemediaobject>
 *           <imageobject><imagedata fileref="up-center.png" format="PNG"/></imageobject>
 *         </inlinemediaobject>
 *       </entry>
 *       <entry>
 *         <inlinemediaobject>
 *           <imageobject><imagedata fileref="up-end.png" format="PNG"/></imageobject>
 *         </inlinemediaobject>
 *       </entry>
 *      </row>
 *     </tbody>
 *   </tgroup>
 * </informaltable>
 * <informaltable>
 *   <tgroup cols="3">
 *     <tbody>
 *       <row>
 *         <entry></entry>
 *         <entry>direction = left</entry>
 *         <entry>direction = right</entry>
 *       </row>
 *     <row>
 *       <entry>valign = start</entry>
 *       <entry>
 *         <inlinemediaobject>
 *           <imageobject><imagedata fileref="left-start.png" format="PNG"/></imageobject>
 *         </inlinemediaobject>
 *       </entry>
 *       <entry>
 *         <inlinemediaobject>
 *           <imageobject><imagedata fileref="right-start.png" format="PNG"/></imageobject>
 *         </inlinemediaobject>
 *       </entry>
 *     </row>
 *     <row>
 *       <entry>valign = center</entry>
 *       <entry>
 *         <inlinemediaobject>
 *           <imageobject><imagedata fileref="left-center.png" format="PNG"/></imageobject>
 *         </inlinemediaobject>
 *       </entry>
 *       <entry>
 *         <inlinemediaobject>
 *           <imageobject><imagedata fileref="right-center.png" format="PNG"/></imageobject>
 *         </inlinemediaobject>
 *       </entry>
 *     </row>
 *     <row>
 *       <entry>valign = end</entry>
 *       <entry>
 *         <inlinemediaobject>
 *           <imageobject><imagedata fileref="left-end.png" format="PNG"/></imageobject>
 *         </inlinemediaobject>
 *       </entry>
 *       <entry>
 *         <inlinemediaobject>
 *           <imageobject><imagedata fileref="right-end.png" format="PNG"/></imageobject>
 *         </inlinemediaobject>
 *       </entry>
 *      </row>
 *     </tbody>
 *   </tgroup>
 * </informaltable>
141 142 143 144 145 146 147 148 149 150 151 152 153
 */

#include "config.h"

#include "gtkmenubutton.h"
#include "gtkmenubuttonprivate.h"
#include "gtkarrow.h"

#include "gtkprivate.h"
#include "gtkintl.h"

struct _GtkMenuButtonPrivate
{
154
  GtkWidget *popup;
155 156 157 158 159 160 161 162 163 164 165 166 167
  GMenuModel *model;

  GtkMenuButtonShowMenuCallback func;
  gpointer user_data;

  GtkArrowType arrow_type;
  GtkWidget *align_widget;
  gpointer arrow_widget;
};

enum
{
  PROP_0,
168
  PROP_POPUP,
169 170 171 172 173 174 175
  PROP_MODEL,
  PROP_ALIGN_WIDGET,
  PROP_DIRECTION
};

G_DEFINE_TYPE(GtkMenuButton, gtk_menu_button, GTK_TYPE_TOGGLE_BUTTON)

176
static void gtk_menu_button_dispose (GObject *object);
177 178 179

static void
gtk_menu_button_set_property (GObject      *object,
Matthias Clasen's avatar
Matthias Clasen committed
180 181 182
                              guint         property_id,
                              const GValue *value,
                              GParamSpec   *pspec)
183 184 185 186 187
{
  GtkMenuButton *self = GTK_MENU_BUTTON (object);

  switch (property_id)
    {
188 189
      case PROP_POPUP:
        gtk_menu_button_set_popup (self, g_value_get_object (value));
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
        break;
      case PROP_MODEL:
        gtk_menu_button_set_menu_model (self, g_value_get_object (value));
        break;
      case PROP_ALIGN_WIDGET:
        gtk_menu_button_set_align_widget (self, g_value_get_object (value));
        break;
      case PROP_DIRECTION:
        gtk_menu_button_set_direction (self, g_value_get_enum (value));
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
gtk_menu_button_get_property (GObject    *object,
Matthias Clasen's avatar
Matthias Clasen committed
207 208 209
                              guint       property_id,
                              GValue     *value,
                              GParamSpec *pspec)
210 211 212 213 214
{
  GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (object)->priv;

  switch (property_id)
    {
215 216
      case PROP_POPUP:
        g_value_set_object (value, priv->popup);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        break;
      case PROP_MODEL:
        g_value_set_object (value, priv->model);
        break;
      case PROP_ALIGN_WIDGET:
        g_value_set_object (value, priv->align_widget);
        break;
      case PROP_DIRECTION:
        g_value_set_enum (value, priv->arrow_type);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
gtk_menu_button_state_flags_changed (GtkWidget    *widget,
                                     GtkStateFlags previous_state_flags)
{
  GtkMenuButton *button = GTK_MENU_BUTTON (widget);
  GtkMenuButtonPrivate *priv = button->priv;

239 240
  if (!gtk_widget_is_sensitive (widget) && priv->popup)
    gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->popup));
241 242 243
}

static void
244 245 246 247 248
menu_position_up_down_func (GtkMenu       *menu,
                            gint          *x,
                            gint          *y,
                            gboolean      *push_in,
                            GtkMenuButton *menu_button)
249 250 251
{
  GtkMenuButtonPrivate *priv = menu_button->priv;
  GtkWidget *widget = GTK_WIDGET (menu_button);
252
  GtkWidget *toplevel;
253 254 255 256 257 258 259
  GtkRequisition menu_req;
  GtkTextDirection direction;
  GdkRectangle monitor;
  gint monitor_num;
  GdkScreen *screen;
  GdkWindow *window;
  GtkAllocation allocation, arrow_allocation;
260
  GtkAlign align;
261

262 263 264 265 266 267 268 269 270 271
  /* In the common case the menu button is showing a dropdown menu, set the
   * corresponding type hint on the toplevel, so the WM can omit the top side
   * of the shadows.
   */
  if (priv->arrow_type == GTK_ARROW_DOWN)
    {
      toplevel = gtk_widget_get_toplevel (GTK_WIDGET (priv->popup));
      gtk_window_set_type_hint (GTK_WINDOW (toplevel), GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU);
    }

272
  gtk_widget_get_preferred_size (GTK_WIDGET (priv->popup),
273 274
                                 &menu_req, NULL);

275
  align = gtk_widget_get_halign (GTK_WIDGET (priv->popup));
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
  direction = gtk_widget_get_direction (widget);
  window = gtk_widget_get_window (priv->align_widget ? priv->align_widget : widget);

  screen = gtk_widget_get_screen (GTK_WIDGET (menu));
  monitor_num = gdk_screen_get_monitor_at_window (screen, window);
  if (monitor_num < 0)
    monitor_num = 0;
  gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);

  gtk_widget_get_allocation (priv->align_widget ? priv->align_widget : widget, &allocation);
  gtk_widget_get_allocation (widget, &arrow_allocation);

  gdk_window_get_origin (window, x, y);
  *x += allocation.x;
  *y += allocation.y;

292 293 294
  /* treat the default align value like START */
  if (align == GTK_ALIGN_FILL)
    align = GTK_ALIGN_START;
295

296 297 298 299
  if (align == GTK_ALIGN_CENTER)
    *x -= (menu_req.width - allocation.width) / 2;
  else if ((align == GTK_ALIGN_START && direction == GTK_TEXT_DIR_LTR) ||
           (align == GTK_ALIGN_END && direction == GTK_TEXT_DIR_RTL))
300 301 302 303
    *x += MAX (allocation.width - menu_req.width, 0);
  else if (menu_req.width > allocation.width)
    *x -= menu_req.width - allocation.width;

304 305 306 307 308
  if (priv->arrow_type == GTK_ARROW_UP && *y - menu_req.height >= monitor.y)
    {
      *y -= menu_req.height;
    }
  else
309
    {
310 311 312 313 314 315 316 317
      if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
        *y += arrow_allocation.height;
      else if ((*y - menu_req.height) >= monitor.y)
        *y -= menu_req.height;
      else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
        *y += arrow_allocation.height;
      else
        *y -= menu_req.height;
318 319 320 321 322 323
    }

  *push_in = FALSE;
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
324 325 326 327 328
menu_position_side_func (GtkMenu       *menu,
                         gint          *x,
                         gint          *y,
                         gboolean      *push_in,
                         GtkMenuButton *menu_button)
329 330
{
  GtkMenuButtonPrivate *priv = menu_button->priv;
331
  GtkAllocation allocation;
332 333 334 335 336 337
  GtkWidget *widget = GTK_WIDGET (menu_button);
  GtkRequisition menu_req;
  GdkRectangle monitor;
  gint monitor_num;
  GdkScreen *screen;
  GdkWindow *window;
338
  GtkAlign align;
339

340
  gtk_widget_get_preferred_size (GTK_WIDGET (priv->popup),
341 342 343 344
                                 &menu_req, NULL);

  window = gtk_widget_get_window (widget);

345
  align = gtk_widget_get_valign (GTK_WIDGET (menu));
346 347 348 349 350 351 352 353
  screen = gtk_widget_get_screen (GTK_WIDGET (menu));
  monitor_num = gdk_screen_get_monitor_at_window (screen, window);
  if (monitor_num < 0)
    monitor_num = 0;
  gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);

  gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (menu_button)), x, y);

354
  gtk_widget_get_allocation (widget, &allocation);
355 356

  if (priv->arrow_type == GTK_ARROW_RIGHT)
357 358 359 360 361 362
    {
      if (*x + allocation.width + menu_req.width <= monitor.x + monitor.width)
        *x += allocation.width;
      else
        *x -= menu_req.width;
    }
363
  else
364 365 366 367 368 369 370 371 372 373
    {
      if (*x - menu_req.width >= monitor.x)
        *x -= menu_req.width;
      else
        *x += allocation.width;
    }

  /* treat the default align value like START */
  if (align == GTK_ALIGN_FILL)
    align = GTK_ALIGN_START;
374

375 376 377 378
  if (align == GTK_ALIGN_CENTER)
    *y -= (menu_req.height - allocation.height) / 2;
  else if (align == GTK_ALIGN_END)
    *y -= menu_req.height - allocation.height;
379 380 381 382 383 384 385 386 387 388 389 390 391 392

  *push_in = FALSE;
}

static void
popup_menu (GtkMenuButton  *menu_button,
            GdkEventButton *event)
{
  GtkMenuButtonPrivate *priv = menu_button->priv;
  GtkMenuPositionFunc func;

  if (priv->func)
    priv->func (priv->user_data);

393
  if (!priv->popup)
394 395 396 397 398 399 400 401 402
    return;

  switch (priv->arrow_type)
    {
      case GTK_ARROW_LEFT:
      case GTK_ARROW_RIGHT:
        func = (GtkMenuPositionFunc) menu_position_side_func;
        break;
      default:
403
        func = (GtkMenuPositionFunc) menu_position_up_down_func;
404 405 406
        break;
  }

407
  gtk_menu_popup_for_device (GTK_MENU (priv->popup),
408 409
                             event ? event->device : NULL,
                             NULL, NULL,
410 411 412 413 414 415 416 417 418 419 420 421 422
                             func,
                             GTK_WIDGET (menu_button),
                             NULL,
                             event ? event->button : 0,
                             event ? event->time : gtk_get_current_event_time ());
}

static void
gtk_menu_button_toggled (GtkToggleButton *button)
{
  GtkMenuButton *menu_button = GTK_MENU_BUTTON (button);
  GtkMenuButtonPrivate *priv = menu_button->priv;

423
  if (!priv->popup)
424 425 426
    return;

  if (gtk_toggle_button_get_active (button) &&
427
      !gtk_widget_get_visible (GTK_WIDGET (priv->popup)))
428 429 430 431 432
    {
      /* we get here only when the menu is activated by a key
       * press, so that we can select the first menu item
       */
      popup_menu (menu_button, NULL);
433
      gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup), FALSE);
434 435 436 437
    }
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
438 439
gtk_menu_button_button_press_event (GtkWidget      *widget,
                                    GdkEventButton *event)
440 441 442 443 444 445 446 447 448 449 450 451
{
  if (event->button == GDK_BUTTON_PRIMARY)
    {
      popup_menu (GTK_MENU_BUTTON (widget), event);
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);

      return TRUE;
    }

  return GTK_WIDGET_CLASS (gtk_menu_button_parent_class)->button_press_event (widget, event);
}

452 453 454 455 456 457 458
static void
gtk_menu_button_add (GtkContainer *container,
                     GtkWidget    *child)
{
  GtkMenuButton *button = GTK_MENU_BUTTON (container);

  if (button->priv->arrow_widget)
459
    gtk_container_remove (container, button->priv->arrow_widget);
460 461 462 463

  GTK_CONTAINER_CLASS (gtk_menu_button_parent_class)->add (container, child);
}

464 465 466 467 468 469 470 471 472 473 474 475
static void
gtk_menu_button_remove (GtkContainer *container,
                        GtkWidget    *child)
{
  GtkMenuButton *button = GTK_MENU_BUTTON (container);

  if (child == button->priv->arrow_widget)
    button->priv->arrow_widget = NULL;

  GTK_CONTAINER_CLASS (gtk_menu_button_parent_class)->remove (container, child);
}

476 477 478 479 480
static void
gtk_menu_button_class_init (GtkMenuButtonClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
481
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
482 483 484 485 486 487
  GtkToggleButtonClass *toggle_button_class = GTK_TOGGLE_BUTTON_CLASS (klass);

  g_type_class_add_private (klass, sizeof (GtkMenuButtonPrivate));

  gobject_class->set_property = gtk_menu_button_set_property;
  gobject_class->get_property = gtk_menu_button_get_property;
488
  gobject_class->dispose = gtk_menu_button_dispose;
489 490 491 492

  widget_class->state_flags_changed = gtk_menu_button_state_flags_changed;
  widget_class->button_press_event = gtk_menu_button_button_press_event;

493
  container_class->add = gtk_menu_button_add;
494
  container_class->remove = gtk_menu_button_remove;
495

496 497 498
  toggle_button_class->toggled = gtk_menu_button_toggled;

  /**
499 500 501 502 503 504 505 506 507 508 509 510 511 512
   * GtkMenuButton:popup:
   *
   * The #GtkMenu that will be popped up when the button is clicked.
   *
   * Since: 3.6
   */
  g_object_class_install_property (gobject_class,
                                   PROP_POPUP,
                                   g_param_spec_object ("popup",
                                                        P_("popup"),
                                                        P_("The dropdown menu."),
                                                        GTK_TYPE_MENU,
                                                        G_PARAM_READWRITE));
   /**
513 514 515
   * GtkMenuButton:menu:
   *
   * The #GtkMenu that will be popped up when the button is clicked.
516 517
   * This property has been renamed to "popup".  "menu" will be
   * removed before 3.6.0.
518 519 520 521
   *
   * Since: 3.6
   */
  g_object_class_install_property (gobject_class,
522
                                   PROP_POPUP, /* [sic] */
523 524 525 526
                                   g_param_spec_object ("menu",
                                                        P_("menu"),
                                                        P_("The dropdown menu."),
                                                        GTK_TYPE_MENU,
527
                                                        G_PARAM_DEPRECATED | G_PARAM_READWRITE));
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
  /**
   * GtkMenuButton:menu-model:
   *
   * The #GMenuModel from which the menu to pop up will be created.
   * See gtk_menu_button_set_menu_model() for the interaction with the
   * #GtkMenuButton:menu property.
   *
   * Since: 3.6
   */
  g_object_class_install_property (gobject_class,
                                   PROP_MODEL,
                                   g_param_spec_object ("menu-model",
                                                        P_("menu-model"),
                                                        P_("The dropdown menu's model."),
                                                        G_TYPE_MENU_MODEL,
                                                        G_PARAM_READWRITE));
  /**
   * GtkMenuButton:align-widget:
   *
   * The #GtkWidget to use to align the popup menu with.
   *
   * Since: 3.6
   */
  g_object_class_install_property (gobject_class,
                                   PROP_ALIGN_WIDGET,
                                   g_param_spec_object ("align-widget",
                                                        P_("align-widget"),
                                                        P_("The parent widget which the menu should align with."),
                                                        GTK_TYPE_CONTAINER,
                                                        G_PARAM_READWRITE));
  /**
   * GtkMenuButton:direction:
   *
   * The #GtkArrowType representing the direction in which the
   * menu will be popped out.
   *
   * Since: 3.6
   */
  g_object_class_install_property (gobject_class,
                                   PROP_DIRECTION,
                                   g_param_spec_enum ("direction",
                                                      P_("direction"),
                                                      P_("The direction the arrow should point."),
                                                      GTK_TYPE_ARROW_TYPE,
                                                      GTK_ARROW_DOWN,
573
                                                      G_PARAM_READWRITE));
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
}

static void
add_arrow (GtkMenuButton *menu_button)
{
  GtkWidget *arrow;

  arrow = gtk_arrow_new (menu_button->priv->arrow_type, GTK_SHADOW_NONE);
  gtk_container_add (GTK_CONTAINER (menu_button), arrow);
  gtk_widget_show (arrow);
  menu_button->priv->arrow_widget = arrow;
}

static void
gtk_menu_button_init (GtkMenuButton *menu_button)
{
  GtkMenuButtonPrivate *priv;

  priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_button, GTK_TYPE_MENU_BUTTON, GtkMenuButtonPrivate);
  menu_button->priv = priv;
  priv->arrow_type = GTK_ARROW_DOWN;

596 597
  add_arrow (menu_button);

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
  gtk_widget_set_sensitive (GTK_WIDGET (menu_button), FALSE);
}

/**
 * gtk_menu_button_new:
 *
 * Creates a new #GtkMenuButton widget with downwards-pointing
 * arrow as the only child. You can replace the child widget
 * with another #GtkWidget should you wish to.
 *
 * Returns: The newly created #GtkMenuButton widget.
 *
 * Since: 3.6
 */
GtkWidget *
gtk_menu_button_new (void)
{
  return g_object_new (GTK_TYPE_MENU_BUTTON, NULL);
}

/* Callback for the "deactivate" signal on the pop-up menu.
 * This is used so that we unset the state of the toggle button
 * when the pop-up menu disappears.
 */
static int
Matthias Clasen's avatar
Matthias Clasen committed
623
menu_deactivate_cb (GtkMenuShell  *menu_shell,
624 625 626 627 628 629 630 631 632 633 634 635 636
                    GtkMenuButton *menu_button)
{
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu_button), FALSE);

  return TRUE;
}

static void
menu_detacher (GtkWidget *widget,
               GtkMenu   *menu)
{
  GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (widget)->priv;

637
  g_return_if_fail (priv->popup == (GtkWidget *) menu);
638

639
  priv->popup = NULL;
640 641 642 643 644 645 646
}

/* This function is used in GtkMenuToolButton, the call back will
 * be called when GtkMenuToolButton would have emitted the "show-menu"
 * signal.
 */
void
647 648 649 650
_gtk_menu_button_set_popup_with_func (GtkMenuButton                 *menu_button,
                                      GtkWidget                     *menu,
                                      GtkMenuButtonShowMenuCallback  func,
                                      gpointer                       user_data)
651 652 653 654 655 656 657 658 659 660
{
  GtkMenuButtonPrivate *priv;

  g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
  g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);

  priv = menu_button->priv;
  priv->func = func;
  priv->user_data = user_data;

661
  if (priv->popup == GTK_WIDGET (menu))
662 663
    return;

664
  if (priv->popup)
665
    {
666 667
      if (gtk_widget_get_visible (GTK_WIDGET (priv->popup)))
        gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->popup));
668 669
    }

670
  if (priv->popup)
671
    {
672
      g_signal_handlers_disconnect_by_func (priv->popup,
673 674
                                            menu_deactivate_cb,
                                            menu_button);
675
      gtk_menu_detach (GTK_MENU (priv->popup));
676 677
    }

678
  priv->popup = menu;
679

680
  if (priv->popup)
681
    {
682
      gtk_menu_attach_to_widget (GTK_MENU (priv->popup), GTK_WIDGET (menu_button),
683 684 685 686
                                 menu_detacher);

      gtk_widget_set_sensitive (GTK_WIDGET (menu_button), TRUE);

687
      g_signal_connect (priv->popup, "deactivate",
688 689 690 691 692 693 694 695 696 697 698 699
                        G_CALLBACK (menu_deactivate_cb), menu_button);
    }
  else
    {
      gtk_widget_set_sensitive (GTK_WIDGET (menu_button), FALSE);
    }

  g_object_notify (G_OBJECT (menu_button), "menu");
  g_object_notify (G_OBJECT (menu_button), "menu-model");
}

/**
700
 * gtk_menu_button_set_popup:
701
 * @menu_button: a #GtkMenuButton
702
 * @popup: (allow-none): a #GtkMenu
703 704 705 706 707 708 709 710
 *
 * Sets the #GtkMenu that will be popped up when the button is clicked,
 * or %NULL to disable the button. If #GtkMenuButton:menu-model is set,
 * it will be set to %NULL.
 *
 * Since: 3.6
 */
void
711 712
gtk_menu_button_set_popup (GtkMenuButton *menu_button,
                           GtkWidget     *popup)
713 714 715 716
{
  GtkMenuButtonPrivate *priv;

  g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
717
  g_return_if_fail (GTK_IS_MENU (popup) || popup == NULL);
718 719 720 721

  priv = menu_button->priv;
  g_clear_object (&priv->model);

722
  _gtk_menu_button_set_popup_with_func (menu_button, popup, NULL, NULL);
723 724 725
}

/**
726
 * gtk_menu_button_get_popup:
727 728 729 730 731 732 733 734 735
 * @menu_button: a #GtkMenuButton
 *
 * Returns the #GtkMenu that pops out of the button.
 *
 * Returns: (transfer none): a #GtkMenu or %NULL.
 *
 * Since: 3.6
 */
GtkMenu *
736
gtk_menu_button_get_popup (GtkMenuButton *menu_button)
737 738 739
{
  g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);

740 741 742 743 744 745 746 747 748 749 750 751 752 753
  return GTK_MENU (menu_button->priv->popup);
}

void
gtk_menu_button_set_menu (GtkMenuButton *menu_button,
                          GtkWidget     *menu)
{
  gtk_menu_button_set_popup (menu_button, menu);
}

GtkMenu *
gtk_menu_button_get_menu (GtkMenuButton *menu_button)
{
  return gtk_menu_button_get_popup (menu_button);
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
}

/**
 * gtk_menu_button_set_menu_model:
 * @menu_button: a #GtkMenuButton
 * @menu_model: (allow-none): a #GMenuModel
 *
 * Sets the #GMenuModel from which the #GtkMenuButton:menu property will be
 * filled in, or %NULL to disable the button.
 *
 * The #GtkMenu will be created with gtk_menu_new_from_model(), so actions
 * will be connected as documented there.
 *
 * If you #GtkMenuButton:menu * is already set, then its content will be lost
 * and replaced by our newly created #GtkMenu.
 *
 * Since: 3.6
 */
void
gtk_menu_button_set_menu_model (GtkMenuButton *menu_button,
Matthias Clasen's avatar
Matthias Clasen committed
774
                                GMenuModel    *menu_model)
775 776 777 778 779 780 781 782 783 784 785 786
{
  GtkMenuButtonPrivate *priv;
  GtkWidget *menu;

  g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
  g_return_if_fail (G_IS_MENU_MODEL (menu_model) || menu_model == NULL);

  priv = menu_button->priv;
  g_clear_object (&priv->model);

  if (menu_model == NULL)
    {
787
      gtk_menu_button_set_popup (menu_button, NULL);
788 789 790 791 792 793
      return;
    }

  priv->model = g_object_ref (menu_model);
  menu = gtk_menu_new_from_model (menu_model);
  gtk_widget_show_all (menu);
794
  gtk_menu_button_set_popup (menu_button, menu);
795 796 797 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
}

/**
 * gtk_menu_button_get_menu_model:
 * @menu_button: a #GtkMenuButton
 *
 * Returns the #GMenuModel used to generate the menu.
 *
 * Returns: (transfer none): a #GMenuModel or %NULL.
 *
 * Since: 3.6
 */
GMenuModel *
gtk_menu_button_get_menu_model (GtkMenuButton *menu_button)
{
  g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);

  return menu_button->priv->model;
}

/**
 * gtk_menu_button_set_align_widget:
 * @menu_button: a #GtkMenuButton
 * @align_widget: (allow-none): a #GtkWidget
 *
 * Sets the #GtkWidget to use to line the menu with when popped up. Note that
 * the @align_widget must contain the #GtkMenuButton itself.
 *
 * Setting it to %NULL means that the popup menu will be aligned with the
 * button itself.
 *
 * Since: 3.6
 */
void
gtk_menu_button_set_align_widget (GtkMenuButton *menu_button,
Matthias Clasen's avatar
Matthias Clasen committed
830
                                  GtkWidget     *align_widget)
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
{
  GtkMenuButtonPrivate *priv;

  g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
  g_return_if_fail (align_widget == NULL || gtk_widget_is_ancestor (GTK_WIDGET (menu_button), align_widget));

  priv = menu_button->priv;
  if (priv->align_widget == align_widget)
    return;

  priv->align_widget = align_widget;

  if (priv->align_widget)
    g_object_add_weak_pointer (G_OBJECT (priv->align_widget), (gpointer *) &priv->align_widget);

  g_object_notify (G_OBJECT (menu_button), "align-widget");
}

/**
 * gtk_menu_button_get_align_widget:
 * @menu_button: a #GtkMenuButton
 *
 * Returns the parent #GtkWidget to use to line up with menu.
 *
 * Returns: (transfer none): a #GtkWidget value or %NULL.
 *
 * Since: 3.6
 */
GtkWidget *
gtk_menu_button_get_align_widget (GtkMenuButton *menu_button)
{
  g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);

  return menu_button->priv->align_widget;
}

/**
 * gtk_menu_button_set_direction:
 * @menu_button: a #GtkMenuButton
 * @direction: a #GtkArrowType
 *
 * Sets the direction in which the menu will be popped up, as
 * well as changing the arrow's direction. The child will not
 * be changed to an arrow if it was customized.
 *
 * If the menu when popped out would have collided with screen edges,
 * we will do our best to keep it inside the screen and fully visible.
 *
 * If you pass GTK_ARROW_NONE for a @direction, the menu will behave
 * as if you passed GTK_ARROW_DOWN (although you won't see any arrows).
 *
 * Since: 3.6
 */
void
gtk_menu_button_set_direction (GtkMenuButton *menu_button,
                               GtkArrowType   direction)
{
  GtkMenuButtonPrivate *priv = menu_button->priv;
  GtkWidget *child;

  g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));

  if (priv->arrow_type == direction)
    return;

  priv->arrow_type = direction;

898
  /* Is it custom content? We don't change that */
899
  child = gtk_bin_get_child (GTK_BIN (menu_button));
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
  if (priv->arrow_widget != child)
    return;

  gtk_arrow_set (GTK_ARROW (child), priv->arrow_type, GTK_SHADOW_NONE);
}

/**
 * gtk_menu_button_get_direction:
 * @menu_button: a #GtkMenuButton
 *
 * Returns the direction the menu will be pointing at when popped up.
 *
 * Returns: a #GtkArrowType value.
 *
 * Since: 3.6
 */
GtkArrowType
gtk_menu_button_get_direction (GtkMenuButton *menu_button)
{
  g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), GTK_ARROW_DOWN);

  return menu_button->priv->arrow_type;
}

static void
925
gtk_menu_button_dispose (GObject *object)
926 927 928
{
  GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (object)->priv;

929
  if (priv->popup)
930
    {
931
      g_signal_handlers_disconnect_by_func (priv->popup,
932 933
                                            menu_deactivate_cb,
                                            object);
934
      gtk_menu_detach (GTK_MENU (priv->popup));
935 936 937 938
    }

  g_clear_object (&priv->model);

939
  G_OBJECT_CLASS (gtk_menu_button_parent_class)->dispose (object);
940
}