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
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // Define a new application
55 class MyApp
: public wxApp
62 class MyFrame
: public wxFrame
69 void LogMenuEvent(const wxCommandEvent
& event
);
72 void OnQuit(wxCommandEvent
& event
);
73 void OnClearLog(wxCommandEvent
& event
);
75 void OnAbout(wxCommandEvent
& event
);
77 void OnDummy(wxCommandEvent
& event
);
79 void OnAppendMenuItem(wxCommandEvent
& event
);
80 void OnAppendSubMenu(wxCommandEvent
& event
);
81 void OnDeleteMenuItem(wxCommandEvent
& event
);
82 void OnInsertMenuItem(wxCommandEvent
& event
);
83 void OnCheckMenuItem(wxCommandEvent
& event
);
84 void OnEnableMenuItem(wxCommandEvent
& event
);
85 void OnGetLabelMenuItem(wxCommandEvent
& event
);
86 void OnSetLabelMenuItem(wxCommandEvent
& event
);
87 void OnGetMenuItemInfo(wxCommandEvent
& event
);
88 void OnFindMenuItem(wxCommandEvent
& event
);
90 void OnAppendMenu(wxCommandEvent
& event
);
91 void OnInsertMenu(wxCommandEvent
& event
);
92 void OnDeleteMenu(wxCommandEvent
& event
);
93 void OnToggleMenu(wxCommandEvent
& event
);
94 void OnEnableMenu(wxCommandEvent
& event
);
95 void OnGetLabelMenu(wxCommandEvent
& event
);
96 void OnSetLabelMenu(wxCommandEvent
& event
);
97 void OnFindMenu(wxCommandEvent
& event
);
99 void OnTestNormal(wxCommandEvent
& event
);
100 void OnTestCheck(wxCommandEvent
& event
);
101 void OnTestRadio(wxCommandEvent
& event
);
103 void OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
);
104 void OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
);
105 void OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
);
108 void OnContextMenu(wxContextMenuEvent
& event
)
109 { ShowContextMenu(ScreenToClient(event
.GetPosition())); }
111 void OnRightUp(wxMouseEvent
& event
)
112 { ShowContextMenu(event
.GetPosition()); }
115 void OnMenuOpen(wxMenuEvent
& event
)
116 { LogMenuOpenOrClose(event
, _T("opened")); event
.Skip(); }
117 void OnMenuClose(wxMenuEvent
& event
)
118 { LogMenuOpenOrClose(event
, _T("closed")); event
.Skip(); }
120 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
122 void OnSize(wxSizeEvent
& event
);
125 void LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
);
126 void ShowContextMenu(const wxPoint
& pos
);
128 wxMenu
*CreateDummyMenu(wxString
*title
);
130 wxMenuItem
*GetLastMenuItem() const;
132 // the menu previously detached from the menubar (may be NULL)
135 // the count of dummy menus already created
138 // the control used for logging
139 wxTextCtrl
*m_textctrl
;
141 // the previous log target
144 DECLARE_EVENT_TABLE()
147 // A small helper class which intercepts all menu events and logs them
148 class MyEvtHandler
: public wxEvtHandler
151 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
153 void OnMenuEvent(wxCommandEvent
& event
)
155 m_frame
->LogMenuEvent(event
);
163 DECLARE_EVENT_TABLE()
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
172 Menu_File_Quit
= 100,
175 Menu_MenuBar_Toggle
= 200,
180 Menu_MenuBar_GetLabel
,
181 Menu_MenuBar_SetLabel
,
182 Menu_MenuBar_FindMenu
,
184 Menu_Menu_Append
= 300,
195 Menu_Test_Normal
= 400,
208 Menu_Dummy_First
= 500,
214 Menu_Help_About
= 1000,
216 Menu_Popup_ToBeDeleted
= 2000,
217 Menu_Popup_ToBeGreyed
,
218 Menu_Popup_ToBeChecked
,
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
228 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
229 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
230 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
232 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
234 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
235 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
236 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
237 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
238 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
239 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
240 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
241 EVT_MENU(Menu_MenuBar_FindMenu
, MyFrame::OnFindMenu
)
243 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
244 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
245 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
246 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
247 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
248 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
249 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
250 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
251 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
252 EVT_MENU(Menu_Menu_FindItem
, MyFrame::OnFindMenuItem
)
254 EVT_MENU(Menu_Test_Normal
, MyFrame::OnTestNormal
)
255 EVT_MENU(Menu_Test_Check
, MyFrame::OnTestCheck
)
256 EVT_MENU(Menu_Test_Radio1
, MyFrame::OnTestRadio
)
257 EVT_MENU(Menu_Test_Radio2
, MyFrame::OnTestRadio
)
258 EVT_MENU(Menu_Test_Radio3
, MyFrame::OnTestRadio
)
260 EVT_UPDATE_UI(Menu_SubMenu_Normal
, MyFrame::OnUpdateSubMenuNormal
)
261 EVT_UPDATE_UI(Menu_SubMenu_Check
, MyFrame::OnUpdateSubMenuCheck
)
262 EVT_UPDATE_UI(Menu_SubMenu_Radio1
, MyFrame::OnUpdateSubMenuRadio
)
263 EVT_UPDATE_UI(Menu_SubMenu_Radio2
, MyFrame::OnUpdateSubMenuRadio
)
264 EVT_UPDATE_UI(Menu_SubMenu_Radio3
, MyFrame::OnUpdateSubMenuRadio
)
266 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
268 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
271 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
273 EVT_RIGHT_UP(MyFrame::OnRightUp
)
276 EVT_MENU_OPEN(MyFrame::OnMenuOpen
)
277 EVT_MENU_CLOSE(MyFrame::OnMenuClose
)
279 EVT_SIZE(MyFrame::OnSize
)
282 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
283 EVT_MENU(-1, MyEvtHandler::OnMenuEvent
)
286 // ============================================================================
288 // ============================================================================
290 // ----------------------------------------------------------------------------
292 // ----------------------------------------------------------------------------
296 // The `main program' equivalent, creating the windows and returning the
300 // Create the main frame window
301 MyFrame
* frame
= new MyFrame
;
306 frame
->SetStatusText(_T("Welcome to wxWindows menu sample"));
307 #endif // wxUSE_STATUSBAR
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
318 // Define my frame constructor
320 : wxFrame((wxFrame
*)NULL
, -1, _T("wxWindows menu sample"),
321 wxDefaultPosition
, wxSize(400, 250))
330 #endif // wxUSE_STATUSBAR
332 // create the menubar
333 wxMenu
*fileMenu
= new wxMenu
;
335 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
336 _T("Clear &log\tCtrl-L"));
337 item
->SetBitmap(copy_xpm
);
338 fileMenu
->Append(item
);
339 fileMenu
->AppendSeparator();
340 fileMenu
->Append(Menu_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit menu sample"));
342 wxMenu
*menubarMenu
= new wxMenu
;
343 menubarMenu
->Append(Menu_MenuBar_Append
, _T("&Append menu\tCtrl-A"),
344 _T("Append a menu to the menubar"));
345 menubarMenu
->Append(Menu_MenuBar_Insert
, _T("&Insert menu\tCtrl-I"),
346 _T("Insert a menu into the menubar"));
347 menubarMenu
->Append(Menu_MenuBar_Delete
, _T("&Delete menu\tCtrl-D"),
348 _T("Delete the last menu from the menubar"));
349 menubarMenu
->Append(Menu_MenuBar_Toggle
, _T("&Toggle menu\tCtrl-T"),
350 _T("Toggle the first menu in the menubar"), true);
351 menubarMenu
->AppendSeparator();
352 menubarMenu
->Append(Menu_MenuBar_Enable
, _T("&Enable menu\tCtrl-E"),
353 _T("Enable or disable the last menu"), true);
354 menubarMenu
->AppendSeparator();
355 menubarMenu
->Append(Menu_MenuBar_GetLabel
, _T("&Get menu label\tCtrl-G"),
356 _T("Get the label of the last menu"));
357 menubarMenu
->Append(Menu_MenuBar_SetLabel
, _T("&Set menu label\tCtrl-S"),
358 _T("Change the label of the last menu"));
359 menubarMenu
->AppendSeparator();
360 menubarMenu
->Append(Menu_MenuBar_FindMenu
, _T("&Find menu from label\tCtrl-F"),
361 _T("Find a menu by searching for its label"));
363 wxMenu
* subMenu
= new wxMenu
;
364 subMenu
->Append(Menu_SubMenu_Normal
, _T("&Normal submenu item"), _T("Disabled submenu item"));
365 subMenu
->AppendCheckItem(Menu_SubMenu_Check
, _T("&Unchecked submenu item"), _T("Unchecked submenu item"));
366 subMenu
->AppendRadioItem(Menu_SubMenu_Radio1
, _T("&Radio item 1"), _T("Radio item"));
367 subMenu
->AppendRadioItem(Menu_SubMenu_Radio2
, _T("&Radio item 2"), _T("Radio item"));
368 subMenu
->AppendRadioItem(Menu_SubMenu_Radio3
, _T("&Radio item 3"), _T("Radio item"));
370 menubarMenu
->Append(Menu_SubMenu
, _T("Submenu"), subMenu
);
372 wxMenu
*menuMenu
= new wxMenu
;
373 menuMenu
->Append(Menu_Menu_Append
, _T("&Append menu item\tAlt-A"),
374 _T("Append a menu item to the last menu"));
375 menuMenu
->Append(Menu_Menu_AppendSub
, _T("&Append sub menu\tAlt-S"),
376 _T("Append a sub menu to the last menu"));
377 menuMenu
->Append(Menu_Menu_Insert
, _T("&Insert menu item\tAlt-I"),
378 _T("Insert a menu item in head of the last menu"));
379 menuMenu
->Append(Menu_Menu_Delete
, _T("&Delete menu item\tAlt-D"),
380 _T("Delete the last menu item from the last menu"));
381 menuMenu
->AppendSeparator();
382 menuMenu
->Append(Menu_Menu_Enable
, _T("&Enable menu item\tAlt-E"),
383 _T("Enable or disable the last menu item"), true);
384 menuMenu
->Append(Menu_Menu_Check
, _T("&Check menu item\tAlt-C"),
385 _T("Check or uncheck the last menu item"), true);
386 menuMenu
->AppendSeparator();
387 menuMenu
->Append(Menu_Menu_GetInfo
, _T("Get menu item in&fo\tAlt-F"),
388 _T("Show the state of the last menu item"));
389 menuMenu
->AppendSeparator();
390 menuMenu
->Append(Menu_Menu_FindItem
, _T("Find menu item from label"),
391 _T("Find a menu item by searching for its label"));
393 wxMenu
*testMenu
= new wxMenu
;
394 testMenu
->Append(Menu_Test_Normal
, _T("&Normal item"));
395 testMenu
->AppendSeparator();
396 testMenu
->AppendCheckItem(Menu_Test_Check
, _T("&Check item"));
397 testMenu
->AppendSeparator();
398 testMenu
->AppendRadioItem(Menu_Test_Radio1
, _T("Radio item &1"));
399 testMenu
->AppendRadioItem(Menu_Test_Radio2
, _T("Radio item &2"));
400 testMenu
->AppendRadioItem(Menu_Test_Radio3
, _T("Radio item &3"));
402 wxMenu
*helpMenu
= new wxMenu
;
403 helpMenu
->Append(Menu_Help_About
, _T("&About\tF1"), _T("About menu sample"));
405 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
407 menuBar
->Append(fileMenu
, _T("&File"));
408 menuBar
->Append(menubarMenu
, _T("Menu&bar"));
409 menuBar
->Append(menuMenu
, _T("&Menu"));
410 menuBar
->Append(testMenu
, _T("&Test"));
411 menuBar
->Append(helpMenu
, _T("&Help"));
413 // these items should be initially checked
414 menuBar
->Check(Menu_MenuBar_Toggle
, true);
415 menuBar
->Check(Menu_MenuBar_Enable
, true);
416 menuBar
->Check(Menu_Menu_Enable
, true);
417 menuBar
->Check(Menu_Menu_Check
, false);
419 // associate the menu bar with the frame
422 // intercept all menu events and log them in this custom event handler
423 PushEventHandler(new MyEvtHandler(this));
425 // create the log text window
426 m_textctrl
= new wxTextCtrl(this, -1, _T(""),
427 wxDefaultPosition
, wxDefaultSize
,
429 m_textctrl
->SetEditable(false);
431 wxLog::SetTimestamp(NULL
);
432 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
434 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
435 _T("append/insert/delete items to/from the last menu.\n")
436 _T("The commands from \"Menubar\" menu work with the ")
437 _T("menubar itself.\n\n")
438 _T("Right click the band below to test popup menus.\n"));
445 // delete the event handler installed in ctor
446 PopEventHandler(true);
448 // restore old logger
449 delete wxLog::SetActiveTarget(m_logOld
);
452 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
454 wxMenu
*menu
= new wxMenu
;
455 menu
->Append(Menu_Dummy_First
, _T("&First item\tCtrl-F1"));
456 menu
->AppendSeparator();
457 menu
->Append(Menu_Dummy_Second
, _T("&Second item\tCtrl-F2"), _T(""), true);
461 title
->Printf(_T("Dummy menu &%u"), (unsigned)++m_countDummy
);
467 wxMenuItem
*MyFrame::GetLastMenuItem() const
469 wxMenuBar
*menubar
= GetMenuBar();
470 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
472 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
475 wxLogWarning(_T("No last item in the last menu!"));
481 return node
->GetData();
485 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
487 int id
= event
.GetId();
488 if ( !GetMenuBar()->FindItem(id
) )
491 wxString msg
= wxString::Format(_T("Menu command %d"), id
);
492 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
494 msg
+= wxString::Format(_T(" (the item is currently %schecked)"),
495 event
.IsChecked() ? _T("") : _T("not "));
501 // ----------------------------------------------------------------------------
503 // ----------------------------------------------------------------------------
505 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
510 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
515 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
517 (void)wxMessageBox(_T("wxWindows menu sample\n© 1999-2001 Vadim Zeitlin"),
518 _T("About wxWindows menu sample"),
522 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
524 wxMenuBar
*mbar
= GetMenuBar();
526 size_t count
= mbar
->GetMenuCount();
529 // don't let delete the first 2 menus
530 wxLogError(_T("Can't delete any more menus"));
534 delete mbar
->Remove(count
- 1);
538 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
541 wxMenu
*menu
= CreateDummyMenu(&title
);
542 GetMenuBar()->Insert(0, menu
, title
);
545 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
548 wxMenu
*menu
= CreateDummyMenu(&title
);
549 GetMenuBar()->Append(menu
, title
);
552 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
554 wxMenuBar
*mbar
= GetMenuBar();
558 m_menu
= mbar
->Remove(0);
563 mbar
->Insert(0, m_menu
, _T("&File"));
568 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
570 wxMenuBar
*mbar
= GetMenuBar();
571 size_t count
= mbar
->GetMenuCount();
573 mbar
->EnableTop(count
- 1, event
.IsChecked());
576 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
578 wxMenuBar
*mbar
= GetMenuBar();
579 size_t count
= mbar
->GetMenuCount();
581 wxCHECK_RET( count
, _T("no last menu?") );
583 wxLogMessage(_T("The label of the last menu item is '%s'"),
584 mbar
->GetLabelTop(count
- 1).c_str());
587 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
589 wxMenuBar
*mbar
= GetMenuBar();
590 size_t count
= mbar
->GetMenuCount();
592 wxCHECK_RET( count
, _T("no last menu?") );
594 wxString label
= wxGetTextFromUser
596 _T("Enter new label: "),
597 _T("Change last menu text"),
598 mbar
->GetLabelTop(count
- 1),
602 if ( !label
.empty() )
604 mbar
->SetLabelTop(count
- 1, label
);
608 void MyFrame::OnFindMenu(wxCommandEvent
& WXUNUSED(event
))
610 wxMenuBar
*mbar
= GetMenuBar();
611 size_t count
= mbar
->GetMenuCount();
613 wxCHECK_RET( count
, _T("no last menu?") );
615 wxString label
= wxGetTextFromUser
617 _T("Enter label to search for: "),
623 if ( !label
.empty() )
625 int index
= mbar
->FindMenu(label
);
627 if (index
== wxNOT_FOUND
)
629 wxLogWarning(_T("No menu with label '%s'"), label
.c_str());
633 wxLogMessage(_T("Menu %d has label '%s'"), index
, label
.c_str());
638 void MyFrame::OnDummy(wxCommandEvent
& event
)
640 wxLogMessage(_T("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
643 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
645 wxMenuBar
*menubar
= GetMenuBar();
646 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
648 menu
->AppendSeparator();
649 menu
->Append(Menu_Dummy_Third
, _T("&Third dummy item\tCtrl-F3"),
650 _T("Checkable item"), true);
653 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
655 wxMenuBar
*menubar
= GetMenuBar();
657 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 2);
659 menu
->Append(Menu_Dummy_Last
, _T("&Dummy sub menu"),
660 CreateDummyMenu(NULL
), _T("Dummy sub menu help"));
663 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
665 wxMenuBar
*menubar
= GetMenuBar();
666 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
668 size_t count
= menu
->GetMenuItemCount();
671 wxLogWarning(_T("No items to delete!"));
675 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
679 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
681 wxMenuBar
*menubar
= GetMenuBar();
682 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
684 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
685 _T("Fourth dummy item\tCtrl-F4")));
686 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, _T("")));
689 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
691 wxMenuItem
*item
= GetLastMenuItem();
695 item
->Enable(!item
->IsEnabled());
699 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
701 wxMenuItem
*item
= GetLastMenuItem();
706 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
710 wxMenuItem
*item
= GetLastMenuItem();
712 event
.Enable(item
&& item
->IsCheckable());
715 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
717 wxMenuItem
*item
= GetLastMenuItem();
721 wxLogMessage(_T("The label of the last menu item is '%s'"),
722 item
->GetLabel().c_str());
726 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
728 wxMenuItem
*item
= GetLastMenuItem();
732 wxString label
= wxGetTextFromUser
734 _T("Enter new label: "),
735 _T("Change last menu item text"),
740 if ( !label
.empty() )
742 item
->SetText(label
);
747 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
749 wxMenuItem
*item
= GetLastMenuItem();
754 msg
<< _T("The item is ") << (item
->IsEnabled() ? _T("enabled")
758 if ( item
->IsCheckable() )
760 msg
<< _T("It is checkable and ") << (item
->IsChecked() ? _T("") : _T("un"))
765 wxAcceleratorEntry
*accel
= item
->GetAccel();
768 msg
<< _T("Its accelerator is ");
770 int flags
= accel
->GetFlags();
771 if ( flags
& wxACCEL_ALT
)
773 if ( flags
& wxACCEL_CTRL
)
775 if ( flags
& wxACCEL_SHIFT
)
778 int code
= accel
->GetKeyCode();
793 msg
<< _T('F') << code
- WXK_F1
+ 1;
796 // if there are any other keys wxGetAccelFromString() may return,
797 // we should process them here
800 if ( wxIsalnum(code
) )
807 wxFAIL_MSG( _T("unknown keyboard accel") );
814 msg
<< _T("It doesn't have an accelerator");
816 #endif // wxUSE_ACCEL
822 void MyFrame::OnFindMenuItem(wxCommandEvent
& WXUNUSED(event
))
824 wxMenuBar
*mbar
= GetMenuBar();
825 size_t count
= mbar
->GetMenuCount();
827 wxCHECK_RET( count
, _T("no last menu?") );
829 wxString label
= wxGetTextFromUser
831 _T("Enter label to search for: "),
832 _T("Find menu item"),
837 if ( !label
.empty() )
840 int index
= wxNOT_FOUND
;
842 for (menuindex
= 0; (menuindex
< count
) && (index
== wxNOT_FOUND
); ++menuindex
)
844 index
= mbar
->FindMenuItem(mbar
->GetMenu(menuindex
)->GetTitle(), label
);
846 if (index
== wxNOT_FOUND
)
848 wxLogWarning(_T("No menu item with label '%s'"), label
.c_str());
852 wxLogMessage(_T("Menu item %d in menu %lu has label '%s'"),
853 index
, (unsigned long)menuindex
, label
.c_str());
858 void MyFrame::ShowContextMenu(const wxPoint
& pos
)
860 wxMenu
menu(_T("Test popup"));
862 menu
.Append(Menu_Help_About
, _T("&About"));
863 menu
.Append(Menu_Popup_Submenu
, _T("&Submenu"), CreateDummyMenu(NULL
));
864 menu
.Append(Menu_Popup_ToBeDeleted
, _T("To be &deleted"));
865 menu
.Append(Menu_Popup_ToBeChecked
, _T("To be &checked"), _T(""), true);
866 menu
.Append(Menu_Popup_ToBeGreyed
, _T("To be &greyed"),
867 _T("This menu item should be initially greyed out"));
868 menu
.AppendSeparator();
869 menu
.Append(Menu_File_Quit
, _T("E&xit"));
871 menu
.Delete(Menu_Popup_ToBeDeleted
);
872 menu
.Check(Menu_Popup_ToBeChecked
, true);
873 menu
.Enable(Menu_Popup_ToBeGreyed
, false);
875 PopupMenu(&menu
, pos
.x
, pos
.y
);
877 // test for destroying items in popup menus
878 #if 0 // doesn't work in wxGTK!
879 menu
.Destroy(Menu_Popup_Submenu
);
881 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
885 void MyFrame::OnTestNormal(wxCommandEvent
& WXUNUSED(event
))
887 wxLogMessage(_T("Normal item selected"));
890 void MyFrame::OnTestCheck(wxCommandEvent
& event
)
892 wxLogMessage(_T("Check item %schecked"),
893 event
.IsChecked() ? _T("") : _T("un"));
896 void MyFrame::OnTestRadio(wxCommandEvent
& event
)
898 wxLogMessage(_T("Radio item %d selected"),
899 event
.GetId() - Menu_Test_Radio1
+ 1);
902 void MyFrame::LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
)
906 << ( event
.IsPopup() ? _T("popup ") : _T("") )
907 << _T("menu has been ")
911 wxLogStatus(this, msg
.c_str());
914 void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
)
919 void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
)
925 void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
)
927 int which
= (event
.GetId() - Menu_SubMenu_Radio1
+ 1);
934 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
939 // leave a band below for popup menu testing
940 wxSize size
= GetClientSize();
941 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
943 // this is really ugly but we have to do it as we can't just call
944 // event.Skip() because wxFrameBase would make the text control fill the
946 #ifdef __WXUNIVERSAL__
948 #endif // __WXUNIVERSAL__