]>
Commit | Line | Data |
---|---|---|
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". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
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" | |
34 | #include "wx/textdlg.h" | |
35 | #endif | |
36 | ||
37 | #if !wxUSE_MENUS | |
38 | // nice try... | |
39 | #error "menu sample requires wxUSE_MENUS=1" | |
40 | #endif // wxUSE_MENUS | |
41 | ||
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__) || defined(__WXX11__) || defined(__WXMGL__) | |
45 | #define USE_CONTEXT_MENU 0 | |
46 | #else | |
47 | #define USE_CONTEXT_MENU 1 | |
48 | #endif | |
49 | ||
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 | ||
58 | #if wxUSE_OWNER_DRAWN | |
59 | #include "copy.xpm" | |
60 | #endif | |
61 | ||
62 | // ---------------------------------------------------------------------------- | |
63 | // classes | |
64 | // ---------------------------------------------------------------------------- | |
65 | ||
66 | // Define a new application | |
67 | class MyApp: public wxApp | |
68 | { | |
69 | public: | |
70 | bool OnInit(); | |
71 | }; | |
72 | ||
73 | // Define a new frame | |
74 | class MyFrame: public wxFrame | |
75 | { | |
76 | public: | |
77 | MyFrame(); | |
78 | ||
79 | virtual ~MyFrame(); | |
80 | ||
81 | void LogMenuEvent(const wxCommandEvent& event); | |
82 | ||
83 | protected: | |
84 | void OnQuit(wxCommandEvent& event); | |
85 | #if USE_LOG_WINDOW | |
86 | void OnClearLog(wxCommandEvent& event); | |
87 | #endif | |
88 | ||
89 | void OnAbout(wxCommandEvent& event); | |
90 | ||
91 | void OnDummy(wxCommandEvent& event); | |
92 | ||
93 | void OnAppendMenuItem(wxCommandEvent& event); | |
94 | void OnAppendSubMenu(wxCommandEvent& event); | |
95 | void OnDeleteMenuItem(wxCommandEvent& event); | |
96 | void OnInsertMenuItem(wxCommandEvent& event); | |
97 | void OnCheckMenuItem(wxCommandEvent& event); | |
98 | void OnEnableMenuItem(wxCommandEvent& event); | |
99 | void OnGetLabelMenuItem(wxCommandEvent& event); | |
100 | #if wxUSE_TEXTDLG | |
101 | void OnSetLabelMenuItem(wxCommandEvent& event); | |
102 | #endif | |
103 | void OnGetMenuItemInfo(wxCommandEvent& event); | |
104 | #if wxUSE_TEXTDLG | |
105 | void OnFindMenuItem(wxCommandEvent& event); | |
106 | #endif | |
107 | ||
108 | void OnAppendMenu(wxCommandEvent& event); | |
109 | void OnInsertMenu(wxCommandEvent& event); | |
110 | void OnDeleteMenu(wxCommandEvent& event); | |
111 | void OnToggleMenu(wxCommandEvent& event); | |
112 | void OnEnableMenu(wxCommandEvent& event); | |
113 | void OnGetLabelMenu(wxCommandEvent& event); | |
114 | void OnSetLabelMenu(wxCommandEvent& event); | |
115 | #if wxUSE_TEXTDLG | |
116 | void OnFindMenu(wxCommandEvent& event); | |
117 | #endif | |
118 | ||
119 | void OnTestNormal(wxCommandEvent& event); | |
120 | void OnTestCheck(wxCommandEvent& event); | |
121 | void OnTestRadio(wxCommandEvent& event); | |
122 | ||
123 | void OnUpdateSubMenuNormal(wxUpdateUIEvent& event); | |
124 | void OnUpdateSubMenuCheck(wxUpdateUIEvent& event); | |
125 | void OnUpdateSubMenuRadio(wxUpdateUIEvent& event); | |
126 | ||
127 | #if USE_CONTEXT_MENU | |
128 | void OnContextMenu(wxContextMenuEvent& event); | |
129 | #else | |
130 | void OnRightUp(wxMouseEvent& event) | |
131 | { ShowContextMenu(event.GetPosition()); } | |
132 | #endif | |
133 | ||
134 | void OnMenuOpen(wxMenuEvent& event) | |
135 | { | |
136 | #if USE_LOG_WINDOW | |
137 | LogMenuOpenOrClose(event, _T("opened")); event.Skip(); | |
138 | #endif | |
139 | } | |
140 | void OnMenuClose(wxMenuEvent& event) | |
141 | { | |
142 | #if USE_LOG_WINDOW | |
143 | LogMenuOpenOrClose(event, _T("closed")); event.Skip(); | |
144 | #endif | |
145 | } | |
146 | ||
147 | void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event); | |
148 | ||
149 | void OnSize(wxSizeEvent& event); | |
150 | ||
151 | private: | |
152 | void LogMenuOpenOrClose(const wxMenuEvent& event, const wxChar *what); | |
153 | void ShowContextMenu(const wxPoint& pos); | |
154 | ||
155 | wxMenu *CreateDummyMenu(wxString *title); | |
156 | ||
157 | wxMenuItem *GetLastMenuItem() const; | |
158 | ||
159 | // the menu previously detached from the menubar (may be NULL) | |
160 | wxMenu *m_menu; | |
161 | ||
162 | // the count of dummy menus already created | |
163 | size_t m_countDummy; | |
164 | ||
165 | #if USE_LOG_WINDOW | |
166 | // the control used for logging | |
167 | wxTextCtrl *m_textctrl; | |
168 | #endif | |
169 | ||
170 | // the previous log target | |
171 | wxLog *m_logOld; | |
172 | ||
173 | DECLARE_EVENT_TABLE() | |
174 | }; | |
175 | ||
176 | // A small helper class which intercepts all menu events and logs them | |
177 | class MyEvtHandler : public wxEvtHandler | |
178 | { | |
179 | public: | |
180 | MyEvtHandler(MyFrame *frame) { m_frame = frame; } | |
181 | ||
182 | void OnMenuEvent(wxCommandEvent& event) | |
183 | { | |
184 | m_frame->LogMenuEvent(event); | |
185 | ||
186 | event.Skip(); | |
187 | } | |
188 | ||
189 | private: | |
190 | MyFrame *m_frame; | |
191 | ||
192 | DECLARE_EVENT_TABLE() | |
193 | }; | |
194 | ||
195 | // ---------------------------------------------------------------------------- | |
196 | // constants | |
197 | // ---------------------------------------------------------------------------- | |
198 | ||
199 | enum | |
200 | { | |
201 | Menu_File_Quit = wxID_EXIT, | |
202 | #if USE_LOG_WINDOW | |
203 | Menu_File_ClearLog, | |
204 | #endif | |
205 | ||
206 | Menu_MenuBar_Toggle = 200, | |
207 | Menu_MenuBar_Append, | |
208 | Menu_MenuBar_Insert, | |
209 | Menu_MenuBar_Delete, | |
210 | Menu_MenuBar_Enable, | |
211 | Menu_MenuBar_GetLabel, | |
212 | #if wxUSE_TEXTDLG | |
213 | Menu_MenuBar_SetLabel, | |
214 | Menu_MenuBar_FindMenu, | |
215 | #endif | |
216 | ||
217 | Menu_Menu_Append = 300, | |
218 | Menu_Menu_AppendSub, | |
219 | Menu_Menu_Insert, | |
220 | Menu_Menu_Delete, | |
221 | Menu_Menu_Enable, | |
222 | Menu_Menu_Check, | |
223 | Menu_Menu_GetLabel, | |
224 | #if wxUSE_TEXTDLG | |
225 | Menu_Menu_SetLabel, | |
226 | #endif | |
227 | Menu_Menu_GetInfo, | |
228 | #if wxUSE_TEXTDLG | |
229 | Menu_Menu_FindItem, | |
230 | #endif | |
231 | ||
232 | Menu_Test_Normal = 400, | |
233 | Menu_Test_Check, | |
234 | Menu_Test_Radio1, | |
235 | Menu_Test_Radio2, | |
236 | Menu_Test_Radio3, | |
237 | ||
238 | Menu_SubMenu = 450, | |
239 | Menu_SubMenu_Normal, | |
240 | Menu_SubMenu_Check, | |
241 | Menu_SubMenu_Radio1, | |
242 | Menu_SubMenu_Radio2, | |
243 | Menu_SubMenu_Radio3, | |
244 | ||
245 | Menu_Dummy_First = 500, | |
246 | Menu_Dummy_Second, | |
247 | Menu_Dummy_Third, | |
248 | Menu_Dummy_Fourth, | |
249 | Menu_Dummy_Last, | |
250 | ||
251 | Menu_Help_About = wxID_ABOUT, | |
252 | ||
253 | Menu_Popup_ToBeDeleted = 2000, | |
254 | Menu_Popup_ToBeGreyed, | |
255 | Menu_Popup_ToBeChecked, | |
256 | Menu_Popup_Submenu, | |
257 | ||
258 | Menu_Max | |
259 | }; | |
260 | ||
261 | // ---------------------------------------------------------------------------- | |
262 | // event tables | |
263 | // ---------------------------------------------------------------------------- | |
264 | ||
265 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
266 | EVT_MENU(Menu_File_Quit, MyFrame::OnQuit) | |
267 | #if USE_LOG_WINDOW | |
268 | EVT_MENU(Menu_File_ClearLog, MyFrame::OnClearLog) | |
269 | #endif | |
270 | ||
271 | EVT_MENU(Menu_Help_About, MyFrame::OnAbout) | |
272 | ||
273 | EVT_MENU(Menu_MenuBar_Toggle, MyFrame::OnToggleMenu) | |
274 | EVT_MENU(Menu_MenuBar_Append, MyFrame::OnAppendMenu) | |
275 | EVT_MENU(Menu_MenuBar_Insert, MyFrame::OnInsertMenu) | |
276 | EVT_MENU(Menu_MenuBar_Delete, MyFrame::OnDeleteMenu) | |
277 | EVT_MENU(Menu_MenuBar_Enable, MyFrame::OnEnableMenu) | |
278 | EVT_MENU(Menu_MenuBar_GetLabel, MyFrame::OnGetLabelMenu) | |
279 | #if wxUSE_TEXTDLG | |
280 | EVT_MENU(Menu_MenuBar_SetLabel, MyFrame::OnSetLabelMenu) | |
281 | EVT_MENU(Menu_MenuBar_FindMenu, MyFrame::OnFindMenu) | |
282 | #endif | |
283 | ||
284 | EVT_MENU(Menu_Menu_Append, MyFrame::OnAppendMenuItem) | |
285 | EVT_MENU(Menu_Menu_AppendSub, MyFrame::OnAppendSubMenu) | |
286 | EVT_MENU(Menu_Menu_Insert, MyFrame::OnInsertMenuItem) | |
287 | EVT_MENU(Menu_Menu_Delete, MyFrame::OnDeleteMenuItem) | |
288 | EVT_MENU(Menu_Menu_Enable, MyFrame::OnEnableMenuItem) | |
289 | EVT_MENU(Menu_Menu_Check, MyFrame::OnCheckMenuItem) | |
290 | EVT_MENU(Menu_Menu_GetLabel, MyFrame::OnGetLabelMenuItem) | |
291 | #if wxUSE_TEXTDLG | |
292 | EVT_MENU(Menu_Menu_SetLabel, MyFrame::OnSetLabelMenuItem) | |
293 | #endif | |
294 | EVT_MENU(Menu_Menu_GetInfo, MyFrame::OnGetMenuItemInfo) | |
295 | #if wxUSE_TEXTDLG | |
296 | EVT_MENU(Menu_Menu_FindItem, MyFrame::OnFindMenuItem) | |
297 | #endif | |
298 | ||
299 | EVT_MENU(Menu_Test_Normal, MyFrame::OnTestNormal) | |
300 | EVT_MENU(Menu_Test_Check, MyFrame::OnTestCheck) | |
301 | EVT_MENU(Menu_Test_Radio1, MyFrame::OnTestRadio) | |
302 | EVT_MENU(Menu_Test_Radio2, MyFrame::OnTestRadio) | |
303 | EVT_MENU(Menu_Test_Radio3, MyFrame::OnTestRadio) | |
304 | ||
305 | EVT_UPDATE_UI(Menu_SubMenu_Normal, MyFrame::OnUpdateSubMenuNormal) | |
306 | EVT_UPDATE_UI(Menu_SubMenu_Check, MyFrame::OnUpdateSubMenuCheck) | |
307 | EVT_UPDATE_UI(Menu_SubMenu_Radio1, MyFrame::OnUpdateSubMenuRadio) | |
308 | EVT_UPDATE_UI(Menu_SubMenu_Radio2, MyFrame::OnUpdateSubMenuRadio) | |
309 | EVT_UPDATE_UI(Menu_SubMenu_Radio3, MyFrame::OnUpdateSubMenuRadio) | |
310 | ||
311 | EVT_MENU_RANGE(Menu_Dummy_First, Menu_Dummy_Last, MyFrame::OnDummy) | |
312 | ||
313 | EVT_UPDATE_UI(Menu_Menu_Check, MyFrame::OnUpdateCheckMenuItemUI) | |
314 | ||
315 | #if USE_CONTEXT_MENU | |
316 | EVT_CONTEXT_MENU(MyFrame::OnContextMenu) | |
317 | #else | |
318 | EVT_RIGHT_UP(MyFrame::OnRightUp) | |
319 | #endif | |
320 | ||
321 | EVT_MENU_OPEN(MyFrame::OnMenuOpen) | |
322 | EVT_MENU_CLOSE(MyFrame::OnMenuClose) | |
323 | ||
324 | EVT_SIZE(MyFrame::OnSize) | |
325 | END_EVENT_TABLE() | |
326 | ||
327 | BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler) | |
328 | EVT_MENU(wxID_ANY, MyEvtHandler::OnMenuEvent) | |
329 | END_EVENT_TABLE() | |
330 | ||
331 | // ============================================================================ | |
332 | // implementation | |
333 | // ============================================================================ | |
334 | ||
335 | // ---------------------------------------------------------------------------- | |
336 | // MyApp | |
337 | // ---------------------------------------------------------------------------- | |
338 | ||
339 | IMPLEMENT_APP(MyApp) | |
340 | ||
341 | // The `main program' equivalent, creating the windows and returning the | |
342 | // main frame | |
343 | bool MyApp::OnInit() | |
344 | { | |
345 | // Create the main frame window | |
346 | MyFrame* frame = new MyFrame; | |
347 | ||
348 | frame->Show(true); | |
349 | ||
350 | #if wxUSE_STATUSBAR | |
351 | frame->SetStatusText(_T("Welcome to wxWidgets menu sample")); | |
352 | #endif // wxUSE_STATUSBAR | |
353 | ||
354 | SetTopWindow(frame); | |
355 | ||
356 | return true; | |
357 | } | |
358 | ||
359 | // ---------------------------------------------------------------------------- | |
360 | // MyFrame | |
361 | // ---------------------------------------------------------------------------- | |
362 | ||
363 | // Define my frame constructor | |
364 | MyFrame::MyFrame() | |
365 | : wxFrame((wxFrame *)NULL, wxID_ANY, _T("wxWidgets menu sample")) | |
366 | { | |
367 | #if USE_LOG_WINDOW | |
368 | m_textctrl = NULL; | |
369 | #endif | |
370 | m_menu = NULL; | |
371 | m_countDummy = 0; | |
372 | m_logOld = NULL; | |
373 | ||
374 | #if wxUSE_STATUSBAR | |
375 | CreateStatusBar(); | |
376 | #endif // wxUSE_STATUSBAR | |
377 | ||
378 | // create the menubar | |
379 | wxMenu *fileMenu = new wxMenu; | |
380 | ||
381 | #if USE_LOG_WINDOW | |
382 | wxMenuItem *item = new wxMenuItem(fileMenu, Menu_File_ClearLog, | |
383 | _T("Clear &log\tCtrl-L")); | |
384 | #if wxUSE_OWNER_DRAWN | |
385 | item->SetBitmap(copy_xpm); | |
386 | #endif | |
387 | fileMenu->Append(item); | |
388 | fileMenu->AppendSeparator(); | |
389 | #endif | |
390 | fileMenu->Append(Menu_File_Quit, _T("E&xit\tAlt-X"), _T("Quit menu sample")); | |
391 | ||
392 | wxMenu *menubarMenu = new wxMenu; | |
393 | menubarMenu->Append(Menu_MenuBar_Append, _T("&Append menu\tCtrl-A"), | |
394 | _T("Append a menu to the menubar")); | |
395 | menubarMenu->Append(Menu_MenuBar_Insert, _T("&Insert menu\tCtrl-I"), | |
396 | _T("Insert a menu into the menubar")); | |
397 | menubarMenu->Append(Menu_MenuBar_Delete, _T("&Delete menu\tCtrl-D"), | |
398 | _T("Delete the last menu from the menubar")); | |
399 | menubarMenu->Append(Menu_MenuBar_Toggle, _T("&Toggle menu\tCtrl-T"), | |
400 | _T("Toggle the first menu in the menubar"), true); | |
401 | menubarMenu->AppendSeparator(); | |
402 | menubarMenu->Append(Menu_MenuBar_Enable, _T("&Enable menu\tCtrl-E"), | |
403 | _T("Enable or disable the last menu"), true); | |
404 | menubarMenu->AppendSeparator(); | |
405 | menubarMenu->Append(Menu_MenuBar_GetLabel, _T("&Get menu label\tCtrl-G"), | |
406 | _T("Get the label of the last menu")); | |
407 | #if wxUSE_TEXTDLG | |
408 | menubarMenu->Append(Menu_MenuBar_SetLabel, _T("&Set menu label\tCtrl-S"), | |
409 | _T("Change the label of the last menu")); | |
410 | menubarMenu->AppendSeparator(); | |
411 | menubarMenu->Append(Menu_MenuBar_FindMenu, _T("&Find menu from label\tCtrl-F"), | |
412 | _T("Find a menu by searching for its label")); | |
413 | #endif | |
414 | ||
415 | wxMenu* subMenu = new wxMenu; | |
416 | subMenu->Append(Menu_SubMenu_Normal, _T("&Normal submenu item"), _T("Disabled submenu item")); | |
417 | subMenu->AppendCheckItem(Menu_SubMenu_Check, _T("&Unchecked submenu item"), _T("Unchecked submenu item")); | |
418 | subMenu->AppendRadioItem(Menu_SubMenu_Radio1, _T("Radio item &1"), _T("Radio item")); | |
419 | subMenu->AppendRadioItem(Menu_SubMenu_Radio2, _T("Radio item &2"), _T("Radio item")); | |
420 | subMenu->AppendRadioItem(Menu_SubMenu_Radio3, _T("Radio item &3"), _T("Radio item")); | |
421 | ||
422 | menubarMenu->Append(Menu_SubMenu, _T("Submenu"), subMenu); | |
423 | ||
424 | wxMenu *menuMenu = new wxMenu; | |
425 | menuMenu->Append(Menu_Menu_Append, _T("&Append menu item\tAlt-A"), | |
426 | _T("Append a menu item to the last menu")); | |
427 | menuMenu->Append(Menu_Menu_AppendSub, _T("&Append sub menu\tAlt-S"), | |
428 | _T("Append a sub menu to the last menu")); | |
429 | menuMenu->Append(Menu_Menu_Insert, _T("&Insert menu item\tAlt-I"), | |
430 | _T("Insert a menu item in head of the last menu")); | |
431 | menuMenu->Append(Menu_Menu_Delete, _T("&Delete menu item\tAlt-D"), | |
432 | _T("Delete the last menu item from the last menu")); | |
433 | menuMenu->AppendSeparator(); | |
434 | menuMenu->Append(Menu_Menu_Enable, _T("&Enable menu item\tAlt-E"), | |
435 | _T("Enable or disable the last menu item"), true); | |
436 | menuMenu->Append(Menu_Menu_Check, _T("&Check menu item\tAlt-C"), | |
437 | _T("Check or uncheck the last menu item"), true); | |
438 | menuMenu->AppendSeparator(); | |
439 | menuMenu->Append(Menu_Menu_GetInfo, _T("Get menu item in&fo\tAlt-F"), | |
440 | _T("Show the state of the last menu item")); | |
441 | #if wxUSE_TEXTDLG | |
442 | menuMenu->Append(Menu_Menu_SetLabel, _T("Set menu item label\tAlt-L"), | |
443 | _T("Set the label of a menu item")); | |
444 | #endif | |
445 | #if wxUSE_TEXTDLG | |
446 | menuMenu->AppendSeparator(); | |
447 | menuMenu->Append(Menu_Menu_FindItem, _T("Find menu item from label"), | |
448 | _T("Find a menu item by searching for its label")); | |
449 | #endif | |
450 | ||
451 | wxMenu *testMenu = new wxMenu; | |
452 | testMenu->Append(Menu_Test_Normal, _T("&Normal item")); | |
453 | testMenu->AppendSeparator(); | |
454 | testMenu->AppendCheckItem(Menu_Test_Check, _T("&Check item")); | |
455 | testMenu->AppendSeparator(); | |
456 | testMenu->AppendRadioItem(Menu_Test_Radio1, _T("Radio item &1")); | |
457 | testMenu->AppendRadioItem(Menu_Test_Radio2, _T("Radio item &2")); | |
458 | testMenu->AppendRadioItem(Menu_Test_Radio3, _T("Radio item &3")); | |
459 | ||
460 | wxMenu *helpMenu = new wxMenu; | |
461 | helpMenu->Append(Menu_Help_About, _T("&About\tF1"), _T("About menu sample")); | |
462 | ||
463 | wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE ); | |
464 | ||
465 | menuBar->Append(fileMenu, _T("&File")); | |
466 | menuBar->Append(menubarMenu, _T("Menu&bar")); | |
467 | menuBar->Append(menuMenu, _T("&Menu")); | |
468 | menuBar->Append(testMenu, _T("&Test")); | |
469 | menuBar->Append(helpMenu, _T("&Help")); | |
470 | ||
471 | // these items should be initially checked | |
472 | menuBar->Check(Menu_MenuBar_Toggle, true); | |
473 | menuBar->Check(Menu_MenuBar_Enable, true); | |
474 | menuBar->Check(Menu_Menu_Enable, true); | |
475 | menuBar->Check(Menu_Menu_Check, false); | |
476 | ||
477 | // associate the menu bar with the frame | |
478 | SetMenuBar(menuBar); | |
479 | ||
480 | // intercept all menu events and log them in this custom event handler | |
481 | PushEventHandler(new MyEvtHandler(this)); | |
482 | ||
483 | #if USE_LOG_WINDOW | |
484 | // create the log text window | |
485 | m_textctrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, | |
486 | wxDefaultPosition, wxDefaultSize, | |
487 | wxTE_MULTILINE); | |
488 | m_textctrl->SetEditable(false); | |
489 | ||
490 | wxLog::SetTimestamp(NULL); | |
491 | m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl)); | |
492 | ||
493 | wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ") | |
494 | _T("append/insert/delete items to/from the last menu.\n") | |
495 | _T("The commands from \"Menubar\" menu work with the ") | |
496 | _T("menubar itself.\n\n") | |
497 | _T("Right click the band below to test popup menus.\n")); | |
498 | #endif | |
499 | } | |
500 | ||
501 | MyFrame::~MyFrame() | |
502 | { | |
503 | delete m_menu; | |
504 | ||
505 | // delete the event handler installed in ctor | |
506 | PopEventHandler(true); | |
507 | ||
508 | #if USE_LOG_WINDOW | |
509 | // restore old logger | |
510 | delete wxLog::SetActiveTarget(m_logOld); | |
511 | #endif | |
512 | } | |
513 | ||
514 | wxMenu *MyFrame::CreateDummyMenu(wxString *title) | |
515 | { | |
516 | wxMenu *menu = new wxMenu; | |
517 | menu->Append(Menu_Dummy_First, _T("&First item\tCtrl-F1")); | |
518 | menu->AppendSeparator(); | |
519 | menu->AppendCheckItem(Menu_Dummy_Second, _T("&Second item\tCtrl-F2")); | |
520 | ||
521 | if ( title ) | |
522 | { | |
523 | title->Printf(_T("Dummy menu &%u"), (unsigned)++m_countDummy); | |
524 | } | |
525 | ||
526 | return menu; | |
527 | } | |
528 | ||
529 | wxMenuItem *MyFrame::GetLastMenuItem() const | |
530 | { | |
531 | wxMenuBar *menubar = GetMenuBar(); | |
532 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); | |
533 | ||
534 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetLast(); | |
535 | if ( !node ) | |
536 | { | |
537 | wxLogWarning(_T("No last item in the last menu!")); | |
538 | ||
539 | return NULL; | |
540 | } | |
541 | else | |
542 | { | |
543 | return node->GetData(); | |
544 | } | |
545 | } | |
546 | ||
547 | void MyFrame::LogMenuEvent(const wxCommandEvent& event) | |
548 | { | |
549 | int id = event.GetId(); | |
550 | if ( !GetMenuBar()->FindItem(id) ) | |
551 | return; | |
552 | ||
553 | wxString msg = wxString::Format(_T("Menu command %d"), id); | |
554 | if ( GetMenuBar()->FindItem(id)->IsCheckable() ) | |
555 | { | |
556 | msg += wxString::Format(_T(" (the item is currently %schecked)"), | |
557 | event.IsChecked() ? _T("") : _T("not ")); | |
558 | } | |
559 | ||
560 | wxLogMessage(msg); | |
561 | } | |
562 | ||
563 | // ---------------------------------------------------------------------------- | |
564 | // menu callbacks | |
565 | // ---------------------------------------------------------------------------- | |
566 | ||
567 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
568 | { | |
569 | Close(true); | |
570 | } | |
571 | ||
572 | #if USE_LOG_WINDOW | |
573 | void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event)) | |
574 | { | |
575 | m_textctrl->Clear(); | |
576 | } | |
577 | #endif | |
578 | ||
579 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
580 | { | |
581 | (void)wxMessageBox(_T("wxWidgets menu sample\n(c) 1999-2001 Vadim Zeitlin"), | |
582 | _T("About wxWidgets menu sample"), | |
583 | wxICON_INFORMATION); | |
584 | } | |
585 | ||
586 | void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event)) | |
587 | { | |
588 | wxMenuBar *mbar = GetMenuBar(); | |
589 | ||
590 | size_t count = mbar->GetMenuCount(); | |
591 | if ( count == 2 ) | |
592 | { | |
593 | // don't let delete the first 2 menus | |
594 | wxLogError(_T("Can't delete any more menus")); | |
595 | } | |
596 | else | |
597 | { | |
598 | delete mbar->Remove(count - 1); | |
599 | } | |
600 | } | |
601 | ||
602 | void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event)) | |
603 | { | |
604 | wxString title; | |
605 | wxMenu *menu = CreateDummyMenu(&title); | |
606 | GetMenuBar()->Insert(0, menu, title); | |
607 | } | |
608 | ||
609 | void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event)) | |
610 | { | |
611 | wxString title; | |
612 | wxMenu *menu = CreateDummyMenu(&title); | |
613 | GetMenuBar()->Append(menu, title); | |
614 | } | |
615 | ||
616 | void MyFrame::OnToggleMenu(wxCommandEvent& WXUNUSED(event)) | |
617 | { | |
618 | wxMenuBar *mbar = GetMenuBar(); | |
619 | if ( !m_menu ) | |
620 | { | |
621 | // hide the menu | |
622 | m_menu = mbar->Remove(0); | |
623 | } | |
624 | else | |
625 | { | |
626 | // restore it | |
627 | mbar->Insert(0, m_menu, _T("&File")); | |
628 | m_menu = NULL; | |
629 | } | |
630 | } | |
631 | ||
632 | void MyFrame::OnEnableMenu(wxCommandEvent& event) | |
633 | { | |
634 | wxMenuBar *mbar = GetMenuBar(); | |
635 | size_t count = mbar->GetMenuCount(); | |
636 | ||
637 | mbar->EnableTop(count - 1, event.IsChecked()); | |
638 | } | |
639 | ||
640 | void MyFrame::OnGetLabelMenu(wxCommandEvent& WXUNUSED(event)) | |
641 | { | |
642 | wxMenuBar *mbar = GetMenuBar(); | |
643 | size_t count = mbar->GetMenuCount(); | |
644 | ||
645 | wxCHECK_RET( count, _T("no last menu?") ); | |
646 | ||
647 | wxLogMessage(_T("The label of the last menu item is '%s'"), | |
648 | mbar->GetLabelTop(count - 1).c_str()); | |
649 | } | |
650 | ||
651 | #if wxUSE_TEXTDLG | |
652 | void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event)) | |
653 | { | |
654 | wxMenuBar *mbar = GetMenuBar(); | |
655 | size_t count = mbar->GetMenuCount(); | |
656 | ||
657 | wxCHECK_RET( count, _T("no last menu?") ); | |
658 | ||
659 | wxString label = wxGetTextFromUser | |
660 | ( | |
661 | _T("Enter new label: "), | |
662 | _T("Change last menu text"), | |
663 | mbar->GetLabelTop(count - 1), | |
664 | this | |
665 | ); | |
666 | ||
667 | if ( !label.empty() ) | |
668 | { | |
669 | mbar->SetLabelTop(count - 1, label); | |
670 | } | |
671 | } | |
672 | ||
673 | void MyFrame::OnFindMenu(wxCommandEvent& WXUNUSED(event)) | |
674 | { | |
675 | wxMenuBar *mbar = GetMenuBar(); | |
676 | size_t count = mbar->GetMenuCount(); | |
677 | ||
678 | wxCHECK_RET( count, _T("no last menu?") ); | |
679 | ||
680 | wxString label = wxGetTextFromUser | |
681 | ( | |
682 | _T("Enter label to search for: "), | |
683 | _T("Find menu"), | |
684 | wxEmptyString, | |
685 | this | |
686 | ); | |
687 | ||
688 | if ( !label.empty() ) | |
689 | { | |
690 | int index = mbar->FindMenu(label); | |
691 | ||
692 | if (index == wxNOT_FOUND) | |
693 | { | |
694 | wxLogWarning(_T("No menu with label '%s'"), label.c_str()); | |
695 | } | |
696 | else | |
697 | { | |
698 | wxLogMessage(_T("Menu %d has label '%s'"), index, label.c_str()); | |
699 | } | |
700 | } | |
701 | } | |
702 | #endif | |
703 | ||
704 | void MyFrame::OnDummy(wxCommandEvent& event) | |
705 | { | |
706 | wxLogMessage(_T("Dummy item #%d"), event.GetId() - Menu_Dummy_First + 1); | |
707 | } | |
708 | ||
709 | void MyFrame::OnAppendMenuItem(wxCommandEvent& WXUNUSED(event)) | |
710 | { | |
711 | wxMenuBar *menubar = GetMenuBar(); | |
712 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); | |
713 | ||
714 | menu->AppendSeparator(); | |
715 | menu->Append(Menu_Dummy_Third, _T("&Third dummy item\tCtrl-F3"), | |
716 | _T("Checkable item"), true); | |
717 | } | |
718 | ||
719 | void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event)) | |
720 | { | |
721 | wxMenuBar *menubar = GetMenuBar(); | |
722 | ||
723 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 2); | |
724 | ||
725 | menu->Append(Menu_Dummy_Last, _T("&Dummy sub menu"), | |
726 | CreateDummyMenu(NULL), _T("Dummy sub menu help")); | |
727 | } | |
728 | ||
729 | void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event)) | |
730 | { | |
731 | wxMenuBar *menubar = GetMenuBar(); | |
732 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); | |
733 | ||
734 | size_t count = menu->GetMenuItemCount(); | |
735 | if ( !count ) | |
736 | { | |
737 | wxLogWarning(_T("No items to delete!")); | |
738 | } | |
739 | else | |
740 | { | |
741 | menu->Destroy(menu->GetMenuItems().Item(count - 1)->GetData()); | |
742 | } | |
743 | } | |
744 | ||
745 | void MyFrame::OnInsertMenuItem(wxCommandEvent& WXUNUSED(event)) | |
746 | { | |
747 | wxMenuBar *menubar = GetMenuBar(); | |
748 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); | |
749 | ||
750 | menu->Insert(0, wxMenuItem::New(menu, Menu_Dummy_Fourth, | |
751 | _T("Fourth dummy item\tCtrl-F4"))); | |
752 | menu->Insert(1, wxMenuItem::New(menu, wxID_SEPARATOR)); | |
753 | } | |
754 | ||
755 | void MyFrame::OnEnableMenuItem(wxCommandEvent& WXUNUSED(event)) | |
756 | { | |
757 | wxMenuItem *item = GetLastMenuItem(); | |
758 | ||
759 | if ( item ) | |
760 | { | |
761 | item->Enable(!item->IsEnabled()); | |
762 | } | |
763 | } | |
764 | ||
765 | void MyFrame::OnCheckMenuItem(wxCommandEvent& WXUNUSED(event)) | |
766 | { | |
767 | wxMenuItem *item = GetLastMenuItem(); | |
768 | ||
769 | item->Toggle(); | |
770 | } | |
771 | ||
772 | void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event) | |
773 | { | |
774 | wxLogNull nolog; | |
775 | ||
776 | wxMenuItem *item = GetLastMenuItem(); | |
777 | ||
778 | event.Enable(item && item->IsCheckable()); | |
779 | } | |
780 | ||
781 | void MyFrame::OnGetLabelMenuItem(wxCommandEvent& WXUNUSED(event)) | |
782 | { | |
783 | wxMenuItem *item = GetLastMenuItem(); | |
784 | ||
785 | if ( item ) | |
786 | { | |
787 | wxLogMessage(_T("The label of the last menu item is '%s'"), | |
788 | item->GetLabel().c_str()); | |
789 | } | |
790 | } | |
791 | ||
792 | #if wxUSE_TEXTDLG | |
793 | void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event)) | |
794 | { | |
795 | wxMenuItem *item = GetLastMenuItem(); | |
796 | ||
797 | if ( item ) | |
798 | { | |
799 | wxString label = wxGetTextFromUser | |
800 | ( | |
801 | _T("Enter new label: "), | |
802 | _T("Change last menu item text"), | |
803 | item->GetLabel(), | |
804 | this | |
805 | ); | |
806 | label.Replace( _T("\\t"), _T("\t") ); | |
807 | ||
808 | if ( !label.empty() ) | |
809 | { | |
810 | item->SetText(label); | |
811 | } | |
812 | } | |
813 | } | |
814 | #endif | |
815 | ||
816 | void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event)) | |
817 | { | |
818 | wxMenuItem *item = GetLastMenuItem(); | |
819 | ||
820 | if ( item ) | |
821 | { | |
822 | wxString msg; | |
823 | msg << _T("The item is ") << (item->IsEnabled() ? _T("enabled") | |
824 | : _T("disabled")) | |
825 | << _T('\n'); | |
826 | ||
827 | if ( item->IsCheckable() ) | |
828 | { | |
829 | msg << _T("It is checkable and ") << (item->IsChecked() ? _T("") : _T("un")) | |
830 | << _T("checked\n"); | |
831 | } | |
832 | ||
833 | #if wxUSE_ACCEL | |
834 | wxAcceleratorEntry *accel = item->GetAccel(); | |
835 | if ( accel ) | |
836 | { | |
837 | msg << _T("Its accelerator is "); | |
838 | ||
839 | int flags = accel->GetFlags(); | |
840 | if ( flags & wxACCEL_ALT ) | |
841 | msg << _T("Alt-"); | |
842 | if ( flags & wxACCEL_CTRL ) | |
843 | msg << _T("Ctrl-"); | |
844 | if ( flags & wxACCEL_SHIFT ) | |
845 | msg << _T("Shift-"); | |
846 | ||
847 | int code = accel->GetKeyCode(); | |
848 | switch ( code ) | |
849 | { | |
850 | case WXK_F1: | |
851 | case WXK_F2: | |
852 | case WXK_F3: | |
853 | case WXK_F4: | |
854 | case WXK_F5: | |
855 | case WXK_F6: | |
856 | case WXK_F7: | |
857 | case WXK_F8: | |
858 | case WXK_F9: | |
859 | case WXK_F10: | |
860 | case WXK_F11: | |
861 | case WXK_F12: | |
862 | msg << _T('F') << code - WXK_F1 + 1; | |
863 | break; | |
864 | ||
865 | // if there are any other keys wxGetAccelFromString() may return, | |
866 | // we should process them here | |
867 | ||
868 | default: | |
869 | if ( wxIsalnum(code) ) | |
870 | { | |
871 | msg << (wxChar)code; | |
872 | ||
873 | break; | |
874 | } | |
875 | ||
876 | wxFAIL_MSG( _T("unknown keyboard accel") ); | |
877 | } | |
878 | ||
879 | delete accel; | |
880 | } | |
881 | else | |
882 | { | |
883 | msg << _T("It doesn't have an accelerator"); | |
884 | } | |
885 | #endif // wxUSE_ACCEL | |
886 | ||
887 | wxLogMessage(msg); | |
888 | } | |
889 | } | |
890 | ||
891 | #if wxUSE_TEXTDLG | |
892 | void MyFrame::OnFindMenuItem(wxCommandEvent& WXUNUSED(event)) | |
893 | { | |
894 | wxMenuBar *mbar = GetMenuBar(); | |
895 | size_t count = mbar->GetMenuCount(); | |
896 | ||
897 | wxCHECK_RET( count, _T("no last menu?") ); | |
898 | ||
899 | wxString label = wxGetTextFromUser | |
900 | ( | |
901 | _T("Enter label to search for: "), | |
902 | _T("Find menu item"), | |
903 | wxEmptyString, | |
904 | this | |
905 | ); | |
906 | ||
907 | if ( !label.empty() ) | |
908 | { | |
909 | size_t menuindex; | |
910 | int index = wxNOT_FOUND; | |
911 | ||
912 | for (menuindex = 0; (menuindex < count) && (index == wxNOT_FOUND); ++menuindex) | |
913 | { | |
914 | index = mbar->FindMenuItem(mbar->GetMenu(menuindex)->GetTitle(), label); | |
915 | } | |
916 | if (index == wxNOT_FOUND) | |
917 | { | |
918 | wxLogWarning(_T("No menu item with label '%s'"), label.c_str()); | |
919 | } | |
920 | else | |
921 | { | |
922 | wxLogMessage(_T("Menu item %d in menu %lu has label '%s'"), | |
923 | index, (unsigned long)menuindex, label.c_str()); | |
924 | } | |
925 | } | |
926 | } | |
927 | #endif | |
928 | ||
929 | void MyFrame::ShowContextMenu(const wxPoint& pos) | |
930 | { | |
931 | wxMenu menu(_T("Test popup")); | |
932 | ||
933 | menu.Append(Menu_Help_About, _T("&About")); | |
934 | menu.Append(Menu_Popup_Submenu, _T("&Submenu"), CreateDummyMenu(NULL)); | |
935 | menu.Append(Menu_Popup_ToBeDeleted, _T("To be &deleted")); | |
936 | menu.AppendCheckItem(Menu_Popup_ToBeChecked, _T("To be &checked")); | |
937 | menu.Append(Menu_Popup_ToBeGreyed, _T("To be &greyed"), | |
938 | _T("This menu item should be initially greyed out")); | |
939 | menu.AppendSeparator(); | |
940 | menu.Append(Menu_File_Quit, _T("E&xit")); | |
941 | ||
942 | menu.Delete(Menu_Popup_ToBeDeleted); | |
943 | menu.Check(Menu_Popup_ToBeChecked, true); | |
944 | menu.Enable(Menu_Popup_ToBeGreyed, false); | |
945 | ||
946 | PopupMenu(&menu, pos.x, pos.y); | |
947 | ||
948 | // test for destroying items in popup menus | |
949 | #if 0 // doesn't work in wxGTK! | |
950 | menu.Destroy(Menu_Popup_Submenu); | |
951 | ||
952 | PopupMenu( &menu, event.GetX(), event.GetY() ); | |
953 | #endif // 0 | |
954 | } | |
955 | ||
956 | void MyFrame::OnTestNormal(wxCommandEvent& WXUNUSED(event)) | |
957 | { | |
958 | wxLogMessage(_T("Normal item selected")); | |
959 | } | |
960 | ||
961 | void MyFrame::OnTestCheck(wxCommandEvent& event) | |
962 | { | |
963 | wxLogMessage(_T("Check item %schecked"), | |
964 | event.IsChecked() ? _T("") : _T("un")); | |
965 | } | |
966 | ||
967 | void MyFrame::OnTestRadio(wxCommandEvent& event) | |
968 | { | |
969 | wxLogMessage(_T("Radio item %d selected"), | |
970 | event.GetId() - Menu_Test_Radio1 + 1); | |
971 | } | |
972 | ||
973 | #if USE_LOG_WINDOW | |
974 | void MyFrame::LogMenuOpenOrClose(const wxMenuEvent& event, const wxChar *what) | |
975 | { | |
976 | wxString msg; | |
977 | msg << _T("A ") | |
978 | << ( event.IsPopup() ? _T("popup ") : _T("") ) | |
979 | << _T("menu has been ") | |
980 | << what | |
981 | << _T("."); | |
982 | ||
983 | wxLogStatus(this, msg.c_str()); | |
984 | } | |
985 | #endif | |
986 | ||
987 | void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent& event) | |
988 | { | |
989 | event.Enable(false); | |
990 | } | |
991 | ||
992 | void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent& event) | |
993 | { | |
994 | event.Enable(true); | |
995 | event.Check(true); | |
996 | } | |
997 | ||
998 | void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent& event) | |
999 | { | |
1000 | int which = (event.GetId() - Menu_SubMenu_Radio1 + 1); | |
1001 | if (which == 2) | |
1002 | event.Check(true); | |
1003 | else | |
1004 | event.Check(false); | |
1005 | } | |
1006 | ||
1007 | #if USE_CONTEXT_MENU | |
1008 | void MyFrame::OnContextMenu(wxContextMenuEvent& event) | |
1009 | { | |
1010 | wxPoint point = event.GetPosition(); | |
1011 | // If from keyboard | |
1012 | if (point.x == -1 && point.y == -1) { | |
1013 | wxSize size = GetSize(); | |
1014 | point.x = size.x / 2; | |
1015 | point.y = size.y / 2; | |
1016 | } else { | |
1017 | point = ScreenToClient(point); | |
1018 | } | |
1019 | ShowContextMenu(point); | |
1020 | } | |
1021 | #endif | |
1022 | ||
1023 | void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event)) | |
1024 | { | |
1025 | #if USE_LOG_WINDOW | |
1026 | if ( !m_textctrl ) | |
1027 | return; | |
1028 | ||
1029 | // leave a band below for popup menu testing | |
1030 | wxSize size = GetClientSize(); | |
1031 | m_textctrl->SetSize(0, 0, size.x, (3*size.y)/4); | |
1032 | #endif | |
1033 | ||
1034 | // this is really ugly but we have to do it as we can't just call | |
1035 | // event.Skip() because wxFrameBase would make the text control fill the | |
1036 | // entire frame then | |
1037 | #ifdef __WXUNIVERSAL__ | |
1038 | PositionMenuBar(); | |
1039 | #endif // __WXUNIVERSAL__ | |
1040 | } | |
1041 |