]>
git.saurik.com Git - wxWidgets.git/blob - samples/menu/menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/menu.cpp
3 // Purpose: wxMenu/wxMenuBar sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
31 #include "wx/msgdlg.h"
33 #include "wx/textctrl.h"
34 #include "wx/textdlg.h"
39 #error "menu sample requires wxUSE_MENUS=1"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // Define a new application
49 class MyApp
: public wxApp
56 class MyFrame
: public wxFrame
63 void LogMenuEvent(const wxCommandEvent
& event
);
66 void OnQuit(wxCommandEvent
& event
);
67 void OnClearLog(wxCommandEvent
& event
);
69 void OnAbout(wxCommandEvent
& event
);
71 void OnDummy(wxCommandEvent
& event
);
73 void OnAppendMenuItem(wxCommandEvent
& event
);
74 void OnAppendSubMenu(wxCommandEvent
& event
);
75 void OnDeleteMenuItem(wxCommandEvent
& event
);
76 void OnInsertMenuItem(wxCommandEvent
& event
);
77 void OnCheckMenuItem(wxCommandEvent
& event
);
78 void OnEnableMenuItem(wxCommandEvent
& event
);
79 void OnGetLabelMenuItem(wxCommandEvent
& event
);
80 void OnSetLabelMenuItem(wxCommandEvent
& event
);
81 void OnGetMenuItemInfo(wxCommandEvent
& event
);
82 void OnFindMenuItem(wxCommandEvent
& event
);
84 void OnAppendMenu(wxCommandEvent
& event
);
85 void OnInsertMenu(wxCommandEvent
& event
);
86 void OnDeleteMenu(wxCommandEvent
& event
);
87 void OnToggleMenu(wxCommandEvent
& event
);
88 void OnEnableMenu(wxCommandEvent
& event
);
89 void OnGetLabelMenu(wxCommandEvent
& event
);
90 void OnSetLabelMenu(wxCommandEvent
& event
);
91 void OnFindMenu(wxCommandEvent
& event
);
93 void OnTestNormal(wxCommandEvent
& event
);
94 void OnTestCheck(wxCommandEvent
& event
);
95 void OnTestRadio(wxCommandEvent
& event
);
97 #if defined( __WXMSW__ ) || defined( __WXMAC__ )
98 void OnContextMenu(wxContextMenuEvent
& event
)
99 { ShowContextMenu(ScreenToClient(event
.GetPosition())); }
101 void OnRightUp(wxMouseEvent
& event
)
102 { ShowContextMenu(event
.GetPosition()); }
105 void OnMenuOpen(wxMenuEvent
& event
)
106 { LogMenuOpenOrClose(event
, _T("opened")); }
107 void OnMenuClose(wxMenuEvent
& event
)
108 { LogMenuOpenOrClose(event
, _T("closed")); }
110 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
112 void OnSize(wxSizeEvent
& event
);
115 void LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
);
116 void ShowContextMenu(const wxPoint
& pos
);
118 wxMenu
*CreateDummyMenu(wxString
*title
);
120 wxMenuItem
*GetLastMenuItem() const;
122 // the menu previously detached from the menubar (may be NULL)
125 // the count of dummy menus already created
128 // the control used for logging
129 wxTextCtrl
*m_textctrl
;
131 // the previous log target
134 DECLARE_EVENT_TABLE()
137 // A small helper class which intercepts all menu events and logs them
138 class MyEvtHandler
: public wxEvtHandler
141 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
143 void OnMenuEvent(wxCommandEvent
& event
)
145 m_frame
->LogMenuEvent(event
);
153 DECLARE_EVENT_TABLE()
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
162 Menu_File_Quit
= 100,
165 Menu_MenuBar_Toggle
= 200,
170 Menu_MenuBar_GetLabel
,
171 Menu_MenuBar_SetLabel
,
172 Menu_MenuBar_FindMenu
,
174 Menu_Menu_Append
= 300,
185 Menu_Test_Normal
= 400,
191 Menu_Dummy_First
= 500,
197 Menu_Help_About
= 1000,
199 Menu_Popup_ToBeDeleted
= 2000,
200 Menu_Popup_ToBeGreyed
,
201 Menu_Popup_ToBeChecked
,
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
212 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
213 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
215 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
217 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
218 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
219 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
220 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
221 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
222 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
223 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
224 EVT_MENU(Menu_MenuBar_FindMenu
, MyFrame::OnFindMenu
)
226 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
227 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
228 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
229 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
230 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
231 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
232 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
233 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
234 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
235 EVT_MENU(Menu_Menu_FindItem
, MyFrame::OnFindMenuItem
)
237 EVT_MENU(Menu_Test_Normal
, MyFrame::OnTestNormal
)
238 EVT_MENU(Menu_Test_Check
, MyFrame::OnTestCheck
)
239 EVT_MENU(Menu_Test_Radio1
, MyFrame::OnTestRadio
)
240 EVT_MENU(Menu_Test_Radio2
, MyFrame::OnTestRadio
)
241 EVT_MENU(Menu_Test_Radio3
, MyFrame::OnTestRadio
)
243 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
245 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
247 #if defined( __WXMSW__ ) || defined( __WXMAC__ )
248 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
250 EVT_RIGHT_UP(MyFrame::OnRightUp
)
253 EVT_MENU_OPEN(MyFrame::OnMenuOpen
)
254 EVT_MENU_CLOSE(MyFrame::OnMenuClose
)
256 EVT_SIZE(MyFrame::OnSize
)
259 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
260 EVT_MENU(-1, MyEvtHandler::OnMenuEvent
)
263 // ============================================================================
265 // ============================================================================
267 // ----------------------------------------------------------------------------
269 // ----------------------------------------------------------------------------
273 // The `main program' equivalent, creating the windows and returning the
277 // Create the main frame window
278 MyFrame
* frame
= new MyFrame
;
283 frame
->SetStatusText(_T("Welcome to wxWindows menu sample"));
284 #endif // wxUSE_STATUSBAR
291 // ----------------------------------------------------------------------------
293 // ----------------------------------------------------------------------------
295 // Define my frame constructor
297 : wxFrame((wxFrame
*)NULL
, -1, _T("wxWindows menu sample"),
298 wxDefaultPosition
, wxSize(400, 250))
307 #endif // wxUSE_STATUSBAR
309 // create the menubar
310 wxMenu
*fileMenu
= new wxMenu
;
312 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
313 _T("Clear &log\tCtrl-L"));
314 item
->SetBitmap(copy_xpm
);
315 fileMenu
->Append(item
);
316 fileMenu
->AppendSeparator();
317 fileMenu
->Append(Menu_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit menu sample"));
319 wxMenu
*menubarMenu
= new wxMenu
;
320 menubarMenu
->Append(Menu_MenuBar_Append
, _T("&Append menu\tCtrl-A"),
321 _T("Append a menu to the menubar"));
322 menubarMenu
->Append(Menu_MenuBar_Insert
, _T("&Insert menu\tCtrl-I"),
323 _T("Insert a menu into the menubar"));
324 menubarMenu
->Append(Menu_MenuBar_Delete
, _T("&Delete menu\tCtrl-D"),
325 _T("Delete the last menu from the menubar"));
326 menubarMenu
->Append(Menu_MenuBar_Toggle
, _T("&Toggle menu\tCtrl-T"),
327 _T("Toggle the first menu in the menubar"), TRUE
);
328 menubarMenu
->AppendSeparator();
329 menubarMenu
->Append(Menu_MenuBar_Enable
, _T("&Enable menu\tCtrl-E"),
330 _T("Enable or disable the last menu"), TRUE
);
331 menubarMenu
->AppendSeparator();
332 menubarMenu
->Append(Menu_MenuBar_GetLabel
, _T("&Get menu label\tCtrl-G"),
333 _T("Get the label of the last menu"));
334 menubarMenu
->Append(Menu_MenuBar_SetLabel
, _T("&Set menu label\tCtrl-S"),
335 _T("Change the label of the last menu"));
336 menubarMenu
->AppendSeparator();
337 menubarMenu
->Append(Menu_MenuBar_FindMenu
, _T("&Find menu from label\tCtrl-F"),
338 _T("Find a menu by searching for its label"));
340 wxMenu
*menuMenu
= new wxMenu
;
341 menuMenu
->Append(Menu_Menu_Append
, _T("&Append menu item\tAlt-A"),
342 _T("Append a menu item to the last menu"));
343 menuMenu
->Append(Menu_Menu_AppendSub
, _T("&Append sub menu\tAlt-S"),
344 _T("Append a sub menu to the last menu"));
345 menuMenu
->Append(Menu_Menu_Insert
, _T("&Insert menu item\tAlt-I"),
346 _T("Insert a menu item in head of the last menu"));
347 menuMenu
->Append(Menu_Menu_Delete
, _T("&Delete menu item\tAlt-D"),
348 _T("Delete the last menu item from the last menu"));
349 menuMenu
->AppendSeparator();
350 menuMenu
->Append(Menu_Menu_Enable
, _T("&Enable menu item\tAlt-E"),
351 _T("Enable or disable the last menu item"), TRUE
);
352 menuMenu
->Append(Menu_Menu_Check
, _T("&Check menu item\tAlt-C"),
353 _T("Check or uncheck the last menu item"), TRUE
);
354 menuMenu
->AppendSeparator();
355 menuMenu
->Append(Menu_Menu_GetInfo
, _T("Get menu item in&fo\tAlt-F"),
356 _T("Show the state of the last menu item"));
357 menuMenu
->AppendSeparator();
358 menuMenu
->Append(Menu_Menu_FindItem
, _T("Find menu item from label"),
359 _T("Find a menu item by searching for its label"));
361 wxMenu
*testMenu
= new wxMenu
;
362 testMenu
->Append(Menu_Test_Normal
, _T("&Normal item"));
363 testMenu
->AppendSeparator();
364 testMenu
->AppendCheckItem(Menu_Test_Check
, _T("&Check item"));
365 testMenu
->AppendSeparator();
366 testMenu
->AppendRadioItem(Menu_Test_Radio1
, _T("Radio item &1"));
367 testMenu
->AppendRadioItem(Menu_Test_Radio2
, _T("Radio item &2"));
368 testMenu
->AppendRadioItem(Menu_Test_Radio3
, _T("Radio item &3"));
370 wxMenu
*helpMenu
= new wxMenu
;
371 helpMenu
->Append(Menu_Help_About
, _T("&About\tF1"), _T("About menu sample"));
373 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
375 menuBar
->Append(fileMenu
, _T("&File"));
376 menuBar
->Append(menubarMenu
, _T("Menu&bar"));
377 menuBar
->Append(menuMenu
, _T("&Menu"));
378 menuBar
->Append(testMenu
, _T("&Test"));
379 menuBar
->Append(helpMenu
, _T("&Help"));
381 // these items should be initially checked
382 menuBar
->Check(Menu_MenuBar_Toggle
, TRUE
);
383 menuBar
->Check(Menu_MenuBar_Enable
, TRUE
);
384 menuBar
->Check(Menu_Menu_Enable
, TRUE
);
385 menuBar
->Check(Menu_Menu_Check
, FALSE
);
387 // associate the menu bar with the frame
390 // intercept all menu events and log them in this custom event handler
391 PushEventHandler(new MyEvtHandler(this));
393 // create the log text window
394 m_textctrl
= new wxTextCtrl(this, -1, _T(""),
395 wxDefaultPosition
, wxDefaultSize
,
397 m_textctrl
->SetEditable(FALSE
);
399 wxLog::SetTimestamp(NULL
);
400 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
402 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
403 _T("append/insert/delete items to/from the last menu.\n")
404 _T("The commands from \"Menubar\" menu work with the ")
405 _T("menubar itself.\n\n")
406 _T("Right click the band below to test popup menus.\n"));
413 // delete the event handler installed in ctor
414 PopEventHandler(TRUE
);
416 // restore old logger
417 delete wxLog::SetActiveTarget(m_logOld
);
420 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
422 wxMenu
*menu
= new wxMenu
;
423 menu
->Append(Menu_Dummy_First
, _T("&First item\tCtrl-F1"));
424 menu
->AppendSeparator();
425 menu
->Append(Menu_Dummy_Second
, _T("&Second item\tCtrl-F2"), _T(""), TRUE
);
429 title
->Printf(wxT("Dummy menu &%u"), (unsigned)++m_countDummy
);
435 wxMenuItem
*MyFrame::GetLastMenuItem() const
437 wxMenuBar
*menubar
= GetMenuBar();
438 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
440 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
443 wxLogWarning(wxT("No last item in the last menu!"));
449 return node
->GetData();
453 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
455 int id
= event
.GetId();
456 if ( !GetMenuBar()->FindItem(id
) )
459 wxString msg
= wxString::Format(wxT("Menu command %d"), id
);
460 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
462 msg
+= wxString::Format(wxT(" (the item is currently %schecked)"),
463 event
.IsChecked() ? "" : "not ");
469 // ----------------------------------------------------------------------------
471 // ----------------------------------------------------------------------------
473 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
478 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
483 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
485 (void)wxMessageBox(_T("wxWindows menu sample\n© 1999-2001 Vadim Zeitlin"),
486 _T("About wxWindows menu sample"),
490 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
492 wxMenuBar
*mbar
= GetMenuBar();
494 size_t count
= mbar
->GetMenuCount();
497 // don't let delete the first 2 menus
498 wxLogError(wxT("Can't delete any more menus"));
502 delete mbar
->Remove(count
- 1);
506 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
509 wxMenu
*menu
= CreateDummyMenu(&title
);
510 GetMenuBar()->Insert(0, menu
, title
);
513 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
516 wxMenu
*menu
= CreateDummyMenu(&title
);
517 GetMenuBar()->Append(menu
, title
);
520 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
522 wxMenuBar
*mbar
= GetMenuBar();
526 m_menu
= mbar
->Remove(0);
531 mbar
->Insert(0, m_menu
, _T("&File"));
536 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
538 wxMenuBar
*mbar
= GetMenuBar();
539 size_t count
= mbar
->GetMenuCount();
541 mbar
->EnableTop(count
- 1, event
.IsChecked());
544 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
546 wxMenuBar
*mbar
= GetMenuBar();
547 size_t count
= mbar
->GetMenuCount();
549 wxCHECK_RET( count
, _T("no last menu?") );
551 wxLogMessage(wxT("The label of the last menu item is '%s'"),
552 mbar
->GetLabelTop(count
- 1).c_str());
555 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
557 wxMenuBar
*mbar
= GetMenuBar();
558 size_t count
= mbar
->GetMenuCount();
560 wxCHECK_RET( count
, _T("no last menu?") );
562 wxString label
= wxGetTextFromUser
564 _T("Enter new label: "),
565 _T("Change last menu text"),
566 mbar
->GetLabelTop(count
- 1),
570 if ( !label
.empty() )
572 mbar
->SetLabelTop(count
- 1, label
);
576 void MyFrame::OnFindMenu(wxCommandEvent
& WXUNUSED(event
))
578 wxMenuBar
*mbar
= GetMenuBar();
579 size_t count
= mbar
->GetMenuCount();
581 wxCHECK_RET( count
, _T("no last menu?") );
583 wxString label
= wxGetTextFromUser
585 _T("Enter label to search for: "),
591 if ( !label
.empty() )
593 int index
= mbar
->FindMenu(label
);
595 if (index
== wxNOT_FOUND
)
597 wxLogWarning(wxT("No menu with label '%s'"), label
.c_str());
601 wxLogMessage(wxT("Menu %d has label '%s'"), index
, label
.c_str());
606 void MyFrame::OnDummy(wxCommandEvent
& event
)
608 wxLogMessage(wxT("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
611 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
613 wxMenuBar
*menubar
= GetMenuBar();
614 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
616 menu
->AppendSeparator();
617 menu
->Append(Menu_Dummy_Third
, _T("&Third dummy item\tCtrl-F3"),
618 _T("Checkable item"), TRUE
);
621 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
623 wxMenuBar
*menubar
= GetMenuBar();
625 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 2);
627 menu
->Append(Menu_Dummy_Last
, _T("&Dummy sub menu"),
628 CreateDummyMenu(NULL
), _T("Dummy sub menu help"));
631 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
633 wxMenuBar
*menubar
= GetMenuBar();
634 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
636 size_t count
= menu
->GetMenuItemCount();
639 wxLogWarning(wxT("No items to delete!"));
643 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
647 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
649 wxMenuBar
*menubar
= GetMenuBar();
650 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
652 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
653 _T("Fourth dummy item\tCtrl-F4")));
654 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, _T("")));
657 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
659 wxMenuItem
*item
= GetLastMenuItem();
663 item
->Enable(!item
->IsEnabled());
667 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
669 wxMenuItem
*item
= GetLastMenuItem();
674 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
678 wxMenuItem
*item
= GetLastMenuItem();
680 event
.Enable(item
&& item
->IsCheckable());
683 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
685 wxMenuItem
*item
= GetLastMenuItem();
689 wxLogMessage(wxT("The label of the last menu item is '%s'"),
690 item
->GetLabel().c_str());
694 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
696 wxMenuItem
*item
= GetLastMenuItem();
700 wxString label
= wxGetTextFromUser
702 _T("Enter new label: "),
703 _T("Change last menu item text"),
708 if ( !label
.empty() )
710 item
->SetText(label
);
715 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
717 wxMenuItem
*item
= GetLastMenuItem();
722 msg
<< _T("The item is ") << (item
->IsEnabled() ? _T("enabled")
726 if ( item
->IsCheckable() )
728 msg
<< _T("It is checkable and ") << (item
->IsChecked() ? _T("") : _T("un"))
733 wxAcceleratorEntry
*accel
= item
->GetAccel();
736 msg
<< _T("Its accelerator is ");
738 int flags
= accel
->GetFlags();
739 if ( flags
& wxACCEL_ALT
)
741 if ( flags
& wxACCEL_CTRL
)
743 if ( flags
& wxACCEL_SHIFT
)
744 msg
<< wxT("Shift-");
746 int code
= accel
->GetKeyCode();
761 msg
<< wxT('F') << code
- WXK_F1
+ 1;
764 // if there are any other keys wxGetAccelFromString() may return,
765 // we should process them here
768 if ( wxIsalnum(code
) )
775 wxFAIL_MSG( wxT("unknown keyboard accel") );
782 msg
<< _T("It doesn't have an accelerator");
784 #endif // wxUSE_ACCEL
790 void MyFrame::OnFindMenuItem(wxCommandEvent
& WXUNUSED(event
))
792 wxMenuBar
*mbar
= GetMenuBar();
793 size_t count
= mbar
->GetMenuCount();
795 wxCHECK_RET( count
, _T("no last menu?") );
797 wxString label
= wxGetTextFromUser
799 _T("Enter label to search for: "),
800 _T("Find menu item"),
805 if ( !label
.empty() )
807 size_t menuindex
= 0;
808 int index
= wxNOT_FOUND
;
810 for (menuindex
= 0; (menuindex
< count
) && (index
== wxNOT_FOUND
); ++menuindex
)
812 index
= mbar
->FindMenuItem(mbar
->GetMenu(menuindex
)->GetTitle(), label
);
814 if (index
== wxNOT_FOUND
)
816 wxLogWarning(wxT("No menu item with label '%s'"), label
.c_str());
820 wxLogMessage(wxT("Menu item %d in menu %lu has label '%s'"),
821 index
, (unsigned long)menuindex
, label
.c_str());
826 void MyFrame::ShowContextMenu(const wxPoint
& pos
)
828 wxMenu
menu(_T("Test popup"));
830 menu
.Append(Menu_Help_About
, _T("&About"));
831 menu
.Append(Menu_Popup_Submenu
, _T("&Submenu"), CreateDummyMenu(NULL
));
832 menu
.Append(Menu_Popup_ToBeDeleted
, _T("To be &deleted"));
833 menu
.Append(Menu_Popup_ToBeChecked
, _T("To be &checked"), _T(""), TRUE
);
834 menu
.Append(Menu_Popup_ToBeGreyed
, _T("To be &greyed"));
835 menu
.AppendSeparator();
836 menu
.Append(Menu_File_Quit
, _T("E&xit"));
838 menu
.Delete(Menu_Popup_ToBeDeleted
);
839 menu
.Check(Menu_Popup_ToBeChecked
, TRUE
);
840 menu
.Enable(Menu_Popup_ToBeGreyed
, FALSE
);
842 PopupMenu(&menu
, pos
.x
, pos
.y
);
844 // test for destroying items in popup menus
845 #if 0 // doesn't work in wxGTK!
846 menu
.Destroy(Menu_Popup_Submenu
);
848 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
852 void MyFrame::OnTestNormal(wxCommandEvent
& event
)
854 wxLogMessage(_T("Normal item selected"));
857 void MyFrame::OnTestCheck(wxCommandEvent
& event
)
859 wxLogMessage(_T("Check item %schecked"),
860 event
.IsChecked() ? _T("") : _T("un"));
863 void MyFrame::OnTestRadio(wxCommandEvent
& event
)
865 wxLogMessage(_T("Radio item %d selected"),
866 event
.GetId() - Menu_Test_Radio1
+ 1);
869 void MyFrame::LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
)
871 wxLogStatus(this, _T("A %smenu has been %s."),
872 event
.IsPopup() ? _T("popup ") : _T(""), what
);
875 void MyFrame::OnSize(wxSizeEvent
& event
)
880 // leave a band below for popup menu testing
881 wxSize size
= GetClientSize();
882 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
884 // this is really ugly but we have to do it as we can't just call
885 // event.Skip() because wxFrameBase would make the text control fill the
887 #ifdef __WXUNIVERSAL__
889 #endif // __WXUNIVERSAL__