gtk.Menu



class Menu : gtk.MenuShell.MenuShell;
A #GtkMenu is a #GtkMenuShell that implements a drop down menu consisting of a list of #GtkMenuItem objects which can be navigated and activated by the user to perform application functions.

A #GtkMenu is most commonly dropped down by activating a #GtkMenuItem in a #GtkMenuBar or popped up by activating a #GtkMenuItem in another #GtkMenu.

A #GtkMenu can also be popped up by activating a #GtkComboBox. Other composite widgets such as the #GtkNotebook can pop up a #GtkMenu as well.

Applications can display a #GtkMenu as a popup menu by calling the gtk_menu_popup() function. The example below shows how an application can pop up a menu when the 3rd mouse button is pressed.

## Connecting the popup signal handler.

|[ // connect our handler which will popup the menu g_signal_connect_swapped (window, "button_press_event", G_CALLBACK (my_popup_handler), menu); ]|

## Signal handler which displays a popup menu.

|[ static gint my_popup_handler (GtkWidget *widget, GdkEvent *event) { GtkMenu *menu; GdkEventButton *event_button;

g_return_val_if_fail (widget != NULL, FALSE); g_return_val_if_fail (GTK_IS_MENU (widget), FALSE); g_return_val_if_fail (event != NULL, FALSE);

// The "widget" is the menu that was supplied when // g_signal_connect_swapped() was called. menu = GTK_MENU (widget);

if (event->type == GDK_BUTTON_PRESS) { event_button = (GdkEventButton *) event; if (event_button->button == GDK_BUTTON_SECONDARY) { gtk_menu_popup (menu, NULL, NULL, NULL, NULL, event_button->button, event_button->time); return TRUE; } }

return FALSE; } ]|

# CSS nodes

|[ menu ├── arrow.top ├── ┊ ├── ╰── arrow.bottom ]|

The main CSS node of GtkMenu has name menu, and there are two subnodes with name arrow, for scrolling menu arrows. These subnodes get the .top and .bottom style classes.

protected GtkMenu* gtkMenu ;
the main Gtk struct

GtkMenu* getMenuStruct ();
Get the main Gtk struct

protected void* getStruct ();
the main Gtk struct as a void*

this(GtkMenu* gtkMenu, bool ownedRef = false);
Sets our main struct and passes it to the parent class.

void popup (uint button, uint activateTime);
Popups up this menu

Params:
uint button you can pass a button number here
uint activateTime you can pass the time from an event here

Menu appendSubmenu (string label);
Creates and append a submenu to this menu. This menu item that actualy has the sub menu is also created.

Params:
string label the sub menu item label

Returns:
the new menu

void appendSubmenu (string label, Menu submenu);


Menu prependSubmenu (string label);


static GType getType ();


this();
Creates a new #GtkMenu

Return:
a new #GtkMenu

Throws:
ConstructionException GTK+ fails to create the object.

this(MenuModel model);
Creates a #GtkMenu and populates it with menu items and submenus according to @model.

The created menu items are connected to actions found in the #GtkApplicationWindow to which the menu belongs - typically by means of being attached to a widget (see gtk_menu_attach_to_widget()) that is contained within the #GtkApplicationWindows widget hierarchy.

Actions can also be added using gtk_widget_insert_action_group() on the menu's attach widget or on any of its parent widgets.

Params:
MenuModel model a #GMenuModel

Return:
a new #GtkMenu

Since:
3.4

Throws:
ConstructionException GTK+ fails to create the object.

static ListG getForAttachWidget (Widget widget);
Returns a list of the menus which are attached to this widget. This list is owned by GTK+ and must not be modified.

Params:
Widget widget a #GtkWidget

Return:
the list of menus attached to his widget.

Since:
2.6

void attach (Widget child, uint leftAttach, uint rightAttach, uint topAttach, uint bottomAttach);
Adds a new #GtkMenuItem to a (table) menu. The number of “cells” that an item will occupy is specified by @left_attach, @right_attach, @top_attach and @bottom_attach. These each represent the leftmost, rightmost, uppermost and lower column and row numbers of the table. (Columns and rows are indexed from zero).

Note that this function is not related to gtk_menu_detach().

Params:
Widget child a #GtkMenuItem
uint leftAttach The column number to attach the left side of the item to
uint rightAttach The column number to attach the right side of the item to
uint topAttach The row number to attach the top of the item to
uint bottomAttach The row number to attach the bottom of the item to

Since:
2.4

void attachToWidget (Widget attachWidget, GtkMenuDetachFunc detacher);
Attaches the menu to the widget and provides a callback function that will be invoked when the menu calls gtk_menu_detach() during its destruction.

If the menu is attached to the widget then it will be destroyed when the widget is destroyed, as if it was a child widget. An attached menu will also move between screens correctly if the widgets moves between screens.

Params:
Widget attachWidget the #GtkWidget that the menu will be attached to
GtkMenuDetachFunc detacher the user supplied callback function that will be called when the menu calls gtk_menu_detach()

void detach ();
Detaches the menu from the widget to which it had been attached. This function will call the callback function, @detacher, provided when the gtk_menu_attach_to_widget() function was called.

AccelGroup getAccelGroup ();
Gets the #GtkAccelGroup which holds global accelerators for the menu. See gtk_menu_set_accel_group().

Return:
the #GtkAccelGroup associated with the menu

string getAccelPath ();
Retrieves the accelerator path set on the menu.

Return:
the accelerator path set on the menu.

Since:
2.14

Widget getActive ();
Returns the selected menu item from the menu. This is used by the #GtkComboBox.

Return:
the #GtkMenuItem that was last selected in the menu. If a selection has not yet been made, the first menu item is selected.

Widget getAttachWidget ();
Returns the #GtkWidget that the menu is attached to.

Return:
the #GtkWidget that the menu is attached to

int getMonitor ();
Retrieves the number of the monitor on which to show the menu.

Return:
the number of the monitor on which the menu should be popped up or -1, if no monitor has been set

Since:
2.14

bool getReserveToggleSize ();
Returns whether the menu reserves space for toggles and icons, regardless of their actual presence.

Return:
Whether the menu reserves toggle space

Since:
2.18

bool getTearoffState ();
Returns whether the menu is torn off. See gtk_menu_set_tearoff_state().

Return:
%TRUE if the menu is currently torn off.

string getTitle ();
Returns the title of the menu. See gtk_menu_set_title().

Return:
the title of the menu, or %NULL if the menu has no title set on it. This string is owned by GTK+ and should not be modified or freed.

void placeOnMonitor (MonitorG monitor);
Places @menu on the given monitor.

Params:
MonitorG monitor the monitor to place the menu on

Since:
3.22

void popdown ();
Removes the menu from the screen.

void popup (Widget parentMenuShell, Widget parentMenuItem, GtkMenuPositionFunc func, void* data, uint button, uint activateTime);
Displays a menu and makes it available for selection.

Applications can use this function to display context-sensitive menus, and will typically supply %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data parameters. The default menu positioning function will position the menu at the current mouse cursor position.

The @button parameter should be the mouse button pressed to initiate the menu popup . If the menu popup was initiated by something other than a mouse button press, such as a mouse button release or a keypress, @button should be 0.

The @activate_time parameter is used to conflict-resolve initiation of concurrent requests for mouse/keyboard grab requests. To function properly, this needs to be the timestamp of the user event (such as a mouse click or key press) that caused the initiation of the popup . Only if no such event is available, gtk_get_current_event_time() can be used instead.

Note that this function does not work very well on GDK backends that do not have global coordinates, such as Wayland or Mir. You should probably use one of the gtk_menu_popup_at_ variants, which do not have this problem.

Deprecated:
Please use gtk_menu_popup_at_widget(), gtk_menu_popup_at_pointer(). or gtk_menu_popup_at_rect() instead

Params:
Widget parentMenuShell the menu shell containing the triggering menu item, or %NULL
Widget parentMenuItem the menu item whose activation triggered the popup , or %NULL
GtkMenuPositionFunc func a user supplied function used to position the menu, or %NULL
void* data user supplied data to be passed to @func.
uint button the mouse button which was pressed to initiate the event.
uint activateTime the time at which the activation event occurred.

void popupAtPointer (Event triggerEvent);
Displays @menu and makes it available for selection.

See gtk_menu_popup_at_widget () to pop up a menu at a widget. gtk_menu_popup_at_rect () also allows you to position a menu at an arbitrary rectangle.

@menu will be positioned at the pointer associated with @trigger_event.

Properties that influence the behaviour of this function are #GtkMenu:anchor-hints, #GtkMenu:rect-anchor-dx, #GtkMenu:rect-anchor-dy, and #GtkMenu:menu-type-hint. Connect to the #GtkMenu::popped-up signal to find out how it was actually positioned.

Params:
Event triggerEvent the #GdkEvent that initiated this request or %NULL if it's the current event

Since:
3.22

void popupAtRect (Window rectWindow, GdkRectangle* rect, GdkGravity rectAnchor, GdkGravity menuAnchor, Event triggerEvent);
Displays @menu and makes it available for selection.

See gtk_menu_popup_at_widget () and gtk_menu_popup_at_pointer (), which handle more common cases for popping up menus.

@menu will be positioned at @rect, aligning their anchor points. @rect is relative to the top-left corner of @rect_window. @rect_anchor and @menu_anchor determine anchor points on @rect and @menu to pin together. @menu can optionally be offset by #GtkMenu:rect-anchor-dx and #GtkMenu:rect-anchor-dy.

Anchors should be specified under the assumption that the text direction is left-to-right; they will be flipped horizontally automatically if the text direction is right-to-left.

Other properties that influence the behaviour of this function are #GtkMenu:anchor-hints and #GtkMenu:menu-type-hint. Connect to the #GtkMenu::popped-up signal to find out how it was actually positioned.

Params:
Window rectWindow the #GdkWindow @rect is relative to
GdkRectangle* rect the #GdkRectangle to align @menu with
GdkGravity rectAnchor the point on @rect to align with @menu's anchor point
GdkGravity menuAnchor the point on @menu to align with @rect's anchor point
Event triggerEvent the #GdkEvent that initiated this request or %NULL if it's the current event

Since:
3.22

void popupAtWidget (Widget widget, GdkGravity widgetAnchor, GdkGravity menuAnchor, Event triggerEvent);
Displays @menu and makes it available for selection.

See gtk_menu_popup_at_pointer () to pop up a menu at the master pointer. gtk_menu_popup_at_rect () also allows you to position a menu at an arbitrary rectangle.

![](popup-anchors.png)

@menu will be positioned at @widget, aligning their anchor points. @widget_anchor and @menu_anchor determine anchor points on @widget and @menu to pin together. @menu can optionally be offset by #GtkMenu:rect-anchor-dx and #GtkMenu:rect-anchor-dy.

Anchors should be specified under the assumption that the text direction is left-to-right; they will be flipped horizontally automatically if the text direction is right-to-left.

Other properties that influence the behaviour of this function are #GtkMenu:anchor-hints and #GtkMenu:menu-type-hint. Connect to the #GtkMenu::popped-up signal to find out how it was actually positioned.

Params:
Widget widget the #GtkWidget to align @menu with
GdkGravity widgetAnchor the point on @widget to align with @menu's anchor point
GdkGravity menuAnchor the point on @menu to align with @widget's anchor point
Event triggerEvent the #GdkEvent that initiated this request or %NULL if it's the current event

Since:
3.22

void popupForDevice (Device device, Widget parentMenuShell, Widget parentMenuItem, GtkMenuPositionFunc func, void* data, GDestroyNotify destroy, uint button, uint activateTime);
Displays a menu and makes it available for selection.

Applications can use this function to display context-sensitive menus, and will typically supply %NULL for the @parent_menu_shell, @parent_menu_item, @func, @data and @destroy parameters. The default menu positioning function will position the menu at the current position of @device (or its corresponding pointer).

The @button parameter should be the mouse button pressed to initiate the menu popup. If the menu popup was initiated by something other than a mouse button press, such as a mouse button release or a keypress, @button should be 0.

The @activate_time parameter is used to conflict-resolve initiation of concurrent requests for mouse/keyboard grab requests. To function properly, this needs to be the time stamp of the user event (such as a mouse click or key press) that caused the initiation of the popup. Only if no such event is available, gtk_get_current_event_time() can be used instead.

Note that this function does not work very well on GDK backends that do not have global coordinates, such as Wayland or Mir. You should probably use one of the gtk_menu_popup_at_ variants, which do not have this problem.

Deprecated:
Please use gtk_menu_popup_at_widget(), gtk_menu_popup_at_pointer(). or gtk_menu_popup_at_rect() instead

Params:
Device device a #GdkDevice
Widget parentMenuShell the menu shell containing the triggering menu item, or %NULL
Widget parentMenuItem the menu item whose activation triggered the popup, or %NULL
GtkMenuPositionFunc func a user supplied function used to position the menu, or %NULL
void* data user supplied data to be passed to @func
GDestroyNotify destroy destroy notify for @data
uint button the mouse button which was pressed to initiate the event
uint activateTime the time at which the activation event occurred

Since:
3.0

void reorderChild (Widget child, int position);
Moves @child to a new @position in the list of @menu children.

Params:
Widget child the #GtkMenuItem to move
int position the new position to place @child. Positions are numbered from 0 to n - 1

void reposition ();
Repositions the menu according to its position function.

void setAccelGroup (AccelGroup accelGroup);
Set the #GtkAccelGroup which holds global accelerators for the menu. This accelerator group needs to also be added to all windows that this menu is being used in with gtk_window_add_accel_group(), in order for those windows to support all the accelerators contained in this group.

Params:
AccelGroup accelGroup the #GtkAccelGroup to be associated with the menu.

void setAccelPath (string accelPath);
Sets an accelerator path for this menu from which accelerator paths for its immediate children, its menu items, can be constructed. The main purpose of this function is to spare the programmer the inconvenience of having to call gtk_menu_item_set_accel_path() on each menu item that should support runtime user changable accelerators. Instead, by just calling gtk_menu_set_accel_path() on their parent, each menu item of this menu, that contains a label describing its purpose, automatically gets an accel path assigned.

For example, a menu containing menu items “New” and “Exit”, will, after
gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");
has been called, assign its items the accel paths:
"<Gnumeric-Sheet>/File/New"
and
"<Gnumeric-Sheet>/File/Exit"
.

Assigning accel paths to menu items then enables the user to change their accelerators at runtime. More details about accelerator paths and their default setups can be found at gtk_accel_map_add_entry().

Note that @accel_path string will be stored in a #GQuark. Therefore, if you pass a static string, you can save some memory by interning it first with g_intern_static_string().

Params:
string accelPath a valid accelerator path

void setActive (uint index);
Selects the specified menu item within the menu. This is used by the #GtkComboBox and should not be used by anyone else.

Params:
uint index the index of the menu item to select. Index values are from 0 to n-1

void setMonitor (int monitorNum);
Informs GTK+ on which monitor a menu should be popped up. See gdk_monitor_get_geometry().

This function should be called from a #GtkMenuPositionFunc if the menu should not appear on the same monitor as the pointer. This information can’t be reliably inferred from the coordinates returned by a #GtkMenuPositionFunc, since, for very long menus, these coordinates may extend beyond the monitor boundaries or even the screen boundaries.

Params:
int monitorNum the number of the monitor on which the menu should be popped up

Since:
2.4

void setReserveToggleSize (bool reserveToggleSize);
Sets whether the menu should reserve space for drawing toggles or icons, regardless of their actual presence.

Params:
bool reserveToggleSize whether to reserve size for toggles

Since:
2.18

void setScreen (Screen screen);
Sets the #GdkScreen on which the menu will be displayed.

Params:
Screen screen a #GdkScreen, or %NULL if the screen should be determined by the widget the menu is attached to

Since:
2.2

void setTearoffState (bool tornOff);
Changes the tearoff state of the menu. A menu is normally displayed as drop down menu which persists as long as the menu is active. It can also be displayed as a tearoff menu which persists until it is closed or reattached.

Params:
bool tornOff If %TRUE, menu is displayed as a tearoff menu.

void setTitle (string title);
Sets the title string for the menu.

The title is displayed when the menu is shown as a tearoff menu. If @title is %NULL, the menu will see if it is attached to a parent menu item, and if so it will try to use the same text as that menu item’s label.

Params:
string title a string containing the title for the menu

gulong addOnMoveScroll (void delegate(GtkScrollType, Menu) dlg, ConnectFlags connectFlags = cast(ConnectFlags)0);


gulong addOnPoppedUp (void delegate(void*, void*, bool, bool, Menu) dlg, ConnectFlags connectFlags = cast(ConnectFlags)0);
Emitted when the position of @menu is finalized after being popped up using gtk_menu_popup_at_rect (), gtk_menu_popup_at_widget (), or gtk_menu_popup_at_pointer ().

@menu might be flipped over the anchor rectangle in order to keep it on-screen, in which case @flipped_x and @flipped_y will be set to %TRUE accordingly.

@flipped_rect is the ideal position of @menu after any possible flipping, but before any possible sliding. @final_rect is @flipped_rect, but possibly translated in the case that flipping is still ineffective in keeping @menu on-screen.

![](popup-slide.png)

The blue menu is @menu's ideal position, the green menu is @flipped_rect, and the red menu is @final_rect.

See gtk_menu_popup_at_rect (), gtk_menu_popup_at_widget (), gtk_menu_popup_at_pointer (), #GtkMenu:anchor-hints, #GtkMenu:rect-anchor-dx, #GtkMenu:rect-anchor-dy, and #GtkMenu:menu-type-hint.

Params:
flippedRect the position of @menu after any possible flipping or %NULL if the backend can't obtain it
finalRect the final position of @menu or %NULL if the backend can't obtain it
flippedX %TRUE if the anchors were flipped horizontally
flippedY %TRUE if the anchors were flipped vertically

Since:
3.22

Page was generated with on Fri Jan 6 23:09:20 2017