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