menu.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/univ/menu.h
  3. // Purpose: wxMenu and wxMenuBar classes for wxUniversal
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 05.05.01
  7. // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_UNIV_MENU_H_
  11. #define _WX_UNIV_MENU_H_
  12. #if wxUSE_ACCEL
  13. #include "wx/accel.h"
  14. #endif // wxUSE_ACCEL
  15. #include "wx/dynarray.h"
  16. // fwd declarations
  17. class WXDLLIMPEXP_FWD_CORE wxMenuInfo;
  18. WX_DECLARE_EXPORTED_OBJARRAY(wxMenuInfo, wxMenuInfoArray);
  19. class WXDLLIMPEXP_FWD_CORE wxMenuGeometryInfo;
  20. class WXDLLIMPEXP_FWD_CORE wxPopupMenuWindow;
  21. class WXDLLIMPEXP_FWD_CORE wxRenderer;
  22. // ----------------------------------------------------------------------------
  23. // wxMenu
  24. // ----------------------------------------------------------------------------
  25. class WXDLLIMPEXP_CORE wxMenu : public wxMenuBase
  26. {
  27. public:
  28. // ctors and dtor
  29. wxMenu(const wxString& title, long style = 0)
  30. : wxMenuBase(title, style) { Init(); }
  31. wxMenu(long style = 0) : wxMenuBase(style) { Init(); }
  32. virtual ~wxMenu();
  33. // called by wxMenuItem when an item of this menu changes
  34. void RefreshItem(wxMenuItem *item);
  35. // does the menu have any items?
  36. bool IsEmpty() const { return !GetMenuItems().GetFirst(); }
  37. // show this menu at the given position (in screen coords) and optionally
  38. // select its first item
  39. void Popup(const wxPoint& pos, const wxSize& size,
  40. bool selectFirst = true);
  41. // dismiss the menu
  42. void Dismiss();
  43. // override the base class methods to connect/disconnect event handlers
  44. virtual void Attach(wxMenuBarBase *menubar);
  45. virtual void Detach();
  46. // implementation only from here
  47. // do as if this item were clicked, return true if the resulting event was
  48. // processed, false otherwise
  49. bool ClickItem(wxMenuItem *item);
  50. // process the key event, return true if done
  51. bool ProcessKeyDown(int key);
  52. #if wxUSE_ACCEL
  53. // find the item for the given accel and generate an event if found
  54. bool ProcessAccelEvent(const wxKeyEvent& event);
  55. #endif // wxUSE_ACCEL
  56. protected:
  57. // implement base class virtuals
  58. virtual wxMenuItem* DoAppend(wxMenuItem *item);
  59. virtual wxMenuItem* DoInsert(size_t pos, wxMenuItem *item);
  60. virtual wxMenuItem* DoRemove(wxMenuItem *item);
  61. // common part of DoAppend and DoInsert
  62. void OnItemAdded(wxMenuItem *item);
  63. // called by wxPopupMenuWindow when the window is hidden
  64. void OnDismiss(bool dismissParent);
  65. // return true if the menu is currently shown on screen
  66. bool IsShown() const;
  67. // get the menu geometry info
  68. const wxMenuGeometryInfo& GetGeometryInfo() const;
  69. // forget old menu geometry info
  70. void InvalidateGeometryInfo();
  71. // return either the menubar or the invoking window, normally never NULL
  72. wxWindow *GetRootWindow() const;
  73. // get the renderer we use for drawing: either the one of the menu bar or
  74. // the one of the window if we're a popup menu
  75. wxRenderer *GetRenderer() const;
  76. #if wxUSE_ACCEL
  77. // add/remove accel for the given menu item
  78. void AddAccelFor(wxMenuItem *item);
  79. void RemoveAccelFor(wxMenuItem *item);
  80. #endif // wxUSE_ACCEL
  81. private:
  82. // common part of all ctors
  83. void Init();
  84. // terminate the current radio group, if any
  85. void EndRadioGroup();
  86. // the exact menu geometry is defined by a struct derived from this one
  87. // which is opaque and defined by the renderer
  88. wxMenuGeometryInfo *m_geometry;
  89. // the menu shown on screen or NULL if not currently shown
  90. wxPopupMenuWindow *m_popupMenu;
  91. #if wxUSE_ACCEL
  92. // the accel table for this menu
  93. wxAcceleratorTable m_accelTable;
  94. #endif // wxUSE_ACCEL
  95. // the position of the first item in the current radio group or -1
  96. int m_startRadioGroup;
  97. // it calls out OnDismiss()
  98. friend class wxPopupMenuWindow;
  99. DECLARE_DYNAMIC_CLASS(wxMenu)
  100. };
  101. // ----------------------------------------------------------------------------
  102. // wxMenuBar
  103. // ----------------------------------------------------------------------------
  104. class WXDLLIMPEXP_CORE wxMenuBar : public wxMenuBarBase
  105. {
  106. public:
  107. // ctors and dtor
  108. wxMenuBar(long WXUNUSED(style) = 0) { Init(); }
  109. wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style = 0);
  110. virtual ~wxMenuBar();
  111. // implement base class virtuals
  112. virtual bool Append( wxMenu *menu, const wxString &title );
  113. virtual bool Insert(size_t pos, wxMenu *menu, const wxString& title);
  114. virtual wxMenu *Replace(size_t pos, wxMenu *menu, const wxString& title);
  115. virtual wxMenu *Remove(size_t pos);
  116. virtual void EnableTop(size_t pos, bool enable);
  117. virtual bool IsEnabledTop(size_t pos) const;
  118. virtual void SetMenuLabel(size_t pos, const wxString& label);
  119. virtual wxString GetMenuLabel(size_t pos) const;
  120. virtual void Attach(wxFrame *frame);
  121. virtual void Detach();
  122. // get the next item for the givan accel letter (used by wxFrame), return
  123. // -1 if none
  124. //
  125. // if unique is not NULL, filled with true if there is only one item with
  126. // this accel, false if two or more
  127. int FindNextItemForAccel(int idxStart,
  128. int keycode,
  129. bool *unique = NULL) const;
  130. // called by wxFrame to set focus to or open the given menu
  131. void SelectMenu(size_t pos);
  132. void PopupMenu(size_t pos);
  133. #if wxUSE_ACCEL
  134. // find the item for the given accel and generate an event if found
  135. bool ProcessAccelEvent(const wxKeyEvent& event);
  136. #endif // wxUSE_ACCEL
  137. // called by wxMenu when it is dismissed
  138. void OnDismissMenu(bool dismissMenuBar = false);
  139. protected:
  140. // common part of all ctors
  141. void Init();
  142. // event handlers
  143. void OnLeftDown(wxMouseEvent& event);
  144. void OnMouseMove(wxMouseEvent& event);
  145. void OnKeyDown(wxKeyEvent& event);
  146. void OnKillFocus(wxFocusEvent& event);
  147. // process the mouse move event, return true if we did, false to continue
  148. // processing as usual
  149. //
  150. // the coordinates are client coordinates of menubar, convert if necessary
  151. bool ProcessMouseEvent(const wxPoint& pt);
  152. // called when the menu bar loses mouse capture - it is not hidden unlike
  153. // menus, but it doesn't have modal status any longer
  154. void OnDismiss();
  155. // draw the menubar
  156. virtual void DoDraw(wxControlRenderer *renderer);
  157. // menubar geometry
  158. virtual wxSize DoGetBestClientSize() const;
  159. // has the menubar been created already?
  160. bool IsCreated() const { return m_frameLast != NULL; }
  161. // "fast" version of GetMenuCount()
  162. size_t GetCount() const { return m_menuInfos.GetCount(); }
  163. // get the (total) width of the specified menu
  164. wxCoord GetItemWidth(size_t pos) const;
  165. // get the rect of the item
  166. wxRect GetItemRect(size_t pos) const;
  167. // get the menu from the given point or -1 if none
  168. int GetMenuFromPoint(const wxPoint& pos) const;
  169. // refresh the given item
  170. void RefreshItem(size_t pos);
  171. // refresh all items after this one (including it)
  172. void RefreshAllItemsAfter(size_t pos);
  173. // hide the currently shown menu and show this one
  174. void DoSelectMenu(size_t pos);
  175. // popup the currently selected menu
  176. void PopupCurrentMenu(bool selectFirst = true);
  177. // hide the currently selected menu
  178. void DismissMenu();
  179. // do we show a menu currently?
  180. bool IsShowingMenu() const { return m_menuShown != 0; }
  181. // we don't want to have focus except while selecting from menu
  182. void GiveAwayFocus();
  183. // Release the mouse capture if we have it
  184. bool ReleaseMouseCapture();
  185. // the array containing extra menu info we need
  186. wxMenuInfoArray m_menuInfos;
  187. // the current item (only used when menubar has focus)
  188. int m_current;
  189. private:
  190. // the last frame to which we were attached, NULL initially
  191. wxFrame *m_frameLast;
  192. // the currently shown menu or NULL
  193. wxMenu *m_menuShown;
  194. // should be showing the menu? this is subtly different from m_menuShown !=
  195. // NULL as the menu which should be shown may be disabled in which case we
  196. // don't show it - but will do as soon as the focus shifts to another menu
  197. bool m_shouldShowMenu;
  198. // it calls out ProcessMouseEvent()
  199. friend class wxPopupMenuWindow;
  200. DECLARE_EVENT_TABLE()
  201. DECLARE_DYNAMIC_CLASS(wxMenuBar)
  202. };
  203. #endif // _WX_UNIV_MENU_H_