| 1 | = Gtk Menus = |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | == Creating the Menu Bar == |
| 6 | |
| 7 | 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. |
| 8 | |
| 9 | The following program shows how to construct and add the menubar to a window class. |
| 10 | |
| 11 | {{{ |
| 12 | #!cobra |
| 13 | # @args -pkg:gtk-sharp-2.0 # remove initial '#' |
| 14 | |
| 15 | use Gtk |
| 16 | |
| 17 | class ExampleWindow inherits Window |
| 18 | cue init |
| 19 | base.init("Menus Example") |
| 20 | .setDefaultSize(300,200) |
| 21 | listen .deleteEvent, ref .quit |
| 22 | .createMenu |
| 23 | |
| 24 | def createMenu |
| 25 | # create the main menubar |
| 26 | mainMenu = MenuBar () |
| 27 | |
| 28 | # -- the menu construction code goes here -- |
| 29 | |
| 30 | # Add the menubar to the top of the window |
| 31 | box = VBox(false, 0) # make this 'false' so menubar stays compact |
| 32 | box.packStart(mainMenu, false, false, 0) |
| 33 | .add(box) |
| 34 | |
| 35 | def quit(obj, e) |
| 36 | Application.quit |
| 37 | |
| 38 | class MainProgram |
| 39 | def main |
| 40 | Application.init |
| 41 | window = ExampleWindow() |
| 42 | window.showAll |
| 43 | Application.run |
| 44 | }}} |
| 45 | |
| 46 | == Adding Menus to the Menu Bar == |
| 47 | |
| 48 | The menubar displays instances of '!MenuItems'. To make the headings for menus such as 'File', 'Edit', 'Help' we would write: |
| 49 | {{{ |
| 50 | #!cobra |
| 51 | menuItem = MenuItem("_File") |
| 52 | mainMenu.append(menuItem) |
| 53 | |
| 54 | menuItem = MenuItem("_Edit") # (it is convenient to reuse the 'menuItem' variable) |
| 55 | mainMenu.append(menuItem) |
| 56 | |
| 57 | menuItem = MenuItem("_Help") |
| 58 | mainMenu.append(menuItem) |
| 59 | }}} |
| 60 | |
| 61 | 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. |
| 62 | |
| 63 | 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: |
| 64 | {{{ |
| 65 | #!cobra |
| 66 | menuItem = MenuItem("_File") |
| 67 | mainMenu.append(menuItem) |
| 68 | fileMenu = Menu() |
| 69 | menuItem.submenu = fileMenu |
| 70 | |
| 71 | menuItem = MenuItem("_Edit") |
| 72 | mainMenu.append(menuItem) |
| 73 | editMenu = Menu() |
| 74 | menuItem.submenu = editMenu |
| 75 | |
| 76 | menuItem = MenuItem("_Help") |
| 77 | mainMenu.append(menuItem) |
| 78 | helpMenu = Menu() |
| 79 | menuItem.submenu = helpMenu |
| 80 | }}} |
| 81 | |
| 82 | 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. |
| 83 | |
| 84 | == Adding Items to Menus == |
| 85 | |
| 86 | 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''. |
| 87 | |
| 88 | === Simple Items === |
| 89 | |
| 90 | 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: |
| 91 | |
| 92 | {{{ |
| 93 | #!cobra |
| 94 | # create a menu item for 'show values' |
| 95 | menuItem = MenuItem("Show values") |
| 96 | # attach method to call when menu item is selected |
| 97 | listen menuItem.activated, ref .showValues |
| 98 | # add the menu item to the editMenu |
| 99 | editMenu.append(menuItem) |
| 100 | }}} |
| 101 | |
| 102 | === Check Boxes === |
| 103 | |
| 104 | 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. |
| 105 | |
| 106 | {{{ |
| 107 | #!cobra |
| 108 | # create a check box item and add to the edit menu |
| 109 | _checkbox = CheckMenuItem("Select me") |
| 110 | editMenu.append(_checkbox) |
| 111 | }}} |
| 112 | |
| 113 | === Radio Buttons === |
| 114 | |
| 115 | |
| 116 | |
| 117 | === Stock Items and Accelerator Groups === |
| 118 | |
| 119 | |
| 120 | |
| 121 | == Submenus == |
| 122 | |
| 123 | |
| 124 | == Complete Example == |
| 125 | |