Wiki
Version 1 (modified by callisto, 13 years ago)

started GtkMenus page

Gtk Menus

Menus are lists of commands, accessed from a drop-down or pull-down list. Menus are typically found within a menubar, which displays the title of each menu.

Creating the Menu Bar

The menu bar itself is created as an instance of 'MenuBar'. The menubar is a regular Gtk widget and can be located anywhere you choose, but for the classic menu-at-the-top, place the menubar to the top of a non-expandable 'VBox' before adding it the window.

The following program shows how to construct and add the menubar to a window class.

# @args -pkg:gtk-sharp-2.0 # remove initial '#'

use Gtk

class ExampleWindow inherits Window
    cue init
        base.init("Menus Example")
        .setDefaultSize(300,200)
        listen .deleteEvent, ref .quit
        .createMenu

    def createMenu
        # create the main menubar
        mainMenu = MenuBar ()

        # -- the menu construction code goes here --

        # Add the menubar to the top of the window
        box = VBox(false, 0) # make this 'false' so menubar stays compact
        box.packStart(mainMenu, false, false, 0)
        .add(box)

    def quit(obj, e)
        Application.quit

class MainProgram
    def main
        Application.init
        window = ExampleWindow()
        window.showAll
        Application.run

Adding Menus to the Menu Bar

The menubar displays instances of 'MenuItems'. To make the headings for menus such as 'File', 'Edit', 'Help' we would write:

        menuItem = MenuItem("_File")
        mainMenu.append(menuItem)

        menuItem = MenuItem("_Edit") # (it is convenient to reuse the 'menuItem' variable)
        mainMenu.append(menuItem)

        menuItem = MenuItem("_Help")
        mainMenu.append(menuItem)

Notice the underscore in the title - this gets converted into a mnemonic for accessing the menu, and is shown as an underlined letter in the menu.

To get a menu attached to each heading, we must create an instance of 'Menu' and attach it as the submenu for the respective menuitem:

        menuItem = MenuItem("_File")
        mainMenu.append(menuItem)
        fileMenu = Menu()
        menuItem.submenu = fileMenu

        menuItem = MenuItem("_Edit")
        mainMenu.append(menuItem)
        editMenu = Menu()
        menuItem.submenu = editMenu

        menuItem = MenuItem("_Help")
        mainMenu.append(menuItem)
        helpMenu = Menu()
        menuItem.submenu = helpMenu

If you include the above code in the 'createWidgets' method of our example program, you will see a menubar on the window with nothing in the menus.

Adding Items to Menus

For our menus to be useful, we need to include items to launch commands or store information. There are several categories of items we can include, but here we look at: simple items (rather like buttons), check boxes, and radio buttons.

Simple Items

A simple menu item shows as a text label. The following code shows the three steps of creating a menu item, attaching a method to call when activated, and adding it to a menu:

        # create a menu item for 'show values'
        menuItem = MenuItem("Show values")
        # attach method to call when menu item is selected
        listen menuItem.activated, ref .showValues
        # add the menu item to the editMenu
        editMenu.append(menuItem)

Check Boxes

Check boxes can be placed onto a menu, using the 'CheckMenuItem?' class. To retrieve the state of the checkbox, store its instance in a field, and access its 'active' attribute.

        # create a check box item and add to the edit menu
        _checkbox = CheckMenuItem("Select me")
        editMenu.append(_checkbox)

Radio Buttons

Stock Items and Accelerator Groups

Complete Example

Attachments