]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/menuitem.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxMenu, wxMenuItem
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
12 A menu item represents an item in a menu.
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.
17 Also please note that the methods related to fonts and bitmaps are currently
18 only implemented for Windows, Mac and GTK+.
20 @beginEventEmissionTable{wxCommandEvent,wxMenuEvent}
21 @event{EVT_MENU(id, func)}
22 Process a @c wxEVT_COMMAND_MENU_SELECTED command, which is generated by a menu item.
23 This type of event is sent as wxCommandEvent.
24 @event{EVT_MENU_RANGE(id1, id2, func)}
25 Process a @c wxEVT_COMMAND_MENU_RANGE command, which is generated by a range of menu items.
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.
46 @see wxMenuBar, wxMenu
48 class wxMenuItem
: public wxObject
52 Constructs a wxMenuItem object.
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.
59 In fact, leaving at least @a text empty for the stock menu items is strongly
60 recommended as they will have appearance and keyboard interface (including
61 standard accelerators) familiar to the user.
63 For the custom (non-stock) menu items, @a text must be specified and while
64 @a helpString may be left empty, it's recommended to pass the item
65 description (which is automatically shown by the library in the status bar
66 when the menu item is selected) in this parameter.
68 Finally note that you can e.g. use a stock menu label without using its stock
72 // use all stock properties:
73 helpMenu->Append(wxID_ABOUT);
75 // use the stock label and the stock accelerator but not the stock help string:
76 helpMenu->Append(wxID_ABOUT, wxEmptyString, "My custom help string");
78 // use all stock properties except for the bitmap:
79 wxMenuItem *mymenu = new wxMenuItem(helpMenu, wxID_ABOUT);
80 mymenu->SetBitmap(wxArtProvider::GetBitmap(wxART_WARNING));
81 helpMenu->Append(mymenu);
84 that is, stock properties are set independently one from the other.
87 Menu that the menu item belongs to. Can be @NULL if the item is
88 going to be added to the menu later.
90 Identifier for this menu item. May be @c wxID_SEPARATOR, in which
91 case the given kind is ignored and taken to be @c wxITEM_SEPARATOR
94 Text for the menu item, as shown on the menu.
95 See SetItemLabel() for more info.
97 Optional help string that will be shown on the status bar.
99 May be @c wxITEM_SEPARATOR, @c wxITEM_NORMAL, @c wxITEM_CHECK or
102 If non-@NULL, indicates that the menu item is a submenu.
104 wxMenuItem(wxMenu
* parentMenu
= NULL
, int id
= wxID_SEPARATOR
,
105 const wxString
& text
= wxEmptyString
,
106 const wxString
& helpString
= wxEmptyString
,
107 wxItemKind kind
= wxITEM_NORMAL
,
108 wxMenu
* subMenu
= NULL
);
113 virtual ~wxMenuItem();
116 Checks or unchecks the menu item.
117 Note that this only works when the item is already appended to a menu.
119 virtual void Check(bool check
= true);
122 Enables or disables the menu item.
124 virtual void Enable(bool enable
= true);
127 @deprecated This function is deprecated; please use GetLabelText() instead.
131 static wxString
GetLabelFromText(const wxString
& text
);
134 Strips all accelerator characters and mnemonics from the given @a text.
138 wxMenuItem::GetLabelfromText("&Hello\tCtrl-h");
141 will return just @c "Hello".
143 @see GetItemLabelText(), GetItemLabel()
145 static wxString
GetLabelText(const wxString
& text
);
154 Returns the background colour associated with the menu item.
158 wxColour
GetBackgroundColour() const;
161 Returns the checked or unchecked bitmap.
165 virtual const wxBitmap
& GetBitmap() const;
168 Returns the font associated with the menu item.
172 wxFont
GetFont() const;
175 Returns the help string associated with the menu item.
177 const wxString
& GetHelp() const;
180 Returns the menu item identifier.
185 Returns the text associated with the menu item including any accelerator
186 characters that were passed to the constructor or SetItemLabel().
188 @see GetItemLabelText(), GetLabelText()
190 virtual wxString
GetItemLabel() const;
193 Returns the text associated with the menu item, without any accelerator
196 @see GetItemLabel(), GetLabelText()
198 virtual wxString
GetItemLabelText() const;
201 Returns the item kind, one of @c wxITEM_SEPARATOR, @c wxITEM_NORMAL,
202 @c wxITEM_CHECK or @c wxITEM_RADIO.
204 wxItemKind
GetKind() const;
207 Returns the text associated with the menu item without any accelerator
208 characters it might contain.
210 @deprecated This function is deprecated in favour of GetItemLabelText().
212 @see GetItemLabelText()
214 wxString
GetLabel() const;
217 Gets the width of the menu item checkmark bitmap.
221 int GetMarginWidth() const;
224 Returns the menu this menu item is in, or @NULL if this menu item is not
227 wxMenu
* GetMenu() const;
230 Returns the text associated with the menu item.
232 @deprecated This function is deprecated. Please use GetItemLabel() or GetItemLabelText() instead.
234 @see GetItemLabel(), GetItemLabelText()
236 wxString
GetName() const;
239 Returns the submenu associated with the menu item, or @NULL if there isn't one.
241 wxMenu
* GetSubMenu() const;
244 Returns the text associated with the menu item, such as it was passed to the
245 wxMenuItem constructor, i.e. with any accelerator characters it may contain.
247 @deprecated This function is deprecated in favour of GetItemLabel().
249 @see GetLabelFromText()
251 const wxString
& GetText() const;
254 Returns the text colour associated with the menu item.
258 wxColour
GetTextColour() const;
270 Returns @true if the item is checkable.
272 bool IsCheckable() const;
275 Returns @true if the item is checked.
277 virtual bool IsChecked() const;
280 Returns @true if the item is enabled.
282 virtual bool IsEnabled() const;
285 Returns @true if the item is a separator.
287 bool IsSeparator() const;
290 Returns @true if the item is a submenu.
292 bool IsSubMenu() const;
304 Sets the background colour associated with the menu item.
308 void SetBackgroundColour(const wxColour
& colour
) const;
311 Sets the bitmap for the menu item.
313 It is equivalent to wxMenuItem::SetBitmaps(bmp, wxNullBitmap) if
314 @a checked is @true (default value) or SetBitmaps(wxNullBitmap, bmp)
317 Notice that GTK+ uses a global setting called @c gtk-menu-images to
318 determine if the images should be shown in the menus at all. If it is
319 off (which is the case in e.g. Gnome 2.28 by default), no images will
320 be shown, consistently with the native behaviour.
322 @onlyfor{wxmsw,wxosx,wxgtk}
324 virtual void SetBitmap(const wxBitmap
& bmp
, bool checked
= true);
327 Sets the checked/unchecked bitmaps for the menu item.
328 The first bitmap is also used as the single bitmap for uncheckable menu items.
332 void SetBitmaps(const wxBitmap
& checked
,
333 const wxBitmap
& unchecked
= wxNullBitmap
);
336 Sets the font associated with the menu item.
340 void SetFont(const wxFont
& font
);
343 Sets the help string.
345 void SetHelp(const wxString
& helpString
);
348 Sets the label associated with the menu item.
350 Note that if the ID of this menu item corrisponds to a stock ID, then it is
351 not necessary to specify a label: wxWidgets will automatically use the stock
352 item label associated with that ID. See the @ref wxMenuItem::wxMenuItem "constructor"
355 The label string for the normal menu items (not separators) may include the
356 accelerator which can be used to activate the menu item from keyboard.
357 An accelerator key can be specified using the ampersand <tt>&</tt> character.
358 In order to embed an ampersand character in the menu item text, the ampersand
361 Optionally you can specify also an accelerator string appending a tab character
362 <tt>\\t</tt> followed by a valid key combination (e.g. <tt>CTRL+V</tt>).
363 Its general syntax is any combination of @c "CTRL", @c "ALT" and @c "SHIFT" strings
364 (case doesn't matter) separated by either @c '-' or @c '+' characters and followed
365 by the accelerator itself.
366 The accelerator may be any alphanumeric character, any function key
367 (from F1 to F12) or one of the special characters listed in the table
368 below (again, case doesn't matter):
369 - @c DEL or @c DELETE: Delete key
370 - @c INS or @c INSERT: Insert key
371 - @c ENTER or @c RETURN: Enter key
372 - @c PGUP: PageUp key
373 - @c PGDN: PageDown key
374 - @c LEFT: Left cursor arrow key
375 - @c RIGHT: Right cursor arrow key
376 - @c UP: Up cursor arrow key
377 - @c DOWN: Down cursor arrow key
382 - @c ESC or @c ESCAPE: Escape key (Windows only)
387 m_pMyMenuItem->SetItemLabel("My &item\tCTRL+I");
388 m_pMyMenuItem2->SetItemLabel("Clean && build\tF7");
389 m_pMyMenuItem3->SetItemLabel("Simple item");
390 m_pMyMenuItem4->SetItemLabel("Item with &accelerator");
393 @see GetItemLabel(), GetItemLabelText()
395 virtual void SetItemLabel(const wxString
& label
);
398 Sets the width of the menu item checkmark bitmap.
402 void SetMarginWidth(int width
) const;
405 Sets the parent menu which will contain this menu item.
407 void SetMenu(wxMenu
* menu
);
410 Sets the submenu of this menu item.
412 void SetSubMenu(wxMenu
* menu
);
415 Sets the text associated with the menu item.
417 @deprecated This function is deprecated in favour of SetItemLabel().
421 virtual void SetText(const wxString
& text
);
424 Sets the text colour associated with the menu item.
428 void SetTextColour(const wxColour
& colour
);