Class MenuButton

All Implemented Interfaces:
PointerInterface

public class MenuButton extends Widget
The `GtkMenuButton` widget is used to display a popup when clicked.

![An example GtkMenuButton](menu-button.png)

This popup can be provided either as a `GtkPopover` or as an abstract
`GMenuModel`.

The `GtkMenuButton` widget can show either an icon (set with the
[property@Gtk.MenuButton:icon-name] property) or a label (set with the
[property@Gtk.MenuButton:label] property). If neither is explicitly set,
a [class@Gtk.Image] is automatically created, using an arrow image oriented
according to [property@Gtk.MenuButton:direction] or the generic
“open-menu-symbolic” icon if the direction is not set.

The positioning of the popup is determined by the
[property@Gtk.MenuButton:direction] property of the menu button.

For menus, the [property@Gtk.Widget:halign] and [property@Gtk.Widget: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”.

| | start | center | end |
| - | --- | --- | --- |
| **down** | ![](down-start.png) | ![](down-center.png) | ![](down-end.png) |
| **up** | ![](up-start.png) | ![](up-center.png) | ![](up-end.png) |
| **left** | ![](left-start.png) | ![](left-center.png) | ![](left-end.png) |
| **right** | ![](right-start.png) | ![](right-center.png) | ![](right-end.png) |

# CSS nodes

```
menubutton
╰── button.toggle
╰── <content>
╰── [arrow]
```

`GtkMenuButton` has a single CSS node with name `menubutton`
which contains a `button` node with a `.toggle` style class.

If the button contains an icon, it will have the `.image-button` style class,
if it contains text, it will have `.text-button` style class. If an arrow is
visible in addition to an icon, text or a custom child, it will also have
`.arrow-button` style class.

Inside the toggle button content, there is an `arrow` node for
the indicator, which will carry one of the `.none`, `.up`, `.down`,
`.left` or `.right` style classes to indicate the direction that
the menu will appear in. The CSS is expected to provide a suitable
image for each of these cases using the `-gtk-icon-source` property.

Optionally, the `menubutton` node can carry the `.circular` style class
to request a round appearance.

# Accessibility

`GtkMenuButton` uses the %GTK_ACCESSIBLE_ROLE_BUTTON role.

https://docs.gtk.org/gtk4/class.MenuButton.html

  • Field Details

  • Constructor Details

    • MenuButton

      public MenuButton(PointerContainer pointer)
    • MenuButton

      public MenuButton()
      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.
  • Method Details

    • getClassHandler

      public static ClassHandler getClassHandler()
    • getAlwaysShowArrow

      public boolean getAlwaysShowArrow()
      Gets whether to show a dropdown arrow even when using an icon.
      Returns:
      whether to show a dropdown arrow even when using an icon
    • getChild

      public Widget getChild()
      Gets the child widget of @menu_button.
      Returns:
      the child widget of @menu_button
    • getDirection

      public int getDirection()
      Returns the direction the popup will be pointing at when popped up.
      Overrides:
      getDirection in class Widget
      Returns:
      a `GtkArrowType` value
    • getHasFrame

      public boolean getHasFrame()
      Returns whether the button has a frame.
      Returns:
      %TRUE if the button has a frame
    • getIconName

      public Str getIconName()
      Gets the name of the icon shown in the button.
      Returns:
      the name of the icon shown in the button
    • getLabel

      public Str getLabel()
      Gets the label shown in the button
      Returns:
      the label shown in the button
    • getMenuModel

      public MenuModel getMenuModel()
      Returns the `GMenuModel` used to generate the popup.
      Returns:
      a `GMenuModel`
    • getPopover

      public Popover getPopover()
      Returns the `GtkPopover` that pops out of the button.

      If the button is not using a `GtkPopover`, this function
      returns %NULL.
      Returns:
      a `GtkPopover` or %NULL
    • getPrimary

      public boolean getPrimary()
      Returns whether the menu button acts as a primary menu.
      Returns:
      %TRUE if the button is a primary menu
    • getUseUnderline

      public boolean getUseUnderline()
      Returns whether an embedded underline in the text indicates a
      mnemonic.
      Returns:
      %TRUE whether an embedded underline in the text indicates the mnemonic accelerator keys.
    • popdown

      public void popdown()
      Dismiss the menu.
    • popup

      public void popup()
      Pop up the menu.
    • setAlwaysShowArrow

      public void setAlwaysShowArrow(boolean always_show_arrow)
      Sets whether to show a dropdown arrow even when using an icon or a custom
      child.
      Parameters:
      always_show_arrow - hether to show a dropdown arrow even when using an icon
    • setChild

      public void setChild(@Nullable Widget child)
      Sets the child widget of @menu_button.

      Setting a child resets [property@Gtk.MenuButton:label] and
      [property@Gtk.MenuButton:icon-name].

      If [property@Gtk.MenuButton:always-show-arrow] is set to `TRUE` and
      [property@Gtk.MenuButton:direction] is not `GTK_ARROW_NONE`, a dropdown arrow
      will be shown next to the child.
      Parameters:
      child - the child widget
    • setCreatePopupFunc

      public void setCreatePopupFunc(MenuButton.OnMenuButtonCreatePopupFunc func, @Nullable Pointer user_data, MenuButton.OnDestroyNotify destroy_notify)
      Sets @func to be called when a popup is about to be shown.

      @func should use one of

      - [method@Gtk.MenuButton.set_popover]
      - [method@Gtk.MenuButton.set_menu_model]

      to set a popup for @menu_button.
      If @func is non-%NULL, @menu_button will always be sensitive.

      Using this function will not reset the menu widget attached to
      @menu_button. Instead, this can be done manually in @func.
      Parameters:
      func - function to call when a popup is about to be shown, but none has been provided via other means, or %NULL to reset to default behavior.
      user_data - user data to pass to @func.
      destroy_notify - destroy notify for @user_data
    • setDirection

      public void setDirection(int direction)
      Sets the direction in which the popup will be popped up.

      If the button is automatically populated with an arrow icon,
      its direction will be changed to match.

      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 popup will behave
      as if you passed %GTK_ARROW_DOWN (although you won’t see any arrows).
      Overrides:
      setDirection in class Widget
      Parameters:
      direction - a `GtkArrowType`
    • setHasFrame

      public void setHasFrame(boolean has_frame)
      Sets the style of the button.
      Parameters:
      has_frame - whether the button should have a visible frame
    • setIconName

      public void setIconName(@Nonnull Str icon_name)
      Sets the name of an icon to show inside the menu button.

      Setting icon name resets [property@Gtk.MenuButton:label] and
      [property@Gtk.MenuButton:child].

      If [property@Gtk.MenuButton:always-show-arrow] is set to `TRUE` and
      [property@Gtk.MenuButton:direction] is not `GTK_ARROW_NONE`, a dropdown arrow
      will be shown next to the icon.
      Parameters:
      icon_name - the icon name
    • setIconName

      public void setIconName(String icon_name)
      Sets the name of an icon to show inside the menu button.

      Setting icon name resets [property@Gtk.MenuButton:label] and
      [property@Gtk.MenuButton:child].

      If [property@Gtk.MenuButton:always-show-arrow] is set to `TRUE` and
      [property@Gtk.MenuButton:direction] is not `GTK_ARROW_NONE`, a dropdown arrow
      will be shown next to the icon.
      Parameters:
      icon_name - the icon name
    • setLabel

      public void setLabel(@Nonnull Str label)
      Sets the label to show inside the menu button.

      Setting a label resets [property@Gtk.MenuButton:icon-name] and
      [property@Gtk.MenuButton:child].

      If [property@Gtk.MenuButton:direction] is not `GTK_ARROW_NONE`, a dropdown
      arrow will be shown next to the label.
      Parameters:
      label - the label
    • setLabel

      public void setLabel(String label)
      Sets the label to show inside the menu button.

      Setting a label resets [property@Gtk.MenuButton:icon-name] and
      [property@Gtk.MenuButton:child].

      If [property@Gtk.MenuButton:direction] is not `GTK_ARROW_NONE`, a dropdown
      arrow will be shown next to the label.
      Parameters:
      label - the label
    • setMenuModel

      public void setMenuModel(@Nullable MenuModel menu_model)
      Sets the `GMenuModel` from which the popup will be constructed.

      If @menu_model is %NULL, the button is disabled.

      A [class@Gtk.Popover] will be created from the menu model with
      [ctor@Gtk.PopoverMenu.new_from_model]. Actions will be connected
      as documented for this function.

      If [property@Gtk.MenuButton:popover] is already set, it will be
      dissociated from the @menu_button, and the property is set to %NULL.
      Parameters:
      menu_model - a `GMenuModel`, or %NULL to unset and disable the button
    • setPopover

      public void setPopover(@Nullable Widget popover)
      Sets the `GtkPopover` that will be popped up when the @menu_button is clicked.

      If @popover is %NULL, the button is disabled.

      If [property@Gtk.MenuButton:menu-model] is set, the menu model is dissociated
      from the @menu_button, and the property is set to %NULL.
      Parameters:
      popover - a `GtkPopover`, or %NULL to unset and disable the button
    • setPrimary

      public void setPrimary(boolean primary)
      Sets whether menu button acts as a primary menu.

      Primary menus can be opened with the <kbd>F10</kbd> key.
      Parameters:
      primary - whether the menubutton should act as a primary menu
    • setUseUnderline

      public void setUseUnderline(boolean use_underline)
      If true, an underline in the text indicates a mnemonic.
      Parameters:
      use_underline - %TRUE if underlines in the text indicate mnemonics
    • onActivate

      public SignalHandler onActivate(MenuButton.OnActivate signal)
      Connect to signal "activate".
      See MenuButton.OnActivate.onActivate() for signal description.
      Field SIGNAL_ON_ACTIVATE contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • asAccessible

      public Accessible asAccessible()
      Implements interface Accessible. Call this to get access to interface functions.
      Overrides:
      asAccessible in class Widget
      Returns:
      Accessible
    • asBuildable

      public Buildable asBuildable()
      Implements interface Buildable. Call this to get access to interface functions.
      Overrides:
      asBuildable in class Widget
      Returns:
      Buildable
    • asConstraintTarget

      public ConstraintTarget asConstraintTarget()
      Implements interface ConstraintTarget. Call this to get access to interface functions.
      Overrides:
      asConstraintTarget in class Widget
      Returns:
      ConstraintTarget
    • getTypeID

      public static long getTypeID()
    • getParentTypeID

      public static long getParentTypeID()
    • getTypeSize

      public static TypeSystem.TypeSize getTypeSize()
    • getParentTypeSize

      public static TypeSystem.TypeSize getParentTypeSize()
    • getInstanceSize

      public static int getInstanceSize()