A menu is a popup (or pull down) list of items, one of which may be selected before the menu goes away (clicking elsewhere dismisses the menu).
Menus may be used to construct either menu bars or popup menus.
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 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 wxID_ABOUT and wxID_EXIT are predefined by wxWidgets and have a special meaning since entries using these IDs will be taken out of the normal menus under OS X and will be inserted into the system menu (following the appropriate OS X interface guideline).
Menu items may be either normal items, check items or 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. wxWidgets automatically toggles the flag value when the item is clicked and its value may be retrieved using either wxMenu::IsChecked method of wxMenu or wxMenuBar itself or by using wxEvent::IsChecked when you get the menu notification for the item in question.
The radio items are similar to the check items except that all the other items in the same radio group are unchecked when a radio item is checked. The radio group is formed by a contiguous range of radio items, i.e. it starts at the 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.
Allocation strategy
All menus must be created on the 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.
Event handling
If the menu is part of a menubar, then wxMenuBar event processing is used.
With a popup menu (see wxWindow::PopupMenu), there is a variety of ways to handle a menu selection event (wxEVT_MENU
):
- Provide
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
EVT_MENU
macro;
- Set a new event handler for wxMenu, through wxEvtHandler::SetNextHandler, specifying an object whose class has
EVT_MENU
entries;
Note that instead of static EVT_MENU
macros you can also use dynamic connection; see Dynamic Event Handling.
- See Also
- wxMenuBar, wxWindow::PopupMenu, Events and Event Handling, wxFileHistory (most recently used files menu)
|
| wxMenu () |
| Constructs a wxMenu object.
|
|
| wxMenu (long style) |
| Constructs a wxMenu object.
|
|
| wxMenu (const wxString &title, long style=0) |
| Constructs a wxMenu object with a title.
|
|
virtual | ~wxMenu () |
| Destructor, destroying the menu.
|
|
wxMenuItem * | Append (int id, const wxString &item=wxEmptyString, const wxString &helpString=wxEmptyString, wxItemKind kind=wxITEM_NORMAL) |
| Adds a menu item.
|
|
wxMenuItem * | Append (int id, const wxString &item, wxMenu *subMenu, const wxString &helpString=wxEmptyString) |
| Adds a submenu.
|
|
wxMenuItem * | Append (wxMenuItem *menuItem) |
| Adds a menu item object.
|
|
wxMenuItem * | AppendCheckItem (int id, const wxString &item, const wxString &help=wxEmptyString) |
| Adds a checkable item to the end of the menu.
|
|
wxMenuItem * | AppendRadioItem (int id, const wxString &item, const wxString &help=wxEmptyString) |
| Adds a radio item to the end of the menu.
|
|
wxMenuItem * | AppendSeparator () |
| Adds a separator to the end of the menu.
|
|
wxMenuItem * | AppendSubMenu (wxMenu *submenu, const wxString &text, const wxString &help=wxEmptyString) |
| Adds the given submenu to this menu.
|
|
virtual void | Break () |
| Inserts a break in a menu, causing the next appended item to appear in a new column.
|
|
void | Check (int id, bool check) |
| Checks or unchecks the menu item.
|
|
bool | Delete (int id) |
| Deletes the menu item from the menu.
|
|
bool | Delete (wxMenuItem *item) |
| Deletes the menu item from the menu.
|
|
bool | Destroy (int id) |
| Deletes the menu item from the menu.
|
|
bool | Destroy (wxMenuItem *item) |
| Deletes the menu item from the menu.
|
|
void | Enable (int id, bool enable) |
| Enables or disables (greys out) a menu item.
|
|
wxMenuItem * | FindChildItem (int id, size_t *pos=NULL) const |
| Finds the menu item object associated with the given menu item identifier and, optionally, the position of the item in the menu.
|
|
virtual int | FindItem (const wxString &itemString) const |
| Finds the menu id for a menu item string.
|
|
wxMenuItem * | FindItem (int id, wxMenu **menu=NULL) const |
| Finds the menu item object associated with the given menu item identifier and, optionally, the (sub)menu it belongs to.
|
|
wxMenuItem * | FindItemByPosition (size_t position) const |
| Returns the wxMenuItem given a position in the menu.
|
|
virtual wxString | GetHelpString (int id) const |
| Returns the help string associated with a menu item.
|
|
wxString | GetLabel (int id) const |
| Returns a menu item label.
|
|
wxString | GetLabelText (int id) const |
| Returns a menu item label, without any of the original mnemonics and accelerators.
|
|
size_t | GetMenuItemCount () const |
| Returns the number of items in the menu.
|
|
const wxString & | GetTitle () const |
| Returns the title of the menu.
|
|
wxMenuItem * | Insert (size_t pos, wxMenuItem *menuItem) |
| Inserts the given item before the position pos.
|
|
wxMenuItem * | Insert (size_t pos, int id, const wxString &item=wxEmptyString, const wxString &helpString=wxEmptyString, wxItemKind kind=wxITEM_NORMAL) |
| Inserts the given item before the position pos.
|
|
wxMenuItem * | Insert (size_t pos, int id, const wxString &text, wxMenu *submenu, const wxString &help=wxEmptyString) |
| Inserts the given submenu before the position pos.
|
|
wxMenuItem * | InsertCheckItem (size_t pos, int id, const wxString &item, const wxString &helpString=wxEmptyString) |
| Inserts a checkable item at the given position.
|
|
wxMenuItem * | InsertRadioItem (size_t pos, int id, const wxString &item, const wxString &helpString=wxEmptyString) |
| Inserts a radio item at the given position.
|
|
wxMenuItem * | InsertSeparator (size_t pos) |
| Inserts a separator at the given position.
|
|
bool | IsChecked (int id) const |
| Determines whether a menu item is checked.
|
|
bool | IsEnabled (int id) const |
| Determines whether a menu item is enabled.
|
|
wxMenuItem * | Prepend (wxMenuItem *item) |
| Inserts the given item at position 0, i.e. before all the other existing items.
|
|
wxMenuItem * | Prepend (int id, const wxString &item=wxEmptyString, const wxString &helpString=wxEmptyString, wxItemKind kind=wxITEM_NORMAL) |
| Inserts the given item at position 0, i.e. before all the other existing items.
|
|
wxMenuItem * | Prepend (int id, const wxString &text, wxMenu *submenu, const wxString &help=wxEmptyString) |
| Inserts the given submenu at position 0.
|
|
wxMenuItem * | PrependCheckItem (int id, const wxString &item, const wxString &helpString=wxEmptyString) |
| Inserts a checkable item at position 0.
|
|
wxMenuItem * | PrependRadioItem (int id, const wxString &item, const wxString &helpString=wxEmptyString) |
| Inserts a radio item at position 0.
|
|
wxMenuItem * | PrependSeparator () |
| Inserts a separator at position 0.
|
|
wxMenuItem * | Remove (int id) |
| Removes the menu item from the menu but doesn't delete the associated C++ object.
|
|
wxMenuItem * | Remove (wxMenuItem *item) |
| Removes the menu item from the menu but doesn't delete the associated C++ object.
|
|
virtual void | SetHelpString (int id, const wxString &helpString) |
| Sets an item's help string.
|
|
void | SetLabel (int id, const wxString &label) |
| Sets the label of a menu item.
|
|
virtual void | SetTitle (const wxString &title) |
| Sets the title of the menu.
|
|
void | UpdateUI (wxEvtHandler *source=NULL) |
| Sends events to source (or owning window if NULL) to update the menu UI.
|
|
void | SetInvokingWindow (wxWindow *win) |
|
wxWindow * | GetInvokingWindow () const |
|
wxWindow * | GetWindow () const |
|
long | GetStyle () const |
|
void | SetParent (wxMenu *parent) |
|
wxMenu * | GetParent () const |
|
virtual void | Attach (wxMenuBar *menubar) |
|
virtual void | Detach () |
|
bool | IsAttached () const |
|
|
wxMenuItemList & | GetMenuItems () |
| Returns the list of items in the menu.
|
|
const wxMenuItemList & | GetMenuItems () const |
| Returns the list of items in the menu.
|
|
Public Member Functions inherited from wxEvtHandler |
| wxEvtHandler () |
| Constructor.
|
|
virtual | ~wxEvtHandler () |
| Destructor.
|
|
virtual void | QueueEvent (wxEvent *event) |
| Queue event for a later processing.
|
|
virtual void | AddPendingEvent (const wxEvent &event) |
| Post an event to be processed later.
|
|
template<typename T , typename T1 , ... > |
void | CallAfter (void(T::*method)(T1,...), T1 x1,...) |
| Asynchronously call the given method.
|
|
template<typename T > |
void | CallAfter (const T &functor) |
| Asynchronously call the given functor.
|
|
virtual bool | ProcessEvent (wxEvent &event) |
| Processes an event, searching event tables and calling zero or more suitable event handler function(s).
|
|
bool | ProcessEventLocally (wxEvent &event) |
| Try to process the event in this handler and all those chained to it.
|
|
bool | SafelyProcessEvent (wxEvent &event) |
| Processes an event by calling ProcessEvent() and handles any exceptions that occur in the process.
|
|
void | ProcessPendingEvents () |
| Processes the pending events previously queued using QueueEvent() or AddPendingEvent(); you must call this function only if you are sure there are pending events for this handler, otherwise a wxCHECK will fail.
|
|
void | DeletePendingEvents () |
| Deletes all events queued on this event handler using QueueEvent() or AddPendingEvent().
|
|
virtual bool | SearchEventTable (wxEventTable &table, wxEvent &event) |
| Searches the event table, executing an event handler function if an appropriate one is found.
|
|
void | Connect (int id, int lastId, wxEventType eventType, wxObjectEventFunction function, wxObject *userData=NULL, wxEvtHandler *eventSink=NULL) |
| Connects the given function dynamically with the event handler, id and event type.
|
|
void | Connect (int id, wxEventType eventType, wxObjectEventFunction function, wxObject *userData=NULL, wxEvtHandler *eventSink=NULL) |
| See the Connect(int, int, wxEventType, wxObjectEventFunction, wxObject*, wxEvtHandler*) overload for more info.
|
|
void | Connect (wxEventType eventType, wxObjectEventFunction function, wxObject *userData=NULL, wxEvtHandler *eventSink=NULL) |
| See the Connect(int, int, wxEventType, wxObjectEventFunction, wxObject*, wxEvtHandler*) overload for more info.
|
|
bool | Disconnect (wxEventType eventType, wxObjectEventFunction function, wxObject *userData=NULL, wxEvtHandler *eventSink=NULL) |
| Disconnects the given function dynamically from the event handler, using the specified parameters as search criteria and returning true if a matching function has been found and removed.
|
|
bool | Disconnect (int id=wxID_ANY, wxEventType eventType=wxEVT_NULL, wxObjectEventFunction function=NULL, wxObject *userData=NULL, wxEvtHandler *eventSink=NULL) |
| See the Disconnect(wxEventType, wxObjectEventFunction, wxObject*, wxEvtHandler*) overload for more info.
|
|
bool | Disconnect (int id, int lastId, wxEventType eventType, wxObjectEventFunction function=NULL, wxObject *userData=NULL, wxEvtHandler *eventSink=NULL) |
| See the Disconnect(wxEventType, wxObjectEventFunction, wxObject*, wxEvtHandler*) overload for more info.
|
|
template<typename EventTag , typename Functor > |
void | Bind (const EventTag &eventType, Functor functor, int id=wxID_ANY, int lastId=wxID_ANY, wxObject *userData=NULL) |
| Binds the given function, functor or method dynamically with the event.
|
|
template<typename EventTag , typename Class , typename EventArg , typename EventHandler > |
void | Bind (const EventTag &eventType, void(Class::*method)(EventArg &), EventHandler *handler, int id=wxID_ANY, int lastId=wxID_ANY, wxObject *userData=NULL) |
| See the Bind<>(const EventTag&, Functor, int, int, wxObject*) overload for more info.
|
|
template<typename EventTag , typename Functor > |
bool | Unbind (const EventTag &eventType, Functor functor, int id=wxID_ANY, int lastId=wxID_ANY, wxObject *userData=NULL) |
| Unbinds the given function, functor or method dynamically from the event handler, using the specified parameters as search criteria and returning true if a matching function has been found and removed.
|
|
template<typename EventTag , typename Class , typename EventArg , typename EventHandler > |
bool | Unbind (const EventTag &eventType, void(Class::*method)(EventArg &), EventHandler *handler, int id=wxID_ANY, int lastId=wxID_ANY, wxObject *userData=NULL) |
| See the Unbind<>(const EventTag&, Functor, int, int, wxObject*) overload for more info.
|
|
void * | GetClientData () const |
| Returns user-supplied client data.
|
|
wxClientData * | GetClientObject () const |
| Returns a pointer to the user-supplied client data object.
|
|
void | SetClientData (void *data) |
| Sets user-supplied client data.
|
|
void | SetClientObject (wxClientData *data) |
| Set the client data object.
|
|
bool | GetEvtHandlerEnabled () const |
| Returns true if the event handler is enabled, false otherwise.
|
|
wxEvtHandler * | GetNextHandler () const |
| Returns the pointer to the next handler in the chain.
|
|
wxEvtHandler * | GetPreviousHandler () const |
| Returns the pointer to the previous handler in the chain.
|
|
void | SetEvtHandlerEnabled (bool enabled) |
| Enables or disables the event handler.
|
|
virtual void | SetNextHandler (wxEvtHandler *handler) |
| Sets the pointer to the next handler.
|
|
virtual void | SetPreviousHandler (wxEvtHandler *handler) |
| Sets the pointer to the previous handler.
|
|
void | Unlink () |
| Unlinks this event handler from the chain it's part of (if any); then links the "previous" event handler to the "next" one (so that the chain won't be interrupted).
|
|
bool | IsUnlinked () const |
| Returns true if the next and the previous handler pointers of this event handler instance are NULL.
|
|
| wxObject () |
| Default ctor; initializes to NULL the internal reference data.
|
|
| wxObject (const wxObject &other) |
| Copy ctor.
|
|
virtual | ~wxObject () |
| Destructor.
|
|
virtual wxClassInfo * | GetClassInfo () const |
| This virtual function is redefined for every class that requires run-time type information, when using the wxDECLARE_CLASS macro (or similar).
|
|
wxObjectRefData * | GetRefData () const |
| Returns the wxObject::m_refData pointer, i.e. the data referenced by this object.
|
|
bool | IsKindOf (const wxClassInfo *info) const |
| Determines whether this class is a subclass of (or the same class as) the given class.
|
|
bool | IsSameAs (const wxObject &obj) const |
| Returns true if this object has the same data pointer as obj.
|
|
void | Ref (const wxObject &clone) |
| Makes this object refer to the data in clone.
|
|
void | SetRefData (wxObjectRefData *data) |
| Sets the wxObject::m_refData pointer.
|
|
void | UnRef () |
| Decrements the reference count in the associated data, and if it is zero, deletes the data.
|
|
void | UnShare () |
| This is the same of AllocExclusive() but this method is public.
|
|
void | operator delete (void *buf) |
| The delete operator is defined for debugging versions of the library only, when the identifier WXDEBUG is defined.
|
|
void * | operator new (size_t size, const wxString &filename=NULL, int lineNum=0) |
| The new operator is defined for debugging versions of the library only, when the identifier WXDEBUG is defined.
|
|