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