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