]>
git.saurik.com Git - wxWidgets.git/blob - interface/menu.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxMenuBar
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 A menu bar is a series of menus accessible from the top of a frame.
18 @see wxMenu, @ref overview_eventhandlingoverview
20 class wxMenuBar
: public wxWindow
25 Construct a menu bar from arrays of menus and titles.
30 An array of menus. Do not use this array again - it now belongs to the
33 An array of title strings. Deallocate this array after creating the menu
36 If wxMB_DOCKABLE the menu bar can be detached (wxGTK only).
38 wxMenuBar(long style
= 0);
39 wxMenuBar(size_t n
, wxMenu
* menus
[], const wxString titles
[],
44 Destructor, destroying the menu bar and removing it from the parent frame (if
50 Adds the item to the end of the menu bar.
53 The menu to add. Do not deallocate this menu after calling Append.
55 The title of the menu.
57 @returns @true on success, @false if an error occurred.
61 bool Append(wxMenu
* menu
, const wxString
& title
);
64 Checks or unchecks a menu item.
67 The menu item identifier.
69 If @true, checks the menu item, otherwise the item is unchecked.
71 @remarks Only use this when the menu bar has been associated with a
72 frame; otherwise, use the wxMenu equivalent call.
74 void Check(int id
, const bool check
);
77 Enables or disables (greys out) a menu item.
80 The menu item identifier.
82 @true to enable the item, @false to disable it.
84 @remarks Only use this when the menu bar has been associated with a
85 frame; otherwise, use the wxMenu equivalent call.
87 void Enable(int id
, const bool enable
);
90 Enables or disables a whole menu.
93 The position of the menu, starting from zero.
95 @true to enable the menu, @false to disable it.
97 @remarks Only use this when the menu bar has been associated with a frame.
99 void EnableTop(int pos
, const bool enable
);
102 Finds the menu item object associated with the given menu item identifier.
105 Menu item identifier.
107 If not @NULL, menu will get set to the associated menu.
109 @returns The found menu item object, or @NULL if one was not found.
111 wxMenuItem
* FindItem(int id
, wxMenu menu
= NULL
) const;
114 Returns the index of the menu with the given @a title or @c wxNOT_FOUND if no
115 such menu exists in this menubar. The @a title parameter may specify either
116 the menu title (with accelerator characters, i.e. @c "File") or just the
117 menu label (@c "File") indifferently.
119 int FindMenu(const wxString
& title
) const;
122 Finds the menu item id for a menu name/menu item string pair.
129 @returns The menu item identifier, or wxNOT_FOUND if none was found.
131 @remarks Any special menu codes are stripped out of source and target
132 strings before matching.
134 int FindMenuItem(const wxString
& menuString
,
135 const wxString
& itemString
) const;
138 Gets the help string associated with the menu item identifier.
141 The menu item identifier.
143 @returns The help string, or the empty string if there was no help string
144 or the menu item was not found.
148 wxString
GetHelpString(int id
) const;
151 Gets the label associated with a menu item.
154 The menu item identifier.
156 @returns The menu item label, or the empty string if the item was not
159 @remarks Use only after the menubar has been associated with a frame.
161 wxString
GetLabel(int id
) const;
164 Returns the label of a top-level menu. Note that the returned string does not
165 include the accelerator characters which could have been specified in the menu
166 title string during its construction.
169 Position of the menu on the menu bar, starting from zero.
171 @returns The menu label, or the empty string if the menu was not found.
173 @remarks Use only after the menubar has been associated with a frame.
177 wxString
GetLabelTop(int pos
) const;
180 Returns the menu at @a menuIndex (zero-based).
182 wxMenu
* GetMenu(int menuIndex
) const;
185 Returns the number of menus in this menubar.
187 size_t GetMenuCount() const;
190 Returns the label of a top-level menu. Note that the returned string
191 includes the accelerator characters that have been specified in the menu
192 title string during its construction.
195 Position of the menu on the menu bar, starting from zero.
197 @returns The menu label, or the empty string if the menu was not found.
199 @remarks Use only after the menubar has been associated with a frame.
201 @see GetMenuLabelText(), SetMenuLabel()
203 wxString
GetMenuLabel(int pos
) const;
206 Returns the label of a top-level menu. Note that the returned string does not
207 include any accelerator characters that may have been specified in the menu
208 title string during its construction.
211 Position of the menu on the menu bar, starting from zero.
213 @returns The menu label, or the empty string if the menu was not found.
215 @remarks Use only after the menubar has been associated with a frame.
217 @see GetMenuLabel(), SetMenuLabel()
219 wxString
GetMenuLabelText(int pos
) const;
222 Inserts the menu at the given position into the menu bar. Inserting menu at
223 position 0 will insert it in the very beginning of it, inserting at position
224 GetMenuCount() is the same as calling
228 The position of the new menu in the menu bar
230 The menu to add. wxMenuBar owns the menu and will free it.
232 The title of the menu.
234 @returns @true on success, @false if an error occurred.
238 bool Insert(size_t pos
, wxMenu
* menu
, const wxString
& title
);
241 Determines whether an item is checked.
244 The menu item identifier.
246 @returns @true if the item was found and is checked, @false otherwise.
248 bool IsChecked(int id
) const;
251 Determines whether an item is enabled.
254 The menu item identifier.
256 @returns @true if the item was found and is enabled, @false otherwise.
258 bool IsEnabled(int id
) const;
266 Removes the menu from the menu bar and returns the menu object - the caller is
267 responsible for deleting it. This function may be used together with
268 Insert() to change the menubar
273 wxMenu
* Remove(size_t pos
);
276 Replaces the menu at the given position with another one.
279 The position of the new menu in the menu bar
283 The title of the menu.
285 @returns The menu which was previously at position pos. The caller is
286 responsible for deleting it.
288 @see Insert(), Remove()
290 wxMenu
* Replace(size_t pos
, wxMenu
* menu
, const wxString
& title
);
293 Sets the help string associated with a menu item.
296 Menu item identifier.
298 Help string to associate with the menu item.
302 void SetHelpString(int id
, const wxString
& helpString
);
305 Sets the label of a menu item.
308 Menu item identifier.
312 @remarks Use only after the menubar has been associated with a frame.
316 void SetLabel(int id
, const wxString
& label
);
319 Sets the label of a top-level menu.
322 The position of a menu on the menu bar, starting from zero.
326 @remarks Use only after the menubar has been associated with a frame.
330 void SetLabelTop(int pos
, const wxString
& label
);
333 Sets the label of a top-level menu.
336 The position of a menu on the menu bar, starting from zero.
340 @remarks Use only after the menubar has been associated with a frame.
342 void SetMenuLabel(int pos
, const wxString
& label
);
351 A menu is a popup (or pull down) list of items, one of which may be
352 selected before the menu goes away (clicking elsewhere dismisses the
353 menu). Menus may be used to construct either menu bars or popup menus.
355 A menu item has an integer ID associated with it which can be used to
356 identify the selection, or to change the menu item in some way. A menu item
357 with a special identifier -1 is a separator item and doesn't have an
358 associated command but just makes a separator line appear in the menu.
360 @note Please note that @e wxID_ABOUT and @e wxID_EXIT are
361 predefined by wxWidgets and have a special meaning since entries
362 using these IDs will be taken out of the normal menus under MacOS X
363 and will be inserted into the system menu (following the appropriate
364 MacOS X interface guideline). On PalmOS @e wxID_EXIT is disabled according
365 to Palm OS Companion guidelines.
367 Menu items may be either normal items, check items or radio items. Normal items
368 don't have any special properties while the check items have a boolean flag
369 associated to them and they show a checkmark in the menu when the flag is set.
370 wxWidgets automatically toggles the flag value when the item is clicked and its
371 value may be retrieved using either wxMenu::IsChecked method
372 of wxMenu or wxMenuBar itself or by using
373 wxEvent::IsChecked when you get the menu
374 notification for the item in question.
376 The radio items are similar to the check items except that all the other items
377 in the same radio group are unchecked when a radio item is checked. The radio
378 group is formed by a contiguous range of radio items, i.e. it starts at the
379 first item of this kind and ends with the first item of a different kind (or
380 the end of the menu). Notice that because the radio groups are defined in terms
381 of the item positions inserting or removing the items in the menu containing
382 the radio items risks to not work correctly. Finally note that radio items
383 are not supported under Motif.
388 @see wxMenuBar, wxWindow::PopupMenu, @ref overview_eventhandlingoverview, @ref
389 overview_wxfilehistory "wxFileHistory (most recently used files menu)"
391 class wxMenu
: public wxEvtHandler
396 Constructs a wxMenu object.
399 If set to wxMENU_TEAROFF, the menu will be detachable (wxGTK only).
401 wxMenu(const wxString
& title
= "", long style
= 0);
406 Destructor, destroying the menu.
407 Note: under Motif, a popup menu must have a valid parent (the window
408 it was last popped up on) when being destroyed. Therefore, make sure
409 you delete or re-use the popup menu @e before destroying the
410 parent window. Re-use in this context means popping up the menu on
411 a different window from last time, which causes an implicit destruction
412 and recreation of internal data structures.
418 Adds a menu item object. This is the most generic variant of Append() method
419 because it may be used for both items (including separators) and submenus and
420 because you can also specify various extra properties of a menu item this way,
421 such as bitmaps and fonts.
424 The menu command identifier.
426 The string to appear on the menu item.
430 May be wxITEM_SEPARATOR, wxITEM_NORMAL,
431 wxITEM_CHECK or wxITEM_RADIO
433 An optional help string associated with the item.
434 By default, the handler for the wxEVT_MENU_HIGHLIGHT event displays
435 this string in the status line.
437 A menuitem object. It will be owned by the wxMenu object after this function
438 is called, so do not delete it yourself.
440 @remarks This command can be used after the menu has been shown, as well
441 as on initial creation of a menu or menubar.
443 @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
444 AppendSubMenu(), Insert(), SetLabel(),
445 GetHelpString(), SetHelpString(), wxMenuItem
447 wxMenuItem
* Append(int id
, const wxString
& item
= "",
448 const wxString
& helpString
= "",
449 wxItemKind kind
= wxITEM_NORMAL
);
450 wxMenuItem
* Append(int id
, const wxString
& item
,
452 const wxString
& helpString
= "");
453 wxMenuItem
* Append(wxMenuItem
* menuItem
);
457 Adds a checkable item to the end of the menu.
459 @see Append(), InsertCheckItem()
461 wxMenuItem
* AppendCheckItem(int id
, const wxString
& item
,
462 const wxString
& helpString
= "");
465 Adds a radio item to the end of the menu. All consequent radio items form a
466 group and when an item in the group is checked, all the others are
467 automatically unchecked.
469 @see Append(), InsertRadioItem()
471 wxMenuItem
* AppendRadioItem(int id
, const wxString
& item
,
472 const wxString
& helpString
= "");
475 Adds a separator to the end of the menu.
477 @see Append(), InsertSeparator()
479 wxMenuItem
* AppendSeparator();
482 Adds the given @a submenu to this menu. @a text is the text shown in the
483 menu for it and @a help is the help string shown in the status bar when the
484 submenu item is selected.
486 wxMenuItem
* AppendSubMenu(wxMenu
* submenu
, const wxString
& text
,
487 const wxString
& help
= wxEmptyString
);
490 Inserts a break in a menu, causing the next appended item to appear in a new
496 Checks or unchecks the menu item.
499 The menu item identifier.
501 If @true, the item will be checked, otherwise it will be unchecked.
505 void Check(int id
, const bool check
);
509 Deletes the menu item from the menu. If the item is a submenu, it will
510 @b not be deleted. Use Destroy() if you want to
514 Id of the menu item to be deleted.
516 Menu item to be deleted.
518 @see FindItem(), Destroy(), Remove()
521 void Delete(wxMenuItem
* item
);
526 Deletes the menu item from the menu. If the item is a submenu, it will
527 be deleted. Use Remove() if you want to keep the submenu
528 (for example, to reuse it later).
531 Id of the menu item to be deleted.
533 Menu item to be deleted.
535 @see FindItem(), Deletes(), Remove()
537 void Destroy(int id
);
538 void Destroy(wxMenuItem
* item
);
542 Enables or disables (greys out) a menu item.
545 The menu item identifier.
547 @true to enable the menu item, @false to disable it.
551 void Enable(int id
, const bool enable
);
555 Finds the menu item object associated with the given menu item identifier and,
556 optionally, the (sub)menu it belongs to.
559 Menu item string to find.
561 Menu item identifier.
563 If the pointer is not @NULL, it will be filled with the item's
564 parent menu (if the item was found)
566 @returns First form: menu item identifier, or wxNOT_FOUND if none is
569 @remarks Any special menu codes are stripped out of source and target
570 strings before matching.
572 int FindItem(const wxString
& itemString
) const;
573 const wxMenuItem
* FindItem(int id
, wxMenu
** menu
= NULL
) const;
577 Returns the wxMenuItem given a position in the menu.
579 wxMenuItem
* FindItemByPosition(size_t position
) const;
582 Returns the help string associated with a menu item.
585 The menu item identifier.
587 @returns The help string, or the empty string if there is no help string
588 or the item was not found.
590 @see SetHelpString(), Append()
592 wxString
GetHelpString(int id
) const;
595 Returns a menu item label.
598 The menu item identifier.
600 @returns The item label, or the empty string if the item was not found.
602 @see GetLabelText(), SetLabel()
604 wxString
GetLabel(int id
) const;
607 Returns a menu item label, without any of the original mnemonics and
611 The menu item identifier.
613 @returns The item label, or the empty string if the item was not found.
615 @see GetLabel(), SetLabel()
617 wxString
GetLabelText(int id
) const;
620 Returns the number of items in the menu.
622 size_t GetMenuItemCount() const;
625 Returns the list of items in the menu. wxMenuItemList is a pseudo-template
626 list class containing wxMenuItem pointers, see wxList.
628 wxMenuItemList
GetMenuItems() const;
631 Returns the title of the menu.
633 @remarks This is relevant only to popup menus, use
634 wxMenuBar::GetMenuLabel for the menus in the menubar.
638 wxString
GetTitle() const;
642 Inserts the given @a item before the position @e pos. Inserting the item
643 at position GetMenuItemCount() is the same
646 @see Append(), Prepend()
648 wxMenuItem
* Insert(size_t pos
, wxMenuItem
* item
);
649 wxMenuItem
* Insert(size_t pos
, int id
,
650 const wxString
& item
= "",
651 const wxString
& helpString
= "",
652 wxItemKind kind
= wxITEM_NORMAL
);
656 Inserts a checkable item at the given position.
658 @see Insert(), AppendCheckItem()
660 wxMenuItem
* InsertCheckItem(size_t pos
, int id
,
661 const wxString
& item
,
662 const wxString
& helpString
= "");
665 Inserts a radio item at the given position.
667 @see Insert(), AppendRadioItem()
669 wxMenuItem
* InsertRadioItem(size_t pos
, int id
,
670 const wxString
& item
,
671 const wxString
& helpString
= "");
674 Inserts a separator at the given position.
676 @see Insert(), AppendSeparator()
678 wxMenuItem
* InsertSeparator(size_t pos
);
681 Determines whether a menu item is checked.
684 The menu item identifier.
686 @returns @true if the menu item is checked, @false otherwise.
690 bool IsChecked(int id
) const;
693 Determines whether a menu item is enabled.
696 The menu item identifier.
698 @returns @true if the menu item is enabled, @false otherwise.
702 bool IsEnabled(int id
) const;
706 Inserts the given @a item at position 0, i.e. before all the other
709 @see Append(), Insert()
711 wxMenuItem
* Prepend(wxMenuItem
* item
);
712 wxMenuItem
* Prepend(int id
, const wxString
& item
= "",
713 const wxString
& helpString
= "",
714 wxItemKind kind
= wxITEM_NORMAL
);
718 Inserts a checkable item at position 0.
720 @see Prepend(), AppendCheckItem()
722 wxMenuItem
* PrependCheckItem(int id
, const wxString
& item
,
723 const wxString
& helpString
= "");
726 Inserts a radio item at position 0.
728 @see Prepend(), AppendRadioItem()
730 wxMenuItem
* PrependRadioItem(int id
, const wxString
& item
,
731 const wxString
& helpString
= "");
734 Inserts a separator at position 0.
736 @see Prepend(), AppendSeparator()
738 wxMenuItem
* PrependSeparator();
742 Removes the menu item from the menu but doesn't delete the associated C++
743 object. This allows to reuse the same item later by adding it back to the menu
744 (especially useful with submenus).
747 The identifier of the menu item to remove.
749 The menu item to remove.
751 @returns The item which was detached from the menu.
753 wxMenuItem
* Remove(int id
);
754 wxMenuItem
* Remove(wxMenuItem
* item
);
758 Sets an item's help string.
761 The menu item identifier.
763 The help string to set.
767 void SetHelpString(int id
, const wxString
& helpString
);
770 Sets the label of a menu item.
773 The menu item identifier.
775 The menu item label to set.
777 @see Append(), GetLabel()
779 void SetLabel(int id
, const wxString
& label
);
782 Sets the title of the menu.
787 @remarks This is relevant only to popup menus, use
788 wxMenuBar::SetLabelTop for the menus in the menubar.
792 void SetTitle(const wxString
& title
);
795 Sends events to @a source (or owning window if @NULL) to update the
796 menu UI. This is called just before the menu is popped up with
797 wxWindow::PopupMenu, but
798 the application may call it at other times if required.
800 void UpdateUI(wxEvtHandler
* source
= NULL
) const;