1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/menu.cpp
3 // Purpose: wxMenu/wxMenuBar sample
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1999 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
30 #include "wx/msgdlg.h"
32 #include "wx/textctrl.h"
33 #include "wx/textdlg.h"
38 #error "menu sample requires wxUSE_MENUS=1"
41 // not all ports have support for EVT_CONTEXT_MENU yet, don't define
42 // USE_CONTEXT_MENU for those which don't
43 #if defined(__WXMOTIF__) || defined(__WXPM__) || defined(__WXX11__)
44 #define USE_CONTEXT_MENU 0
46 #define USE_CONTEXT_MENU 1
49 // this sample is useful when a new port is developed
50 // and usually a new port has majority of flags turned off
51 #if wxUSE_LOG && wxUSE_TEXTCTRL
52 #define USE_LOG_WINDOW 1
54 #define USE_LOG_WINDOW 0
59 #ifndef wxHAS_IMAGES_IN_RESOURCES
60 #include "../sample.xpm"
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // Define a new application
68 class MyApp
: public wxApp
75 class MyFrame
: public wxFrame
82 void LogMenuEvent(const wxCommandEvent
& event
);
85 void OnQuit(wxCommandEvent
& event
);
87 void OnClearLog(wxCommandEvent
& event
);
88 void OnClearLogUpdateUI(wxUpdateUIEvent
& event
);
89 #endif // USE_LOG_WINDOW
90 void OnShowDialog(wxCommandEvent
& event
);
92 void OnAbout(wxCommandEvent
& event
);
94 void OnDummy(wxCommandEvent
& event
);
96 void OnAppendMenuItem(wxCommandEvent
& event
);
97 void OnAppendSubMenu(wxCommandEvent
& event
);
98 void OnDeleteMenuItem(wxCommandEvent
& event
);
99 void OnDeleteSubMenu(wxCommandEvent
& event
);
100 void OnInsertMenuItem(wxCommandEvent
& event
);
101 void OnCheckMenuItem(wxCommandEvent
& event
);
102 void OnEnableMenuItem(wxCommandEvent
& event
);
103 void OnGetLabelMenuItem(wxCommandEvent
& event
);
105 void OnSetLabelMenuItem(wxCommandEvent
& event
);
107 void OnGetMenuItemInfo(wxCommandEvent
& event
);
109 void OnFindMenuItem(wxCommandEvent
& event
);
112 void OnAppendMenu(wxCommandEvent
& event
);
113 void OnInsertMenu(wxCommandEvent
& event
);
114 void OnDeleteMenu(wxCommandEvent
& event
);
115 void OnToggleMenu(wxCommandEvent
& event
);
116 void OnEnableMenu(wxCommandEvent
& event
);
117 void OnGetLabelMenu(wxCommandEvent
& event
);
118 void OnSetLabelMenu(wxCommandEvent
& event
);
120 void OnFindMenu(wxCommandEvent
& event
);
123 void OnTestNormal(wxCommandEvent
& event
);
124 void OnTestCheck(wxCommandEvent
& event
);
125 void OnTestRadio(wxCommandEvent
& event
);
127 void OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
);
128 void OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
);
129 void OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
);
132 void OnContextMenu(wxContextMenuEvent
& event
);
134 void OnRightUp(wxMouseEvent
& event
)
135 { ShowContextMenu(event
.GetPosition()); }
138 void OnMenuOpen(wxMenuEvent
& event
)
141 LogMenuOpenCloseOrHighlight(event
, wxT("opened")); event
.Skip();
144 void OnMenuClose(wxMenuEvent
& event
)
147 LogMenuOpenCloseOrHighlight(event
, wxT("closed")); event
.Skip();
150 void OnMenuHighlight(wxMenuEvent
& event
)
153 LogMenuOpenCloseOrHighlight(event
, wxT("highlighted")); event
.Skip();
157 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
159 void OnSize(wxSizeEvent
& event
);
163 void LogMenuOpenCloseOrHighlight(const wxMenuEvent
& event
, const wxChar
*what
);
165 void ShowContextMenu(const wxPoint
& pos
);
167 wxMenu
*CreateDummyMenu(wxString
*title
);
169 wxMenuItem
*GetLastMenuItem() const;
171 // the menu previously detached from the menubar (may be NULL)
174 // the count of dummy menus already created
178 // the control used for logging
179 wxTextCtrl
*m_textctrl
;
182 // the previous log target
185 DECLARE_EVENT_TABLE()
188 class MyDialog
: public wxDialog
191 MyDialog(wxWindow
* parent
);
194 void OnContextMenu(wxContextMenuEvent
& event
);
196 void OnRightUp(wxMouseEvent
& event
)
197 { ShowContextMenu(event
.GetPosition()); }
200 void OnMenuOpen(wxMenuEvent
& event
)
203 LogMenuOpenCloseOrHighlight(event
, wxT("opened")); event
.Skip();
206 void OnMenuClose(wxMenuEvent
& event
)
209 LogMenuOpenCloseOrHighlight(event
, wxT("closed")); event
.Skip();
212 void OnMenuHighlight(wxMenuEvent
& event
)
215 LogMenuOpenCloseOrHighlight(event
, wxT("highlighted")); event
.Skip();
221 void LogMenuOpenCloseOrHighlight(const wxMenuEvent
& event
, const wxChar
*what
);
223 void ShowContextMenu(const wxPoint
& pos
);
226 // the control used for logging
227 wxTextCtrl
*m_textctrl
;
230 DECLARE_EVENT_TABLE()
233 // A small helper class which intercepts all menu events and logs them
234 class MyEvtHandler
: public wxEvtHandler
237 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
239 void OnMenuEvent(wxCommandEvent
& event
)
241 m_frame
->LogMenuEvent(event
);
249 DECLARE_EVENT_TABLE()
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
258 Menu_File_Quit
= wxID_EXIT
,
260 Menu_File_ClearLog
= 100,
262 Menu_File_ShowDialog
,
264 Menu_MenuBar_Toggle
= 200,
269 Menu_MenuBar_GetLabel
,
271 Menu_MenuBar_SetLabel
,
272 Menu_MenuBar_FindMenu
,
275 Menu_Menu_Append
= 300,
291 Menu_Test_Normal
= 400,
304 Menu_Dummy_First
= 500,
310 Menu_Help_About
= wxID_ABOUT
,
312 Menu_Popup_ToBeDeleted
= 2000,
313 Menu_Popup_ToBeGreyed
,
314 Menu_Popup_ToBeChecked
,
322 // ----------------------------------------------------------------------------
324 // ----------------------------------------------------------------------------
326 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
327 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
329 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
330 EVT_UPDATE_UI(Menu_File_ClearLog
, MyFrame::OnClearLogUpdateUI
)
332 EVT_MENU(Menu_File_ShowDialog
, MyFrame::OnShowDialog
)
334 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
336 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
337 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
338 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
339 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
340 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
341 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
343 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
344 EVT_MENU(Menu_MenuBar_FindMenu
, MyFrame::OnFindMenu
)
347 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
348 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
349 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
350 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
351 EVT_MENU(Menu_Menu_DeleteSub
, MyFrame::OnDeleteSubMenu
)
352 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
353 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
354 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
356 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
358 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
360 EVT_MENU(Menu_Menu_FindItem
, MyFrame::OnFindMenuItem
)
363 EVT_MENU(Menu_Test_Normal
, MyFrame::OnTestNormal
)
364 EVT_MENU(Menu_Test_Check
, MyFrame::OnTestCheck
)
365 EVT_MENU(Menu_Test_Radio1
, MyFrame::OnTestRadio
)
366 EVT_MENU(Menu_Test_Radio2
, MyFrame::OnTestRadio
)
367 EVT_MENU(Menu_Test_Radio3
, MyFrame::OnTestRadio
)
369 EVT_UPDATE_UI(Menu_SubMenu_Normal
, MyFrame::OnUpdateSubMenuNormal
)
370 EVT_UPDATE_UI(Menu_SubMenu_Check
, MyFrame::OnUpdateSubMenuCheck
)
371 EVT_UPDATE_UI(Menu_SubMenu_Radio1
, MyFrame::OnUpdateSubMenuRadio
)
372 EVT_UPDATE_UI(Menu_SubMenu_Radio2
, MyFrame::OnUpdateSubMenuRadio
)
373 EVT_UPDATE_UI(Menu_SubMenu_Radio3
, MyFrame::OnUpdateSubMenuRadio
)
375 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
377 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
380 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
382 EVT_RIGHT_UP(MyFrame::OnRightUp
)
385 EVT_MENU_OPEN(MyFrame::OnMenuOpen
)
386 EVT_MENU_CLOSE(MyFrame::OnMenuClose
)
387 EVT_MENU_HIGHLIGHT_ALL(MyFrame::OnMenuHighlight
)
389 EVT_SIZE(MyFrame::OnSize
)
392 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
394 EVT_CONTEXT_MENU(MyDialog::OnContextMenu
)
396 EVT_RIGHT_UP(MyDialog::OnRightUp
)
398 EVT_MENU_OPEN(MyDialog::OnMenuOpen
)
399 EVT_MENU_CLOSE(MyDialog::OnMenuClose
)
400 EVT_MENU_HIGHLIGHT_ALL(MyDialog::OnMenuHighlight
)
403 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
404 EVT_MENU(wxID_ANY
, MyEvtHandler::OnMenuEvent
)
407 // ============================================================================
409 // ============================================================================
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
417 // The `main program' equivalent, creating the windows and returning the
421 if ( !wxApp::OnInit() )
424 // Create the main frame window
425 MyFrame
* frame
= new MyFrame
;
430 frame
->SetStatusText(wxT("Welcome to wxWidgets menu sample"));
431 #endif // wxUSE_STATUSBAR
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 // Define my frame constructor
442 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, wxT("wxWidgets menu sample"))
444 SetIcon(wxICON(sample
));
455 #endif // wxUSE_STATUSBAR
457 // create the menubar
458 wxMenu
*fileMenu
= new wxMenu
;
460 wxMenu
*stockSubMenu
= new wxMenu
;
461 stockSubMenu
->Append(wxID_ADD
);
462 stockSubMenu
->Append(wxID_APPLY
);
463 stockSubMenu
->Append(wxID_BACKWARD
);
464 stockSubMenu
->Append(wxID_BOLD
);
465 stockSubMenu
->Append(wxID_BOTTOM
);
466 stockSubMenu
->Append(wxID_CANCEL
);
467 stockSubMenu
->Append(wxID_CDROM
);
468 stockSubMenu
->Append(wxID_CLEAR
);
469 stockSubMenu
->Append(wxID_CLOSE
);
470 stockSubMenu
->Append(wxID_CONVERT
);
471 stockSubMenu
->Append(wxID_COPY
);
472 stockSubMenu
->Append(wxID_CUT
);
473 stockSubMenu
->Append(wxID_DELETE
);
474 stockSubMenu
->Append(wxID_DOWN
);
475 stockSubMenu
->Append(wxID_EXECUTE
);
476 stockSubMenu
->Append(wxID_EXIT
);
477 stockSubMenu
->Append(wxID_FIND
);
478 stockSubMenu
->Append(wxID_FIRST
);
479 stockSubMenu
->Append(wxID_FLOPPY
);
480 stockSubMenu
->Append(wxID_FORWARD
);
481 stockSubMenu
->Append(wxID_HARDDISK
);
482 stockSubMenu
->Append(wxID_HELP
);
483 stockSubMenu
->Append(wxID_HOME
);
484 stockSubMenu
->Append(wxID_INDENT
);
485 stockSubMenu
->Append(wxID_INDEX
);
486 stockSubMenu
->Append(wxID_INFO
);
487 stockSubMenu
->Append(wxID_ITALIC
);
488 stockSubMenu
->Append(wxID_JUMP_TO
);
489 stockSubMenu
->Append(wxID_JUSTIFY_CENTER
);
490 stockSubMenu
->Append(wxID_JUSTIFY_FILL
);
491 stockSubMenu
->Append(wxID_JUSTIFY_LEFT
);
492 stockSubMenu
->Append(wxID_JUSTIFY_RIGHT
);
493 stockSubMenu
->Append(wxID_LAST
);
494 stockSubMenu
->Append(wxID_NETWORK
);
495 stockSubMenu
->Append(wxID_NEW
);
496 stockSubMenu
->Append(wxID_NO
);
497 stockSubMenu
->Append(wxID_OK
);
498 stockSubMenu
->Append(wxID_OPEN
);
499 stockSubMenu
->Append(wxID_PASTE
);
500 stockSubMenu
->Append(wxID_PREFERENCES
);
501 stockSubMenu
->Append(wxID_PREVIEW
);
502 stockSubMenu
->Append(wxID_PRINT
);
503 stockSubMenu
->Append(wxID_PROPERTIES
);
504 stockSubMenu
->Append(wxID_REDO
);
505 stockSubMenu
->Append(wxID_REFRESH
);
506 stockSubMenu
->Append(wxID_REMOVE
);
507 stockSubMenu
->Append(wxID_REPLACE
);
508 stockSubMenu
->Append(wxID_REVERT_TO_SAVED
);
509 stockSubMenu
->Append(wxID_SAVE
);
510 stockSubMenu
->Append(wxID_SAVEAS
);
511 stockSubMenu
->Append(wxID_SELECT_COLOR
);
512 stockSubMenu
->Append(wxID_SELECT_FONT
);
513 stockSubMenu
->Append(wxID_SORT_ASCENDING
);
514 stockSubMenu
->Append(wxID_SORT_DESCENDING
);
515 stockSubMenu
->Append(wxID_SPELL_CHECK
);
516 stockSubMenu
->Append(wxID_STOP
);
517 stockSubMenu
->Append(wxID_STRIKETHROUGH
);
518 stockSubMenu
->Append(wxID_TOP
);
519 stockSubMenu
->Append(wxID_UNDELETE
);
520 stockSubMenu
->Append(wxID_UNDERLINE
);
521 stockSubMenu
->Append(wxID_UNDO
);
522 stockSubMenu
->Append(wxID_UNINDENT
);
523 stockSubMenu
->Append(wxID_UP
);
524 stockSubMenu
->Append(wxID_YES
);
525 stockSubMenu
->Append(wxID_ZOOM_100
);
526 stockSubMenu
->Append(wxID_ZOOM_FIT
);
527 stockSubMenu
->Append(wxID_ZOOM_IN
);
528 stockSubMenu
->Append(wxID_ZOOM_OUT
);
529 fileMenu
->AppendSubMenu(stockSubMenu
, wxT("&Standard items demo"));
532 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
533 wxT("Clear &log\tCtrl-L"));
534 item
->SetBitmap(copy_xpm
);
535 fileMenu
->Append(item
);
536 fileMenu
->AppendSeparator();
537 #endif // USE_LOG_WINDOW
539 fileMenu
->Append(Menu_File_ShowDialog
, wxT("Show &Dialog\tCtrl-D"),
540 wxT("Show a dialog"));
541 fileMenu
->AppendSeparator();
543 fileMenu
->Append(Menu_File_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit menu sample"));
545 wxMenu
*menubarMenu
= new wxMenu
;
546 menubarMenu
->Append(Menu_MenuBar_Append
, wxT("&Append menu\tCtrl-A"),
547 wxT("Append a menu to the menubar"));
548 menubarMenu
->Append(Menu_MenuBar_Insert
, wxT("&Insert menu\tCtrl-I"),
549 wxT("Insert a menu into the menubar"));
550 menubarMenu
->Append(Menu_MenuBar_Delete
, wxT("&Delete menu\tCtrl-D"),
551 wxT("Delete the last menu from the menubar"));
552 menubarMenu
->Append(Menu_MenuBar_Toggle
, wxT("&Toggle menu\tCtrl-T"),
553 wxT("Toggle the first menu in the menubar"), true);
554 menubarMenu
->AppendSeparator();
555 menubarMenu
->Append(Menu_MenuBar_Enable
, wxT("&Enable menu\tCtrl-E"),
556 wxT("Enable or disable the last menu"), true);
557 menubarMenu
->AppendSeparator();
558 menubarMenu
->Append(Menu_MenuBar_GetLabel
, wxT("&Get menu label\tCtrl-G"),
559 wxT("Get the label of the last menu"));
561 menubarMenu
->Append(Menu_MenuBar_SetLabel
, wxT("&Set menu label\tCtrl-S"),
562 wxT("Change the label of the last menu"));
563 menubarMenu
->AppendSeparator();
564 menubarMenu
->Append(Menu_MenuBar_FindMenu
, wxT("&Find menu from label\tCtrl-F"),
565 wxT("Find a menu by searching for its label"));
568 wxMenu
* subMenu
= new wxMenu
;
569 subMenu
->Append(Menu_SubMenu_Normal
, wxT("&Normal submenu item"), wxT("Disabled submenu item"));
570 subMenu
->AppendCheckItem(Menu_SubMenu_Check
, wxT("&Check submenu item"), wxT("Check submenu item"));
571 subMenu
->AppendRadioItem(Menu_SubMenu_Radio1
, wxT("Radio item &1"), wxT("Radio item"));
572 subMenu
->AppendRadioItem(Menu_SubMenu_Radio2
, wxT("Radio item &2"), wxT("Radio item"));
573 subMenu
->AppendRadioItem(Menu_SubMenu_Radio3
, wxT("Radio item &3"), wxT("Radio item"));
575 menubarMenu
->Append(Menu_SubMenu
, wxT("Submenu"), subMenu
);
577 wxMenu
*menuMenu
= new wxMenu
;
578 menuMenu
->Append(Menu_Menu_Append
, wxT("&Append menu item\tAlt-A"),
579 wxT("Append a menu item to the 'Test' menu"));
580 menuMenu
->Append(Menu_Menu_AppendSub
, wxT("&Append sub menu\tAlt-S"),
581 wxT("Append a sub menu to the 'Test' menu"));
582 menuMenu
->Append(Menu_Menu_Insert
, wxT("&Insert menu item\tAlt-I"),
583 wxT("Insert a menu item in head of the 'Test' menu"));
584 menuMenu
->Append(Menu_Menu_Delete
, wxT("&Delete menu item\tAlt-D"),
585 wxT("Delete the last menu item from the 'Test' menu"));
586 menuMenu
->Append(Menu_Menu_DeleteSub
, wxT("Delete last &submenu\tAlt-K"),
587 wxT("Delete the last submenu from the 'Test' menu"));
588 menuMenu
->AppendSeparator();
589 menuMenu
->Append(Menu_Menu_Enable
, wxT("&Enable menu item\tAlt-E"),
590 wxT("Enable or disable the last menu item"), true);
591 menuMenu
->Append(Menu_Menu_Check
, wxT("&Check menu item\tAlt-C"),
592 wxT("Check or uncheck the last menu item"), true);
593 menuMenu
->AppendSeparator();
594 menuMenu
->Append(Menu_Menu_GetInfo
, wxT("Get menu item in&fo\tAlt-F"),
595 wxT("Show the state of the last menu item"));
597 menuMenu
->Append(Menu_Menu_SetLabel
, wxT("Set menu item label\tAlt-L"),
598 wxT("Set the label of a menu item"));
601 menuMenu
->AppendSeparator();
602 menuMenu
->Append(Menu_Menu_FindItem
, wxT("Find menu item from label"),
603 wxT("Find a menu item by searching for its label"));
606 wxMenu
*testMenu
= new wxMenu
;
607 testMenu
->Append(Menu_Test_Normal
, wxT("&Normal item"));
608 testMenu
->AppendSeparator();
609 testMenu
->AppendCheckItem(Menu_Test_Check
, wxT("&Check item"));
610 testMenu
->AppendSeparator();
611 testMenu
->AppendRadioItem(Menu_Test_Radio1
, wxT("Radio item &1"));
612 testMenu
->AppendRadioItem(Menu_Test_Radio2
, wxT("Radio item &2"));
613 testMenu
->AppendRadioItem(Menu_Test_Radio3
, wxT("Radio item &3"));
615 wxMenu
*helpMenu
= new wxMenu
;
616 helpMenu
->Append(Menu_Help_About
, wxT("&About\tF1"), wxT("About menu sample"));
618 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
620 menuBar
->Append(fileMenu
, wxT("&File"));
621 menuBar
->Append(menubarMenu
, wxT("Menu&bar"));
622 menuBar
->Append(menuMenu
, wxT("&Menu"));
623 menuBar
->Append(testMenu
, wxT("&Test"));
624 menuBar
->Append(helpMenu
, wxT("&Help"));
626 // these items should be initially checked
627 menuBar
->Check(Menu_MenuBar_Toggle
, true);
628 menuBar
->Check(Menu_MenuBar_Enable
, true);
629 menuBar
->Check(Menu_Menu_Enable
, true);
630 menuBar
->Check(Menu_Menu_Check
, false);
632 // associate the menu bar with the frame
635 // intercept all menu events and log them in this custom event handler
636 PushEventHandler(new MyEvtHandler(this));
639 // create the log text window
640 m_textctrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
641 wxDefaultPosition
, wxDefaultSize
,
643 m_textctrl
->SetEditable(false);
645 wxLog::DisableTimestamp();
646 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
648 wxLogMessage(wxT("Brief explanations: the commands in the \"Menu\" menu ")
649 wxT("append/insert/delete items to/from the \"Test\" menu.\n")
650 wxT("The commands in the \"Menubar\" menu work with the ")
651 wxT("menubar itself.\n\n")
652 wxT("Right click the band below to test popup menus.\n"));
663 // delete the event handler installed in ctor
664 PopEventHandler(true);
667 // restore old logger
668 delete wxLog::SetActiveTarget(m_logOld
);
672 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
674 wxMenu
*menu
= new wxMenu
;
675 menu
->Append(Menu_Dummy_First
, wxT("&First item\tCtrl-F1"));
676 menu
->AppendSeparator();
677 menu
->AppendCheckItem(Menu_Dummy_Second
, wxT("&Second item\tCtrl-F2"));
681 title
->Printf(wxT("Dummy menu &%u"), (unsigned)++m_countDummy
);
687 wxMenuItem
*MyFrame::GetLastMenuItem() const
689 wxMenuBar
*menubar
= GetMenuBar();
690 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
691 wxCHECK_MSG( menu
, NULL
, wxT("no 'Test' menu?") );
693 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
696 wxLogWarning(wxT("No last item in the 'Test' menu!"));
702 return node
->GetData();
706 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
708 int id
= event
.GetId();
710 wxString msg
= wxString::Format(wxT("Menu command %d"), id
);
712 // catch all checkable menubar items and also the check item from the popup
714 wxMenuItem
*item
= GetMenuBar()->FindItem(id
);
715 if ( (item
&& item
->IsCheckable()) || id
== Menu_Popup_ToBeChecked
)
717 msg
+= wxString::Format(wxT(" (the item is currently %schecked)"),
718 event
.IsChecked() ? wxT("") : wxT("not "));
724 // ----------------------------------------------------------------------------
726 // ----------------------------------------------------------------------------
728 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
735 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
740 void MyFrame::OnClearLogUpdateUI(wxUpdateUIEvent
& event
)
742 // if we only enable this item when the log window is empty, we never see
743 // it in the disable state as a message is logged whenever the menu is
744 // opened, so we disable it if there is not "much" text in the window
745 event
.Enable( m_textctrl
->GetNumberOfLines() > 5 );
748 #endif // USE_LOG_WINDOW
750 void MyFrame::OnShowDialog(wxCommandEvent
& WXUNUSED(event
))
756 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
758 (void)wxMessageBox(wxT("wxWidgets menu sample\n(c) 1999-2001 Vadim Zeitlin"),
759 wxT("About wxWidgets menu sample"),
760 wxOK
| wxICON_INFORMATION
);
763 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
765 wxMenuBar
*mbar
= GetMenuBar();
767 size_t count
= mbar
->GetMenuCount();
770 // don't let delete the first 4 menus
771 wxLogError(wxT("Can't delete any more menus"));
775 delete mbar
->Remove(count
- 1);
779 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
782 wxMenu
*menu
= CreateDummyMenu(&title
);
783 // Insert before the 'Help' menu
784 // Otherwise repeated Deletes will remove the 'Test' menu
785 GetMenuBar()->Insert(4, menu
, title
);
788 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
791 wxMenu
*menu
= CreateDummyMenu(&title
);
792 GetMenuBar()->Append(menu
, title
);
795 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
797 wxMenuBar
*mbar
= GetMenuBar();
801 m_menu
= mbar
->Remove(0);
806 mbar
->Insert(0, m_menu
, wxT("&File"));
811 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
813 wxMenuBar
*mbar
= GetMenuBar();
814 size_t count
= mbar
->GetMenuCount();
816 mbar
->EnableTop(count
- 1, event
.IsChecked());
819 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
821 wxMenuBar
*mbar
= GetMenuBar();
822 size_t count
= mbar
->GetMenuCount();
824 wxCHECK_RET( count
, wxT("no last menu?") );
826 wxLogMessage(wxT("The label of the last menu item is '%s'"),
827 mbar
->GetMenuLabel(count
- 1).c_str());
831 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
833 wxMenuBar
*mbar
= GetMenuBar();
834 size_t count
= mbar
->GetMenuCount();
836 wxCHECK_RET( count
, wxT("no last menu?") );
838 wxString label
= wxGetTextFromUser
840 wxT("Enter new label: "),
841 wxT("Change last menu text"),
842 mbar
->GetMenuLabel(count
- 1),
846 if ( !label
.empty() )
848 mbar
->SetMenuLabel(count
- 1, label
);
852 void MyFrame::OnFindMenu(wxCommandEvent
& WXUNUSED(event
))
854 wxMenuBar
*mbar
= GetMenuBar();
855 size_t count
= mbar
->GetMenuCount();
857 wxCHECK_RET( count
, wxT("no last menu?") );
859 wxString label
= wxGetTextFromUser
861 wxT("Enter label to search for: "),
867 if ( !label
.empty() )
869 int index
= mbar
->FindMenu(label
);
871 if (index
== wxNOT_FOUND
)
873 wxLogWarning(wxT("No menu with label '%s'"), label
.c_str());
877 wxLogMessage(wxT("Menu %d has label '%s'"), index
, label
.c_str());
883 void MyFrame::OnDummy(wxCommandEvent
& event
)
885 wxLogMessage(wxT("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
888 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
890 wxMenuBar
*menubar
= GetMenuBar();
891 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
892 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
894 menu
->AppendSeparator();
895 menu
->Append(Menu_Dummy_Third
, wxT("&Third dummy item\tCtrl-F3"),
896 wxT("Checkable item"), true);
899 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
901 wxMenuBar
*menubar
= GetMenuBar();
902 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
903 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
905 menu
->Append(Menu_Dummy_Last
, wxT("&Dummy sub menu"),
906 CreateDummyMenu(NULL
), wxT("Dummy sub menu help"));
909 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
911 wxMenuBar
*menubar
= GetMenuBar();
912 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
913 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
915 size_t count
= menu
->GetMenuItemCount();
918 wxLogWarning(wxT("No items to delete!"));
922 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
926 void MyFrame::OnDeleteSubMenu(wxCommandEvent
& WXUNUSED(event
))
928 wxMenuBar
*menubar
= GetMenuBar();
929 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
930 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
932 for ( int n
= menu
->GetMenuItemCount() - 1; n
>=0 ; --n
)
934 wxMenuItem
* item
= menu
->FindItemByPosition(n
);
935 if (item
->IsSubMenu())
942 wxLogWarning(wxT("No submenu to delete!"));
945 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
947 wxMenuBar
*menubar
= GetMenuBar();
948 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
949 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
951 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
952 wxT("Fourth dummy item\tCtrl-F4")));
953 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
));
956 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
958 wxMenuItem
*item
= GetLastMenuItem();
962 item
->Enable(!item
->IsEnabled());
966 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
968 wxMenuItem
*item
= GetLastMenuItem();
970 if (item
&& item
->IsCheckable())
976 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
980 wxMenuItem
*item
= GetLastMenuItem();
982 event
.Enable(item
&& item
->IsCheckable());
985 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
987 wxMenuItem
*item
= GetLastMenuItem();
991 wxString label
= item
->GetItemLabel();
992 wxLogMessage(wxT("The label of the last menu item is '%s'"),
998 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
1000 wxMenuItem
*item
= GetLastMenuItem();
1004 wxString label
= wxGetTextFromUser
1006 wxT("Enter new label: "),
1007 wxT("Change last menu item text"),
1008 item
->GetItemLabel(),
1011 label
.Replace( wxT("\\t"), wxT("\t") );
1013 if ( !label
.empty() )
1015 item
->SetItemLabel(label
);
1021 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
1023 wxMenuItem
*item
= GetLastMenuItem();
1028 msg
<< wxT("The item is ") << (item
->IsEnabled() ? wxT("enabled")
1032 if ( item
->IsCheckable() )
1034 msg
<< wxT("It is checkable and ") << (item
->IsChecked() ? wxT("") : wxT("un"))
1035 << wxT("checked\n");
1039 wxAcceleratorEntry
*accel
= item
->GetAccel();
1042 msg
<< wxT("Its accelerator is ");
1044 int flags
= accel
->GetFlags();
1045 if ( flags
& wxACCEL_ALT
)
1047 if ( flags
& wxACCEL_CTRL
)
1048 msg
<< wxT("Ctrl-");
1049 if ( flags
& wxACCEL_SHIFT
)
1050 msg
<< wxT("Shift-");
1052 int code
= accel
->GetKeyCode();
1067 msg
<< wxT('F') << code
- WXK_F1
+ 1;
1070 // if there are any other keys wxGetAccelFromString() may return,
1071 // we should process them here
1074 if ( wxIsalnum(code
) )
1076 msg
<< (wxChar
)code
;
1081 wxFAIL_MSG( wxT("unknown keyboard accel") );
1088 msg
<< wxT("It doesn't have an accelerator");
1090 #endif // wxUSE_ACCEL
1097 void MyFrame::OnFindMenuItem(wxCommandEvent
& WXUNUSED(event
))
1099 wxMenuBar
*mbar
= GetMenuBar();
1100 size_t count
= mbar
->GetMenuCount();
1102 wxCHECK_RET( count
, wxT("no last menu?") );
1104 wxString label
= wxGetTextFromUser
1106 wxT("Enter label to search for: "),
1107 wxT("Find menu item"),
1112 if ( !label
.empty() )
1115 int index
= wxNOT_FOUND
;
1117 for (menuindex
= 0; (menuindex
< count
) && (index
== wxNOT_FOUND
); ++menuindex
)
1119 index
= mbar
->FindMenuItem(mbar
->GetMenu(menuindex
)->GetTitle(), label
);
1121 if (index
== wxNOT_FOUND
)
1123 wxLogWarning(wxT("No menu item with label '%s'"), label
.c_str());
1127 wxLogMessage(wxT("Menu item %d in menu %lu has label '%s'"),
1128 index
, (unsigned long)menuindex
, label
.c_str());
1134 void MyFrame::ShowContextMenu(const wxPoint
& pos
)
1138 if ( wxGetKeyState(WXK_SHIFT
) )
1140 // when Shift is pressed, demonstrate the use of a simple function
1141 // returning the id of the item selected in the popup menu
1142 menu
.SetTitle("Choose one of:");
1143 static const char *choices
[] = { "Apple", "Banana", "Cherry" };
1144 for ( size_t n
= 0; n
< WXSIZEOF(choices
); n
++ )
1145 menu
.Append(Menu_PopupChoice
+ n
, choices
[n
]);
1147 const int rc
= GetPopupMenuSelectionFromUser(menu
, pos
);
1148 if ( rc
== wxID_NONE
)
1150 wxLogMessage("No selection");
1154 wxLogMessage("You have selected \"%s\"",
1155 choices
[rc
- Menu_PopupChoice
]);
1158 else // normal case, shift not pressed
1160 menu
.Append(Menu_Help_About
, wxT("&About"));
1161 menu
.Append(Menu_Popup_Submenu
, wxT("&Submenu"), CreateDummyMenu(NULL
));
1162 menu
.Append(Menu_Popup_ToBeDeleted
, wxT("To be &deleted"));
1163 menu
.AppendCheckItem(Menu_Popup_ToBeChecked
, wxT("To be &checked"));
1164 menu
.Append(Menu_Popup_ToBeGreyed
, wxT("To be &greyed"),
1165 wxT("This menu item should be initially greyed out"));
1166 menu
.AppendSeparator();
1167 menu
.Append(Menu_File_Quit
, wxT("E&xit"));
1169 menu
.Delete(Menu_Popup_ToBeDeleted
);
1170 menu
.Check(Menu_Popup_ToBeChecked
, true);
1171 menu
.Enable(Menu_Popup_ToBeGreyed
, false);
1173 PopupMenu(&menu
, pos
);
1175 // test for destroying items in popup menus
1176 #if 0 // doesn't work in wxGTK!
1177 menu
.Destroy(Menu_Popup_Submenu
);
1179 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
1184 void MyFrame::OnTestNormal(wxCommandEvent
& WXUNUSED(event
))
1186 wxLogMessage(wxT("Normal item selected"));
1189 void MyFrame::OnTestCheck(wxCommandEvent
& event
)
1191 wxLogMessage(wxT("Check item %schecked"),
1192 event
.IsChecked() ? wxT("") : wxT("un"));
1195 void MyFrame::OnTestRadio(wxCommandEvent
& event
)
1197 wxLogMessage(wxT("Radio item %d selected"),
1198 event
.GetId() - Menu_Test_Radio1
+ 1);
1202 void MyFrame::LogMenuOpenCloseOrHighlight(const wxMenuEvent
& event
, const wxChar
*what
)
1206 << ( event
.IsPopup() ? wxT("popup ") : wxT("") )
1207 << wxT("menu has been ")
1210 if ( event
.GetEventType() == wxEVT_MENU_HIGHLIGHT
)
1212 msg
<< wxT(" (id=") << event
.GetId() << wxT(")");
1217 wxLogStatus(this, msg
.c_str());
1221 void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
)
1223 event
.Enable(false);
1226 void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
)
1231 void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
)
1233 int which
= (event
.GetId() - Menu_SubMenu_Radio1
+ 1);
1240 #if USE_CONTEXT_MENU
1241 void MyFrame::OnContextMenu(wxContextMenuEvent
& event
)
1243 wxPoint point
= event
.GetPosition();
1245 if (point
.x
== -1 && point
.y
== -1) {
1246 wxSize size
= GetSize();
1247 point
.x
= size
.x
/ 2;
1248 point
.y
= size
.y
/ 2;
1250 point
= ScreenToClient(point
);
1252 ShowContextMenu(point
);
1256 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
1262 // leave a band below for popup menu testing
1263 wxSize size
= GetClientSize();
1264 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
1267 // this is really ugly but we have to do it as we can't just call
1268 // event.Skip() because wxFrameBase would make the text control fill the
1269 // entire frame then
1270 #ifdef __WXUNIVERSAL__
1272 #endif // __WXUNIVERSAL__
1275 // ----------------------------------------------------------------------------
1277 // ----------------------------------------------------------------------------
1279 MyDialog::MyDialog(wxWindow
* parent
)
1280 : wxDialog(parent
, wxID_ANY
, "Test Dialog")
1283 // create the log text window
1284 m_textctrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
1285 wxDefaultPosition
, wxDefaultSize
,
1287 m_textctrl
->SetEditable(false);
1289 m_textctrl
->AppendText(wxT("Dialogs do not have menus, but popup menus should function the same\n\n")
1290 wxT("Right click this text ctrl to test popup menus.\n"));
1293 EnableContextMenu();
1298 void MyDialog::LogMenuOpenCloseOrHighlight(const wxMenuEvent
& event
, const wxChar
*what
)
1302 << ( event
.IsPopup() ? wxT("popup ") : wxT("") )
1303 << wxT("menu has been ")
1305 if ( event
.GetEventType() == wxEVT_MENU_HIGHLIGHT
)
1307 msg
<< wxT(" (id=") << event
.GetId() << wxT(")");
1311 m_textctrl
->AppendText(msg
);
1313 #endif // USE_LOG_WINDOW
1314 #if USE_CONTEXT_MENU
1315 void MyDialog::OnContextMenu(wxContextMenuEvent
& event
)
1317 wxPoint point
= event
.GetPosition();
1319 if (point
.x
== -1 && point
.y
== -1) {
1320 wxSize size
= GetSize();
1321 point
.x
= size
.x
/ 2;
1322 point
.y
= size
.y
/ 2;
1324 point
= ScreenToClient(point
);
1326 ShowContextMenu(point
);
1330 void MyDialog::ShowContextMenu(const wxPoint
& pos
)
1334 menu
.Append(Menu_Help_About
, wxT("&About"));
1335 menu
.Append(Menu_Popup_ToBeDeleted
, wxT("To be &deleted"));
1336 menu
.AppendCheckItem(Menu_Popup_ToBeChecked
, wxT("To be &checked"));
1337 menu
.Append(Menu_Popup_ToBeGreyed
, wxT("To be &greyed"),
1338 wxT("This menu item should be initially greyed out"));
1339 menu
.AppendSeparator();
1340 menu
.Append(Menu_File_Quit
, wxT("E&xit"));
1342 menu
.Delete(Menu_Popup_ToBeDeleted
);
1343 menu
.Check(Menu_Popup_ToBeChecked
, true);
1344 menu
.Enable(Menu_Popup_ToBeGreyed
, false);
1346 PopupMenu(&menu
, pos
);