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
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
)
129 { ShowContextMenu(ScreenToClient(event
.GetPosition())); }
131 void OnRightUp(wxMouseEvent
& event
)
132 { ShowContextMenu(event
.GetPosition()); }
135 void OnMenuOpen(wxMenuEvent
& event
)
138 LogMenuOpenOrClose(event
, _T("opened")); event
.Skip();
141 void OnMenuClose(wxMenuEvent
& event
)
144 LogMenuOpenOrClose(event
, _T("closed")); event
.Skip();
148 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
150 void OnSize(wxSizeEvent
& event
);
153 void LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
);
154 void ShowContextMenu(const wxPoint
& pos
);
156 wxMenu
*CreateDummyMenu(wxString
*title
);
158 wxMenuItem
*GetLastMenuItem() const;
160 // the menu previously detached from the menubar (may be NULL)
163 // the count of dummy menus already created
167 // the control used for logging
168 wxTextCtrl
*m_textctrl
;
171 // the previous log target
174 DECLARE_EVENT_TABLE()
177 // A small helper class which intercepts all menu events and logs them
178 class MyEvtHandler
: public wxEvtHandler
181 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
183 void OnMenuEvent(wxCommandEvent
& event
)
185 m_frame
->LogMenuEvent(event
);
193 DECLARE_EVENT_TABLE()
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
202 Menu_File_Quit
= wxID_EXIT
,
207 Menu_MenuBar_Toggle
= 200,
212 Menu_MenuBar_GetLabel
,
214 Menu_MenuBar_SetLabel
,
215 Menu_MenuBar_FindMenu
,
218 Menu_Menu_Append
= 300,
233 Menu_Test_Normal
= 400,
246 Menu_Dummy_First
= 500,
252 Menu_Help_About
= wxID_ABOUT
,
254 Menu_Popup_ToBeDeleted
= 2000,
255 Menu_Popup_ToBeGreyed
,
256 Menu_Popup_ToBeChecked
,
262 // ----------------------------------------------------------------------------
264 // ----------------------------------------------------------------------------
266 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
267 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
269 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
272 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
274 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
275 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
276 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
277 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
278 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
279 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
281 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
282 EVT_MENU(Menu_MenuBar_FindMenu
, MyFrame::OnFindMenu
)
285 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
286 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
287 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
288 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
289 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
290 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
291 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
293 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
295 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
297 EVT_MENU(Menu_Menu_FindItem
, MyFrame::OnFindMenuItem
)
300 EVT_MENU(Menu_Test_Normal
, MyFrame::OnTestNormal
)
301 EVT_MENU(Menu_Test_Check
, MyFrame::OnTestCheck
)
302 EVT_MENU(Menu_Test_Radio1
, MyFrame::OnTestRadio
)
303 EVT_MENU(Menu_Test_Radio2
, MyFrame::OnTestRadio
)
304 EVT_MENU(Menu_Test_Radio3
, MyFrame::OnTestRadio
)
306 EVT_UPDATE_UI(Menu_SubMenu_Normal
, MyFrame::OnUpdateSubMenuNormal
)
307 EVT_UPDATE_UI(Menu_SubMenu_Check
, MyFrame::OnUpdateSubMenuCheck
)
308 EVT_UPDATE_UI(Menu_SubMenu_Radio1
, MyFrame::OnUpdateSubMenuRadio
)
309 EVT_UPDATE_UI(Menu_SubMenu_Radio2
, MyFrame::OnUpdateSubMenuRadio
)
310 EVT_UPDATE_UI(Menu_SubMenu_Radio3
, MyFrame::OnUpdateSubMenuRadio
)
312 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
314 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
317 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
319 EVT_RIGHT_UP(MyFrame::OnRightUp
)
322 EVT_MENU_OPEN(MyFrame::OnMenuOpen
)
323 EVT_MENU_CLOSE(MyFrame::OnMenuClose
)
325 EVT_SIZE(MyFrame::OnSize
)
328 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
329 EVT_MENU(wxID_ANY
, MyEvtHandler::OnMenuEvent
)
332 // ============================================================================
334 // ============================================================================
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
342 // The `main program' equivalent, creating the windows and returning the
346 // Create the main frame window
347 MyFrame
* frame
= new MyFrame
;
352 frame
->SetStatusText(_T("Welcome to wxWidgets menu sample"));
353 #endif // wxUSE_STATUSBAR
360 // ----------------------------------------------------------------------------
362 // ----------------------------------------------------------------------------
364 // Define my frame constructor
366 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, _T("wxWidgets menu sample"))
377 #endif // wxUSE_STATUSBAR
379 // create the menubar
380 wxMenu
*fileMenu
= new wxMenu
;
383 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
384 _T("Clear &log\tCtrl-L"));
385 #if wxUSE_OWNER_DRAWN
386 item
->SetBitmap(copy_xpm
);
388 fileMenu
->Append(item
);
389 fileMenu
->AppendSeparator();
391 fileMenu
->Append(Menu_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit menu sample"));
393 wxMenu
*menubarMenu
= new wxMenu
;
394 menubarMenu
->Append(Menu_MenuBar_Append
, _T("&Append menu\tCtrl-A"),
395 _T("Append a menu to the menubar"));
396 menubarMenu
->Append(Menu_MenuBar_Insert
, _T("&Insert menu\tCtrl-I"),
397 _T("Insert a menu into the menubar"));
398 menubarMenu
->Append(Menu_MenuBar_Delete
, _T("&Delete menu\tCtrl-D"),
399 _T("Delete the last menu from the menubar"));
400 menubarMenu
->Append(Menu_MenuBar_Toggle
, _T("&Toggle menu\tCtrl-T"),
401 _T("Toggle the first menu in the menubar"), true);
402 menubarMenu
->AppendSeparator();
403 menubarMenu
->Append(Menu_MenuBar_Enable
, _T("&Enable menu\tCtrl-E"),
404 _T("Enable or disable the last menu"), true);
405 menubarMenu
->AppendSeparator();
406 menubarMenu
->Append(Menu_MenuBar_GetLabel
, _T("&Get menu label\tCtrl-G"),
407 _T("Get the label of the last menu"));
409 menubarMenu
->Append(Menu_MenuBar_SetLabel
, _T("&Set menu label\tCtrl-S"),
410 _T("Change the label of the last menu"));
411 menubarMenu
->AppendSeparator();
412 menubarMenu
->Append(Menu_MenuBar_FindMenu
, _T("&Find menu from label\tCtrl-F"),
413 _T("Find a menu by searching for its label"));
416 wxMenu
* subMenu
= new wxMenu
;
417 subMenu
->Append(Menu_SubMenu_Normal
, _T("&Normal submenu item"), _T("Disabled submenu item"));
418 subMenu
->AppendCheckItem(Menu_SubMenu_Check
, _T("&Unchecked submenu item"), _T("Unchecked submenu item"));
419 subMenu
->AppendRadioItem(Menu_SubMenu_Radio1
, _T("&Radio item 1"), _T("Radio item"));
420 subMenu
->AppendRadioItem(Menu_SubMenu_Radio2
, _T("&Radio item 2"), _T("Radio item"));
421 subMenu
->AppendRadioItem(Menu_SubMenu_Radio3
, _T("&Radio item 3"), _T("Radio item"));
423 menubarMenu
->Append(Menu_SubMenu
, _T("Submenu"), subMenu
);
425 wxMenu
*menuMenu
= new wxMenu
;
426 menuMenu
->Append(Menu_Menu_Append
, _T("&Append menu item\tAlt-A"),
427 _T("Append a menu item to the last menu"));
428 menuMenu
->Append(Menu_Menu_AppendSub
, _T("&Append sub menu\tAlt-S"),
429 _T("Append a sub menu to the last menu"));
430 menuMenu
->Append(Menu_Menu_Insert
, _T("&Insert menu item\tAlt-I"),
431 _T("Insert a menu item in head of the last menu"));
432 menuMenu
->Append(Menu_Menu_Delete
, _T("&Delete menu item\tAlt-D"),
433 _T("Delete the last menu item from the last menu"));
434 menuMenu
->AppendSeparator();
435 menuMenu
->Append(Menu_Menu_Enable
, _T("&Enable menu item\tAlt-E"),
436 _T("Enable or disable the last menu item"), true);
437 menuMenu
->Append(Menu_Menu_Check
, _T("&Check menu item\tAlt-C"),
438 _T("Check or uncheck the last menu item"), true);
439 menuMenu
->AppendSeparator();
440 menuMenu
->Append(Menu_Menu_GetInfo
, _T("Get menu item in&fo\tAlt-F"),
441 _T("Show the state of the last menu item"));
443 menuMenu
->AppendSeparator();
444 menuMenu
->Append(Menu_Menu_FindItem
, _T("Find menu item from label"),
445 _T("Find a menu item by searching for its label"));
448 wxMenu
*testMenu
= new wxMenu
;
449 testMenu
->Append(Menu_Test_Normal
, _T("&Normal item"));
450 testMenu
->AppendSeparator();
451 testMenu
->AppendCheckItem(Menu_Test_Check
, _T("&Check item"));
452 testMenu
->AppendSeparator();
453 testMenu
->AppendRadioItem(Menu_Test_Radio1
, _T("Radio item &1"));
454 testMenu
->AppendRadioItem(Menu_Test_Radio2
, _T("Radio item &2"));
455 testMenu
->AppendRadioItem(Menu_Test_Radio3
, _T("Radio item &3"));
457 wxMenu
*helpMenu
= new wxMenu
;
458 helpMenu
->Append(Menu_Help_About
, _T("&About\tF1"), _T("About menu sample"));
460 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
462 menuBar
->Append(fileMenu
, _T("&File"));
463 menuBar
->Append(menubarMenu
, _T("Menu&bar"));
464 menuBar
->Append(menuMenu
, _T("&Menu"));
465 menuBar
->Append(testMenu
, _T("&Test"));
466 menuBar
->Append(helpMenu
, _T("&Help"));
468 // these items should be initially checked
469 menuBar
->Check(Menu_MenuBar_Toggle
, true);
470 menuBar
->Check(Menu_MenuBar_Enable
, true);
471 menuBar
->Check(Menu_Menu_Enable
, true);
472 menuBar
->Check(Menu_Menu_Check
, false);
474 // associate the menu bar with the frame
477 // intercept all menu events and log them in this custom event handler
478 PushEventHandler(new MyEvtHandler(this));
481 // create the log text window
482 m_textctrl
= new wxTextCtrl(this, wxID_ANY
, _T(""),
483 wxDefaultPosition
, wxDefaultSize
,
485 m_textctrl
->SetEditable(false);
487 wxLog::SetTimestamp(NULL
);
488 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
490 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
491 _T("append/insert/delete items to/from the last menu.\n")
492 _T("The commands from \"Menubar\" menu work with the ")
493 _T("menubar itself.\n\n")
494 _T("Right click the band below to test popup menus.\n"));
502 // delete the event handler installed in ctor
503 PopEventHandler(true);
506 // restore old logger
507 delete wxLog::SetActiveTarget(m_logOld
);
511 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
513 wxMenu
*menu
= new wxMenu
;
514 menu
->Append(Menu_Dummy_First
, _T("&First item\tCtrl-F1"));
515 menu
->AppendSeparator();
516 menu
->AppendCheckItem(Menu_Dummy_Second
, _T("&Second item\tCtrl-F2"));
520 title
->Printf(_T("Dummy menu &%u"), (unsigned)++m_countDummy
);
526 wxMenuItem
*MyFrame::GetLastMenuItem() const
528 wxMenuBar
*menubar
= GetMenuBar();
529 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
531 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
534 wxLogWarning(_T("No last item in the last menu!"));
540 return node
->GetData();
544 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
546 int id
= event
.GetId();
547 if ( !GetMenuBar()->FindItem(id
) )
550 wxString msg
= wxString::Format(_T("Menu command %d"), id
);
551 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
553 msg
+= wxString::Format(_T(" (the item is currently %schecked)"),
554 event
.IsChecked() ? _T("") : _T("not "));
560 // ----------------------------------------------------------------------------
562 // ----------------------------------------------------------------------------
564 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
570 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
576 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
578 (void)wxMessageBox(_T("wxWidgets menu sample\n© 1999-2001 Vadim Zeitlin"),
579 _T("About wxWidgets menu sample"),
583 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
585 wxMenuBar
*mbar
= GetMenuBar();
587 size_t count
= mbar
->GetMenuCount();
590 // don't let delete the first 2 menus
591 wxLogError(_T("Can't delete any more menus"));
595 delete mbar
->Remove(count
- 1);
599 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
602 wxMenu
*menu
= CreateDummyMenu(&title
);
603 GetMenuBar()->Insert(0, menu
, title
);
606 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
609 wxMenu
*menu
= CreateDummyMenu(&title
);
610 GetMenuBar()->Append(menu
, title
);
613 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
615 wxMenuBar
*mbar
= GetMenuBar();
619 m_menu
= mbar
->Remove(0);
624 mbar
->Insert(0, m_menu
, _T("&File"));
629 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
631 wxMenuBar
*mbar
= GetMenuBar();
632 size_t count
= mbar
->GetMenuCount();
634 mbar
->EnableTop(count
- 1, event
.IsChecked());
637 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
639 wxMenuBar
*mbar
= GetMenuBar();
640 size_t count
= mbar
->GetMenuCount();
642 wxCHECK_RET( count
, _T("no last menu?") );
644 wxLogMessage(_T("The label of the last menu item is '%s'"),
645 mbar
->GetLabelTop(count
- 1).c_str());
649 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
651 wxMenuBar
*mbar
= GetMenuBar();
652 size_t count
= mbar
->GetMenuCount();
654 wxCHECK_RET( count
, _T("no last menu?") );
656 wxString label
= wxGetTextFromUser
658 _T("Enter new label: "),
659 _T("Change last menu text"),
660 mbar
->GetLabelTop(count
- 1),
664 if ( !label
.empty() )
666 mbar
->SetLabelTop(count
- 1, label
);
670 void MyFrame::OnFindMenu(wxCommandEvent
& WXUNUSED(event
))
672 wxMenuBar
*mbar
= GetMenuBar();
673 size_t count
= mbar
->GetMenuCount();
675 wxCHECK_RET( count
, _T("no last menu?") );
677 wxString label
= wxGetTextFromUser
679 _T("Enter label to search for: "),
685 if ( !label
.empty() )
687 int index
= mbar
->FindMenu(label
);
689 if (index
== wxNOT_FOUND
)
691 wxLogWarning(_T("No menu with label '%s'"), label
.c_str());
695 wxLogMessage(_T("Menu %d has label '%s'"), index
, label
.c_str());
701 void MyFrame::OnDummy(wxCommandEvent
& event
)
703 wxLogMessage(_T("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
706 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
708 wxMenuBar
*menubar
= GetMenuBar();
709 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
711 menu
->AppendSeparator();
712 menu
->Append(Menu_Dummy_Third
, _T("&Third dummy item\tCtrl-F3"),
713 _T("Checkable item"), true);
716 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
718 wxMenuBar
*menubar
= GetMenuBar();
720 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 2);
722 menu
->Append(Menu_Dummy_Last
, _T("&Dummy sub menu"),
723 CreateDummyMenu(NULL
), _T("Dummy sub menu help"));
726 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
728 wxMenuBar
*menubar
= GetMenuBar();
729 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
731 size_t count
= menu
->GetMenuItemCount();
734 wxLogWarning(_T("No items to delete!"));
738 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
742 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
744 wxMenuBar
*menubar
= GetMenuBar();
745 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
747 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
748 _T("Fourth dummy item\tCtrl-F4")));
749 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, _T("")));
752 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
754 wxMenuItem
*item
= GetLastMenuItem();
758 item
->Enable(!item
->IsEnabled());
762 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
764 wxMenuItem
*item
= GetLastMenuItem();
769 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
773 wxMenuItem
*item
= GetLastMenuItem();
775 event
.Enable(item
&& item
->IsCheckable());
778 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
780 wxMenuItem
*item
= GetLastMenuItem();
784 wxLogMessage(_T("The label of the last menu item is '%s'"),
785 item
->GetLabel().c_str());
790 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
792 wxMenuItem
*item
= GetLastMenuItem();
796 wxString label
= wxGetTextFromUser
798 _T("Enter new label: "),
799 _T("Change last menu item text"),
804 if ( !label
.empty() )
806 item
->SetText(label
);
812 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
814 wxMenuItem
*item
= GetLastMenuItem();
819 msg
<< _T("The item is ") << (item
->IsEnabled() ? _T("enabled")
823 if ( item
->IsCheckable() )
825 msg
<< _T("It is checkable and ") << (item
->IsChecked() ? _T("") : _T("un"))
830 wxAcceleratorEntry
*accel
= item
->GetAccel();
833 msg
<< _T("Its accelerator is ");
835 int flags
= accel
->GetFlags();
836 if ( flags
& wxACCEL_ALT
)
838 if ( flags
& wxACCEL_CTRL
)
840 if ( flags
& wxACCEL_SHIFT
)
843 int code
= accel
->GetKeyCode();
858 msg
<< _T('F') << code
- WXK_F1
+ 1;
861 // if there are any other keys wxGetAccelFromString() may return,
862 // we should process them here
865 if ( wxIsalnum(code
) )
872 wxFAIL_MSG( _T("unknown keyboard accel") );
879 msg
<< _T("It doesn't have an accelerator");
881 #endif // wxUSE_ACCEL
888 void MyFrame::OnFindMenuItem(wxCommandEvent
& WXUNUSED(event
))
890 wxMenuBar
*mbar
= GetMenuBar();
891 size_t count
= mbar
->GetMenuCount();
893 wxCHECK_RET( count
, _T("no last menu?") );
895 wxString label
= wxGetTextFromUser
897 _T("Enter label to search for: "),
898 _T("Find menu item"),
903 if ( !label
.empty() )
906 int index
= wxNOT_FOUND
;
908 for (menuindex
= 0; (menuindex
< count
) && (index
== wxNOT_FOUND
); ++menuindex
)
910 index
= mbar
->FindMenuItem(mbar
->GetMenu(menuindex
)->GetTitle(), label
);
912 if (index
== wxNOT_FOUND
)
914 wxLogWarning(_T("No menu item with label '%s'"), label
.c_str());
918 wxLogMessage(_T("Menu item %d in menu %lu has label '%s'"),
919 index
, (unsigned long)menuindex
, label
.c_str());
925 void MyFrame::ShowContextMenu(const wxPoint
& pos
)
927 wxMenu
menu(_T("Test popup"));
929 menu
.Append(Menu_Help_About
, _T("&About"));
930 menu
.Append(Menu_Popup_Submenu
, _T("&Submenu"), CreateDummyMenu(NULL
));
931 menu
.Append(Menu_Popup_ToBeDeleted
, _T("To be &deleted"));
932 menu
.AppendCheckItem(Menu_Popup_ToBeChecked
, _T("To be &checked"));
933 menu
.Append(Menu_Popup_ToBeGreyed
, _T("To be &greyed"),
934 _T("This menu item should be initially greyed out"));
935 menu
.AppendSeparator();
936 menu
.Append(Menu_File_Quit
, _T("E&xit"));
938 menu
.Delete(Menu_Popup_ToBeDeleted
);
939 menu
.Check(Menu_Popup_ToBeChecked
, true);
940 menu
.Enable(Menu_Popup_ToBeGreyed
, false);
942 PopupMenu(&menu
, pos
.x
, pos
.y
);
944 // test for destroying items in popup menus
945 #if 0 // doesn't work in wxGTK!
946 menu
.Destroy(Menu_Popup_Submenu
);
948 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
952 void MyFrame::OnTestNormal(wxCommandEvent
& WXUNUSED(event
))
954 wxLogMessage(_T("Normal item selected"));
957 void MyFrame::OnTestCheck(wxCommandEvent
& event
)
959 wxLogMessage(_T("Check item %schecked"),
960 event
.IsChecked() ? _T("") : _T("un"));
963 void MyFrame::OnTestRadio(wxCommandEvent
& event
)
965 wxLogMessage(_T("Radio item %d selected"),
966 event
.GetId() - Menu_Test_Radio1
+ 1);
970 void MyFrame::LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
)
974 << ( event
.IsPopup() ? _T("popup ") : _T("") )
975 << _T("menu has been ")
979 wxLogStatus(this, msg
.c_str());
983 void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
)
988 void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
)
994 void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
)
996 int which
= (event
.GetId() - Menu_SubMenu_Radio1
+ 1);
1003 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
1009 // leave a band below for popup menu testing
1010 wxSize size
= GetClientSize();
1011 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
1014 // this is really ugly but we have to do it as we can't just call
1015 // event.Skip() because wxFrameBase would make the text control fill the
1016 // entire frame then
1017 #ifdef __WXUNIVERSAL__
1019 #endif // __WXUNIVERSAL__