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__)
45 #define USE_CONTEXT_MENU 0
47 #define USE_CONTEXT_MENU 1
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // Define a new application
57 class MyApp
: public wxApp
64 class MyFrame
: public wxFrame
71 void LogMenuEvent(const wxCommandEvent
& event
);
74 void OnQuit(wxCommandEvent
& event
);
75 void OnClearLog(wxCommandEvent
& event
);
77 void OnAbout(wxCommandEvent
& event
);
79 void OnDummy(wxCommandEvent
& event
);
81 void OnAppendMenuItem(wxCommandEvent
& event
);
82 void OnAppendSubMenu(wxCommandEvent
& event
);
83 void OnDeleteMenuItem(wxCommandEvent
& event
);
84 void OnInsertMenuItem(wxCommandEvent
& event
);
85 void OnCheckMenuItem(wxCommandEvent
& event
);
86 void OnEnableMenuItem(wxCommandEvent
& event
);
87 void OnGetLabelMenuItem(wxCommandEvent
& event
);
88 void OnSetLabelMenuItem(wxCommandEvent
& event
);
89 void OnGetMenuItemInfo(wxCommandEvent
& event
);
90 void OnFindMenuItem(wxCommandEvent
& event
);
92 void OnAppendMenu(wxCommandEvent
& event
);
93 void OnInsertMenu(wxCommandEvent
& event
);
94 void OnDeleteMenu(wxCommandEvent
& event
);
95 void OnToggleMenu(wxCommandEvent
& event
);
96 void OnEnableMenu(wxCommandEvent
& event
);
97 void OnGetLabelMenu(wxCommandEvent
& event
);
98 void OnSetLabelMenu(wxCommandEvent
& event
);
99 void OnFindMenu(wxCommandEvent
& event
);
101 void OnTestNormal(wxCommandEvent
& event
);
102 void OnTestCheck(wxCommandEvent
& event
);
103 void OnTestRadio(wxCommandEvent
& event
);
105 void OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
);
106 void OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
);
107 void OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
);
110 void OnContextMenu(wxContextMenuEvent
& event
)
111 { ShowContextMenu(ScreenToClient(event
.GetPosition())); }
113 void OnRightUp(wxMouseEvent
& event
)
114 { ShowContextMenu(event
.GetPosition()); }
117 void OnMenuOpen(wxMenuEvent
& event
)
118 { LogMenuOpenOrClose(event
, _T("opened")); event
.Skip(); }
119 void OnMenuClose(wxMenuEvent
& event
)
120 { LogMenuOpenOrClose(event
, _T("closed")); event
.Skip(); }
122 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
124 void OnSize(wxSizeEvent
& event
);
127 void LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
);
128 void ShowContextMenu(const wxPoint
& pos
);
130 wxMenu
*CreateDummyMenu(wxString
*title
);
132 wxMenuItem
*GetLastMenuItem() const;
134 // the menu previously detached from the menubar (may be NULL)
137 // the count of dummy menus already created
140 // the control used for logging
141 wxTextCtrl
*m_textctrl
;
143 // the previous log target
146 DECLARE_EVENT_TABLE()
149 // A small helper class which intercepts all menu events and logs them
150 class MyEvtHandler
: public wxEvtHandler
153 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
155 void OnMenuEvent(wxCommandEvent
& event
)
157 m_frame
->LogMenuEvent(event
);
165 DECLARE_EVENT_TABLE()
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
174 Menu_File_Quit
= 100,
177 Menu_MenuBar_Toggle
= 200,
182 Menu_MenuBar_GetLabel
,
183 Menu_MenuBar_SetLabel
,
184 Menu_MenuBar_FindMenu
,
186 Menu_Menu_Append
= 300,
197 Menu_Test_Normal
= 400,
210 Menu_Dummy_First
= 500,
216 Menu_Help_About
= 1000,
218 Menu_Popup_ToBeDeleted
= 2000,
219 Menu_Popup_ToBeGreyed
,
220 Menu_Popup_ToBeChecked
,
226 // ----------------------------------------------------------------------------
228 // ----------------------------------------------------------------------------
230 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
231 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
232 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
234 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
236 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
237 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
238 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
239 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
240 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
241 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
242 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
243 EVT_MENU(Menu_MenuBar_FindMenu
, MyFrame::OnFindMenu
)
245 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
246 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
247 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
248 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
249 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
250 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
251 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
252 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
253 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
254 EVT_MENU(Menu_Menu_FindItem
, MyFrame::OnFindMenuItem
)
256 EVT_MENU(Menu_Test_Normal
, MyFrame::OnTestNormal
)
257 EVT_MENU(Menu_Test_Check
, MyFrame::OnTestCheck
)
258 EVT_MENU(Menu_Test_Radio1
, MyFrame::OnTestRadio
)
259 EVT_MENU(Menu_Test_Radio2
, MyFrame::OnTestRadio
)
260 EVT_MENU(Menu_Test_Radio3
, MyFrame::OnTestRadio
)
262 EVT_UPDATE_UI(Menu_SubMenu_Normal
, MyFrame::OnUpdateSubMenuNormal
)
263 EVT_UPDATE_UI(Menu_SubMenu_Check
, MyFrame::OnUpdateSubMenuCheck
)
264 EVT_UPDATE_UI(Menu_SubMenu_Radio1
, MyFrame::OnUpdateSubMenuRadio
)
265 EVT_UPDATE_UI(Menu_SubMenu_Radio2
, MyFrame::OnUpdateSubMenuRadio
)
266 EVT_UPDATE_UI(Menu_SubMenu_Radio3
, MyFrame::OnUpdateSubMenuRadio
)
268 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
270 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
273 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
275 EVT_RIGHT_UP(MyFrame::OnRightUp
)
278 EVT_MENU_OPEN(MyFrame::OnMenuOpen
)
279 EVT_MENU_CLOSE(MyFrame::OnMenuClose
)
281 EVT_SIZE(MyFrame::OnSize
)
284 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
285 EVT_MENU(-1, MyEvtHandler::OnMenuEvent
)
288 // ============================================================================
290 // ============================================================================
292 // ----------------------------------------------------------------------------
294 // ----------------------------------------------------------------------------
298 // The `main program' equivalent, creating the windows and returning the
302 // Create the main frame window
303 MyFrame
* frame
= new MyFrame
;
308 frame
->SetStatusText(_T("Welcome to wxWindows menu sample"));
309 #endif // wxUSE_STATUSBAR
316 // ----------------------------------------------------------------------------
318 // ----------------------------------------------------------------------------
320 // Define my frame constructor
322 : wxFrame((wxFrame
*)NULL
, -1, _T("wxWindows menu sample"),
323 wxDefaultPosition
, wxSize(400, 250))
332 #endif // wxUSE_STATUSBAR
334 // create the menubar
335 wxMenu
*fileMenu
= new wxMenu
;
337 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
338 _T("Clear &log\tCtrl-L"));
339 item
->SetBitmap(copy_xpm
);
340 fileMenu
->Append(item
);
341 fileMenu
->AppendSeparator();
342 fileMenu
->Append(Menu_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit menu sample"));
344 wxMenu
*menubarMenu
= new wxMenu
;
345 menubarMenu
->Append(Menu_MenuBar_Append
, _T("&Append menu\tCtrl-A"),
346 _T("Append a menu to the menubar"));
347 menubarMenu
->Append(Menu_MenuBar_Insert
, _T("&Insert menu\tCtrl-I"),
348 _T("Insert a menu into the menubar"));
349 menubarMenu
->Append(Menu_MenuBar_Delete
, _T("&Delete menu\tCtrl-D"),
350 _T("Delete the last menu from the menubar"));
351 menubarMenu
->Append(Menu_MenuBar_Toggle
, _T("&Toggle menu\tCtrl-T"),
352 _T("Toggle the first menu in the menubar"), true);
353 menubarMenu
->AppendSeparator();
354 menubarMenu
->Append(Menu_MenuBar_Enable
, _T("&Enable menu\tCtrl-E"),
355 _T("Enable or disable the last menu"), true);
356 menubarMenu
->AppendSeparator();
357 menubarMenu
->Append(Menu_MenuBar_GetLabel
, _T("&Get menu label\tCtrl-G"),
358 _T("Get the label of the last menu"));
359 menubarMenu
->Append(Menu_MenuBar_SetLabel
, _T("&Set menu label\tCtrl-S"),
360 _T("Change the label of the last menu"));
361 menubarMenu
->AppendSeparator();
362 menubarMenu
->Append(Menu_MenuBar_FindMenu
, _T("&Find menu from label\tCtrl-F"),
363 _T("Find a menu by searching for its label"));
365 wxMenu
* subMenu
= new wxMenu
;
366 subMenu
->Append(Menu_SubMenu_Normal
, _T("&Normal submenu item"), _T("Disabled submenu item"));
367 subMenu
->AppendCheckItem(Menu_SubMenu_Check
, _T("&Unchecked submenu item"), _T("Unchecked submenu item"));
368 subMenu
->AppendRadioItem(Menu_SubMenu_Radio1
, _T("&Radio item 1"), _T("Radio item"));
369 subMenu
->AppendRadioItem(Menu_SubMenu_Radio2
, _T("&Radio item 2"), _T("Radio item"));
370 subMenu
->AppendRadioItem(Menu_SubMenu_Radio3
, _T("&Radio item 3"), _T("Radio item"));
372 menubarMenu
->Append(Menu_SubMenu
, _T("Submenu"), subMenu
);
374 wxMenu
*menuMenu
= new wxMenu
;
375 menuMenu
->Append(Menu_Menu_Append
, _T("&Append menu item\tAlt-A"),
376 _T("Append a menu item to the last menu"));
377 menuMenu
->Append(Menu_Menu_AppendSub
, _T("&Append sub menu\tAlt-S"),
378 _T("Append a sub menu to the last menu"));
379 menuMenu
->Append(Menu_Menu_Insert
, _T("&Insert menu item\tAlt-I"),
380 _T("Insert a menu item in head of the last menu"));
381 menuMenu
->Append(Menu_Menu_Delete
, _T("&Delete menu item\tAlt-D"),
382 _T("Delete the last menu item from the last menu"));
383 menuMenu
->AppendSeparator();
384 menuMenu
->Append(Menu_Menu_Enable
, _T("&Enable menu item\tAlt-E"),
385 _T("Enable or disable the last menu item"), true);
386 menuMenu
->Append(Menu_Menu_Check
, _T("&Check menu item\tAlt-C"),
387 _T("Check or uncheck the last menu item"), true);
388 menuMenu
->AppendSeparator();
389 menuMenu
->Append(Menu_Menu_GetInfo
, _T("Get menu item in&fo\tAlt-F"),
390 _T("Show the state of the last menu item"));
391 menuMenu
->AppendSeparator();
392 menuMenu
->Append(Menu_Menu_FindItem
, _T("Find menu item from label"),
393 _T("Find a menu item by searching for its label"));
395 wxMenu
*testMenu
= new wxMenu
;
396 testMenu
->Append(Menu_Test_Normal
, _T("&Normal item"));
397 testMenu
->AppendSeparator();
398 testMenu
->AppendCheckItem(Menu_Test_Check
, _T("&Check item"));
399 testMenu
->AppendSeparator();
400 testMenu
->AppendRadioItem(Menu_Test_Radio1
, _T("Radio item &1"));
401 testMenu
->AppendRadioItem(Menu_Test_Radio2
, _T("Radio item &2"));
402 testMenu
->AppendRadioItem(Menu_Test_Radio3
, _T("Radio item &3"));
404 wxMenu
*helpMenu
= new wxMenu
;
405 helpMenu
->Append(Menu_Help_About
, _T("&About\tF1"), _T("About menu sample"));
407 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
409 menuBar
->Append(fileMenu
, _T("&File"));
410 menuBar
->Append(menubarMenu
, _T("Menu&bar"));
411 menuBar
->Append(menuMenu
, _T("&Menu"));
412 menuBar
->Append(testMenu
, _T("&Test"));
413 menuBar
->Append(helpMenu
, _T("&Help"));
415 // these items should be initially checked
416 menuBar
->Check(Menu_MenuBar_Toggle
, true);
417 menuBar
->Check(Menu_MenuBar_Enable
, true);
418 menuBar
->Check(Menu_Menu_Enable
, true);
419 menuBar
->Check(Menu_Menu_Check
, false);
421 // associate the menu bar with the frame
424 // intercept all menu events and log them in this custom event handler
425 PushEventHandler(new MyEvtHandler(this));
427 // create the log text window
428 m_textctrl
= new wxTextCtrl(this, -1, _T(""),
429 wxDefaultPosition
, wxDefaultSize
,
431 m_textctrl
->SetEditable(false);
433 wxLog::SetTimestamp(NULL
);
434 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
436 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
437 _T("append/insert/delete items to/from the last menu.\n")
438 _T("The commands from \"Menubar\" menu work with the ")
439 _T("menubar itself.\n\n")
440 _T("Right click the band below to test popup menus.\n"));
447 // delete the event handler installed in ctor
448 PopEventHandler(true);
450 // restore old logger
451 delete wxLog::SetActiveTarget(m_logOld
);
454 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
456 wxMenu
*menu
= new wxMenu
;
457 menu
->Append(Menu_Dummy_First
, _T("&First item\tCtrl-F1"));
458 menu
->AppendSeparator();
459 menu
->Append(Menu_Dummy_Second
, _T("&Second item\tCtrl-F2"), _T(""), true);
463 title
->Printf(_T("Dummy menu &%u"), (unsigned)++m_countDummy
);
469 wxMenuItem
*MyFrame::GetLastMenuItem() const
471 wxMenuBar
*menubar
= GetMenuBar();
472 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
474 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
477 wxLogWarning(_T("No last item in the last menu!"));
483 return node
->GetData();
487 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
489 int id
= event
.GetId();
490 if ( !GetMenuBar()->FindItem(id
) )
493 wxString msg
= wxString::Format(_T("Menu command %d"), id
);
494 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
496 msg
+= wxString::Format(_T(" (the item is currently %schecked)"),
497 event
.IsChecked() ? _T("") : _T("not "));
503 // ----------------------------------------------------------------------------
505 // ----------------------------------------------------------------------------
507 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
512 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
517 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
519 (void)wxMessageBox(_T("wxWindows menu sample\n© 1999-2001 Vadim Zeitlin"),
520 _T("About wxWindows menu sample"),
524 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
526 wxMenuBar
*mbar
= GetMenuBar();
528 size_t count
= mbar
->GetMenuCount();
531 // don't let delete the first 2 menus
532 wxLogError(_T("Can't delete any more menus"));
536 delete mbar
->Remove(count
- 1);
540 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
543 wxMenu
*menu
= CreateDummyMenu(&title
);
544 GetMenuBar()->Insert(0, menu
, title
);
547 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
550 wxMenu
*menu
= CreateDummyMenu(&title
);
551 GetMenuBar()->Append(menu
, title
);
554 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
556 wxMenuBar
*mbar
= GetMenuBar();
560 m_menu
= mbar
->Remove(0);
565 mbar
->Insert(0, m_menu
, _T("&File"));
570 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
572 wxMenuBar
*mbar
= GetMenuBar();
573 size_t count
= mbar
->GetMenuCount();
575 mbar
->EnableTop(count
- 1, event
.IsChecked());
578 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
580 wxMenuBar
*mbar
= GetMenuBar();
581 size_t count
= mbar
->GetMenuCount();
583 wxCHECK_RET( count
, _T("no last menu?") );
585 wxLogMessage(_T("The label of the last menu item is '%s'"),
586 mbar
->GetLabelTop(count
- 1).c_str());
589 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
591 wxMenuBar
*mbar
= GetMenuBar();
592 size_t count
= mbar
->GetMenuCount();
594 wxCHECK_RET( count
, _T("no last menu?") );
596 wxString label
= wxGetTextFromUser
598 _T("Enter new label: "),
599 _T("Change last menu text"),
600 mbar
->GetLabelTop(count
- 1),
604 if ( !label
.empty() )
606 mbar
->SetLabelTop(count
- 1, label
);
610 void MyFrame::OnFindMenu(wxCommandEvent
& WXUNUSED(event
))
612 wxMenuBar
*mbar
= GetMenuBar();
613 size_t count
= mbar
->GetMenuCount();
615 wxCHECK_RET( count
, _T("no last menu?") );
617 wxString label
= wxGetTextFromUser
619 _T("Enter label to search for: "),
625 if ( !label
.empty() )
627 int index
= mbar
->FindMenu(label
);
629 if (index
== wxNOT_FOUND
)
631 wxLogWarning(_T("No menu with label '%s'"), label
.c_str());
635 wxLogMessage(_T("Menu %d has label '%s'"), index
, label
.c_str());
640 void MyFrame::OnDummy(wxCommandEvent
& event
)
642 wxLogMessage(_T("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
645 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
647 wxMenuBar
*menubar
= GetMenuBar();
648 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
650 menu
->AppendSeparator();
651 menu
->Append(Menu_Dummy_Third
, _T("&Third dummy item\tCtrl-F3"),
652 _T("Checkable item"), true);
655 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
657 wxMenuBar
*menubar
= GetMenuBar();
659 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 2);
661 menu
->Append(Menu_Dummy_Last
, _T("&Dummy sub menu"),
662 CreateDummyMenu(NULL
), _T("Dummy sub menu help"));
665 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
667 wxMenuBar
*menubar
= GetMenuBar();
668 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
670 size_t count
= menu
->GetMenuItemCount();
673 wxLogWarning(_T("No items to delete!"));
677 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
681 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
683 wxMenuBar
*menubar
= GetMenuBar();
684 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
686 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
687 _T("Fourth dummy item\tCtrl-F4")));
688 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, _T("")));
691 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
693 wxMenuItem
*item
= GetLastMenuItem();
697 item
->Enable(!item
->IsEnabled());
701 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
703 wxMenuItem
*item
= GetLastMenuItem();
708 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
712 wxMenuItem
*item
= GetLastMenuItem();
714 event
.Enable(item
&& item
->IsCheckable());
717 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
719 wxMenuItem
*item
= GetLastMenuItem();
723 wxLogMessage(_T("The label of the last menu item is '%s'"),
724 item
->GetLabel().c_str());
728 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
730 wxMenuItem
*item
= GetLastMenuItem();
734 wxString label
= wxGetTextFromUser
736 _T("Enter new label: "),
737 _T("Change last menu item text"),
742 if ( !label
.empty() )
744 item
->SetText(label
);
749 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
751 wxMenuItem
*item
= GetLastMenuItem();
756 msg
<< _T("The item is ") << (item
->IsEnabled() ? _T("enabled")
760 if ( item
->IsCheckable() )
762 msg
<< _T("It is checkable and ") << (item
->IsChecked() ? _T("") : _T("un"))
767 wxAcceleratorEntry
*accel
= item
->GetAccel();
770 msg
<< _T("Its accelerator is ");
772 int flags
= accel
->GetFlags();
773 if ( flags
& wxACCEL_ALT
)
775 if ( flags
& wxACCEL_CTRL
)
777 if ( flags
& wxACCEL_SHIFT
)
780 int code
= accel
->GetKeyCode();
795 msg
<< _T('F') << code
- WXK_F1
+ 1;
798 // if there are any other keys wxGetAccelFromString() may return,
799 // we should process them here
802 if ( wxIsalnum(code
) )
809 wxFAIL_MSG( _T("unknown keyboard accel") );
816 msg
<< _T("It doesn't have an accelerator");
818 #endif // wxUSE_ACCEL
824 void MyFrame::OnFindMenuItem(wxCommandEvent
& WXUNUSED(event
))
826 wxMenuBar
*mbar
= GetMenuBar();
827 size_t count
= mbar
->GetMenuCount();
829 wxCHECK_RET( count
, _T("no last menu?") );
831 wxString label
= wxGetTextFromUser
833 _T("Enter label to search for: "),
834 _T("Find menu item"),
839 if ( !label
.empty() )
842 int index
= wxNOT_FOUND
;
844 for (menuindex
= 0; (menuindex
< count
) && (index
== wxNOT_FOUND
); ++menuindex
)
846 index
= mbar
->FindMenuItem(mbar
->GetMenu(menuindex
)->GetTitle(), label
);
848 if (index
== wxNOT_FOUND
)
850 wxLogWarning(_T("No menu item with label '%s'"), label
.c_str());
854 wxLogMessage(_T("Menu item %d in menu %lu has label '%s'"),
855 index
, (unsigned long)menuindex
, label
.c_str());
860 void MyFrame::ShowContextMenu(const wxPoint
& pos
)
862 wxMenu
menu(_T("Test popup"));
864 menu
.Append(Menu_Help_About
, _T("&About"));
865 menu
.Append(Menu_Popup_Submenu
, _T("&Submenu"), CreateDummyMenu(NULL
));
866 menu
.Append(Menu_Popup_ToBeDeleted
, _T("To be &deleted"));
867 menu
.Append(Menu_Popup_ToBeChecked
, _T("To be &checked"), _T(""), true);
868 menu
.Append(Menu_Popup_ToBeGreyed
, _T("To be &greyed"),
869 _T("This menu item should be initially greyed out"));
870 menu
.AppendSeparator();
871 menu
.Append(Menu_File_Quit
, _T("E&xit"));
873 menu
.Delete(Menu_Popup_ToBeDeleted
);
874 menu
.Check(Menu_Popup_ToBeChecked
, true);
875 menu
.Enable(Menu_Popup_ToBeGreyed
, false);
877 PopupMenu(&menu
, pos
.x
, pos
.y
);
879 // test for destroying items in popup menus
880 #if 0 // doesn't work in wxGTK!
881 menu
.Destroy(Menu_Popup_Submenu
);
883 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
887 void MyFrame::OnTestNormal(wxCommandEvent
& WXUNUSED(event
))
889 wxLogMessage(_T("Normal item selected"));
892 void MyFrame::OnTestCheck(wxCommandEvent
& event
)
894 wxLogMessage(_T("Check item %schecked"),
895 event
.IsChecked() ? _T("") : _T("un"));
898 void MyFrame::OnTestRadio(wxCommandEvent
& event
)
900 wxLogMessage(_T("Radio item %d selected"),
901 event
.GetId() - Menu_Test_Radio1
+ 1);
904 void MyFrame::LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
)
908 << ( event
.IsPopup() ? _T("popup ") : _T("") )
909 << _T("menu has been ")
913 wxLogStatus(this, msg
.c_str());
916 void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
)
921 void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
)
927 void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
)
929 int which
= (event
.GetId() - Menu_SubMenu_Radio1
+ 1);
936 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
941 // leave a band below for popup menu testing
942 wxSize size
= GetClientSize();
943 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
945 // this is really ugly but we have to do it as we can't just call
946 // event.Skip() because wxFrameBase would make the text control fill the
948 #ifdef __WXUNIVERSAL__
950 #endif // __WXUNIVERSAL__