]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/menu.h
1. small dnd compilation fixes (no attempt to make icon setting work though)
[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 WX_DEFINE_ARRAY(wxAcceleratorEntry *, wxAcceleratorArray);
36
37 // ----------------------------------------------------------------------------
38 // Menu
39 // ----------------------------------------------------------------------------
40
41 class WXDLLEXPORT wxMenu : public wxEvtHandler
42 {
43 DECLARE_DYNAMIC_CLASS(wxMenu)
44
45 public:
46 // ctors & dtor
47 wxMenu(const wxString& title,
48 const wxFunction func)
49 {
50 Init(title, func);
51 }
52
53 wxMenu( long WXUNUSED(style) )
54 {
55 Init( wxEmptyString );
56 }
57
58 wxMenu(const wxString& title = wxEmptyString, long WXUNUSED(style) = 0)
59 {
60 Init(title);
61 }
62
63 virtual ~wxMenu();
64
65 // construct menu
66 // append a separator to the menu
67 void AppendSeparator();
68 // append a normal item to the menu
69 void Append(int id, const wxString& label,
70 const wxString& helpString = wxEmptyString,
71 bool checkable = FALSE);
72 // append a submenu
73 void Append(int id, const wxString& label,
74 wxMenu *submenu,
75 const wxString& helpString = wxEmptyString);
76 // append anything (create wxMenuItem first)
77 void Append(wxMenuItem *pItem);
78
79 // insert a break in the menu
80 void Break();
81
82 // delete an item
83 // If it's a submenu, menu is not destroyed.
84 // VZ: why? shouldn't it return "wxMenu *" then?
85 void Delete(int id);
86
87 // client data
88 void SetClientData(void* clientData) { m_clientData = clientData; }
89 void* GetClientData() const { return m_clientData; }
90
91 // menu item control
92 // enable/disable item
93 void Enable(int id, bool enable);
94 // TRUE if enabled
95 bool IsEnabled(int id) const;
96
97 // check/uncheck item - only for checkable items, of course
98 void Check(int id, bool check);
99 // TRUE if checked
100 bool IsChecked(int id) const;
101
102 // other properties
103 // the menu title
104 void SetTitle(const wxString& label);
105 const wxString GetTitle() const;
106 // the item label
107 void SetLabel(int id, const wxString& label);
108 wxString GetLabel(int id) const;
109 // help string
110 virtual void SetHelpString(int id, const wxString& helpString);
111 virtual wxString GetHelpString(int id) const;
112
113 // get the list of items
114 wxList& GetItems() const { return (wxList &)m_menuItems; }
115
116 // find item
117 // returns id of the item matching the given string or wxNOT_FOUND
118 virtual int FindItem(const wxString& itemString) const;
119 // returns NULL if not found
120 wxMenuItem* FindItem(int id) const { return FindItemForId(id); }
121 // find wxMenuItem by ID, and item's menu too if itemMenu is !NULL
122 wxMenuItem *FindItemForId(int itemId, wxMenu **itemMenu = NULL) const;
123
124 // Updates the UI for a menu and all submenus recursively. source is the
125 // object that has the update event handlers defined for it. If NULL, the
126 // menu or associated window will be used.
127 void UpdateUI(wxEvtHandler* source = (wxEvtHandler*)NULL);
128
129 bool ProcessCommand(wxCommandEvent& event);
130
131 virtual void SetParent(wxEvtHandler *parent) { m_parent = parent; }
132 void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; }
133 wxEvtHandler *GetEventHandler() const { return m_eventHandler; }
134
135 // IMPLEMENTATION
136 bool MSWCommand(WXUINT param, WXWORD id);
137
138 void SetInvokingWindow(wxWindow *pWin) { m_pInvokingWindow = pWin; }
139 wxWindow *GetInvokingWindow() const { return m_pInvokingWindow; }
140
141 // semi-private accessors
142 // get the window which contains this menu
143 wxWindow *GetWindow() const;
144 // get the menu handle
145 WXHMENU GetHMenu() const;
146
147 // only for wxMenuBar
148 void Attach(wxMenuBar *menubar);
149 void Detach();
150
151 #if wxUSE_ACCEL
152 size_t GetAccelCount() const { return m_accels.GetCount(); }
153 size_t CopyAccels(wxAcceleratorEntry *accels) const;
154 #endif // wxUSE_ACCEL
155
156 wxFunction GetCallback() const { return m_callback; }
157 void Callback(const wxFunction func) { m_callback = func; }
158 wxFunction m_callback;
159
160 #ifdef WXWIN_COMPATIBILITY
161 // compatibility: these functions are deprecated
162 bool Enabled(int id) const { return IsEnabled(id); }
163 bool Checked(int id) const { return IsChecked(id); }
164
165 #endif // WXWIN_COMPATIBILITY
166
167 private:
168 // common part of all ctors
169 void Init(const wxString& title, const wxFunction func = NULL );
170
171 bool m_doBreak;
172
173 // This is used when m_hMenu is NULL because we don't want to
174 // delete it in ~wxMenu (it's been added to a parent menu).
175 // But we'll still need the handle for other purposes.
176 // Might be better to have a flag saying whether it's deleteable or not.
177 WXHMENU m_savehMenu ; // Used for Enable() on popup
178 WXHMENU m_hMenu;
179
180 int m_noItems;
181 wxString m_title;
182 wxMenu * m_topLevelMenu;
183 wxMenuBar * m_menuBar;
184 wxList m_menuItems;
185 wxEvtHandler * m_parent;
186 wxEvtHandler * m_eventHandler;
187 wxWindow *m_pInvokingWindow;
188 void* m_clientData;
189
190 #if wxUSE_ACCEL
191 // the accelerators for our menu items
192 wxAcceleratorArray m_accels;
193 #endif // wxUSE_ACCEL
194 };
195
196 // ----------------------------------------------------------------------------
197 // Menu Bar (a la Windows)
198 // ----------------------------------------------------------------------------
199
200 class WXDLLEXPORT wxMenuBar : public wxEvtHandler
201 {
202 DECLARE_DYNAMIC_CLASS(wxMenuBar)
203
204 public:
205 // ctors & dtor
206 // default constructor
207 wxMenuBar();
208 // unused under MSW
209 wxMenuBar(long style);
210 // menubar takes ownership of the menus arrays but copies the titles
211 wxMenuBar(int n, wxMenu *menus[], const wxString titles[]);
212 virtual ~wxMenuBar();
213
214 // menubar construction
215 WXHMENU Create();
216 void Append(wxMenu *menu, const wxString& title);
217 void Insert(int pos, wxMenu * menu, const wxString& title);
218 void ReplaceMenu(int pos, wxMenu * new_menu, const wxString& title);
219 int FindMenu(const wxString& title);
220 void Detach();
221 virtual void Delete(wxMenu *menu, int index = 0); /* Menu not destroyed */
222
223 // state control
224 // NB: must only be used AFTER menu has been attached to frame,
225 // otherwise use individual menus to enable/disable items
226 // enable the item
227 void Enable(int id, bool enable);
228 // TRUE if item enabled
229 bool IsEnabled(int id) const;
230 //
231 void EnableTop(int pos, bool enable);
232
233 // works only with checkable items
234 void Check(int id, bool check);
235 // TRUE if checked
236 bool IsChecked(int id) const;
237
238 void SetLabel(int id, const wxString& label) ;
239 wxString GetLabel(int id) const ;
240
241 virtual void SetHelpString(int id, const wxString& helpString);
242 virtual wxString GetHelpString(int id) const ;
243
244 void SetLabelTop(int pos, const wxString& label) ;
245 wxString GetLabelTop(int pos) const ;
246
247 // notifications: return FALSE to prevent the menu from being
248 // appended/deleted
249 virtual bool OnAppend(wxMenu *menu, const wxChar *title);
250 virtual bool OnDelete(wxMenu *menu, int index);
251
252 // item search
253 // by menu and item names, returns wxNOT_FOUND if not found
254 virtual int FindMenuItem(const wxString& menuString,
255 const wxString& itemString) const;
256 // returns NULL if not found
257 wxMenuItem* FindItem(int id) const { return FindItemForId(id); }
258 // returns NULL if not found, fills menuForItem if !NULL
259 wxMenuItem *FindItemForId(int itemId, wxMenu **menuForItem = NULL) const;
260
261 // submenus access
262 int GetMenuCount() const { return m_menuCount; }
263 wxMenu *GetMenu(int i) const { return m_menus[i]; }
264
265 void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; }
266 wxEvtHandler *GetEventHandler() { return m_eventHandler; }
267
268 #ifdef WXWIN_COMPATIBILITY
269 // compatibility: these functions are deprecated
270 bool Enabled(int id) const { return IsEnabled(id); }
271 bool Checked(int id) const { return IsChecked(id); }
272 #endif // WXWIN_COMPATIBILITY
273
274 // IMPLEMENTATION
275 // returns TRUE if we're attached to a frame
276 bool IsAttached() const { return m_menuBarFrame != NULL; }
277 // get the frame we live in
278 wxFrame *GetFrame() const { return m_menuBarFrame; }
279 // attach to a frame
280 void Attach(wxFrame *frame);
281
282 #if wxUSE_ACCEL
283 // get the accel table for the menus
284 const wxAcceleratorTable& GetAccelTable() const { return m_accelTable; }
285 #endif // wxUSE_ACCEL
286
287 // get the menu handle
288 WXHMENU GetHMenu() const { return m_hMenu; }
289
290 // if the menubar is modified, the display is not updated automatically,
291 // call this function to update it (m_menuBarFrame should be !NULL)
292 void Refresh();
293
294 protected:
295 // common part of all ctors
296 void Init();
297
298 wxEvtHandler *m_eventHandler;
299 int m_menuCount;
300 wxMenu **m_menus;
301 wxString *m_titles;
302 wxFrame *m_menuBarFrame;
303 WXHMENU m_hMenu;
304
305 #if wxUSE_ACCEL
306 // the accelerator table for all accelerators in all our menus
307 wxAcceleratorTable m_accelTable;
308 #endif // wxUSE_ACCEL
309 };
310
311 #endif // _WX_MENU_H_