1 /* GTK - The GIMP Toolkit
3 * Copyright (C) 2003 Ricardo Fernandez Pascual
4 * Copyright (C) 2004 Paolo Borelli
5 * Copyright (C) 2012 Bastien Nocera
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 * SECTION:gtkmenubutton
23 * @short_description: A widget that shows a menu when clicked on
24 * @title: GtkMenuButton
26 * The #GtkMenuButton widget is used to display a menu when clicked on.
27 * This menu can be provided either as a #GtkMenu, or an abstract #GMenuModel.
29 * The #GtkMenuButton widget can hold any valid child widget. That is, it can hold
30 * almost any other standard #GtkWidget. The most commonly used child is the
36 #include "gtkmenubutton.h"
37 #include "gtkmenubuttonprivate.h"
40 #include "gtkprivate.h"
43 struct _GtkMenuButtonPrivate
48 GtkMenuButtonShowMenuCallback func;
51 GtkArrowType arrow_type;
52 GtkWidget *align_widget;
53 gpointer arrow_widget;
65 G_DEFINE_TYPE(GtkMenuButton, gtk_menu_button, GTK_TYPE_TOGGLE_BUTTON)
67 static void gtk_menu_button_dispose (GObject *object);
70 gtk_menu_button_set_property (GObject *object,
75 GtkMenuButton *self = GTK_MENU_BUTTON (object);
80 gtk_menu_button_set_menu (self, g_value_get_object (value));
83 gtk_menu_button_set_menu_model (self, g_value_get_object (value));
85 case PROP_ALIGN_WIDGET:
86 gtk_menu_button_set_align_widget (self, g_value_get_object (value));
89 gtk_menu_button_set_direction (self, g_value_get_enum (value));
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
97 gtk_menu_button_get_property (GObject *object,
102 GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (object)->priv;
107 g_value_set_object (value, priv->menu);
110 g_value_set_object (value, priv->model);
112 case PROP_ALIGN_WIDGET:
113 g_value_set_object (value, priv->align_widget);
116 g_value_set_enum (value, priv->arrow_type);
119 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
124 gtk_menu_button_state_flags_changed (GtkWidget *widget,
125 GtkStateFlags previous_state_flags)
127 GtkMenuButton *button = GTK_MENU_BUTTON (widget);
128 GtkMenuButtonPrivate *priv = button->priv;
130 if (!gtk_widget_is_sensitive (widget) && priv->menu)
131 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
135 menu_position_down_func (GtkMenu *menu,
139 GtkMenuButton *menu_button)
141 GtkMenuButtonPrivate *priv = menu_button->priv;
142 GtkWidget *widget = GTK_WIDGET (menu_button);
143 GtkRequisition menu_req;
144 GtkTextDirection direction;
145 GdkRectangle monitor;
149 GtkAllocation allocation, arrow_allocation;
152 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (priv->menu));
153 gtk_window_set_type_hint (GTK_WINDOW (toplevel), GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU);
155 gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
158 direction = gtk_widget_get_direction (widget);
159 window = gtk_widget_get_window (priv->align_widget ? priv->align_widget : widget);
161 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
162 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
165 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
167 gtk_widget_get_allocation (priv->align_widget ? priv->align_widget : widget, &allocation);
168 gtk_widget_get_allocation (widget, &arrow_allocation);
170 gdk_window_get_origin (window, x, y);
174 if (direction == GTK_TEXT_DIR_LTR)
175 *x += MAX (allocation.width - menu_req.width, 0);
176 else if (menu_req.width > allocation.width)
177 *x -= menu_req.width - allocation.width;
179 if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
180 *y += arrow_allocation.height;
181 else if ((*y - menu_req.height) >= monitor.y)
182 *y -= menu_req.height;
183 else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
184 *y += arrow_allocation.height;
186 *y -= menu_req.height;
192 menu_position_up_func (GtkMenu *menu,
196 GtkMenuButton *menu_button)
198 GtkMenuButtonPrivate *priv = menu_button->priv;
199 GtkWidget *widget = GTK_WIDGET (menu_button);
200 GtkRequisition menu_req;
201 GtkTextDirection direction;
202 GdkRectangle monitor;
206 GtkAllocation allocation, arrow_allocation;
208 gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
211 direction = gtk_widget_get_direction (widget);
212 window = gtk_widget_get_window (priv->align_widget ? priv->align_widget : widget);
214 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
215 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
218 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
220 gtk_widget_get_allocation (priv->align_widget ? priv->align_widget : widget, &allocation);
221 gtk_widget_get_allocation (widget, &arrow_allocation);
223 gdk_window_get_origin (window, x, y);
227 if (direction == GTK_TEXT_DIR_LTR)
228 *x += MAX (allocation.width - menu_req.width, 0);
229 else if (menu_req.width > allocation.width)
230 *x -= menu_req.width - allocation.width;
232 *y -= menu_req.height;
234 /* If we're going to clip the top, pop down instead */
237 menu_position_down_func (menu, x, y, push_in, menu_button);
245 menu_position_side_func (GtkMenu *menu,
249 GtkMenuButton *menu_button)
251 GtkMenuButtonPrivate *priv = menu_button->priv;
252 GtkAllocation toggle_allocation;
253 GtkWidget *widget = GTK_WIDGET (menu_button);
254 GtkRequisition menu_req;
255 GdkRectangle monitor;
260 gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
263 window = gtk_widget_get_window (widget);
265 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
266 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
269 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
271 gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (menu_button)), x, y);
273 gtk_widget_get_allocation (widget, &toggle_allocation);
275 if (priv->arrow_type == GTK_ARROW_RIGHT)
276 *x += toggle_allocation.width;
278 *x -= menu_req.width;
280 if (*y + menu_req.height > monitor.y + monitor.height &&
281 *y + toggle_allocation.height - monitor.y > monitor.y + monitor.height - *y)
282 *y += toggle_allocation.height - menu_req.height;
288 popup_menu (GtkMenuButton *menu_button,
289 GdkEventButton *event)
291 GtkMenuButtonPrivate *priv = menu_button->priv;
292 GtkMenuPositionFunc func;
295 priv->func (priv->user_data);
300 switch (priv->arrow_type)
303 func = (GtkMenuPositionFunc) menu_position_up_func;
306 case GTK_ARROW_RIGHT:
307 func = (GtkMenuPositionFunc) menu_position_side_func;
310 func = (GtkMenuPositionFunc) menu_position_down_func;
314 gtk_menu_popup_for_device (GTK_MENU (priv->menu),
315 event ? event->device : NULL,
318 GTK_WIDGET (menu_button),
320 event ? event->button : 0,
321 event ? event->time : gtk_get_current_event_time ());
325 gtk_menu_button_toggled (GtkToggleButton *button)
327 GtkMenuButton *menu_button = GTK_MENU_BUTTON (button);
328 GtkMenuButtonPrivate *priv = menu_button->priv;
333 if (gtk_toggle_button_get_active (button) &&
334 !gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
336 /* we get here only when the menu is activated by a key
337 * press, so that we can select the first menu item
339 popup_menu (menu_button, NULL);
340 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
345 gtk_menu_button_button_press_event (GtkWidget *widget,
346 GdkEventButton *event)
348 if (event->button == GDK_BUTTON_PRIMARY)
350 popup_menu (GTK_MENU_BUTTON (widget), event);
351 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
356 return GTK_WIDGET_CLASS (gtk_menu_button_parent_class)->button_press_event (widget, event);
360 gtk_menu_button_class_init (GtkMenuButtonClass *klass)
362 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
363 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
364 GtkToggleButtonClass *toggle_button_class = GTK_TOGGLE_BUTTON_CLASS (klass);
366 g_type_class_add_private (klass, sizeof (GtkMenuButtonPrivate));
368 gobject_class->set_property = gtk_menu_button_set_property;
369 gobject_class->get_property = gtk_menu_button_get_property;
370 gobject_class->dispose = gtk_menu_button_dispose;
372 widget_class->state_flags_changed = gtk_menu_button_state_flags_changed;
373 widget_class->button_press_event = gtk_menu_button_button_press_event;
375 toggle_button_class->toggled = gtk_menu_button_toggled;
378 * GtkMenuButton:menu:
380 * The #GtkMenu that will be popped up when the button is clicked.
384 g_object_class_install_property (gobject_class,
386 g_param_spec_object ("menu",
388 P_("The dropdown menu."),
392 * GtkMenuButton:menu-model:
394 * The #GMenuModel from which the menu to pop up will be created.
395 * See gtk_menu_button_set_menu_model() for the interaction with the
396 * #GtkMenuButton:menu property.
400 g_object_class_install_property (gobject_class,
402 g_param_spec_object ("menu-model",
404 P_("The dropdown menu's model."),
408 * GtkMenuButton:align-widget:
410 * The #GtkWidget to use to align the popup menu with.
414 g_object_class_install_property (gobject_class,
416 g_param_spec_object ("align-widget",
418 P_("The parent widget which the menu should align with."),
422 * GtkMenuButton:direction:
424 * The #GtkArrowType representing the direction in which the
425 * menu will be popped out.
429 g_object_class_install_property (gobject_class,
431 g_param_spec_enum ("direction",
433 P_("The direction the arrow should point."),
440 add_arrow (GtkMenuButton *menu_button)
444 arrow = gtk_arrow_new (menu_button->priv->arrow_type, GTK_SHADOW_NONE);
445 gtk_container_add (GTK_CONTAINER (menu_button), arrow);
446 gtk_widget_show (arrow);
447 menu_button->priv->arrow_widget = arrow;
451 gtk_menu_button_init (GtkMenuButton *menu_button)
453 GtkMenuButtonPrivate *priv;
455 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_button, GTK_TYPE_MENU_BUTTON, GtkMenuButtonPrivate);
456 menu_button->priv = priv;
457 priv->arrow_type = GTK_ARROW_DOWN;
459 add_arrow (menu_button);
461 gtk_widget_set_sensitive (GTK_WIDGET (menu_button), FALSE);
465 * gtk_menu_button_new:
467 * Creates a new #GtkMenuButton widget with downwards-pointing
468 * arrow as the only child. You can replace the child widget
469 * with another #GtkWidget should you wish to.
471 * Returns: The newly created #GtkMenuButton widget.
476 gtk_menu_button_new (void)
478 return g_object_new (GTK_TYPE_MENU_BUTTON, NULL);
481 /* Callback for the "deactivate" signal on the pop-up menu.
482 * This is used so that we unset the state of the toggle button
483 * when the pop-up menu disappears.
486 menu_deactivate_cb (GtkMenuShell *menu_shell,
487 GtkMenuButton *menu_button)
489 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu_button), FALSE);
495 menu_detacher (GtkWidget *widget,
498 GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (widget)->priv;
500 g_return_if_fail (priv->menu == (GtkWidget *) menu);
505 /* This function is used in GtkMenuToolButton, the call back will
506 * be called when GtkMenuToolButton would have emitted the "show-menu"
510 _gtk_menu_button_set_menu_with_func (GtkMenuButton *menu_button,
512 GtkMenuButtonShowMenuCallback func,
515 GtkMenuButtonPrivate *priv;
517 g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
518 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
520 priv = menu_button->priv;
522 priv->user_data = user_data;
524 if (priv->menu == GTK_WIDGET (menu))
529 if (gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
530 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
535 g_signal_handlers_disconnect_by_func (priv->menu,
538 gtk_menu_detach (GTK_MENU (priv->menu));
545 gtk_menu_attach_to_widget (GTK_MENU (priv->menu), GTK_WIDGET (menu_button),
548 gtk_widget_set_sensitive (GTK_WIDGET (menu_button), TRUE);
550 g_signal_connect (priv->menu, "deactivate",
551 G_CALLBACK (menu_deactivate_cb), menu_button);
555 gtk_widget_set_sensitive (GTK_WIDGET (menu_button), FALSE);
558 g_object_notify (G_OBJECT (menu_button), "menu");
559 g_object_notify (G_OBJECT (menu_button), "menu-model");
563 * gtk_menu_button_set_menu:
564 * @menu_button: a #GtkMenuButton
565 * @menu: (allow-none): a #GtkMenu
567 * Sets the #GtkMenu that will be popped up when the button is clicked,
568 * or %NULL to disable the button. If #GtkMenuButton:menu-model is set,
569 * it will be set to %NULL.
574 gtk_menu_button_set_menu (GtkMenuButton *menu_button,
577 GtkMenuButtonPrivate *priv;
579 g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
580 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
582 priv = menu_button->priv;
583 g_clear_object (&priv->model);
585 _gtk_menu_button_set_menu_with_func (menu_button, menu, NULL, NULL);
589 * gtk_menu_button_get_menu:
590 * @menu_button: a #GtkMenuButton
592 * Returns the #GtkMenu that pops out of the button.
594 * Returns: (transfer none): a #GtkMenu or %NULL.
599 gtk_menu_button_get_menu (GtkMenuButton *menu_button)
601 g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);
603 return GTK_MENU (menu_button->priv->menu);
607 * gtk_menu_button_set_menu_model:
608 * @menu_button: a #GtkMenuButton
609 * @menu_model: (allow-none): a #GMenuModel
611 * Sets the #GMenuModel from which the #GtkMenuButton:menu property will be
612 * filled in, or %NULL to disable the button.
614 * The #GtkMenu will be created with gtk_menu_new_from_model(), so actions
615 * will be connected as documented there.
617 * If you #GtkMenuButton:menu * is already set, then its content will be lost
618 * and replaced by our newly created #GtkMenu.
623 gtk_menu_button_set_menu_model (GtkMenuButton *menu_button,
624 GMenuModel *menu_model)
626 GtkMenuButtonPrivate *priv;
629 g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
630 g_return_if_fail (G_IS_MENU_MODEL (menu_model) || menu_model == NULL);
632 priv = menu_button->priv;
633 g_clear_object (&priv->model);
635 if (menu_model == NULL)
637 gtk_menu_button_set_menu (menu_button, NULL);
641 priv->model = g_object_ref (menu_model);
642 menu = gtk_menu_new_from_model (menu_model);
643 gtk_widget_show_all (menu);
644 gtk_menu_button_set_menu (menu_button, menu);
648 * gtk_menu_button_get_menu_model:
649 * @menu_button: a #GtkMenuButton
651 * Returns the #GMenuModel used to generate the menu.
653 * Returns: (transfer none): a #GMenuModel or %NULL.
658 gtk_menu_button_get_menu_model (GtkMenuButton *menu_button)
660 g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);
662 return menu_button->priv->model;
666 * gtk_menu_button_set_align_widget:
667 * @menu_button: a #GtkMenuButton
668 * @align_widget: (allow-none): a #GtkWidget
670 * Sets the #GtkWidget to use to line the menu with when popped up. Note that
671 * the @align_widget must contain the #GtkMenuButton itself.
673 * Setting it to %NULL means that the popup menu will be aligned with the
679 gtk_menu_button_set_align_widget (GtkMenuButton *menu_button,
680 GtkWidget *align_widget)
682 GtkMenuButtonPrivate *priv;
684 g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
685 g_return_if_fail (align_widget == NULL || gtk_widget_is_ancestor (GTK_WIDGET (menu_button), align_widget));
687 priv = menu_button->priv;
688 if (priv->align_widget == align_widget)
691 priv->align_widget = align_widget;
693 if (priv->align_widget)
694 g_object_add_weak_pointer (G_OBJECT (priv->align_widget), (gpointer *) &priv->align_widget);
696 g_object_notify (G_OBJECT (menu_button), "align-widget");
700 * gtk_menu_button_get_align_widget:
701 * @menu_button: a #GtkMenuButton
703 * Returns the parent #GtkWidget to use to line up with menu.
705 * Returns: (transfer none): a #GtkWidget value or %NULL.
710 gtk_menu_button_get_align_widget (GtkMenuButton *menu_button)
712 g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);
714 return menu_button->priv->align_widget;
718 * gtk_menu_button_set_direction:
719 * @menu_button: a #GtkMenuButton
720 * @direction: a #GtkArrowType
722 * Sets the direction in which the menu will be popped up, as
723 * well as changing the arrow's direction. The child will not
724 * be changed to an arrow if it was customized.
726 * If the menu when popped out would have collided with screen edges,
727 * we will do our best to keep it inside the screen and fully visible.
729 * If you pass GTK_ARROW_NONE for a @direction, the menu will behave
730 * as if you passed GTK_ARROW_DOWN (although you won't see any arrows).
735 gtk_menu_button_set_direction (GtkMenuButton *menu_button,
736 GtkArrowType direction)
738 GtkMenuButtonPrivate *priv = menu_button->priv;
741 g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
743 if (priv->arrow_type == direction)
746 priv->arrow_type = direction;
748 /* Is it custom content? We don't change that */
749 child = gtk_bin_get_child (GTK_BIN (menu_button));
750 if (priv->arrow_widget != child)
753 gtk_arrow_set (GTK_ARROW (child), priv->arrow_type, GTK_SHADOW_NONE);
757 * gtk_menu_button_get_direction:
758 * @menu_button: a #GtkMenuButton
760 * Returns the direction the menu will be pointing at when popped up.
762 * Returns: a #GtkArrowType value.
767 gtk_menu_button_get_direction (GtkMenuButton *menu_button)
769 g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), GTK_ARROW_DOWN);
771 return menu_button->priv->arrow_type;
775 gtk_menu_button_dispose (GObject *object)
777 GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (object)->priv;
781 g_signal_handlers_disconnect_by_func (priv->menu,
784 gtk_menu_detach (GTK_MENU (priv->menu));
787 g_clear_object (&priv->model);
789 G_OBJECT_CLASS (gtk_menu_button_parent_class)->dispose (object);