]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menuitem.h | |
ba1d7a6c | 3 | // Purpose: interface of wxMenu, wxMenuItem |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
526954c5 | 6 | // Licence: wxWindows licence |
23324ae1 FM |
7 | ///////////////////////////////////////////////////////////////////////////// |
8 | ||
9 | /** | |
10 | @class wxMenuItem | |
7c913512 | 11 | |
ba1d7a6c FM |
12 | A menu item represents an item in a menu. |
13 | ||
14 | Note that you usually don't have to deal with it directly as wxMenu methods | |
15 | usually construct an object of this class for you. | |
7c913512 | 16 | |
23324ae1 | 17 | Also please note that the methods related to fonts and bitmaps are currently |
30f23bcc | 18 | only implemented for Windows, Mac and GTK+. |
7c913512 | 19 | |
3051a44a | 20 | @beginEventEmissionTable{wxCommandEvent,wxMenuEvent} |
558e89e7 | 21 | @event{EVT_MENU(id, func)} |
ce7fe42e | 22 | Process a @c wxEVT_MENU command, which is generated by a menu item. |
558e89e7 FM |
23 | This type of event is sent as wxCommandEvent. |
24 | @event{EVT_MENU_RANGE(id1, id2, func)} | |
ce7fe42e | 25 | Process a @c wxEVT_MENU command, which is generated by a range of menu items. |
558e89e7 FM |
26 | This type of event is sent as wxCommandEvent. |
27 | @event{EVT_MENU_OPEN(func)} | |
28 | A menu is about to be opened. On Windows, this is only sent once for each | |
29 | navigation of the menubar (up until all menus have closed). | |
30 | This type of event is sent as wxMenuEvent. | |
31 | @event{EVT_MENU_CLOSE(func)} | |
32 | A menu has been just closed. | |
33 | This type of event is sent as wxMenuEvent. | |
34 | @event{EVT_MENU_HIGHLIGHT(id, func)} | |
35 | The menu item with the specified id has been highlighted: used to show | |
36 | help prompts in the status bar by wxFrame | |
37 | This type of event is sent as wxMenuEvent. | |
38 | @event{EVT_MENU_HIGHLIGHT_ALL(func)} | |
39 | A menu item has been highlighted, i.e. the currently selected menu item has changed. | |
40 | This type of event is sent as wxMenuEvent. | |
41 | @endEventTable | |
42 | ||
23324ae1 FM |
43 | @library{wxcore} |
44 | @category{menus} | |
7c913512 | 45 | |
e54c96f1 | 46 | @see wxMenuBar, wxMenu |
23324ae1 FM |
47 | */ |
48 | class wxMenuItem : public wxObject | |
49 | { | |
50 | public: | |
51 | /** | |
52 | Constructs a wxMenuItem object. | |
ba1d7a6c FM |
53 | |
54 | Menu items can be standard, or "stock menu items", or custom. | |
55 | For the standard menu items (such as commands to open a file, exit the | |
56 | program and so on, see @ref page_stockitems for the full list) it is enough | |
57 | to specify just the stock ID and leave @a text and @a helpString empty. | |
e91ce12d VZ |
58 | Some platforms (currently wxGTK only, and see the remark in SetBitmap() |
59 | documentation) will also show standard bitmaps for stock menu items. | |
ba1d7a6c | 60 | |
e91ce12d | 61 | Leaving at least @a text empty for the stock menu items is actually strongly |
23324ae1 FM |
62 | recommended as they will have appearance and keyboard interface (including |
63 | standard accelerators) familiar to the user. | |
ba1d7a6c | 64 | |
4cc4bfaf FM |
65 | For the custom (non-stock) menu items, @a text must be specified and while |
66 | @a helpString may be left empty, it's recommended to pass the item | |
ba1d7a6c FM |
67 | description (which is automatically shown by the library in the status bar |
68 | when the menu item is selected) in this parameter. | |
69 | ||
23324ae1 | 70 | Finally note that you can e.g. use a stock menu label without using its stock |
ba1d7a6c FM |
71 | help string: |
72 | ||
73 | @code | |
74 | // use all stock properties: | |
75 | helpMenu->Append(wxID_ABOUT); | |
76 | ||
77 | // use the stock label and the stock accelerator but not the stock help string: | |
e91ce12d | 78 | helpMenu->Append(wxID_ABOUT, "", "My custom help string"); |
ba1d7a6c FM |
79 | |
80 | // use all stock properties except for the bitmap: | |
81 | wxMenuItem *mymenu = new wxMenuItem(helpMenu, wxID_ABOUT); | |
82 | mymenu->SetBitmap(wxArtProvider::GetBitmap(wxART_WARNING)); | |
83 | helpMenu->Append(mymenu); | |
84 | @endcode | |
85 | ||
86 | that is, stock properties are set independently one from the other. | |
3c4f71cc | 87 | |
7c913512 | 88 | @param parentMenu |
9cd28f48 VZ |
89 | Menu that the menu item belongs to. Can be @NULL if the item is |
90 | going to be added to the menu later. | |
7c913512 | 91 | @param id |
9cd28f48 VZ |
92 | Identifier for this menu item. May be @c wxID_SEPARATOR, in which |
93 | case the given kind is ignored and taken to be @c wxITEM_SEPARATOR | |
94 | instead. | |
7c913512 | 95 | @param text |
7145bcfc FM |
96 | Text for the menu item, as shown on the menu. |
97 | See SetItemLabel() for more info. | |
7c913512 | 98 | @param helpString |
4cc4bfaf | 99 | Optional help string that will be shown on the status bar. |
7c913512 | 100 | @param kind |
ba1d7a6c | 101 | May be @c wxITEM_SEPARATOR, @c wxITEM_NORMAL, @c wxITEM_CHECK or |
30f23bcc | 102 | @c wxITEM_RADIO. |
7c913512 | 103 | @param subMenu |
4cc4bfaf | 104 | If non-@NULL, indicates that the menu item is a submenu. |
23324ae1 | 105 | */ |
4cc4bfaf | 106 | wxMenuItem(wxMenu* parentMenu = NULL, int id = wxID_SEPARATOR, |
0a98423e FM |
107 | const wxString& text = wxEmptyString, |
108 | const wxString& helpString = wxEmptyString, | |
23324ae1 | 109 | wxItemKind kind = wxITEM_NORMAL, |
4cc4bfaf | 110 | wxMenu* subMenu = NULL); |
23324ae1 FM |
111 | |
112 | /** | |
113 | Destructor. | |
114 | */ | |
adaaa686 | 115 | virtual ~wxMenuItem(); |
23324ae1 FM |
116 | |
117 | /** | |
118 | Checks or unchecks the menu item. | |
23324ae1 FM |
119 | Note that this only works when the item is already appended to a menu. |
120 | */ | |
adaaa686 | 121 | virtual void Check(bool check = true); |
23324ae1 FM |
122 | |
123 | /** | |
124 | Enables or disables the menu item. | |
125 | */ | |
adaaa686 | 126 | virtual void Enable(bool enable = true); |
23324ae1 FM |
127 | |
128 | /** | |
7145bcfc FM |
129 | @deprecated This function is deprecated; please use GetLabelText() instead. |
130 | ||
131 | @see GetLabelText() | |
132 | */ | |
133 | static wxString GetLabelFromText(const wxString& text); | |
134 | ||
135 | /** | |
136 | Strips all accelerator characters and mnemonics from the given @a text. | |
137 | For example: | |
138 | ||
139 | @code | |
140 | wxMenuItem::GetLabelfromText("&Hello\tCtrl-h"); | |
141 | @endcode | |
142 | ||
143 | will return just @c "Hello". | |
144 | ||
145 | @see GetItemLabelText(), GetItemLabel() | |
146 | */ | |
147 | static wxString GetLabelText(const wxString& text); | |
148 | ||
149 | ||
150 | /** | |
151 | @name Getters | |
152 | */ | |
153 | //@{ | |
154 | ||
155 | /** | |
156 | Returns the background colour associated with the menu item. | |
157 | ||
158 | @onlyfor{wxmsw} | |
23324ae1 | 159 | */ |
dbac5ccc | 160 | wxColour& GetBackgroundColour() const; |
23324ae1 FM |
161 | |
162 | /** | |
7145bcfc FM |
163 | Returns the checked or unchecked bitmap. |
164 | ||
165 | @onlyfor{wxmsw} | |
23324ae1 | 166 | */ |
43c48e1e | 167 | virtual const wxBitmap& GetBitmap() const; |
23324ae1 FM |
168 | |
169 | /** | |
7145bcfc FM |
170 | Returns the font associated with the menu item. |
171 | ||
172 | @onlyfor{wxmsw} | |
23324ae1 | 173 | */ |
dbac5ccc | 174 | wxFont& GetFont() const; |
23324ae1 FM |
175 | |
176 | /** | |
177 | Returns the help string associated with the menu item. | |
178 | */ | |
43c48e1e | 179 | const wxString& GetHelp() const; |
23324ae1 FM |
180 | |
181 | /** | |
182 | Returns the menu item identifier. | |
183 | */ | |
328f5751 | 184 | int GetId() const; |
23324ae1 FM |
185 | |
186 | /** | |
187 | Returns the text associated with the menu item including any accelerator | |
ba1d7a6c | 188 | characters that were passed to the constructor or SetItemLabel(). |
3c4f71cc | 189 | |
4cc4bfaf | 190 | @see GetItemLabelText(), GetLabelText() |
23324ae1 | 191 | */ |
adaaa686 | 192 | virtual wxString GetItemLabel() const; |
23324ae1 FM |
193 | |
194 | /** | |
195 | Returns the text associated with the menu item, without any accelerator | |
196 | characters. | |
3c4f71cc | 197 | |
4cc4bfaf | 198 | @see GetItemLabel(), GetLabelText() |
23324ae1 | 199 | */ |
adaaa686 | 200 | virtual wxString GetItemLabelText() const; |
23324ae1 FM |
201 | |
202 | /** | |
7c913512 | 203 | Returns the item kind, one of @c wxITEM_SEPARATOR, @c wxITEM_NORMAL, |
23324ae1 FM |
204 | @c wxITEM_CHECK or @c wxITEM_RADIO. |
205 | */ | |
328f5751 | 206 | wxItemKind GetKind() const; |
23324ae1 FM |
207 | |
208 | /** | |
209 | Returns the text associated with the menu item without any accelerator | |
210 | characters it might contain. | |
ba1d7a6c | 211 | |
5dfae0ad | 212 | @deprecated This function is deprecated in favour of GetItemLabelText(). |
ba1d7a6c | 213 | |
7145bcfc | 214 | @see GetItemLabelText() |
23324ae1 | 215 | */ |
328f5751 | 216 | wxString GetLabel() const; |
23324ae1 FM |
217 | |
218 | /** | |
7145bcfc FM |
219 | Gets the width of the menu item checkmark bitmap. |
220 | ||
221 | @onlyfor{wxmsw} | |
23324ae1 | 222 | */ |
328f5751 | 223 | int GetMarginWidth() const; |
23324ae1 FM |
224 | |
225 | /** | |
226 | Returns the menu this menu item is in, or @NULL if this menu item is not | |
227 | attached. | |
228 | */ | |
328f5751 | 229 | wxMenu* GetMenu() const; |
23324ae1 FM |
230 | |
231 | /** | |
232 | Returns the text associated with the menu item. | |
ba1d7a6c | 233 | |
7145bcfc FM |
234 | @deprecated This function is deprecated. Please use GetItemLabel() or GetItemLabelText() instead. |
235 | ||
236 | @see GetItemLabel(), GetItemLabelText() | |
23324ae1 | 237 | */ |
328f5751 | 238 | wxString GetName() const; |
23324ae1 FM |
239 | |
240 | /** | |
241 | Returns the submenu associated with the menu item, or @NULL if there isn't one. | |
242 | */ | |
328f5751 | 243 | wxMenu* GetSubMenu() const; |
23324ae1 FM |
244 | |
245 | /** | |
246 | Returns the text associated with the menu item, such as it was passed to the | |
247 | wxMenuItem constructor, i.e. with any accelerator characters it may contain. | |
ba1d7a6c | 248 | |
5dfae0ad | 249 | @deprecated This function is deprecated in favour of GetItemLabel(). |
ba1d7a6c | 250 | |
7145bcfc | 251 | @see GetLabelFromText() |
23324ae1 | 252 | */ |
43c48e1e | 253 | const wxString& GetText() const; |
23324ae1 FM |
254 | |
255 | /** | |
7145bcfc FM |
256 | Returns the text colour associated with the menu item. |
257 | ||
258 | @onlyfor{wxmsw} | |
23324ae1 | 259 | */ |
dbac5ccc | 260 | wxColour& GetTextColour() const; |
7145bcfc FM |
261 | |
262 | //@} | |
263 | ||
264 | ||
265 | ||
266 | /** | |
267 | @name Checkers | |
268 | */ | |
269 | //@{ | |
23324ae1 | 270 | |
e3f4ca2e VZ |
271 | /** |
272 | Returns @true if the item is a check item. | |
273 | ||
274 | Unlike IsCheckable() this doesn't return @true for the radio buttons. | |
275 | ||
276 | @since 2.9.5 | |
277 | */ | |
278 | bool IsCheck() const; | |
279 | ||
23324ae1 FM |
280 | /** |
281 | Returns @true if the item is checkable. | |
e3f4ca2e VZ |
282 | |
283 | Notice that the radio buttons are considered to be checkable as well, | |
284 | so this method returns @true for them too. Use IsCheck() if you want to | |
285 | test for the check items only. | |
23324ae1 | 286 | */ |
328f5751 | 287 | bool IsCheckable() const; |
23324ae1 FM |
288 | |
289 | /** | |
290 | Returns @true if the item is checked. | |
291 | */ | |
adaaa686 | 292 | virtual bool IsChecked() const; |
23324ae1 FM |
293 | |
294 | /** | |
295 | Returns @true if the item is enabled. | |
296 | */ | |
adaaa686 | 297 | virtual bool IsEnabled() const; |
23324ae1 | 298 | |
e3f4ca2e VZ |
299 | /** |
300 | Returns @true if the item is a radio button. | |
301 | ||
302 | @since 2.9.5 | |
303 | */ | |
304 | bool IsRadio() const; | |
305 | ||
23324ae1 FM |
306 | /** |
307 | Returns @true if the item is a separator. | |
308 | */ | |
328f5751 | 309 | bool IsSeparator() const; |
23324ae1 FM |
310 | |
311 | /** | |
312 | Returns @true if the item is a submenu. | |
313 | */ | |
328f5751 | 314 | bool IsSubMenu() const; |
7145bcfc FM |
315 | |
316 | //@} | |
317 | ||
318 | ||
319 | ||
320 | /** | |
321 | @name Setters | |
322 | */ | |
323 | //@{ | |
23324ae1 FM |
324 | |
325 | /** | |
7145bcfc FM |
326 | Sets the background colour associated with the menu item. |
327 | ||
328 | @onlyfor{wxmsw} | |
23324ae1 | 329 | */ |
04992872 | 330 | void SetBackgroundColour(const wxColour& colour); |
23324ae1 FM |
331 | |
332 | /** | |
30f23bcc | 333 | Sets the bitmap for the menu item. |
4d273001 | 334 | |
7145bcfc FM |
335 | It is equivalent to wxMenuItem::SetBitmaps(bmp, wxNullBitmap) if |
336 | @a checked is @true (default value) or SetBitmaps(wxNullBitmap, bmp) | |
4d273001 | 337 | otherwise. |
30f23bcc | 338 | |
3ebf38b3 VZ |
339 | SetBitmap() must be called before the item is appended to the menu, |
340 | i.e. appending the item without a bitmap and setting one later is not | |
341 | guaranteed to work. But the bitmap can be changed or reset later if it | |
342 | had been set up initially. | |
343 | ||
b3f5fbf6 VZ |
344 | Notice that GTK+ uses a global setting called @c gtk-menu-images to |
345 | determine if the images should be shown in the menus at all. If it is | |
346 | off (which is the case in e.g. Gnome 2.28 by default), no images will | |
347 | be shown, consistently with the native behaviour. | |
348 | ||
0f6c9085 | 349 | @onlyfor{wxmsw,wxosx,wxgtk} |
23324ae1 | 350 | */ |
4d273001 | 351 | virtual void SetBitmap(const wxBitmap& bmp, bool checked = true); |
23324ae1 FM |
352 | |
353 | /** | |
30f23bcc | 354 | Sets the checked/unchecked bitmaps for the menu item. |
ba1d7a6c | 355 | The first bitmap is also used as the single bitmap for uncheckable menu items. |
30f23bcc FM |
356 | |
357 | @onlyfor{wxmsw} | |
23324ae1 FM |
358 | */ |
359 | void SetBitmaps(const wxBitmap& checked, | |
360 | const wxBitmap& unchecked = wxNullBitmap); | |
361 | ||
362 | /** | |
7145bcfc FM |
363 | Sets the font associated with the menu item. |
364 | ||
365 | @onlyfor{wxmsw} | |
23324ae1 FM |
366 | */ |
367 | void SetFont(const wxFont& font); | |
368 | ||
369 | /** | |
370 | Sets the help string. | |
371 | */ | |
372 | void SetHelp(const wxString& helpString); | |
373 | ||
374 | /** | |
375 | Sets the label associated with the menu item. | |
7145bcfc | 376 | |
d13b34d3 | 377 | Note that if the ID of this menu item corresponds to a stock ID, then it is |
7145bcfc FM |
378 | not necessary to specify a label: wxWidgets will automatically use the stock |
379 | item label associated with that ID. See the @ref wxMenuItem::wxMenuItem "constructor" | |
380 | for more info. | |
381 | ||
382 | The label string for the normal menu items (not separators) may include the | |
383 | accelerator which can be used to activate the menu item from keyboard. | |
384 | An accelerator key can be specified using the ampersand <tt>&</tt> character. | |
385 | In order to embed an ampersand character in the menu item text, the ampersand | |
386 | must be doubled. | |
387 | ||
388 | Optionally you can specify also an accelerator string appending a tab character | |
389 | <tt>\\t</tt> followed by a valid key combination (e.g. <tt>CTRL+V</tt>). | |
5ec18516 VZ |
390 | Its general syntax is any combination of @c "CTRL", @c "RAWCTRL", @c |
391 | "ALT" and @c "SHIFT" strings (case doesn't matter) separated by either | |
392 | @c '-' or @c '+' characters and followed by the accelerator itself. | |
393 | Notice that @c CTRL corresponds to the "Ctrl" key on most platforms but | |
394 | not under Mac OS where it is mapped to "Cmd" key on Mac keyboard. | |
395 | Usually this is exactly what you want in portable code but if you | |
396 | really need to use the (rarely used for this purpose) "Ctrl" key even | |
397 | under Mac, you may use @c RAWCTRL to prevent this mapping. Under the | |
398 | other platforms @c RAWCTRL is the same as plain @c CTRL. | |
399 | ||
7145bcfc FM |
400 | The accelerator may be any alphanumeric character, any function key |
401 | (from F1 to F12) or one of the special characters listed in the table | |
402 | below (again, case doesn't matter): | |
403 | - @c DEL or @c DELETE: Delete key | |
1aa1d260 | 404 | - @c BACK : Backspace key |
7145bcfc FM |
405 | - @c INS or @c INSERT: Insert key |
406 | - @c ENTER or @c RETURN: Enter key | |
407 | - @c PGUP: PageUp key | |
408 | - @c PGDN: PageDown key | |
409 | - @c LEFT: Left cursor arrow key | |
410 | - @c RIGHT: Right cursor arrow key | |
411 | - @c UP: Up cursor arrow key | |
412 | - @c DOWN: Down cursor arrow key | |
413 | - @c HOME: Home key | |
414 | - @c END: End key | |
415 | - @c SPACE: Space | |
416 | - @c TAB: Tab key | |
417 | - @c ESC or @c ESCAPE: Escape key (Windows only) | |
418 | ||
419 | Examples: | |
420 | ||
421 | @code | |
422 | m_pMyMenuItem->SetItemLabel("My &item\tCTRL+I"); | |
423 | m_pMyMenuItem2->SetItemLabel("Clean && build\tF7"); | |
424 | m_pMyMenuItem3->SetItemLabel("Simple item"); | |
425 | m_pMyMenuItem4->SetItemLabel("Item with &accelerator"); | |
426 | @endcode | |
09fc8e24 VZ |
427 | |
428 | @note In wxGTK using @c "SHIFT" with non-alphabetic characters | |
429 | currently doesn't work, even in combination with other modifiers, due | |
430 | to GTK+ limitation. E.g. @c Shift+Ctrl+A works but @c Shift+Ctrl+1 or | |
431 | @c Shift+/ do not, so avoid using accelerators of this form in portable | |
432 | code. | |
433 | ||
7145bcfc | 434 | @see GetItemLabel(), GetItemLabelText() |
23324ae1 | 435 | */ |
adaaa686 | 436 | virtual void SetItemLabel(const wxString& label); |
23324ae1 FM |
437 | |
438 | /** | |
7145bcfc FM |
439 | Sets the width of the menu item checkmark bitmap. |
440 | ||
441 | @onlyfor{wxmsw} | |
23324ae1 | 442 | */ |
04992872 | 443 | void SetMarginWidth(int width); |
23324ae1 FM |
444 | |
445 | /** | |
446 | Sets the parent menu which will contain this menu item. | |
447 | */ | |
43c48e1e | 448 | void SetMenu(wxMenu* menu); |
23324ae1 FM |
449 | |
450 | /** | |
451 | Sets the submenu of this menu item. | |
452 | */ | |
43c48e1e | 453 | void SetSubMenu(wxMenu* menu); |
23324ae1 FM |
454 | |
455 | /** | |
456 | Sets the text associated with the menu item. | |
ba1d7a6c | 457 | |
5dfae0ad | 458 | @deprecated This function is deprecated in favour of SetItemLabel(). |
7145bcfc FM |
459 | |
460 | @see SetItemLabel(). | |
23324ae1 | 461 | */ |
adaaa686 | 462 | virtual void SetText(const wxString& text); |
23324ae1 FM |
463 | |
464 | /** | |
7145bcfc FM |
465 | Sets the text colour associated with the menu item. |
466 | ||
467 | @onlyfor{wxmsw} | |
23324ae1 FM |
468 | */ |
469 | void SetTextColour(const wxColour& colour); | |
7145bcfc FM |
470 | |
471 | //@} | |
23324ae1 | 472 | }; |
e54c96f1 | 473 |