X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/fadc2df6823d988f7a264b94e69a7e1bbcac1337..a8bda512079352ba81933e278d9ccdb8ef7a9866:/interface/wx/menu.h?ds=sidebyside diff --git a/interface/wx/menu.h b/interface/wx/menu.h index de697027c9..5cbee4bc0a 100644 --- a/interface/wx/menu.h +++ b/interface/wx/menu.h @@ -3,7 +3,7 @@ // Purpose: interface of wxMenuBar // Author: wxWidgets team // RCS-ID: $Id$ -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// /** @@ -24,11 +24,10 @@ If you are not calling event.Skip() for events that you don't process in these event handlers, menu shortcuts may cease to work. - @library{wxcore} @category{menus} - @see wxMenu, @ref overview_eventhandling + @see wxMenu, @ref overview_events */ class wxMenuBar : public wxWindow { @@ -54,6 +53,10 @@ public: the menu bar. @param style If wxMB_DOCKABLE the menu bar can be detached (wxGTK only). + + @beginWxPerlOnly + Not supported by wxPerl. + @endWxPerlOnly */ wxMenuBar(size_t n, wxMenu* menus[], const wxString titles[], long style = 0); @@ -70,7 +73,7 @@ public: @param menu The menu to add. Do not deallocate this menu after calling Append(). @param title - The title of the menu. + The title of the menu, must be non-empty. @return @true on success, @false if an error occurred. @@ -89,7 +92,7 @@ public: @remarks Only use this when the menu bar has been associated with a frame; otherwise, use the wxMenu equivalent call. */ - void Check(int id, const bool check); + void Check(int id, bool check); /** Enables or disables (greys out) a menu item. @@ -102,7 +105,7 @@ public: @remarks Only use this when the menu bar has been associated with a frame; otherwise, use the wxMenu equivalent call. */ - void Enable(int id, const bool enable); + void Enable(int id, bool enable); /** Enables or disables a whole menu. @@ -114,7 +117,7 @@ public: @remarks Only use this when the menu bar has been associated with a frame. */ - void EnableTop(int pos, const bool enable); + virtual void EnableTop(size_t pos, bool enable); /** Finds the menu item object associated with the given menu item identifier. @@ -125,8 +128,14 @@ public: If not @NULL, menu will get set to the associated menu. @return The found menu item object, or @NULL if one was not found. + + @beginWxPerlOnly + In wxPerl this method takes just the @a id parameter; + in scalar context it returns the associated @c Wx::MenuItem, in list + context it returns a 2-element list (item, submenu). + @endWxPerlOnly */ - wxMenuItem* FindItem(int id, wxMenu menu = NULL) const; + virtual wxMenuItem* FindItem(int id, wxMenu* menu = NULL) const; /** Returns the index of the menu with the given @a title or @c wxNOT_FOUND if no @@ -197,12 +206,12 @@ public: @see SetLabelTop() */ - wxString GetLabelTop(int pos) const; + wxString GetLabelTop(size_t pos) const; /** Returns the menu at @a menuIndex (zero-based). */ - wxMenu* GetMenu(int menuIndex) const; + wxMenu* GetMenu(size_t menuIndex) const; /** Returns the number of menus in this menubar. @@ -223,7 +232,7 @@ public: @see GetMenuLabelText(), SetMenuLabel() */ - wxString GetMenuLabel(int pos) const; + virtual wxString GetMenuLabel(size_t pos) const; /** Returns the label of a top-level menu. Note that the returned string does not @@ -239,7 +248,7 @@ public: @see GetMenuLabel(), SetMenuLabel() */ - wxString GetMenuLabelText(int pos) const; + virtual wxString GetMenuLabelText(size_t pos) const; /** Inserts the menu at the given position into the menu bar. Inserting menu at @@ -282,7 +291,7 @@ public: /** Redraw the menu bar */ - void Refresh(); + virtual void Refresh(bool eraseBackground = true, const wxRect* rect = NULL); /** Removes the menu from the menu bar and returns the menu object - the caller @@ -351,7 +360,7 @@ public: @see GetLabelTop() */ - void SetLabelTop(int pos, const wxString& label); + void SetLabelTop(size_t pos, const wxString& label); /** Sets the label of a top-level menu. @@ -363,7 +372,7 @@ public: @remarks Use only after the menubar has been associated with a frame. */ - void SetMenuLabel(int pos, const wxString& label); + virtual void SetMenuLabel(size_t pos, const wxString& label); }; @@ -377,8 +386,9 @@ public: A menu item has an integer ID associated with it which can be used to identify the selection, or to change the menu item in some way. A menu item - with a special identifier -1 is a separator item and doesn't have an - associated command but just makes a separator line appear in the menu. + with a special identifier @e wxID_SEPARATOR is a separator item and doesn't + have an associated command but just makes a separator line appear in the + menu. @note Please note that @e wxID_ABOUT and @e wxID_EXIT are predefined by wxWidgets @@ -387,7 +397,7 @@ public: (following the appropriate MacOS X interface guideline). On PalmOS @e wxID_EXIT is disabled according to Palm OS Companion guidelines. - Menu items may be either normal items, check items or radio items. + Menu items may be either @e normal items, @e check items or @e radio items. Normal items don't have any special properties while the check items have a boolean flag associated to them and they show a checkmark in the menu when the flag is set. @@ -402,15 +412,19 @@ public: first item of this kind and ends with the first item of a different kind (or the end of the menu). Notice that because the radio groups are defined in terms of the item positions inserting or removing the items in the menu containing - the radio items risks to not work correctly. Finally note that radio items - are not supported under Motif. + the radio items risks to not work correctly. @section menu_allocation Allocation strategy - All menus except the popup ones must be created on the heap. - All menus attached to a menubar or to another menu will be deleted by their - parent when it is deleted. + All menus must be created on the @b heap because all menus attached to a + menubar or to another menu will be deleted by their parent when it is + deleted. The only exception to this rule are the popup menus (i.e. menus + used with wxWindow::PopupMenu()) as wxWidgets does not destroy them to + allow reusing the same menu more than once. But the exception applies only + to the menus themselves and not to any submenus of popup menus which are + still destroyed by wxWidgets as usual and so must be heap-allocated. + As the frame menubar is deleted by the frame itself, it means that normally all menus used are deleted automatically. @@ -418,18 +432,22 @@ public: @section menu_eventhandling Event handling If the menu is part of a menubar, then wxMenuBar event processing is used. - With a popup menu, there is a variety of ways to handle a menu selection event - (wxEVT_COMMAND_MENU_SELECTED). - Derive a new class from wxMenu and define event table entries using the EVT_MENU macro. - Set a new event handler for wxMenu, using an object whose class has EVT_MENU entries. - Provide EVT_MENU handlers in the window which pops up the menu, or in an - ancestor of this window. + With a popup menu (see wxWindow::PopupMenu), there is a variety of ways to + handle a menu selection event (@c wxEVT_COMMAND_MENU_SELECTED): + - Provide @c EVT_MENU handlers in the window which pops up the menu, or in an + ancestor of that window (the simplest method); + - Derive a new class from wxMenu and define event table entries using the @c EVT_MENU macro; + - Set a new event handler for wxMenu, through wxEvtHandler::SetNextHandler, + specifying an object whose class has @c EVT_MENU entries; + + Note that instead of static @c EVT_MENU macros you can also use dynamic + connection; see @ref overview_events_bind. @library{wxcore} @category{menus} - @see wxMenuBar, wxWindow::PopupMenu, @ref overview_eventhandling, + @see wxMenuBar, wxWindow::PopupMenu, @ref overview_events, @ref wxFileHistory "wxFileHistory (most recently used files menu)" */ class wxMenu : public wxEvtHandler @@ -451,7 +469,7 @@ public: @param style If set to wxMENU_TEAROFF, the menu will be detachable (wxGTK only). */ - wxMenu(const wxString& title = "", long style = 0); + wxMenu(const wxString& title, long style = 0); /** Destructor, destroying the menu. @@ -473,45 +491,27 @@ public: The menu command identifier. @param item The string to appear on the menu item. + See wxMenuItem::SetItemLabel() for more details. @param helpString An optional help string associated with the item. - By default, the handler for the wxEVT_MENU_HIGHLIGHT event displays + By default, the handler for the @c wxEVT_MENU_HIGHLIGHT event displays this string in the status line. @param kind - May be wxITEM_SEPARATOR, wxITEM_NORMAL, wxITEM_CHECK or wxITEM_RADIO + May be @c wxITEM_SEPARATOR, @c wxITEM_NORMAL, @c wxITEM_CHECK or @c wxITEM_RADIO. + + Example: + @code + m_pFileMenu->Append(ID_NEW_FILE, "&New file\tCTRL+N", "Creates a new XYZ document"); + @endcode + or even better for stock menu items (see wxMenuItem::wxMenuItem): + @code + m_pFileMenu->Append(wxID_NEW, "", "Creates a new XYZ document"); + @endcode @remarks This command can be used after the menu has been shown, as well as on initial creation of a menu or menubar. - The item string for the normal menu items (not submenus or separators) - may include the accelerator which can be used to activate the menu item - from keyboard. - The accelerator string follows the item label and is separated from it - by a TAB character ('\\t'). - - Its general syntax is any combination of "CTRL", "ALT" and "SHIFT" strings - (case doesn't matter) separated by either '-' or '+' characters and followed - by the accelerator itself. - The accelerator may be any alphanumeric character, any function key - (from F1 to F12) or one of the special characters listed in the table - below (again, case doesn't matter): - - - DEL or DELETE: Delete key - - INS or INSERT: Insert key - - ENTER or RETURN: Enter key - - PGUP: PageUp key - - PGDN: PageDown key - - LEFT: Left cursor arrow key - - RIGHT: Right cursor arrow key - - UP: Up cursor arrow key - - DOWN: Down cursor arrow key - - HOME: Home key - - END: End key - - SPACE: Space - - TAB: Tab key - - ESC: or ESCAPE Escape key (Windows only) - @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(), AppendSubMenu(), Insert(), SetLabel(), GetHelpString(), SetHelpString(), wxMenuItem @@ -533,7 +533,7 @@ public: Pull-right submenu. @param helpString An optional help string associated with the item. - By default, the handler for the wxEVT_MENU_HIGHLIGHT event displays + By default, the handler for the @c wxEVT_MENU_HIGHLIGHT event displays this string in the status line. @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(), @@ -570,17 +570,19 @@ public: @see Append(), InsertCheckItem() */ wxMenuItem* AppendCheckItem(int id, const wxString& item, - const wxString& helpString = ""); + const wxString& help = wxEmptyString); /** Adds a radio item to the end of the menu. All consequent radio items form a group and when an item in the group is checked, all the others are automatically unchecked. + @note Radio items are not supported under wxMotif. + @see Append(), InsertRadioItem() */ wxMenuItem* AppendRadioItem(int id, const wxString& item, - const wxString& helpString = ""); + const wxString& help = wxEmptyString); /** Adds a separator to the end of the menu. @@ -613,7 +615,7 @@ public: @see IsChecked() */ - void Check(int id, const bool check); + void Check(int id, bool check); /** Deletes the menu item from the menu. If the item is a submenu, it will @@ -624,7 +626,7 @@ public: @see FindItem(), Destroy(), Remove() */ - void Delete(int id); + bool Delete(int id); /** Deletes the menu item from the menu. If the item is a submenu, it will @@ -635,7 +637,7 @@ public: @see FindItem(), Destroy(), Remove() */ - void Delete(wxMenuItem* item); + bool Delete(wxMenuItem* item); /** Deletes the menu item from the menu. If the item is a submenu, it will @@ -645,9 +647,9 @@ public: @param id Id of the menu item to be deleted. - @see FindItem(), Deletes(), Remove() + @see FindItem(), Delete(), Remove() */ - void Destroy(int id); + bool Destroy(int id); /** Deletes the menu item from the menu. If the item is a submenu, it will @@ -657,9 +659,9 @@ public: @param item Menu item to be deleted. - @see FindItem(), Deletes(), Remove() + @see FindItem(), Delete(), Remove() */ - void Destroy(wxMenuItem* item); + bool Destroy(wxMenuItem* item); /** Enables or disables (greys out) a menu item. @@ -671,7 +673,24 @@ public: @see IsEnabled() */ - void Enable(int id, const bool enable); + void Enable(int id, bool enable); + + /** + Finds the menu item object associated with the given menu item identifier + and, optionally, the position of the item in the menu. + + Unlike FindItem(), this function doesn't recurse but only looks at the + direct children of this menu. + + @param id + The identifier of the menu item to find. + @param pos + If the pointer is not @NULL, it is filled with the item's position if + it was found or @c (size_t)wxNOT_FOUND otherwise. + @return + Menu item object or @NULL if not found. + */ + wxMenuItem *FindChildItem(int id, size_t *pos = NULL) const; /** Finds the menu id for a menu item string. @@ -698,7 +717,7 @@ public: @return Menu item object or NULL if none is found. */ - const wxMenuItem* FindItem(int id, wxMenu** menu = NULL) const; + wxMenuItem* FindItem(int id, wxMenu** menu = NULL) const; /** Returns the wxMenuItem given a position in the menu. @@ -748,23 +767,23 @@ public: */ size_t GetMenuItemCount() const; + //@{ /** Returns the list of items in the menu. wxMenuItemList is a pseudo-template list class containing wxMenuItem pointers, see wxList. */ - wxMenuItemList GetMenuItems() const; + wxMenuItemList& GetMenuItems() const; + const wxMenuItemList& GetMenuItems() const; + //@} /** Returns the title of the menu. - @remarks This is relevant only to popup menus, use - wxMenuBar::GetMenuLabel for the menus in the menubar. - @see SetTitle() */ - wxString GetTitle() const; + const wxString& GetTitle() const; /** Inserts the given @a item before the position @a pos. @@ -785,8 +804,8 @@ public: @see Append(), Prepend() */ wxMenuItem* Insert(size_t pos, int id, - const wxString& item = "", - const wxString& helpString = "", + const wxString& item = wxEmptyString, + const wxString& helpString = wxEmptyString, wxItemKind kind = wxITEM_NORMAL); /** @@ -794,18 +813,16 @@ public: @see Insert(), AppendCheckItem() */ - wxMenuItem* InsertCheckItem(size_t pos, int id, - const wxString& item, - const wxString& helpString = ""); + wxMenuItem* InsertCheckItem(size_t pos, int id, const wxString& item, + const wxString& helpString = wxEmptyString); /** Inserts a radio item at the given position. @see Insert(), AppendRadioItem() */ - wxMenuItem* InsertRadioItem(size_t pos, int id, - const wxString& item, - const wxString& helpString = ""); + wxMenuItem* InsertRadioItem(size_t pos, int id, const wxString& item, + const wxString& helpString = wxEmptyString); /** Inserts a separator at the given position. @@ -852,8 +869,8 @@ public: @see Append(), Insert() */ - wxMenuItem* Prepend(int id, const wxString& item = "", - const wxString& helpString = "", + wxMenuItem* Prepend(int id, const wxString& item = wxEmptyString, + const wxString& helpString = wxEmptyString, wxItemKind kind = wxITEM_NORMAL); /** @@ -862,7 +879,7 @@ public: @see Prepend(), AppendCheckItem() */ wxMenuItem* PrependCheckItem(int id, const wxString& item, - const wxString& helpString = ""); + const wxString& helpString = wxEmptyString); /** Inserts a radio item at position 0. @@ -870,7 +887,7 @@ public: @see Prepend(), AppendRadioItem() */ wxMenuItem* PrependRadioItem(int id, const wxString& item, - const wxString& helpString = ""); + const wxString& helpString = wxEmptyString); /** Inserts a separator at position 0. @@ -933,8 +950,9 @@ public: @param title The title to set. - @remarks This is relevant only to popup menus, use - wxMenuBar::SetLabelTop for the menus in the menubar. + @remarks Notice that you can only call this method directly for the + popup menus, to change the title of a menu that is part of a menu + bar you need to use wxMenuBar::SetLabelTop(). @see GetTitle() */