]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/menu.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxMenuBar
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
12 A menu bar is a series of menus accessible from the top of a frame.
15 To respond to a menu selection, provide a handler for EVT_MENU, in the frame
16 that contains the menu bar.
18 If you have a toolbar which uses the same identifiers as your EVT_MENU entries,
19 events from the toolbar will also be processed by your EVT_MENU event handlers.
21 Tip: under Windows, if you discover that menu shortcuts (for example, Alt-F
22 to show the file menu) are not working, check any EVT_CHAR events you are
23 handling in child windows.
24 If you are not calling event.Skip() for events that you don't process in
25 these event handlers, menu shortcuts may cease to work.
30 @see wxMenu, @ref overview_eventhandling
32 class wxMenuBar
: public wxWindow
36 Construct an empty menu bar.
39 If wxMB_DOCKABLE the menu bar can be detached (wxGTK only).
41 wxMenuBar(long style
= 0);
44 Construct a menu bar from arrays of menus and titles.
49 An array of menus. Do not use this array again - it now belongs to
52 An array of title strings. Deallocate this array after creating
55 If wxMB_DOCKABLE the menu bar can be detached (wxGTK only).
57 wxMenuBar(size_t n
, wxMenu
* menus
[], const wxString titles
[],
61 Destructor, destroying the menu bar and removing it from the parent
67 Adds the item to the end of the menu bar.
70 The menu to add. Do not deallocate this menu after calling Append().
72 The title of the menu.
74 @return @true on success, @false if an error occurred.
78 virtual bool Append(wxMenu
* menu
, const wxString
& title
);
81 Checks or unchecks a menu item.
84 The menu item identifier.
86 If @true, checks the menu item, otherwise the item is unchecked.
88 @remarks Only use this when the menu bar has been associated with a
89 frame; otherwise, use the wxMenu equivalent call.
91 void Check(int id
, bool check
);
94 Enables or disables (greys out) a menu item.
97 The menu item identifier.
99 @true to enable the item, @false to disable it.
101 @remarks Only use this when the menu bar has been associated with a
102 frame; otherwise, use the wxMenu equivalent call.
104 void Enable(int id
, bool enable
);
107 Enables or disables a whole menu.
110 The position of the menu, starting from zero.
112 @true to enable the menu, @false to disable it.
114 @remarks Only use this when the menu bar has been associated with a frame.
116 virtual void EnableTop(size_t pos
, bool enable
);
119 Finds the menu item object associated with the given menu item identifier.
122 Menu item identifier.
124 If not @NULL, menu will get set to the associated menu.
126 @return The found menu item object, or @NULL if one was not found.
128 virtual wxMenuItem
* FindItem(int id
, wxMenu
* menu
= NULL
) const;
131 Returns the index of the menu with the given @a title or @c wxNOT_FOUND if no
132 such menu exists in this menubar.
134 The @a title parameter may specify either the menu title
135 (with accelerator characters, i.e. @c "&File") or just the
136 menu label (@c "File") indifferently.
138 int FindMenu(const wxString
& title
) const;
141 Finds the menu item id for a menu name/menu item string pair.
148 @return The menu item identifier, or wxNOT_FOUND if none was found.
150 @remarks Any special menu codes are stripped out of source and target
151 strings before matching.
153 virtual int FindMenuItem(const wxString
& menuString
,
154 const wxString
& itemString
) const;
157 Gets the help string associated with the menu item identifier.
160 The menu item identifier.
162 @return The help string, or the empty string if there was no help string
163 or the menu item was not found.
167 wxString
GetHelpString(int id
) const;
170 Gets the label associated with a menu item.
173 The menu item identifier.
175 @return The menu item label, or the empty string if the item was not
178 @remarks Use only after the menubar has been associated with a frame.
180 wxString
GetLabel(int id
) const;
183 Returns the label of a top-level menu. Note that the returned string does not
184 include the accelerator characters which could have been specified in the menu
185 title string during its construction.
188 Position of the menu on the menu bar, starting from zero.
190 @return The menu label, or the empty string if the menu was not found.
192 @remarks Use only after the menubar has been associated with a frame.
195 This function is deprecated in favour of GetMenuLabel() and GetMenuLabelText().
199 wxString
GetLabelTop(size_t pos
) const;
202 Returns the menu at @a menuIndex (zero-based).
204 wxMenu
* GetMenu(size_t menuIndex
) const;
207 Returns the number of menus in this menubar.
209 size_t GetMenuCount() const;
212 Returns the label of a top-level menu. Note that the returned string
213 includes the accelerator characters that have been specified in the menu
214 title string during its construction.
217 Position of the menu on the menu bar, starting from zero.
219 @return The menu label, or the empty string if the menu was not found.
221 @remarks Use only after the menubar has been associated with a frame.
223 @see GetMenuLabelText(), SetMenuLabel()
225 virtual wxString
GetMenuLabel(size_t pos
) const;
228 Returns the label of a top-level menu. Note that the returned string does not
229 include any accelerator characters that may have been specified in the menu
230 title string during its construction.
233 Position of the menu on the menu bar, starting from zero.
235 @return The menu label, or the empty string if the menu was not found.
237 @remarks Use only after the menubar has been associated with a frame.
239 @see GetMenuLabel(), SetMenuLabel()
241 virtual wxString
GetMenuLabelText(size_t pos
) const;
244 Inserts the menu at the given position into the menu bar. Inserting menu at
245 position 0 will insert it in the very beginning of it, inserting at position
246 GetMenuCount() is the same as calling Append().
249 The position of the new menu in the menu bar
251 The menu to add. wxMenuBar owns the menu and will free it.
253 The title of the menu.
255 @return @true on success, @false if an error occurred.
259 virtual bool Insert(size_t pos
, wxMenu
* menu
, const wxString
& title
);
262 Determines whether an item is checked.
265 The menu item identifier.
267 @return @true if the item was found and is checked, @false otherwise.
269 bool IsChecked(int id
) const;
272 Determines whether an item is enabled.
275 The menu item identifier.
277 @return @true if the item was found and is enabled, @false otherwise.
279 bool IsEnabled(int id
) const;
284 virtual void Refresh(bool eraseBackground
= true, const wxRect
* rect
= NULL
);
287 Removes the menu from the menu bar and returns the menu object - the caller
288 is responsible for deleting it. This function may be used together with
289 Insert() to change the menubar dynamically.
293 virtual wxMenu
* Remove(size_t pos
);
296 Replaces the menu at the given position with another one.
299 The position of the new menu in the menu bar
303 The title of the menu.
305 @return The menu which was previously at position pos.
306 The caller is responsible for deleting it.
308 @see Insert(), Remove()
310 virtual wxMenu
* Replace(size_t pos
, wxMenu
* menu
, const wxString
& title
);
313 Sets the help string associated with a menu item.
316 Menu item identifier.
318 Help string to associate with the menu item.
322 void SetHelpString(int id
, const wxString
& helpString
);
325 Sets the label of a menu item.
328 Menu item identifier.
332 @remarks Use only after the menubar has been associated with a frame.
336 void SetLabel(int id
, const wxString
& label
);
339 Sets the label of a top-level menu.
342 The position of a menu on the menu bar, starting from zero.
346 @remarks Use only after the menubar has been associated with a frame.
349 This function has been deprecated in favour of SetMenuLabel().
353 void SetLabelTop(size_t pos
, const wxString
& label
);
356 Sets the label of a top-level menu.
359 The position of a menu on the menu bar, starting from zero.
363 @remarks Use only after the menubar has been associated with a frame.
365 virtual void SetMenuLabel(size_t pos
, const wxString
& label
);
373 A menu is a popup (or pull down) list of items, one of which may be
374 selected before the menu goes away (clicking elsewhere dismisses the
375 menu). Menus may be used to construct either menu bars or popup menus.
377 A menu item has an integer ID associated with it which can be used to
378 identify the selection, or to change the menu item in some way. A menu item
379 with a special identifier -1 is a separator item and doesn't have an
380 associated command but just makes a separator line appear in the menu.
383 Please note that @e wxID_ABOUT and @e wxID_EXIT are predefined by wxWidgets
384 and have a special meaning since entries using these IDs will be taken out
385 of the normal menus under MacOS X and will be inserted into the system menu
386 (following the appropriate MacOS X interface guideline).
387 On PalmOS @e wxID_EXIT is disabled according to Palm OS Companion guidelines.
389 Menu items may be either @e normal items, @e check items or @e radio items.
390 Normal items don't have any special properties while the check items have a
391 boolean flag associated to them and they show a checkmark in the menu when
393 wxWidgets automatically toggles the flag value when the item is clicked and its
394 value may be retrieved using either wxMenu::IsChecked method of wxMenu or
395 wxMenuBar itself or by using wxEvent::IsChecked when you get the menu
396 notification for the item in question.
398 The radio items are similar to the check items except that all the other items
399 in the same radio group are unchecked when a radio item is checked. The radio
400 group is formed by a contiguous range of radio items, i.e. it starts at the
401 first item of this kind and ends with the first item of a different kind (or
402 the end of the menu). Notice that because the radio groups are defined in terms
403 of the item positions inserting or removing the items in the menu containing
404 the radio items risks to not work correctly.
407 @section menu_allocation Allocation strategy
409 All menus except the popup ones must be created on the @b heap.
410 All menus attached to a menubar or to another menu will be deleted by their
411 parent when it is deleted.
413 As the frame menubar is deleted by the frame itself, it means that normally
414 all menus used are deleted automatically.
417 @section menu_eventhandling Event handling
419 If the menu is part of a menubar, then wxMenuBar event processing is used.
421 With a popup menu (see wxWindow::PopupMenu), there is a variety of ways to
422 handle a menu selection event (@c wxEVT_COMMAND_MENU_SELECTED):
423 - Provide @c EVT_MENU handlers in the window which pops up the menu, or in an
424 ancestor of that window (the simplest method);
425 - Derive a new class from wxMenu and define event table entries using the @c EVT_MENU macro;
426 - Set a new event handler for wxMenu, through wxEvtHandler::SetNextHandler,
427 specifying an object whose class has @c EVT_MENU entries;
429 Note that instead of static @c EVT_MENU macros you can also use dynamic
430 connection; see @ref overview_eventhandling_connect.
435 @see wxMenuBar, wxWindow::PopupMenu, @ref overview_eventhandling,
436 @ref wxFileHistory "wxFileHistory (most recently used files menu)"
438 class wxMenu
: public wxEvtHandler
442 Constructs a wxMenu object.
445 If set to wxMENU_TEAROFF, the menu will be detachable (wxGTK only).
450 Constructs a wxMenu object with a title.
453 Title at the top of the menu (not always supported).
455 If set to wxMENU_TEAROFF, the menu will be detachable (wxGTK only).
457 wxMenu(const wxString
& title
, long style
= 0);
460 Destructor, destroying the menu.
463 Under Motif, a popup menu must have a valid parent (the window
464 it was last popped up on) when being destroyed. Therefore, make sure
465 you delete or re-use the popup menu @e before destroying the parent
466 window. Re-use in this context means popping up the menu on a different
467 window from last time, which causes an implicit destruction and
468 recreation of internal data structures.
476 The menu command identifier.
478 The string to appear on the menu item.
480 An optional help string associated with the item.
481 By default, the handler for the wxEVT_MENU_HIGHLIGHT event displays
482 this string in the status line.
484 May be wxITEM_SEPARATOR, wxITEM_NORMAL, wxITEM_CHECK or wxITEM_RADIO
487 This command can be used after the menu has been shown, as well as on
488 initial creation of a menu or menubar.
490 The item string for the normal menu items (not submenus or separators)
491 may include the accelerator which can be used to activate the menu item
493 The accelerator string follows the item label and is separated from it
494 by a TAB character ('\\t').
496 Its general syntax is any combination of "CTRL", "ALT" and "SHIFT" strings
497 (case doesn't matter) separated by either '-' or '+' characters and followed
498 by the accelerator itself.
499 The accelerator may be any alphanumeric character, any function key
500 (from F1 to F12) or one of the special characters listed in the table
501 below (again, case doesn't matter):
503 - DEL or DELETE: Delete key
504 - INS or INSERT: Insert key
505 - ENTER or RETURN: Enter key
508 - LEFT: Left cursor arrow key
509 - RIGHT: Right cursor arrow key
510 - UP: Up cursor arrow key
511 - DOWN: Down cursor arrow key
516 - ESC: or ESCAPE Escape key (Windows only)
518 @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
519 AppendSubMenu(), Insert(), SetLabel(), GetHelpString(),
520 SetHelpString(), wxMenuItem
522 wxMenuItem
* Append(int id
, const wxString
& item
= wxEmptyString
,
523 const wxString
& helpString
= wxEmptyString
,
524 wxItemKind kind
= wxITEM_NORMAL
);
529 @deprecated This function is deprecated, use AppendSubMenu() instead.
532 The menu command identifier.
534 The string to appear on the menu item.
538 An optional help string associated with the item.
539 By default, the handler for the wxEVT_MENU_HIGHLIGHT event displays
540 this string in the status line.
542 @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
543 AppendSubMenu(), Insert(), SetLabel(), GetHelpString(),
544 SetHelpString(), wxMenuItem
546 wxMenuItem
* Append(int id
, const wxString
& item
, wxMenu
* subMenu
,
547 const wxString
& helpString
= wxEmptyString
);
550 Adds a menu item object.
552 This is the most generic variant of Append() method because it may be
553 used for both items (including separators) and submenus and because
554 you can also specify various extra properties of a menu item this way,
555 such as bitmaps and fonts.
558 A menuitem object. It will be owned by the wxMenu object after this
559 function is called, so do not delete it yourself.
562 See the remarks for the other Append() overloads.
564 @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
565 AppendSubMenu(), Insert(), SetLabel(), GetHelpString(),
566 SetHelpString(), wxMenuItem
568 wxMenuItem
* Append(wxMenuItem
* menuItem
);
571 Adds a checkable item to the end of the menu.
573 @see Append(), InsertCheckItem()
575 wxMenuItem
* AppendCheckItem(int id
, const wxString
& item
,
576 const wxString
& help
= wxEmptyString
);
579 Adds a radio item to the end of the menu.
580 All consequent radio items form a group and when an item in the group is
581 checked, all the others are automatically unchecked.
583 @note Radio items are not supported under wxMotif.
585 @see Append(), InsertRadioItem()
587 wxMenuItem
* AppendRadioItem(int id
, const wxString
& item
,
588 const wxString
& help
= wxEmptyString
);
591 Adds a separator to the end of the menu.
593 @see Append(), InsertSeparator()
595 wxMenuItem
* AppendSeparator();
598 Adds the given @a submenu to this menu. @a text is the text shown in the
599 menu for it and @a help is the help string shown in the status bar when the
600 submenu item is selected.
602 wxMenuItem
* AppendSubMenu(wxMenu
* submenu
, const wxString
& text
,
603 const wxString
& help
= wxEmptyString
);
606 Inserts a break in a menu, causing the next appended item to appear in
609 virtual void Break();
612 Checks or unchecks the menu item.
615 The menu item identifier.
617 If @true, the item will be checked, otherwise it will be unchecked.
621 void Check(int id
, bool check
);
624 Deletes the menu item from the menu. If the item is a submenu, it will
625 @b not be deleted. Use Destroy() if you want to delete a submenu.
628 Id of the menu item to be deleted.
630 @see FindItem(), Destroy(), Remove()
635 Deletes the menu item from the menu. If the item is a submenu, it will
636 @b not be deleted. Use Destroy() if you want to delete a submenu.
639 Menu item to be deleted.
641 @see FindItem(), Destroy(), Remove()
643 bool Delete(wxMenuItem
* item
);
646 Deletes the menu item from the menu. If the item is a submenu, it will
647 be deleted. Use Remove() if you want to keep the submenu (for example,
651 Id of the menu item to be deleted.
653 @see FindItem(), Deletes(), Remove()
655 bool Destroy(int id
);
658 Deletes the menu item from the menu. If the item is a submenu, it will
659 be deleted. Use Remove() if you want to keep the submenu (for example,
663 Menu item to be deleted.
665 @see FindItem(), Deletes(), Remove()
667 bool Destroy(wxMenuItem
* item
);
670 Enables or disables (greys out) a menu item.
673 The menu item identifier.
675 @true to enable the menu item, @false to disable it.
679 void Enable(int id
, bool enable
);
682 Finds the menu item object associated with the given menu item identifier
683 and, optionally, the position of the item in the menu.
685 Unlike FindItem(), this function doesn't recurse but only looks at the
686 direct children of this menu.
689 The identifier of the menu item to find.
691 If the pointer is not @NULL, it is filled with the item's position if
692 it was found or @c (size_t)wxNOT_FOUND otherwise.
694 Menu item object or @NULL if not found.
696 wxMenuItem
*FindChildItem(int id
, size_t *pos
= NULL
) const;
699 Finds the menu id for a menu item string.
702 Menu item string to find.
704 @return Menu item identifier, or wxNOT_FOUND if none is found.
706 @remarks Any special menu codes are stripped out of source and target
707 strings before matching.
709 virtual int FindItem(const wxString
& itemString
) const;
712 Finds the menu item object associated with the given menu item identifier and,
713 optionally, the (sub)menu it belongs to.
716 Menu item identifier.
718 If the pointer is not @NULL, it will be filled with the item's
719 parent menu (if the item was found)
721 @return Menu item object or NULL if none is found.
723 wxMenuItem
* FindItem(int id
, wxMenu
** menu
= NULL
) const;
726 Returns the wxMenuItem given a position in the menu.
728 wxMenuItem
* FindItemByPosition(size_t position
) const;
731 Returns the help string associated with a menu item.
734 The menu item identifier.
736 @return The help string, or the empty string if there is no help string
737 or the item was not found.
739 @see SetHelpString(), Append()
741 virtual wxString
GetHelpString(int id
) const;
744 Returns a menu item label.
747 The menu item identifier.
749 @return The item label, or the empty string if the item was not found.
751 @see GetLabelText(), SetLabel()
753 wxString
GetLabel(int id
) const;
756 Returns a menu item label, without any of the original mnemonics and
760 The menu item identifier.
762 @return The item label, or the empty string if the item was not found.
764 @see GetLabel(), SetLabel()
766 wxString
GetLabelText(int id
) const;
769 Returns the number of items in the menu.
771 size_t GetMenuItemCount() const;
775 Returns the list of items in the menu.
777 wxMenuItemList is a pseudo-template list class containing wxMenuItem
778 pointers, see wxList.
780 wxMenuItemList
& GetMenuItems() const;
781 const wxMenuItemList
& GetMenuItems() const;
785 Returns the title of the menu.
787 @remarks This is relevant only to popup menus, use
788 wxMenuBar::GetMenuLabel for the menus in the menubar.
792 const wxString
& GetTitle() const;
795 Inserts the given @a item before the position @a pos.
797 Inserting the item at position GetMenuItemCount() is the same
800 @see Append(), Prepend()
802 wxMenuItem
* Insert(size_t pos
, wxMenuItem
* item
);
805 Inserts the given @a item before the position @a pos.
807 Inserting the item at position GetMenuItemCount() is the same
810 @see Append(), Prepend()
812 wxMenuItem
* Insert(size_t pos
, int id
,
813 const wxString
& item
= wxEmptyString
,
814 const wxString
& helpString
= wxEmptyString
,
815 wxItemKind kind
= wxITEM_NORMAL
);
818 Inserts a checkable item at the given position.
820 @see Insert(), AppendCheckItem()
822 wxMenuItem
* InsertCheckItem(size_t pos
, int id
, const wxString
& item
,
823 const wxString
& helpString
= wxEmptyString
);
826 Inserts a radio item at the given position.
828 @see Insert(), AppendRadioItem()
830 wxMenuItem
* InsertRadioItem(size_t pos
, int id
, const wxString
& item
,
831 const wxString
& helpString
= wxEmptyString
);
834 Inserts a separator at the given position.
836 @see Insert(), AppendSeparator()
838 wxMenuItem
* InsertSeparator(size_t pos
);
841 Determines whether a menu item is checked.
844 The menu item identifier.
846 @return @true if the menu item is checked, @false otherwise.
850 bool IsChecked(int id
) const;
853 Determines whether a menu item is enabled.
856 The menu item identifier.
858 @return @true if the menu item is enabled, @false otherwise.
862 bool IsEnabled(int id
) const;
865 Inserts the given @a item at position 0, i.e. before all the other
868 @see Append(), Insert()
870 wxMenuItem
* Prepend(wxMenuItem
* item
);
873 Inserts the given @a item at position 0, i.e. before all the other
876 @see Append(), Insert()
878 wxMenuItem
* Prepend(int id
, const wxString
& item
= wxEmptyString
,
879 const wxString
& helpString
= wxEmptyString
,
880 wxItemKind kind
= wxITEM_NORMAL
);
883 Inserts a checkable item at position 0.
885 @see Prepend(), AppendCheckItem()
887 wxMenuItem
* PrependCheckItem(int id
, const wxString
& item
,
888 const wxString
& helpString
= wxEmptyString
);
891 Inserts a radio item at position 0.
893 @see Prepend(), AppendRadioItem()
895 wxMenuItem
* PrependRadioItem(int id
, const wxString
& item
,
896 const wxString
& helpString
= wxEmptyString
);
899 Inserts a separator at position 0.
901 @see Prepend(), AppendSeparator()
903 wxMenuItem
* PrependSeparator();
906 Removes the menu item from the menu but doesn't delete the associated C++
907 object. This allows you to reuse the same item later by adding it back to
908 the menu (especially useful with submenus).
911 The identifier of the menu item to remove.
913 @return A pointer to the item which was detached from the menu.
915 wxMenuItem
* Remove(int id
);
918 Removes the menu item from the menu but doesn't delete the associated C++
919 object. This allows you to reuse the same item later by adding it back to
920 the menu (especially useful with submenus).
923 The menu item to remove.
925 @return A pointer to the item which was detached from the menu.
927 wxMenuItem
* Remove(wxMenuItem
* item
);
930 Sets an item's help string.
933 The menu item identifier.
935 The help string to set.
939 virtual void SetHelpString(int id
, const wxString
& helpString
);
942 Sets the label of a menu item.
945 The menu item identifier.
947 The menu item label to set.
949 @see Append(), GetLabel()
951 void SetLabel(int id
, const wxString
& label
);
954 Sets the title of the menu.
959 @remarks This is relevant only to popup menus, use
960 wxMenuBar::SetLabelTop for the menus in the menubar.
964 virtual void SetTitle(const wxString
& title
);
967 Sends events to @a source (or owning window if @NULL) to update the
970 This is called just before the menu is popped up with wxWindow::PopupMenu,
971 but the application may call it at other times if required.
973 void UpdateUI(wxEvtHandler
* source
= NULL
);