]> git.saurik.com Git - wxWidgets.git/blob - interface/wx/menu.h
use standard tags @class,,@lib,@category for wxScopeGuard, too (to keep the documenta...
[wxWidgets.git] / interface / wx / menu.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: menu.h
3 // Purpose: interface of wxMenuBar
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 @class wxMenuBar
11
12 A menu bar is a series of menus accessible from the top of a frame.
13
14 @library{wxcore}
15 @category{menus}
16
17 @see wxMenu, @ref overview_eventhandling "Event Handling Overview"
18 */
19 class wxMenuBar : public wxWindow
20 {
21 public:
22 /**
23 Construct an empty menu barM
24
25 @param style
26 If wxMB_DOCKABLE the menu bar can be detached (wxGTK only).
27 */
28 wxMenuBar(long style = 0);
29
30 /**
31 Construct a menu bar from arrays of menus and titles.
32
33 @param n
34 The number of menus.
35 @param menus
36 An array of menus. Do not use this array again - it now belongs to
37 the menu bar.
38 @param titles
39 An array of title strings. Deallocate this array after creating
40 the menu bar.
41 @param style
42 If wxMB_DOCKABLE the menu bar can be detached (wxGTK only).
43 */
44 wxMenuBar(size_t n, wxMenu* menus[], const wxString titles[],
45 long style = 0);
46
47 /**
48 Destructor, destroying the menu bar and removing it from the parent frame (if
49 any).
50 */
51 virtual ~wxMenuBar();
52
53 /**
54 Adds the item to the end of the menu bar.
55
56 @param menu
57 The menu to add. Do not deallocate this menu after calling Append.
58 @param title
59 The title of the menu.
60
61 @return @true on success, @false if an error occurred.
62
63 @see Insert()
64 */
65 virtual bool Append(wxMenu* menu, const wxString& title);
66
67 /**
68 Checks or unchecks a menu item.
69
70 @param id
71 The menu item identifier.
72 @param check
73 If @true, checks the menu item, otherwise the item is unchecked.
74
75 @remarks Only use this when the menu bar has been associated with a
76 frame; otherwise, use the wxMenu equivalent call.
77 */
78 void Check(int id, const bool check);
79
80 /**
81 Enables or disables (greys out) a menu item.
82
83 @param id
84 The menu item identifier.
85 @param enable
86 @true to enable the item, @false to disable it.
87
88 @remarks Only use this when the menu bar has been associated with a
89 frame; otherwise, use the wxMenu equivalent call.
90 */
91 void Enable(int id, const bool enable);
92
93 /**
94 Enables or disables a whole menu.
95
96 @param pos
97 The position of the menu, starting from zero.
98 @param enable
99 @true to enable the menu, @false to disable it.
100
101 @remarks Only use this when the menu bar has been associated with a frame.
102 */
103 void EnableTop(int pos, const bool enable);
104
105 /**
106 Finds the menu item object associated with the given menu item identifier.
107
108 @param id
109 Menu item identifier.
110 @param menu
111 If not @NULL, menu will get set to the associated menu.
112
113 @return The found menu item object, or @NULL if one was not found.
114 */
115 wxMenuItem* FindItem(int id, wxMenu menu = NULL) const;
116
117 /**
118 Returns the index of the menu with the given @a title or @c wxNOT_FOUND if no
119 such menu exists in this menubar. The @a title parameter may specify either
120 the menu title (with accelerator characters, i.e. @c "File") or just the
121 menu label (@c "File") indifferently.
122 */
123 int FindMenu(const wxString& title) const;
124
125 /**
126 Finds the menu item id for a menu name/menu item string pair.
127
128 @param menuString
129 Menu title to find.
130 @param itemString
131 Item to find.
132
133 @return The menu item identifier, or wxNOT_FOUND if none was found.
134
135 @remarks Any special menu codes are stripped out of source and target
136 strings before matching.
137 */
138 int FindMenuItem(const wxString& menuString,
139 const wxString& itemString) const;
140
141 /**
142 Gets the help string associated with the menu item identifier.
143
144 @param id
145 The menu item identifier.
146
147 @return The help string, or the empty string if there was no help string
148 or the menu item was not found.
149
150 @see SetHelpString()
151 */
152 wxString GetHelpString(int id) const;
153
154 /**
155 Gets the label associated with a menu item.
156
157 @param id
158 The menu item identifier.
159
160 @return The menu item label, or the empty string if the item was not
161 found.
162
163 @remarks Use only after the menubar has been associated with a frame.
164 */
165 wxString GetLabel(int id) const;
166
167 /**
168 Returns the label of a top-level menu. Note that the returned string does not
169 include the accelerator characters which could have been specified in the menu
170 title string during its construction.
171
172 @param pos
173 Position of the menu on the menu bar, starting from zero.
174
175 @return The menu label, or the empty string if the menu was not found.
176
177 @remarks Use only after the menubar has been associated with a frame.
178
179 @see SetLabelTop()
180 */
181 wxString GetLabelTop(int pos) const;
182
183 /**
184 Returns the menu at @a menuIndex (zero-based).
185 */
186 wxMenu* GetMenu(int menuIndex) const;
187
188 /**
189 Returns the number of menus in this menubar.
190 */
191 size_t GetMenuCount() const;
192
193 /**
194 Returns the label of a top-level menu. Note that the returned string
195 includes the accelerator characters that have been specified in the menu
196 title string during its construction.
197
198 @param pos
199 Position of the menu on the menu bar, starting from zero.
200
201 @return The menu label, or the empty string if the menu was not found.
202
203 @remarks Use only after the menubar has been associated with a frame.
204
205 @see GetMenuLabelText(), SetMenuLabel()
206 */
207 wxString GetMenuLabel(int pos) const;
208
209 /**
210 Returns the label of a top-level menu. Note that the returned string does not
211 include any accelerator characters that may have been specified in the menu
212 title string during its construction.
213
214 @param pos
215 Position of the menu on the menu bar, starting from zero.
216
217 @return The menu label, or the empty string if the menu was not found.
218
219 @remarks Use only after the menubar has been associated with a frame.
220
221 @see GetMenuLabel(), SetMenuLabel()
222 */
223 wxString GetMenuLabelText(int pos) const;
224
225 /**
226 Inserts the menu at the given position into the menu bar. Inserting menu at
227 position 0 will insert it in the very beginning of it, inserting at position
228 GetMenuCount() is the same as calling
229 Append().
230
231 @param pos
232 The position of the new menu in the menu bar
233 @param menu
234 The menu to add. wxMenuBar owns the menu and will free it.
235 @param title
236 The title of the menu.
237
238 @return @true on success, @false if an error occurred.
239
240 @see Append()
241 */
242 virtual bool Insert(size_t pos, wxMenu* menu, const wxString& title);
243
244 /**
245 Determines whether an item is checked.
246
247 @param id
248 The menu item identifier.
249
250 @return @true if the item was found and is checked, @false otherwise.
251 */
252 bool IsChecked(int id) const;
253
254 /**
255 Determines whether an item is enabled.
256
257 @param id
258 The menu item identifier.
259
260 @return @true if the item was found and is enabled, @false otherwise.
261 */
262 bool IsEnabled(int id) const;
263
264 /**
265 Redraw the menu bar
266 */
267 void Refresh();
268
269 /**
270 Removes the menu from the menu bar and returns the menu object - the caller is
271 responsible for deleting it. This function may be used together with
272 Insert() to change the menubar
273 dynamically.
274
275 @see Replace()
276 */
277 virtual wxMenu* Remove(size_t pos);
278
279 /**
280 Replaces the menu at the given position with another one.
281
282 @param pos
283 The position of the new menu in the menu bar
284 @param menu
285 The menu to add.
286 @param title
287 The title of the menu.
288
289 @return The menu which was previously at position pos. The caller is
290 responsible for deleting it.
291
292 @see Insert(), Remove()
293 */
294 virtual wxMenu* Replace(size_t pos, wxMenu* menu, const wxString& title);
295
296 /**
297 Sets the help string associated with a menu item.
298
299 @param id
300 Menu item identifier.
301 @param helpString
302 Help string to associate with the menu item.
303
304 @see GetHelpString()
305 */
306 void SetHelpString(int id, const wxString& helpString);
307
308 /**
309 Sets the label of a menu item.
310
311 @param id
312 Menu item identifier.
313 @param label
314 Menu item label.
315
316 @remarks Use only after the menubar has been associated with a frame.
317
318 @see GetLabel()
319 */
320 void SetLabel(int id, const wxString& label);
321
322 /**
323 Sets the label of a top-level menu.
324
325 @param pos
326 The position of a menu on the menu bar, starting from zero.
327 @param label
328 The menu label.
329
330 @remarks Use only after the menubar has been associated with a frame.
331
332 @see GetLabelTop()
333 */
334 void SetLabelTop(int pos, const wxString& label);
335
336 /**
337 Sets the label of a top-level menu.
338
339 @param pos
340 The position of a menu on the menu bar, starting from zero.
341 @param label
342 The menu label.
343
344 @remarks Use only after the menubar has been associated with a frame.
345 */
346 void SetMenuLabel(int pos, const wxString& label);
347 };
348
349
350
351 /**
352 @class wxMenu
353
354 A menu is a popup (or pull down) list of items, one of which may be
355 selected before the menu goes away (clicking elsewhere dismisses the
356 menu). Menus may be used to construct either menu bars or popup menus.
357
358 A menu item has an integer ID associated with it which can be used to
359 identify the selection, or to change the menu item in some way. A menu item
360 with a special identifier -1 is a separator item and doesn't have an
361 associated command but just makes a separator line appear in the menu.
362
363 @note Please note that @e wxID_ABOUT and @e wxID_EXIT are
364 predefined by wxWidgets and have a special meaning since entries
365 using these IDs will be taken out of the normal menus under MacOS X
366 and will be inserted into the system menu (following the appropriate
367 MacOS X interface guideline). On PalmOS @e wxID_EXIT is disabled according
368 to Palm OS Companion guidelines.
369
370 Menu items may be either normal items, check items or radio items. Normal items
371 don't have any special properties while the check items have a boolean flag
372 associated to them and they show a checkmark in the menu when the flag is set.
373 wxWidgets automatically toggles the flag value when the item is clicked and its
374 value may be retrieved using either wxMenu::IsChecked method
375 of wxMenu or wxMenuBar itself or by using
376 wxEvent::IsChecked when you get the menu
377 notification for the item in question.
378
379 The radio items are similar to the check items except that all the other items
380 in the same radio group are unchecked when a radio item is checked. The radio
381 group is formed by a contiguous range of radio items, i.e. it starts at the
382 first item of this kind and ends with the first item of a different kind (or
383 the end of the menu). Notice that because the radio groups are defined in terms
384 of the item positions inserting or removing the items in the menu containing
385 the radio items risks to not work correctly. Finally note that radio items
386 are not supported under Motif.
387
388 @library{wxcore}
389 @category{menus}
390
391 @see wxMenuBar, wxWindow::PopupMenu,
392 @ref overview_eventhandling "Event Handling Overview",
393 @ref wxFileHistory "wxFileHistory (most recently used files menu)"
394 */
395 class wxMenu : public wxEvtHandler
396 {
397 public:
398 /**
399 Constructs a wxMenu object.
400
401 @param style
402 If set to wxMENU_TEAROFF, the menu will be detachable (wxGTK only).
403 */
404 wxMenu(long style);
405
406 /**
407 Constructs a wxMenu object with a title
408
409 @param title
410 Title at the top of the menu (not always supported).
411 @param style
412 If set to wxMENU_TEAROFF, the menu will be detachable (wxGTK only).
413 */
414 wxMenu(const wxString& title = "", long style = 0);
415
416 /**
417 Destructor, destroying the menu.
418 Note: under Motif, a popup menu must have a valid parent (the window
419 it was last popped up on) when being destroyed. Therefore, make sure
420 you delete or re-use the popup menu @e before destroying the
421 parent window. Re-use in this context means popping up the menu on
422 a different window from last time, which causes an implicit destruction
423 and recreation of internal data structures.
424 */
425 virtual ~wxMenu();
426
427 /**
428 Adds a menu item.
429
430 @param id
431 The menu command identifier.
432 @param item
433 The string to appear on the menu item.
434 @param helpString
435 An optional help string associated with the item.
436 By default, the handler for the wxEVT_MENU_HIGHLIGHT event displays
437 this string in the status line.
438 @param kind
439 May be wxITEM_SEPARATOR, wxITEM_NORMAL,
440 wxITEM_CHECK or wxITEM_RADIO
441
442 @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
443 AppendSubMenu(), Insert(), SetLabel(),
444 GetHelpString(), SetHelpString(), wxMenuItem
445 */
446 wxMenuItem* Append(int id, const wxString& item = wxEmptyString,
447 const wxString& helpString = wxEmptyString,
448 wxItemKind kind = wxITEM_NORMAL);
449
450 /**
451 Adds a submenu.
452
453 @deprecated This function is deprecated, use AppendSubMenu() instead.
454
455 @param id
456 The menu command identifier.
457 @param item
458 The string to appear on the menu item.
459 @param subMenu
460 Pull-right submenu.
461 @param helpString
462 An optional help string associated with the item.
463 By default, the handler for the wxEVT_MENU_HIGHLIGHT event displays
464 this string in the status line.
465
466 @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
467 AppendSubMenu(), Insert(), SetLabel(),
468 GetHelpString(), SetHelpString(), wxMenuItem
469 */
470 wxMenuItem* Append(int id, const wxString& item, wxMenu* subMenu,
471 const wxString& helpString = wxEmptyString);
472
473 /**
474 Adds a menu item object. This is the most generic variant of Append() method
475 because it may be used for both items (including separators) and submenus and
476 because you can also specify various extra properties of a menu item this way,
477 such as bitmaps and fonts.
478
479 @param menuItem
480 A menuitem object. It will be owned by the wxMenu object after this function
481 is called, so do not delete it yourself.
482
483 @see AppendSeparator(), AppendCheckItem(), AppendRadioItem(),
484 AppendSubMenu(), Insert(), SetLabel(),
485 GetHelpString(), SetHelpString(), wxMenuItem
486 */
487 wxMenuItem* Append(wxMenuItem* menuItem);
488
489 /**
490 Adds a checkable item to the end of the menu.
491
492 @see Append(), InsertCheckItem()
493 */
494 wxMenuItem* AppendCheckItem(int id, const wxString& item,
495 const wxString& helpString = "");
496
497 /**
498 Adds a radio item to the end of the menu. All consequent radio items form a
499 group and when an item in the group is checked, all the others are
500 automatically unchecked.
501
502 @see Append(), InsertRadioItem()
503 */
504 wxMenuItem* AppendRadioItem(int id, const wxString& item,
505 const wxString& helpString = "");
506
507 /**
508 Adds a separator to the end of the menu.
509
510 @see Append(), InsertSeparator()
511 */
512 wxMenuItem* AppendSeparator();
513
514 /**
515 Adds the given @a submenu to this menu. @a text is the text shown in the
516 menu for it and @a help is the help string shown in the status bar when the
517 submenu item is selected.
518 */
519 wxMenuItem* AppendSubMenu(wxMenu* submenu, const wxString& text,
520 const wxString& help = wxEmptyString);
521
522 /**
523 Inserts a break in a menu, causing the next appended item to appear in a new
524 column.
525 */
526 virtual void Break();
527
528 /**
529 Checks or unchecks the menu item.
530
531 @param id
532 The menu item identifier.
533 @param check
534 If @true, the item will be checked, otherwise it will be unchecked.
535
536 @see IsChecked()
537 */
538 void Check(int id, const bool check);
539
540 /**
541 Deletes the menu item from the menu. If the item is a submenu, it will
542 @b not be deleted. Use Destroy() if you want to delete a submenu.
543
544 @param id
545 Id of the menu item to be deleted.
546
547 @see FindItem(), Destroy(), Remove()
548 */
549 void Delete(int id);
550
551 /**
552 Deletes the menu item from the menu. If the item is a submenu, it will
553 @b not be deleted. Use Destroy() if you want to delete a submenu.
554
555 @param item
556 Menu item to be deleted.
557
558 @see FindItem(), Destroy(), Remove()
559 */
560 void Delete(wxMenuItem* item);
561
562 /**
563 Deletes the menu item from the menu. If the item is a submenu, it will
564 be deleted. Use Remove() if you want to keep the submenu (for example,
565 to reuse it later).
566
567 @param id
568 Id of the menu item to be deleted.
569
570 @see FindItem(), Deletes(), Remove()
571 */
572 void Destroy(int id);
573
574 /**
575 Deletes the menu item from the menu. If the item is a submenu, it will
576 be deleted. Use Remove() if you want to keep the submenu (for example,
577 to reuse it later).
578
579 @param item
580 Menu item to be deleted.
581
582 @see FindItem(), Deletes(), Remove()
583 */
584 void Destroy(wxMenuItem* item);
585
586 /**
587 Enables or disables (greys out) a menu item.
588
589 @param id
590 The menu item identifier.
591 @param enable
592 @true to enable the menu item, @false to disable it.
593
594 @see IsEnabled()
595 */
596 void Enable(int id, const bool enable);
597
598 /**
599 Finds the menu id for a menu item string.
600
601 @param itemString
602 Menu item string to find.
603
604 @return Menu item identifier, or wxNOT_FOUND if none is found.
605
606 @remarks Any special menu codes are stripped out of source and target
607 strings before matching.
608 */
609 virtual int FindItem(const wxString& itemString) const;
610
611 /**
612 Finds the menu item object associated with the given menu item identifier and,
613 optionally, the (sub)menu it belongs to.
614
615 @param id
616 Menu item identifier.
617 @param menu
618 If the pointer is not @NULL, it will be filled with the item's
619 parent menu (if the item was found)
620
621 @return Menu item object or NULL if none is found.
622 */
623 const wxMenuItem * FindItem(int id, wxMenu** menu = NULL) const;
624
625 /**
626 Returns the wxMenuItem given a position in the menu.
627 */
628 wxMenuItem* FindItemByPosition(size_t position) const;
629
630 /**
631 Returns the help string associated with a menu item.
632
633 @param id
634 The menu item identifier.
635
636 @return The help string, or the empty string if there is no help string
637 or the item was not found.
638
639 @see SetHelpString(), Append()
640 */
641 virtual wxString GetHelpString(int id) const;
642
643 /**
644 Returns a menu item label.
645
646 @param id
647 The menu item identifier.
648
649 @return The item label, or the empty string if the item was not found.
650
651 @see GetLabelText(), SetLabel()
652 */
653 wxString GetLabel(int id) const;
654
655 /**
656 Returns a menu item label, without any of the original mnemonics and
657 accelerators.
658
659 @param id
660 The menu item identifier.
661
662 @return The item label, or the empty string if the item was not found.
663
664 @see GetLabel(), SetLabel()
665 */
666 wxString GetLabelText(int id) const;
667
668 /**
669 Returns the number of items in the menu.
670 */
671 size_t GetMenuItemCount() const;
672
673 /**
674 Returns the list of items in the menu. wxMenuItemList is a pseudo-template
675 list class containing wxMenuItem pointers, see wxList.
676 */
677 wxMenuItemList GetMenuItems() const;
678
679 /**
680 Returns the title of the menu.
681
682 @remarks This is relevant only to popup menus, use
683 wxMenuBar::GetMenuLabel for the menus in the menubar.
684
685 @see SetTitle()
686 */
687 wxString GetTitle() const;
688
689 /**
690 Inserts the given @a item before the position @e pos. Inserting the item
691 at position GetMenuItemCount() is the same
692 as appending it.
693
694 @see Append(), Prepend()
695 */
696 wxMenuItem* Insert(size_t pos, wxMenuItem* item);
697
698 /**
699 Inserts the given @a item before the position @e pos. Inserting the item
700 at position GetMenuItemCount() is the same
701 as appending it.
702
703 @see Append(), Prepend()
704 */
705 wxMenuItem* Insert(size_t pos, int id,
706 const wxString& item = "",
707 const wxString& helpString = "",
708 wxItemKind kind = wxITEM_NORMAL);
709
710 /**
711 Inserts a checkable item at the given position.
712
713 @see Insert(), AppendCheckItem()
714 */
715 wxMenuItem* InsertCheckItem(size_t pos, int id,
716 const wxString& item,
717 const wxString& helpString = "");
718
719 /**
720 Inserts a radio item at the given position.
721
722 @see Insert(), AppendRadioItem()
723 */
724 wxMenuItem* InsertRadioItem(size_t pos, int id,
725 const wxString& item,
726 const wxString& helpString = "");
727
728 /**
729 Inserts a separator at the given position.
730
731 @see Insert(), AppendSeparator()
732 */
733 wxMenuItem* InsertSeparator(size_t pos);
734
735 /**
736 Determines whether a menu item is checked.
737
738 @param id
739 The menu item identifier.
740
741 @return @true if the menu item is checked, @false otherwise.
742
743 @see Check()
744 */
745 bool IsChecked(int id) const;
746
747 /**
748 Determines whether a menu item is enabled.
749
750 @param id
751 The menu item identifier.
752
753 @return @true if the menu item is enabled, @false otherwise.
754
755 @see Enable()
756 */
757 bool IsEnabled(int id) const;
758
759 /**
760 Inserts the given @a item at position 0, i.e. before all the other
761 existing items.
762
763 @see Append(), Insert()
764 */
765 wxMenuItem* Prepend(wxMenuItem* item);
766
767 /**
768 Inserts the given @a item at position 0, i.e. before all the other
769 existing items.
770
771 @see Append(), Insert()
772 */
773 wxMenuItem* Prepend(int id, const wxString& item = "",
774 const wxString& helpString = "",
775 wxItemKind kind = wxITEM_NORMAL);
776
777 /**
778 Inserts a checkable item at position 0.
779
780 @see Prepend(), AppendCheckItem()
781 */
782 wxMenuItem* PrependCheckItem(int id, const wxString& item,
783 const wxString& helpString = "");
784
785 /**
786 Inserts a radio item at position 0.
787
788 @see Prepend(), AppendRadioItem()
789 */
790 wxMenuItem* PrependRadioItem(int id, const wxString& item,
791 const wxString& helpString = "");
792
793 /**
794 Inserts a separator at position 0.
795
796 @see Prepend(), AppendSeparator()
797 */
798 wxMenuItem* PrependSeparator();
799
800 /**
801 Removes the menu item from the menu but doesn't delete the associated C++
802 object. This allows you to reuse the same item later by adding it back to the menu
803 (especially useful with submenus).
804
805 @param id
806 The identifier of the menu item to remove.
807
808 @return A pointer to the item which was detached from the menu.
809 */
810 wxMenuItem* Remove(int id);
811
812 /**
813 Removes the menu item from the menu but doesn't delete the associated C++
814 object. This allows you to reuse the same item later by adding it back to the menu
815 (especially useful with submenus).
816
817 @param item
818 The menu item to remove.
819
820 @return A pointer to the item which was detached from the menu.
821 */
822 wxMenuItem* Remove(wxMenuItem* item);
823
824 /**
825 Sets an item's help string.
826
827 @param id
828 The menu item identifier.
829 @param helpString
830 The help string to set.
831
832 @see GetHelpString()
833 */
834 virtual void SetHelpString(int id, const wxString& helpString);
835
836 /**
837 Sets the label of a menu item.
838
839 @param id
840 The menu item identifier.
841 @param label
842 The menu item label to set.
843
844 @see Append(), GetLabel()
845 */
846 void SetLabel(int id, const wxString& label);
847
848 /**
849 Sets the title of the menu.
850
851 @param title
852 The title to set.
853
854 @remarks This is relevant only to popup menus, use
855 wxMenuBar::SetLabelTop for the menus in the menubar.
856
857 @see GetTitle()
858 */
859 virtual void SetTitle(const wxString& title);
860
861 /**
862 Sends events to @a source (or owning window if @NULL) to update the
863 menu UI. This is called just before the menu is popped up with
864 wxWindow::PopupMenu, but
865 the application may call it at other times if required.
866 */
867 void UpdateUI(wxEvtHandler* source = NULL);
868 };
869