]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/menu.h
Small optical changes for MSW
[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 virtual void Delete(wxMenu *menu, int index = 0); /* Menu not destroyed */
216
217 // state control
218 // NB: must only be used AFTER menu has been attached to frame,
219 // otherwise use individual menus to enable/disable items
220 // enable the item
221 void Enable(int id, bool enable);
222 // TRUE if item enabled
223 bool IsEnabled(int id) const;
224 //
225 void EnableTop(int pos, bool enable);
226
227 // works only with checkable items
228 void Check(int id, bool check);
229 // TRUE if checked
230 bool IsChecked(int id) const;
231
232 void SetLabel(int id, const wxString& label) ;
233 wxString GetLabel(int id) const ;
234
235 virtual void SetHelpString(int id, const wxString& helpString);
236 virtual wxString GetHelpString(int id) const ;
237
238 void SetLabelTop(int pos, const wxString& label) ;
239 wxString GetLabelTop(int pos) const ;
240
241 // notifications: return FALSE to prevent the menu from being
242 // appended/deleted
243 virtual bool OnAppend(wxMenu *menu, const wxChar *title);
244 virtual bool OnDelete(wxMenu *menu, int index);
245
246 // item search
247 // by menu and item names, returns wxNOT_FOUND if not found
248 virtual int FindMenuItem(const wxString& menuString,
249 const wxString& itemString) const;
250 // returns NULL if not found
251 wxMenuItem* FindItem(int id) const { return FindItemForId(id); }
252 // returns NULL if not found, fills menuForItem if !NULL
253 wxMenuItem *FindItemForId(int itemId, wxMenu **menuForItem = NULL) const;
254
255 // submenus access
256 int GetMenuCount() const { return m_menuCount; }
257 wxMenu *GetMenu(int i) const { return m_menus[i]; }
258
259 void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; }
260 wxEvtHandler *GetEventHandler() { return m_eventHandler; }
261
262 #ifdef WXWIN_COMPATIBILITY
263 // compatibility: these functions are deprecated
264 bool Enabled(int id) const { return IsEnabled(id); }
265 bool Checked(int id) const { return IsChecked(id); }
266 #endif // WXWIN_COMPATIBILITY
267
268 // IMPLEMENTATION
269 // returns TRUE if we're attached to a frame
270 bool IsAttached() const { return m_menuBarFrame != NULL; }
271 // get the frame we live in
272 wxFrame *GetFrame() const { return m_menuBarFrame; }
273 // attach to a frame
274 void Attach(wxFrame *frame);
275
276 #if wxUSE_ACCEL
277 // get the accel table for the menus
278 const wxAcceleratorTable& GetAccelTable() const { return m_accelTable; }
279 #endif // wxUSE_ACCEL
280
281 // get the menu handle
282 WXHMENU GetHMenu() const { return m_hMenu; }
283
284 protected:
285 // common part of all ctors
286 void Init();
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 wxEvtHandler *m_eventHandler;
293 int m_menuCount;
294 wxMenu **m_menus;
295 wxString *m_titles;
296 wxFrame *m_menuBarFrame;
297 WXHMENU m_hMenu;
298
299 #if wxUSE_ACCEL
300 // the accelerator table for all accelerators in all our menus
301 wxAcceleratorTable m_accelTable;
302 #endif // wxUSE_ACCEL
303 };
304
305 #endif // _WX_MENU_H_