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 '#' |
| 11 | The following 'createMenu' definition shows how to construct and add the menubar to a window class. (A complete example is given at the bottom of this page.) |
| 12 | |
| 13 | {{{ |
| 14 | #!cobra |
| 15 | def createMenu |
| 16 | # create the main menubar |
| 17 | mainMenu = MenuBar () |
| 18 | |
| 19 | # -- the menu construction code goes here -- |
| 20 | |
| 21 | # Add the menubar to the top of the window |
| 22 | box = VBox(false, 0) # make this 'false' so menubar stays compact |
| 23 | box.packStart(mainMenu, false, false, 0) |
| 24 | .add(box) |
| 25 | }}} |
| 26 | |
| 27 | == Adding Menus to the Menu Bar == |
| 28 | |
| 29 | The menubar displays instances of '!MenuItems'. To make the headings for menus such as 'File', 'Edit', 'Help' we would write: |
| 30 | {{{ |
| 31 | #!cobra |
| 32 | menuItem = MenuItem("_File") |
| 33 | mainMenu.append(menuItem) |
| 34 | |
| 35 | menuItem = MenuItem("_Edit") # (it is convenient to reuse the 'menuItem' variable) |
| 36 | mainMenu.append(menuItem) |
| 37 | |
| 38 | menuItem = MenuItem("_Help") |
| 39 | mainMenu.append(menuItem) |
| 40 | }}} |
| 41 | |
| 42 | 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. |
| 43 | |
| 44 | To get an actual ''menu'' under each heading, we first create an instance of 'Menu' and attach it as the submenu for the respective menuitem: |
| 45 | {{{ |
| 46 | #!cobra |
| 47 | menuItem = MenuItem("_File") |
| 48 | mainMenu.append(menuItem) |
| 49 | fileMenu = Menu() |
| 50 | menuItem.submenu = fileMenu |
| 51 | |
| 52 | menuItem = MenuItem("_Edit") |
| 53 | mainMenu.append(menuItem) |
| 54 | editMenu = Menu() |
| 55 | menuItem.submenu = editMenu |
| 56 | |
| 57 | menuItem = MenuItem("_Help") |
| 58 | mainMenu.append(menuItem) |
| 59 | helpMenu = Menu() |
| 60 | menuItem.submenu = helpMenu |
| 61 | }}} |
| 62 | |
| 63 | 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. |
| 64 | |
| 65 | == Adding Items to Menus == |
| 66 | |
| 67 | 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''. |
| 68 | |
| 69 | === Simple Items === |
| 70 | |
| 71 | 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: |
| 72 | |
| 73 | {{{ |
| 74 | #!cobra |
| 75 | # create a menu item for 'show values' |
| 76 | menuItem = MenuItem("Show values") |
| 77 | # attach method to call when menu item is selected |
| 78 | listen menuItem.activated, ref .showValues |
| 79 | # add the menu item to the editMenu |
| 80 | editMenu.append(menuItem) |
| 81 | }}} |
| 82 | |
| 83 | === Check Boxes === |
| 84 | |
| 85 | Check boxes can be placed onto a menu, using the '!CheckMenuItem' class. We need to store a reference to the checkbox to later retrieve its state, by accessing its 'active' attribute (see the method 'showValues' at the bottom of this page). |
| 86 | |
| 87 | {{{ |
| 88 | #!cobra |
| 89 | # create a check box item and add to the edit menu |
| 90 | _checkbox = CheckMenuItem("Select me") |
| 91 | editMenu.append(_checkbox) |
| 92 | }}} |
| 93 | |
| 94 | === Radio Buttons === |
| 95 | |
| 96 | Radio buttons on menus are similar to regular radio buttons: the user is allowed to pick one option out of a pre-defined set. |
| 97 | |
| 98 | To store the state of radio buttons we can use a variable to hold the currently selected option, and provide some methods to change that variable as different options are selected. For example, to select a language from three options: |
| 99 | |
| 100 | {{{ |
| 101 | #!cobra |
| 102 | var _language as String |
| 103 | |
| 104 | def selectCobra(obj, e) |
| 105 | _language = "cobra" |
| 106 | |
| 107 | def selectJava(obj, e) |
| 108 | _language = "java" |
| 109 | |
| 110 | def selectPython(obj, e) |
| 111 | _language = "python" |
| 112 | }}} |
| 113 | |
| 114 | The radio buttons can then be set up to call the respective 'selectN' method when toggled: |
| 115 | {{{ |
| 116 | #!cobra |
| 117 | # create some radio buttons |
| 118 | _language = "cobra" # set initial option |
| 119 | radioItem1 = RadioMenuItem("Cobra") # this one selected by default |
| 120 | editMenu.append(radioItem1) |
| 121 | listen radioItem1.toggled, ref .selectCobra |
| 122 | radioItem2 = RadioMenuItem(radioItem1, "Java") |
| 123 | editMenu.append(radioItem2) |
| 124 | listen radioItem2.toggled, ref .selectJava |
| 125 | radioItem3 = RadioMenuItem(radioItem1, "Python") |
| 126 | editMenu.append(radioItem3) |
| 127 | listen radioItem3.toggled, ref .selectPython |
| 128 | }}} |
| 129 | |
| 130 | As with standard radio buttons, radio buttons are placed into groups by including the first member of the group in the constructor of subsequent members. |
| 131 | |
| 132 | === Stock Items and Accelerator Groups === |
| 133 | |
| 134 | There are several standard menu items that you find in many applications: these are listed in Gtk as a set of [http://developer.gnome.org/gtk/2.24/gtk-Stock-Items.html Stock] items. Stock items can be used on menus by creating an instance of an '!ImageMenuItem'. Because these standard items are also usually linked with keyboard shortcuts (such as CTRL-Q to quit the application), the menu items must also be linked to an ''accelerator group''. The following code creates an accelerator group, and then adds 'new', 'open', 'save' and 'quit' items to the file menu: |
| 135 | {{{ |
| 136 | #!cobra |
| 137 | # create an accelerator group |
| 138 | acceleratorGroup = AccelGroup() |
| 139 | .addAccelGroup(acceleratorGroup) |
| 140 | # -- new item |
| 141 | menuItem = ImageMenuItem(Stock.new, acceleratorGroup) |
| 142 | listen menuItem.activated, ref .newAction |
| 143 | fileMenu.append(menuItem) |
| 144 | # -- open item |
| 145 | menuItem = ImageMenuItem(Stock.open, acceleratorGroup) |
| 146 | listen menuItem.activated, ref .openAction |
| 147 | fileMenu.append(menuItem) |
| 148 | # -- save item |
| 149 | menuItem = ImageMenuItem(Stock.save, acceleratorGroup) |
| 150 | listen menuItem.activated, ref .saveAction |
| 151 | fileMenu.append(menuItem) |
| 152 | # -- separator |
| 153 | fileMenu.append(SeparatorMenuItem()) |
| 154 | # -- quit item |
| 155 | menuItem = ImageMenuItem(Stock.quit, acceleratorGroup) |
| 156 | listen menuItem.activated, ref .quit |
| 157 | fileMenu.append(menuItem) |
| 158 | }}} |
| 159 | |
| 160 | |
| 161 | == Submenus == |
| 162 | |
| 163 | A submenu is a menu contained within another menu; a submenu is shown on the screenshot at the top of this page. Submenus are simply menus added to an existing menu instance rather than the menubar. We can create a submenu for the language selector as follows: |
| 164 | {{{ |
| 165 | #!cobra |
| 166 | languageMenu = Menu() # instance of menu on which to build submenu |
| 167 | radioItem1 = RadioMenuItem("Cobra") |
| 168 | languageMenu.append(radioItem1) # attach the radio items to the submenu |
| 169 | listen radioItem1.toggled, ref .selectCobra |
| 170 | radioItem2 = RadioMenuItem(radioItem1, "Java") |
| 171 | languageMenu.append(radioItem2) |
| 172 | listen radioItem2.toggled, ref .selectJava |
| 173 | radioItem3 = RadioMenuItem(radioItem1, "Python") |
| 174 | languageMenu.append(radioItem3) |
| 175 | listen radioItem3.toggled, ref .selectPython |
| 176 | languageMenuItem = MenuItem("Language") # create a menuitem labelled 'Language' |
| 177 | languageMenuItem.submenu = languageMenu # and attach the languageMenu to it as a submenu |
| 178 | editMenu.append(languageMenuItem) # before adding to the edit menu |
| 179 | }}} |
| 180 | |
| 181 | == Complete Example == |
| 182 | |
| 183 | {{{ |
| 184 | #!cobra |
| 185 | # @args -pkg:gtk-sharp-2.0 # remove initial '#' |
27 | | |
28 | | # -- the menu construction code goes here -- |
| 230 | # create an accelerator group |
| 231 | acceleratorGroup = AccelGroup() |
| 232 | .addAccelGroup(acceleratorGroup) |
| 233 | |
| 234 | # -- menu 1: file menu, with three stock buttons and a separator |
| 235 | menuItem = MenuItem("_File") |
| 236 | mainMenu.append(menuItem) |
| 237 | fileMenu = Menu() |
| 238 | menuItem.submenu = fileMenu |
| 239 | |
| 240 | # create an accelerator group |
| 241 | acceleratorGroup = AccelGroup() |
| 242 | .addAccelGroup(acceleratorGroup) |
| 243 | # -- new item |
| 244 | menuItem = ImageMenuItem(Stock.new, acceleratorGroup) |
| 245 | listen menuItem.activated, ref .newAction |
| 246 | fileMenu.append(menuItem) |
| 247 | # -- open item |
| 248 | menuItem = ImageMenuItem(Stock.open, acceleratorGroup) |
| 249 | listen menuItem.activated, ref .openAction |
| 250 | fileMenu.append(menuItem) |
| 251 | # -- save item |
| 252 | menuItem = ImageMenuItem(Stock.save, acceleratorGroup) |
| 253 | listen menuItem.activated, ref .saveAction |
| 254 | fileMenu.append(menuItem) |
| 255 | # -- separator |
| 256 | fileMenu.append(SeparatorMenuItem()) |
| 257 | # -- quit item |
| 258 | menuItem = ImageMenuItem(Stock.quit, acceleratorGroup) |
| 259 | listen menuItem.activated, ref .quit |
| 260 | fileMenu.append(menuItem) |
| 261 | |
| 262 | # -- menu 2: edit menu, with menu item, check item, and submenu |
| 263 | menuItem = MenuItem("_Edit") |
| 264 | mainMenu.append(menuItem) |
| 265 | editMenu = Menu() |
| 266 | menuItem.submenu = editMenu |
| 267 | |
| 268 | # create a menu item for 'show values' |
| 269 | menuItem = MenuItem("Show values") |
| 270 | # attach method to call when menu item is selected |
| 271 | listen menuItem.activated, ref .showValues |
| 272 | # add the menu item to the editMenu |
| 273 | editMenu.append(menuItem) |
| 274 | |
| 275 | # create a check box item |
| 276 | _checkbox = CheckMenuItem("Select me") |
| 277 | editMenu.append(_checkbox) |
| 278 | |
| 279 | # create some radio buttons on a submenu |
| 280 | _language = "cobra" # set initial item |
| 281 | languageMenu = Menu() |
| 282 | radioItem1 = RadioMenuItem("Cobra") |
| 283 | languageMenu.append(radioItem1) |
| 284 | listen radioItem1.toggled, ref .selectCobra |
| 285 | radioItem2 = RadioMenuItem(radioItem1, "Java") |
| 286 | languageMenu.append(radioItem2) |
| 287 | listen radioItem2.toggled, ref .selectJava |
| 288 | radioItem3 = RadioMenuItem(radioItem1, "Python") |
| 289 | languageMenu.append(radioItem3) |
| 290 | listen radioItem3.toggled, ref .selectPython |
| 291 | languageMenuItem = MenuItem("Language") |
| 292 | languageMenuItem.submenu = languageMenu |
| 293 | editMenu.append(languageMenuItem) |
| 294 | |
| 295 | # -- menu 3: help menu |
| 296 | menuItem = MenuItem("_Help") |
| 297 | mainMenu.append(menuItem) |
| 298 | helpMenu = Menu() |
| 299 | menuItem.submenu = helpMenu |
| 300 | |
| 301 | menuItem = ImageMenuItem(Stock.about, acceleratorGroup) |
| 302 | listen menuItem.activated, ref .aboutAction |
| 303 | helpMenu.append(menuItem) |
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 | | |