1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/menu.cpp
3 // Purpose: wxMenu/wxMenuBar sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
31 #include "wx/msgdlg.h"
33 #include "wx/textctrl.h"
34 #include "wx/textdlg.h"
39 #error "menu sample requires wxUSE_MENUS=1"
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__)
45 #define USE_CONTEXT_MENU 0
47 #define USE_CONTEXT_MENU 1
50 // this sample is useful when a new port is developed
51 // and usually a new port has majority of flags turned off
52 #if wxUSE_LOG && wxUSE_TEXTCTRL
53 #define USE_LOG_WINDOW 1
55 #define USE_LOG_WINDOW 0
60 #ifndef wxHAS_IMAGES_IN_RESOURCES
61 #include "../sample.xpm"
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // Define a new application
69 class MyApp
: public wxApp
76 class MyFrame
: public wxFrame
83 void LogMenuEvent(const wxCommandEvent
& event
);
86 void OnQuit(wxCommandEvent
& event
);
88 void OnClearLog(wxCommandEvent
& event
);
89 void OnClearLogUpdateUI(wxUpdateUIEvent
& event
);
90 #endif // USE_LOG_WINDOW
91 void OnShowDialog(wxCommandEvent
& event
);
93 void OnAbout(wxCommandEvent
& event
);
95 void OnDummy(wxCommandEvent
& event
);
97 void OnAppendMenuItem(wxCommandEvent
& event
);
98 void OnAppendSubMenu(wxCommandEvent
& event
);
99 void OnDeleteMenuItem(wxCommandEvent
& event
);
100 void OnDeleteSubMenu(wxCommandEvent
& event
);
101 void OnInsertMenuItem(wxCommandEvent
& event
);
102 void OnCheckMenuItem(wxCommandEvent
& event
);
103 void OnEnableMenuItem(wxCommandEvent
& event
);
104 void OnGetLabelMenuItem(wxCommandEvent
& event
);
106 void OnSetLabelMenuItem(wxCommandEvent
& event
);
108 void OnGetMenuItemInfo(wxCommandEvent
& event
);
110 void OnFindMenuItem(wxCommandEvent
& event
);
113 void OnAppendMenu(wxCommandEvent
& event
);
114 void OnInsertMenu(wxCommandEvent
& event
);
115 void OnDeleteMenu(wxCommandEvent
& event
);
116 void OnToggleMenu(wxCommandEvent
& event
);
117 void OnEnableMenu(wxCommandEvent
& event
);
118 void OnGetLabelMenu(wxCommandEvent
& event
);
119 void OnSetLabelMenu(wxCommandEvent
& event
);
121 void OnFindMenu(wxCommandEvent
& event
);
124 void OnTestNormal(wxCommandEvent
& event
);
125 void OnTestCheck(wxCommandEvent
& event
);
126 void OnTestRadio(wxCommandEvent
& event
);
128 void OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
);
129 void OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
);
130 void OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
);
133 void OnContextMenu(wxContextMenuEvent
& event
);
135 void OnRightUp(wxMouseEvent
& event
)
136 { ShowContextMenu(event
.GetPosition()); }
139 void OnMenuOpen(wxMenuEvent
& event
)
142 LogMenuOpenCloseOrHighlight(event
, wxT("opened")); event
.Skip();
145 void OnMenuClose(wxMenuEvent
& event
)
148 LogMenuOpenCloseOrHighlight(event
, wxT("closed")); event
.Skip();
151 void OnMenuHighlight(wxMenuEvent
& event
)
154 LogMenuOpenCloseOrHighlight(event
, wxT("highlighted")); event
.Skip();
158 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
160 void OnSize(wxSizeEvent
& event
);
164 void LogMenuOpenCloseOrHighlight(const wxMenuEvent
& event
, const wxChar
*what
);
166 void ShowContextMenu(const wxPoint
& pos
);
168 wxMenu
*CreateDummyMenu(wxString
*title
);
170 wxMenuItem
*GetLastMenuItem() const;
172 // the menu previously detached from the menubar (may be NULL)
175 // the count of dummy menus already created
179 // the control used for logging
180 wxTextCtrl
*m_textctrl
;
183 // the previous log target
186 DECLARE_EVENT_TABLE()
189 class MyDialog
: public wxDialog
192 MyDialog(wxWindow
* parent
);
195 void OnContextMenu(wxContextMenuEvent
& event
);
197 void OnRightUp(wxMouseEvent
& event
)
198 { ShowContextMenu(event
.GetPosition()); }
201 void OnMenuOpen(wxMenuEvent
& event
)
204 LogMenuOpenCloseOrHighlight(event
, wxT("opened")); event
.Skip();
207 void OnMenuClose(wxMenuEvent
& event
)
210 LogMenuOpenCloseOrHighlight(event
, wxT("closed")); event
.Skip();
213 void OnMenuHighlight(wxMenuEvent
& event
)
216 LogMenuOpenCloseOrHighlight(event
, wxT("highlighted")); event
.Skip();
222 void LogMenuOpenCloseOrHighlight(const wxMenuEvent
& event
, const wxChar
*what
);
224 void ShowContextMenu(const wxPoint
& pos
);
227 // the control used for logging
228 wxTextCtrl
*m_textctrl
;
231 DECLARE_EVENT_TABLE()
234 // A small helper class which intercepts all menu events and logs them
235 class MyEvtHandler
: public wxEvtHandler
238 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
240 void OnMenuEvent(wxCommandEvent
& event
)
242 m_frame
->LogMenuEvent(event
);
250 DECLARE_EVENT_TABLE()
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
259 Menu_File_Quit
= wxID_EXIT
,
261 Menu_File_ClearLog
= 100,
263 Menu_File_ShowDialog
,
265 Menu_MenuBar_Toggle
= 200,
270 Menu_MenuBar_GetLabel
,
272 Menu_MenuBar_SetLabel
,
273 Menu_MenuBar_FindMenu
,
276 Menu_Menu_Append
= 300,
292 Menu_Test_Normal
= 400,
305 Menu_Dummy_First
= 500,
311 Menu_Help_About
= wxID_ABOUT
,
313 Menu_Popup_ToBeDeleted
= 2000,
314 Menu_Popup_ToBeGreyed
,
315 Menu_Popup_ToBeChecked
,
323 // ----------------------------------------------------------------------------
325 // ----------------------------------------------------------------------------
327 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
328 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
330 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
331 EVT_UPDATE_UI(Menu_File_ClearLog
, MyFrame::OnClearLogUpdateUI
)
333 EVT_MENU(Menu_File_ShowDialog
, MyFrame::OnShowDialog
)
335 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
337 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
338 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
339 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
340 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
341 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
342 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
344 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
345 EVT_MENU(Menu_MenuBar_FindMenu
, MyFrame::OnFindMenu
)
348 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
349 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
350 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
351 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
352 EVT_MENU(Menu_Menu_DeleteSub
, MyFrame::OnDeleteSubMenu
)
353 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
354 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
355 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
357 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
359 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
361 EVT_MENU(Menu_Menu_FindItem
, MyFrame::OnFindMenuItem
)
364 EVT_MENU(Menu_Test_Normal
, MyFrame::OnTestNormal
)
365 EVT_MENU(Menu_Test_Check
, MyFrame::OnTestCheck
)
366 EVT_MENU(Menu_Test_Radio1
, MyFrame::OnTestRadio
)
367 EVT_MENU(Menu_Test_Radio2
, MyFrame::OnTestRadio
)
368 EVT_MENU(Menu_Test_Radio3
, MyFrame::OnTestRadio
)
370 EVT_UPDATE_UI(Menu_SubMenu_Normal
, MyFrame::OnUpdateSubMenuNormal
)
371 EVT_UPDATE_UI(Menu_SubMenu_Check
, MyFrame::OnUpdateSubMenuCheck
)
372 EVT_UPDATE_UI(Menu_SubMenu_Radio1
, MyFrame::OnUpdateSubMenuRadio
)
373 EVT_UPDATE_UI(Menu_SubMenu_Radio2
, MyFrame::OnUpdateSubMenuRadio
)
374 EVT_UPDATE_UI(Menu_SubMenu_Radio3
, MyFrame::OnUpdateSubMenuRadio
)
376 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
378 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
381 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
383 EVT_RIGHT_UP(MyFrame::OnRightUp
)
386 EVT_MENU_OPEN(MyFrame::OnMenuOpen
)
387 EVT_MENU_CLOSE(MyFrame::OnMenuClose
)
388 EVT_MENU_HIGHLIGHT_ALL(MyFrame::OnMenuHighlight
)
390 EVT_SIZE(MyFrame::OnSize
)
393 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
395 EVT_CONTEXT_MENU(MyDialog::OnContextMenu
)
397 EVT_RIGHT_UP(MyDialog::OnRightUp
)
399 EVT_MENU_OPEN(MyDialog::OnMenuOpen
)
400 EVT_MENU_CLOSE(MyDialog::OnMenuClose
)
401 EVT_MENU_HIGHLIGHT_ALL(MyDialog::OnMenuHighlight
)
404 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
405 EVT_MENU(wxID_ANY
, MyEvtHandler::OnMenuEvent
)
408 // ============================================================================
410 // ============================================================================
412 // ----------------------------------------------------------------------------
414 // ----------------------------------------------------------------------------
418 // The `main program' equivalent, creating the windows and returning the
422 if ( !wxApp::OnInit() )
425 // Create the main frame window
426 MyFrame
* frame
= new MyFrame
;
431 frame
->SetStatusText(wxT("Welcome to wxWidgets menu sample"));
432 #endif // wxUSE_STATUSBAR
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
441 // Define my frame constructor
443 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, wxT("wxWidgets menu sample"))
445 SetIcon(wxICON(sample
));
456 #endif // wxUSE_STATUSBAR
458 // create the menubar
459 wxMenu
*fileMenu
= new wxMenu
;
461 wxMenu
*stockSubMenu
= new wxMenu
;
462 stockSubMenu
->Append(wxID_ADD
);
463 stockSubMenu
->Append(wxID_APPLY
);
464 stockSubMenu
->Append(wxID_BACKWARD
);
465 stockSubMenu
->Append(wxID_BOLD
);
466 stockSubMenu
->Append(wxID_BOTTOM
);
467 stockSubMenu
->Append(wxID_CANCEL
);
468 stockSubMenu
->Append(wxID_CDROM
);
469 stockSubMenu
->Append(wxID_CLEAR
);
470 stockSubMenu
->Append(wxID_CLOSE
);
471 stockSubMenu
->Append(wxID_CONVERT
);
472 stockSubMenu
->Append(wxID_COPY
);
473 stockSubMenu
->Append(wxID_CUT
);
474 stockSubMenu
->Append(wxID_DELETE
);
475 stockSubMenu
->Append(wxID_DOWN
);
476 stockSubMenu
->Append(wxID_EXECUTE
);
477 stockSubMenu
->Append(wxID_EXIT
);
478 stockSubMenu
->Append(wxID_FIND
);
479 stockSubMenu
->Append(wxID_FIRST
);
480 stockSubMenu
->Append(wxID_FLOPPY
);
481 stockSubMenu
->Append(wxID_FORWARD
);
482 stockSubMenu
->Append(wxID_HARDDISK
);
483 stockSubMenu
->Append(wxID_HELP
);
484 stockSubMenu
->Append(wxID_HOME
);
485 stockSubMenu
->Append(wxID_INDENT
);
486 stockSubMenu
->Append(wxID_INDEX
);
487 stockSubMenu
->Append(wxID_INFO
);
488 stockSubMenu
->Append(wxID_ITALIC
);
489 stockSubMenu
->Append(wxID_JUMP_TO
);
490 stockSubMenu
->Append(wxID_JUSTIFY_CENTER
);
491 stockSubMenu
->Append(wxID_JUSTIFY_FILL
);
492 stockSubMenu
->Append(wxID_JUSTIFY_LEFT
);
493 stockSubMenu
->Append(wxID_JUSTIFY_RIGHT
);
494 stockSubMenu
->Append(wxID_LAST
);
495 stockSubMenu
->Append(wxID_NETWORK
);
496 stockSubMenu
->Append(wxID_NEW
);
497 stockSubMenu
->Append(wxID_NO
);
498 stockSubMenu
->Append(wxID_OK
);
499 stockSubMenu
->Append(wxID_OPEN
);
500 stockSubMenu
->Append(wxID_PASTE
);
501 stockSubMenu
->Append(wxID_PREFERENCES
);
502 stockSubMenu
->Append(wxID_PREVIEW
);
503 stockSubMenu
->Append(wxID_PRINT
);
504 stockSubMenu
->Append(wxID_PROPERTIES
);
505 stockSubMenu
->Append(wxID_REDO
);
506 stockSubMenu
->Append(wxID_REFRESH
);
507 stockSubMenu
->Append(wxID_REMOVE
);
508 stockSubMenu
->Append(wxID_REPLACE
);
509 stockSubMenu
->Append(wxID_REVERT_TO_SAVED
);
510 stockSubMenu
->Append(wxID_SAVE
);
511 stockSubMenu
->Append(wxID_SAVEAS
);
512 stockSubMenu
->Append(wxID_SELECT_COLOR
);
513 stockSubMenu
->Append(wxID_SELECT_FONT
);
514 stockSubMenu
->Append(wxID_SORT_ASCENDING
);
515 stockSubMenu
->Append(wxID_SORT_DESCENDING
);
516 stockSubMenu
->Append(wxID_SPELL_CHECK
);
517 stockSubMenu
->Append(wxID_STOP
);
518 stockSubMenu
->Append(wxID_STRIKETHROUGH
);
519 stockSubMenu
->Append(wxID_TOP
);
520 stockSubMenu
->Append(wxID_UNDELETE
);
521 stockSubMenu
->Append(wxID_UNDERLINE
);
522 stockSubMenu
->Append(wxID_UNDO
);
523 stockSubMenu
->Append(wxID_UNINDENT
);
524 stockSubMenu
->Append(wxID_UP
);
525 stockSubMenu
->Append(wxID_YES
);
526 stockSubMenu
->Append(wxID_ZOOM_100
);
527 stockSubMenu
->Append(wxID_ZOOM_FIT
);
528 stockSubMenu
->Append(wxID_ZOOM_IN
);
529 stockSubMenu
->Append(wxID_ZOOM_OUT
);
530 fileMenu
->AppendSubMenu(stockSubMenu
, wxT("&Standard items demo"));
533 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
534 wxT("Clear &log\tCtrl-L"));
535 item
->SetBitmap(copy_xpm
);
536 fileMenu
->Append(item
);
537 fileMenu
->AppendSeparator();
538 #endif // USE_LOG_WINDOW
540 fileMenu
->Append(Menu_File_ShowDialog
, wxT("Show &Dialog\tCtrl-D"),
541 wxT("Show a dialog"));
542 fileMenu
->AppendSeparator();
544 fileMenu
->Append(Menu_File_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit menu sample"));
546 wxMenu
*menubarMenu
= new wxMenu
;
547 menubarMenu
->Append(Menu_MenuBar_Append
, wxT("&Append menu\tCtrl-A"),
548 wxT("Append a menu to the menubar"));
549 menubarMenu
->Append(Menu_MenuBar_Insert
, wxT("&Insert menu\tCtrl-I"),
550 wxT("Insert a menu into the menubar"));
551 menubarMenu
->Append(Menu_MenuBar_Delete
, wxT("&Delete menu\tCtrl-D"),
552 wxT("Delete the last menu from the menubar"));
553 menubarMenu
->Append(Menu_MenuBar_Toggle
, wxT("&Toggle menu\tCtrl-T"),
554 wxT("Toggle the first menu in the menubar"), true);
555 menubarMenu
->AppendSeparator();
556 menubarMenu
->Append(Menu_MenuBar_Enable
, wxT("&Enable menu\tCtrl-E"),
557 wxT("Enable or disable the last menu"), true);
558 menubarMenu
->AppendSeparator();
559 menubarMenu
->Append(Menu_MenuBar_GetLabel
, wxT("&Get menu label\tCtrl-G"),
560 wxT("Get the label of the last menu"));
562 menubarMenu
->Append(Menu_MenuBar_SetLabel
, wxT("&Set menu label\tCtrl-S"),
563 wxT("Change the label of the last menu"));
564 menubarMenu
->AppendSeparator();
565 menubarMenu
->Append(Menu_MenuBar_FindMenu
, wxT("&Find menu from label\tCtrl-F"),
566 wxT("Find a menu by searching for its label"));
569 wxMenu
* subMenu
= new wxMenu
;
570 subMenu
->Append(Menu_SubMenu_Normal
, wxT("&Normal submenu item"), wxT("Disabled submenu item"));
571 subMenu
->AppendCheckItem(Menu_SubMenu_Check
, wxT("&Check submenu item"), wxT("Check submenu item"));
572 subMenu
->AppendRadioItem(Menu_SubMenu_Radio1
, wxT("Radio item &1"), wxT("Radio item"));
573 subMenu
->AppendRadioItem(Menu_SubMenu_Radio2
, wxT("Radio item &2"), wxT("Radio item"));
574 subMenu
->AppendRadioItem(Menu_SubMenu_Radio3
, wxT("Radio item &3"), wxT("Radio item"));
576 menubarMenu
->Append(Menu_SubMenu
, wxT("Submenu"), subMenu
);
578 wxMenu
*menuMenu
= new wxMenu
;
579 menuMenu
->Append(Menu_Menu_Append
, wxT("&Append menu item\tAlt-A"),
580 wxT("Append a menu item to the 'Test' menu"));
581 menuMenu
->Append(Menu_Menu_AppendSub
, wxT("&Append sub menu\tAlt-S"),
582 wxT("Append a sub menu to the 'Test' menu"));
583 menuMenu
->Append(Menu_Menu_Insert
, wxT("&Insert menu item\tAlt-I"),
584 wxT("Insert a menu item in head of the 'Test' menu"));
585 menuMenu
->Append(Menu_Menu_Delete
, wxT("&Delete menu item\tAlt-D"),
586 wxT("Delete the last menu item from the 'Test' menu"));
587 menuMenu
->Append(Menu_Menu_DeleteSub
, wxT("Delete last &submenu\tAlt-K"),
588 wxT("Delete the last submenu from the 'Test' menu"));
589 menuMenu
->AppendSeparator();
590 menuMenu
->Append(Menu_Menu_Enable
, wxT("&Enable menu item\tAlt-E"),
591 wxT("Enable or disable the last menu item"), true);
592 menuMenu
->Append(Menu_Menu_Check
, wxT("&Check menu item\tAlt-C"),
593 wxT("Check or uncheck the last menu item"), true);
594 menuMenu
->AppendSeparator();
595 menuMenu
->Append(Menu_Menu_GetInfo
, wxT("Get menu item in&fo\tAlt-F"),
596 wxT("Show the state of the last menu item"));
598 menuMenu
->Append(Menu_Menu_SetLabel
, wxT("Set menu item label\tAlt-L"),
599 wxT("Set the label of a menu item"));
602 menuMenu
->AppendSeparator();
603 menuMenu
->Append(Menu_Menu_FindItem
, wxT("Find menu item from label"),
604 wxT("Find a menu item by searching for its label"));
607 wxMenu
*testMenu
= new wxMenu
;
608 testMenu
->Append(Menu_Test_Normal
, wxT("&Normal item"));
609 testMenu
->AppendSeparator();
610 testMenu
->AppendCheckItem(Menu_Test_Check
, wxT("&Check item"));
611 testMenu
->AppendSeparator();
612 testMenu
->AppendRadioItem(Menu_Test_Radio1
, wxT("Radio item &1"));
613 testMenu
->AppendRadioItem(Menu_Test_Radio2
, wxT("Radio item &2"));
614 testMenu
->AppendRadioItem(Menu_Test_Radio3
, wxT("Radio item &3"));
616 wxMenu
*helpMenu
= new wxMenu
;
617 helpMenu
->Append(Menu_Help_About
, wxT("&About\tF1"), wxT("About menu sample"));
619 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
621 menuBar
->Append(fileMenu
, wxT("&File"));
622 menuBar
->Append(menubarMenu
, wxT("Menu&bar"));
623 menuBar
->Append(menuMenu
, wxT("&Menu"));
624 menuBar
->Append(testMenu
, wxT("&Test"));
625 menuBar
->Append(helpMenu
, wxT("&Help"));
627 // these items should be initially checked
628 menuBar
->Check(Menu_MenuBar_Toggle
, true);
629 menuBar
->Check(Menu_MenuBar_Enable
, true);
630 menuBar
->Check(Menu_Menu_Enable
, true);
631 menuBar
->Check(Menu_Menu_Check
, false);
633 // associate the menu bar with the frame
636 // intercept all menu events and log them in this custom event handler
637 PushEventHandler(new MyEvtHandler(this));
640 // create the log text window
641 m_textctrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
642 wxDefaultPosition
, wxDefaultSize
,
644 m_textctrl
->SetEditable(false);
646 wxLog::DisableTimestamp();
647 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
649 wxLogMessage(wxT("Brief explanations: the commands in the \"Menu\" menu ")
650 wxT("append/insert/delete items to/from the \"Test\" menu.\n")
651 wxT("The commands in the \"Menubar\" menu work with the ")
652 wxT("menubar itself.\n\n")
653 wxT("Right click the band below to test popup menus.\n"));
664 // delete the event handler installed in ctor
665 PopEventHandler(true);
668 // restore old logger
669 delete wxLog::SetActiveTarget(m_logOld
);
673 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
675 wxMenu
*menu
= new wxMenu
;
676 menu
->Append(Menu_Dummy_First
, wxT("&First item\tCtrl-F1"));
677 menu
->AppendSeparator();
678 menu
->AppendCheckItem(Menu_Dummy_Second
, wxT("&Second item\tCtrl-F2"));
682 title
->Printf(wxT("Dummy menu &%u"), (unsigned)++m_countDummy
);
688 wxMenuItem
*MyFrame::GetLastMenuItem() const
690 wxMenuBar
*menubar
= GetMenuBar();
691 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
692 wxCHECK_MSG( menu
, NULL
, wxT("no 'Test' menu?") );
694 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
697 wxLogWarning(wxT("No last item in the 'Test' menu!"));
703 return node
->GetData();
707 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
709 int id
= event
.GetId();
711 wxString msg
= wxString::Format(wxT("Menu command %d"), id
);
713 // catch all checkable menubar items and also the check item from the popup
715 wxMenuItem
*item
= GetMenuBar()->FindItem(id
);
716 if ( (item
&& item
->IsCheckable()) || id
== Menu_Popup_ToBeChecked
)
718 msg
+= wxString::Format(wxT(" (the item is currently %schecked)"),
719 event
.IsChecked() ? wxT("") : wxT("not "));
725 // ----------------------------------------------------------------------------
727 // ----------------------------------------------------------------------------
729 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
736 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
741 void MyFrame::OnClearLogUpdateUI(wxUpdateUIEvent
& event
)
743 // if we only enable this item when the log window is empty, we never see
744 // it in the disable state as a message is logged whenever the menu is
745 // opened, so we disable it if there is not "much" text in the window
746 event
.Enable( m_textctrl
->GetNumberOfLines() > 5 );
749 #endif // USE_LOG_WINDOW
751 void MyFrame::OnShowDialog(wxCommandEvent
& WXUNUSED(event
))
757 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
759 (void)wxMessageBox(wxT("wxWidgets menu sample\n(c) 1999-2001 Vadim Zeitlin"),
760 wxT("About wxWidgets menu sample"),
761 wxOK
| wxICON_INFORMATION
);
764 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
766 wxMenuBar
*mbar
= GetMenuBar();
768 size_t count
= mbar
->GetMenuCount();
771 // don't let delete the first 4 menus
772 wxLogError(wxT("Can't delete any more menus"));
776 delete mbar
->Remove(count
- 1);
780 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
783 wxMenu
*menu
= CreateDummyMenu(&title
);
784 // Insert before the 'Help' menu
785 // Otherwise repeated Deletes will remove the 'Test' menu
786 GetMenuBar()->Insert(4, menu
, title
);
789 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
792 wxMenu
*menu
= CreateDummyMenu(&title
);
793 GetMenuBar()->Append(menu
, title
);
796 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
798 wxMenuBar
*mbar
= GetMenuBar();
802 m_menu
= mbar
->Remove(0);
807 mbar
->Insert(0, m_menu
, wxT("&File"));
812 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
814 wxMenuBar
*mbar
= GetMenuBar();
815 size_t count
= mbar
->GetMenuCount();
817 mbar
->EnableTop(count
- 1, event
.IsChecked());
820 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
822 wxMenuBar
*mbar
= GetMenuBar();
823 size_t count
= mbar
->GetMenuCount();
825 wxCHECK_RET( count
, wxT("no last menu?") );
827 wxLogMessage(wxT("The label of the last menu item is '%s'"),
828 mbar
->GetMenuLabel(count
- 1).c_str());
832 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
834 wxMenuBar
*mbar
= GetMenuBar();
835 size_t count
= mbar
->GetMenuCount();
837 wxCHECK_RET( count
, wxT("no last menu?") );
839 wxString label
= wxGetTextFromUser
841 wxT("Enter new label: "),
842 wxT("Change last menu text"),
843 mbar
->GetMenuLabel(count
- 1),
847 if ( !label
.empty() )
849 mbar
->SetMenuLabel(count
- 1, label
);
853 void MyFrame::OnFindMenu(wxCommandEvent
& WXUNUSED(event
))
855 wxMenuBar
*mbar
= GetMenuBar();
856 size_t count
= mbar
->GetMenuCount();
858 wxCHECK_RET( count
, wxT("no last menu?") );
860 wxString label
= wxGetTextFromUser
862 wxT("Enter label to search for: "),
868 if ( !label
.empty() )
870 int index
= mbar
->FindMenu(label
);
872 if (index
== wxNOT_FOUND
)
874 wxLogWarning(wxT("No menu with label '%s'"), label
.c_str());
878 wxLogMessage(wxT("Menu %d has label '%s'"), index
, label
.c_str());
884 void MyFrame::OnDummy(wxCommandEvent
& event
)
886 wxLogMessage(wxT("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
889 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
891 wxMenuBar
*menubar
= GetMenuBar();
892 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
893 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
895 menu
->AppendSeparator();
896 menu
->Append(Menu_Dummy_Third
, wxT("&Third dummy item\tCtrl-F3"),
897 wxT("Checkable item"), true);
900 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
902 wxMenuBar
*menubar
= GetMenuBar();
903 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
904 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
906 menu
->Append(Menu_Dummy_Last
, wxT("&Dummy sub menu"),
907 CreateDummyMenu(NULL
), wxT("Dummy sub menu help"));
910 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
912 wxMenuBar
*menubar
= GetMenuBar();
913 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
914 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
916 size_t count
= menu
->GetMenuItemCount();
919 wxLogWarning(wxT("No items to delete!"));
923 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
927 void MyFrame::OnDeleteSubMenu(wxCommandEvent
& WXUNUSED(event
))
929 wxMenuBar
*menubar
= GetMenuBar();
930 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
931 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
933 for ( int n
= menu
->GetMenuItemCount() - 1; n
>=0 ; --n
)
935 wxMenuItem
* item
= menu
->FindItemByPosition(n
);
936 if (item
->IsSubMenu())
943 wxLogWarning(wxT("No submenu to delete!"));
946 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
948 wxMenuBar
*menubar
= GetMenuBar();
949 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
950 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
952 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
953 wxT("Fourth dummy item\tCtrl-F4")));
954 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
));
957 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
959 wxMenuItem
*item
= GetLastMenuItem();
963 item
->Enable(!item
->IsEnabled());
967 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
969 wxMenuItem
*item
= GetLastMenuItem();
971 if (item
&& item
->IsCheckable())
977 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
981 wxMenuItem
*item
= GetLastMenuItem();
983 event
.Enable(item
&& item
->IsCheckable());
986 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
988 wxMenuItem
*item
= GetLastMenuItem();
992 wxString label
= item
->GetItemLabel();
993 wxLogMessage(wxT("The label of the last menu item is '%s'"),
999 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
1001 wxMenuItem
*item
= GetLastMenuItem();
1005 wxString label
= wxGetTextFromUser
1007 wxT("Enter new label: "),
1008 wxT("Change last menu item text"),
1009 item
->GetItemLabel(),
1012 label
.Replace( wxT("\\t"), wxT("\t") );
1014 if ( !label
.empty() )
1016 item
->SetItemLabel(label
);
1022 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
1024 wxMenuItem
*item
= GetLastMenuItem();
1029 msg
<< wxT("The item is ") << (item
->IsEnabled() ? wxT("enabled")
1033 if ( item
->IsCheckable() )
1035 msg
<< wxT("It is checkable and ") << (item
->IsChecked() ? wxT("") : wxT("un"))
1036 << wxT("checked\n");
1040 wxAcceleratorEntry
*accel
= item
->GetAccel();
1043 msg
<< wxT("Its accelerator is ");
1045 int flags
= accel
->GetFlags();
1046 if ( flags
& wxACCEL_ALT
)
1048 if ( flags
& wxACCEL_CTRL
)
1049 msg
<< wxT("Ctrl-");
1050 if ( flags
& wxACCEL_SHIFT
)
1051 msg
<< wxT("Shift-");
1053 int code
= accel
->GetKeyCode();
1068 msg
<< wxT('F') << code
- WXK_F1
+ 1;
1071 // if there are any other keys wxGetAccelFromString() may return,
1072 // we should process them here
1075 if ( wxIsalnum(code
) )
1077 msg
<< (wxChar
)code
;
1082 wxFAIL_MSG( wxT("unknown keyboard accel") );
1089 msg
<< wxT("It doesn't have an accelerator");
1091 #endif // wxUSE_ACCEL
1098 void MyFrame::OnFindMenuItem(wxCommandEvent
& WXUNUSED(event
))
1100 wxMenuBar
*mbar
= GetMenuBar();
1101 size_t count
= mbar
->GetMenuCount();
1103 wxCHECK_RET( count
, wxT("no last menu?") );
1105 wxString label
= wxGetTextFromUser
1107 wxT("Enter label to search for: "),
1108 wxT("Find menu item"),
1113 if ( !label
.empty() )
1116 int index
= wxNOT_FOUND
;
1118 for (menuindex
= 0; (menuindex
< count
) && (index
== wxNOT_FOUND
); ++menuindex
)
1120 index
= mbar
->FindMenuItem(mbar
->GetMenu(menuindex
)->GetTitle(), label
);
1122 if (index
== wxNOT_FOUND
)
1124 wxLogWarning(wxT("No menu item with label '%s'"), label
.c_str());
1128 wxLogMessage(wxT("Menu item %d in menu %lu has label '%s'"),
1129 index
, (unsigned long)menuindex
, label
.c_str());
1135 void MyFrame::ShowContextMenu(const wxPoint
& pos
)
1139 if ( wxGetKeyState(WXK_SHIFT
) )
1141 // when Shift is pressed, demonstrate the use of a simple function
1142 // returning the id of the item selected in the popup menu
1143 menu
.SetTitle("Choose one of:");
1144 static const char *choices
[] = { "Apple", "Banana", "Cherry" };
1145 for ( size_t n
= 0; n
< WXSIZEOF(choices
); n
++ )
1146 menu
.Append(Menu_PopupChoice
+ n
, choices
[n
]);
1148 const int rc
= GetPopupMenuSelectionFromUser(menu
, pos
);
1149 if ( rc
== wxID_NONE
)
1151 wxLogMessage("No selection");
1155 wxLogMessage("You have selected \"%s\"",
1156 choices
[rc
- Menu_PopupChoice
]);
1159 else // normal case, shift not pressed
1161 menu
.Append(Menu_Help_About
, wxT("&About"));
1162 menu
.Append(Menu_Popup_Submenu
, wxT("&Submenu"), CreateDummyMenu(NULL
));
1163 menu
.Append(Menu_Popup_ToBeDeleted
, wxT("To be &deleted"));
1164 menu
.AppendCheckItem(Menu_Popup_ToBeChecked
, wxT("To be &checked"));
1165 menu
.Append(Menu_Popup_ToBeGreyed
, wxT("To be &greyed"),
1166 wxT("This menu item should be initially greyed out"));
1167 menu
.AppendSeparator();
1168 menu
.Append(Menu_File_Quit
, wxT("E&xit"));
1170 menu
.Delete(Menu_Popup_ToBeDeleted
);
1171 menu
.Check(Menu_Popup_ToBeChecked
, true);
1172 menu
.Enable(Menu_Popup_ToBeGreyed
, false);
1174 PopupMenu(&menu
, pos
);
1176 // test for destroying items in popup menus
1177 #if 0 // doesn't work in wxGTK!
1178 menu
.Destroy(Menu_Popup_Submenu
);
1180 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
1185 void MyFrame::OnTestNormal(wxCommandEvent
& WXUNUSED(event
))
1187 wxLogMessage(wxT("Normal item selected"));
1190 void MyFrame::OnTestCheck(wxCommandEvent
& event
)
1192 wxLogMessage(wxT("Check item %schecked"),
1193 event
.IsChecked() ? wxT("") : wxT("un"));
1196 void MyFrame::OnTestRadio(wxCommandEvent
& event
)
1198 wxLogMessage(wxT("Radio item %d selected"),
1199 event
.GetId() - Menu_Test_Radio1
+ 1);
1203 void MyFrame::LogMenuOpenCloseOrHighlight(const wxMenuEvent
& event
, const wxChar
*what
)
1207 << ( event
.IsPopup() ? wxT("popup ") : wxT("") )
1208 << wxT("menu has been ")
1211 if ( event
.GetEventType() == wxEVT_MENU_HIGHLIGHT
)
1213 msg
<< wxT(" (id=") << event
.GetId() << wxT(")");
1218 wxLogStatus(this, msg
.c_str());
1222 void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
)
1224 event
.Enable(false);
1227 void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
)
1232 void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
)
1234 int which
= (event
.GetId() - Menu_SubMenu_Radio1
+ 1);
1241 #if USE_CONTEXT_MENU
1242 void MyFrame::OnContextMenu(wxContextMenuEvent
& event
)
1244 wxPoint point
= event
.GetPosition();
1246 if (point
.x
== -1 && point
.y
== -1) {
1247 wxSize size
= GetSize();
1248 point
.x
= size
.x
/ 2;
1249 point
.y
= size
.y
/ 2;
1251 point
= ScreenToClient(point
);
1253 ShowContextMenu(point
);
1257 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
1263 // leave a band below for popup menu testing
1264 wxSize size
= GetClientSize();
1265 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
1268 // this is really ugly but we have to do it as we can't just call
1269 // event.Skip() because wxFrameBase would make the text control fill the
1270 // entire frame then
1271 #ifdef __WXUNIVERSAL__
1273 #endif // __WXUNIVERSAL__
1276 // ----------------------------------------------------------------------------
1278 // ----------------------------------------------------------------------------
1280 MyDialog::MyDialog(wxWindow
* parent
)
1281 : wxDialog(parent
, wxID_ANY
, "Test Dialog")
1284 // create the log text window
1285 m_textctrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
1286 wxDefaultPosition
, wxDefaultSize
,
1288 m_textctrl
->SetEditable(false);
1290 m_textctrl
->AppendText(wxT("Dialogs do not have menus, but popup menus should function the same\n\n")
1291 wxT("Right click this text ctrl to test popup menus.\n"));
1294 EnableContextMenu();
1299 void MyDialog::LogMenuOpenCloseOrHighlight(const wxMenuEvent
& event
, const wxChar
*what
)
1303 << ( event
.IsPopup() ? wxT("popup ") : wxT("") )
1304 << wxT("menu has been ")
1306 if ( event
.GetEventType() == wxEVT_MENU_HIGHLIGHT
)
1308 msg
<< wxT(" (id=") << event
.GetId() << wxT(")");
1312 m_textctrl
->AppendText(msg
);
1314 #endif // USE_LOG_WINDOW
1315 #if USE_CONTEXT_MENU
1316 void MyDialog::OnContextMenu(wxContextMenuEvent
& event
)
1318 wxPoint point
= event
.GetPosition();
1320 if (point
.x
== -1 && point
.y
== -1) {
1321 wxSize size
= GetSize();
1322 point
.x
= size
.x
/ 2;
1323 point
.y
= size
.y
/ 2;
1325 point
= ScreenToClient(point
);
1327 ShowContextMenu(point
);
1331 void MyDialog::ShowContextMenu(const wxPoint
& pos
)
1335 menu
.Append(Menu_Help_About
, wxT("&About"));
1336 menu
.Append(Menu_Popup_ToBeDeleted
, wxT("To be &deleted"));
1337 menu
.AppendCheckItem(Menu_Popup_ToBeChecked
, wxT("To be &checked"));
1338 menu
.Append(Menu_Popup_ToBeGreyed
, wxT("To be &greyed"),
1339 wxT("This menu item should be initially greyed out"));
1340 menu
.AppendSeparator();
1341 menu
.Append(Menu_File_Quit
, wxT("E&xit"));
1343 menu
.Delete(Menu_Popup_ToBeDeleted
);
1344 menu
.Check(Menu_Popup_ToBeChecked
, true);
1345 menu
.Enable(Menu_Popup_ToBeGreyed
, false);
1347 PopupMenu(&menu
, pos
);