1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/univ/menu.h
3 // Purpose: wxMenu and wxMenuBar classes for wxUniversal
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_UNIV_MENU_H_
13 #define _WX_UNIV_MENU_H_
16 #pragma interface "univmenu.h"
23 #include "wx/dynarray.h"
26 class WXDLLEXPORT wxMenuInfo
;
27 WX_DECLARE_OBJARRAY(wxMenuInfo
, wxMenuInfoArray
);
29 class wxPopupMenuWindow
;
31 class WXDLLEXPORT wxRenderer
;
33 // ----------------------------------------------------------------------------
34 // wxMenu helper classes, used in implementation only
35 // ----------------------------------------------------------------------------
38 class WXDLLEXPORT wxMenuGeometryInfo
41 // get the total size of the menu
42 virtual wxSize
GetSize() const = 0;
44 virtual ~wxMenuGeometryInfo();
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 class WXDLLEXPORT wxMenu
: public wxMenuBase
55 wxMenu(const wxString
& title
, long style
= 0)
56 : wxMenuBase(title
, style
) { Init(); }
58 wxMenu(long style
= 0) : wxMenuBase(style
) { Init(); }
62 // called by wxMenuItem when an item of this menu changes
63 void RefreshItem(wxMenuItem
*item
);
65 // does the menu have any items?
66 bool IsEmpty() const { return !GetMenuItems().GetFirst(); }
68 // show this menu at the given position (in screen coords) and optionally
69 // select its first item
70 void Popup(const wxPoint
& pos
, const wxSize
& size
,
71 bool selectFirst
= TRUE
);
76 // override the base class methods to connect/disconnect event handlers
77 virtual void Attach(wxMenuBarBase
*menubar
);
78 virtual void Detach();
80 // implementation only from here
82 // do as if this item were clicked, return TRUE if the resulting event was
83 // processed, FALSE otherwise
84 bool ClickItem(wxMenuItem
*item
);
86 // process the key event, return TRUE if done
87 bool ProcessKeyDown(int key
);
90 // find the item for the given accel and generate an event if found
91 bool ProcessAccelEvent(const wxKeyEvent
& event
);
95 // implement base class virtuals
96 virtual bool DoAppend(wxMenuItem
*item
);
97 virtual bool DoInsert(size_t pos
, wxMenuItem
*item
);
98 virtual wxMenuItem
*DoRemove(wxMenuItem
*item
);
100 // common part of DoAppend and DoInsert
101 void OnItemAdded(wxMenuItem
*item
);
103 // called by wxPopupMenuWindow when the window is hidden
104 void OnDismiss(bool dismissParent
);
106 // return true if the menu is currently shown on screen
107 bool IsShown() const;
109 // get the menu geometry info
110 const wxMenuGeometryInfo
& GetGeometryInfo() const;
112 // forget old menu geometry info
113 void InvalidateGeometryInfo();
115 // return either the menubar or the invoking window, normally never NULL
116 wxWindow
*GetRootWindow() const;
118 // get the renderer we use for drawing: either the one of the menu bar or
119 // the one of the window if we're a popup menu
120 wxRenderer
*GetRenderer() const;
123 // add/remove accel for the given menu item
124 void AddAccelFor(wxMenuItem
*item
);
125 void RemoveAccelFor(wxMenuItem
*item
);
126 #endif // wxUSE_ACCEL
129 // common part of all ctors
132 // the exact menu geometry is defined by a struct derived from this one
133 // which is opaque and defined by the renderer
134 wxMenuGeometryInfo
*m_geometry
;
136 // the menu shown on screen or NULL if not currently shown
137 wxPopupMenuWindow
*m_popupMenu
;
140 // the accel table for this menu
141 wxAcceleratorTable m_accelTable
;
142 #endif // wxUSE_ACCEL
144 // it calls out OnDismiss()
145 friend wxPopupMenuWindow
;
147 DECLARE_DYNAMIC_CLASS(wxMenu
)
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
154 class WXDLLEXPORT wxMenuBar
: public wxMenuBarBase
158 wxMenuBar(long WXUNUSED(style
) = 0) { Init(); }
159 virtual ~wxMenuBar();
161 // implement base class virtuals
162 virtual bool Append( wxMenu
*menu
, const wxString
&title
);
163 virtual bool Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
);
164 virtual wxMenu
*Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
);
165 virtual wxMenu
*Remove(size_t pos
);
167 virtual void EnableTop(size_t pos
, bool enable
);
168 virtual bool IsEnabledTop(size_t pos
) const;
170 virtual void SetLabelTop(size_t pos
, const wxString
& label
);
171 virtual wxString
GetLabelTop(size_t pos
) const;
173 virtual void Attach(wxFrame
*frame
);
174 virtual void Detach();
176 // get the next item for the givan accel letter (used by wxFrame), return
179 // if unique is not NULL, filled with TRUE if there is only one item with
180 // this accel, FALSE if two or more
181 int FindNextItemForAccel(int idxStart
,
183 bool *unique
= NULL
) const;
185 // called by wxFrame to set focus to or open the given menu
186 void SelectMenu(size_t pos
);
187 void PopupMenu(size_t pos
);
190 // find the item for the given accel and generate an event if found
191 bool ProcessAccelEvent(const wxKeyEvent
& event
);
192 #endif // wxUSE_ACCEL
194 // called by wxMenu when it is dismissed
195 void OnDismissMenu(bool dismissMenuBar
= FALSE
);
198 // common part of all ctors
202 void OnLeftDown(wxMouseEvent
& event
);
203 void OnMouseMove(wxMouseEvent
& event
);
204 void OnKeyDown(wxKeyEvent
& event
);
205 void OnKillFocus(wxFocusEvent
& event
);
207 // process the mouse move event, return TRUE if we did, FALSE to continue
208 // processing as usual
210 // the coordinates are client coordinates of menubar, convert if necessary
211 bool ProcessMouseEvent(const wxPoint
& pt
);
213 // called when the menu bar loses mouse capture - it is not hidden unlike
214 // menus, but it doesn't have modal status any longer
218 virtual void DoDraw(wxControlRenderer
*renderer
);
221 virtual wxSize
DoGetBestClientSize() const;
223 // has the menubar been created already?
224 bool IsCreated() const { return m_frameLast
!= NULL
; }
226 // "fast" version of GetMenuCount()
227 size_t GetCount() const { return m_menuInfos
.GetCount(); }
229 // get the (total) width of the specified menu
230 wxCoord
GetItemWidth(size_t pos
) const;
232 // get the rect of the item
233 wxRect
GetItemRect(size_t pos
) const;
235 // get the menu from the given point or -1 if none
236 int GetMenuFromPoint(const wxPoint
& pos
) const;
238 // refresh the given item
239 void RefreshItem(size_t pos
);
241 // refresh all items after this one (including it)
242 void RefreshAllItemsAfter(size_t pos
);
244 // hide the currently shown menu and show this one
245 void DoSelectMenu(size_t pos
);
247 // popup the currently selected menu
248 void PopupCurrentMenu(bool selectFirst
= TRUE
);
250 // hide the currently selected menu
253 // do we show a menu currently?
254 bool IsShowingMenu() const { return m_menuShown
!= 0; }
256 // we don't want to have focus except while selecting from menu
257 void GiveAwayFocus();
259 // the array containing extra menu info we need
260 wxMenuInfoArray m_menuInfos
;
262 // the current item (only used when menubar has focus)
266 // the last frame to which we were attached, NULL initially
267 wxFrame
*m_frameLast
;
269 // the currently shown menu or NULL
272 // should be showing the menu? this is subtly different from m_menuShown !=
273 // NULL as the menu which should be shown may be disabled in which case we
274 // don't show it - but will do as soon as the focus shifts to another menu
275 bool m_shouldShowMenu
;
277 // it calls out ProcessMouseEvent()
278 friend wxPopupMenuWindow
;
280 DECLARE_EVENT_TABLE()
281 DECLARE_DYNAMIC_CLASS(wxMenuBar
)
284 #endif // _WX_UNIV_MENU_H_