1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/univ/menu.h
3 // Purpose: wxMenu and wxMenuBar classes for wxUniversal
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_UNIV_MENU_H_
13 #define _WX_UNIV_MENU_H_
19 #include "wx/dynarray.h"
22 class WXDLLIMPEXP_FWD_CORE wxMenuInfo
;
23 WX_DECLARE_EXPORTED_OBJARRAY(wxMenuInfo
, wxMenuInfoArray
);
25 class WXDLLIMPEXP_FWD_CORE wxMenuGeometryInfo
;
26 class WXDLLIMPEXP_FWD_CORE wxPopupMenuWindow
;
27 class WXDLLIMPEXP_FWD_CORE wxRenderer
;
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 class WXDLLEXPORT wxMenu
: public wxMenuBase
37 wxMenu(const wxString
& title
, long style
= 0)
38 : wxMenuBase(title
, style
) { Init(); }
40 wxMenu(long style
= 0) : wxMenuBase(style
) { Init(); }
44 // called by wxMenuItem when an item of this menu changes
45 void RefreshItem(wxMenuItem
*item
);
47 // does the menu have any items?
48 bool IsEmpty() const { return !GetMenuItems().GetFirst(); }
50 // show this menu at the given position (in screen coords) and optionally
51 // select its first item
52 void Popup(const wxPoint
& pos
, const wxSize
& size
,
53 bool selectFirst
= true);
58 // override the base class methods to connect/disconnect event handlers
59 virtual void Attach(wxMenuBarBase
*menubar
);
60 virtual void Detach();
62 // implementation only from here
64 // do as if this item were clicked, return true if the resulting event was
65 // processed, false otherwise
66 bool ClickItem(wxMenuItem
*item
);
68 // process the key event, return true if done
69 bool ProcessKeyDown(int key
);
72 // find the item for the given accel and generate an event if found
73 bool ProcessAccelEvent(const wxKeyEvent
& event
);
77 // implement base class virtuals
78 virtual wxMenuItem
* DoAppend(wxMenuItem
*item
);
79 virtual wxMenuItem
* DoInsert(size_t pos
, wxMenuItem
*item
);
80 virtual wxMenuItem
* DoRemove(wxMenuItem
*item
);
82 // common part of DoAppend and DoInsert
83 void OnItemAdded(wxMenuItem
*item
);
85 // called by wxPopupMenuWindow when the window is hidden
86 void OnDismiss(bool dismissParent
);
88 // return true if the menu is currently shown on screen
91 // get the menu geometry info
92 const wxMenuGeometryInfo
& GetGeometryInfo() const;
94 // forget old menu geometry info
95 void InvalidateGeometryInfo();
97 // return either the menubar or the invoking window, normally never NULL
98 wxWindow
*GetRootWindow() const;
100 // get the renderer we use for drawing: either the one of the menu bar or
101 // the one of the window if we're a popup menu
102 wxRenderer
*GetRenderer() const;
105 // add/remove accel for the given menu item
106 void AddAccelFor(wxMenuItem
*item
);
107 void RemoveAccelFor(wxMenuItem
*item
);
108 #endif // wxUSE_ACCEL
111 // common part of all ctors
114 // terminate the current radio group, if any
115 void EndRadioGroup();
117 // the exact menu geometry is defined by a struct derived from this one
118 // which is opaque and defined by the renderer
119 wxMenuGeometryInfo
*m_geometry
;
121 // the menu shown on screen or NULL if not currently shown
122 wxPopupMenuWindow
*m_popupMenu
;
125 // the accel table for this menu
126 wxAcceleratorTable m_accelTable
;
127 #endif // wxUSE_ACCEL
129 // the position of the first item in the current radio group or -1
130 int m_startRadioGroup
;
132 // it calls out OnDismiss()
133 friend class wxPopupMenuWindow
;
134 DECLARE_DYNAMIC_CLASS(wxMenu
)
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 class WXDLLEXPORT wxMenuBar
: public wxMenuBarBase
145 wxMenuBar(long WXUNUSED(style
) = 0) { Init(); }
146 wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
= 0);
147 virtual ~wxMenuBar();
149 // implement base class virtuals
150 virtual bool Append( wxMenu
*menu
, const wxString
&title
);
151 virtual bool Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
);
152 virtual wxMenu
*Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
);
153 virtual wxMenu
*Remove(size_t pos
);
155 virtual void EnableTop(size_t pos
, bool enable
);
156 virtual bool IsEnabledTop(size_t pos
) const;
158 virtual void SetLabelTop(size_t pos
, const wxString
& label
);
159 virtual wxString
GetLabelTop(size_t pos
) const;
161 virtual void Attach(wxFrame
*frame
);
162 virtual void Detach();
164 // get the next item for the givan accel letter (used by wxFrame), return
167 // if unique is not NULL, filled with true if there is only one item with
168 // this accel, false if two or more
169 int FindNextItemForAccel(int idxStart
,
171 bool *unique
= NULL
) const;
173 // called by wxFrame to set focus to or open the given menu
174 void SelectMenu(size_t pos
);
175 void PopupMenu(size_t pos
);
178 // find the item for the given accel and generate an event if found
179 bool ProcessAccelEvent(const wxKeyEvent
& event
);
180 #endif // wxUSE_ACCEL
182 // called by wxMenu when it is dismissed
183 void OnDismissMenu(bool dismissMenuBar
= false);
186 // common part of all ctors
190 void OnLeftDown(wxMouseEvent
& event
);
191 void OnMouseMove(wxMouseEvent
& event
);
192 void OnKeyDown(wxKeyEvent
& event
);
193 void OnKillFocus(wxFocusEvent
& event
);
195 // process the mouse move event, return true if we did, false to continue
196 // processing as usual
198 // the coordinates are client coordinates of menubar, convert if necessary
199 bool ProcessMouseEvent(const wxPoint
& pt
);
201 // called when the menu bar loses mouse capture - it is not hidden unlike
202 // menus, but it doesn't have modal status any longer
206 virtual void DoDraw(wxControlRenderer
*renderer
);
209 virtual wxSize
DoGetBestClientSize() const;
211 // has the menubar been created already?
212 bool IsCreated() const { return m_frameLast
!= NULL
; }
214 // "fast" version of GetMenuCount()
215 size_t GetCount() const { return m_menuInfos
.GetCount(); }
217 // get the (total) width of the specified menu
218 wxCoord
GetItemWidth(size_t pos
) const;
220 // get the rect of the item
221 wxRect
GetItemRect(size_t pos
) const;
223 // get the menu from the given point or -1 if none
224 int GetMenuFromPoint(const wxPoint
& pos
) const;
226 // refresh the given item
227 void RefreshItem(size_t pos
);
229 // refresh all items after this one (including it)
230 void RefreshAllItemsAfter(size_t pos
);
232 // hide the currently shown menu and show this one
233 void DoSelectMenu(size_t pos
);
235 // popup the currently selected menu
236 void PopupCurrentMenu(bool selectFirst
= true);
238 // hide the currently selected menu
241 // do we show a menu currently?
242 bool IsShowingMenu() const { return m_menuShown
!= 0; }
244 // we don't want to have focus except while selecting from menu
245 void GiveAwayFocus();
247 // Release the mouse capture if we have it
248 bool ReleaseMouseCapture();
250 // the array containing extra menu info we need
251 wxMenuInfoArray m_menuInfos
;
253 // the current item (only used when menubar has focus)
257 // the last frame to which we were attached, NULL initially
258 wxFrame
*m_frameLast
;
260 // the currently shown menu or NULL
263 // should be showing the menu? this is subtly different from m_menuShown !=
264 // NULL as the menu which should be shown may be disabled in which case we
265 // don't show it - but will do as soon as the focus shifts to another menu
266 bool m_shouldShowMenu
;
268 // it calls out ProcessMouseEvent()
269 friend class wxPopupMenuWindow
;
271 DECLARE_EVENT_TABLE()
272 DECLARE_DYNAMIC_CLASS(wxMenuBar
)
275 #endif // _WX_UNIV_MENU_H_