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
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
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // Define a new application
65 class MyApp
: public wxApp
72 class MyFrame
: public wxFrame
79 void LogMenuEvent(const wxCommandEvent
& event
);
82 void OnQuit(wxCommandEvent
& event
);
84 void OnClearLog(wxCommandEvent
& event
);
87 void OnAbout(wxCommandEvent
& event
);
89 void OnDummy(wxCommandEvent
& event
);
91 void OnAppendMenuItem(wxCommandEvent
& event
);
92 void OnAppendSubMenu(wxCommandEvent
& event
);
93 void OnDeleteMenuItem(wxCommandEvent
& event
);
94 void OnInsertMenuItem(wxCommandEvent
& event
);
95 void OnCheckMenuItem(wxCommandEvent
& event
);
96 void OnEnableMenuItem(wxCommandEvent
& event
);
97 void OnGetLabelMenuItem(wxCommandEvent
& event
);
99 void OnSetLabelMenuItem(wxCommandEvent
& event
);
101 void OnGetMenuItemInfo(wxCommandEvent
& event
);
103 void OnFindMenuItem(wxCommandEvent
& event
);
106 void OnAppendMenu(wxCommandEvent
& event
);
107 void OnInsertMenu(wxCommandEvent
& event
);
108 void OnDeleteMenu(wxCommandEvent
& event
);
109 void OnToggleMenu(wxCommandEvent
& event
);
110 void OnEnableMenu(wxCommandEvent
& event
);
111 void OnGetLabelMenu(wxCommandEvent
& event
);
112 void OnSetLabelMenu(wxCommandEvent
& event
);
114 void OnFindMenu(wxCommandEvent
& event
);
117 void OnTestNormal(wxCommandEvent
& event
);
118 void OnTestCheck(wxCommandEvent
& event
);
119 void OnTestRadio(wxCommandEvent
& event
);
121 void OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
);
122 void OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
);
123 void OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
);
126 void OnContextMenu(wxContextMenuEvent
& event
)
127 { ShowContextMenu(ScreenToClient(event
.GetPosition())); }
129 void OnRightUp(wxMouseEvent
& event
)
130 { ShowContextMenu(event
.GetPosition()); }
133 void OnMenuOpen(wxMenuEvent
& event
)
136 LogMenuOpenOrClose(event
, _T("opened")); event
.Skip();
139 void OnMenuClose(wxMenuEvent
& event
)
142 LogMenuOpenOrClose(event
, _T("closed")); event
.Skip();
146 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
148 void OnSize(wxSizeEvent
& event
);
151 void LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
);
152 void ShowContextMenu(const wxPoint
& pos
);
154 wxMenu
*CreateDummyMenu(wxString
*title
);
156 wxMenuItem
*GetLastMenuItem() const;
158 // the menu previously detached from the menubar (may be NULL)
161 // the count of dummy menus already created
165 // the control used for logging
166 wxTextCtrl
*m_textctrl
;
169 // the previous log target
172 DECLARE_EVENT_TABLE()
175 // A small helper class which intercepts all menu events and logs them
176 class MyEvtHandler
: public wxEvtHandler
179 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
181 void OnMenuEvent(wxCommandEvent
& event
)
183 m_frame
->LogMenuEvent(event
);
191 DECLARE_EVENT_TABLE()
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
200 Menu_File_Quit
= wxID_EXIT
,
205 Menu_MenuBar_Toggle
= 200,
210 Menu_MenuBar_GetLabel
,
212 Menu_MenuBar_SetLabel
,
213 Menu_MenuBar_FindMenu
,
216 Menu_Menu_Append
= 300,
231 Menu_Test_Normal
= 400,
244 Menu_Dummy_First
= 500,
250 Menu_Help_About
= wxID_ABOUT
,
252 Menu_Popup_ToBeDeleted
= 2000,
253 Menu_Popup_ToBeGreyed
,
254 Menu_Popup_ToBeChecked
,
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
264 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
265 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
267 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
270 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
272 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
273 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
274 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
275 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
276 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
277 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
279 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
280 EVT_MENU(Menu_MenuBar_FindMenu
, MyFrame::OnFindMenu
)
283 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
284 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
285 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
286 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
287 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
288 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
289 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
291 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
293 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
295 EVT_MENU(Menu_Menu_FindItem
, MyFrame::OnFindMenuItem
)
298 EVT_MENU(Menu_Test_Normal
, MyFrame::OnTestNormal
)
299 EVT_MENU(Menu_Test_Check
, MyFrame::OnTestCheck
)
300 EVT_MENU(Menu_Test_Radio1
, MyFrame::OnTestRadio
)
301 EVT_MENU(Menu_Test_Radio2
, MyFrame::OnTestRadio
)
302 EVT_MENU(Menu_Test_Radio3
, MyFrame::OnTestRadio
)
304 EVT_UPDATE_UI(Menu_SubMenu_Normal
, MyFrame::OnUpdateSubMenuNormal
)
305 EVT_UPDATE_UI(Menu_SubMenu_Check
, MyFrame::OnUpdateSubMenuCheck
)
306 EVT_UPDATE_UI(Menu_SubMenu_Radio1
, MyFrame::OnUpdateSubMenuRadio
)
307 EVT_UPDATE_UI(Menu_SubMenu_Radio2
, MyFrame::OnUpdateSubMenuRadio
)
308 EVT_UPDATE_UI(Menu_SubMenu_Radio3
, MyFrame::OnUpdateSubMenuRadio
)
310 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
312 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
315 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
317 EVT_RIGHT_UP(MyFrame::OnRightUp
)
320 EVT_MENU_OPEN(MyFrame::OnMenuOpen
)
321 EVT_MENU_CLOSE(MyFrame::OnMenuClose
)
323 EVT_SIZE(MyFrame::OnSize
)
326 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
327 EVT_MENU(wxID_ANY
, MyEvtHandler::OnMenuEvent
)
330 // ============================================================================
332 // ============================================================================
334 // ----------------------------------------------------------------------------
336 // ----------------------------------------------------------------------------
340 // The `main program' equivalent, creating the windows and returning the
344 // Create the main frame window
345 MyFrame
* frame
= new MyFrame
;
350 frame
->SetStatusText(_T("Welcome to wxWindows menu sample"));
351 #endif // wxUSE_STATUSBAR
358 // ----------------------------------------------------------------------------
360 // ----------------------------------------------------------------------------
362 // Define my frame constructor
364 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, _T("wxWindows menu sample"))
375 #endif // wxUSE_STATUSBAR
377 // create the menubar
378 wxMenu
*fileMenu
= new wxMenu
;
381 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
382 _T("Clear &log\tCtrl-L"));
383 #if wxUSE_OWNER_DRAWN
384 item
->SetBitmap(copy_xpm
);
386 fileMenu
->Append(item
);
387 fileMenu
->AppendSeparator();
389 fileMenu
->Append(Menu_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit menu sample"));
391 wxMenu
*menubarMenu
= new wxMenu
;
392 menubarMenu
->Append(Menu_MenuBar_Append
, _T("&Append menu\tCtrl-A"),
393 _T("Append a menu to the menubar"));
394 menubarMenu
->Append(Menu_MenuBar_Insert
, _T("&Insert menu\tCtrl-I"),
395 _T("Insert a menu into the menubar"));
396 menubarMenu
->Append(Menu_MenuBar_Delete
, _T("&Delete menu\tCtrl-D"),
397 _T("Delete the last menu from the menubar"));
398 menubarMenu
->Append(Menu_MenuBar_Toggle
, _T("&Toggle menu\tCtrl-T"),
399 _T("Toggle the first menu in the menubar"), true);
400 menubarMenu
->AppendSeparator();
401 menubarMenu
->Append(Menu_MenuBar_Enable
, _T("&Enable menu\tCtrl-E"),
402 _T("Enable or disable the last menu"), true);
403 menubarMenu
->AppendSeparator();
404 menubarMenu
->Append(Menu_MenuBar_GetLabel
, _T("&Get menu label\tCtrl-G"),
405 _T("Get the label of the last menu"));
407 menubarMenu
->Append(Menu_MenuBar_SetLabel
, _T("&Set menu label\tCtrl-S"),
408 _T("Change the label of the last menu"));
409 menubarMenu
->AppendSeparator();
410 menubarMenu
->Append(Menu_MenuBar_FindMenu
, _T("&Find menu from label\tCtrl-F"),
411 _T("Find a menu by searching for its label"));
414 wxMenu
* subMenu
= new wxMenu
;
415 subMenu
->Append(Menu_SubMenu_Normal
, _T("&Normal submenu item"), _T("Disabled submenu item"));
416 subMenu
->AppendCheckItem(Menu_SubMenu_Check
, _T("&Unchecked submenu item"), _T("Unchecked submenu item"));
417 subMenu
->AppendRadioItem(Menu_SubMenu_Radio1
, _T("&Radio item 1"), _T("Radio item"));
418 subMenu
->AppendRadioItem(Menu_SubMenu_Radio2
, _T("&Radio item 2"), _T("Radio item"));
419 subMenu
->AppendRadioItem(Menu_SubMenu_Radio3
, _T("&Radio item 3"), _T("Radio item"));
421 menubarMenu
->Append(Menu_SubMenu
, _T("Submenu"), subMenu
);
423 wxMenu
*menuMenu
= new wxMenu
;
424 menuMenu
->Append(Menu_Menu_Append
, _T("&Append menu item\tAlt-A"),
425 _T("Append a menu item to the last menu"));
426 menuMenu
->Append(Menu_Menu_AppendSub
, _T("&Append sub menu\tAlt-S"),
427 _T("Append a sub menu to the last menu"));
428 menuMenu
->Append(Menu_Menu_Insert
, _T("&Insert menu item\tAlt-I"),
429 _T("Insert a menu item in head of the last menu"));
430 menuMenu
->Append(Menu_Menu_Delete
, _T("&Delete menu item\tAlt-D"),
431 _T("Delete the last menu item from the last menu"));
432 menuMenu
->AppendSeparator();
433 menuMenu
->Append(Menu_Menu_Enable
, _T("&Enable menu item\tAlt-E"),
434 _T("Enable or disable the last menu item"), true);
435 menuMenu
->Append(Menu_Menu_Check
, _T("&Check menu item\tAlt-C"),
436 _T("Check or uncheck the last menu item"), true);
437 menuMenu
->AppendSeparator();
438 menuMenu
->Append(Menu_Menu_GetInfo
, _T("Get menu item in&fo\tAlt-F"),
439 _T("Show the state of the last menu item"));
441 menuMenu
->AppendSeparator();
442 menuMenu
->Append(Menu_Menu_FindItem
, _T("Find menu item from label"),
443 _T("Find a menu item by searching for its label"));
446 wxMenu
*testMenu
= new wxMenu
;
447 testMenu
->Append(Menu_Test_Normal
, _T("&Normal item"));
448 testMenu
->AppendSeparator();
449 testMenu
->AppendCheckItem(Menu_Test_Check
, _T("&Check item"));
450 testMenu
->AppendSeparator();
451 testMenu
->AppendRadioItem(Menu_Test_Radio1
, _T("Radio item &1"));
452 testMenu
->AppendRadioItem(Menu_Test_Radio2
, _T("Radio item &2"));
453 testMenu
->AppendRadioItem(Menu_Test_Radio3
, _T("Radio item &3"));
455 wxMenu
*helpMenu
= new wxMenu
;
456 helpMenu
->Append(Menu_Help_About
, _T("&About\tF1"), _T("About menu sample"));
458 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
460 menuBar
->Append(fileMenu
, _T("&File"));
461 menuBar
->Append(menubarMenu
, _T("Menu&bar"));
462 menuBar
->Append(menuMenu
, _T("&Menu"));
463 menuBar
->Append(testMenu
, _T("&Test"));
464 menuBar
->Append(helpMenu
, _T("&Help"));
466 // these items should be initially checked
467 menuBar
->Check(Menu_MenuBar_Toggle
, true);
468 menuBar
->Check(Menu_MenuBar_Enable
, true);
469 menuBar
->Check(Menu_Menu_Enable
, true);
470 menuBar
->Check(Menu_Menu_Check
, false);
472 // associate the menu bar with the frame
475 // intercept all menu events and log them in this custom event handler
476 PushEventHandler(new MyEvtHandler(this));
479 // create the log text window
480 m_textctrl
= new wxTextCtrl(this, wxID_ANY
, _T(""),
481 wxDefaultPosition
, wxDefaultSize
,
483 m_textctrl
->SetEditable(false);
485 wxLog::SetTimestamp(NULL
);
486 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
488 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
489 _T("append/insert/delete items to/from the last menu.\n")
490 _T("The commands from \"Menubar\" menu work with the ")
491 _T("menubar itself.\n\n")
492 _T("Right click the band below to test popup menus.\n"));
500 // delete the event handler installed in ctor
501 PopEventHandler(true);
504 // restore old logger
505 delete wxLog::SetActiveTarget(m_logOld
);
509 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
511 wxMenu
*menu
= new wxMenu
;
512 menu
->Append(Menu_Dummy_First
, _T("&First item\tCtrl-F1"));
513 menu
->AppendSeparator();
514 menu
->Append(Menu_Dummy_Second
, _T("&Second item\tCtrl-F2"), _T(""), true);
518 title
->Printf(_T("Dummy menu &%u"), (unsigned)++m_countDummy
);
524 wxMenuItem
*MyFrame::GetLastMenuItem() const
526 wxMenuBar
*menubar
= GetMenuBar();
527 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
529 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
532 wxLogWarning(_T("No last item in the last menu!"));
538 return node
->GetData();
542 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
544 int id
= event
.GetId();
545 if ( !GetMenuBar()->FindItem(id
) )
548 wxString msg
= wxString::Format(_T("Menu command %d"), id
);
549 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
551 msg
+= wxString::Format(_T(" (the item is currently %schecked)"),
552 event
.IsChecked() ? _T("") : _T("not "));
558 // ----------------------------------------------------------------------------
560 // ----------------------------------------------------------------------------
562 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
568 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
574 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
576 (void)wxMessageBox(_T("wxWindows menu sample\n© 1999-2001 Vadim Zeitlin"),
577 _T("About wxWindows menu sample"),
581 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
583 wxMenuBar
*mbar
= GetMenuBar();
585 size_t count
= mbar
->GetMenuCount();
588 // don't let delete the first 2 menus
589 wxLogError(_T("Can't delete any more menus"));
593 delete mbar
->Remove(count
- 1);
597 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
600 wxMenu
*menu
= CreateDummyMenu(&title
);
601 GetMenuBar()->Insert(0, menu
, title
);
604 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
607 wxMenu
*menu
= CreateDummyMenu(&title
);
608 GetMenuBar()->Append(menu
, title
);
611 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
613 wxMenuBar
*mbar
= GetMenuBar();
617 m_menu
= mbar
->Remove(0);
622 mbar
->Insert(0, m_menu
, _T("&File"));
627 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
629 wxMenuBar
*mbar
= GetMenuBar();
630 size_t count
= mbar
->GetMenuCount();
632 mbar
->EnableTop(count
- 1, event
.IsChecked());
635 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
637 wxMenuBar
*mbar
= GetMenuBar();
638 size_t count
= mbar
->GetMenuCount();
640 wxCHECK_RET( count
, _T("no last menu?") );
642 wxLogMessage(_T("The label of the last menu item is '%s'"),
643 mbar
->GetLabelTop(count
- 1).c_str());
647 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
649 wxMenuBar
*mbar
= GetMenuBar();
650 size_t count
= mbar
->GetMenuCount();
652 wxCHECK_RET( count
, _T("no last menu?") );
654 wxString label
= wxGetTextFromUser
656 _T("Enter new label: "),
657 _T("Change last menu text"),
658 mbar
->GetLabelTop(count
- 1),
662 if ( !label
.empty() )
664 mbar
->SetLabelTop(count
- 1, label
);
668 void MyFrame::OnFindMenu(wxCommandEvent
& WXUNUSED(event
))
670 wxMenuBar
*mbar
= GetMenuBar();
671 size_t count
= mbar
->GetMenuCount();
673 wxCHECK_RET( count
, _T("no last menu?") );
675 wxString label
= wxGetTextFromUser
677 _T("Enter label to search for: "),
683 if ( !label
.empty() )
685 int index
= mbar
->FindMenu(label
);
687 if (index
== wxNOT_FOUND
)
689 wxLogWarning(_T("No menu with label '%s'"), label
.c_str());
693 wxLogMessage(_T("Menu %d has label '%s'"), index
, label
.c_str());
699 void MyFrame::OnDummy(wxCommandEvent
& event
)
701 wxLogMessage(_T("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
704 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
706 wxMenuBar
*menubar
= GetMenuBar();
707 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
709 menu
->AppendSeparator();
710 menu
->Append(Menu_Dummy_Third
, _T("&Third dummy item\tCtrl-F3"),
711 _T("Checkable item"), true);
714 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
716 wxMenuBar
*menubar
= GetMenuBar();
718 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 2);
720 menu
->Append(Menu_Dummy_Last
, _T("&Dummy sub menu"),
721 CreateDummyMenu(NULL
), _T("Dummy sub menu help"));
724 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
726 wxMenuBar
*menubar
= GetMenuBar();
727 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
729 size_t count
= menu
->GetMenuItemCount();
732 wxLogWarning(_T("No items to delete!"));
736 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
740 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
742 wxMenuBar
*menubar
= GetMenuBar();
743 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
745 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
746 _T("Fourth dummy item\tCtrl-F4")));
747 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, _T("")));
750 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
752 wxMenuItem
*item
= GetLastMenuItem();
756 item
->Enable(!item
->IsEnabled());
760 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
762 wxMenuItem
*item
= GetLastMenuItem();
767 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
771 wxMenuItem
*item
= GetLastMenuItem();
773 event
.Enable(item
&& item
->IsCheckable());
776 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
778 wxMenuItem
*item
= GetLastMenuItem();
782 wxLogMessage(_T("The label of the last menu item is '%s'"),
783 item
->GetLabel().c_str());
788 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
790 wxMenuItem
*item
= GetLastMenuItem();
794 wxString label
= wxGetTextFromUser
796 _T("Enter new label: "),
797 _T("Change last menu item text"),
802 if ( !label
.empty() )
804 item
->SetText(label
);
810 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
812 wxMenuItem
*item
= GetLastMenuItem();
817 msg
<< _T("The item is ") << (item
->IsEnabled() ? _T("enabled")
821 if ( item
->IsCheckable() )
823 msg
<< _T("It is checkable and ") << (item
->IsChecked() ? _T("") : _T("un"))
828 wxAcceleratorEntry
*accel
= item
->GetAccel();
831 msg
<< _T("Its accelerator is ");
833 int flags
= accel
->GetFlags();
834 if ( flags
& wxACCEL_ALT
)
836 if ( flags
& wxACCEL_CTRL
)
838 if ( flags
& wxACCEL_SHIFT
)
841 int code
= accel
->GetKeyCode();
856 msg
<< _T('F') << code
- WXK_F1
+ 1;
859 // if there are any other keys wxGetAccelFromString() may return,
860 // we should process them here
863 if ( wxIsalnum(code
) )
870 wxFAIL_MSG( _T("unknown keyboard accel") );
877 msg
<< _T("It doesn't have an accelerator");
879 #endif // wxUSE_ACCEL
886 void MyFrame::OnFindMenuItem(wxCommandEvent
& WXUNUSED(event
))
888 wxMenuBar
*mbar
= GetMenuBar();
889 size_t count
= mbar
->GetMenuCount();
891 wxCHECK_RET( count
, _T("no last menu?") );
893 wxString label
= wxGetTextFromUser
895 _T("Enter label to search for: "),
896 _T("Find menu item"),
901 if ( !label
.empty() )
904 int index
= wxNOT_FOUND
;
906 for (menuindex
= 0; (menuindex
< count
) && (index
== wxNOT_FOUND
); ++menuindex
)
908 index
= mbar
->FindMenuItem(mbar
->GetMenu(menuindex
)->GetTitle(), label
);
910 if (index
== wxNOT_FOUND
)
912 wxLogWarning(_T("No menu item with label '%s'"), label
.c_str());
916 wxLogMessage(_T("Menu item %d in menu %lu has label '%s'"),
917 index
, (unsigned long)menuindex
, label
.c_str());
923 void MyFrame::ShowContextMenu(const wxPoint
& pos
)
925 wxMenu
menu(_T("Test popup"));
927 menu
.Append(Menu_Help_About
, _T("&About"));
928 menu
.Append(Menu_Popup_Submenu
, _T("&Submenu"), CreateDummyMenu(NULL
));
929 menu
.Append(Menu_Popup_ToBeDeleted
, _T("To be &deleted"));
930 menu
.Append(Menu_Popup_ToBeChecked
, _T("To be &checked"), _T(""), true);
931 menu
.Append(Menu_Popup_ToBeGreyed
, _T("To be &greyed"),
932 _T("This menu item should be initially greyed out"));
933 menu
.AppendSeparator();
934 menu
.Append(Menu_File_Quit
, _T("E&xit"));
936 menu
.Delete(Menu_Popup_ToBeDeleted
);
937 menu
.Check(Menu_Popup_ToBeChecked
, true);
938 menu
.Enable(Menu_Popup_ToBeGreyed
, false);
940 PopupMenu(&menu
, pos
.x
, pos
.y
);
942 // test for destroying items in popup menus
943 #if 0 // doesn't work in wxGTK!
944 menu
.Destroy(Menu_Popup_Submenu
);
946 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
950 void MyFrame::OnTestNormal(wxCommandEvent
& WXUNUSED(event
))
952 wxLogMessage(_T("Normal item selected"));
955 void MyFrame::OnTestCheck(wxCommandEvent
& event
)
957 wxLogMessage(_T("Check item %schecked"),
958 event
.IsChecked() ? _T("") : _T("un"));
961 void MyFrame::OnTestRadio(wxCommandEvent
& event
)
963 wxLogMessage(_T("Radio item %d selected"),
964 event
.GetId() - Menu_Test_Radio1
+ 1);
968 void MyFrame::LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
)
972 << ( event
.IsPopup() ? _T("popup ") : _T("") )
973 << _T("menu has been ")
977 wxLogStatus(this, msg
.c_str());
981 void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
)
986 void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
)
992 void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
)
994 int which
= (event
.GetId() - Menu_SubMenu_Radio1
+ 1);
1001 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
1007 // leave a band below for popup menu testing
1008 wxSize size
= GetClientSize();
1009 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
1012 // this is really ugly but we have to do it as we can't just call
1013 // event.Skip() because wxFrameBase would make the text control fill the
1014 // entire frame then
1015 #ifdef __WXUNIVERSAL__
1017 #endif // __WXUNIVERSAL__