Class MenuButton

All Implemented Interfaces:
Accessible, Buildable, ConstraintTarget, Proxy

@Generated("org.javagi.JavaGI") public class MenuButton extends Widget implements Accessible, Buildable, ConstraintTarget

Displays a popup when clicked.

An example GtkMenuButton

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 Gtk.MenuButton:icon-name property) or a label (set with the Gtk.MenuButton:label property). If neither is explicitly set, a Image is automatically created, using an arrow image oriented according to 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 Gtk.MenuButton:direction property of the menu button.

For menus, the Gtk.Widget:halign and Gtk.Widget:valign properties of the menu are also taken into account. For example, when the direction is ArrowType.DOWN and the horizontal alignment is 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 !Gtk.AccessibleRole.button role.
  • Constructor Details

    • MenuButton

      public MenuButton(MemorySegment address)
      Create a MenuButton instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • MenuButton

      public MenuButton()
      Create a new MenuButton.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the MenuButton class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected MenuButton asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class Widget
      Returns:
      the instance as if it were its parent type
    • getActive

      public boolean getActive()
      Returns whether the menu button is active.
      Returns:
      TRUE if the button is active
      Since:
      4.10
    • getAlwaysShowArrow

      public boolean getAlwaysShowArrow()
      Gets whether to show a dropdown arrow even when using an icon or a custom child.
      Returns:
      whether to show a dropdown arrow even when using an icon or a custom child.
      Since:
      4.4
    • getCanShrink

      public boolean getCanShrink()
      Retrieves whether the button can be smaller than the natural size of its contents.
      Returns:
      true if the button can shrink, and false otherwise
      Since:
      4.12
    • getChild

      public @Nullable Widget getChild()
      Gets the child widget of menuButton.
      Returns:
      the child widget of this MenuButton
      Since:
      4.6
    • getArrowDirection

      public ArrowType getArrowDirection()
      Returns the direction the popup will be pointing at when popped up.
      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 @Nullable String getIconName()
      Gets the name of the icon shown in the button.
      Returns:
      the name of the icon shown in the button
    • getLabel

      public @Nullable String getLabel()
      Gets the label shown in the button
      Returns:
      the label shown in the button
    • getMenuModel

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

      public @Nullable 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
      Since:
      4.4
    • 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.
    • setActive

      public void setActive(boolean active)
      Sets whether the menu button is active.
      Parameters:
      active - whether the menu button is active
      Since:
      4.10
    • setAlwaysShowArrow

      public void setAlwaysShowArrow(boolean alwaysShowArrow)
      Sets whether to show a dropdown arrow even when using an icon or a custom child.
      Parameters:
      alwaysShowArrow - whether to show a dropdown arrow even when using an icon or a custom child
      Since:
      4.4
    • setCanShrink

      public void setCanShrink(boolean canShrink)

      Sets whether the button size can be smaller than the natural size of its contents.

      For text buttons, setting canShrink to true will ellipsize the label.

      For icon buttons, this function has no effect.

      Parameters:
      canShrink - whether the button can shrink
      Since:
      4.12
    • setChild

      public void setChild(@Nullable Widget child)

      Sets the child widget of menuButton.

      Setting a child resets Gtk.MenuButton:label and Gtk.MenuButton:icon-name.

      If Gtk.MenuButton:always-show-arrow is set to TRUE and Gtk.MenuButton:direction is not GTK_ARROW_NONE, a dropdown arrow will be shown next to the child.

      Parameters:
      child - the child widget
      Since:
      4.6
    • setCreatePopupFunc

      public void setCreatePopupFunc(@Nullable MenuButtonCreatePopupFunc func)

      Sets func to be called when a popup is about to be shown.

      func should use one of

      to set a popup for menuButton. If func is non-null, this MenuButton will always be sensitive.

      Using this function will not reset the menu widget attached to menuButton. 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
    • setDirection

      public void setDirection(ArrowType 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 ArrowType.NONE for a direction, the popup will behave as if you passed ArrowType.DOWN (although you won’t see any arrows).

      Parameters:
      direction - a GtkArrowType
    • setHasFrame

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

      public void setIconName(String iconName)

      Sets the name of an icon to show inside the menu button.

      Setting icon name resets Gtk.MenuButton:label and Gtk.MenuButton:child.

      If Gtk.MenuButton:always-show-arrow is set to TRUE and Gtk.MenuButton:direction is not GTK_ARROW_NONE, a dropdown arrow will be shown next to the icon.

      Parameters:
      iconName - the icon name
    • setLabel

      public void setLabel(String label)

      Sets the label to show inside the menu button.

      Setting a label resets Gtk.MenuButton:icon-name and Gtk.MenuButton:child.

      If 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 menuModel)

      Sets the GMenuModel from which the popup will be constructed.

      If menuModel is null, the button is disabled.

      A Popover will be created from the menu model with PopoverMenu.fromModel(MenuModel). Actions will be connected as documented for this function.

      If Gtk.MenuButton:popover is already set, it will be dissociated from the menuButton, and the property is set to null.

      Parameters:
      menuModel - a GMenuModel, or null to unset and disable the button
    • setPopover

      public void setPopover(@Nullable Popover popover)

      Sets the GtkPopover that will be popped up when the this MenuButton is clicked.

      If popover is null, the button is disabled.

      If Gtk.MenuButton:menu-model is set, the menu model is dissociated from the menuButton, 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 F10 key.

      Parameters:
      primary - whether the menubutton should act as a primary menu
      Since:
      4.4
    • setUseUnderline

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

      Emitted to when the menu button is activated.

      The ::activate signal on GtkMenuButton is an action signal and emitting it causes the button to pop up its menu.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      4.4
      See Also:
    • emitActivate

      public void emitActivate()
      Emits the "activate" signal. See onActivate(MenuButton.ActivateCallback).
    • builder

      public static MenuButton.Builder<? extends MenuButton.Builder> builder()
      A MenuButton.Builder object constructs a MenuButton with the specified properties. Use the various set...() methods to set properties, and finish construction with MenuButton.Builder.build().
      Returns:
      the builder object