menu.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msw/menu.h
  3. // Purpose: wxMenu, wxMenuBar classes
  4. // Author: Julian Smart
  5. // Modified by: Vadim Zeitlin (wxMenuItem is now in separate file)
  6. // Created: 01/02/97
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_MENU_H_
  11. #define _WX_MENU_H_
  12. #if wxUSE_ACCEL
  13. #include "wx/accel.h"
  14. #include "wx/dynarray.h"
  15. WX_DEFINE_EXPORTED_ARRAY_PTR(wxAcceleratorEntry *, wxAcceleratorArray);
  16. #endif // wxUSE_ACCEL
  17. class WXDLLIMPEXP_FWD_CORE wxFrame;
  18. #if defined(__WXWINCE__) && wxUSE_TOOLBAR
  19. class WXDLLIMPEXP_FWD_CORE wxToolBar;
  20. #endif
  21. class wxMenuRadioItemsData;
  22. // Not using a combined wxToolBar/wxMenuBar? then use
  23. // a commandbar in WinCE .NET to implement the
  24. // menubar, since there is no ::SetMenu function.
  25. #if defined(__WXWINCE__)
  26. # if ((_WIN32_WCE >= 400) && !defined(__POCKETPC__) && !defined(__SMARTPHONE__)) || \
  27. defined(__HANDHELDPC__)
  28. # define WINCE_WITH_COMMANDBAR
  29. # else
  30. # define WINCE_WITHOUT_COMMANDBAR
  31. # endif
  32. #endif
  33. #include "wx/arrstr.h"
  34. // ----------------------------------------------------------------------------
  35. // Menu
  36. // ----------------------------------------------------------------------------
  37. class WXDLLIMPEXP_CORE wxMenu : public wxMenuBase
  38. {
  39. public:
  40. // ctors & dtor
  41. wxMenu(const wxString& title, long style = 0)
  42. : wxMenuBase(title, style) { Init(); }
  43. wxMenu(long style = 0) : wxMenuBase(style) { Init(); }
  44. virtual ~wxMenu();
  45. virtual void Break();
  46. virtual void SetTitle(const wxString& title);
  47. // MSW-only methods
  48. // ----------------
  49. // Create a new menu from the given native HMENU. Takes ownership of the
  50. // menu handle and will delete it when this object is destroyed.
  51. static wxMenu *MSWNewFromHMENU(WXHMENU hMenu) { return new wxMenu(hMenu); }
  52. // implementation only from now on
  53. // -------------------------------
  54. bool MSWCommand(WXUINT param, WXWORD id);
  55. // get the native menu handle
  56. WXHMENU GetHMenu() const { return m_hMenu; }
  57. // Return the start and end position of the radio group to which the item
  58. // at the given position belongs. Returns false if there is no radio group
  59. // containing this position.
  60. bool MSWGetRadioGroupRange(int pos, int *start, int *end) const;
  61. #if wxUSE_ACCEL
  62. // called by wxMenuBar to build its accel table from the accels of all menus
  63. bool HasAccels() const { return !m_accels.empty(); }
  64. size_t GetAccelCount() const { return m_accels.size(); }
  65. size_t CopyAccels(wxAcceleratorEntry *accels) const;
  66. // called by wxMenuItem when its accels changes
  67. void UpdateAccel(wxMenuItem *item);
  68. // helper used by wxMenu itself (returns the index in m_accels)
  69. int FindAccel(int id) const;
  70. // used only by wxMDIParentFrame currently but could be useful elsewhere:
  71. // returns a new accelerator table with accelerators for just this menu
  72. // (shouldn't be called if we don't have any accelerators)
  73. wxAcceleratorTable *CreateAccelTable() const;
  74. #endif // wxUSE_ACCEL
  75. #if wxUSE_OWNER_DRAWN
  76. int GetMaxAccelWidth()
  77. {
  78. if (m_maxAccelWidth == -1)
  79. CalculateMaxAccelWidth();
  80. return m_maxAccelWidth;
  81. }
  82. void ResetMaxAccelWidth()
  83. {
  84. m_maxAccelWidth = -1;
  85. }
  86. // get the menu with given handle (recursively)
  87. wxMenu* MSWGetMenu(WXHMENU hMenu);
  88. private:
  89. void CalculateMaxAccelWidth();
  90. #endif // wxUSE_OWNER_DRAWN
  91. protected:
  92. virtual wxMenuItem* DoAppend(wxMenuItem *item);
  93. virtual wxMenuItem* DoInsert(size_t pos, wxMenuItem *item);
  94. virtual wxMenuItem* DoRemove(wxMenuItem *item);
  95. private:
  96. // This constructor is private, use MSWNewFromHMENU() to use it.
  97. wxMenu(WXHMENU hMenu);
  98. // Common part of all ctors, it doesn't create a new HMENU.
  99. void InitNoCreate();
  100. // Common part of all ctors except of the one above taking a native menu
  101. // handler: calls InitNoCreate() and also creates a new menu.
  102. void Init();
  103. // common part of Append/Insert (behaves as Append is pos == (size_t)-1)
  104. bool DoInsertOrAppend(wxMenuItem *item, size_t pos = (size_t)-1);
  105. // This variable contains the description of the radio item groups and
  106. // allows to find whether an item at the given position is part of the
  107. // group and also where its group starts and ends.
  108. //
  109. // It is initially NULL and only allocated if we have any radio items.
  110. wxMenuRadioItemsData *m_radioData;
  111. // if true, insert a breal before appending the next item
  112. bool m_doBreak;
  113. // the menu handle of this menu
  114. WXHMENU m_hMenu;
  115. #if wxUSE_ACCEL
  116. // the accelerators for our menu items
  117. wxAcceleratorArray m_accels;
  118. #endif // wxUSE_ACCEL
  119. #if wxUSE_OWNER_DRAWN
  120. // true if the menu has any ownerdrawn items
  121. bool m_ownerDrawn;
  122. // the max width of menu items bitmaps
  123. int m_maxBitmapWidth;
  124. // the max width of menu items accels
  125. int m_maxAccelWidth;
  126. #endif // wxUSE_OWNER_DRAWN
  127. DECLARE_DYNAMIC_CLASS_NO_COPY(wxMenu)
  128. };
  129. // ----------------------------------------------------------------------------
  130. // Menu Bar (a la Windows)
  131. // ----------------------------------------------------------------------------
  132. class WXDLLIMPEXP_CORE wxMenuBar : public wxMenuBarBase
  133. {
  134. public:
  135. // ctors & dtor
  136. // default constructor
  137. wxMenuBar();
  138. // unused under MSW
  139. wxMenuBar(long style);
  140. // menubar takes ownership of the menus arrays but copies the titles
  141. wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style = 0);
  142. virtual ~wxMenuBar();
  143. // menubar construction
  144. virtual bool Append( wxMenu *menu, const wxString &title );
  145. virtual bool Insert(size_t pos, wxMenu *menu, const wxString& title);
  146. virtual wxMenu *Replace(size_t pos, wxMenu *menu, const wxString& title);
  147. virtual wxMenu *Remove(size_t pos);
  148. virtual void EnableTop( size_t pos, bool flag );
  149. virtual bool IsEnabledTop(size_t pos) const;
  150. virtual void SetMenuLabel( size_t pos, const wxString& label );
  151. virtual wxString GetMenuLabel( size_t pos ) const;
  152. // implementation from now on
  153. WXHMENU Create();
  154. virtual void Detach();
  155. virtual void Attach(wxFrame *frame);
  156. #if defined(__WXWINCE__) && wxUSE_TOOLBAR
  157. // Under WinCE, a menubar is owned by the frame's toolbar
  158. void SetToolBar(wxToolBar* toolBar) { m_toolBar = toolBar; }
  159. wxToolBar* GetToolBar() const { return m_toolBar; }
  160. #endif
  161. #ifdef WINCE_WITH_COMMANDBAR
  162. WXHWND GetCommandBar() const { return m_commandBar; }
  163. bool AddAdornments(long style);
  164. #endif
  165. #if wxUSE_ACCEL
  166. // update the accel table (must be called after adding/deleting a menu)
  167. void RebuildAccelTable();
  168. #endif // wxUSE_ACCEL
  169. // get the menu handle
  170. WXHMENU GetHMenu() const { return m_hMenu; }
  171. // if the menubar is modified, the display is not updated automatically,
  172. // call this function to update it (m_menuBarFrame should be !NULL)
  173. void Refresh();
  174. // To avoid compile warning
  175. void Refresh( bool eraseBackground,
  176. const wxRect *rect = (const wxRect *) NULL ) { wxWindow::Refresh(eraseBackground, rect); }
  177. // get the menu with given handle (recursively)
  178. wxMenu* MSWGetMenu(WXHMENU hMenu);
  179. protected:
  180. // common part of all ctors
  181. void Init();
  182. WXHMENU m_hMenu;
  183. // Return the MSW position for a wxMenu which is sometimes different from
  184. // the wxWidgets position.
  185. int MSWPositionForWxMenu(wxMenu *menu, int wxpos);
  186. #if defined(__WXWINCE__) && wxUSE_TOOLBAR
  187. wxToolBar* m_toolBar;
  188. #endif
  189. #ifdef WINCE_WITH_COMMANDBAR
  190. WXHWND m_commandBar;
  191. bool m_adornmentsAdded;
  192. #endif
  193. private:
  194. DECLARE_DYNAMIC_CLASS_NO_COPY(wxMenuBar)
  195. };
  196. #endif // _WX_MENU_H_