]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/menu.h
9d2ca80bd8f63c33d8b8c0b511def5d4b1063b85
[wxWidgets.git] / include / wx / msw / menu.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: menu.h
3 // Purpose: wxMenu, wxMenuBar classes
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin (wxMenuItem is now in separate file)
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_MENU_H_
13 #define _WX_MENU_H_
14
15 #ifdef __GNUG__
16 #pragma interface "menu.h"
17 #endif
18
19 #include "wx/defs.h"
20 #include "wx/event.h"
21 #include "wx/dynarray.h"
22 #include "wx/string.h"
23
24 #if wxUSE_ACCEL
25 #include "wx/accel.h"
26 #endif // wxUSE_ACCEL
27
28 class WXDLLEXPORT wxMenuItem;
29 class WXDLLEXPORT wxMenuBar;
30 class WXDLLEXPORT wxMenu;
31 class WXDLLEXPORT wxFrame;
32
33 WXDLLEXPORT_DATA(extern const wxChar*) wxEmptyString;
34
35 // ----------------------------------------------------------------------------
36 // Menu
37 // ----------------------------------------------------------------------------
38
39 class WXDLLEXPORT wxMenu : public wxEvtHandler
40 {
41 DECLARE_DYNAMIC_CLASS(wxMenu)
42
43 public:
44 // ctors & dtor
45 wxMenu(const wxString& title,
46 const wxFunction func)
47 {
48 Init(title, func);
49 }
50
51 wxMenu( long WXUNUSED(style) )
52 {
53 Init( wxEmptyString );
54 }
55
56 wxMenu(const wxString& title = wxEmptyString, long WXUNUSED(style) = 0)
57 {
58 Init(title);
59 }
60
61 virtual ~wxMenu();
62
63 // construct menu
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);
70 // append a submenu
71 void Append(int id, const wxString& label,
72 wxMenu *submenu,
73 const wxString& helpString = wxEmptyString);
74 // append anything (create wxMenuItem first)
75 void Append(wxMenuItem *pItem);
76
77 // insert a break in the menu
78 void Break();
79
80 // delete an item
81 // If it's a submenu, menu is not destroyed.
82 // VZ: why? shouldn't it return "wxMenu *" then?
83 void Delete(int id);
84
85 // client data
86 void SetClientData(void* clientData) { m_clientData = clientData; }
87 void* GetClientData() const { return m_clientData; }
88
89 // menu item control
90 // enable/disable item
91 void Enable(int id, bool enable);
92 // TRUE if enabled
93 bool IsEnabled(int id) const;
94
95 // check/uncheck item - only for checkable items, of course
96 void Check(int id, bool check);
97 // TRUE if checked
98 bool IsChecked(int id) const;
99
100 // other properties
101 // the menu title
102 void SetTitle(const wxString& label);
103 const wxString GetTitle() const;
104 // the item label
105 void SetLabel(int id, const wxString& label);
106 wxString GetLabel(int id) const;
107 // help string
108 virtual void SetHelpString(int id, const wxString& helpString);
109 virtual wxString GetHelpString(int id) const;
110
111 // get the list of items
112 wxList& GetItems() const { return (wxList &)m_menuItems; }
113
114 // find item
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;
121
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);
126
127 bool ProcessCommand(wxCommandEvent& event);
128
129 virtual void SetParent(wxEvtHandler *parent) { m_parent = parent; }
130 void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; }
131 wxEvtHandler *GetEventHandler() const { return m_eventHandler; }
132
133 // IMPLEMENTATION
134 bool MSWCommand(WXUINT param, WXWORD id);
135
136 void SetInvokingWindow(wxWindow *pWin) { m_pInvokingWindow = pWin; }
137 wxWindow *GetInvokingWindow() const { return m_pInvokingWindow; }
138
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;
144
145 // only for wxMenuBar
146 void Attach(wxMenuBar *menubar);
147 void Detach();
148
149 #if wxUSE_ACCEL
150 size_t GetAccelCount() const { return m_accelKeyCodes.GetCount(); }
151 size_t CopyAccels(wxAcceleratorEntry *accels) const;
152 #endif // wxUSE_ACCEL
153
154 wxFunction GetCallback() const { return m_callback; }
155 void Callback(const wxFunction func) { m_callback = func; }
156 wxFunction m_callback;
157
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); }
162
163 #endif // WXWIN_COMPATIBILITY
164
165 private:
166 // common part of all ctors
167 void Init(const wxString& title, const wxFunction func = NULL );
168
169 bool m_doBreak;
170
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
176 WXHMENU m_hMenu;
177
178 int m_noItems;
179 wxString m_title;
180 wxMenu * m_topLevelMenu;
181 wxMenuBar * m_menuBar;
182 wxList m_menuItems;
183 wxEvtHandler * m_parent;
184 wxEvtHandler * m_eventHandler;
185 wxWindow *m_pInvokingWindow;
186 void* m_clientData;
187
188 #if wxUSE_ACCEL
189 // the accelerators data
190 wxArrayInt m_accelKeyCodes, m_accelFlags, m_accelIds;
191 #endif // wxUSE_ACCEL
192 };
193
194 // ----------------------------------------------------------------------------
195 // Menu Bar (a la Windows)
196 // ----------------------------------------------------------------------------
197
198 class WXDLLEXPORT wxMenuBar : public wxEvtHandler
199 {
200 DECLARE_DYNAMIC_CLASS(wxMenuBar)
201
202 public:
203 // ctors & dtor
204 // default constructor
205 wxMenuBar();
206 // unused under MSW
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();
211
212 // menubar construction
213 WXHMENU Create();
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);
218 void Detach();
219 virtual void Delete(wxMenu *menu, int index = 0); /* Menu not destroyed */
220
221 // state control
222 // NB: must only be used AFTER menu has been attached to frame,
223 // otherwise use individual menus to enable/disable items
224 // enable the item
225 void Enable(int id, bool enable);
226 // TRUE if item enabled
227 bool IsEnabled(int id) const;
228 //
229 void EnableTop(int pos, bool enable);
230
231 // works only with checkable items
232 void Check(int id, bool check);
233 // TRUE if checked
234 bool IsChecked(int id) const;
235
236 void SetLabel(int id, const wxString& label) ;
237 wxString GetLabel(int id) const ;
238
239 virtual void SetHelpString(int id, const wxString& helpString);
240 virtual wxString GetHelpString(int id) const ;
241
242 void SetLabelTop(int pos, const wxString& label) ;
243 wxString GetLabelTop(int pos) const ;
244
245 // notifications: return FALSE to prevent the menu from being
246 // appended/deleted
247 virtual bool OnAppend(wxMenu *menu, const wxChar *title);
248 virtual bool OnDelete(wxMenu *menu, int index);
249
250 // item search
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;
258
259 // submenus access
260 int GetMenuCount() const { return m_menuCount; }
261 wxMenu *GetMenu(int i) const { return m_menus[i]; }
262
263 void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; }
264 wxEvtHandler *GetEventHandler() { return m_eventHandler; }
265
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
271
272 // IMPLEMENTATION
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; }
277 // attach to a frame
278 void Attach(wxFrame *frame);
279
280 #if wxUSE_ACCEL
281 // get the accel table for the menus
282 const wxAcceleratorTable& GetAccelTable() const { return m_accelTable; }
283 #endif // wxUSE_ACCEL
284
285 // get the menu handle
286 WXHMENU GetHMenu() const { return m_hMenu; }
287
288 // if the menubar is modified, the display is not updated automatically,
289 // call this function to update it (m_menuBarFrame should be !NULL)
290 void Refresh();
291
292 protected:
293 // common part of all ctors
294 void Init();
295
296 wxEvtHandler *m_eventHandler;
297 int m_menuCount;
298 wxMenu **m_menus;
299 wxString *m_titles;
300 wxFrame *m_menuBarFrame;
301 WXHMENU m_hMenu;
302
303 #if wxUSE_ACCEL
304 // the accelerator table for all accelerators in all our menus
305 wxAcceleratorTable m_accelTable;
306 #endif // wxUSE_ACCEL
307 };
308
309 #endif // _WX_MENU_H_