]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/menu.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxMenuBar
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
11 A menu bar is a series of menus accessible from the top of a frame.
14 To respond to a menu selection, provide a handler for EVT_MENU, in the frame
15 that contains the menu bar.
17 If you have a toolbar which uses the same identifiers as your EVT_MENU entries,
18 events from the toolbar will also be processed by your EVT_MENU event handlers.
20 Tip: under Windows, if you discover that menu shortcuts (for example, Alt-F
21 to show the file menu) are not working, check any EVT_CHAR events you are
22 handling in child windows.
23 If you are not calling event.Skip() for events that you don't process in
24 these event handlers, menu shortcuts may cease to work.
29 @see wxMenu, @ref overview_events
31 class wxMenuBar
: public wxWindow
35 Construct an empty menu bar.
38 If wxMB_DOCKABLE the menu bar can be detached (wxGTK only).
40 wxMenuBar(long style
= 0);
43 Construct a menu bar from arrays of menus and titles.
48 An array of menus. Do not use this array again - it now belongs to
51 An array of title strings. Deallocate this array after creating
54 If wxMB_DOCKABLE the menu bar can be detached (wxGTK only).
57 Not supported by wxPerl.
60 wxMenuBar(size_t n
, wxMenu
* menus
[], const wxString titles
[],
64 Destructor, destroying the menu bar and removing it from the parent
70 Adds the item to the end of the menu bar.
73 The menu to add. Do not deallocate this menu after calling Append().
75 The title of the menu, must be non-empty.
77 @return @true on success, @false if an error occurred.
81 virtual bool Append(wxMenu
* menu
, const wxString
& title
);
84 Checks or unchecks a menu item.
87 The menu item identifier.
89 If @true, checks the menu item, otherwise the item is unchecked.
91 @remarks Only use this when the menu bar has been associated with a
92 frame; otherwise, use the wxMenu equivalent call.
94 void Check(int id
, bool check
);
97 Enables or disables (greys out) a menu item.
100 The menu item identifier.
102 @true to enable the item, @false to disable it.
104 @remarks Only use this when the menu bar has been associated with a
105 frame; otherwise, use the wxMenu equivalent call.
107 void Enable(int id
, bool enable
);
110 Returns true if the menu with the given index is enabled.
113 The menu position (0-based)
117 bool IsEnabledTop(size_t pos
) const;
120 Enables or disables a whole menu.
123 The position of the menu, starting from zero.
125 @true to enable the menu, @false to disable it.
127 @remarks Only use this when the menu bar has been associated with a frame.
129 virtual void EnableTop(size_t pos
, bool enable
);
132 Finds the menu item object associated with the given menu item identifier.
135 Menu item identifier.
137 If not @NULL, menu will get set to the associated menu.
139 @return The found menu item object, or @NULL if one was not found.
142 In wxPerl this method takes just the @a id parameter;
143 in scalar context it returns the associated @c Wx::MenuItem, in list
144 context it returns a 2-element list (item, submenu).
147 virtual wxMenuItem
* FindItem(int id
, wxMenu
** menu
= NULL
) const;
150 Returns the index of the menu with the given @a title or @c wxNOT_FOUND if no
151 such menu exists in this menubar.
153 The @a title parameter may specify either the menu title
154 (with accelerator characters, i.e. @c "&File") or just the
155 menu label (@c "File") indifferently.
157 int FindMenu(const wxString
& title
) const;
160 Finds the menu item id for a menu name/menu item string pair.
167 @return The menu item identifier, or wxNOT_FOUND if none was found.
169 @remarks Any special menu codes are stripped out of source and target
170 strings before matching.
172 virtual int FindMenuItem(const wxString
& menuString
,
173 const wxString
& itemString
) const;
176 Gets the help string associated with the menu item identifier.
179 The menu item identifier.
181 @return The help string, or the empty string if there was no help string
182 or the menu item was not found.
186 wxString
GetHelpString(int id
) const;
189 Gets the label associated with a menu item.
192 The menu item identifier.
194 @return The menu item label, or the empty string if the item was not
197 @remarks Use only after the menubar has been associated with a frame.
199 wxString
GetLabel(int id
) const;
202 Returns the label of a top-level menu. Note that the returned string does not
203 include the accelerator characters which could have been specified in the menu
204 title string during its construction.
207 Position of the menu on the menu bar, starting from zero.
209 @return The menu label, or the empty string if the menu was not found.
211 @remarks Use only after the menubar has been associated with a frame.
214 This function is deprecated in favour of GetMenuLabel() and GetMenuLabelText().
218 wxString
GetLabelTop(size_t pos
) const;
221 Returns the menu at @a menuIndex (zero-based).
223 wxMenu
* GetMenu(size_t menuIndex
) const;
226 Returns the number of menus in this menubar.
228 size_t GetMenuCount() const;
231 Returns the label of a top-level menu. Note that the returned string
232 includes the accelerator characters that have been specified in the menu
233 title string during its construction.
236 Position of the menu on the menu bar, starting from zero.
238 @return The menu label, or the empty string if the menu was not found.
240 @remarks Use only after the menubar has been associated with a frame.
242 @see GetMenuLabelText(), SetMenuLabel()
244 virtual wxString
GetMenuLabel(size_t pos
) const;
247 Returns the label of a top-level menu. Note that the returned string does not
248 include any accelerator characters that may have been specified in the menu
249 title string during its construction.
252 Position of the menu on the menu bar, starting from zero.
254 @return The menu label, or the empty string if the menu was not found.
256 @remarks Use only after the menubar has been associated with a frame.
258 @see GetMenuLabel(), SetMenuLabel()
260 virtual wxString
GetMenuLabelText(size_t pos
) const;
263 Inserts the menu at the given position into the menu bar. Inserting menu at
264 position 0 will insert it in the very beginning of it, inserting at position
265 GetMenuCount() is the same as calling Append().
268 The position of the new menu in the menu bar
270 The menu to add. wxMenuBar owns the menu and will free it.
272 The title of the menu.
274 @return @true on success, @false if an error occurred.
278 virtual bool Insert(size_t pos
, wxMenu
* menu
, const wxString
& title
);
281 Determines whether an item is checked.
284 The menu item identifier.
286 @return @true if the item was found and is checked, @false otherwise.
288 bool IsChecked(int id
) const;
291 Determines whether an item is enabled.
294 The menu item identifier.
296 @return @true if the item was found and is enabled, @false otherwise.
298 bool IsEnabled(int id
) const;
303 virtual void Refresh(bool eraseBackground
= true, const wxRect
* rect
= NULL
);
306 Removes the menu from the menu bar and returns the menu object - the caller
307 is responsible for deleting it. This function may be used together with
308 Insert() to change the menubar dynamically.
312 virtual wxMenu
* Remove(size_t pos
);
315 Replaces the menu at the given position with another one.
318 The position of the new menu in the menu bar
322 The title of the menu.
324 @return The menu which was previously at position pos.
325 The caller is responsible for deleting it.
327 @see Insert(), Remove()
329 virtual wxMenu
* Replace(size_t pos
, wxMenu
* menu
, const wxString
& title
);
332 Sets the help string associated with a menu item.
335 Menu item identifier.
337 Help string to associate with the menu item.
341 void SetHelpString(int id
, const wxString
& helpString
);
344 Sets the label of a menu item.
347 Menu item identifier.
351 @remarks Use only after the menubar has been associated with a frame.
355 void SetLabel(int id
, const wxString
& label
);
358 Sets the label of a top-level menu.
361 The position of a menu on the menu bar, starting from zero.
365 @remarks Use only after the menubar has been associated with a frame.
368 This function has been deprecated in favour of SetMenuLabel().
372 void SetLabelTop(size_t pos
, const wxString
& label
);
375 Sets the label of a top-level menu.
378 The position of a menu on the menu bar, starting from zero.
382 @remarks Use only after the menubar has been associated with a frame.
384 virtual void SetMenuLabel(size_t pos
, const wxString
& label
);
387 Enables you to set the global menubar on Mac, that is, the menubar displayed
388 when the app is running without any frames open.
393 @remarks Only exists on Mac, other platforms do not have this method.
397 static void MacSetCommonMenuBar(wxMenuBar
* menubar
);
400 Enables you to get the global menubar on Mac, that is, the menubar displayed
401 when the app is running without any frames open.
403 @return The global menubar.
405 @remarks Only exists on Mac, other platforms do not have this method.
409 static wxMenuBar
* MacGetCommonMenuBar();
411 wxFrame
*GetFrame() const;
412 bool IsAttached() const;
413 virtual void Attach(wxFrame
*frame
);
414 virtual void Detach();
423 A menu is a popup (or pull down) list of items, one of which may be
424 selected before the menu goes away (clicking elsewhere dismisses the
425 menu). Menus may be used to construct either menu bars or popup menus.
427 A menu item has an integer ID associated with it which can be used to
428 identify the selection, or to change the menu item in some way. A menu item
429 with a special identifier @e wxID_SEPARATOR is a separator item and doesn't
430 have an associated command but just makes a separator line appear in the
434 Please note that @e wxID_ABOUT and @e wxID_EXIT are predefined by wxWidgets
435 and have a special meaning since entries using these IDs will be taken out
436 of the normal menus under MacOS X and will be inserted into the system menu
437 (following the appropriate MacOS X interface guideline).
439 Menu items may be either @e normal items, @e check items or @e radio items.
440 Normal items don't have any special properties while the check items have a
441 boolean flag associated to them and they show a checkmark in the menu when
443 wxWidgets automatically toggles the flag value when the item is clicked and its
444 value may be retrieved using either wxMenu::IsChecked method of wxMenu or
445 wxMenuBar itself or by using wxEvent::IsChecked when you get the menu
446 notification for the item in question.
448 The radio items are similar to the check items except that all the other items
449 in the same radio group are unchecked when a radio item is checked. The radio
450 group is formed by a contiguous range of radio items, i.e. it starts at the
451 first item of this kind and ends with the first item of a different kind (or
452 the end of the menu). Notice that because the radio groups are defined in terms
453 of the item positions inserting or removing the items in the menu containing
454 the radio items risks to not work correctly.
457 @section menu_allocation Allocation strategy
459 All menus must be created on the @b heap because all menus attached to a
460 menubar or to another menu will be deleted by their parent when it is
461 deleted. The only exception to this rule are the popup menus (i.e. menus
462 used with wxWindow::PopupMenu()) as wxWidgets does not destroy them to
463 allow reusing the same menu more than once. But the exception applies only
464 to the menus themselves and not to any submenus of popup menus which are
465 still destroyed by wxWidgets as usual and so must be heap-allocated.
467 As the frame menubar is deleted by the frame itself, it means that normally
468 all menus used are deleted automatically.
471 @section menu_eventhandling Event handling
473 If the menu is part of a menubar, then wxMenuBar event processing is used.
475 With a popup menu (see wxWindow::PopupMenu), there is a variety of ways to
476 handle a menu selection event (@c wxEVT_MENU):
477 - Provide @c EVT_MENU handlers in the window which pops up the menu, or in an
478 ancestor of that window (the simplest method);
479 - Derive a new class from wxMenu and define event table entries using the @c EVT_MENU macro;
480 - Set a new event handler for wxMenu, through wxEvtHandler::SetNextHandler,
481 specifying an object whose class has @c EVT_MENU entries;
483 Note that instead of static @c EVT_MENU macros you can also use dynamic
484 connection; see @ref overview_events_bind.
489 @see wxMenuBar, wxWindow::PopupMenu, @ref overview_events,
490 @ref wxFileHistory "wxFileHistory (most recently used files menu)"
492 class wxMenu
: public wxEvtHandler
497 Constructs a wxMenu object.
502 Constructs a wxMenu object.
505 If set to wxMENU_TEAROFF, the menu will be detachable (wxGTK only).
510 Constructs a wxMenu object with a title.
513 Title at the top of the menu (not always supported).
515 If set to wxMENU_TEAROFF, the menu will be detachable (wxGTK only).
517 wxMenu(const wxString
& title
, long style
= 0);
520 Destructor, destroying the menu.
523 Under Motif, a popup menu must have a valid parent (the window
524 it was last popped up on) when being destroyed. Therefore, make sure
525 you delete or re-use the popup menu @e before destroying the parent
526 window. Re-use in this context means popping up the menu on a different
527 window from last time, which causes an implicit destruction and
528 recreation of internal data structures.
536 The menu command identifier.
538 The string to appear on the menu item.
539 See wxMenuItem::SetItemLabel() for more details.
541 An optional help string associated with the item.
542 By default, the handler for the @c wxEVT_MENU_HIGHLIGHT event displays
543 this string in the status line.
545 May be @c wxITEM_SEPARATOR, @c wxITEM_NORMAL, @c wxITEM_CHECK or @c wxITEM_RADIO.
549 m_pFileMenu->Append(ID_NEW_FILE, "&New file\tCTRL+N", "Creates a new XYZ document");
551 or even better for stock menu items (see wxMenuItem::wxMenuItem):
553 m_pFileMenu->Append(wxID_NEW, "", "Creates a new XYZ document");
557 This command can be used after the menu has been shown, as well as on
558 initial creation of a menu or menubar.
560 @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
561 AppendSubMenu(), Insert(), SetLabel(), GetHelpString(),
562 SetHelpString(), wxMenuItem
564 wxMenuItem
* Append(int id
, const wxString
& item
= wxEmptyString
,
565 const wxString
& helpString
= wxEmptyString
,
566 wxItemKind kind
= wxITEM_NORMAL
);
571 @deprecated This function is deprecated, use AppendSubMenu() instead.
574 The menu command identifier.
576 The string to appear on the menu item.
580 An optional help string associated with the item.
581 By default, the handler for the @c wxEVT_MENU_HIGHLIGHT event displays
582 this string in the status line.
584 @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
585 AppendSubMenu(), Insert(), SetLabel(), GetHelpString(),
586 SetHelpString(), wxMenuItem
588 wxMenuItem
* Append(int id
, const wxString
& item
, wxMenu
* subMenu
,
589 const wxString
& helpString
= wxEmptyString
);
592 Adds a menu item object.
594 This is the most generic variant of Append() method because it may be
595 used for both items (including separators) and submenus and because
596 you can also specify various extra properties of a menu item this way,
597 such as bitmaps and fonts.
600 A menuitem object. It will be owned by the wxMenu object after this
601 function is called, so do not delete it yourself.
604 See the remarks for the other Append() overloads.
606 @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
607 AppendSubMenu(), Insert(), SetLabel(), GetHelpString(),
608 SetHelpString(), wxMenuItem
610 wxMenuItem
* Append(wxMenuItem
* menuItem
);
613 Adds a checkable item to the end of the menu.
615 @see Append(), InsertCheckItem()
617 wxMenuItem
* AppendCheckItem(int id
, const wxString
& item
,
618 const wxString
& help
= wxEmptyString
);
621 Adds a radio item to the end of the menu.
622 All consequent radio items form a group and when an item in the group is
623 checked, all the others are automatically unchecked.
625 @note Radio items are not supported under wxMotif.
627 @see Append(), InsertRadioItem()
629 wxMenuItem
* AppendRadioItem(int id
, const wxString
& item
,
630 const wxString
& help
= wxEmptyString
);
633 Adds a separator to the end of the menu.
635 @see Append(), InsertSeparator()
637 wxMenuItem
* AppendSeparator();
640 Adds the given @a submenu to this menu. @a text is the text shown in the
641 menu for it and @a help is the help string shown in the status bar when the
642 submenu item is selected.
644 wxMenuItem
* AppendSubMenu(wxMenu
* submenu
, const wxString
& text
,
645 const wxString
& help
= wxEmptyString
);
648 Inserts a break in a menu, causing the next appended item to appear in
651 virtual void Break();
654 Checks or unchecks the menu item.
657 The menu item identifier.
659 If @true, the item will be checked, otherwise it will be unchecked.
663 void Check(int id
, bool check
);
666 Deletes the menu item from the menu. If the item is a submenu, it will
667 @b not be deleted. Use Destroy() if you want to delete a submenu.
670 Id of the menu item to be deleted.
672 @see FindItem(), Destroy(), Remove()
677 Deletes the menu item from the menu. If the item is a submenu, it will
678 @b not be deleted. Use Destroy() if you want to delete a submenu.
681 Menu item to be deleted.
683 @see FindItem(), Destroy(), Remove()
685 bool Delete(wxMenuItem
* item
);
688 Deletes the menu item from the menu. If the item is a submenu, it will
689 be deleted. Use Remove() if you want to keep the submenu (for example,
693 Id of the menu item to be deleted.
695 @see FindItem(), Delete(), Remove()
697 bool Destroy(int id
);
700 Deletes the menu item from the menu. If the item is a submenu, it will
701 be deleted. Use Remove() if you want to keep the submenu (for example,
705 Menu item to be deleted.
707 @see FindItem(), Delete(), Remove()
709 bool Destroy(wxMenuItem
* item
);
712 Enables or disables (greys out) a menu item.
715 The menu item identifier.
717 @true to enable the menu item, @false to disable it.
721 void Enable(int id
, bool enable
);
724 Finds the menu item object associated with the given menu item identifier
725 and, optionally, the position of the item in the menu.
727 Unlike FindItem(), this function doesn't recurse but only looks at the
728 direct children of this menu.
731 The identifier of the menu item to find.
733 If the pointer is not @NULL, it is filled with the item's position if
734 it was found or @c (size_t)wxNOT_FOUND otherwise.
736 Menu item object or @NULL if not found.
738 wxMenuItem
*FindChildItem(int id
, size_t *pos
= NULL
) const;
741 Finds the menu id for a menu item string.
744 Menu item string to find.
746 @return Menu item identifier, or wxNOT_FOUND if none is found.
748 @remarks Any special menu codes are stripped out of source and target
749 strings before matching.
751 virtual int FindItem(const wxString
& itemString
) const;
754 Finds the menu item object associated with the given menu item identifier and,
755 optionally, the (sub)menu it belongs to.
758 Menu item identifier.
760 If the pointer is not @NULL, it will be filled with the item's
761 parent menu (if the item was found)
763 @return Menu item object or NULL if none is found.
765 wxMenuItem
* FindItem(int id
, wxMenu
** menu
= NULL
) const;
768 Returns the wxMenuItem given a position in the menu.
770 wxMenuItem
* FindItemByPosition(size_t position
) const;
773 Returns the help string associated with a menu item.
776 The menu item identifier.
778 @return The help string, or the empty string if there is no help string
779 or the item was not found.
781 @see SetHelpString(), Append()
783 virtual wxString
GetHelpString(int id
) const;
786 Returns a menu item label.
789 The menu item identifier.
791 @return The item label, or the empty string if the item was not found.
793 @see GetLabelText(), SetLabel()
795 wxString
GetLabel(int id
) const;
798 Returns a menu item label, without any of the original mnemonics and
802 The menu item identifier.
804 @return The item label, or the empty string if the item was not found.
806 @see GetLabel(), SetLabel()
808 wxString
GetLabelText(int id
) const;
811 Returns the number of items in the menu.
813 size_t GetMenuItemCount() const;
817 Returns the list of items in the menu.
819 wxMenuItemList is a pseudo-template list class containing wxMenuItem
820 pointers, see wxList.
822 wxMenuItemList
& GetMenuItems();
823 const wxMenuItemList
& GetMenuItems() const;
827 Returns the title of the menu.
831 const wxString
& GetTitle() const;
834 Inserts the given @a item before the position @a pos.
836 Inserting the item at position GetMenuItemCount() is the same
839 @see Append(), Prepend()
841 wxMenuItem
* Insert(size_t pos
, wxMenuItem
* menuItem
);
844 Inserts the given @a item before the position @a pos.
846 Inserting the item at position GetMenuItemCount() is the same
849 @see Append(), Prepend()
851 wxMenuItem
* Insert(size_t pos
, int id
,
852 const wxString
& item
= wxEmptyString
,
853 const wxString
& helpString
= wxEmptyString
,
854 wxItemKind kind
= wxITEM_NORMAL
);
857 Inserts a checkable item at the given position.
859 @see Insert(), AppendCheckItem()
861 wxMenuItem
* InsertCheckItem(size_t pos
, int id
, const wxString
& item
,
862 const wxString
& helpString
= wxEmptyString
);
865 Inserts a radio item at the given position.
867 @see Insert(), AppendRadioItem()
869 wxMenuItem
* InsertRadioItem(size_t pos
, int id
, const wxString
& item
,
870 const wxString
& helpString
= wxEmptyString
);
873 Inserts a separator at the given position.
875 @see Insert(), AppendSeparator()
877 wxMenuItem
* InsertSeparator(size_t pos
);
880 Determines whether a menu item is checked.
883 The menu item identifier.
885 @return @true if the menu item is checked, @false otherwise.
889 bool IsChecked(int id
) const;
892 Determines whether a menu item is enabled.
895 The menu item identifier.
897 @return @true if the menu item is enabled, @false otherwise.
901 bool IsEnabled(int id
) const;
904 Inserts the given @a item at position 0, i.e.\ before all the other
907 @see Append(), Insert()
909 wxMenuItem
* Prepend(wxMenuItem
* item
);
912 Inserts the given @a item at position 0, i.e.\ before all the other
915 @see Append(), Insert()
917 wxMenuItem
* Prepend(int id
, const wxString
& item
= wxEmptyString
,
918 const wxString
& helpString
= wxEmptyString
,
919 wxItemKind kind
= wxITEM_NORMAL
);
922 Inserts a checkable item at position 0.
924 @see Prepend(), AppendCheckItem()
926 wxMenuItem
* PrependCheckItem(int id
, const wxString
& item
,
927 const wxString
& helpString
= wxEmptyString
);
930 Inserts a radio item at position 0.
932 @see Prepend(), AppendRadioItem()
934 wxMenuItem
* PrependRadioItem(int id
, const wxString
& item
,
935 const wxString
& helpString
= wxEmptyString
);
938 Inserts a separator at position 0.
940 @see Prepend(), AppendSeparator()
942 wxMenuItem
* PrependSeparator();
945 Removes the menu item from the menu but doesn't delete the associated C++
946 object. This allows you to reuse the same item later by adding it back to
947 the menu (especially useful with submenus).
950 The identifier of the menu item to remove.
952 @return A pointer to the item which was detached from the menu.
954 wxMenuItem
* Remove(int id
);
957 Removes the menu item from the menu but doesn't delete the associated C++
958 object. This allows you to reuse the same item later by adding it back to
959 the menu (especially useful with submenus).
962 The menu item to remove.
964 @return A pointer to the item which was detached from the menu.
966 wxMenuItem
* Remove(wxMenuItem
* item
);
969 Sets an item's help string.
972 The menu item identifier.
974 The help string to set.
978 virtual void SetHelpString(int id
, const wxString
& helpString
);
981 Sets the label of a menu item.
984 The menu item identifier.
986 The menu item label to set.
988 @see Append(), GetLabel()
990 void SetLabel(int id
, const wxString
& label
);
993 Sets the title of the menu.
998 @remarks Notice that you can only call this method directly for the
999 popup menus, to change the title of a menu that is part of a menu
1000 bar you need to use wxMenuBar::SetLabelTop().
1004 virtual void SetTitle(const wxString
& title
);
1007 Sends events to @a source (or owning window if @NULL) to update the
1010 This is called just before the menu is popped up with wxWindow::PopupMenu,
1011 but the application may call it at other times if required.
1013 void UpdateUI(wxEvtHandler
* source
= NULL
);
1016 void SetInvokingWindow(wxWindow
*win
);
1017 wxWindow
*GetInvokingWindow() const;
1018 wxWindow
*GetWindow() const;
1019 long GetStyle() const;
1020 void SetParent(wxMenu
*parent
);
1021 wxMenu
*GetParent() const;
1023 virtual void Attach(wxMenuBar
*menubar
);
1024 virtual void Detach();
1025 bool IsAttached() const;