1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar classes
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin (wxMenuItem is now in separate file)
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "menu.h"
21 #include "wx/dynarray.h"
22 #include "wx/string.h"
28 class WXDLLEXPORT wxMenuItem
;
29 class WXDLLEXPORT wxMenuBar
;
30 class WXDLLEXPORT wxMenu
;
31 class WXDLLEXPORT wxFrame
;
33 WXDLLEXPORT_DATA(extern const wxChar
*) wxEmptyString
;
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 class WXDLLEXPORT wxMenu
: public wxEvtHandler
41 DECLARE_DYNAMIC_CLASS(wxMenu
)
45 wxMenu(const wxString
& title
,
46 const wxFunction func
)
51 wxMenu( long WXUNUSED(style
) )
53 Init( wxEmptyString
);
56 wxMenu(const wxString
& title
= wxEmptyString
, long WXUNUSED(style
) = 0)
64 // append a separator to the menu
65 void AppendSeparator();
66 // append a normal item to the menu
67 void Append(int id
, const wxString
& label
,
68 const wxString
& helpString
= wxEmptyString
,
69 bool checkable
= FALSE
);
71 void Append(int id
, const wxString
& label
,
73 const wxString
& helpString
= wxEmptyString
);
74 // append anything (create wxMenuItem first)
75 void Append(wxMenuItem
*pItem
);
77 // insert a break in the menu
81 // If it's a submenu, menu is not destroyed.
82 // VZ: why? shouldn't it return "wxMenu *" then?
86 void SetClientData(void* clientData
) { m_clientData
= clientData
; }
87 void* GetClientData() const { return m_clientData
; }
90 // enable/disable item
91 void Enable(int id
, bool enable
);
93 bool IsEnabled(int id
) const;
95 // check/uncheck item - only for checkable items, of course
96 void Check(int id
, bool check
);
98 bool IsChecked(int id
) const;
102 void SetTitle(const wxString
& label
);
103 const wxString
GetTitle() const;
105 void SetLabel(int id
, const wxString
& label
);
106 wxString
GetLabel(int id
) const;
108 virtual void SetHelpString(int id
, const wxString
& helpString
);
109 virtual wxString
GetHelpString(int id
) const;
111 // get the list of items
112 wxList
& GetItems() const { return (wxList
&)m_menuItems
; }
115 // returns id of the item matching the given string or wxNOT_FOUND
116 virtual int FindItem(const wxString
& itemString
) const;
117 // returns NULL if not found
118 wxMenuItem
* FindItem(int id
) const { return FindItemForId(id
); }
119 // find wxMenuItem by ID, and item's menu too if itemMenu is !NULL
120 wxMenuItem
*FindItemForId(int itemId
, wxMenu
**itemMenu
= NULL
) const;
122 // Updates the UI for a menu and all submenus recursively. source is the
123 // object that has the update event handlers defined for it. If NULL, the
124 // menu or associated window will be used.
125 void UpdateUI(wxEvtHandler
* source
= (wxEvtHandler
*)NULL
);
127 bool ProcessCommand(wxCommandEvent
& event
);
129 virtual void SetParent(wxEvtHandler
*parent
) { m_parent
= parent
; }
130 void SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
131 wxEvtHandler
*GetEventHandler() const { return m_eventHandler
; }
134 bool MSWCommand(WXUINT param
, WXWORD id
);
136 void SetInvokingWindow(wxWindow
*pWin
) { m_pInvokingWindow
= pWin
; }
137 wxWindow
*GetInvokingWindow() const { return m_pInvokingWindow
; }
139 // semi-private accessors
140 // get the window which contains this menu
141 wxWindow
*GetWindow() const;
142 // get the menu handle
143 WXHMENU
GetHMenu() const;
145 // only for wxMenuBar
146 void Attach(wxMenuBar
*menubar
);
150 size_t GetAccelCount() const { return m_accelKeyCodes
.GetCount(); }
151 size_t CopyAccels(wxAcceleratorEntry
*accels
) const;
152 #endif // wxUSE_ACCEL
154 wxFunction
GetCallback() const { return m_callback
; }
155 void Callback(const wxFunction func
) { m_callback
= func
; }
156 wxFunction m_callback
;
158 #ifdef WXWIN_COMPATIBILITY
159 // compatibility: these functions are deprecated
160 bool Enabled(int id
) const { return IsEnabled(id
); }
161 bool Checked(int id
) const { return IsChecked(id
); }
163 #endif // WXWIN_COMPATIBILITY
166 // common part of all ctors
167 void Init(const wxString
& title
, const wxFunction func
= NULL
);
171 // This is used when m_hMenu is NULL because we don't want to
172 // delete it in ~wxMenu (it's been added to a parent menu).
173 // But we'll still need the handle for other purposes.
174 // Might be better to have a flag saying whether it's deleteable or not.
175 WXHMENU m_savehMenu
; // Used for Enable() on popup
180 wxMenu
* m_topLevelMenu
;
181 wxMenuBar
* m_menuBar
;
183 wxEvtHandler
* m_parent
;
184 wxEvtHandler
* m_eventHandler
;
185 wxWindow
*m_pInvokingWindow
;
189 // the accelerators data
190 wxArrayInt m_accelKeyCodes
, m_accelFlags
, m_accelIds
;
191 #endif // wxUSE_ACCEL
194 // ----------------------------------------------------------------------------
195 // Menu Bar (a la Windows)
196 // ----------------------------------------------------------------------------
198 class WXDLLEXPORT wxMenuBar
: public wxEvtHandler
200 DECLARE_DYNAMIC_CLASS(wxMenuBar
)
204 // default constructor
207 wxMenuBar(long style
);
208 // menubar takes ownership of the menus arrays but copies the titles
209 wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[]);
210 virtual ~wxMenuBar();
212 // menubar construction
214 void Append(wxMenu
*menu
, const wxString
& title
);
215 void Insert(int pos
, wxMenu
* menu
, const wxString
& title
);
216 void ReplaceMenu(int pos
, wxMenu
* new_menu
, const wxString
& title
);
217 int FindMenu(const wxString
& title
);
219 virtual void Delete(wxMenu
*menu
, int index
= 0); /* Menu not destroyed */
222 // NB: must only be used AFTER menu has been attached to frame,
223 // otherwise use individual menus to enable/disable items
225 void Enable(int id
, bool enable
);
226 // TRUE if item enabled
227 bool IsEnabled(int id
) const;
229 void EnableTop(int pos
, bool enable
);
231 // works only with checkable items
232 void Check(int id
, bool check
);
234 bool IsChecked(int id
) const;
236 void SetLabel(int id
, const wxString
& label
) ;
237 wxString
GetLabel(int id
) const ;
239 virtual void SetHelpString(int id
, const wxString
& helpString
);
240 virtual wxString
GetHelpString(int id
) const ;
242 void SetLabelTop(int pos
, const wxString
& label
) ;
243 wxString
GetLabelTop(int pos
) const ;
245 // notifications: return FALSE to prevent the menu from being
247 virtual bool OnAppend(wxMenu
*menu
, const wxChar
*title
);
248 virtual bool OnDelete(wxMenu
*menu
, int index
);
251 // by menu and item names, returns wxNOT_FOUND if not found
252 virtual int FindMenuItem(const wxString
& menuString
,
253 const wxString
& itemString
) const;
254 // returns NULL if not found
255 wxMenuItem
* FindItem(int id
) const { return FindItemForId(id
); }
256 // returns NULL if not found, fills menuForItem if !NULL
257 wxMenuItem
*FindItemForId(int itemId
, wxMenu
**menuForItem
= NULL
) const;
260 int GetMenuCount() const { return m_menuCount
; }
261 wxMenu
*GetMenu(int i
) const { return m_menus
[i
]; }
263 void SetEventHandler(wxEvtHandler
*handler
) { m_eventHandler
= handler
; }
264 wxEvtHandler
*GetEventHandler() { return m_eventHandler
; }
266 #ifdef WXWIN_COMPATIBILITY
267 // compatibility: these functions are deprecated
268 bool Enabled(int id
) const { return IsEnabled(id
); }
269 bool Checked(int id
) const { return IsChecked(id
); }
270 #endif // WXWIN_COMPATIBILITY
273 // returns TRUE if we're attached to a frame
274 bool IsAttached() const { return m_menuBarFrame
!= NULL
; }
275 // get the frame we live in
276 wxFrame
*GetFrame() const { return m_menuBarFrame
; }
278 void Attach(wxFrame
*frame
);
281 // get the accel table for the menus
282 const wxAcceleratorTable
& GetAccelTable() const { return m_accelTable
; }
283 #endif // wxUSE_ACCEL
285 // get the menu handle
286 WXHMENU
GetHMenu() const { return m_hMenu
; }
288 // if the menubar is modified, the display is not updated automatically,
289 // call this function to update it (m_menuBarFrame should be !NULL)
293 // common part of all ctors
296 wxEvtHandler
*m_eventHandler
;
300 wxFrame
*m_menuBarFrame
;
304 // the accelerator table for all accelerators in all our menus
305 wxAcceleratorTable m_accelTable
;
306 #endif // wxUSE_ACCEL
309 #endif // _WX_MENU_H_