diff options
author | Matthias Clasen <mclasen@redhat.com> | 2014-02-07 19:21:49 -0500 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2014-02-08 20:42:20 -0500 |
commit | 9822d510a68e01db4468db7629d4e7fb40e7abcc (patch) | |
tree | 6d314c96167be8bc7674f39d9d0dbbb6761a5f9c /gtk | |
parent | 552c29b488ecd7bcc3303dd5514ce6dbfff04437 (diff) | |
download | gtk+-9822d510a68e01db4468db7629d4e7fb40e7abcc.tar.gz |
GtkMenuButton: Support popovers
Add api to allow explicitly setting a GtkPopover instead of
a GtkMenu as the popup of a GtkMenuButton. Also, add api to
instruct the menu button to construct a popover when given
a menu model.
We set the style class "menu-button" on the button only when
it pops up a menu, to allow different treatment for the active
state of the button in the two cases.
https://bugzilla.gnome.org/show_bug.cgi?id=723878
Diffstat (limited to 'gtk')
-rw-r--r-- | gtk/gtkmenubutton.c | 443 | ||||
-rw-r--r-- | gtk/gtkmenubutton.h | 17 |
2 files changed, 360 insertions, 100 deletions
diff --git a/gtk/gtkmenubutton.c b/gtk/gtkmenubutton.c index dbce31b631..c79cebeae8 100644 --- a/gtk/gtkmenubutton.c +++ b/gtk/gtkmenubutton.c @@ -20,25 +20,28 @@ /** * SECTION:gtkmenubutton - * @short_description: A widget that shows a menu when clicked on + * @short_description: A widget that shows a popup when clicked on * @title: GtkMenuButton * - * 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. + * The #GtkMenuButton widget is used to display a popup when clicked on. + * This popup can be provided either as a #GtkMenu, a #GtkPopover or an + * abstract #GMenuModel. * * 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”. + * The positioning of the popup is determined by the #GtkMenuButton:direction + * property of the menu button. + * + * For menus, the #GtkWidget:halign and #GtkWidget:valign properties of the + * menu are also taken into account. 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”. * * ## Direction = Down * @@ -107,6 +110,7 @@ #include "gtkwindow.h" #include "gtkmain.h" #include "gtkaccessible.h" +#include "gtkpopover.h" #include "a11y/gtkmenubuttonaccessible.h" #include "gtkprivate.h" @@ -114,7 +118,8 @@ struct _GtkMenuButtonPrivate { - GtkWidget *popup; + GtkWidget *menu; /* The menu and the popover are mutually exclusive */ + GtkWidget *popover; /* Only one at a time can be set */ GMenuModel *model; GtkMenuButtonShowMenuCallback func; @@ -123,6 +128,7 @@ struct _GtkMenuButtonPrivate GtkWidget *align_widget; gpointer arrow_widget; GtkArrowType arrow_type; + gboolean use_popover; }; enum @@ -131,7 +137,9 @@ enum PROP_POPUP, PROP_MODEL, PROP_ALIGN_WIDGET, - PROP_DIRECTION + PROP_DIRECTION, + PROP_USE_POPOVER, + PROP_POPOVER }; G_DEFINE_TYPE_WITH_PRIVATE (GtkMenuButton, gtk_menu_button, GTK_TYPE_TOGGLE_BUTTON) @@ -160,6 +168,12 @@ gtk_menu_button_set_property (GObject *object, case PROP_DIRECTION: gtk_menu_button_set_direction (self, g_value_get_enum (value)); break; + case PROP_USE_POPOVER: + gtk_menu_button_set_use_popover (self, g_value_get_boolean (value)); + break; + case PROP_POPOVER: + gtk_menu_button_set_popover (self, g_value_get_object (value)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); } @@ -176,7 +190,7 @@ gtk_menu_button_get_property (GObject *object, switch (property_id) { case PROP_POPUP: - g_value_set_object (value, priv->popup); + g_value_set_object (value, priv->menu); break; case PROP_MODEL: g_value_set_object (value, priv->model); @@ -187,6 +201,12 @@ gtk_menu_button_get_property (GObject *object, case PROP_DIRECTION: g_value_set_enum (value, priv->arrow_type); break; + case PROP_USE_POPOVER: + g_value_set_boolean (value, priv->use_popover); + break; + case PROP_POPOVER: + g_value_set_object (value, priv->popover); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); } @@ -199,8 +219,13 @@ gtk_menu_button_state_flags_changed (GtkWidget *widget, GtkMenuButton *button = GTK_MENU_BUTTON (widget); GtkMenuButtonPrivate *priv = button->priv; - if (!gtk_widget_is_sensitive (widget) && priv->popup) - gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->popup)); + if (!gtk_widget_is_sensitive (widget)) + { + if (priv->menu) + gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu)); + else if (priv->popover) + gtk_widget_hide (priv->popover); + } } static void @@ -227,11 +252,11 @@ menu_position_up_down_func (GtkMenu *menu, */ if (priv->arrow_type == GTK_ARROW_DOWN) { - toplevel = gtk_widget_get_toplevel (GTK_WIDGET (priv->popup)); + toplevel = gtk_widget_get_toplevel (priv->menu); gtk_window_set_type_hint (GTK_WINDOW (toplevel), GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU); } - align = gtk_widget_get_halign (GTK_WIDGET (priv->popup)); + align = gtk_widget_get_halign (priv->menu); direction = gtk_widget_get_direction (widget); window = gtk_widget_get_window (priv->align_widget ? priv->align_widget : widget); @@ -243,7 +268,7 @@ menu_position_up_down_func (GtkMenu *menu, gtk_widget_get_allocation (priv->align_widget ? priv->align_widget : widget, &allocation); gtk_widget_get_allocation (widget, &arrow_allocation); - gtk_widget_get_allocation (GTK_WIDGET (priv->popup), &menu_allocation); + gtk_widget_get_allocation (priv->menu, &menu_allocation); gdk_window_get_origin (window, x, y); *x += allocation.x; @@ -311,7 +336,7 @@ menu_position_side_func (GtkMenu *menu, gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (menu_button)), x, y); gtk_widget_get_allocation (widget, &allocation); - gtk_widget_get_allocation (GTK_WIDGET (priv->popup), &menu_allocation); + gtk_widget_get_allocation (priv->menu, &menu_allocation); if ((priv->arrow_type == GTK_ARROW_RIGHT && direction == GTK_TEXT_DIR_LTR) || (priv->arrow_type == GTK_ARROW_LEFT && direction == GTK_TEXT_DIR_RTL)) @@ -352,7 +377,7 @@ popup_menu (GtkMenuButton *menu_button, if (priv->func) priv->func (priv->user_data); - if (!priv->popup) + if (!priv->menu) return; switch (priv->arrow_type) @@ -366,7 +391,7 @@ popup_menu (GtkMenuButton *menu_button, break; } - gtk_menu_popup_for_device (GTK_MENU (priv->popup), + gtk_menu_popup_for_device (GTK_MENU (priv->menu), event ? event->device : NULL, NULL, NULL, func, @@ -382,27 +407,38 @@ gtk_menu_button_toggled (GtkToggleButton *button) GtkMenuButton *menu_button = GTK_MENU_BUTTON (button); GtkMenuButtonPrivate *priv = menu_button->priv; - if (!priv->popup) + if (!gtk_toggle_button_get_active (button)) return; - if (gtk_toggle_button_get_active (button) && - !gtk_widget_get_visible (GTK_WIDGET (priv->popup))) - { - /* 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); - gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup), FALSE); + if (priv->menu) + { + if (!gtk_widget_get_visible (priv->menu)) + { + /* 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); + gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE); + } } + else if (priv->popover) + gtk_widget_show (priv->popover); } static gboolean gtk_menu_button_button_press_event (GtkWidget *widget, GdkEventButton *event) { + GtkMenuButton *menu_button = GTK_MENU_BUTTON (widget); + GtkMenuButtonPrivate *priv = menu_button->priv; + if (event->button == GDK_BUTTON_PRIMARY) { - popup_menu (GTK_MENU_BUTTON (widget), event); + if (priv->menu) + popup_menu (menu_button, event); + else if (priv->popover) + gtk_widget_show (priv->popover); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE); return TRUE; @@ -473,7 +509,10 @@ gtk_menu_button_class_init (GtkMenuButtonClass *klass) /** * GtkMenuButton:menu-model: * - * The #GMenuModel from which the menu to pop up will be created. + * The #GMenuModel from which the popup will be created. + * Depending on the #GtkMenuButton:use-popover property, that may + * be a menu or a popover. + * * See gtk_menu_button_set_menu_model() for the interaction with the * #GtkMenuButton:popup property. * @@ -483,13 +522,13 @@ gtk_menu_button_class_init (GtkMenuButtonClass *klass) PROP_MODEL, g_param_spec_object ("menu-model", P_("Menu model"), - P_("The dropdown menu's model."), + P_("The model from which the popup is made."), G_TYPE_MENU_MODEL, G_PARAM_READWRITE)); /** * GtkMenuButton:align-widget: * - * The #GtkWidget to use to align the popup menu with. + * The #GtkWidget to use to align the menu with. * * Since: 3.6 */ @@ -504,7 +543,7 @@ gtk_menu_button_class_init (GtkMenuButtonClass *klass) * GtkMenuButton:direction: * * The #GtkArrowType representing the direction in which the - * menu will be popped out. + * menu or popover will be popped out. * * Since: 3.6 */ @@ -516,6 +555,37 @@ gtk_menu_button_class_init (GtkMenuButtonClass *klass) GTK_TYPE_ARROW_TYPE, GTK_ARROW_DOWN, G_PARAM_READWRITE)); + /** + * GtkMenuButton:use-popover: + * + * Whether to construct a #GtkPopover from the menu model, + * or a #GtkMenu. + * + * Since: 3.12 + */ + g_object_class_install_property (gobject_class, + PROP_USE_POPOVER, + g_param_spec_boolean ("use-popover", + P_("Use a popover"), + P_("Use a popover instead of a menu"), + FALSE, + G_PARAM_READWRITE)); + + /** + * GtkMenuButton:popover: + * + * The #GtkPopover that will be popped up when the button is clicked. + * + * Since: 3.12 + */ + g_object_class_install_property (gobject_class, + PROP_POPOVER, + g_param_spec_object ("popover", + P_("Popover"), + P_("The popover"), + GTK_TYPE_POPOVER, + G_PARAM_READWRITE)); + gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_BUTTON_ACCESSIBLE); } @@ -557,7 +627,7 @@ gtk_menu_button_init (GtkMenuButton *menu_button) * arrow as the only child. You can replace the child widget * with another #GtkWidget should you wish to. * - * Returns: The newly created #GtkMenuButton widget. + * Returns: The newly created #GtkMenuButton widget * * Since: 3.6 */ @@ -570,10 +640,10 @@ gtk_menu_button_new (void) /* 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. + * Also used for the "close" signal on the popover. */ -static int -menu_deactivate_cb (GtkMenuShell *menu_shell, - GtkMenuButton *menu_button) +static gboolean +menu_deactivate_cb (GtkMenuButton *menu_button) { gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu_button), FALSE); @@ -586,9 +656,9 @@ menu_detacher (GtkWidget *widget, { GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (widget)->priv; - g_return_if_fail (priv->popup == (GtkWidget *) menu); + g_return_if_fail (priv->menu == (GtkWidget *) menu); - priv->popup = NULL; + priv->menu = NULL; } /* This function is used in GtkMenuToolButton, the call back will @@ -610,36 +680,32 @@ _gtk_menu_button_set_popup_with_func (GtkMenuButton *menu_button priv->func = func; priv->user_data = user_data; - if (priv->popup == GTK_WIDGET (menu)) + if (priv->menu == GTK_WIDGET (menu)) return; - if (priv->popup) + if (priv->menu) { - if (gtk_widget_get_visible (GTK_WIDGET (priv->popup))) - gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->popup)); + if (gtk_widget_get_visible (priv->menu)) + gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu)); - g_signal_handlers_disconnect_by_func (priv->popup, + g_signal_handlers_disconnect_by_func (priv->menu, menu_deactivate_cb, menu_button); - gtk_menu_detach (GTK_MENU (priv->popup)); + gtk_menu_detach (GTK_MENU (priv->menu)); } - priv->popup = menu; + priv->menu = menu; - if (priv->popup) + if (priv->menu) { - gtk_menu_attach_to_widget (GTK_MENU (priv->popup), GTK_WIDGET (menu_button), + gtk_menu_attach_to_widget (GTK_MENU (priv->menu), GTK_WIDGET (menu_button), menu_detacher); - gtk_widget_set_visible (priv->popup, FALSE); - gtk_widget_set_sensitive (GTK_WIDGET (menu_button), TRUE); + gtk_widget_set_visible (priv->menu, FALSE); - g_signal_connect (priv->popup, "deactivate", - G_CALLBACK (menu_deactivate_cb), menu_button); - } - else - { - gtk_widget_set_sensitive (GTK_WIDGET (menu_button), FALSE); + g_signal_connect_swapped (priv->menu, "deactivate", + G_CALLBACK (menu_deactivate_cb), menu_button); + gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (menu_button)), "menu-button"); } g_object_notify (G_OBJECT (menu_button), "popup"); @@ -649,27 +715,32 @@ _gtk_menu_button_set_popup_with_func (GtkMenuButton *menu_button /** * gtk_menu_button_set_popup: * @menu_button: a #GtkMenuButton - * @popup: (allow-none): a #GtkMenu + * @menu: (allow-none): a #GtkMenu * * 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. + * or %NULL to disable the button. If #GtkMenuButton:menu-model or + * #GtkMenuButton:popover are set, they will be set to %NULL. * * Since: 3.6 */ void gtk_menu_button_set_popup (GtkMenuButton *menu_button, - GtkWidget *popup) + GtkWidget *menu) { - GtkMenuButtonPrivate *priv; + GtkMenuButtonPrivate *priv = menu_button->priv; g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button)); - g_return_if_fail (GTK_IS_MENU (popup) || popup == NULL); + g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL); - priv = menu_button->priv; g_clear_object (&priv->model); - _gtk_menu_button_set_popup_with_func (menu_button, popup, NULL, NULL); + _gtk_menu_button_set_popup_with_func (menu_button, menu, NULL, NULL); + + if (menu && priv->popover) + gtk_menu_button_set_popover (menu_button, NULL); + + gtk_widget_set_sensitive (GTK_WIDGET (menu_button), + priv->menu != NULL || priv->popover != NULL); } /** @@ -677,8 +748,10 @@ gtk_menu_button_set_popup (GtkMenuButton *menu_button, * @menu_button: a #GtkMenuButton * * Returns the #GtkMenu that pops out of the button. + * If the button does not use a #GtkMenu, this function + * returns %NULL. * - * Returns: (transfer none): a #GtkMenu or %NULL. + * Returns: (transfer none): a #GtkMenu or %NULL * * Since: 3.6 */ @@ -687,7 +760,7 @@ gtk_menu_button_get_popup (GtkMenuButton *menu_button) { g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL); - return GTK_MENU (menu_button->priv->popup); + return GTK_MENU (menu_button->priv->menu); } /** @@ -695,14 +768,16 @@ gtk_menu_button_get_popup (GtkMenuButton *menu_button) * @menu_button: a #GtkMenuButton * @menu_model: (allow-none): a #GMenuModel * - * Sets the #GMenuModel from which the #GtkMenuButton:popup property will be - * filled in, or %NULL to disable the button. + * Sets the #GMenuModel from which the popup will be constructed, + * 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. + * Depending on the value of #GtkMenuButton:use-popover, either a + * #GtkMenu will be created with gtk_menu_new_from_model(), or a + * #GtkPopover with gtk_popover_new_from_model(). In either case, + * actions will be connected as documented for these functions. * - * If #GtkMenuButton:popup is already set, then its content will be lost - * and replaced by our newly created #GtkMenu. + * If #GtkMenuButton:popup or #GtkMenuButton:popover are already set, + * their content will be lost and replaced by the newly created popup. * * Since: 3.6 */ @@ -711,33 +786,45 @@ gtk_menu_button_set_menu_model (GtkMenuButton *menu_button, GMenuModel *menu_model) { 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); + gtk_menu_button_set_popup (menu_button, NULL); + gtk_menu_button_set_popover (menu_button, NULL); if (menu_model == NULL) + return; + + priv->model = g_object_ref (menu_model); + + if (priv->use_popover) { - gtk_menu_button_set_popup (menu_button, NULL); - return; + GtkWidget *popover; + + popover = gtk_popover_new_from_model (GTK_WIDGET (menu_button), menu_model); + gtk_menu_button_set_popover (menu_button, popover); } + else + { + GtkWidget *menu; - priv->model = g_object_ref (menu_model); - menu = gtk_menu_new_from_model (menu_model); - gtk_widget_show_all (menu); - gtk_menu_button_set_popup (menu_button, menu); + menu = gtk_menu_new_from_model (menu_model); + gtk_widget_show_all (menu); + gtk_menu_button_set_popup (menu_button, menu); + } } /** * gtk_menu_button_get_menu_model: * @menu_button: a #GtkMenuButton * - * Returns the #GMenuModel used to generate the menu. + * Returns the #GMenuModel used to generate the popup. * - * Returns: (transfer none): a #GMenuModel or %NULL. + * Returns: (transfer none): a #GMenuModel or %NULL * * Since: 3.6 */ @@ -771,12 +858,15 @@ set_align_widget_pointer (GtkMenuButton *menu_button, * @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. + * 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 + * Setting it to %NULL means that the menu will be aligned with the * button itself. * + * Note that this property is only used with menus currently, + * and not for popovers. + * * Since: 3.6 */ void @@ -803,7 +893,7 @@ gtk_menu_button_set_align_widget (GtkMenuButton *menu_button, * * Returns the parent #GtkWidget to use to line up with menu. * - * Returns: (transfer none): a #GtkWidget value or %NULL. + * Returns: (transfer none): a #GtkWidget value or %NULL * * Since: 3.6 */ @@ -815,19 +905,45 @@ gtk_menu_button_get_align_widget (GtkMenuButton *menu_button) return menu_button->priv->align_widget; } +static void +update_popover_direction (GtkMenuButton *menu_button) +{ + GtkMenuButtonPrivate *priv = menu_button->priv; + + if (!priv->popover) + return; + + switch (priv->arrow_type) + { + case GTK_ARROW_UP: + gtk_popover_set_position (GTK_POPOVER (priv->popover), GTK_POS_TOP); + break; + case GTK_ARROW_DOWN: + case GTK_ARROW_NONE: + gtk_popover_set_position (GTK_POPOVER (priv->popover), GTK_POS_BOTTOM); + break; + case GTK_ARROW_LEFT: + gtk_popover_set_position (GTK_POPOVER (priv->popover), GTK_POS_LEFT); + break; + case GTK_ARROW_RIGHT: + gtk_popover_set_position (GTK_POPOVER (priv->popover), GTK_POS_RIGHT); + break; + } +} + /** * gtk_menu_button_set_direction: * @menu_button: a #GtkMenuButton * @direction: a #GtkArrowType * - * Sets the direction in which the menu will be popped up, as + * Sets the direction in which the popup 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 the does not fit in the available space in the given direction, + * GTK+ will its best to keep it inside the screen and fully visible. * - * If you pass %GTK_ARROW_NONE for a @direction, the menu will behave + * If you pass %GTK_ARROW_NONE for a @direction, the popup will behave * as if you passed %GTK_ARROW_DOWN (although you won’t see any arrows). * * Since: 3.6 @@ -852,15 +968,17 @@ gtk_menu_button_set_direction (GtkMenuButton *menu_button, return; gtk_arrow_set (GTK_ARROW (child), priv->arrow_type, GTK_SHADOW_NONE); + + update_popover_direction (menu_button); } /** * gtk_menu_button_get_direction: * @menu_button: a #GtkMenuButton * - * Returns the direction the menu will be pointing at when popped up. + * Returns the direction the popup will be pointing at when popped up. * - * Returns: a #GtkArrowType value. + * Returns: a #GtkArrowType value * * Since: 3.6 */ @@ -877,13 +995,19 @@ gtk_menu_button_dispose (GObject *object) { GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (object)->priv; - if (priv->popup) + if (priv->menu) { - g_signal_handlers_disconnect_by_func (priv->popup, + g_signal_handlers_disconnect_by_func (priv->menu, menu_deactivate_cb, object); - gtk_menu_detach (GTK_MENU (priv->popup)); - priv->popup = NULL; + gtk_menu_detach (GTK_MENU (priv->menu)); + priv->menu = NULL; + } + + if (priv->popover) + { + gtk_widget_destroy (priv->popover); + priv->popover = NULL; } set_align_widget_pointer (GTK_MENU_BUTTON (object), NULL); @@ -892,3 +1016,124 @@ gtk_menu_button_dispose (GObject *object) G_OBJECT_CLASS (gtk_menu_button_parent_class)->dispose (object); } + +/** + * gtk_menu_button_set_use_popover: + * @menu_button: a #GtkMenuButton + * @use_popover: %TRUE to construct a popover from the menu model + * + * Sets whether to construct a #GtkPopover instead of #GtkMenu + * when gtk_menu_button_set_menu_model() is called. Note that + * this property is only consulted when a new menu model is set. + * + * Since: 3.12 + */ +void +gtk_menu_button_set_use_popover (GtkMenuButton *menu_button, + gboolean use_popover) +{ + GtkMenuButtonPrivate *priv; + + g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button)); + + priv = menu_button->priv; + + use_popover = use_popover != FALSE; + + if (priv->use_popover == use_popover) + return; + + priv->use_popover = use_popover; + + g_object_notify (G_OBJECT (menu_button), "use-popover"); +} + +/** + * gtk_menu_button_get_use_popover: + * @menu_button: a #GtkMenuButton + * + * Returns whether a #GtkPopover or a #GtkMenu will be constructed + * from the menu model. + * + * Return value: %TRUE if using a #GtkPopover + * + * Since: 3.12 + */ +gboolean +gtk_menu_button_get_use_popover (GtkMenuButton *menu_button) +{ + g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), FALSE); + + return menu_button->priv->use_popover; +} + +/** + * gtk_menu_button_set_popover: + * @menu_button: a #GtkMenuButton + * @popover: (allow-none): a #GtkPopover + * + * Sets the #GtkPopover that will be popped up when the button is + * clicked, or %NULL to disable the button. If #GtkMenuButton:menu-model + * or #GtkMenuButton:popup are set, they will be set to %NULL. + * + * Since: 3.12 + */ +void +gtk_menu_button_set_popover (GtkMenuButton *menu_button, + GtkWidget *popover) +{ + GtkMenuButtonPrivate *priv = menu_button->priv; + + g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button)); + g_return_if_fail (GTK_IS_POPOVER (popover) || popover == NULL); + + g_clear_object (&priv->model); + + if (priv->popover) + { + if (gtk_widget_get_visible (priv->popover)) + gtk_widget_hide (priv->popover); + + g_signal_handlers_disconnect_by_func (priv->popover, + menu_deactivate_cb, + menu_button); + gtk_popover_set_relative_to (GTK_POPOVER (priv->popover), NULL); + } + + priv->popover = popover; + + if (popover) + { + gtk_popover_set_relative_to (GTK_POPOVER (priv->popover), GTK_WIDGET (menu_button)); + g_signal_connect_swapped (priv->popover, "closed", + G_CALLBACK (menu_deactivate_cb), menu_button); + update_popover_direction (menu_button); + gtk_style_context_remove_class (gtk_widget_get_style_context (GTK_WIDGET (menu_button)), "menu-button"); + } + + if (popover && priv->menu) + gtk_menu_button_set_popup (menu_button, NULL); + + gtk_widget_set_sensitive (GTK_WIDGET (menu_button), + priv->menu != NULL || priv->popover != NULL); +} + +/** + * gtk_menu_button_get_popover: + * @menu_button: a #GtkMenuButton + * + * Returns the #GtkPopover that pops out of the button. + * If the button is not using a #GtkPopover, this function + * returns %NULL. + * + * Returns: (transfer none): a #GtkPopover or %NULL + * + * Since: 3.12 + */ +GtkPopover * +gtk_menu_button_get_popover (GtkMenuButton *menu_button) +{ + g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL); + + return GTK_POPOVER (menu_button->priv->popover); +} diff --git a/gtk/gtkmenubutton.h b/gtk/gtkmenubutton.h index 8aa2644cd2..82a876b606 100644 --- a/gtk/gtkmenubutton.h +++ b/gtk/gtkmenubutton.h @@ -27,6 +27,7 @@ #include <gtk/gtktogglebutton.h> #include <gtk/gtkmenu.h> +#include <gtk/gtkpopover.h> G_BEGIN_DECLS @@ -67,10 +68,16 @@ GtkWidget *gtk_menu_button_new (void); GDK_AVAILABLE_IN_3_6 void gtk_menu_button_set_popup (GtkMenuButton *menu_button, - GtkWidget *popup); + GtkWidget *menu); GDK_AVAILABLE_IN_3_6 GtkMenu *gtk_menu_button_get_popup (GtkMenuButton *menu_button); +GDK_AVAILABLE_IN_3_12 +void gtk_menu_button_set_popover (GtkMenuButton *menu_button, + GtkWidget *popover); +GDK_AVAILABLE_IN_3_12 +GtkPopover *gtk_menu_button_get_popover (GtkMenuButton *menu_button); + GDK_AVAILABLE_IN_3_6 void gtk_menu_button_set_direction (GtkMenuButton *menu_button, GtkArrowType direction); @@ -89,6 +96,14 @@ void gtk_menu_button_set_align_widget (GtkMenuButton *menu_button, GDK_AVAILABLE_IN_3_6 GtkWidget *gtk_menu_button_get_align_widget (GtkMenuButton *menu_button); +GDK_AVAILABLE_IN_3_12 +void gtk_menu_button_set_use_popover (GtkMenuButton *menu_button, + gboolean use_popover); + +GDK_AVAILABLE_IN_3_12 +gboolean gtk_menu_button_get_use_popover (GtkMenuButton *menu_button); + + G_END_DECLS #endif /* __GTK_MENU_BUTTON_H__ */ |