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