menuitem.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/menuitem.h
  3. // Purpose: wxMenuItem class
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 25.10.99
  7. // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_MENUITEM_H_BASE_
  11. #define _WX_MENUITEM_H_BASE_
  12. #include "wx/defs.h"
  13. #if wxUSE_MENUS
  14. // ----------------------------------------------------------------------------
  15. // headers
  16. // ----------------------------------------------------------------------------
  17. #include "wx/object.h" // base class
  18. // ----------------------------------------------------------------------------
  19. // forward declarations
  20. // ----------------------------------------------------------------------------
  21. class WXDLLIMPEXP_FWD_CORE wxAcceleratorEntry;
  22. class WXDLLIMPEXP_FWD_CORE wxMenuItem;
  23. class WXDLLIMPEXP_FWD_CORE wxMenu;
  24. // ----------------------------------------------------------------------------
  25. // wxMenuItem is an item in the menu which may be either a normal item, a sub
  26. // menu or a separator
  27. // ----------------------------------------------------------------------------
  28. class WXDLLIMPEXP_CORE wxMenuItemBase : public wxObject
  29. {
  30. public:
  31. // creation
  32. static wxMenuItem *New(wxMenu *parentMenu = NULL,
  33. int itemid = wxID_SEPARATOR,
  34. const wxString& text = wxEmptyString,
  35. const wxString& help = wxEmptyString,
  36. wxItemKind kind = wxITEM_NORMAL,
  37. wxMenu *subMenu = NULL);
  38. // destruction: wxMenuItem will delete its submenu
  39. virtual ~wxMenuItemBase();
  40. // the menu we're in
  41. wxMenu *GetMenu() const { return m_parentMenu; }
  42. void SetMenu(wxMenu* menu) { m_parentMenu = menu; }
  43. // get/set id
  44. void SetId(int itemid) { m_id = itemid; }
  45. int GetId() const { return m_id; }
  46. // the item's text (or name)
  47. //
  48. // NB: the item's label includes the accelerators and mnemonics info (if
  49. // any), i.e. it may contain '&' or '_' or "\t..." and thus is
  50. // different from the item's text which only contains the text shown
  51. // in the menu. This used to be called SetText.
  52. virtual void SetItemLabel(const wxString& str);
  53. // return the item label including any mnemonics and accelerators.
  54. // This used to be called GetText.
  55. virtual wxString GetItemLabel() const { return m_text; }
  56. // return just the text of the item label, without any mnemonics
  57. // This used to be called GetLabel.
  58. virtual wxString GetItemLabelText() const { return GetLabelText(m_text); }
  59. // return just the text part of the given label (implemented in platform-specific code)
  60. // This used to be called GetLabelFromText.
  61. static wxString GetLabelText(const wxString& label);
  62. // what kind of menu item we are
  63. wxItemKind GetKind() const { return m_kind; }
  64. void SetKind(wxItemKind kind) { m_kind = kind; }
  65. bool IsSeparator() const { return m_kind == wxITEM_SEPARATOR; }
  66. bool IsCheck() const { return m_kind == wxITEM_CHECK; }
  67. bool IsRadio() const { return m_kind == wxITEM_RADIO; }
  68. virtual void SetCheckable(bool checkable)
  69. { m_kind = checkable ? wxITEM_CHECK : wxITEM_NORMAL; }
  70. // Notice that this doesn't quite match SetCheckable().
  71. bool IsCheckable() const
  72. { return m_kind == wxITEM_CHECK || m_kind == wxITEM_RADIO; }
  73. bool IsSubMenu() const { return m_subMenu != NULL; }
  74. void SetSubMenu(wxMenu *menu) { m_subMenu = menu; }
  75. wxMenu *GetSubMenu() const { return m_subMenu; }
  76. // state
  77. virtual void Enable(bool enable = true) { m_isEnabled = enable; }
  78. virtual bool IsEnabled() const { return m_isEnabled; }
  79. virtual void Check(bool check = true) { m_isChecked = check; }
  80. virtual bool IsChecked() const { return m_isChecked; }
  81. void Toggle() { Check(!m_isChecked); }
  82. // help string (displayed in the status bar by default)
  83. void SetHelp(const wxString& str);
  84. const wxString& GetHelp() const { return m_help; }
  85. #if wxUSE_ACCEL
  86. // extract the accelerator from the given menu string, return NULL if none
  87. // found
  88. static wxAcceleratorEntry *GetAccelFromString(const wxString& label);
  89. // get our accelerator or NULL (caller must delete the pointer)
  90. virtual wxAcceleratorEntry *GetAccel() const;
  91. // set the accel for this item - this may also be done indirectly with
  92. // SetText()
  93. virtual void SetAccel(wxAcceleratorEntry *accel);
  94. #endif // wxUSE_ACCEL
  95. #if WXWIN_COMPATIBILITY_2_8
  96. // compatibility only, use new functions in the new code
  97. wxDEPRECATED( void SetName(const wxString& str) );
  98. wxDEPRECATED( wxString GetName() const );
  99. // Now use GetItemLabelText
  100. wxDEPRECATED( wxString GetLabel() const ) ;
  101. // Now use GetItemLabel
  102. wxDEPRECATED( const wxString& GetText() const );
  103. // Now use GetLabelText to strip the accelerators
  104. static wxDEPRECATED( wxString GetLabelFromText(const wxString& text) );
  105. // Now use SetItemLabel
  106. wxDEPRECATED( virtual void SetText(const wxString& str) );
  107. #endif // WXWIN_COMPATIBILITY_2_8
  108. static wxMenuItem *New(wxMenu *parentMenu,
  109. int itemid,
  110. const wxString& text,
  111. const wxString& help,
  112. bool isCheckable,
  113. wxMenu *subMenu = NULL)
  114. {
  115. return New(parentMenu, itemid, text, help,
  116. isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu);
  117. }
  118. protected:
  119. wxWindowIDRef m_id; // numeric id of the item >= 0 or wxID_ANY or wxID_SEPARATOR
  120. wxMenu *m_parentMenu, // the menu we belong to
  121. *m_subMenu; // our sub menu or NULL
  122. wxString m_text, // label of the item
  123. m_help; // the help string for the item
  124. wxItemKind m_kind; // separator/normal/check/radio item?
  125. bool m_isChecked; // is checked?
  126. bool m_isEnabled; // is enabled?
  127. // this ctor is for the derived classes only, we're never created directly
  128. wxMenuItemBase(wxMenu *parentMenu = NULL,
  129. int itemid = wxID_SEPARATOR,
  130. const wxString& text = wxEmptyString,
  131. const wxString& help = wxEmptyString,
  132. wxItemKind kind = wxITEM_NORMAL,
  133. wxMenu *subMenu = NULL);
  134. private:
  135. // and, if we have one ctor, compiler won't generate a default copy one, so
  136. // declare them ourselves - but don't implement as they shouldn't be used
  137. wxMenuItemBase(const wxMenuItemBase& item);
  138. wxMenuItemBase& operator=(const wxMenuItemBase& item);
  139. };
  140. #if WXWIN_COMPATIBILITY_2_8
  141. inline void wxMenuItemBase::SetName(const wxString &str)
  142. { SetItemLabel(str); }
  143. inline wxString wxMenuItemBase::GetName() const
  144. { return GetItemLabel(); }
  145. inline wxString wxMenuItemBase::GetLabel() const
  146. { return GetLabelText(m_text); }
  147. inline const wxString& wxMenuItemBase::GetText() const { return m_text; }
  148. inline void wxMenuItemBase::SetText(const wxString& text) { SetItemLabel(text); }
  149. #endif // WXWIN_COMPATIBILITY_2_8
  150. // ----------------------------------------------------------------------------
  151. // include the real class declaration
  152. // ----------------------------------------------------------------------------
  153. #ifdef wxUSE_BASE_CLASSES_ONLY
  154. #define wxMenuItem wxMenuItemBase
  155. #else // !wxUSE_BASE_CLASSES_ONLY
  156. #if defined(__WXUNIVERSAL__)
  157. #include "wx/univ/menuitem.h"
  158. #elif defined(__WXMSW__)
  159. #include "wx/msw/menuitem.h"
  160. #elif defined(__WXMOTIF__)
  161. #include "wx/motif/menuitem.h"
  162. #elif defined(__WXGTK20__)
  163. #include "wx/gtk/menuitem.h"
  164. #elif defined(__WXGTK__)
  165. #include "wx/gtk1/menuitem.h"
  166. #elif defined(__WXMAC__)
  167. #include "wx/osx/menuitem.h"
  168. #elif defined(__WXCOCOA__)
  169. #include "wx/cocoa/menuitem.h"
  170. #elif defined(__WXPM__)
  171. #include "wx/os2/menuitem.h"
  172. #endif
  173. #endif // wxUSE_BASE_CLASSES_ONLY/!wxUSE_BASE_CLASSES_ONLY
  174. #endif // wxUSE_MENUS
  175. #endif
  176. // _WX_MENUITEM_H_BASE_