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
30 * can hold almost any other standard #GtkWidget. The most commonly used
31 * child is the provided #GtkArrow.
33 * The positioning of the menu is determined by the #GtkMenuButton:direction
34 * property of the menu button and the #GtkWidget:halign or #GtkWidget:valign
35 * properties of the menu. For example, when the direction is %GTK_ARROW_DOWN
36 * and the horizontal alignment is %GTK_ALIGN_START, the menu will be
37 * positioned below the button, with the starting edge (depending on the
38 * text direction) of the menu aligned with the starting edge of the button.
39 * If there is not enough space below the button, the menu is popped up above
40 * the button instead. If the alignment would move part of the menu offscreen,
48 * <entry>halign = start</entry>
49 * <entry>halign = center</entry>
50 * <entry>halign = end</entry>
53 * <entry>direction = down</entry>
56 * <imageobject><imagedata fileref="down-start.png" format="PNG"/></imageobject>
57 * </inlinemediaobject>
61 * <imageobject><imagedata fileref="down-center.png" format="PNG"/></imageobject>
62 * </inlinemediaobject>
66 * <imageobject><imagedata fileref="down-end.png" format="PNG"/></imageobject>
67 * </inlinemediaobject>
71 * <entry>direction = up</entry>
74 * <imageobject><imagedata fileref="up-start.png" format="PNG"/></imageobject>
75 * </inlinemediaobject>
79 * <imageobject><imagedata fileref="up-center.png" format="PNG"/></imageobject>
80 * </inlinemediaobject>
84 * <imageobject><imagedata fileref="up-end.png" format="PNG"/></imageobject>
85 * </inlinemediaobject>
96 * <entry>direction = left</entry>
97 * <entry>direction = right</entry>
100 * <entry>valign = start</entry>
102 * <inlinemediaobject>
103 * <imageobject><imagedata fileref="left-start.png" format="PNG"/></imageobject>
104 * </inlinemediaobject>
107 * <inlinemediaobject>
108 * <imageobject><imagedata fileref="right-start.png" format="PNG"/></imageobject>
109 * </inlinemediaobject>
113 * <entry>valign = center</entry>
115 * <inlinemediaobject>
116 * <imageobject><imagedata fileref="left-center.png" format="PNG"/></imageobject>
117 * </inlinemediaobject>
120 * <inlinemediaobject>
121 * <imageobject><imagedata fileref="right-center.png" format="PNG"/></imageobject>
122 * </inlinemediaobject>
126 * <entry>valign = end</entry>
128 * <inlinemediaobject>
129 * <imageobject><imagedata fileref="left-end.png" format="PNG"/></imageobject>
130 * </inlinemediaobject>
133 * <inlinemediaobject>
134 * <imageobject><imagedata fileref="right-end.png" format="PNG"/></imageobject>
135 * </inlinemediaobject>
145 #include "gtkmenubutton.h"
146 #include "gtkmenubuttonprivate.h"
147 #include "gtkarrow.h"
149 #include "gtkprivate.h"
152 struct _GtkMenuButtonPrivate
157 GtkMenuButtonShowMenuCallback func;
160 GtkArrowType arrow_type;
161 GtkWidget *align_widget;
162 gpointer arrow_widget;
174 G_DEFINE_TYPE(GtkMenuButton, gtk_menu_button, GTK_TYPE_TOGGLE_BUTTON)
176 static void gtk_menu_button_constructed (GObject *object);
177 static void gtk_menu_button_dispose (GObject *object);
180 gtk_menu_button_set_property (GObject *object,
185 GtkMenuButton *self = GTK_MENU_BUTTON (object);
190 gtk_menu_button_set_popup (self, g_value_get_object (value));
193 gtk_menu_button_set_menu_model (self, g_value_get_object (value));
195 case PROP_ALIGN_WIDGET:
196 gtk_menu_button_set_align_widget (self, g_value_get_object (value));
199 gtk_menu_button_set_direction (self, g_value_get_enum (value));
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
207 gtk_menu_button_get_property (GObject *object,
212 GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (object)->priv;
217 g_value_set_object (value, priv->popup);
220 g_value_set_object (value, priv->model);
222 case PROP_ALIGN_WIDGET:
223 g_value_set_object (value, priv->align_widget);
226 g_value_set_enum (value, priv->arrow_type);
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
234 gtk_menu_button_state_flags_changed (GtkWidget *widget,
235 GtkStateFlags previous_state_flags)
237 GtkMenuButton *button = GTK_MENU_BUTTON (widget);
238 GtkMenuButtonPrivate *priv = button->priv;
240 if (!gtk_widget_is_sensitive (widget) && priv->popup)
241 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->popup));
245 menu_position_up_down_func (GtkMenu *menu,
249 GtkMenuButton *menu_button)
251 GtkMenuButtonPrivate *priv = menu_button->priv;
252 GtkWidget *widget = GTK_WIDGET (menu_button);
253 GtkRequisition menu_req;
254 GtkTextDirection direction;
255 GdkRectangle monitor;
259 GtkAllocation allocation, arrow_allocation;
262 gtk_widget_get_preferred_size (GTK_WIDGET (priv->popup),
265 align = gtk_widget_get_halign (GTK_WIDGET (priv->popup));
266 direction = gtk_widget_get_direction (widget);
267 window = gtk_widget_get_window (priv->align_widget ? priv->align_widget : widget);
269 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
270 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
273 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
275 gtk_widget_get_allocation (priv->align_widget ? priv->align_widget : widget, &allocation);
276 gtk_widget_get_allocation (widget, &arrow_allocation);
278 gdk_window_get_origin (window, x, y);
282 /* treat the default align value like START */
283 if (align == GTK_ALIGN_FILL)
284 align = GTK_ALIGN_START;
286 if (align == GTK_ALIGN_CENTER)
287 *x -= (menu_req.width - allocation.width) / 2;
288 else if ((align == GTK_ALIGN_START && direction == GTK_TEXT_DIR_LTR) ||
289 (align == GTK_ALIGN_END && direction == GTK_TEXT_DIR_RTL))
290 *x += MAX (allocation.width - menu_req.width, 0);
291 else if (menu_req.width > allocation.width)
292 *x -= menu_req.width - allocation.width;
294 if (priv->arrow_type == GTK_ARROW_UP && *y - menu_req.height >= monitor.y)
296 *y -= menu_req.height;
300 if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
301 *y += arrow_allocation.height;
302 else if ((*y - menu_req.height) >= monitor.y)
303 *y -= menu_req.height;
304 else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
305 *y += arrow_allocation.height;
307 *y -= menu_req.height;
314 menu_position_side_func (GtkMenu *menu,
318 GtkMenuButton *menu_button)
320 GtkMenuButtonPrivate *priv = menu_button->priv;
321 GtkAllocation allocation;
322 GtkWidget *widget = GTK_WIDGET (menu_button);
323 GtkRequisition menu_req;
324 GdkRectangle monitor;
330 gtk_widget_get_preferred_size (GTK_WIDGET (priv->popup),
333 window = gtk_widget_get_window (widget);
335 align = gtk_widget_get_valign (GTK_WIDGET (menu));
336 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
337 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
340 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
342 gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (menu_button)), x, y);
344 gtk_widget_get_allocation (widget, &allocation);
346 if (priv->arrow_type == GTK_ARROW_RIGHT)
348 if (*x + allocation.width + menu_req.width <= monitor.x + monitor.width)
349 *x += allocation.width;
351 *x -= menu_req.width;
355 if (*x - menu_req.width >= monitor.x)
356 *x -= menu_req.width;
358 *x += allocation.width;
361 /* treat the default align value like START */
362 if (align == GTK_ALIGN_FILL)
363 align = GTK_ALIGN_START;
365 if (align == GTK_ALIGN_CENTER)
366 *y -= (menu_req.height - allocation.height) / 2;
367 else if (align == GTK_ALIGN_END)
368 *y -= menu_req.height - allocation.height;
374 popup_menu (GtkMenuButton *menu_button,
375 GdkEventButton *event)
377 GtkMenuButtonPrivate *priv = menu_button->priv;
378 GtkMenuPositionFunc func;
381 priv->func (priv->user_data);
386 switch (priv->arrow_type)
389 case GTK_ARROW_RIGHT:
390 func = (GtkMenuPositionFunc) menu_position_side_func;
393 func = (GtkMenuPositionFunc) menu_position_up_down_func;
397 gtk_menu_popup_for_device (GTK_MENU (priv->popup),
398 event ? event->device : NULL,
401 GTK_WIDGET (menu_button),
403 event ? event->button : 0,
404 event ? event->time : gtk_get_current_event_time ());
408 gtk_menu_button_toggled (GtkToggleButton *button)
410 GtkMenuButton *menu_button = GTK_MENU_BUTTON (button);
411 GtkMenuButtonPrivate *priv = menu_button->priv;
416 if (gtk_toggle_button_get_active (button) &&
417 !gtk_widget_get_visible (GTK_WIDGET (priv->popup)))
419 /* we get here only when the menu is activated by a key
420 * press, so that we can select the first menu item
422 popup_menu (menu_button, NULL);
423 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup), FALSE);
428 gtk_menu_button_button_press_event (GtkWidget *widget,
429 GdkEventButton *event)
431 if (event->button == GDK_BUTTON_PRIMARY)
433 popup_menu (GTK_MENU_BUTTON (widget), event);
434 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
439 return GTK_WIDGET_CLASS (gtk_menu_button_parent_class)->button_press_event (widget, event);
443 gtk_menu_button_add (GtkContainer *container,
446 GtkMenuButton *button = GTK_MENU_BUTTON (container);
448 if (button->priv->arrow_widget)
450 gtk_container_remove (container, button->priv->arrow_widget);
451 button->priv->arrow_widget = NULL;
454 GTK_CONTAINER_CLASS (gtk_menu_button_parent_class)->add (container, child);
458 gtk_menu_button_class_init (GtkMenuButtonClass *klass)
460 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
461 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
462 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
463 GtkToggleButtonClass *toggle_button_class = GTK_TOGGLE_BUTTON_CLASS (klass);
465 g_type_class_add_private (klass, sizeof (GtkMenuButtonPrivate));
467 gobject_class->set_property = gtk_menu_button_set_property;
468 gobject_class->get_property = gtk_menu_button_get_property;
469 gobject_class->constructed = gtk_menu_button_constructed;
470 gobject_class->dispose = gtk_menu_button_dispose;
472 widget_class->state_flags_changed = gtk_menu_button_state_flags_changed;
473 widget_class->button_press_event = gtk_menu_button_button_press_event;
475 container_class->add = gtk_menu_button_add;
477 toggle_button_class->toggled = gtk_menu_button_toggled;
480 * GtkMenuButton:popup:
482 * The #GtkMenu that will be popped up when the button is clicked.
486 g_object_class_install_property (gobject_class,
488 g_param_spec_object ("popup",
490 P_("The dropdown menu."),
494 * GtkMenuButton:menu:
496 * The #GtkMenu that will be popped up when the button is clicked.
497 * This property has been renamed to "popup". "menu" will be
498 * removed before 3.6.0.
502 g_object_class_install_property (gobject_class,
503 PROP_POPUP, /* [sic] */
504 g_param_spec_object ("menu",
506 P_("The dropdown menu."),
508 G_PARAM_DEPRECATED | G_PARAM_READWRITE));
510 * GtkMenuButton:menu-model:
512 * The #GMenuModel from which the menu to pop up will be created.
513 * See gtk_menu_button_set_menu_model() for the interaction with the
514 * #GtkMenuButton:menu property.
518 g_object_class_install_property (gobject_class,
520 g_param_spec_object ("menu-model",
522 P_("The dropdown menu's model."),
526 * GtkMenuButton:align-widget:
528 * The #GtkWidget to use to align the popup menu with.
532 g_object_class_install_property (gobject_class,
534 g_param_spec_object ("align-widget",
536 P_("The parent widget which the menu should align with."),
540 * GtkMenuButton:direction:
542 * The #GtkArrowType representing the direction in which the
543 * menu will be popped out.
547 g_object_class_install_property (gobject_class,
549 g_param_spec_enum ("direction",
551 P_("The direction the arrow should point."),
554 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
558 add_arrow (GtkMenuButton *menu_button)
562 arrow = gtk_arrow_new (menu_button->priv->arrow_type, GTK_SHADOW_NONE);
563 gtk_container_add (GTK_CONTAINER (menu_button), arrow);
564 gtk_widget_show (arrow);
565 menu_button->priv->arrow_widget = arrow;
569 gtk_menu_button_init (GtkMenuButton *menu_button)
571 GtkMenuButtonPrivate *priv;
573 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_button, GTK_TYPE_MENU_BUTTON, GtkMenuButtonPrivate);
574 menu_button->priv = priv;
575 priv->arrow_type = GTK_ARROW_DOWN;
577 gtk_widget_set_sensitive (GTK_WIDGET (menu_button), FALSE);
581 * gtk_menu_button_new:
583 * Creates a new #GtkMenuButton widget with downwards-pointing
584 * arrow as the only child. You can replace the child widget
585 * with another #GtkWidget should you wish to.
587 * Returns: The newly created #GtkMenuButton widget.
592 gtk_menu_button_new (void)
594 return g_object_new (GTK_TYPE_MENU_BUTTON, NULL);
597 /* Callback for the "deactivate" signal on the pop-up menu.
598 * This is used so that we unset the state of the toggle button
599 * when the pop-up menu disappears.
602 menu_deactivate_cb (GtkMenuShell *menu_shell,
603 GtkMenuButton *menu_button)
605 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu_button), FALSE);
611 menu_detacher (GtkWidget *widget,
614 GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (widget)->priv;
616 g_return_if_fail (priv->popup == (GtkWidget *) menu);
621 /* This function is used in GtkMenuToolButton, the call back will
622 * be called when GtkMenuToolButton would have emitted the "show-menu"
626 _gtk_menu_button_set_popup_with_func (GtkMenuButton *menu_button,
628 GtkMenuButtonShowMenuCallback func,
631 GtkMenuButtonPrivate *priv;
633 g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
634 g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
636 priv = menu_button->priv;
638 priv->user_data = user_data;
640 if (priv->popup == GTK_WIDGET (menu))
645 if (gtk_widget_get_visible (GTK_WIDGET (priv->popup)))
646 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->popup));
651 g_signal_handlers_disconnect_by_func (priv->popup,
654 gtk_menu_detach (GTK_MENU (priv->popup));
661 gtk_menu_attach_to_widget (GTK_MENU (priv->popup), GTK_WIDGET (menu_button),
664 gtk_widget_set_sensitive (GTK_WIDGET (menu_button), TRUE);
666 g_signal_connect (priv->popup, "deactivate",
667 G_CALLBACK (menu_deactivate_cb), menu_button);
671 gtk_widget_set_sensitive (GTK_WIDGET (menu_button), FALSE);
674 g_object_notify (G_OBJECT (menu_button), "menu");
675 g_object_notify (G_OBJECT (menu_button), "menu-model");
679 * gtk_menu_button_set_popup:
680 * @menu_button: a #GtkMenuButton
681 * @popup: (allow-none): a #GtkMenu
683 * Sets the #GtkMenu that will be popped up when the button is clicked,
684 * or %NULL to disable the button. If #GtkMenuButton:menu-model is set,
685 * it will be set to %NULL.
690 gtk_menu_button_set_popup (GtkMenuButton *menu_button,
693 GtkMenuButtonPrivate *priv;
695 g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
696 g_return_if_fail (GTK_IS_MENU (popup) || popup == NULL);
698 priv = menu_button->priv;
699 g_clear_object (&priv->model);
701 _gtk_menu_button_set_popup_with_func (menu_button, popup, NULL, NULL);
705 * gtk_menu_button_get_popup:
706 * @menu_button: a #GtkMenuButton
708 * Returns the #GtkMenu that pops out of the button.
710 * Returns: (transfer none): a #GtkMenu or %NULL.
715 gtk_menu_button_get_popup (GtkMenuButton *menu_button)
717 g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);
719 return GTK_MENU (menu_button->priv->popup);
723 gtk_menu_button_set_menu (GtkMenuButton *menu_button,
726 gtk_menu_button_set_popup (menu_button, menu);
730 gtk_menu_button_get_menu (GtkMenuButton *menu_button)
732 return gtk_menu_button_get_popup (menu_button);
736 * gtk_menu_button_set_menu_model:
737 * @menu_button: a #GtkMenuButton
738 * @menu_model: (allow-none): a #GMenuModel
740 * Sets the #GMenuModel from which the #GtkMenuButton:menu property will be
741 * filled in, or %NULL to disable the button.
743 * The #GtkMenu will be created with gtk_menu_new_from_model(), so actions
744 * will be connected as documented there.
746 * If you #GtkMenuButton:menu * is already set, then its content will be lost
747 * and replaced by our newly created #GtkMenu.
752 gtk_menu_button_set_menu_model (GtkMenuButton *menu_button,
753 GMenuModel *menu_model)
755 GtkMenuButtonPrivate *priv;
758 g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
759 g_return_if_fail (G_IS_MENU_MODEL (menu_model) || menu_model == NULL);
761 priv = menu_button->priv;
762 g_clear_object (&priv->model);
764 if (menu_model == NULL)
766 gtk_menu_button_set_popup (menu_button, NULL);
770 priv->model = g_object_ref (menu_model);
771 menu = gtk_menu_new_from_model (menu_model);
772 gtk_widget_show_all (menu);
773 gtk_menu_button_set_popup (menu_button, menu);
777 * gtk_menu_button_get_menu_model:
778 * @menu_button: a #GtkMenuButton
780 * Returns the #GMenuModel used to generate the menu.
782 * Returns: (transfer none): a #GMenuModel or %NULL.
787 gtk_menu_button_get_menu_model (GtkMenuButton *menu_button)
789 g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);
791 return menu_button->priv->model;
795 * gtk_menu_button_set_align_widget:
796 * @menu_button: a #GtkMenuButton
797 * @align_widget: (allow-none): a #GtkWidget
799 * Sets the #GtkWidget to use to line the menu with when popped up. Note that
800 * the @align_widget must contain the #GtkMenuButton itself.
802 * Setting it to %NULL means that the popup menu will be aligned with the
808 gtk_menu_button_set_align_widget (GtkMenuButton *menu_button,
809 GtkWidget *align_widget)
811 GtkMenuButtonPrivate *priv;
813 g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
814 g_return_if_fail (align_widget == NULL || gtk_widget_is_ancestor (GTK_WIDGET (menu_button), align_widget));
816 priv = menu_button->priv;
817 if (priv->align_widget == align_widget)
820 priv->align_widget = align_widget;
822 if (priv->align_widget)
823 g_object_add_weak_pointer (G_OBJECT (priv->align_widget), (gpointer *) &priv->align_widget);
825 g_object_notify (G_OBJECT (menu_button), "align-widget");
829 * gtk_menu_button_get_align_widget:
830 * @menu_button: a #GtkMenuButton
832 * Returns the parent #GtkWidget to use to line up with menu.
834 * Returns: (transfer none): a #GtkWidget value or %NULL.
839 gtk_menu_button_get_align_widget (GtkMenuButton *menu_button)
841 g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);
843 return menu_button->priv->align_widget;
847 * gtk_menu_button_set_direction:
848 * @menu_button: a #GtkMenuButton
849 * @direction: a #GtkArrowType
851 * Sets the direction in which the menu will be popped up, as
852 * well as changing the arrow's direction. The child will not
853 * be changed to an arrow if it was customized.
855 * If the menu when popped out would have collided with screen edges,
856 * we will do our best to keep it inside the screen and fully visible.
858 * If you pass GTK_ARROW_NONE for a @direction, the menu will behave
859 * as if you passed GTK_ARROW_DOWN (although you won't see any arrows).
864 gtk_menu_button_set_direction (GtkMenuButton *menu_button,
865 GtkArrowType direction)
867 GtkMenuButtonPrivate *priv = menu_button->priv;
870 g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
872 if (priv->arrow_type == direction)
875 priv->arrow_type = direction;
877 child = gtk_bin_get_child (GTK_BIN (menu_button));
881 /* Is it custom content? We don't change that */
882 if (priv->arrow_widget != child)
885 gtk_arrow_set (GTK_ARROW (child), priv->arrow_type, GTK_SHADOW_NONE);
889 * gtk_menu_button_get_direction:
890 * @menu_button: a #GtkMenuButton
892 * Returns the direction the menu will be pointing at when popped up.
894 * Returns: a #GtkArrowType value.
899 gtk_menu_button_get_direction (GtkMenuButton *menu_button)
901 g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), GTK_ARROW_DOWN);
903 return menu_button->priv->arrow_type;
907 gtk_menu_button_constructed (GObject *object)
909 GtkMenuButton *button = GTK_MENU_BUTTON (object);
911 if (button->priv->arrow_type != GTK_ARROW_NONE)
914 G_OBJECT_CLASS (gtk_menu_button_parent_class)->constructed (object);
918 gtk_menu_button_dispose (GObject *object)
920 GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (object)->priv;
924 g_signal_handlers_disconnect_by_func (priv->popup,
927 gtk_menu_detach (GTK_MENU (priv->popup));
930 g_clear_object (&priv->model);
932 G_OBJECT_CLASS (gtk_menu_button_parent_class)->dispose (object);