GtkMenuShell: GTK+ 3 Reference Manual

## GtkMenuShell GtkMenuShell — A base class for menu objects |

## Functions

void | gtk_menu_shell_append ()
void | gtk_menu_shell_prepend ()
void | gtk_menu_shell_insert ()
void | gtk_menu_shell_deactivate ()
void | gtk_menu_shell_select_item ()
void | gtk_menu_shell_select_first ()
void | gtk_menu_shell_deselect ()
void | gtk_menu_shell_activate_item ()
void | gtk_menu_shell_cancel ()
void | gtk_menu_shell_set_take_focus ()
gboolean | gtk_menu_shell_get_take_focus ()
GtkWidget * | gtk_menu_shell_get_selected_item ()
GtkWidget * | gtk_menu_shell_get_parent_shell ()
void | gtk_menu_shell_bind_model ()

## Properties

gboolean | take-focus | Read / Write

## Signals

void | activate-current | Action
void | cancel | Action
void | cycle-focus | Action
void | deactivate | Run First
void | insert | Run First
void | move-current | Action
gboolean | move-selected | Run Last
void | selection-done | Run First

## Types and Values

struct | GtkMenuShell
enum | GtkMenuDirectionType

## Object Hierarchy

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

## Implemented Interfaces

GtkMenuShell implements AtkImplementorIface and GtkBuildable.

## Includes

    #include <gtk/gtk.h>

## Description

A GtkMenuShell is the abstract base class used to derive the GtkMenu and GtkMenuBar subclasses.

A GtkMenuShell is a container of GtkMenuItem objects arranged in a list which can be navigated, selected, and activated by the user to perform application functions. A GtkMenuItem can have a submenu associated with it, allowing for nested hierarchical menus.

### Terminology

A menu item can be “selected”, this means that it is displayed in the prelight state, and if it has a submenu, that submenu will be popped up.

A menu is “active” when it is visible onscreen and the user is selecting from it. A menubar is not active until the user clicks on one of its menuitems. When a menu is active, passing the mouse over a submenu will pop it up.

There is also is a concept of the current menu and a current menu item. The current menu item is the selected menu item that is furthest down in the hierarchy. (Every active menu shell does not necessarily contain a selected menu item, but if it does, then the parent menu shell must also contain a selected menu item.) The current menu is the menu that contains the current menu item. It will always have a GTK grab and receive all key presses.

## Functions

### gtk_menu_shell_append ()

    void
    gtk_menu_shell_append (GtkMenuShell *menu_shell,
                           GtkWidget *child);

Adds a new GtkMenuItem to the end of the menu shell's item list.

#### Parameters

menu_shell | a GtkMenuShell |
child | The GtkMenuItem to add. | [type Gtk.MenuItem]

### gtk_menu_shell_prepend ()

    void
    gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
                            GtkWidget *child);

Adds a new GtkMenuItem to the beginning of the menu shell's item list.

#### Parameters

menu_shell | a GtkMenuShell |
child | The GtkMenuItem to add |

### gtk_menu_shell_insert ()

    void
    gtk_menu_shell_insert (GtkMenuShell *menu_shell,
                           GtkWidget *child,
                           gint position);

Adds a new GtkMenuItem to the menu shell’s item list at the position indicated by `position` .

#### Parameters

menu_shell | a GtkMenuShell |
child | The GtkMenuItem to add |
position | The position in the item list where `child` is added. Positions are numbered from 0 to n-1 |

### gtk_menu_shell_deactivate ()

    void
    gtk_menu_shell_deactivate (GtkMenuShell *menu_shell);

Deactivates the menu shell.

Typically this results in the menu shell being erased from the screen.

#### Parameters

menu_shell | a GtkMenuShell |

### gtk_menu_shell_select_item ()

    void
    gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
                                GtkWidget *menu_item);

Selects the menu item from the menu shell.

#### Parameters

menu_shell | a GtkMenuShell |
menu_item | The GtkMenuItem to select |

### gtk_menu_shell_select_first ()

    void
    gtk_menu_shell_select_first (GtkMenuShell *menu_shell,
                                 gboolean search_sensitive);

Select the first visible or selectable child of the menu shell; don’t select tearoff items unless the only item is a tearoff item.

#### Parameters

menu_shell | a GtkMenuShell |
search_sensitive | if `TRUE`, search for the first selectable menu item, otherwise select nothing if the first item isn’t sensitive. This should be `FALSE` if the menu is being popped up initially. |

Since: 2.2

### gtk_menu_shell_deselect ()

    void
    gtk_menu_shell_deselect (GtkMenuShell *menu_shell);

Deselects the currently selected item from the menu shell, if any.

#### Parameters

menu_shell | a GtkMenuShell |

### gtk_menu_shell_activate_item ()

    void
    gtk_menu_shell_activate_item (GtkMenuShell *menu_shell,
                                  GtkWidget *menu_item,
                                  gboolean force_deactivate);

Activates the menu item within the menu shell.

#### Parameters

menu_shell | a GtkMenuShell |
menu_item | the GtkMenuItem to activate |
force_deactivate | if `TRUE`, force the deactivation of the menu shell after the menu item is activated |

### gtk_menu_shell_cancel ()

    void
    gtk_menu_shell_cancel (GtkMenuShell *menu_shell);

Cancels the selection within the menu shell.

#### Parameters

menu_shell | a GtkMenuShell |

Since: 2.4

### gtk_menu_shell_set_take_focus ()

    void
    gtk_menu_shell_set_take_focus (GtkMenuShell *menu_shell,
                                   gboolean take_focus);

If `take_focus` is `TRUE` (the default) the menu shell will take the keyboard focus so that it will receive all keyboard events which is needed to enable keyboard navigation in menus.

Setting `take_focus` to `FALSE` is useful only for special applications like virtual keyboard implementations which should not take keyboard focus.

The `take_focus` state of a menu or menu bar is automatically propagated to submenus whenever a submenu is popped up, so you don’t have to worry about recursively setting it for your entire menu hierarchy. Only when programmatically picking a submenu and popping it up manually, the `take_focus` property of the submenu needs to be set explicitly.

Note that setting it to `FALSE` has side-effects:

If the focus is in some other app, it keeps the focus and keynav in the menu doesn’t work. Consequently, keynav on the menu will only work if the focus is on some toplevel owned by the onscreen keyboard.

To avoid confusing the user, menus with `take_focus` set to `FALSE` should not display mnemonics or accelerators, since it cannot be guaranteed that they will work.

See also `gdk_keyboard_grab()`

#### Parameters

menu_shell | a GtkMenuShell |
take_focus | `TRUE` if the menu shell should take the keyboard focus on popup |

Since: 2.8

### gtk_menu_shell_get_take_focus ()

    gboolean
    gtk_menu_shell_get_take_focus (GtkMenuShell *menu_shell);

Returns `TRUE` if the menu shell will take the keyboard focus on popup.

#### Parameters

menu_shell | a GtkMenuShell |

#### Returns

`TRUE` if the menu shell will take the keyboard focus on popup.

Since: 2.8

### gtk_menu_shell_get_selected_item ()

    GtkWidget *
    gtk_menu_shell_get_selected_item (GtkMenuShell *menu_shell);

Gets the currently selected item.

#### Parameters

menu_shell | a GtkMenuShell |

#### Returns

the currently selected item.

[transfer none]

Since: 3.0

### gtk_menu_shell_get_parent_shell ()

    GtkWidget *
    gtk_menu_shell_get_parent_shell (GtkMenuShell *menu_shell);

Gets the parent menu shell.

The parent menu shell of a submenu is the GtkMenu or GtkMenuBar from which it was opened up.

#### Parameters

menu_shell | a GtkMenuShell |

#### Returns

the parent GtkMenuShell.

[transfer none]

Since: 3.0

### gtk_menu_shell_bind_model ()

    void
    gtk_menu_shell_bind_model (GtkMenuShell *menu_shell,
                               GMenuModel *model,
                               const gchar *action_namespace,
                               gboolean with_separators);

Establishes a binding between a GtkMenuShell and a GMenuModel.

The contents of `shell` are removed and then refilled with menu items according to `model` . When `model` changes, `shell` is updated. Calling this function twice on `shell` with different `model` will cause the first binding to be replaced with a binding to the new model. If `model` is `NULL` then any previous binding is undone and all children are removed.

`with_separators` determines if toplevel items (eg: sections) have separators inserted between them. This is typically desired for menus but doesn’t make sense for menubars.

If `action_namespace` is non-`NULL` then the effect is as if all actions mentioned in the `model` have their names prefixed with the namespace, plus a dot. For example, if the action “quit” is mentioned and `action_namespace` is “app” then the effective action name is “app.quit”.

This function uses GtkActionable to define the action name and target values on the created menu items. If you want to use an action group other than “app” and “win”, or if you want to use a GtkMenuShell outside of a GtkApplicationWindow, then you will need to attach your own action group to the widget hierarchy using `gtk_widget_insert_action_group()`. As an example, if you created a group with a “quit” action and inserted it with the name “mygroup” then you would use the action name “mygroup.quit” in your GMenuModel.

For most cases you are probably better off using `gtk_menu_new_from_model()` or `gtk_menu_bar_new_from_model()` or just directly passing the GMenuModel to `gtk_application_set_app_menu()` or `gtk_application_set_menubar()`.

#### Parameters

menu_shell | a GtkMenuShell |
model | the GMenuModel to bind to or `NULL` to remove binding. | [allow-none]
action_namespace | the namespace for actions in `model` . | [allow-none]
with_separators | `TRUE` if toplevel items in `shell` should have separators between them |

Since: 3.6

## Types and Values

### struct GtkMenuShell

    struct GtkMenuShell;

### enum GtkMenuDirectionType

An enumeration representing directional movements within a menu.

#### Members

GTK_MENU_DIR_PARENT | To the parent menu shell |
GTK_MENU_DIR_CHILD | To the submenu, if any, associated with the item |
GTK_MENU_DIR_NEXT | To the next menu item |
GTK_MENU_DIR_PREV | To the previous menu item |

## Property Details

### The `“take-focus”` property

      “take-focus”               gboolean

A boolean that determines whether the menu and its submenus grab the keyboard focus. See `gtk_menu_shell_set_take_focus()` and `gtk_menu_shell_get_take_focus()`.

Owner: GtkMenuShell

Flags: Read / Write

Default value: TRUE

Since: 2.8

## Signal Details

### The `“activate-current”` signal

    void
    user_function (GtkMenuShell *menushell,
                   gboolean      force_hide,
                   gpointer      user_data)

An action signal that activates the current menu item within the menu shell.

#### Parameters

menushell | the object which received the signal |
force_hide | if `TRUE`, hide the menu after activating the menu item |
user_data | user data set when the signal handler was connected. |

Flags: Action

### The `“cancel”` signal

    void
    user_function (GtkMenuShell *menushell,
                   gpointer      user_data)

An action signal which cancels the selection within the menu shell. Causes the “selection-done” signal to be emitted.

#### Parameters

menushell | the object which received the signal |
user_data | user data set when the signal handler was connected. |

Flags: Action

### The `“cycle-focus”` signal

    void
    user_function (GtkMenuShell    *menushell,
                   GtkDirectionType direction,
                   gpointer         user_data)

A keybinding signal which moves the focus in the given `direction` .

#### Parameters

menushell | the object which received the signal |
direction | the direction to cycle in |
user_data | user data set when the signal handler was connected. |

Flags: Action

### The `“deactivate”` signal

    void
    user_function (GtkMenuShell *menushell,
                   gpointer      user_data)

This signal is emitted when a menu shell is deactivated.

#### Parameters

menushell | the object which received the signal |
user_data | user data set when the signal handler was connected. |

Flags: Run First

### The `“insert”` signal

    void
    user_function (GtkMenuShell *menu_shell,
                   GtkWidget    *child,
                   int           position,
                   gpointer      user_data)

The ::insert signal is emitted when a new GtkMenuItem is added to a GtkMenuShell. A separate signal is used instead of GtkContainer::add because of the need for an additional position parameter.

The inverse of this signal is the GtkContainer::removed signal.

#### Parameters

menu_shell | the object on which the signal is emitted |
child | the GtkMenuItem that is being inserted |
position | the position at which the insert occurs |
user_data | user data set when the signal handler was connected. |

Flags: Run First

Since: 3.2

### The `“move-current”` signal

    void
    user_function (GtkMenuShell        *menushell,
                   GtkMenuDirectionType direction,
                   gpointer             user_data)

An keybinding signal which moves the current menu item in the direction specified by `direction` .

#### Parameters

menushell | the object which received the signal |
direction | the direction to move |
user_data | user data set when the signal handler was connected. |

Flags: Action

### The `“move-selected”` signal

    gboolean
    user_function (GtkMenuShell *menu_shell,
                   int           distance,
                   gpointer      user_data)

The ::move-selected signal is emitted to move the selection to another item.

#### Parameters

menu_shell | the object on which the signal is emitted |
distance | +1 to move to the next item, -1 to move to the previous |
user_data | user data set when the signal handler was connected. |

#### Returns

`TRUE` to stop the signal emission, `FALSE` to continue

Flags: Run Last

Since: 2.12

### The `“selection-done”` signal

    void
    user_function (GtkMenuShell *menushell,
                   gpointer      user_data)

This signal is emitted when a selection has been completed within a menu shell.

#### Parameters

menushell | the object which received the signal |
user_data | user data set when the signal handler was connected. |

Flags: Run First