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