GtkMenu

GtkMenu — A menu widget

Functions

Properties

GtkAccelGroup * accel-group Read / Write
gchar * accel-path Read / Write
gint active Read / Write
GdkAnchorHints anchor-hints Read / Write / Construct
GtkWidget * attach-widget Read / Write
GdkSurfaceTypeHint menu-type-hint Read / Write / Construct
gint monitor Read / Write
gint rect-anchor-dx Read / Write / Construct
gint rect-anchor-dy Read / Write / Construct
gboolean reserve-toggle-size Read / Write

Signals

Types and Values

Object Hierarchy

    GObject
    ╰── GInitiallyUnowned
        ╰── GtkWidget
            ╰── GtkContainer
                ╰── GtkMenuShell
                    ╰── GtkMenu

Implemented Interfaces

GtkMenu implements AtkImplementorIface and GtkBuildable.

Includes

#include <gtk/gtk.h>

Description

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 one of 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.

1
2
3
4
5
// connect our handler which will popup the menu
gesture = gtk_gesture_multi_press_new (window);
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (gesture),
                               GDK_BUTTON_SECONDARY);
g_signal_connect (gesture, "begin", G_CALLBACK (my_popup_handler), menu);

Signal handler which displays a popup menu.

1
2
3
4
5
6
7
8
9
10
11
static void
my_popup_handler (GtkGesture       *gesture,
                  GdkEventSequence *sequence
                  gpointer          data)
{
  GtkMenu *menu = data;
  const GdkEvent *event;

  event = gtk_gesture_get_last_event (gesture, sequence);
  gtk_menu_popup_at_pointer (menu, event);
}

CSS nodes

1
2
3
4
5
6
menu
├── arrow.top
├── <child>

├── <child>
╰── 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.

Functions

gtk_menu_new ()

GtkWidget *
gtk_menu_new (void);

Creates a new GtkMenu

Returns

a new GtkMenu

gtk_menu_new_from_model ()

GtkWidget *
gtk_menu_new_from_model (GMenuModel *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.

Parameters

model

a GMenuModel

 

Returns

a new GtkMenu

gtk_menu_reorder_child ()

void
gtk_menu_reorder_child (GtkMenu *menu,
                        GtkWidget *child,
                        gint position);

Moves child to a new position in the list of menu children.

Parameters

menu

a GtkMenu

 

child

the GtkMenuItem to move

 

position

the new position to place child . Positions are numbered from 0 to n - 1

 

gtk_menu_popup_at_rect ()

void
gtk_menu_popup_at_rect (GtkMenu *menu,
                        GdkSurface *rect_surface,
                        const GdkRectangle *rect,
                        GdkGravity rect_anchor,
                        GdkGravity menu_anchor,
                        const GdkEvent *trigger_event);

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_surface . rect_anchor and menu_anchor determine anchor points on rect and menu to pin together. menu can optionally be offset by “rect-anchor-dx” and “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 “anchor-hints” and “menu-type-hint”. Connect to the “popped-up” signal to find out how it was actually positioned.

Parameters

menu

the GtkMenu to pop up

 

rect_surface

the GdkSurface rect is relative to.

[not nullable]

rect

the GdkRectangle to align menu with.

[not nullable]

rect_anchor

the point on rect to align with menu 's anchor point

 

menu_anchor

the point on menu to align with rect 's anchor point

 

trigger_event

the GdkEvent that initiated this request or NULL if it's the current event.

[nullable]

gtk_menu_popup_at_widget ()

void
gtk_menu_popup_at_widget (GtkMenu *menu,
                          GtkWidget *widget,
                          GdkGravity widget_anchor,
                          GdkGravity menu_anchor,
                          const GdkEvent *trigger_event);

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.

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 “rect-anchor-dx” and “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 “anchor-hints” and “menu-type-hint”. Connect to the “popped-up” signal to find out how it was actually positioned.

Parameters

menu

the GtkMenu to pop up

 

widget

the GtkWidget to align menu with.

[not nullable]

widget_anchor

the point on widget to align with menu 's anchor point

 

menu_anchor

the point on menu to align with widget 's anchor point

 

trigger_event

the GdkEvent that initiated this request or NULL if it's the current event.

[nullable]

gtk_menu_popup_at_pointer ()

void
gtk_menu_popup_at_pointer (GtkMenu *menu,
                           const GdkEvent *trigger_event);

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 “anchor-hints”, “rect-anchor-dx”, “rect-anchor-dy”, and “menu-type-hint”. Connect to the “popped-up” signal to find out how it was actually positioned.

Parameters

menu

the GtkMenu to pop up

 

trigger_event

the GdkEvent that initiated this request or NULL if it's the current event.

[nullable]

gtk_menu_set_accel_group ()

void
gtk_menu_set_accel_group (GtkMenu *menu,
                          GtkAccelGroup *accel_group);

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.

Parameters

menu

a GtkMenu

 

accel_group

the GtkAccelGroup to be associated with the menu.

[allow-none]

gtk_menu_get_accel_group ()

GtkAccelGroup *
gtk_menu_get_accel_group (GtkMenu *menu);

Gets the GtkAccelGroup which holds global accelerators for the menu. See gtk_menu_set_accel_group().

Parameters

menu

a GtkMenu

 

Returns

the GtkAccelGroup associated with the menu.

[transfer none]

gtk_menu_set_accel_path ()

void
gtk_menu_set_accel_path (GtkMenu *menu,
                         const gchar *accel_path);

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, "&lt;Gnumeric-Sheet>/File"); has been called, assign its items the accel paths: "&lt;Gnumeric-Sheet>/File/New" and "&lt;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().

Parameters

menu

a valid GtkMenu

 

accel_path

a valid accelerator path, or NULL to unset the path.

[nullable]

gtk_menu_get_accel_path ()

const gchar *
gtk_menu_get_accel_path (GtkMenu *menu);

Retrieves the accelerator path set on the menu.

Parameters

menu

a valid GtkMenu

 

Returns

the accelerator path set on the menu.

gtk_menu_set_monitor ()

void
gtk_menu_set_monitor (GtkMenu *menu,
                      gint monitor_num);

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.

Parameters

menu

a GtkMenu

 

monitor_num

the number of the monitor on which the menu should be popped up

 

gtk_menu_get_monitor ()

gint
gtk_menu_get_monitor (GtkMenu *menu);

Retrieves the number of the monitor on which to show the menu.

Parameters

menu

a GtkMenu

 

Returns

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

gtk_menu_place_on_monitor ()

void
gtk_menu_place_on_monitor (GtkMenu *menu,
                           GdkMonitor *monitor);

Places menu on the given monitor.

Parameters

menu

a GtkMenu

 

monitor

the monitor to place the menu on

 

gtk_menu_set_reserve_toggle_size ()

void
gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
                                  gboolean reserve_toggle_size);

Sets whether the menu should reserve space for drawing toggles or icons, regardless of their actual presence.

Parameters

menu

a GtkMenu

 

reserve_toggle_size

whether to reserve size for toggles

 

gtk_menu_get_reserve_toggle_size ()

gboolean
gtk_menu_get_reserve_toggle_size (GtkMenu *menu);

Returns whether the menu reserves space for toggles and icons, regardless of their actual presence.

Parameters

menu

a GtkMenu

 

Returns

Whether the menu reserves toggle space

gtk_menu_popdown ()

void
gtk_menu_popdown (GtkMenu *menu);

Removes the menu from the screen.

Parameters

menu

a GtkMenu

 

gtk_menu_reposition ()

void
gtk_menu_reposition (GtkMenu *menu);

Repositions the menu according to its position function.

Parameters

menu

a GtkMenu

 

gtk_menu_get_active ()

GtkWidget *
gtk_menu_get_active (GtkMenu *menu);

Returns the selected menu item from the menu. This is used by the GtkComboBox.

Parameters

menu

a GtkMenu

 

Returns

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

[transfer none]

gtk_menu_set_active ()

void
gtk_menu_set_active (GtkMenu *menu,
                     guint index);

Selects the specified menu item within the menu. This is used by the GtkComboBox and should not be used by anyone else.

Parameters

menu

a GtkMenu

 

index

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

 

gtk_menu_attach_to_widget ()

void
gtk_menu_attach_to_widget (GtkMenu *menu,
                           GtkWidget *attach_widget,
                           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.

Parameters

menu

a GtkMenu

 

attach_widget

the GtkWidget that the menu will be attached to

 

detacher

the user supplied callback function that will be called when the menu calls gtk_menu_detach().

[scope async][allow-none]

gtk_menu_detach ()

void
gtk_menu_detach (GtkMenu *menu);

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.

Parameters

menu

a GtkMenu

 

gtk_menu_get_attach_widget ()

GtkWidget *
gtk_menu_get_attach_widget (GtkMenu *menu);

Returns the GtkWidget that the menu is attached to.

Parameters

menu

a GtkMenu

 

Returns

the GtkWidget that the menu is attached to.

[transfer none]

gtk_menu_get_for_attach_widget ()

GList *
gtk_menu_get_for_attach_widget (GtkWidget *widget);

Returns a list of the menus which are attached to this widget. This list is owned by GTK+ and must not be modified.

Parameters

widget

a GtkWidget

 

Returns

the list of menus attached to his widget.

[element-type GtkWidget][transfer none]

GtkMenuDetachFunc ()

void
(*GtkMenuDetachFunc) (GtkWidget *attach_widget,
                      GtkMenu *menu);

A user function supplied when calling gtk_menu_attach_to_widget() which will be called when the menu is later detached from the widget.

Parameters

attach_widget

the GtkWidget that the menu is being detached from.

 

menu

the GtkMenu being detached.

 

Types and Values

struct GtkMenu

struct GtkMenu;

enum GtkArrowPlacement

Used to specify the placement of scroll arrows in scrolling menus.

Members

GTK_ARROWS_BOTH

Place one arrow on each end of the menu.

 

GTK_ARROWS_START

Place both arrows at the top of the menu.

 

GTK_ARROWS_END

Place both arrows at the bottom of the menu.

 

Property Details

The “accel-group” property

  “accel-group”              GtkAccelGroup *

The accel group holding accelerators for the menu.

Owner: GtkMenu

Flags: Read / Write

The “accel-path” property

  “accel-path”               gchar *

An accel path used to conveniently construct accel paths of child items.

Owner: GtkMenu

Flags: Read / Write

Default value: NULL

The “active” property

  “active”                   gint

The index of the currently selected menu item, or -1 if no menu item is selected.

Owner: GtkMenu

Flags: Read / Write

Allowed values: >= -1

Default value: -1

The “anchor-hints” property

  “anchor-hints”             GdkAnchorHints

Positioning hints for aligning the menu relative to a rectangle.

These hints determine how the menu should be positioned in the case that the menu would fall off-screen if placed in its ideal position.

For example, GDK_ANCHOR_FLIP_Y will replace GDK_GRAVITY_NORTH_WEST with GDK_GRAVITY_SOUTH_WEST and vice versa if the menu extends beyond the bottom edge of the monitor.

See gtk_menu_popup_at_rect(), gtk_menu_popup_at_widget(), gtk_menu_popup_at_pointer(), “rect-anchor-dx”, “rect-anchor-dy”, “menu-type-hint”, and “popped-up”.

Owner: GtkMenu

Flags: Read / Write / Construct

Default value: GDK_ANCHOR_FLIP_X | GDK_ANCHOR_FLIP_Y | GDK_ANCHOR_SLIDE_X | GDK_ANCHOR_SLIDE_Y | GDK_ANCHOR_RESIZE_X | GDK_ANCHOR_RESIZE_Y

The “attach-widget” property

  “attach-widget”            GtkWidget *

The widget the menu is attached to. Setting this property attaches the menu without a GtkMenuDetachFunc. If you need to use a detacher, use gtk_menu_attach_to_widget() directly.

Owner: GtkMenu

Flags: Read / Write

The “menu-type-hint” property

  “menu-type-hint”           GdkSurfaceTypeHint

The GdkSurfaceTypeHint to use for the menu's GdkSurface.

See gtk_menu_popup_at_rect(), gtk_menu_popup_at_widget(), gtk_menu_popup_at_pointer(), “anchor-hints”, “rect-anchor-dx”, “rect-anchor-dy”, and “popped-up”.

Owner: GtkMenu

Flags: Read / Write / Construct

Default value: GDK_SURFACE_TYPE_HINT_POPUP_MENU

The “monitor” property

  “monitor”                  gint

The monitor the menu will be popped up on.

Owner: GtkMenu

Flags: Read / Write

Allowed values: >= -1

Default value: -1

The “rect-anchor-dx” property

  “rect-anchor-dx”           gint

Horizontal offset to apply to the menu, i.e. the rectangle or widget anchor.

See gtk_menu_popup_at_rect(), gtk_menu_popup_at_widget(), gtk_menu_popup_at_pointer(), “anchor-hints”, “rect-anchor-dy”, “menu-type-hint”, and “popped-up”.

Owner: GtkMenu

Flags: Read / Write / Construct

Default value: 0

The “rect-anchor-dy” property

  “rect-anchor-dy”           gint

Vertical offset to apply to the menu, i.e. the rectangle or widget anchor.

See gtk_menu_popup_at_rect(), gtk_menu_popup_at_widget(), gtk_menu_popup_at_pointer(), “anchor-hints”, “rect-anchor-dx”, “menu-type-hint”, and “popped-up”.

Owner: GtkMenu

Flags: Read / Write / Construct

Default value: 0

The “reserve-toggle-size” property

  “reserve-toggle-size”      gboolean

A boolean that indicates whether the menu reserves space for toggles and icons, regardless of their actual presence.

This property should only be changed from its default value for special-purposes such as tabular menus. Regular menus that are connected to a menu bar or context menus should reserve toggle space for consistency.

Owner: GtkMenu

Flags: Read / Write

Default value: TRUE

Signal Details

The “move-scroll” signal

void
user_function (GtkMenu      *menu,
               GtkScrollType scroll_type,
               gpointer      user_data)

Parameters

menu

a GtkMenu

 

scroll_type

a GtkScrollType

 

user_data

user data set when the signal handler was connected.

 

Flags: Action

The “popped-up” signal

void
user_function (GtkMenu *menu,
               gpointer flipped_rect,
               gpointer final_rect,
               gboolean flipped_x,
               gboolean flipped_y,
               gpointer user_data)

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.

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(), “anchor-hints”, “rect-anchor-dx”, “rect-anchor-dy”, and “menu-type-hint”.

Parameters

menu

the GtkMenu that popped up

 

flipped_rect

the position of menu after any possible flipping or NULL if the backend can't obtain it.

[nullable]

final_rect

the final position of menu or NULL if the backend can't obtain it.

[nullable]

flipped_x

TRUE if the anchors were flipped horizontally

 

flipped_y

TRUE if the anchors were flipped vertically

 

user_data

user data set when the signal handler was connected.

 

Flags: Run First