]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/menu.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxMenuBar
4 // Author: wxWidgets team
6 // Licence: wxWindows licence
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_events
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).
58 Not supported by wxPerl.
61 wxMenuBar(size_t n
, wxMenu
* menus
[], const wxString titles
[],
65 Destructor, destroying the menu bar and removing it from the parent
71 Adds the item to the end of the menu bar.
74 The menu to add. Do not deallocate this menu after calling Append().
76 The title of the menu, must be non-empty.
78 @return @true on success, @false if an error occurred.
82 virtual bool Append(wxMenu
* menu
, const wxString
& title
);
85 Checks or unchecks a menu item.
88 The menu item identifier.
90 If @true, checks the menu item, otherwise the item is unchecked.
92 @remarks Only use this when the menu bar has been associated with a
93 frame; otherwise, use the wxMenu equivalent call.
95 void Check(int id
, bool check
);
98 Enables or disables (greys out) a menu item.
101 The menu item identifier.
103 @true to enable the item, @false to disable it.
105 @remarks Only use this when the menu bar has been associated with a
106 frame; otherwise, use the wxMenu equivalent call.
108 void Enable(int id
, bool enable
);
111 Enables or disables a whole menu.
114 The position of the menu, starting from zero.
116 @true to enable the menu, @false to disable it.
118 @remarks Only use this when the menu bar has been associated with a frame.
120 virtual void EnableTop(size_t pos
, bool enable
);
123 Finds the menu item object associated with the given menu item identifier.
126 Menu item identifier.
128 If not @NULL, menu will get set to the associated menu.
130 @return The found menu item object, or @NULL if one was not found.
133 In wxPerl this method takes just the @a id parameter;
134 in scalar context it returns the associated @c Wx::MenuItem, in list
135 context it returns a 2-element list (item, submenu).
138 virtual wxMenuItem
* FindItem(int id
, wxMenu
** menu
= NULL
) const;
141 Returns the index of the menu with the given @a title or @c wxNOT_FOUND if no
142 such menu exists in this menubar.
144 The @a title parameter may specify either the menu title
145 (with accelerator characters, i.e. @c "&File") or just the
146 menu label (@c "File") indifferently.
148 int FindMenu(const wxString
& title
) const;
151 Finds the menu item id for a menu name/menu item string pair.
158 @return The menu item identifier, or wxNOT_FOUND if none was found.
160 @remarks Any special menu codes are stripped out of source and target
161 strings before matching.
163 virtual int FindMenuItem(const wxString
& menuString
,
164 const wxString
& itemString
) const;
167 Gets the help string associated with the menu item identifier.
170 The menu item identifier.
172 @return The help string, or the empty string if there was no help string
173 or the menu item was not found.
177 wxString
GetHelpString(int id
) const;
180 Gets the label associated with a menu item.
183 The menu item identifier.
185 @return The menu item label, or the empty string if the item was not
188 @remarks Use only after the menubar has been associated with a frame.
190 wxString
GetLabel(int id
) const;
193 Returns the label of a top-level menu. Note that the returned string does not
194 include the accelerator characters which could have been specified in the menu
195 title string during its construction.
198 Position of the menu on the menu bar, starting from zero.
200 @return The menu label, or the empty string if the menu was not found.
202 @remarks Use only after the menubar has been associated with a frame.
205 This function is deprecated in favour of GetMenuLabel() and GetMenuLabelText().
209 wxString
GetLabelTop(size_t pos
) const;
212 Returns the menu at @a menuIndex (zero-based).
214 wxMenu
* GetMenu(size_t menuIndex
) const;
217 Returns the number of menus in this menubar.
219 size_t GetMenuCount() const;
222 Returns the label of a top-level menu. Note that the returned string
223 includes the accelerator characters that have been specified in the menu
224 title string during its construction.
227 Position of the menu on the menu bar, starting from zero.
229 @return The menu label, or the empty string if the menu was not found.
231 @remarks Use only after the menubar has been associated with a frame.
233 @see GetMenuLabelText(), SetMenuLabel()
235 virtual wxString
GetMenuLabel(size_t pos
) const;
238 Returns the label of a top-level menu. Note that the returned string does not
239 include any accelerator characters that may have been specified in the menu
240 title string during its construction.
243 Position of the menu on the menu bar, starting from zero.
245 @return The menu label, or the empty string if the menu was not found.
247 @remarks Use only after the menubar has been associated with a frame.
249 @see GetMenuLabel(), SetMenuLabel()
251 virtual wxString
GetMenuLabelText(size_t pos
) const;
254 Inserts the menu at the given position into the menu bar. Inserting menu at
255 position 0 will insert it in the very beginning of it, inserting at position
256 GetMenuCount() is the same as calling Append().
259 The position of the new menu in the menu bar
261 The menu to add. wxMenuBar owns the menu and will free it.
263 The title of the menu.
265 @return @true on success, @false if an error occurred.
269 virtual bool Insert(size_t pos
, wxMenu
* menu
, const wxString
& title
);
272 Determines whether an item is checked.
275 The menu item identifier.
277 @return @true if the item was found and is checked, @false otherwise.
279 bool IsChecked(int id
) const;
282 Determines whether an item is enabled.
285 The menu item identifier.
287 @return @true if the item was found and is enabled, @false otherwise.
289 bool IsEnabled(int id
) const;
294 virtual void Refresh(bool eraseBackground
= true, const wxRect
* rect
= NULL
);
297 Removes the menu from the menu bar and returns the menu object - the caller
298 is responsible for deleting it. This function may be used together with
299 Insert() to change the menubar dynamically.
303 virtual wxMenu
* Remove(size_t pos
);
306 Replaces the menu at the given position with another one.
309 The position of the new menu in the menu bar
313 The title of the menu.
315 @return The menu which was previously at position pos.
316 The caller is responsible for deleting it.
318 @see Insert(), Remove()
320 virtual wxMenu
* Replace(size_t pos
, wxMenu
* menu
, const wxString
& title
);
323 Sets the help string associated with a menu item.
326 Menu item identifier.
328 Help string to associate with the menu item.
332 void SetHelpString(int id
, const wxString
& helpString
);
335 Sets the label of a menu item.
338 Menu item identifier.
342 @remarks Use only after the menubar has been associated with a frame.
346 void SetLabel(int id
, const wxString
& label
);
349 Sets the label of a top-level menu.
352 The position of a menu on the menu bar, starting from zero.
356 @remarks Use only after the menubar has been associated with a frame.
359 This function has been deprecated in favour of SetMenuLabel().
363 void SetLabelTop(size_t pos
, const wxString
& label
);
366 Sets the label of a top-level menu.
369 The position of a menu on the menu bar, starting from zero.
373 @remarks Use only after the menubar has been associated with a frame.
375 virtual void SetMenuLabel(size_t pos
, const wxString
& label
);
378 Enables you to set the global menubar on Mac, that is, the menubar displayed
379 when the app is running without any frames open.
384 @remarks Only exists on Mac, other platforms do not have this method.
386 static void MacSetCommonMenuBar(wxMenuBar
* menubar
);
389 Enables you to get the global menubar on Mac, that is, the menubar displayed
390 when the app is running without any frames open.
392 @return The global menubar.
394 @remarks Only exists on Mac, other platforms do not have this method.
396 static wxMenuBar
* MacGetCommonMenuBar();
405 A menu is a popup (or pull down) list of items, one of which may be
406 selected before the menu goes away (clicking elsewhere dismisses the
407 menu). Menus may be used to construct either menu bars or popup menus.
409 A menu item has an integer ID associated with it which can be used to
410 identify the selection, or to change the menu item in some way. A menu item
411 with a special identifier @e wxID_SEPARATOR is a separator item and doesn't
412 have an associated command but just makes a separator line appear in the
416 Please note that @e wxID_ABOUT and @e wxID_EXIT are predefined by wxWidgets
417 and have a special meaning since entries using these IDs will be taken out
418 of the normal menus under MacOS X and will be inserted into the system menu
419 (following the appropriate MacOS X interface guideline).
420 On PalmOS @e wxID_EXIT is disabled according to Palm OS Companion guidelines.
422 Menu items may be either @e normal items, @e check items or @e radio items.
423 Normal items don't have any special properties while the check items have a
424 boolean flag associated to them and they show a checkmark in the menu when
426 wxWidgets automatically toggles the flag value when the item is clicked and its
427 value may be retrieved using either wxMenu::IsChecked method of wxMenu or
428 wxMenuBar itself or by using wxEvent::IsChecked when you get the menu
429 notification for the item in question.
431 The radio items are similar to the check items except that all the other items
432 in the same radio group are unchecked when a radio item is checked. The radio
433 group is formed by a contiguous range of radio items, i.e. it starts at the
434 first item of this kind and ends with the first item of a different kind (or
435 the end of the menu). Notice that because the radio groups are defined in terms
436 of the item positions inserting or removing the items in the menu containing
437 the radio items risks to not work correctly.
440 @section menu_allocation Allocation strategy
442 All menus must be created on the @b heap because all menus attached to a
443 menubar or to another menu will be deleted by their parent when it is
444 deleted. The only exception to this rule are the popup menus (i.e. menus
445 used with wxWindow::PopupMenu()) as wxWidgets does not destroy them to
446 allow reusing the same menu more than once. But the exception applies only
447 to the menus themselves and not to any submenus of popup menus which are
448 still destroyed by wxWidgets as usual and so must be heap-allocated.
450 As the frame menubar is deleted by the frame itself, it means that normally
451 all menus used are deleted automatically.
454 @section menu_eventhandling Event handling
456 If the menu is part of a menubar, then wxMenuBar event processing is used.
458 With a popup menu (see wxWindow::PopupMenu), there is a variety of ways to
459 handle a menu selection event (@c wxEVT_COMMAND_MENU_SELECTED):
460 - Provide @c EVT_MENU handlers in the window which pops up the menu, or in an
461 ancestor of that window (the simplest method);
462 - Derive a new class from wxMenu and define event table entries using the @c EVT_MENU macro;
463 - Set a new event handler for wxMenu, through wxEvtHandler::SetNextHandler,
464 specifying an object whose class has @c EVT_MENU entries;
466 Note that instead of static @c EVT_MENU macros you can also use dynamic
467 connection; see @ref overview_events_bind.
472 @see wxMenuBar, wxWindow::PopupMenu, @ref overview_events,
473 @ref wxFileHistory "wxFileHistory (most recently used files menu)"
475 class wxMenu
: public wxEvtHandler
480 Constructs a wxMenu object.
485 Constructs a wxMenu object.
488 If set to wxMENU_TEAROFF, the menu will be detachable (wxGTK only).
493 Constructs a wxMenu object with a title.
496 Title at the top of the menu (not always supported).
498 If set to wxMENU_TEAROFF, the menu will be detachable (wxGTK only).
500 wxMenu(const wxString
& title
, long style
= 0);
503 Destructor, destroying the menu.
506 Under Motif, a popup menu must have a valid parent (the window
507 it was last popped up on) when being destroyed. Therefore, make sure
508 you delete or re-use the popup menu @e before destroying the parent
509 window. Re-use in this context means popping up the menu on a different
510 window from last time, which causes an implicit destruction and
511 recreation of internal data structures.
519 The menu command identifier.
521 The string to appear on the menu item.
522 See wxMenuItem::SetItemLabel() for more details.
524 An optional help string associated with the item.
525 By default, the handler for the @c wxEVT_MENU_HIGHLIGHT event displays
526 this string in the status line.
528 May be @c wxITEM_SEPARATOR, @c wxITEM_NORMAL, @c wxITEM_CHECK or @c wxITEM_RADIO.
532 m_pFileMenu->Append(ID_NEW_FILE, "&New file\tCTRL+N", "Creates a new XYZ document");
534 or even better for stock menu items (see wxMenuItem::wxMenuItem):
536 m_pFileMenu->Append(wxID_NEW, "", "Creates a new XYZ document");
540 This command can be used after the menu has been shown, as well as on
541 initial creation of a menu or menubar.
543 @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
544 AppendSubMenu(), Insert(), SetLabel(), GetHelpString(),
545 SetHelpString(), wxMenuItem
547 wxMenuItem
* Append(int id
, const wxString
& item
= wxEmptyString
,
548 const wxString
& helpString
= wxEmptyString
,
549 wxItemKind kind
= wxITEM_NORMAL
);
554 @deprecated This function is deprecated, use AppendSubMenu() instead.
557 The menu command identifier.
559 The string to appear on the menu item.
563 An optional help string associated with the item.
564 By default, the handler for the @c wxEVT_MENU_HIGHLIGHT event displays
565 this string in the status line.
567 @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
568 AppendSubMenu(), Insert(), SetLabel(), GetHelpString(),
569 SetHelpString(), wxMenuItem
571 wxMenuItem
* Append(int id
, const wxString
& item
, wxMenu
* subMenu
,
572 const wxString
& helpString
= wxEmptyString
);
575 Adds a menu item object.
577 This is the most generic variant of Append() method because it may be
578 used for both items (including separators) and submenus and because
579 you can also specify various extra properties of a menu item this way,
580 such as bitmaps and fonts.
583 A menuitem object. It will be owned by the wxMenu object after this
584 function is called, so do not delete it yourself.
587 See the remarks for the other Append() overloads.
589 @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
590 AppendSubMenu(), Insert(), SetLabel(), GetHelpString(),
591 SetHelpString(), wxMenuItem
593 wxMenuItem
* Append(wxMenuItem
* menuItem
);
596 Adds a checkable item to the end of the menu.
598 @see Append(), InsertCheckItem()
600 wxMenuItem
* AppendCheckItem(int id
, const wxString
& item
,
601 const wxString
& help
= wxEmptyString
);
604 Adds a radio item to the end of the menu.
605 All consequent radio items form a group and when an item in the group is
606 checked, all the others are automatically unchecked.
608 @note Radio items are not supported under wxMotif.
610 @see Append(), InsertRadioItem()
612 wxMenuItem
* AppendRadioItem(int id
, const wxString
& item
,
613 const wxString
& help
= wxEmptyString
);
616 Adds a separator to the end of the menu.
618 @see Append(), InsertSeparator()
620 wxMenuItem
* AppendSeparator();
623 Adds the given @a submenu to this menu. @a text is the text shown in the
624 menu for it and @a help is the help string shown in the status bar when the
625 submenu item is selected.
627 wxMenuItem
* AppendSubMenu(wxMenu
* submenu
, const wxString
& text
,
628 const wxString
& help
= wxEmptyString
);
631 Inserts a break in a menu, causing the next appended item to appear in
634 virtual void Break();
637 Checks or unchecks the menu item.
640 The menu item identifier.
642 If @true, the item will be checked, otherwise it will be unchecked.
646 void Check(int id
, bool check
);
649 Deletes the menu item from the menu. If the item is a submenu, it will
650 @b not be deleted. Use Destroy() if you want to delete a submenu.
653 Id of the menu item to be deleted.
655 @see FindItem(), Destroy(), Remove()
660 Deletes the menu item from the menu. If the item is a submenu, it will
661 @b not be deleted. Use Destroy() if you want to delete a submenu.
664 Menu item to be deleted.
666 @see FindItem(), Destroy(), Remove()
668 bool Delete(wxMenuItem
* item
);
671 Deletes the menu item from the menu. If the item is a submenu, it will
672 be deleted. Use Remove() if you want to keep the submenu (for example,
676 Id of the menu item to be deleted.
678 @see FindItem(), Delete(), Remove()
680 bool Destroy(int id
);
683 Deletes the menu item from the menu. If the item is a submenu, it will
684 be deleted. Use Remove() if you want to keep the submenu (for example,
688 Menu item to be deleted.
690 @see FindItem(), Delete(), Remove()
692 bool Destroy(wxMenuItem
* item
);
695 Enables or disables (greys out) a menu item.
698 The menu item identifier.
700 @true to enable the menu item, @false to disable it.
704 void Enable(int id
, bool enable
);
707 Finds the menu item object associated with the given menu item identifier
708 and, optionally, the position of the item in the menu.
710 Unlike FindItem(), this function doesn't recurse but only looks at the
711 direct children of this menu.
714 The identifier of the menu item to find.
716 If the pointer is not @NULL, it is filled with the item's position if
717 it was found or @c (size_t)wxNOT_FOUND otherwise.
719 Menu item object or @NULL if not found.
721 wxMenuItem
*FindChildItem(int id
, size_t *pos
= NULL
) const;
724 Finds the menu id for a menu item string.
727 Menu item string to find.
729 @return Menu item identifier, or wxNOT_FOUND if none is found.
731 @remarks Any special menu codes are stripped out of source and target
732 strings before matching.
734 virtual int FindItem(const wxString
& itemString
) const;
737 Finds the menu item object associated with the given menu item identifier and,
738 optionally, the (sub)menu it belongs to.
741 Menu item identifier.
743 If the pointer is not @NULL, it will be filled with the item's
744 parent menu (if the item was found)
746 @return Menu item object or NULL if none is found.
748 wxMenuItem
* FindItem(int id
, wxMenu
** menu
= NULL
) const;
751 Returns the wxMenuItem given a position in the menu.
753 wxMenuItem
* FindItemByPosition(size_t position
) const;
756 Returns the help string associated with a menu item.
759 The menu item identifier.
761 @return The help string, or the empty string if there is no help string
762 or the item was not found.
764 @see SetHelpString(), Append()
766 virtual wxString
GetHelpString(int id
) const;
769 Returns a menu item label.
772 The menu item identifier.
774 @return The item label, or the empty string if the item was not found.
776 @see GetLabelText(), SetLabel()
778 wxString
GetLabel(int id
) const;
781 Returns a menu item label, without any of the original mnemonics and
785 The menu item identifier.
787 @return The item label, or the empty string if the item was not found.
789 @see GetLabel(), SetLabel()
791 wxString
GetLabelText(int id
) const;
794 Returns the number of items in the menu.
796 size_t GetMenuItemCount() const;
800 Returns the list of items in the menu.
802 wxMenuItemList is a pseudo-template list class containing wxMenuItem
803 pointers, see wxList.
805 wxMenuItemList
& GetMenuItems() const;
806 const wxMenuItemList
& GetMenuItems() const;
810 Returns the title of the menu.
814 const wxString
& GetTitle() const;
817 Inserts the given @a item before the position @a pos.
819 Inserting the item at position GetMenuItemCount() is the same
822 @see Append(), Prepend()
824 wxMenuItem
* Insert(size_t pos
, wxMenuItem
* menuItem
);
827 Inserts the given @a item before the position @a pos.
829 Inserting the item at position GetMenuItemCount() is the same
832 @see Append(), Prepend()
834 wxMenuItem
* Insert(size_t pos
, int id
,
835 const wxString
& item
= wxEmptyString
,
836 const wxString
& helpString
= wxEmptyString
,
837 wxItemKind kind
= wxITEM_NORMAL
);
840 Inserts a checkable item at the given position.
842 @see Insert(), AppendCheckItem()
844 wxMenuItem
* InsertCheckItem(size_t pos
, int id
, const wxString
& item
,
845 const wxString
& helpString
= wxEmptyString
);
848 Inserts a radio item at the given position.
850 @see Insert(), AppendRadioItem()
852 wxMenuItem
* InsertRadioItem(size_t pos
, int id
, const wxString
& item
,
853 const wxString
& helpString
= wxEmptyString
);
856 Inserts a separator at the given position.
858 @see Insert(), AppendSeparator()
860 wxMenuItem
* InsertSeparator(size_t pos
);
863 Determines whether a menu item is checked.
866 The menu item identifier.
868 @return @true if the menu item is checked, @false otherwise.
872 bool IsChecked(int id
) const;
875 Determines whether a menu item is enabled.
878 The menu item identifier.
880 @return @true if the menu item is enabled, @false otherwise.
884 bool IsEnabled(int id
) const;
887 Inserts the given @a item at position 0, i.e. before all the other
890 @see Append(), Insert()
892 wxMenuItem
* Prepend(wxMenuItem
* item
);
895 Inserts the given @a item at position 0, i.e. before all the other
898 @see Append(), Insert()
900 wxMenuItem
* Prepend(int id
, const wxString
& item
= wxEmptyString
,
901 const wxString
& helpString
= wxEmptyString
,
902 wxItemKind kind
= wxITEM_NORMAL
);
905 Inserts a checkable item at position 0.
907 @see Prepend(), AppendCheckItem()
909 wxMenuItem
* PrependCheckItem(int id
, const wxString
& item
,
910 const wxString
& helpString
= wxEmptyString
);
913 Inserts a radio item at position 0.
915 @see Prepend(), AppendRadioItem()
917 wxMenuItem
* PrependRadioItem(int id
, const wxString
& item
,
918 const wxString
& helpString
= wxEmptyString
);
921 Inserts a separator at position 0.
923 @see Prepend(), AppendSeparator()
925 wxMenuItem
* PrependSeparator();
928 Removes the menu item from the menu but doesn't delete the associated C++
929 object. This allows you to reuse the same item later by adding it back to
930 the menu (especially useful with submenus).
933 The identifier of the menu item to remove.
935 @return A pointer to the item which was detached from the menu.
937 wxMenuItem
* Remove(int id
);
940 Removes the menu item from the menu but doesn't delete the associated C++
941 object. This allows you to reuse the same item later by adding it back to
942 the menu (especially useful with submenus).
945 The menu item to remove.
947 @return A pointer to the item which was detached from the menu.
949 wxMenuItem
* Remove(wxMenuItem
* item
);
952 Sets an item's help string.
955 The menu item identifier.
957 The help string to set.
961 virtual void SetHelpString(int id
, const wxString
& helpString
);
964 Sets the label of a menu item.
967 The menu item identifier.
969 The menu item label to set.
971 @see Append(), GetLabel()
973 void SetLabel(int id
, const wxString
& label
);
976 Sets the title of the menu.
981 @remarks Notice that you can only call this method directly for the
982 popup menus, to change the title of a menu that is part of a menu
983 bar you need to use wxMenuBar::SetLabelTop().
987 virtual void SetTitle(const wxString
& title
);
990 Sends events to @a source (or owning window if @NULL) to update the
993 This is called just before the menu is popped up with wxWindow::PopupMenu,
994 but the application may call it at other times if required.
996 void UpdateUI(wxEvtHandler
* source
= NULL
);