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__) || defined(__WXMGL__)
45 #define USE_CONTEXT_MENU 0
47 #define USE_CONTEXT_MENU 1
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
55 #define USE_LOG_WINDOW 0
58 #if wxUSE_OWNER_DRAWN || defined(__WXGTK__)
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // Define a new application
67 class MyApp
: public wxApp
74 class MyFrame
: public wxFrame
81 void LogMenuEvent(const wxCommandEvent
& event
);
84 void OnQuit(wxCommandEvent
& event
);
86 void OnClearLog(wxCommandEvent
& event
);
87 void OnClearLogUpdateUI(wxUpdateUIEvent
& event
);
88 #endif // USE_LOG_WINDOW
90 void OnAbout(wxCommandEvent
& event
);
92 void OnDummy(wxCommandEvent
& event
);
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
);
102 void OnSetLabelMenuItem(wxCommandEvent
& event
);
104 void OnGetMenuItemInfo(wxCommandEvent
& event
);
106 void OnFindMenuItem(wxCommandEvent
& event
);
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
);
117 void OnFindMenu(wxCommandEvent
& event
);
120 void OnTestNormal(wxCommandEvent
& event
);
121 void OnTestCheck(wxCommandEvent
& event
);
122 void OnTestRadio(wxCommandEvent
& event
);
124 void OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
);
125 void OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
);
126 void OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
);
129 void OnContextMenu(wxContextMenuEvent
& event
);
131 void OnRightUp(wxMouseEvent
& event
)
132 { ShowContextMenu(event
.GetPosition()); }
135 void OnMenuOpen(wxMenuEvent
& event
)
138 LogMenuOpenOrClose(event
, _T("opened")); event
.Skip();
141 void OnMenuClose(wxMenuEvent
& event
)
144 LogMenuOpenOrClose(event
, _T("closed")); event
.Skip();
148 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
150 void OnSize(wxSizeEvent
& event
);
153 void LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
);
154 void ShowContextMenu(const wxPoint
& pos
);
156 wxMenu
*CreateDummyMenu(wxString
*title
);
158 wxMenuItem
*GetLastMenuItem() const;
160 // the menu previously detached from the menubar (may be NULL)
163 // the count of dummy menus already created
167 // the control used for logging
168 wxTextCtrl
*m_textctrl
;
171 // the previous log target
174 DECLARE_EVENT_TABLE()
177 // A small helper class which intercepts all menu events and logs them
178 class MyEvtHandler
: public wxEvtHandler
181 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
183 void OnMenuEvent(wxCommandEvent
& event
)
185 m_frame
->LogMenuEvent(event
);
193 DECLARE_EVENT_TABLE()
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
202 Menu_File_Quit
= wxID_EXIT
,
204 Menu_File_ClearLog
= 100,
207 Menu_MenuBar_Toggle
= 200,
212 Menu_MenuBar_GetLabel
,
214 Menu_MenuBar_SetLabel
,
215 Menu_MenuBar_FindMenu
,
218 Menu_Menu_Append
= 300,
233 Menu_Test_Normal
= 400,
246 Menu_Dummy_First
= 500,
252 Menu_Help_About
= wxID_ABOUT
,
254 Menu_Popup_ToBeDeleted
= 2000,
255 Menu_Popup_ToBeGreyed
,
256 Menu_Popup_ToBeChecked
,
264 // ----------------------------------------------------------------------------
266 // ----------------------------------------------------------------------------
268 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
269 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
271 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
272 EVT_UPDATE_UI(Menu_File_ClearLog
, MyFrame::OnClearLogUpdateUI
)
275 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
277 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
278 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
279 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
280 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
281 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
282 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
284 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
285 EVT_MENU(Menu_MenuBar_FindMenu
, MyFrame::OnFindMenu
)
288 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
289 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
290 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
291 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
292 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
293 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
294 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
296 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
298 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
300 EVT_MENU(Menu_Menu_FindItem
, MyFrame::OnFindMenuItem
)
303 EVT_MENU(Menu_Test_Normal
, MyFrame::OnTestNormal
)
304 EVT_MENU(Menu_Test_Check
, MyFrame::OnTestCheck
)
305 EVT_MENU(Menu_Test_Radio1
, MyFrame::OnTestRadio
)
306 EVT_MENU(Menu_Test_Radio2
, MyFrame::OnTestRadio
)
307 EVT_MENU(Menu_Test_Radio3
, MyFrame::OnTestRadio
)
309 EVT_UPDATE_UI(Menu_SubMenu_Normal
, MyFrame::OnUpdateSubMenuNormal
)
310 EVT_UPDATE_UI(Menu_SubMenu_Check
, MyFrame::OnUpdateSubMenuCheck
)
311 EVT_UPDATE_UI(Menu_SubMenu_Radio1
, MyFrame::OnUpdateSubMenuRadio
)
312 EVT_UPDATE_UI(Menu_SubMenu_Radio2
, MyFrame::OnUpdateSubMenuRadio
)
313 EVT_UPDATE_UI(Menu_SubMenu_Radio3
, MyFrame::OnUpdateSubMenuRadio
)
315 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
317 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
320 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
322 EVT_RIGHT_UP(MyFrame::OnRightUp
)
325 EVT_MENU_OPEN(MyFrame::OnMenuOpen
)
326 EVT_MENU_CLOSE(MyFrame::OnMenuClose
)
328 EVT_SIZE(MyFrame::OnSize
)
331 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
332 EVT_MENU(wxID_ANY
, MyEvtHandler::OnMenuEvent
)
335 // ============================================================================
337 // ============================================================================
339 // ----------------------------------------------------------------------------
341 // ----------------------------------------------------------------------------
345 // The `main program' equivalent, creating the windows and returning the
349 if ( !wxApp::OnInit() )
352 // Create the main frame window
353 MyFrame
* frame
= new MyFrame
;
358 frame
->SetStatusText(_T("Welcome to wxWidgets menu sample"));
359 #endif // wxUSE_STATUSBAR
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 // Define my frame constructor
372 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, _T("wxWidgets menu sample"))
383 #endif // wxUSE_STATUSBAR
385 // create the menubar
386 wxMenu
*fileMenu
= new wxMenu
;
388 wxMenu
*stockSubMenu
= new wxMenu
;
389 stockSubMenu
->Append(wxID_ADD
);
390 stockSubMenu
->Append(wxID_APPLY
);
391 stockSubMenu
->Append(wxID_BACKWARD
);
392 stockSubMenu
->Append(wxID_BOLD
);
393 stockSubMenu
->Append(wxID_BOTTOM
);
394 stockSubMenu
->Append(wxID_CANCEL
);
395 stockSubMenu
->Append(wxID_CDROM
);
396 stockSubMenu
->Append(wxID_CLEAR
);
397 stockSubMenu
->Append(wxID_CLOSE
);
398 stockSubMenu
->Append(wxID_CONVERT
);
399 stockSubMenu
->Append(wxID_COPY
);
400 stockSubMenu
->Append(wxID_CUT
);
401 stockSubMenu
->Append(wxID_DELETE
);
402 stockSubMenu
->Append(wxID_DOWN
);
403 stockSubMenu
->Append(wxID_EXECUTE
);
404 stockSubMenu
->Append(wxID_EXIT
);
405 stockSubMenu
->Append(wxID_FIND
);
406 stockSubMenu
->Append(wxID_FIRST
);
407 stockSubMenu
->Append(wxID_FLOPPY
);
408 stockSubMenu
->Append(wxID_FORWARD
);
409 stockSubMenu
->Append(wxID_HARDDISK
);
410 stockSubMenu
->Append(wxID_HELP
);
411 stockSubMenu
->Append(wxID_HOME
);
412 stockSubMenu
->Append(wxID_INDENT
);
413 stockSubMenu
->Append(wxID_INDEX
);
414 stockSubMenu
->Append(wxID_INFO
);
415 stockSubMenu
->Append(wxID_ITALIC
);
416 stockSubMenu
->Append(wxID_JUMP_TO
);
417 stockSubMenu
->Append(wxID_JUSTIFY_CENTER
);
418 stockSubMenu
->Append(wxID_JUSTIFY_FILL
);
419 stockSubMenu
->Append(wxID_JUSTIFY_LEFT
);
420 stockSubMenu
->Append(wxID_JUSTIFY_RIGHT
);
421 stockSubMenu
->Append(wxID_LAST
);
422 stockSubMenu
->Append(wxID_NETWORK
);
423 stockSubMenu
->Append(wxID_NEW
);
424 stockSubMenu
->Append(wxID_NO
);
425 stockSubMenu
->Append(wxID_OK
);
426 stockSubMenu
->Append(wxID_OPEN
);
427 stockSubMenu
->Append(wxID_PASTE
);
428 stockSubMenu
->Append(wxID_PREFERENCES
);
429 stockSubMenu
->Append(wxID_PREVIEW
);
430 stockSubMenu
->Append(wxID_PRINT
);
431 stockSubMenu
->Append(wxID_PROPERTIES
);
432 stockSubMenu
->Append(wxID_REDO
);
433 stockSubMenu
->Append(wxID_REFRESH
);
434 stockSubMenu
->Append(wxID_REMOVE
);
435 stockSubMenu
->Append(wxID_REPLACE
);
436 stockSubMenu
->Append(wxID_REVERT_TO_SAVED
);
437 stockSubMenu
->Append(wxID_SAVE
);
438 stockSubMenu
->Append(wxID_SAVEAS
);
439 stockSubMenu
->Append(wxID_SELECT_COLOR
);
440 stockSubMenu
->Append(wxID_SELECT_FONT
);
441 stockSubMenu
->Append(wxID_SORT_ASCENDING
);
442 stockSubMenu
->Append(wxID_SORT_DESCENDING
);
443 stockSubMenu
->Append(wxID_SPELL_CHECK
);
444 stockSubMenu
->Append(wxID_STOP
);
445 stockSubMenu
->Append(wxID_STRIKETHROUGH
);
446 stockSubMenu
->Append(wxID_TOP
);
447 stockSubMenu
->Append(wxID_UNDELETE
);
448 stockSubMenu
->Append(wxID_UNDERLINE
);
449 stockSubMenu
->Append(wxID_UNDO
);
450 stockSubMenu
->Append(wxID_UNINDENT
);
451 stockSubMenu
->Append(wxID_UP
);
452 stockSubMenu
->Append(wxID_YES
);
453 stockSubMenu
->Append(wxID_ZOOM_100
);
454 stockSubMenu
->Append(wxID_ZOOM_FIT
);
455 stockSubMenu
->Append(wxID_ZOOM_IN
);
456 stockSubMenu
->Append(wxID_ZOOM_OUT
);
457 fileMenu
->AppendSubMenu(stockSubMenu
, _T("&Standard items demo"));
460 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
461 _T("Clear &log\tCtrl-L"));
462 #if wxUSE_OWNER_DRAWN || defined(__WXGTK__)
463 item
->SetBitmap(copy_xpm
);
465 fileMenu
->Append(item
);
466 fileMenu
->AppendSeparator();
467 #endif // USE_LOG_WINDOW
469 fileMenu
->Append(Menu_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit menu sample"));
471 wxMenu
*menubarMenu
= new wxMenu
;
472 menubarMenu
->Append(Menu_MenuBar_Append
, _T("&Append menu\tCtrl-A"),
473 _T("Append a menu to the menubar"));
474 menubarMenu
->Append(Menu_MenuBar_Insert
, _T("&Insert menu\tCtrl-I"),
475 _T("Insert a menu into the menubar"));
476 menubarMenu
->Append(Menu_MenuBar_Delete
, _T("&Delete menu\tCtrl-D"),
477 _T("Delete the last menu from the menubar"));
478 menubarMenu
->Append(Menu_MenuBar_Toggle
, _T("&Toggle menu\tCtrl-T"),
479 _T("Toggle the first menu in the menubar"), true);
480 menubarMenu
->AppendSeparator();
481 menubarMenu
->Append(Menu_MenuBar_Enable
, _T("&Enable menu\tCtrl-E"),
482 _T("Enable or disable the last menu"), true);
483 menubarMenu
->AppendSeparator();
484 menubarMenu
->Append(Menu_MenuBar_GetLabel
, _T("&Get menu label\tCtrl-G"),
485 _T("Get the label of the last menu"));
487 menubarMenu
->Append(Menu_MenuBar_SetLabel
, _T("&Set menu label\tCtrl-S"),
488 _T("Change the label of the last menu"));
489 menubarMenu
->AppendSeparator();
490 menubarMenu
->Append(Menu_MenuBar_FindMenu
, _T("&Find menu from label\tCtrl-F"),
491 _T("Find a menu by searching for its label"));
494 wxMenu
* subMenu
= new wxMenu
;
495 subMenu
->Append(Menu_SubMenu_Normal
, _T("&Normal submenu item"), _T("Disabled submenu item"));
496 subMenu
->AppendCheckItem(Menu_SubMenu_Check
, _T("&Check submenu item"), _T("Check submenu item"));
497 subMenu
->AppendRadioItem(Menu_SubMenu_Radio1
, _T("Radio item &1"), _T("Radio item"));
498 subMenu
->AppendRadioItem(Menu_SubMenu_Radio2
, _T("Radio item &2"), _T("Radio item"));
499 subMenu
->AppendRadioItem(Menu_SubMenu_Radio3
, _T("Radio item &3"), _T("Radio item"));
501 menubarMenu
->Append(Menu_SubMenu
, _T("Submenu"), subMenu
);
503 wxMenu
*menuMenu
= new wxMenu
;
504 menuMenu
->Append(Menu_Menu_Append
, _T("&Append menu item\tAlt-A"),
505 _T("Append a menu item to the last menu"));
506 menuMenu
->Append(Menu_Menu_AppendSub
, _T("&Append sub menu\tAlt-S"),
507 _T("Append a sub menu to the last menu"));
508 menuMenu
->Append(Menu_Menu_Insert
, _T("&Insert menu item\tAlt-I"),
509 _T("Insert a menu item in head of the last menu"));
510 menuMenu
->Append(Menu_Menu_Delete
, _T("&Delete menu item\tAlt-D"),
511 _T("Delete the last menu item from the last menu"));
512 menuMenu
->AppendSeparator();
513 menuMenu
->Append(Menu_Menu_Enable
, _T("&Enable menu item\tAlt-E"),
514 _T("Enable or disable the last menu item"), true);
515 menuMenu
->Append(Menu_Menu_Check
, _T("&Check menu item\tAlt-C"),
516 _T("Check or uncheck the last menu item"), true);
517 menuMenu
->AppendSeparator();
518 menuMenu
->Append(Menu_Menu_GetInfo
, _T("Get menu item in&fo\tAlt-F"),
519 _T("Show the state of the last menu item"));
521 menuMenu
->Append(Menu_Menu_SetLabel
, _T("Set menu item label\tAlt-L"),
522 _T("Set the label of a menu item"));
525 menuMenu
->AppendSeparator();
526 menuMenu
->Append(Menu_Menu_FindItem
, _T("Find menu item from label"),
527 _T("Find a menu item by searching for its label"));
530 wxMenu
*testMenu
= new wxMenu
;
531 testMenu
->Append(Menu_Test_Normal
, _T("&Normal item"));
532 testMenu
->AppendSeparator();
533 testMenu
->AppendCheckItem(Menu_Test_Check
, _T("&Check item"));
534 testMenu
->AppendSeparator();
535 testMenu
->AppendRadioItem(Menu_Test_Radio1
, _T("Radio item &1"));
536 testMenu
->AppendRadioItem(Menu_Test_Radio2
, _T("Radio item &2"));
537 testMenu
->AppendRadioItem(Menu_Test_Radio3
, _T("Radio item &3"));
539 wxMenu
*helpMenu
= new wxMenu
;
540 helpMenu
->Append(Menu_Help_About
, _T("&About\tF1"), _T("About menu sample"));
542 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
544 menuBar
->Append(fileMenu
, _T("&File"));
545 menuBar
->Append(menubarMenu
, _T("Menu&bar"));
546 menuBar
->Append(menuMenu
, _T("&Menu"));
547 menuBar
->Append(testMenu
, _T("&Test"));
548 menuBar
->Append(helpMenu
, _T("&Help"));
550 // these items should be initially checked
551 menuBar
->Check(Menu_MenuBar_Toggle
, true);
552 menuBar
->Check(Menu_MenuBar_Enable
, true);
553 menuBar
->Check(Menu_Menu_Enable
, true);
554 menuBar
->Check(Menu_Menu_Check
, false);
556 // associate the menu bar with the frame
559 // intercept all menu events and log them in this custom event handler
560 PushEventHandler(new MyEvtHandler(this));
563 // create the log text window
564 m_textctrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
565 wxDefaultPosition
, wxDefaultSize
,
567 m_textctrl
->SetEditable(false);
569 wxLog::DisableTimestamp();
570 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
572 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
573 _T("append/insert/delete items to/from the last menu.\n")
574 _T("The commands from \"Menubar\" menu work with the ")
575 _T("menubar itself.\n\n")
576 _T("Right click the band below to test popup menus.\n"));
587 // delete the event handler installed in ctor
588 PopEventHandler(true);
591 // restore old logger
592 delete wxLog::SetActiveTarget(m_logOld
);
596 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
598 wxMenu
*menu
= new wxMenu
;
599 menu
->Append(Menu_Dummy_First
, _T("&First item\tCtrl-F1"));
600 menu
->AppendSeparator();
601 menu
->AppendCheckItem(Menu_Dummy_Second
, _T("&Second item\tCtrl-F2"));
605 title
->Printf(_T("Dummy menu &%u"), (unsigned)++m_countDummy
);
611 wxMenuItem
*MyFrame::GetLastMenuItem() const
613 wxMenuBar
*menubar
= GetMenuBar();
614 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
616 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
619 wxLogWarning(_T("No last item in the last menu!"));
625 return node
->GetData();
629 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
631 int id
= event
.GetId();
633 wxString msg
= wxString::Format(_T("Menu command %d"), id
);
635 // catch all checkable menubar items and also the check item from the popup
637 wxMenuItem
*item
= GetMenuBar()->FindItem(id
);
638 if ( (item
&& item
->IsCheckable()) || id
== Menu_Popup_ToBeChecked
)
640 msg
+= wxString::Format(_T(" (the item is currently %schecked)"),
641 event
.IsChecked() ? _T("") : _T("not "));
647 // ----------------------------------------------------------------------------
649 // ----------------------------------------------------------------------------
651 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
658 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
663 void MyFrame::OnClearLogUpdateUI(wxUpdateUIEvent
& event
)
665 // if we only enable this item when the log window is empty, we never see
666 // it in the disable state as a message is logged whenever the menu is
667 // opened, so we disable it if there is not "much" text in the window
668 event
.Enable( m_textctrl
->GetNumberOfLines() > 5 );
671 #endif // USE_LOG_WINDOW
673 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
675 (void)wxMessageBox(_T("wxWidgets menu sample\n(c) 1999-2001 Vadim Zeitlin"),
676 _T("About wxWidgets menu sample"),
680 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
682 wxMenuBar
*mbar
= GetMenuBar();
684 size_t count
= mbar
->GetMenuCount();
687 // don't let delete the first 2 menus
688 wxLogError(_T("Can't delete any more menus"));
692 delete mbar
->Remove(count
- 1);
696 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
699 wxMenu
*menu
= CreateDummyMenu(&title
);
700 GetMenuBar()->Insert(0, menu
, title
);
703 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
706 wxMenu
*menu
= CreateDummyMenu(&title
);
707 GetMenuBar()->Append(menu
, title
);
710 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
712 wxMenuBar
*mbar
= GetMenuBar();
716 m_menu
= mbar
->Remove(0);
721 mbar
->Insert(0, m_menu
, _T("&File"));
726 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
728 wxMenuBar
*mbar
= GetMenuBar();
729 size_t count
= mbar
->GetMenuCount();
731 mbar
->EnableTop(count
- 1, event
.IsChecked());
734 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
736 wxMenuBar
*mbar
= GetMenuBar();
737 size_t count
= mbar
->GetMenuCount();
739 wxCHECK_RET( count
, _T("no last menu?") );
741 wxLogMessage(_T("The label of the last menu item is '%s'"),
742 mbar
->GetMenuLabel(count
- 1).c_str());
746 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
748 wxMenuBar
*mbar
= GetMenuBar();
749 size_t count
= mbar
->GetMenuCount();
751 wxCHECK_RET( count
, _T("no last menu?") );
753 wxString label
= wxGetTextFromUser
755 _T("Enter new label: "),
756 _T("Change last menu text"),
757 mbar
->GetMenuLabel(count
- 1),
761 if ( !label
.empty() )
763 mbar
->SetMenuLabel(count
- 1, label
);
767 void MyFrame::OnFindMenu(wxCommandEvent
& WXUNUSED(event
))
769 wxMenuBar
*mbar
= GetMenuBar();
770 size_t count
= mbar
->GetMenuCount();
772 wxCHECK_RET( count
, _T("no last menu?") );
774 wxString label
= wxGetTextFromUser
776 _T("Enter label to search for: "),
782 if ( !label
.empty() )
784 int index
= mbar
->FindMenu(label
);
786 if (index
== wxNOT_FOUND
)
788 wxLogWarning(_T("No menu with label '%s'"), label
.c_str());
792 wxLogMessage(_T("Menu %d has label '%s'"), index
, label
.c_str());
798 void MyFrame::OnDummy(wxCommandEvent
& event
)
800 wxLogMessage(_T("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
803 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
805 wxMenuBar
*menubar
= GetMenuBar();
806 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
808 menu
->AppendSeparator();
809 menu
->Append(Menu_Dummy_Third
, _T("&Third dummy item\tCtrl-F3"),
810 _T("Checkable item"), true);
813 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
815 wxMenuBar
*menubar
= GetMenuBar();
817 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 2);
819 menu
->Append(Menu_Dummy_Last
, _T("&Dummy sub menu"),
820 CreateDummyMenu(NULL
), _T("Dummy sub menu help"));
823 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
825 wxMenuBar
*menubar
= GetMenuBar();
826 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
828 size_t count
= menu
->GetMenuItemCount();
831 wxLogWarning(_T("No items to delete!"));
835 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
839 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
841 wxMenuBar
*menubar
= GetMenuBar();
842 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
844 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
845 _T("Fourth dummy item\tCtrl-F4")));
846 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
));
849 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
851 wxMenuItem
*item
= GetLastMenuItem();
855 item
->Enable(!item
->IsEnabled());
859 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
861 wxMenuItem
*item
= GetLastMenuItem();
866 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
870 wxMenuItem
*item
= GetLastMenuItem();
872 event
.Enable(item
&& item
->IsCheckable());
875 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
877 wxMenuItem
*item
= GetLastMenuItem();
881 wxString label
= item
->GetItemLabel();
882 wxLogMessage(_T("The label of the last menu item is '%s'"),
888 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
890 wxMenuItem
*item
= GetLastMenuItem();
894 wxString label
= wxGetTextFromUser
896 _T("Enter new label: "),
897 _T("Change last menu item text"),
898 item
->GetItemLabel(),
901 label
.Replace( _T("\\t"), _T("\t") );
903 if ( !label
.empty() )
905 item
->SetItemLabel(label
);
911 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
913 wxMenuItem
*item
= GetLastMenuItem();
918 msg
<< _T("The item is ") << (item
->IsEnabled() ? _T("enabled")
922 if ( item
->IsCheckable() )
924 msg
<< _T("It is checkable and ") << (item
->IsChecked() ? _T("") : _T("un"))
929 wxAcceleratorEntry
*accel
= item
->GetAccel();
932 msg
<< _T("Its accelerator is ");
934 int flags
= accel
->GetFlags();
935 if ( flags
& wxACCEL_ALT
)
937 if ( flags
& wxACCEL_CTRL
)
939 if ( flags
& wxACCEL_SHIFT
)
942 int code
= accel
->GetKeyCode();
957 msg
<< _T('F') << code
- WXK_F1
+ 1;
960 // if there are any other keys wxGetAccelFromString() may return,
961 // we should process them here
964 if ( wxIsalnum(code
) )
971 wxFAIL_MSG( _T("unknown keyboard accel") );
978 msg
<< _T("It doesn't have an accelerator");
980 #endif // wxUSE_ACCEL
987 void MyFrame::OnFindMenuItem(wxCommandEvent
& WXUNUSED(event
))
989 wxMenuBar
*mbar
= GetMenuBar();
990 size_t count
= mbar
->GetMenuCount();
992 wxCHECK_RET( count
, _T("no last menu?") );
994 wxString label
= wxGetTextFromUser
996 _T("Enter label to search for: "),
997 _T("Find menu item"),
1002 if ( !label
.empty() )
1005 int index
= wxNOT_FOUND
;
1007 for (menuindex
= 0; (menuindex
< count
) && (index
== wxNOT_FOUND
); ++menuindex
)
1009 index
= mbar
->FindMenuItem(mbar
->GetMenu(menuindex
)->GetTitle(), label
);
1011 if (index
== wxNOT_FOUND
)
1013 wxLogWarning(_T("No menu item with label '%s'"), label
.c_str());
1017 wxLogMessage(_T("Menu item %d in menu %lu has label '%s'"),
1018 index
, (unsigned long)menuindex
, label
.c_str());
1024 void MyFrame::ShowContextMenu(const wxPoint
& pos
)
1028 if ( wxGetKeyState(WXK_SHIFT
) )
1030 // when Shift is pressed, demonstrate the use of a simple function
1031 // returning the id of the item selected in the popup menu
1032 menu
.SetTitle("Choose one of:");
1033 static const char *choices
[] = { "Apple", "Banana", "Cherry" };
1034 for ( size_t n
= 0; n
< WXSIZEOF(choices
); n
++ )
1035 menu
.Append(Menu_PopupChoice
+ n
, choices
[n
]);
1037 const int rc
= GetPopupMenuSelectionFromUser(menu
, pos
);
1038 if ( rc
== wxID_NONE
)
1040 wxLogMessage("No selection");
1044 wxLogMessage("You have selected \"%s\"",
1045 choices
[rc
- Menu_PopupChoice
]);
1048 else // normal case, shift not pressed
1050 menu
.Append(Menu_Help_About
, _T("&About"));
1051 menu
.Append(Menu_Popup_Submenu
, _T("&Submenu"), CreateDummyMenu(NULL
));
1052 menu
.Append(Menu_Popup_ToBeDeleted
, _T("To be &deleted"));
1053 menu
.AppendCheckItem(Menu_Popup_ToBeChecked
, _T("To be &checked"));
1054 menu
.Append(Menu_Popup_ToBeGreyed
, _T("To be &greyed"),
1055 _T("This menu item should be initially greyed out"));
1056 menu
.AppendSeparator();
1057 menu
.Append(Menu_File_Quit
, _T("E&xit"));
1059 menu
.Delete(Menu_Popup_ToBeDeleted
);
1060 menu
.Check(Menu_Popup_ToBeChecked
, true);
1061 menu
.Enable(Menu_Popup_ToBeGreyed
, false);
1063 PopupMenu(&menu
, pos
);
1065 // test for destroying items in popup menus
1066 #if 0 // doesn't work in wxGTK!
1067 menu
.Destroy(Menu_Popup_Submenu
);
1069 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
1074 void MyFrame::OnTestNormal(wxCommandEvent
& WXUNUSED(event
))
1076 wxLogMessage(_T("Normal item selected"));
1079 void MyFrame::OnTestCheck(wxCommandEvent
& event
)
1081 wxLogMessage(_T("Check item %schecked"),
1082 event
.IsChecked() ? _T("") : _T("un"));
1085 void MyFrame::OnTestRadio(wxCommandEvent
& event
)
1087 wxLogMessage(_T("Radio item %d selected"),
1088 event
.GetId() - Menu_Test_Radio1
+ 1);
1092 void MyFrame::LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
)
1096 << ( event
.IsPopup() ? _T("popup ") : _T("") )
1097 << _T("menu has been ")
1101 wxLogStatus(this, msg
.c_str());
1105 void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
)
1107 event
.Enable(false);
1110 void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
)
1115 void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
)
1117 int which
= (event
.GetId() - Menu_SubMenu_Radio1
+ 1);
1124 #if USE_CONTEXT_MENU
1125 void MyFrame::OnContextMenu(wxContextMenuEvent
& event
)
1127 wxPoint point
= event
.GetPosition();
1129 if (point
.x
== -1 && point
.y
== -1) {
1130 wxSize size
= GetSize();
1131 point
.x
= size
.x
/ 2;
1132 point
.y
= size
.y
/ 2;
1134 point
= ScreenToClient(point
);
1136 ShowContextMenu(point
);
1140 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
1146 // leave a band below for popup menu testing
1147 wxSize size
= GetClientSize();
1148 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
1151 // this is really ugly but we have to do it as we can't just call
1152 // event.Skip() because wxFrameBase would make the text control fill the
1153 // entire frame then
1154 #ifdef __WXUNIVERSAL__
1156 #endif // __WXUNIVERSAL__