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"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // Define a new application
49 class MyApp
: public wxApp
56 class MyFrame
: public wxFrame
63 void LogMenuEvent(const wxCommandEvent
& event
);
66 void OnQuit(wxCommandEvent
& event
);
67 void OnClearLog(wxCommandEvent
& event
);
69 void OnAbout(wxCommandEvent
& event
);
71 void OnDummy(wxCommandEvent
& event
);
73 void OnAppendMenuItem(wxCommandEvent
& event
);
74 void OnAppendSubMenu(wxCommandEvent
& event
);
75 void OnDeleteMenuItem(wxCommandEvent
& event
);
76 void OnInsertMenuItem(wxCommandEvent
& event
);
77 void OnCheckMenuItem(wxCommandEvent
& event
);
78 void OnEnableMenuItem(wxCommandEvent
& event
);
79 void OnGetLabelMenuItem(wxCommandEvent
& event
);
80 void OnSetLabelMenuItem(wxCommandEvent
& event
);
81 void OnGetMenuItemInfo(wxCommandEvent
& event
);
82 void OnFindMenuItem(wxCommandEvent
& event
);
84 void OnAppendMenu(wxCommandEvent
& event
);
85 void OnInsertMenu(wxCommandEvent
& event
);
86 void OnDeleteMenu(wxCommandEvent
& event
);
87 void OnToggleMenu(wxCommandEvent
& event
);
88 void OnEnableMenu(wxCommandEvent
& event
);
89 void OnGetLabelMenu(wxCommandEvent
& event
);
90 void OnSetLabelMenu(wxCommandEvent
& event
);
91 void OnFindMenu(wxCommandEvent
& event
);
93 void OnTestNormal(wxCommandEvent
& event
);
94 void OnTestCheck(wxCommandEvent
& event
);
95 void OnTestRadio(wxCommandEvent
& event
);
97 void OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
);
98 void OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
);
99 void OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
);
101 #if defined( __WXMSW__ ) || defined( __WXMAC__ )
102 void OnContextMenu(wxContextMenuEvent
& event
)
103 { ShowContextMenu(ScreenToClient(event
.GetPosition())); }
105 void OnRightUp(wxMouseEvent
& event
)
106 { ShowContextMenu(event
.GetPosition()); }
109 void OnMenuOpen(wxMenuEvent
& event
)
110 { LogMenuOpenOrClose(event
, _T("opened")); }
111 void OnMenuClose(wxMenuEvent
& event
)
112 { LogMenuOpenOrClose(event
, _T("closed")); }
114 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
116 void OnSize(wxSizeEvent
& event
);
119 void LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
);
120 void ShowContextMenu(const wxPoint
& pos
);
122 wxMenu
*CreateDummyMenu(wxString
*title
);
124 wxMenuItem
*GetLastMenuItem() const;
126 // the menu previously detached from the menubar (may be NULL)
129 // the count of dummy menus already created
132 // the control used for logging
133 wxTextCtrl
*m_textctrl
;
135 // the previous log target
138 DECLARE_EVENT_TABLE()
141 // A small helper class which intercepts all menu events and logs them
142 class MyEvtHandler
: public wxEvtHandler
145 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
147 void OnMenuEvent(wxCommandEvent
& event
)
149 m_frame
->LogMenuEvent(event
);
157 DECLARE_EVENT_TABLE()
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
166 Menu_File_Quit
= 100,
169 Menu_MenuBar_Toggle
= 200,
174 Menu_MenuBar_GetLabel
,
175 Menu_MenuBar_SetLabel
,
176 Menu_MenuBar_FindMenu
,
178 Menu_Menu_Append
= 300,
189 Menu_Test_Normal
= 400,
202 Menu_Dummy_First
= 500,
208 Menu_Help_About
= 1000,
210 Menu_Popup_ToBeDeleted
= 2000,
211 Menu_Popup_ToBeGreyed
,
212 Menu_Popup_ToBeChecked
,
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
223 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
224 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
226 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
228 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
229 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
230 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
231 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
232 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
233 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
234 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
235 EVT_MENU(Menu_MenuBar_FindMenu
, MyFrame::OnFindMenu
)
237 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
238 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
239 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
240 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
241 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
242 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
243 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
244 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
245 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
246 EVT_MENU(Menu_Menu_FindItem
, MyFrame::OnFindMenuItem
)
248 EVT_MENU(Menu_Test_Normal
, MyFrame::OnTestNormal
)
249 EVT_MENU(Menu_Test_Check
, MyFrame::OnTestCheck
)
250 EVT_MENU(Menu_Test_Radio1
, MyFrame::OnTestRadio
)
251 EVT_MENU(Menu_Test_Radio2
, MyFrame::OnTestRadio
)
252 EVT_MENU(Menu_Test_Radio3
, MyFrame::OnTestRadio
)
254 EVT_UPDATE_UI(Menu_SubMenu_Normal
, MyFrame::OnUpdateSubMenuNormal
)
255 EVT_UPDATE_UI(Menu_SubMenu_Check
, MyFrame::OnUpdateSubMenuCheck
)
256 EVT_UPDATE_UI(Menu_SubMenu_Radio1
, MyFrame::OnUpdateSubMenuRadio
)
257 EVT_UPDATE_UI(Menu_SubMenu_Radio2
, MyFrame::OnUpdateSubMenuRadio
)
258 EVT_UPDATE_UI(Menu_SubMenu_Radio3
, MyFrame::OnUpdateSubMenuRadio
)
260 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
262 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
264 #if defined( __WXMSW__ ) || defined( __WXMAC__ )
265 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
267 EVT_RIGHT_UP(MyFrame::OnRightUp
)
270 EVT_MENU_OPEN(MyFrame::OnMenuOpen
)
271 EVT_MENU_CLOSE(MyFrame::OnMenuClose
)
273 EVT_SIZE(MyFrame::OnSize
)
276 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
277 EVT_MENU(-1, MyEvtHandler::OnMenuEvent
)
280 // ============================================================================
282 // ============================================================================
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
290 // The `main program' equivalent, creating the windows and returning the
294 // Create the main frame window
295 MyFrame
* frame
= new MyFrame
;
300 frame
->SetStatusText(_T("Welcome to wxWindows menu sample"));
301 #endif // wxUSE_STATUSBAR
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
312 // Define my frame constructor
314 : wxFrame((wxFrame
*)NULL
, -1, _T("wxWindows menu sample"),
315 wxDefaultPosition
, wxSize(400, 250))
324 #endif // wxUSE_STATUSBAR
326 // create the menubar
327 wxMenu
*fileMenu
= new wxMenu
;
329 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
330 _T("Clear &log\tCtrl-L"));
331 item
->SetBitmap(copy_xpm
);
332 fileMenu
->Append(item
);
333 fileMenu
->AppendSeparator();
334 fileMenu
->Append(Menu_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit menu sample"));
336 wxMenu
*menubarMenu
= new wxMenu
;
337 menubarMenu
->Append(Menu_MenuBar_Append
, _T("&Append menu\tCtrl-A"),
338 _T("Append a menu to the menubar"));
339 menubarMenu
->Append(Menu_MenuBar_Insert
, _T("&Insert menu\tCtrl-I"),
340 _T("Insert a menu into the menubar"));
341 menubarMenu
->Append(Menu_MenuBar_Delete
, _T("&Delete menu\tCtrl-D"),
342 _T("Delete the last menu from the menubar"));
343 menubarMenu
->Append(Menu_MenuBar_Toggle
, _T("&Toggle menu\tCtrl-T"),
344 _T("Toggle the first menu in the menubar"), TRUE
);
345 menubarMenu
->AppendSeparator();
346 menubarMenu
->Append(Menu_MenuBar_Enable
, _T("&Enable menu\tCtrl-E"),
347 _T("Enable or disable the last menu"), TRUE
);
348 menubarMenu
->AppendSeparator();
349 menubarMenu
->Append(Menu_MenuBar_GetLabel
, _T("&Get menu label\tCtrl-G"),
350 _T("Get the label of the last menu"));
351 menubarMenu
->Append(Menu_MenuBar_SetLabel
, _T("&Set menu label\tCtrl-S"),
352 _T("Change the label of the last menu"));
353 menubarMenu
->AppendSeparator();
354 menubarMenu
->Append(Menu_MenuBar_FindMenu
, _T("&Find menu from label\tCtrl-F"),
355 _T("Find a menu by searching for its label"));
357 wxMenu
* subMenu
= new wxMenu
;
358 subMenu
->Append(Menu_SubMenu_Normal
, _T("&Normal submenu item"), _T("Disabled submenu item"));
359 subMenu
->AppendCheckItem(Menu_SubMenu_Check
, _T("&Unchecked submenu item"), _T("Unchecked submenu item"));
360 subMenu
->AppendRadioItem(Menu_SubMenu_Radio1
, _T("&Radio item 1"), _T("Radio item"));
361 subMenu
->AppendRadioItem(Menu_SubMenu_Radio2
, _T("&Radio item 2"), _T("Radio item"));
362 subMenu
->AppendRadioItem(Menu_SubMenu_Radio3
, _T("&Radio item 3"), _T("Radio item"));
364 menubarMenu
->Append(Menu_SubMenu
, _T("Submenu"), subMenu
);
366 wxMenu
*menuMenu
= new wxMenu
;
367 menuMenu
->Append(Menu_Menu_Append
, _T("&Append menu item\tAlt-A"),
368 _T("Append a menu item to the last menu"));
369 menuMenu
->Append(Menu_Menu_AppendSub
, _T("&Append sub menu\tAlt-S"),
370 _T("Append a sub menu to the last menu"));
371 menuMenu
->Append(Menu_Menu_Insert
, _T("&Insert menu item\tAlt-I"),
372 _T("Insert a menu item in head of the last menu"));
373 menuMenu
->Append(Menu_Menu_Delete
, _T("&Delete menu item\tAlt-D"),
374 _T("Delete the last menu item from the last menu"));
375 menuMenu
->AppendSeparator();
376 menuMenu
->Append(Menu_Menu_Enable
, _T("&Enable menu item\tAlt-E"),
377 _T("Enable or disable the last menu item"), TRUE
);
378 menuMenu
->Append(Menu_Menu_Check
, _T("&Check menu item\tAlt-C"),
379 _T("Check or uncheck the last menu item"), TRUE
);
380 menuMenu
->AppendSeparator();
381 menuMenu
->Append(Menu_Menu_GetInfo
, _T("Get menu item in&fo\tAlt-F"),
382 _T("Show the state of the last menu item"));
383 menuMenu
->AppendSeparator();
384 menuMenu
->Append(Menu_Menu_FindItem
, _T("Find menu item from label"),
385 _T("Find a menu item by searching for its label"));
387 wxMenu
*testMenu
= new wxMenu
;
388 testMenu
->Append(Menu_Test_Normal
, _T("&Normal item"));
389 testMenu
->AppendSeparator();
390 testMenu
->AppendCheckItem(Menu_Test_Check
, _T("&Check item"));
391 testMenu
->AppendSeparator();
392 testMenu
->AppendRadioItem(Menu_Test_Radio1
, _T("Radio item &1"));
393 testMenu
->AppendRadioItem(Menu_Test_Radio2
, _T("Radio item &2"));
394 testMenu
->AppendRadioItem(Menu_Test_Radio3
, _T("Radio item &3"));
396 wxMenu
*helpMenu
= new wxMenu
;
397 helpMenu
->Append(Menu_Help_About
, _T("&About\tF1"), _T("About menu sample"));
399 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
401 menuBar
->Append(fileMenu
, _T("&File"));
402 menuBar
->Append(menubarMenu
, _T("Menu&bar"));
403 menuBar
->Append(menuMenu
, _T("&Menu"));
404 menuBar
->Append(testMenu
, _T("&Test"));
405 menuBar
->Append(helpMenu
, _T("&Help"));
407 // these items should be initially checked
408 menuBar
->Check(Menu_MenuBar_Toggle
, TRUE
);
409 menuBar
->Check(Menu_MenuBar_Enable
, TRUE
);
410 menuBar
->Check(Menu_Menu_Enable
, TRUE
);
411 menuBar
->Check(Menu_Menu_Check
, FALSE
);
413 // associate the menu bar with the frame
416 // intercept all menu events and log them in this custom event handler
417 PushEventHandler(new MyEvtHandler(this));
419 // create the log text window
420 m_textctrl
= new wxTextCtrl(this, -1, _T(""),
421 wxDefaultPosition
, wxDefaultSize
,
423 m_textctrl
->SetEditable(FALSE
);
425 wxLog::SetTimestamp(NULL
);
426 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
428 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
429 _T("append/insert/delete items to/from the last menu.\n")
430 _T("The commands from \"Menubar\" menu work with the ")
431 _T("menubar itself.\n\n")
432 _T("Right click the band below to test popup menus.\n"));
439 // delete the event handler installed in ctor
440 PopEventHandler(TRUE
);
442 // restore old logger
443 delete wxLog::SetActiveTarget(m_logOld
);
446 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
448 wxMenu
*menu
= new wxMenu
;
449 menu
->Append(Menu_Dummy_First
, _T("&First item\tCtrl-F1"));
450 menu
->AppendSeparator();
451 menu
->Append(Menu_Dummy_Second
, _T("&Second item\tCtrl-F2"), _T(""), TRUE
);
455 title
->Printf(wxT("Dummy menu &%u"), (unsigned)++m_countDummy
);
461 wxMenuItem
*MyFrame::GetLastMenuItem() const
463 wxMenuBar
*menubar
= GetMenuBar();
464 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
466 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
469 wxLogWarning(wxT("No last item in the last menu!"));
475 return node
->GetData();
479 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
481 int id
= event
.GetId();
482 if ( !GetMenuBar()->FindItem(id
) )
485 wxString msg
= wxString::Format(wxT("Menu command %d"), id
);
486 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
488 msg
+= wxString::Format(wxT(" (the item is currently %schecked)"),
489 event
.IsChecked() ? "" : "not ");
495 // ----------------------------------------------------------------------------
497 // ----------------------------------------------------------------------------
499 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
504 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
509 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
511 (void)wxMessageBox(_T("wxWindows menu sample\n© 1999-2001 Vadim Zeitlin"),
512 _T("About wxWindows menu sample"),
516 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
518 wxMenuBar
*mbar
= GetMenuBar();
520 size_t count
= mbar
->GetMenuCount();
523 // don't let delete the first 2 menus
524 wxLogError(wxT("Can't delete any more menus"));
528 delete mbar
->Remove(count
- 1);
532 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
535 wxMenu
*menu
= CreateDummyMenu(&title
);
536 GetMenuBar()->Insert(0, menu
, title
);
539 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
542 wxMenu
*menu
= CreateDummyMenu(&title
);
543 GetMenuBar()->Append(menu
, title
);
546 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
548 wxMenuBar
*mbar
= GetMenuBar();
552 m_menu
= mbar
->Remove(0);
557 mbar
->Insert(0, m_menu
, _T("&File"));
562 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
564 wxMenuBar
*mbar
= GetMenuBar();
565 size_t count
= mbar
->GetMenuCount();
567 mbar
->EnableTop(count
- 1, event
.IsChecked());
570 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
572 wxMenuBar
*mbar
= GetMenuBar();
573 size_t count
= mbar
->GetMenuCount();
575 wxCHECK_RET( count
, _T("no last menu?") );
577 wxLogMessage(wxT("The label of the last menu item is '%s'"),
578 mbar
->GetLabelTop(count
- 1).c_str());
581 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
583 wxMenuBar
*mbar
= GetMenuBar();
584 size_t count
= mbar
->GetMenuCount();
586 wxCHECK_RET( count
, _T("no last menu?") );
588 wxString label
= wxGetTextFromUser
590 _T("Enter new label: "),
591 _T("Change last menu text"),
592 mbar
->GetLabelTop(count
- 1),
596 if ( !label
.empty() )
598 mbar
->SetLabelTop(count
- 1, label
);
602 void MyFrame::OnFindMenu(wxCommandEvent
& WXUNUSED(event
))
604 wxMenuBar
*mbar
= GetMenuBar();
605 size_t count
= mbar
->GetMenuCount();
607 wxCHECK_RET( count
, _T("no last menu?") );
609 wxString label
= wxGetTextFromUser
611 _T("Enter label to search for: "),
617 if ( !label
.empty() )
619 int index
= mbar
->FindMenu(label
);
621 if (index
== wxNOT_FOUND
)
623 wxLogWarning(wxT("No menu with label '%s'"), label
.c_str());
627 wxLogMessage(wxT("Menu %d has label '%s'"), index
, label
.c_str());
632 void MyFrame::OnDummy(wxCommandEvent
& event
)
634 wxLogMessage(wxT("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
637 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
639 wxMenuBar
*menubar
= GetMenuBar();
640 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
642 menu
->AppendSeparator();
643 menu
->Append(Menu_Dummy_Third
, _T("&Third dummy item\tCtrl-F3"),
644 _T("Checkable item"), TRUE
);
647 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
649 wxMenuBar
*menubar
= GetMenuBar();
651 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 2);
653 menu
->Append(Menu_Dummy_Last
, _T("&Dummy sub menu"),
654 CreateDummyMenu(NULL
), _T("Dummy sub menu help"));
657 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
659 wxMenuBar
*menubar
= GetMenuBar();
660 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
662 size_t count
= menu
->GetMenuItemCount();
665 wxLogWarning(wxT("No items to delete!"));
669 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
673 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
675 wxMenuBar
*menubar
= GetMenuBar();
676 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
678 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
679 _T("Fourth dummy item\tCtrl-F4")));
680 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, _T("")));
683 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
685 wxMenuItem
*item
= GetLastMenuItem();
689 item
->Enable(!item
->IsEnabled());
693 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
695 wxMenuItem
*item
= GetLastMenuItem();
700 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
704 wxMenuItem
*item
= GetLastMenuItem();
706 event
.Enable(item
&& item
->IsCheckable());
709 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
711 wxMenuItem
*item
= GetLastMenuItem();
715 wxLogMessage(wxT("The label of the last menu item is '%s'"),
716 item
->GetLabel().c_str());
720 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
722 wxMenuItem
*item
= GetLastMenuItem();
726 wxString label
= wxGetTextFromUser
728 _T("Enter new label: "),
729 _T("Change last menu item text"),
734 if ( !label
.empty() )
736 item
->SetText(label
);
741 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
743 wxMenuItem
*item
= GetLastMenuItem();
748 msg
<< _T("The item is ") << (item
->IsEnabled() ? _T("enabled")
752 if ( item
->IsCheckable() )
754 msg
<< _T("It is checkable and ") << (item
->IsChecked() ? _T("") : _T("un"))
759 wxAcceleratorEntry
*accel
= item
->GetAccel();
762 msg
<< _T("Its accelerator is ");
764 int flags
= accel
->GetFlags();
765 if ( flags
& wxACCEL_ALT
)
767 if ( flags
& wxACCEL_CTRL
)
769 if ( flags
& wxACCEL_SHIFT
)
770 msg
<< wxT("Shift-");
772 int code
= accel
->GetKeyCode();
787 msg
<< wxT('F') << code
- WXK_F1
+ 1;
790 // if there are any other keys wxGetAccelFromString() may return,
791 // we should process them here
794 if ( wxIsalnum(code
) )
801 wxFAIL_MSG( wxT("unknown keyboard accel") );
808 msg
<< _T("It doesn't have an accelerator");
810 #endif // wxUSE_ACCEL
816 void MyFrame::OnFindMenuItem(wxCommandEvent
& WXUNUSED(event
))
818 wxMenuBar
*mbar
= GetMenuBar();
819 size_t count
= mbar
->GetMenuCount();
821 wxCHECK_RET( count
, _T("no last menu?") );
823 wxString label
= wxGetTextFromUser
825 _T("Enter label to search for: "),
826 _T("Find menu item"),
831 if ( !label
.empty() )
833 size_t menuindex
= 0;
834 int index
= wxNOT_FOUND
;
836 for (menuindex
= 0; (menuindex
< count
) && (index
== wxNOT_FOUND
); ++menuindex
)
838 index
= mbar
->FindMenuItem(mbar
->GetMenu(menuindex
)->GetTitle(), label
);
840 if (index
== wxNOT_FOUND
)
842 wxLogWarning(wxT("No menu item with label '%s'"), label
.c_str());
846 wxLogMessage(wxT("Menu item %d in menu %lu has label '%s'"),
847 index
, (unsigned long)menuindex
, label
.c_str());
852 void MyFrame::ShowContextMenu(const wxPoint
& pos
)
854 wxMenu
menu(_T("Test popup"));
856 menu
.Append(Menu_Help_About
, _T("&About"));
857 menu
.Append(Menu_Popup_Submenu
, _T("&Submenu"), CreateDummyMenu(NULL
));
858 menu
.Append(Menu_Popup_ToBeDeleted
, _T("To be &deleted"));
859 menu
.Append(Menu_Popup_ToBeChecked
, _T("To be &checked"), _T(""), TRUE
);
860 menu
.Append(Menu_Popup_ToBeGreyed
, _T("To be &greyed"),
861 _T("This menu item should be initially greyed out"));
862 menu
.AppendSeparator();
863 menu
.Append(Menu_File_Quit
, _T("E&xit"));
865 menu
.Delete(Menu_Popup_ToBeDeleted
);
866 menu
.Check(Menu_Popup_ToBeChecked
, TRUE
);
867 menu
.Enable(Menu_Popup_ToBeGreyed
, FALSE
);
869 PopupMenu(&menu
, pos
.x
, pos
.y
);
871 // test for destroying items in popup menus
872 #if 0 // doesn't work in wxGTK!
873 menu
.Destroy(Menu_Popup_Submenu
);
875 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
879 void MyFrame::OnTestNormal(wxCommandEvent
& WXUNUSED(event
))
881 wxLogMessage(_T("Normal item selected"));
884 void MyFrame::OnTestCheck(wxCommandEvent
& event
)
886 wxLogMessage(_T("Check item %schecked"),
887 event
.IsChecked() ? _T("") : _T("un"));
890 void MyFrame::OnTestRadio(wxCommandEvent
& event
)
892 wxLogMessage(_T("Radio item %d selected"),
893 event
.GetId() - Menu_Test_Radio1
+ 1);
896 void MyFrame::LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
)
898 wxLogStatus(this, _T("A %smenu has been %s."),
899 event
.IsPopup() ? _T("popup ") : _T(""), what
);
902 void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
)
907 void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
)
913 void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
)
915 int which
= (event
.GetId() - Menu_SubMenu_Radio1
+ 1);
922 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
927 // leave a band below for popup menu testing
928 wxSize size
= GetClientSize();
929 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
931 // this is really ugly but we have to do it as we can't just call
932 // event.Skip() because wxFrameBase would make the text control fill the
934 #ifdef __WXUNIVERSAL__
936 #endif // __WXUNIVERSAL__