]>
Commit | Line | Data |
---|---|---|
717a57c2 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: samples/menu.cpp | |
3 | // Purpose: wxMenu/wxMenuBar sample | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 01.11.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999 Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx/wx.h". | |
92a19c2e | 21 | #include "wx/wxprec.h" |
717a57c2 VZ |
22 | |
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
f91bf72f GD |
28 | #include "wx/app.h" |
29 | #include "wx/frame.h" | |
30 | #include "wx/menu.h" | |
31 | #include "wx/msgdlg.h" | |
32 | #include "wx/log.h" | |
33 | #include "wx/textctrl.h" | |
5c7766de | 34 | #include "wx/textdlg.h" |
717a57c2 VZ |
35 | #endif |
36 | ||
6d5b2a57 VZ |
37 | #if !wxUSE_MENUS |
38 | // nice try... | |
39 | #error "menu sample requires wxUSE_MENUS=1" | |
40 | #endif // wxUSE_MENUS | |
41 | ||
62ad15a5 VZ |
42 | // not all ports have support for EVT_CONTEXT_MENU yet, don't define |
43 | // USE_CONTEXT_MENU for those which don't | |
ba8eb933 | 44 | #if defined(__WXMOTIF__) || defined(__WXPM__) |
51a9eb73 VZ |
45 | #define USE_CONTEXT_MENU 0 |
46 | #else | |
47 | #define USE_CONTEXT_MENU 1 | |
62ad15a5 VZ |
48 | #endif |
49 | ||
1242c2d9 WS |
50 | // this sample is usefull when new port is developed |
51 | // and usually new port has majority of flags turned off | |
52 | #if wxUSE_LOG && wxUSE_TEXTCTRL | |
53 | #define USE_LOG_WINDOW 1 | |
54 | #else | |
55 | #define USE_LOG_WINDOW 0 | |
56 | #endif | |
57 | ||
fcaf9e70 RR |
58 | #include "copy.xpm" |
59 | ||
717a57c2 VZ |
60 | // ---------------------------------------------------------------------------- |
61 | // classes | |
62 | // ---------------------------------------------------------------------------- | |
63 | ||
64 | // Define a new application | |
65 | class MyApp: public wxApp | |
66 | { | |
67 | public: | |
68 | bool OnInit(); | |
69 | }; | |
70 | ||
71 | // Define a new frame | |
72 | class MyFrame: public wxFrame | |
73 | { | |
74 | public: | |
75 | MyFrame(); | |
76 | ||
3ca6a5f0 BP |
77 | virtual ~MyFrame(); |
78 | ||
79 | void LogMenuEvent(const wxCommandEvent& event); | |
717a57c2 | 80 | |
e421922f | 81 | protected: |
717a57c2 | 82 | void OnQuit(wxCommandEvent& event); |
1242c2d9 | 83 | #if USE_LOG_WINDOW |
e421922f | 84 | void OnClearLog(wxCommandEvent& event); |
1242c2d9 | 85 | #endif |
e421922f | 86 | |
717a57c2 VZ |
87 | void OnAbout(wxCommandEvent& event); |
88 | ||
89 | void OnDummy(wxCommandEvent& event); | |
90 | ||
91 | void OnAppendMenuItem(wxCommandEvent& event); | |
92 | void OnAppendSubMenu(wxCommandEvent& event); | |
93 | void OnDeleteMenuItem(wxCommandEvent& event); | |
94 | void OnInsertMenuItem(wxCommandEvent& event); | |
95 | void OnCheckMenuItem(wxCommandEvent& event); | |
96 | void OnEnableMenuItem(wxCommandEvent& event); | |
97 | void OnGetLabelMenuItem(wxCommandEvent& event); | |
1242c2d9 | 98 | #if wxUSE_TEXTDLG |
717a57c2 | 99 | void OnSetLabelMenuItem(wxCommandEvent& event); |
1242c2d9 | 100 | #endif |
a80c322c | 101 | void OnGetMenuItemInfo(wxCommandEvent& event); |
1242c2d9 | 102 | #if wxUSE_TEXTDLG |
f6d90fb9 | 103 | void OnFindMenuItem(wxCommandEvent& event); |
1242c2d9 | 104 | #endif |
717a57c2 VZ |
105 | |
106 | void OnAppendMenu(wxCommandEvent& event); | |
f03ec224 | 107 | void OnInsertMenu(wxCommandEvent& event); |
717a57c2 VZ |
108 | void OnDeleteMenu(wxCommandEvent& event); |
109 | void OnToggleMenu(wxCommandEvent& event); | |
110 | void OnEnableMenu(wxCommandEvent& event); | |
111 | void OnGetLabelMenu(wxCommandEvent& event); | |
112 | void OnSetLabelMenu(wxCommandEvent& event); | |
1242c2d9 | 113 | #if wxUSE_TEXTDLG |
f6d90fb9 | 114 | void OnFindMenu(wxCommandEvent& event); |
1242c2d9 | 115 | #endif |
717a57c2 | 116 | |
d65c269b VZ |
117 | void OnTestNormal(wxCommandEvent& event); |
118 | void OnTestCheck(wxCommandEvent& event); | |
119 | void OnTestRadio(wxCommandEvent& event); | |
120 | ||
c377f1be JS |
121 | void OnUpdateSubMenuNormal(wxUpdateUIEvent& event); |
122 | void OnUpdateSubMenuCheck(wxUpdateUIEvent& event); | |
123 | void OnUpdateSubMenuRadio(wxUpdateUIEvent& event); | |
124 | ||
62ad15a5 | 125 | #if USE_CONTEXT_MENU |
ccef86c7 VZ |
126 | void OnContextMenu(wxContextMenuEvent& event) |
127 | { ShowContextMenu(ScreenToClient(event.GetPosition())); } | |
128 | #else | |
129 | void OnRightUp(wxMouseEvent& event) | |
130 | { ShowContextMenu(event.GetPosition()); } | |
131 | #endif | |
132 | ||
133 | void OnMenuOpen(wxMenuEvent& event) | |
1242c2d9 WS |
134 | { |
135 | #if USE_LOG_WINDOW | |
136 | LogMenuOpenOrClose(event, _T("opened")); event.Skip(); | |
137 | #endif | |
138 | } | |
ccef86c7 | 139 | void OnMenuClose(wxMenuEvent& event) |
1242c2d9 WS |
140 | { |
141 | #if USE_LOG_WINDOW | |
142 | LogMenuOpenOrClose(event, _T("closed")); event.Skip(); | |
143 | #endif | |
144 | } | |
717a57c2 VZ |
145 | |
146 | void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event); | |
147 | ||
e421922f VZ |
148 | void OnSize(wxSizeEvent& event); |
149 | ||
717a57c2 | 150 | private: |
ccef86c7 VZ |
151 | void LogMenuOpenOrClose(const wxMenuEvent& event, const wxChar *what); |
152 | void ShowContextMenu(const wxPoint& pos); | |
153 | ||
f03ec224 | 154 | wxMenu *CreateDummyMenu(wxString *title); |
717a57c2 VZ |
155 | |
156 | wxMenuItem *GetLastMenuItem() const; | |
157 | ||
e421922f VZ |
158 | // the menu previously detached from the menubar (may be NULL) |
159 | wxMenu *m_menu; | |
717a57c2 | 160 | |
e421922f | 161 | // the count of dummy menus already created |
f03ec224 VZ |
162 | size_t m_countDummy; |
163 | ||
1242c2d9 | 164 | #if USE_LOG_WINDOW |
e421922f VZ |
165 | // the control used for logging |
166 | wxTextCtrl *m_textctrl; | |
1242c2d9 | 167 | #endif |
e421922f VZ |
168 | |
169 | // the previous log target | |
170 | wxLog *m_logOld; | |
171 | ||
717a57c2 VZ |
172 | DECLARE_EVENT_TABLE() |
173 | }; | |
174 | ||
3ca6a5f0 BP |
175 | // A small helper class which intercepts all menu events and logs them |
176 | class MyEvtHandler : public wxEvtHandler | |
177 | { | |
178 | public: | |
179 | MyEvtHandler(MyFrame *frame) { m_frame = frame; } | |
180 | ||
181 | void OnMenuEvent(wxCommandEvent& event) | |
182 | { | |
183 | m_frame->LogMenuEvent(event); | |
184 | ||
185 | event.Skip(); | |
186 | } | |
187 | ||
188 | private: | |
189 | MyFrame *m_frame; | |
190 | ||
191 | DECLARE_EVENT_TABLE() | |
192 | }; | |
193 | ||
717a57c2 VZ |
194 | // ---------------------------------------------------------------------------- |
195 | // constants | |
196 | // ---------------------------------------------------------------------------- | |
197 | ||
198 | enum | |
199 | { | |
1242c2d9 WS |
200 | Menu_File_Quit = wxID_EXIT, |
201 | #if USE_LOG_WINDOW | |
e421922f | 202 | Menu_File_ClearLog, |
1242c2d9 | 203 | #endif |
717a57c2 VZ |
204 | |
205 | Menu_MenuBar_Toggle = 200, | |
206 | Menu_MenuBar_Append, | |
f03ec224 | 207 | Menu_MenuBar_Insert, |
717a57c2 VZ |
208 | Menu_MenuBar_Delete, |
209 | Menu_MenuBar_Enable, | |
210 | Menu_MenuBar_GetLabel, | |
1242c2d9 | 211 | #if wxUSE_TEXTDLG |
717a57c2 | 212 | Menu_MenuBar_SetLabel, |
f6d90fb9 | 213 | Menu_MenuBar_FindMenu, |
1242c2d9 | 214 | #endif |
717a57c2 VZ |
215 | |
216 | Menu_Menu_Append = 300, | |
217 | Menu_Menu_AppendSub, | |
218 | Menu_Menu_Insert, | |
219 | Menu_Menu_Delete, | |
220 | Menu_Menu_Enable, | |
221 | Menu_Menu_Check, | |
222 | Menu_Menu_GetLabel, | |
1242c2d9 | 223 | #if wxUSE_TEXTDLG |
717a57c2 | 224 | Menu_Menu_SetLabel, |
1242c2d9 | 225 | #endif |
a80c322c | 226 | Menu_Menu_GetInfo, |
1242c2d9 | 227 | #if wxUSE_TEXTDLG |
f6d90fb9 | 228 | Menu_Menu_FindItem, |
1242c2d9 | 229 | #endif |
717a57c2 | 230 | |
d65c269b VZ |
231 | Menu_Test_Normal = 400, |
232 | Menu_Test_Check, | |
233 | Menu_Test_Radio1, | |
234 | Menu_Test_Radio2, | |
235 | Menu_Test_Radio3, | |
236 | ||
c377f1be JS |
237 | Menu_SubMenu = 450, |
238 | Menu_SubMenu_Normal, | |
239 | Menu_SubMenu_Check, | |
240 | Menu_SubMenu_Radio1, | |
241 | Menu_SubMenu_Radio2, | |
242 | Menu_SubMenu_Radio3, | |
243 | ||
d65c269b | 244 | Menu_Dummy_First = 500, |
717a57c2 VZ |
245 | Menu_Dummy_Second, |
246 | Menu_Dummy_Third, | |
247 | Menu_Dummy_Fourth, | |
248 | Menu_Dummy_Last, | |
249 | ||
1242c2d9 | 250 | Menu_Help_About = wxID_ABOUT, |
717a57c2 VZ |
251 | |
252 | Menu_Popup_ToBeDeleted = 2000, | |
253 | Menu_Popup_ToBeGreyed, | |
254 | Menu_Popup_ToBeChecked, | |
255 | Menu_Popup_Submenu, | |
256 | ||
257 | Menu_Max | |
258 | }; | |
259 | ||
260 | // ---------------------------------------------------------------------------- | |
261 | // event tables | |
262 | // ---------------------------------------------------------------------------- | |
263 | ||
264 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
e421922f | 265 | EVT_MENU(Menu_File_Quit, MyFrame::OnQuit) |
1242c2d9 | 266 | #if USE_LOG_WINDOW |
e421922f | 267 | EVT_MENU(Menu_File_ClearLog, MyFrame::OnClearLog) |
1242c2d9 | 268 | #endif |
717a57c2 VZ |
269 | |
270 | EVT_MENU(Menu_Help_About, MyFrame::OnAbout) | |
271 | ||
272 | EVT_MENU(Menu_MenuBar_Toggle, MyFrame::OnToggleMenu) | |
273 | EVT_MENU(Menu_MenuBar_Append, MyFrame::OnAppendMenu) | |
f03ec224 | 274 | EVT_MENU(Menu_MenuBar_Insert, MyFrame::OnInsertMenu) |
717a57c2 VZ |
275 | EVT_MENU(Menu_MenuBar_Delete, MyFrame::OnDeleteMenu) |
276 | EVT_MENU(Menu_MenuBar_Enable, MyFrame::OnEnableMenu) | |
277 | EVT_MENU(Menu_MenuBar_GetLabel, MyFrame::OnGetLabelMenu) | |
1242c2d9 | 278 | #if wxUSE_TEXTDLG |
717a57c2 | 279 | EVT_MENU(Menu_MenuBar_SetLabel, MyFrame::OnSetLabelMenu) |
f6d90fb9 | 280 | EVT_MENU(Menu_MenuBar_FindMenu, MyFrame::OnFindMenu) |
1242c2d9 | 281 | #endif |
717a57c2 VZ |
282 | |
283 | EVT_MENU(Menu_Menu_Append, MyFrame::OnAppendMenuItem) | |
284 | EVT_MENU(Menu_Menu_AppendSub, MyFrame::OnAppendSubMenu) | |
285 | EVT_MENU(Menu_Menu_Insert, MyFrame::OnInsertMenuItem) | |
286 | EVT_MENU(Menu_Menu_Delete, MyFrame::OnDeleteMenuItem) | |
287 | EVT_MENU(Menu_Menu_Enable, MyFrame::OnEnableMenuItem) | |
a80c322c | 288 | EVT_MENU(Menu_Menu_Check, MyFrame::OnCheckMenuItem) |
717a57c2 | 289 | EVT_MENU(Menu_Menu_GetLabel, MyFrame::OnGetLabelMenuItem) |
1242c2d9 | 290 | #if wxUSE_TEXTDLG |
717a57c2 | 291 | EVT_MENU(Menu_Menu_SetLabel, MyFrame::OnSetLabelMenuItem) |
1242c2d9 | 292 | #endif |
a80c322c | 293 | EVT_MENU(Menu_Menu_GetInfo, MyFrame::OnGetMenuItemInfo) |
1242c2d9 | 294 | #if wxUSE_TEXTDLG |
f6d90fb9 | 295 | EVT_MENU(Menu_Menu_FindItem, MyFrame::OnFindMenuItem) |
1242c2d9 | 296 | #endif |
717a57c2 | 297 | |
d65c269b VZ |
298 | EVT_MENU(Menu_Test_Normal, MyFrame::OnTestNormal) |
299 | EVT_MENU(Menu_Test_Check, MyFrame::OnTestCheck) | |
300 | EVT_MENU(Menu_Test_Radio1, MyFrame::OnTestRadio) | |
301 | EVT_MENU(Menu_Test_Radio2, MyFrame::OnTestRadio) | |
302 | EVT_MENU(Menu_Test_Radio3, MyFrame::OnTestRadio) | |
303 | ||
c377f1be JS |
304 | EVT_UPDATE_UI(Menu_SubMenu_Normal, MyFrame::OnUpdateSubMenuNormal) |
305 | EVT_UPDATE_UI(Menu_SubMenu_Check, MyFrame::OnUpdateSubMenuCheck) | |
306 | EVT_UPDATE_UI(Menu_SubMenu_Radio1, MyFrame::OnUpdateSubMenuRadio) | |
307 | EVT_UPDATE_UI(Menu_SubMenu_Radio2, MyFrame::OnUpdateSubMenuRadio) | |
308 | EVT_UPDATE_UI(Menu_SubMenu_Radio3, MyFrame::OnUpdateSubMenuRadio) | |
309 | ||
717a57c2 VZ |
310 | EVT_MENU_RANGE(Menu_Dummy_First, Menu_Dummy_Last, MyFrame::OnDummy) |
311 | ||
312 | EVT_UPDATE_UI(Menu_Menu_Check, MyFrame::OnUpdateCheckMenuItemUI) | |
313 | ||
62ad15a5 | 314 | #if USE_CONTEXT_MENU |
ccef86c7 VZ |
315 | EVT_CONTEXT_MENU(MyFrame::OnContextMenu) |
316 | #else | |
e421922f | 317 | EVT_RIGHT_UP(MyFrame::OnRightUp) |
ccef86c7 VZ |
318 | #endif |
319 | ||
320 | EVT_MENU_OPEN(MyFrame::OnMenuOpen) | |
321 | EVT_MENU_CLOSE(MyFrame::OnMenuClose) | |
e421922f VZ |
322 | |
323 | EVT_SIZE(MyFrame::OnSize) | |
717a57c2 VZ |
324 | END_EVENT_TABLE() |
325 | ||
3ca6a5f0 | 326 | BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler) |
1242c2d9 | 327 | EVT_MENU(wxID_ANY, MyEvtHandler::OnMenuEvent) |
3ca6a5f0 BP |
328 | END_EVENT_TABLE() |
329 | ||
717a57c2 VZ |
330 | // ============================================================================ |
331 | // implementation | |
332 | // ============================================================================ | |
333 | ||
334 | // ---------------------------------------------------------------------------- | |
335 | // MyApp | |
336 | // ---------------------------------------------------------------------------- | |
337 | ||
338 | IMPLEMENT_APP(MyApp) | |
339 | ||
340 | // The `main program' equivalent, creating the windows and returning the | |
341 | // main frame | |
342 | bool MyApp::OnInit() | |
343 | { | |
344 | // Create the main frame window | |
345 | MyFrame* frame = new MyFrame; | |
346 | ||
6ee9b7b5 | 347 | frame->Show(true); |
717a57c2 | 348 | |
6d5b2a57 | 349 | #if wxUSE_STATUSBAR |
42ed7532 | 350 | frame->SetStatusText(_T("Welcome to wxWindows menu sample")); |
6d5b2a57 | 351 | #endif // wxUSE_STATUSBAR |
717a57c2 VZ |
352 | |
353 | SetTopWindow(frame); | |
354 | ||
6ee9b7b5 | 355 | return true; |
717a57c2 VZ |
356 | } |
357 | ||
358 | // ---------------------------------------------------------------------------- | |
359 | // MyFrame | |
360 | // ---------------------------------------------------------------------------- | |
361 | ||
362 | // Define my frame constructor | |
363 | MyFrame::MyFrame() | |
1242c2d9 | 364 | : wxFrame((wxFrame *)NULL, wxID_ANY, _T("wxWindows menu sample")) |
717a57c2 | 365 | { |
1242c2d9 | 366 | #if USE_LOG_WINDOW |
268766dd | 367 | m_textctrl = NULL; |
1242c2d9 | 368 | #endif |
717a57c2 | 369 | m_menu = NULL; |
f03ec224 | 370 | m_countDummy = 0; |
e421922f | 371 | m_logOld = NULL; |
717a57c2 | 372 | |
6d5b2a57 | 373 | #if wxUSE_STATUSBAR |
e421922f | 374 | CreateStatusBar(); |
6d5b2a57 | 375 | #endif // wxUSE_STATUSBAR |
717a57c2 VZ |
376 | |
377 | // create the menubar | |
378 | wxMenu *fileMenu = new wxMenu; | |
6d5b2a57 | 379 | |
1242c2d9 | 380 | #if USE_LOG_WINDOW |
e421922f | 381 | wxMenuItem *item = new wxMenuItem(fileMenu, Menu_File_ClearLog, |
42ed7532 | 382 | _T("Clear &log\tCtrl-L")); |
1242c2d9 | 383 | #if wxUSE_OWNER_DRAWN |
e421922f | 384 | item->SetBitmap(copy_xpm); |
1242c2d9 | 385 | #endif |
e421922f VZ |
386 | fileMenu->Append(item); |
387 | fileMenu->AppendSeparator(); | |
1242c2d9 | 388 | #endif |
42ed7532 | 389 | fileMenu->Append(Menu_File_Quit, _T("E&xit\tAlt-X"), _T("Quit menu sample")); |
717a57c2 VZ |
390 | |
391 | wxMenu *menubarMenu = new wxMenu; | |
42ed7532 MB |
392 | menubarMenu->Append(Menu_MenuBar_Append, _T("&Append menu\tCtrl-A"), |
393 | _T("Append a menu to the menubar")); | |
394 | menubarMenu->Append(Menu_MenuBar_Insert, _T("&Insert menu\tCtrl-I"), | |
395 | _T("Insert a menu into the menubar")); | |
396 | menubarMenu->Append(Menu_MenuBar_Delete, _T("&Delete menu\tCtrl-D"), | |
397 | _T("Delete the last menu from the menubar")); | |
398 | menubarMenu->Append(Menu_MenuBar_Toggle, _T("&Toggle menu\tCtrl-T"), | |
6ee9b7b5 | 399 | _T("Toggle the first menu in the menubar"), true); |
717a57c2 | 400 | menubarMenu->AppendSeparator(); |
42ed7532 | 401 | menubarMenu->Append(Menu_MenuBar_Enable, _T("&Enable menu\tCtrl-E"), |
6ee9b7b5 | 402 | _T("Enable or disable the last menu"), true); |
717a57c2 | 403 | menubarMenu->AppendSeparator(); |
42ed7532 MB |
404 | menubarMenu->Append(Menu_MenuBar_GetLabel, _T("&Get menu label\tCtrl-G"), |
405 | _T("Get the label of the last menu")); | |
1242c2d9 | 406 | #if wxUSE_TEXTDLG |
42ed7532 MB |
407 | menubarMenu->Append(Menu_MenuBar_SetLabel, _T("&Set menu label\tCtrl-S"), |
408 | _T("Change the label of the last menu")); | |
f6d90fb9 | 409 | menubarMenu->AppendSeparator(); |
42ed7532 MB |
410 | menubarMenu->Append(Menu_MenuBar_FindMenu, _T("&Find menu from label\tCtrl-F"), |
411 | _T("Find a menu by searching for its label")); | |
1242c2d9 | 412 | #endif |
717a57c2 | 413 | |
c377f1be JS |
414 | wxMenu* subMenu = new wxMenu; |
415 | subMenu->Append(Menu_SubMenu_Normal, _T("&Normal submenu item"), _T("Disabled submenu item")); | |
416 | subMenu->AppendCheckItem(Menu_SubMenu_Check, _T("&Unchecked submenu item"), _T("Unchecked submenu item")); | |
417 | subMenu->AppendRadioItem(Menu_SubMenu_Radio1, _T("&Radio item 1"), _T("Radio item")); | |
418 | subMenu->AppendRadioItem(Menu_SubMenu_Radio2, _T("&Radio item 2"), _T("Radio item")); | |
419 | subMenu->AppendRadioItem(Menu_SubMenu_Radio3, _T("&Radio item 3"), _T("Radio item")); | |
420 | ||
421 | menubarMenu->Append(Menu_SubMenu, _T("Submenu"), subMenu); | |
422 | ||
717a57c2 | 423 | wxMenu *menuMenu = new wxMenu; |
42ed7532 MB |
424 | menuMenu->Append(Menu_Menu_Append, _T("&Append menu item\tAlt-A"), |
425 | _T("Append a menu item to the last menu")); | |
426 | menuMenu->Append(Menu_Menu_AppendSub, _T("&Append sub menu\tAlt-S"), | |
427 | _T("Append a sub menu to the last menu")); | |
428 | menuMenu->Append(Menu_Menu_Insert, _T("&Insert menu item\tAlt-I"), | |
429 | _T("Insert a menu item in head of the last menu")); | |
430 | menuMenu->Append(Menu_Menu_Delete, _T("&Delete menu item\tAlt-D"), | |
431 | _T("Delete the last menu item from the last menu")); | |
717a57c2 | 432 | menuMenu->AppendSeparator(); |
42ed7532 | 433 | menuMenu->Append(Menu_Menu_Enable, _T("&Enable menu item\tAlt-E"), |
6ee9b7b5 | 434 | _T("Enable or disable the last menu item"), true); |
42ed7532 | 435 | menuMenu->Append(Menu_Menu_Check, _T("&Check menu item\tAlt-C"), |
6ee9b7b5 | 436 | _T("Check or uncheck the last menu item"), true); |
717a57c2 | 437 | menuMenu->AppendSeparator(); |
42ed7532 MB |
438 | menuMenu->Append(Menu_Menu_GetInfo, _T("Get menu item in&fo\tAlt-F"), |
439 | _T("Show the state of the last menu item")); | |
1242c2d9 | 440 | #if wxUSE_TEXTDLG |
a80c322c | 441 | menuMenu->AppendSeparator(); |
42ed7532 MB |
442 | menuMenu->Append(Menu_Menu_FindItem, _T("Find menu item from label"), |
443 | _T("Find a menu item by searching for its label")); | |
1242c2d9 | 444 | #endif |
717a57c2 | 445 | |
d65c269b | 446 | wxMenu *testMenu = new wxMenu; |
42ed7532 | 447 | testMenu->Append(Menu_Test_Normal, _T("&Normal item")); |
d65c269b | 448 | testMenu->AppendSeparator(); |
42ed7532 | 449 | testMenu->AppendCheckItem(Menu_Test_Check, _T("&Check item")); |
d65c269b | 450 | testMenu->AppendSeparator(); |
42ed7532 MB |
451 | testMenu->AppendRadioItem(Menu_Test_Radio1, _T("Radio item &1")); |
452 | testMenu->AppendRadioItem(Menu_Test_Radio2, _T("Radio item &2")); | |
453 | testMenu->AppendRadioItem(Menu_Test_Radio3, _T("Radio item &3")); | |
d65c269b | 454 | |
717a57c2 | 455 | wxMenu *helpMenu = new wxMenu; |
42ed7532 | 456 | helpMenu->Append(Menu_Help_About, _T("&About\tF1"), _T("About menu sample")); |
717a57c2 VZ |
457 | |
458 | wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE ); | |
459 | ||
42ed7532 MB |
460 | menuBar->Append(fileMenu, _T("&File")); |
461 | menuBar->Append(menubarMenu, _T("Menu&bar")); | |
462 | menuBar->Append(menuMenu, _T("&Menu")); | |
463 | menuBar->Append(testMenu, _T("&Test")); | |
464 | menuBar->Append(helpMenu, _T("&Help")); | |
717a57c2 VZ |
465 | |
466 | // these items should be initially checked | |
6ee9b7b5 JS |
467 | menuBar->Check(Menu_MenuBar_Toggle, true); |
468 | menuBar->Check(Menu_MenuBar_Enable, true); | |
469 | menuBar->Check(Menu_Menu_Enable, true); | |
470 | menuBar->Check(Menu_Menu_Check, false); | |
717a57c2 VZ |
471 | |
472 | // associate the menu bar with the frame | |
473 | SetMenuBar(menuBar); | |
3ca6a5f0 BP |
474 | |
475 | // intercept all menu events and log them in this custom event handler | |
476 | PushEventHandler(new MyEvtHandler(this)); | |
e421922f | 477 | |
1242c2d9 | 478 | #if USE_LOG_WINDOW |
e421922f | 479 | // create the log text window |
1242c2d9 | 480 | m_textctrl = new wxTextCtrl(this, wxID_ANY, _T(""), |
e421922f VZ |
481 | wxDefaultPosition, wxDefaultSize, |
482 | wxTE_MULTILINE); | |
6ee9b7b5 | 483 | m_textctrl->SetEditable(false); |
5c7766de VZ |
484 | |
485 | wxLog::SetTimestamp(NULL); | |
e421922f VZ |
486 | m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl)); |
487 | ||
5c7766de | 488 | wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ") |
e421922f | 489 | _T("append/insert/delete items to/from the last menu.\n") |
5c7766de VZ |
490 | _T("The commands from \"Menubar\" menu work with the ") |
491 | _T("menubar itself.\n\n") | |
e421922f | 492 | _T("Right click the band below to test popup menus.\n")); |
1242c2d9 | 493 | #endif |
3ca6a5f0 BP |
494 | } |
495 | ||
496 | MyFrame::~MyFrame() | |
497 | { | |
498 | delete m_menu; | |
499 | ||
500 | // delete the event handler installed in ctor | |
6ee9b7b5 | 501 | PopEventHandler(true); |
e421922f | 502 | |
1242c2d9 | 503 | #if USE_LOG_WINDOW |
e421922f VZ |
504 | // restore old logger |
505 | delete wxLog::SetActiveTarget(m_logOld); | |
1242c2d9 | 506 | #endif |
717a57c2 VZ |
507 | } |
508 | ||
f03ec224 | 509 | wxMenu *MyFrame::CreateDummyMenu(wxString *title) |
717a57c2 VZ |
510 | { |
511 | wxMenu *menu = new wxMenu; | |
42ed7532 | 512 | menu->Append(Menu_Dummy_First, _T("&First item\tCtrl-F1")); |
717a57c2 | 513 | menu->AppendSeparator(); |
6ee9b7b5 | 514 | menu->Append(Menu_Dummy_Second, _T("&Second item\tCtrl-F2"), _T(""), true); |
717a57c2 | 515 | |
f03ec224 VZ |
516 | if ( title ) |
517 | { | |
6ee9b7b5 | 518 | title->Printf(_T("Dummy menu &%u"), (unsigned)++m_countDummy); |
f03ec224 VZ |
519 | } |
520 | ||
717a57c2 VZ |
521 | return menu; |
522 | } | |
523 | ||
524 | wxMenuItem *MyFrame::GetLastMenuItem() const | |
525 | { | |
526 | wxMenuBar *menubar = GetMenuBar(); | |
527 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); | |
528 | ||
65c0217e | 529 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetLast(); |
717a57c2 VZ |
530 | if ( !node ) |
531 | { | |
6ee9b7b5 | 532 | wxLogWarning(_T("No last item in the last menu!")); |
717a57c2 VZ |
533 | |
534 | return NULL; | |
535 | } | |
536 | else | |
537 | { | |
538 | return node->GetData(); | |
539 | } | |
540 | } | |
541 | ||
3ca6a5f0 BP |
542 | void MyFrame::LogMenuEvent(const wxCommandEvent& event) |
543 | { | |
544 | int id = event.GetId(); | |
e421922f | 545 | if ( !GetMenuBar()->FindItem(id) ) |
d3d69314 | 546 | return; |
e421922f | 547 | |
6ee9b7b5 | 548 | wxString msg = wxString::Format(_T("Menu command %d"), id); |
3ca6a5f0 BP |
549 | if ( GetMenuBar()->FindItem(id)->IsCheckable() ) |
550 | { | |
6ee9b7b5 JS |
551 | msg += wxString::Format(_T(" (the item is currently %schecked)"), |
552 | event.IsChecked() ? _T("") : _T("not ")); | |
3ca6a5f0 BP |
553 | } |
554 | ||
e421922f | 555 | wxLogMessage(msg); |
3ca6a5f0 BP |
556 | } |
557 | ||
558 | // ---------------------------------------------------------------------------- | |
559 | // menu callbacks | |
560 | // ---------------------------------------------------------------------------- | |
561 | ||
717a57c2 VZ |
562 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
563 | { | |
6ee9b7b5 | 564 | Close(true); |
717a57c2 VZ |
565 | } |
566 | ||
1242c2d9 | 567 | #if USE_LOG_WINDOW |
e421922f VZ |
568 | void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event)) |
569 | { | |
570 | m_textctrl->Clear(); | |
571 | } | |
1242c2d9 | 572 | #endif |
e421922f | 573 | |
717a57c2 VZ |
574 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
575 | { | |
42ed7532 MB |
576 |