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