menu.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/cocoa/menu.h
  3. // Purpose: wxMenu and wxMenuBar classes
  4. // Author: David Elliott
  5. // Modified by:
  6. // Created: 2002/12/09
  7. // Copyright: (c) 2002 David Elliott
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef __WX_COCOA_MENU_H__
  11. #define __WX_COCOA_MENU_H__
  12. #include "wx/cocoa/NSMenu.h"
  13. #if wxUSE_ACCEL
  14. #include "wx/accel.h"
  15. #endif // wxUSE_ACCEL
  16. // ========================================================================
  17. // wxMenu
  18. // ========================================================================
  19. class WXDLLIMPEXP_CORE wxMenu : public wxMenuBase, public wxCocoaNSMenu
  20. {
  21. public:
  22. // ctors and dtor
  23. wxMenu(const wxString& title, long style = 0)
  24. : wxMenuBase(title, style)
  25. , m_cocoaDeletes(false)
  26. { Create(title,style); }
  27. bool Create(const wxString& title, long style = 0);
  28. wxMenu(long style = 0) : wxMenuBase(style) { Create(wxEmptyString, style); }
  29. virtual ~wxMenu();
  30. // ------------------------------------------------------------------------
  31. // Cocoa specifics
  32. // ------------------------------------------------------------------------
  33. public:
  34. inline WX_NSMenu GetNSMenu() { return m_cocoaNSMenu; }
  35. void SetCocoaDeletes(bool cocoaDeletes);
  36. virtual void Cocoa_dealloc();
  37. protected:
  38. WX_NSMenu m_cocoaNSMenu;
  39. bool m_cocoaDeletes;
  40. // ------------------------------------------------------------------------
  41. // Implementation
  42. // ------------------------------------------------------------------------
  43. protected:
  44. // implement base class virtuals
  45. virtual wxMenuItem* DoAppend(wxMenuItem *item);
  46. virtual wxMenuItem* DoInsert(size_t pos, wxMenuItem *item);
  47. virtual wxMenuItem* DoRemove(wxMenuItem *item);
  48. #if wxUSE_ACCEL
  49. // add/remove accel for the given menu item
  50. void AddAccelFor(wxMenuItem *item);
  51. void RemoveAccelFor(wxMenuItem *item);
  52. #endif // wxUSE_ACCEL
  53. private:
  54. #if wxUSE_ACCEL
  55. // the accel table for this menu
  56. wxAcceleratorTable m_accelTable;
  57. #endif // wxUSE_ACCEL
  58. DECLARE_DYNAMIC_CLASS(wxMenu)
  59. };
  60. // ========================================================================
  61. // wxMenuBar
  62. // ========================================================================
  63. class WXDLLIMPEXP_CORE wxMenuBar : public wxMenuBarBase
  64. {
  65. public:
  66. // ctors and dtor
  67. wxMenuBar(long style = 0) { Create(style); }
  68. wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style = 0);
  69. bool Create(long style = 0);
  70. virtual ~wxMenuBar();
  71. // ------------------------------------------------------------------------
  72. // Cocoa specifics
  73. // ------------------------------------------------------------------------
  74. public:
  75. inline WX_NSMenu GetNSMenu() { return m_cocoaNSMenu; }
  76. protected:
  77. WX_NSMenu m_cocoaNSMenu;
  78. // ------------------------------------------------------------------------
  79. // Implementation
  80. // ------------------------------------------------------------------------
  81. public:
  82. // implement base class virtuals
  83. virtual bool Append(wxMenu *menu, const wxString &title);
  84. virtual bool Insert(size_t pos, wxMenu *menu, const wxString& title);
  85. virtual wxMenu *Replace(size_t pos, wxMenu *menu, const wxString& title);
  86. virtual wxMenu *Remove(size_t pos);
  87. virtual void EnableTop(size_t pos, bool enable);
  88. virtual bool IsEnabledTop(size_t pos) const;
  89. virtual void SetMenuLabel(size_t pos, const wxString& label);
  90. virtual wxString GetMenuLabel(size_t pos) const;
  91. virtual void Attach(wxFrame *frame);
  92. virtual void Detach();
  93. // get the next item for the givan accel letter (used by wxFrame), return
  94. // -1 if none
  95. //
  96. // if unique is not NULL, filled with TRUE if there is only one item with
  97. // this accel, FALSE if two or more
  98. int FindNextItemForAccel(int idxStart,
  99. int keycode,
  100. bool *unique = NULL) const;
  101. // called by wxFrame to set focus to or open the given menu
  102. void SelectMenu(size_t pos);
  103. #if wxUSE_ACCEL
  104. // find the item for the given accel and generate an event if found
  105. bool ProcessAccelEvent(const wxKeyEvent& event);
  106. #endif // wxUSE_ACCEL
  107. protected:
  108. // event handlers
  109. void OnLeftDown(wxMouseEvent& event);
  110. void OnMouseMove(wxMouseEvent& event);
  111. void OnKeyDown(wxKeyEvent& event);
  112. void OnKillFocus(wxFocusEvent& event);
  113. // process the mouse move event, return TRUE if we did, FALSE to continue
  114. // processing as usual
  115. //
  116. // the coordinates are client coordinates of menubar, convert if necessary
  117. bool ProcessMouseEvent(const wxPoint& pt);
  118. // menubar geometry
  119. virtual wxSize DoGetBestClientSize() const;
  120. // has the menubar been created already?
  121. bool IsCreated() const { return m_frameLast != NULL; }
  122. // get the (total) width of the specified menu
  123. wxCoord GetItemWidth(size_t pos) const;
  124. // get the rect of the item
  125. wxRect GetItemRect(size_t pos) const;
  126. // get the menu from the given point or -1 if none
  127. int GetMenuFromPoint(const wxPoint& pos) const;
  128. // refresh the given item
  129. void RefreshItem(size_t pos);
  130. // refresh all items after this one (including it)
  131. void RefreshAllItemsAfter(size_t pos);
  132. // do we show a menu currently?
  133. bool IsShowingMenu() const { return m_menuShown != 0; }
  134. // we don't want to have focus except while selecting from menu
  135. void GiveAwayFocus();
  136. // the current item (only used when menubar has focus)
  137. int m_current;
  138. private:
  139. // the last frame to which we were attached, NULL initially
  140. wxFrame *m_frameLast;
  141. // the currently shown menu or NULL
  142. wxMenu *m_menuShown;
  143. // should be showing the menu? this is subtly different from m_menuShown !=
  144. // NULL as the menu which should be shown may be disabled in which case we
  145. // don't show it - but will do as soon as the focus shifts to another menu
  146. bool m_shouldShowMenu;
  147. DECLARE_DYNAMIC_CLASS(wxMenuBar)
  148. };
  149. #endif // _WX_COCOA_MENU_H_