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
);
89 void OnAbout(wxCommandEvent
& event
);
91 void OnDummy(wxCommandEvent
& event
);
93 void OnAppendMenuItem(wxCommandEvent
& event
);
94 void OnAppendSubMenu(wxCommandEvent
& event
);
95 void OnDeleteMenuItem(wxCommandEvent
& event
);
96 void OnInsertMenuItem(wxCommandEvent
& event
);
97 void OnCheckMenuItem(wxCommandEvent
& event
);
98 void OnEnableMenuItem(wxCommandEvent
& event
);
99 void OnGetLabelMenuItem(wxCommandEvent
& event
);
101 void OnSetLabelMenuItem(wxCommandEvent
& event
);
103 void OnGetMenuItemInfo(wxCommandEvent
& event
);
105 void OnFindMenuItem(wxCommandEvent
& event
);
108 void OnAppendMenu(wxCommandEvent
& event
);
109 void OnInsertMenu(wxCommandEvent
& event
);
110 void OnDeleteMenu(wxCommandEvent
& event
);
111 void OnToggleMenu(wxCommandEvent
& event
);
112 void OnEnableMenu(wxCommandEvent
& event
);
113 void OnGetLabelMenu(wxCommandEvent
& event
);
114 void OnSetLabelMenu(wxCommandEvent
& event
);
116 void OnFindMenu(wxCommandEvent
& event
);
119 void OnTestNormal(wxCommandEvent
& event
);
120 void OnTestCheck(wxCommandEvent
& event
);
121 void OnTestRadio(wxCommandEvent
& event
);
123 void OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
);
124 void OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
);
125 void OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
);
128 void OnContextMenu(wxContextMenuEvent
& event
);
130 void OnRightUp(wxMouseEvent
& event
)
131 { ShowContextMenu(event
.GetPosition()); }
134 void OnMenuOpen(wxMenuEvent
& event
)
137 LogMenuOpenOrClose(event
, _T("opened")); event
.Skip();
140 void OnMenuClose(wxMenuEvent
& event
)
143 LogMenuOpenOrClose(event
, _T("closed")); event
.Skip();
147 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
149 void OnSize(wxSizeEvent
& event
);
152 void LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
);
153 void ShowContextMenu(const wxPoint
& pos
);
155 wxMenu
*CreateDummyMenu(wxString
*title
);
157 wxMenuItem
*GetLastMenuItem() const;
159 // the menu previously detached from the menubar (may be NULL)
162 // the count of dummy menus already created
166 // the control used for logging
167 wxTextCtrl
*m_textctrl
;
170 // the previous log target
173 DECLARE_EVENT_TABLE()
176 // A small helper class which intercepts all menu events and logs them
177 class MyEvtHandler
: public wxEvtHandler
180 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
182 void OnMenuEvent(wxCommandEvent
& event
)
184 m_frame
->LogMenuEvent(event
);
192 DECLARE_EVENT_TABLE()
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
201 Menu_File_Quit
= wxID_EXIT
,
206 Menu_MenuBar_Toggle
= 200,
211 Menu_MenuBar_GetLabel
,
213 Menu_MenuBar_SetLabel
,
214 Menu_MenuBar_FindMenu
,
217 Menu_Menu_Append
= 300,
232 Menu_Test_Normal
= 400,
245 Menu_Dummy_First
= 500,
251 Menu_Help_About
= wxID_ABOUT
,
253 Menu_Popup_ToBeDeleted
= 2000,
254 Menu_Popup_ToBeGreyed
,
255 Menu_Popup_ToBeChecked
,
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
266 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
268 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
271 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
273 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
274 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
275 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
276 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
277 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
278 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
280 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
281 EVT_MENU(Menu_MenuBar_FindMenu
, MyFrame::OnFindMenu
)
284 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
285 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
286 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
287 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
288 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
289 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
290 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
292 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
294 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
296 EVT_MENU(Menu_Menu_FindItem
, MyFrame::OnFindMenuItem
)
299 EVT_MENU(Menu_Test_Normal
, MyFrame::OnTestNormal
)
300 EVT_MENU(Menu_Test_Check
, MyFrame::OnTestCheck
)
301 EVT_MENU(Menu_Test_Radio1
, MyFrame::OnTestRadio
)
302 EVT_MENU(Menu_Test_Radio2
, MyFrame::OnTestRadio
)
303 EVT_MENU(Menu_Test_Radio3
, MyFrame::OnTestRadio
)
305 EVT_UPDATE_UI(Menu_SubMenu_Normal
, MyFrame::OnUpdateSubMenuNormal
)
306 EVT_UPDATE_UI(Menu_SubMenu_Check
, MyFrame::OnUpdateSubMenuCheck
)
307 EVT_UPDATE_UI(Menu_SubMenu_Radio1
, MyFrame::OnUpdateSubMenuRadio
)
308 EVT_UPDATE_UI(Menu_SubMenu_Radio2
, MyFrame::OnUpdateSubMenuRadio
)
309 EVT_UPDATE_UI(Menu_SubMenu_Radio3
, MyFrame::OnUpdateSubMenuRadio
)
311 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
313 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
316 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
318 EVT_RIGHT_UP(MyFrame::OnRightUp
)
321 EVT_MENU_OPEN(MyFrame::OnMenuOpen
)
322 EVT_MENU_CLOSE(MyFrame::OnMenuClose
)
324 EVT_SIZE(MyFrame::OnSize
)
327 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
328 EVT_MENU(wxID_ANY
, MyEvtHandler::OnMenuEvent
)
331 // ============================================================================
333 // ============================================================================
335 // ----------------------------------------------------------------------------
337 // ----------------------------------------------------------------------------
341 // The `main program' equivalent, creating the windows and returning the
345 // Create the main frame window
346 MyFrame
* frame
= new MyFrame
;
351 frame
->SetStatusText(_T("Welcome to wxWidgets menu sample"));
352 #endif // wxUSE_STATUSBAR
359 // ----------------------------------------------------------------------------
361 // ----------------------------------------------------------------------------
363 // Define my frame constructor
365 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, _T("wxWidgets menu sample"))
376 #endif // wxUSE_STATUSBAR
378 // create the menubar
379 wxMenu
*fileMenu
= new wxMenu
;
382 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
383 _T("Clear &log\tCtrl-L"));
384 #if wxUSE_OWNER_DRAWN || defined(__WXGTK__)
385 item
->SetBitmap(copy_xpm
);
387 fileMenu
->Append(item
);
388 fileMenu
->AppendSeparator();
390 fileMenu
->Append(Menu_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit menu sample"));
392 wxMenu
*menubarMenu
= new wxMenu
;
393 menubarMenu
->Append(Menu_MenuBar_Append
, _T("&Append menu\tCtrl-A"),
394 _T("Append a menu to the menubar"));
395 menubarMenu
->Append(Menu_MenuBar_Insert
, _T("&Insert menu\tCtrl-I"),
396 _T("Insert a menu into the menubar"));
397 menubarMenu
->Append(Menu_MenuBar_Delete
, _T("&Delete menu\tCtrl-D"),
398 _T("Delete the last menu from the menubar"));
399 menubarMenu
->Append(Menu_MenuBar_Toggle
, _T("&Toggle menu\tCtrl-T"),
400 _T("Toggle the first menu in the menubar"), true);
401 menubarMenu
->AppendSeparator();
402 menubarMenu
->Append(Menu_MenuBar_Enable
, _T("&Enable menu\tCtrl-E"),
403 _T("Enable or disable the last menu"), true);
404 menubarMenu
->AppendSeparator();
405 menubarMenu
->Append(Menu_MenuBar_GetLabel
, _T("&Get menu label\tCtrl-G"),
406 _T("Get the label of the last menu"));
408 menubarMenu
->Append(Menu_MenuBar_SetLabel
, _T("&Set menu label\tCtrl-S"),
409 _T("Change the label of the last menu"));
410 menubarMenu
->AppendSeparator();
411 menubarMenu
->Append(Menu_MenuBar_FindMenu
, _T("&Find menu from label\tCtrl-F"),
412 _T("Find a menu by searching for its label"));
415 wxMenu
* subMenu
= new wxMenu
;
416 subMenu
->Append(Menu_SubMenu_Normal
, _T("&Normal submenu item"), _T("Disabled submenu item"));
417 subMenu
->AppendCheckItem(Menu_SubMenu_Check
, _T("&Check submenu item"), _T("Check submenu item"));
418 subMenu
->AppendRadioItem(Menu_SubMenu_Radio1
, _T("Radio item &1"), _T("Radio item"));
419 subMenu
->AppendRadioItem(Menu_SubMenu_Radio2
, _T("Radio item &2"), _T("Radio item"));
420 subMenu
->AppendRadioItem(Menu_SubMenu_Radio3
, _T("Radio item &3"), _T("Radio item"));
422 menubarMenu
->Append(Menu_SubMenu
, _T("Submenu"), subMenu
);
424 wxMenu
*menuMenu
= new wxMenu
;
425 menuMenu
->Append(Menu_Menu_Append
, _T("&Append menu item\tAlt-A"),
426 _T("Append a menu item to the last menu"));
427 menuMenu
->Append(Menu_Menu_AppendSub
, _T("&Append sub menu\tAlt-S"),
428 _T("Append a sub menu to the last menu"));
429 menuMenu
->Append(Menu_Menu_Insert
, _T("&Insert menu item\tAlt-I"),
430 _T("Insert a menu item in head of the last menu"));
431 menuMenu
->Append(Menu_Menu_Delete
, _T("&Delete menu item\tAlt-D"),
432 _T("Delete the last menu item from the last menu"));
433 menuMenu
->AppendSeparator();
434 menuMenu
->Append(Menu_Menu_Enable
, _T("&Enable menu item\tAlt-E"),
435 _T("Enable or disable the last menu item"), true);
436 menuMenu
->Append(Menu_Menu_Check
, _T("&Check menu item\tAlt-C"),
437 _T("Check or uncheck the last menu item"), true);
438 menuMenu
->AppendSeparator();
439 menuMenu
->Append(Menu_Menu_GetInfo
, _T("Get menu item in&fo\tAlt-F"),
440 _T("Show the state of the last menu item"));
442 menuMenu
->Append(Menu_Menu_SetLabel
, _T("Set menu item label\tAlt-L"),
443 _T("Set the label of a menu item"));
446 menuMenu
->AppendSeparator();
447 menuMenu
->Append(Menu_Menu_FindItem
, _T("Find menu item from label"),
448 _T("Find a menu item by searching for its label"));
451 wxMenu
*testMenu
= new wxMenu
;
452 testMenu
->Append(Menu_Test_Normal
, _T("&Normal item"));
453 testMenu
->AppendSeparator();
454 testMenu
->AppendCheckItem(Menu_Test_Check
, _T("&Check item"));
455 testMenu
->AppendSeparator();
456 testMenu
->AppendRadioItem(Menu_Test_Radio1
, _T("Radio item &1"));
457 testMenu
->AppendRadioItem(Menu_Test_Radio2
, _T("Radio item &2"));
458 testMenu
->AppendRadioItem(Menu_Test_Radio3
, _T("Radio item &3"));
460 wxMenu
*helpMenu
= new wxMenu
;
461 helpMenu
->Append(Menu_Help_About
, _T("&About\tF1"), _T("About menu sample"));
463 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
465 menuBar
->Append(fileMenu
, _T("&File"));
466 menuBar
->Append(menubarMenu
, _T("Menu&bar"));
467 menuBar
->Append(menuMenu
, _T("&Menu"));
468 menuBar
->Append(testMenu
, _T("&Test"));
469 menuBar
->Append(helpMenu
, _T("&Help"));
471 // these items should be initially checked
472 menuBar
->Check(Menu_MenuBar_Toggle
, true);
473 menuBar
->Check(Menu_MenuBar_Enable
, true);
474 menuBar
->Check(Menu_Menu_Enable
, true);
475 menuBar
->Check(Menu_Menu_Check
, false);
477 // associate the menu bar with the frame
480 // intercept all menu events and log them in this custom event handler
481 PushEventHandler(new MyEvtHandler(this));
484 // create the log text window
485 m_textctrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
486 wxDefaultPosition
, wxDefaultSize
,
488 m_textctrl
->SetEditable(false);
490 wxLog::SetTimestamp(NULL
);
491 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
493 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
494 _T("append/insert/delete items to/from the last menu.\n")
495 _T("The commands from \"Menubar\" menu work with the ")
496 _T("menubar itself.\n\n")
497 _T("Right click the band below to test popup menus.\n"));
508 // delete the event handler installed in ctor
509 PopEventHandler(true);
512 // restore old logger
513 delete wxLog::SetActiveTarget(m_logOld
);
517 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
519 wxMenu
*menu
= new wxMenu
;
520 menu
->Append(Menu_Dummy_First
, _T("&First item\tCtrl-F1"));
521 menu
->AppendSeparator();
522 menu
->AppendCheckItem(Menu_Dummy_Second
, _T("&Second item\tCtrl-F2"));
526 title
->Printf(_T("Dummy menu &%u"), (unsigned)++m_countDummy
);
532 wxMenuItem
*MyFrame::GetLastMenuItem() const
534 wxMenuBar
*menubar
= GetMenuBar();
535 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
537 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
540 wxLogWarning(_T("No last item in the last menu!"));
546 return node
->GetData();
550 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
552 int id
= event
.GetId();
554 wxString msg
= wxString::Format(_T("Menu command %d"), id
);
556 // catch all checkable menubar items and also the check item from the popup
558 wxMenuItem
*item
= GetMenuBar()->FindItem(id
);
559 if ( (item
&& item
->IsCheckable()) || id
== Menu_Popup_ToBeChecked
)
561 msg
+= wxString::Format(_T(" (the item is currently %schecked)"),
562 event
.IsChecked() ? _T("") : _T("not "));
568 // ----------------------------------------------------------------------------
570 // ----------------------------------------------------------------------------
572 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
578 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
584 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
586 (void)wxMessageBox(_T("wxWidgets menu sample\n(c) 1999-2001 Vadim Zeitlin"),
587 _T("About wxWidgets menu sample"),
591 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
593 wxMenuBar
*mbar
= GetMenuBar();
595 size_t count
= mbar
->GetMenuCount();
598 // don't let delete the first 2 menus
599 wxLogError(_T("Can't delete any more menus"));
603 delete mbar
->Remove(count
- 1);
607 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
610 wxMenu
*menu
= CreateDummyMenu(&title
);
611 GetMenuBar()->Insert(0, menu
, title
);
614 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
617 wxMenu
*menu
= CreateDummyMenu(&title
);
618 GetMenuBar()->Append(menu
, title
);
621 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
623 wxMenuBar
*mbar
= GetMenuBar();
627 m_menu
= mbar
->Remove(0);
632 mbar
->Insert(0, m_menu
, _T("&File"));
637 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
639 wxMenuBar
*mbar
= GetMenuBar();
640 size_t count
= mbar
->GetMenuCount();
642 mbar
->EnableTop(count
- 1, event
.IsChecked());
645 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
647 wxMenuBar
*mbar
= GetMenuBar();
648 size_t count
= mbar
->GetMenuCount();
650 wxCHECK_RET( count
, _T("no last menu?") );
652 wxLogMessage(_T("The label of the last menu item is '%s'"),
653 mbar
->GetLabelTop(count
- 1).c_str());
657 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
659 wxMenuBar
*mbar
= GetMenuBar();
660 size_t count
= mbar
->GetMenuCount();
662 wxCHECK_RET( count
, _T("no last menu?") );
664 wxString label
= wxGetTextFromUser
666 _T("Enter new label: "),
667 _T("Change last menu text"),
668 mbar
->GetLabelTop(count
- 1),
672 if ( !label
.empty() )
674 mbar
->SetLabelTop(count
- 1, label
);
678 void MyFrame::OnFindMenu(wxCommandEvent
& WXUNUSED(event
))
680 wxMenuBar
*mbar
= GetMenuBar();
681 size_t count
= mbar
->GetMenuCount();
683 wxCHECK_RET( count
, _T("no last menu?") );
685 wxString label
= wxGetTextFromUser
687 _T("Enter label to search for: "),
693 if ( !label
.empty() )
695 int index
= mbar
->FindMenu(label
);
697 if (index
== wxNOT_FOUND
)
699 wxLogWarning(_T("No menu with label '%s'"), label
.c_str());
703 wxLogMessage(_T("Menu %d has label '%s'"), index
, label
.c_str());
709 void MyFrame::OnDummy(wxCommandEvent
& event
)
711 wxLogMessage(_T("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
714 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
716 wxMenuBar
*menubar
= GetMenuBar();
717 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
719 menu
->AppendSeparator();
720 menu
->Append(Menu_Dummy_Third
, _T("&Third dummy item\tCtrl-F3"),
721 _T("Checkable item"), true);
724 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
726 wxMenuBar
*menubar
= GetMenuBar();
728 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 2);
730 menu
->Append(Menu_Dummy_Last
, _T("&Dummy sub menu"),
731 CreateDummyMenu(NULL
), _T("Dummy sub menu help"));
734 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
736 wxMenuBar
*menubar
= GetMenuBar();
737 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
739 size_t count
= menu
->GetMenuItemCount();
742 wxLogWarning(_T("No items to delete!"));
746 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
750 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
752 wxMenuBar
*menubar
= GetMenuBar();
753 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
755 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
756 _T("Fourth dummy item\tCtrl-F4")));
757 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
));
760 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
762 wxMenuItem
*item
= GetLastMenuItem();
766 item
->Enable(!item
->IsEnabled());
770 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
772 wxMenuItem
*item
= GetLastMenuItem();
777 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
781 wxMenuItem
*item
= GetLastMenuItem();
783 event
.Enable(item
&& item
->IsCheckable());
786 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
788 wxMenuItem
*item
= GetLastMenuItem();
792 wxLogMessage(_T("The label of the last menu item is '%s'"),
793 item
->GetLabel().c_str());
798 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
800 wxMenuItem
*item
= GetLastMenuItem();
804 wxString label
= wxGetTextFromUser
806 _T("Enter new label: "),
807 _T("Change last menu item text"),
811 label
.Replace( _T("\\t"), _T("\t") );
813 if ( !label
.empty() )
815 item
->SetText(label
);
821 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
823 wxMenuItem
*item
= GetLastMenuItem();
828 msg
<< _T("The item is ") << (item
->IsEnabled() ? _T("enabled")
832 if ( item
->IsCheckable() )
834 msg
<< _T("It is checkable and ") << (item
->IsChecked() ? _T("") : _T("un"))
839 wxAcceleratorEntry
*accel
= item
->GetAccel();
842 msg
<< _T("Its accelerator is ");
844 int flags
= accel
->GetFlags();
845 if ( flags
& wxACCEL_ALT
)
847 if ( flags
& wxACCEL_CTRL
)
849 if ( flags
& wxACCEL_SHIFT
)
852 int code
= accel
->GetKeyCode();
867 msg
<< _T('F') << code
- WXK_F1
+ 1;
870 // if there are any other keys wxGetAccelFromString() may return,
871 // we should process them here
874 if ( wxIsalnum(code
) )
881 wxFAIL_MSG( _T("unknown keyboard accel") );
888 msg
<< _T("It doesn't have an accelerator");
890 #endif // wxUSE_ACCEL
897 void MyFrame::OnFindMenuItem(wxCommandEvent
& WXUNUSED(event
))
899 wxMenuBar
*mbar
= GetMenuBar();
900 size_t count
= mbar
->GetMenuCount();
902 wxCHECK_RET( count
, _T("no last menu?") );
904 wxString label
= wxGetTextFromUser
906 _T("Enter label to search for: "),
907 _T("Find menu item"),
912 if ( !label
.empty() )
915 int index
= wxNOT_FOUND
;
917 for (menuindex
= 0; (menuindex
< count
) && (index
== wxNOT_FOUND
); ++menuindex
)
919 index
= mbar
->FindMenuItem(mbar
->GetMenu(menuindex
)->GetTitle(), label
);
921 if (index
== wxNOT_FOUND
)
923 wxLogWarning(_T("No menu item with label '%s'"), label
.c_str());
927 wxLogMessage(_T("Menu item %d in menu %lu has label '%s'"),
928 index
, (unsigned long)menuindex
, label
.c_str());
934 void MyFrame::ShowContextMenu(const wxPoint
& pos
)
938 menu
.Append(Menu_Help_About
, _T("&About"));
939 menu
.Append(Menu_Popup_Submenu
, _T("&Submenu"), CreateDummyMenu(NULL
));
940 menu
.Append(Menu_Popup_ToBeDeleted
, _T("To be &deleted"));
941 menu
.AppendCheckItem(Menu_Popup_ToBeChecked
, _T("To be &checked"));
942 menu
.Append(Menu_Popup_ToBeGreyed
, _T("To be &greyed"),
943 _T("This menu item should be initially greyed out"));
944 menu
.AppendSeparator();
945 menu
.Append(Menu_File_Quit
, _T("E&xit"));
947 menu
.Delete(Menu_Popup_ToBeDeleted
);
948 menu
.Check(Menu_Popup_ToBeChecked
, true);
949 menu
.Enable(Menu_Popup_ToBeGreyed
, false);
951 PopupMenu(&menu
, pos
.x
, pos
.y
);
953 // test for destroying items in popup menus
954 #if 0 // doesn't work in wxGTK!
955 menu
.Destroy(Menu_Popup_Submenu
);
957 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
961 void MyFrame::OnTestNormal(wxCommandEvent
& WXUNUSED(event
))
963 wxLogMessage(_T("Normal item selected"));
966 void MyFrame::OnTestCheck(wxCommandEvent
& event
)
968 wxLogMessage(_T("Check item %schecked"),
969 event
.IsChecked() ? _T("") : _T("un"));
972 void MyFrame::OnTestRadio(wxCommandEvent
& event
)
974 wxLogMessage(_T("Radio item %d selected"),
975 event
.GetId() - Menu_Test_Radio1
+ 1);
979 void MyFrame::LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
)
983 << ( event
.IsPopup() ? _T("popup ") : _T("") )
984 << _T("menu has been ")
988 wxLogStatus(this, msg
.c_str());
992 void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
)
997 void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
)
1002 void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
)
1004 int which
= (event
.GetId() - Menu_SubMenu_Radio1
+ 1);
1011 #if USE_CONTEXT_MENU
1012 void MyFrame::OnContextMenu(wxContextMenuEvent
& event
)
1014 wxPoint point
= event
.GetPosition();
1016 if (point
.x
== -1 && point
.y
== -1) {
1017 wxSize size
= GetSize();
1018 point
.x
= size
.x
/ 2;
1019 point
.y
= size
.y
/ 2;
1021 point
= ScreenToClient(point
);
1023 ShowContextMenu(point
);
1027 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
1033 // leave a band below for popup menu testing
1034 wxSize size
= GetClientSize();
1035 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
1038 // this is really ugly but we have to do it as we can't just call
1039 // event.Skip() because wxFrameBase would make the text control fill the
1040 // entire frame then
1041 #ifdef __WXUNIVERSAL__
1043 #endif // __WXUNIVERSAL__