MenuItem


Object Hierarchy:

Object hierarchy for MenuItem

Description:

public class MenuItem : Bin, Implementor, Actionable, Activatable, Buildable

The MenuItem widget and the derived widgets are the only valid children for menus.

Their function is to correctly handle highlighting, alignment, events and submenus.

As a GtkMenuItem derives from Bin it can hold any valid child widget, although only a few are really useful.

By default, a GtkMenuItem sets a AccelLabel as its child. GtkMenuItem has direct functions to set the label and its mnemonic. For more advanced label settings, you can fetch the child widget from the GtkBin.

An example for setting markup and accelerator on a MenuItem:

GtkWidget *menu_item = gtk_menu_item_new_with_label ("Example Menu Item");

GtkWidget *child = gtk_bin_get_child (GTK_BIN (menu_item));
gtk_label_set_markup (GTK_LABEL (child), "<i>new label</i> with <b>markup</b>");
gtk_accel_label_set_accel (GTK_ACCEL_LABEL (child), GDK_KEY_1, 0);

GtkMenuItem as GtkBuildable

The GtkMenuItem implementation of the Buildable interface supports adding a submenu by specifying “submenu” as the “type” attribute of a <child> element.

An example of UI definition fragment with submenus:

<object class="GtkMenuItem">
<child type="submenu">
<object class="GtkMenu"/>
</child>
</object>

CSS nodes

menuitem
├── <child>
╰── [arrow.right]
m has a single CSS node with name menuitem. If the menuitem has a submenu, it gets another CSS node with name arrow, which has the .left or .right style class.

Example: MenuItem:

public class Application : Gtk.Window {
public Application () {
// Prepare Gtk.Window:
this.title = "My Gtk.Window";
this.window_position = Gtk.WindowPosition.CENTER;
this.destroy.connect (Gtk.main_quit);

// MenuBar:
Gtk.MenuBar bar = new Gtk.MenuBar ();
this.add (bar);

// File:
Gtk.MenuItem item_file = new Gtk.MenuItem.with_label ("File");
bar.add (item_file);

Gtk.Menu filemenu = new Gtk.Menu ();
item_file.set_submenu (filemenu);

Gtk.MenuItem item_open = new Gtk.MenuItem.with_label ("Open");
item_open.activate.connect (() => {
Gtk.FileChooserDialog chooser = new Gtk.FileChooserDialog (
"Select your favorite file", this, Gtk.FileChooserAction.OPEN,
"_Cancel",
Gtk.ResponseType.CANCEL,
"_Open",
Gtk.ResponseType.ACCEPT);
chooser.run ();
chooser.close ();
});
filemenu.add (item_open);

Gtk.MenuItem item_exit = new Gtk.MenuItem.with_label ("Exit");
item_exit.activate.connect (Gtk.main_quit);
filemenu.add (item_exit);
}

public static int main (string[] args) {
Gtk.init (ref args);

Application app = new Application ();
app.show_all ();
Gtk.main ();
return 0;
}
}

valac --pkg gtk+-3.0 Gtk.MenuItem.vala


Namespace: Gtk
Package: gtk+-3.0

Content:

Properties:

Creation methods:

Methods:

Signals:

Inherited Members:

All known members inherited from class Gtk.Bin
All known members inherited from class Gtk.Widget
All known members inherited from interface Atk.Implementor