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__) || defined(__WXX11__)
45 #define USE_CONTEXT_MENU 0
47 #define USE_CONTEXT_MENU 1
50 // this sample is useful when a new port is developed
51 // and usually a 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
58 #if wxUSE_OWNER_DRAWN || defined(__WXGTK__)
63 #include "../sample.xpm"
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // Define a new application
71 class MyApp
: public wxApp
78 class MyFrame
: public wxFrame
85 void LogMenuEvent(const wxCommandEvent
& event
);
88 void OnQuit(wxCommandEvent
& event
);
90 void OnClearLog(wxCommandEvent
& event
);
91 void OnClearLogUpdateUI(wxUpdateUIEvent
& event
);
92 #endif // USE_LOG_WINDOW
94 void OnAbout(wxCommandEvent
& event
);
96 void OnDummy(wxCommandEvent
& event
);
98 void OnAppendMenuItem(wxCommandEvent
& event
);
99 void OnAppendSubMenu(wxCommandEvent
& event
);
100 void OnDeleteMenuItem(wxCommandEvent
& event
);
101 void OnDeleteSubMenu(wxCommandEvent
& event
);
102 void OnInsertMenuItem(wxCommandEvent
& event
);
103 void OnCheckMenuItem(wxCommandEvent
& event
);
104 void OnEnableMenuItem(wxCommandEvent
& event
);
105 void OnGetLabelMenuItem(wxCommandEvent
& event
);
107 void OnSetLabelMenuItem(wxCommandEvent
& event
);
109 void OnGetMenuItemInfo(wxCommandEvent
& event
);
111 void OnFindMenuItem(wxCommandEvent
& event
);
114 void OnAppendMenu(wxCommandEvent
& event
);
115 void OnInsertMenu(wxCommandEvent
& event
);
116 void OnDeleteMenu(wxCommandEvent
& event
);
117 void OnToggleMenu(wxCommandEvent
& event
);
118 void OnEnableMenu(wxCommandEvent
& event
);
119 void OnGetLabelMenu(wxCommandEvent
& event
);
120 void OnSetLabelMenu(wxCommandEvent
& event
);
122 void OnFindMenu(wxCommandEvent
& event
);
125 void OnTestNormal(wxCommandEvent
& event
);
126 void OnTestCheck(wxCommandEvent
& event
);
127 void OnTestRadio(wxCommandEvent
& event
);
129 void OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
);
130 void OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
);
131 void OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
);
134 void OnContextMenu(wxContextMenuEvent
& event
);
136 void OnRightUp(wxMouseEvent
& event
)
137 { ShowContextMenu(event
.GetPosition()); }
140 void OnMenuOpen(wxMenuEvent
& event
)
143 LogMenuOpenOrClose(event
, wxT("opened")); event
.Skip();
146 void OnMenuClose(wxMenuEvent
& event
)
149 LogMenuOpenOrClose(event
, wxT("closed")); event
.Skip();
153 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
155 void OnSize(wxSizeEvent
& event
);
158 void LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
);
159 void ShowContextMenu(const wxPoint
& pos
);
161 wxMenu
*CreateDummyMenu(wxString
*title
);
163 wxMenuItem
*GetLastMenuItem() const;
165 // the menu previously detached from the menubar (may be NULL)
168 // the count of dummy menus already created
172 // the control used for logging
173 wxTextCtrl
*m_textctrl
;
176 // the previous log target
179 DECLARE_EVENT_TABLE()
182 // A small helper class which intercepts all menu events and logs them
183 class MyEvtHandler
: public wxEvtHandler
186 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
188 void OnMenuEvent(wxCommandEvent
& event
)
190 m_frame
->LogMenuEvent(event
);
198 DECLARE_EVENT_TABLE()
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
207 Menu_File_Quit
= wxID_EXIT
,
209 Menu_File_ClearLog
= 100,
212 Menu_MenuBar_Toggle
= 200,
217 Menu_MenuBar_GetLabel
,
219 Menu_MenuBar_SetLabel
,
220 Menu_MenuBar_FindMenu
,
223 Menu_Menu_Append
= 300,
239 Menu_Test_Normal
= 400,
252 Menu_Dummy_First
= 500,
258 Menu_Help_About
= wxID_ABOUT
,
260 Menu_Popup_ToBeDeleted
= 2000,
261 Menu_Popup_ToBeGreyed
,
262 Menu_Popup_ToBeChecked
,
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
275 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
277 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
278 EVT_UPDATE_UI(Menu_File_ClearLog
, MyFrame::OnClearLogUpdateUI
)
281 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
283 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
284 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
285 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
286 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
287 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
288 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
290 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
291 EVT_MENU(Menu_MenuBar_FindMenu
, MyFrame::OnFindMenu
)
294 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
295 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
296 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
297 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
298 EVT_MENU(Menu_Menu_DeleteSub
, MyFrame::OnDeleteSubMenu
)
299 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
300 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
301 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
303 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
305 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
307 EVT_MENU(Menu_Menu_FindItem
, MyFrame::OnFindMenuItem
)
310 EVT_MENU(Menu_Test_Normal
, MyFrame::OnTestNormal
)
311 EVT_MENU(Menu_Test_Check
, MyFrame::OnTestCheck
)
312 EVT_MENU(Menu_Test_Radio1
, MyFrame::OnTestRadio
)
313 EVT_MENU(Menu_Test_Radio2
, MyFrame::OnTestRadio
)
314 EVT_MENU(Menu_Test_Radio3
, MyFrame::OnTestRadio
)
316 EVT_UPDATE_UI(Menu_SubMenu_Normal
, MyFrame::OnUpdateSubMenuNormal
)
317 EVT_UPDATE_UI(Menu_SubMenu_Check
, MyFrame::OnUpdateSubMenuCheck
)
318 EVT_UPDATE_UI(Menu_SubMenu_Radio1
, MyFrame::OnUpdateSubMenuRadio
)
319 EVT_UPDATE_UI(Menu_SubMenu_Radio2
, MyFrame::OnUpdateSubMenuRadio
)
320 EVT_UPDATE_UI(Menu_SubMenu_Radio3
, MyFrame::OnUpdateSubMenuRadio
)
322 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
324 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
327 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
329 EVT_RIGHT_UP(MyFrame::OnRightUp
)
332 EVT_MENU_OPEN(MyFrame::OnMenuOpen
)
333 EVT_MENU_CLOSE(MyFrame::OnMenuClose
)
335 EVT_SIZE(MyFrame::OnSize
)
338 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
339 EVT_MENU(wxID_ANY
, MyEvtHandler::OnMenuEvent
)
342 // ============================================================================
344 // ============================================================================
346 // ----------------------------------------------------------------------------
348 // ----------------------------------------------------------------------------
352 // The `main program' equivalent, creating the windows and returning the
356 if ( !wxApp::OnInit() )
359 // Create the main frame window
360 MyFrame
* frame
= new MyFrame
;
365 frame
->SetStatusText(wxT("Welcome to wxWidgets menu sample"));
366 #endif // wxUSE_STATUSBAR
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
375 // Define my frame constructor
377 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, wxT("wxWidgets menu sample"))
379 SetIcon(wxICON(sample
));
390 #endif // wxUSE_STATUSBAR
392 // create the menubar
393 wxMenu
*fileMenu
= new wxMenu
;
395 wxMenu
*stockSubMenu
= new wxMenu
;
396 stockSubMenu
->Append(wxID_ADD
);
397 stockSubMenu
->Append(wxID_APPLY
);
398 stockSubMenu
->Append(wxID_BACKWARD
);
399 stockSubMenu
->Append(wxID_BOLD
);
400 stockSubMenu
->Append(wxID_BOTTOM
);
401 stockSubMenu
->Append(wxID_CANCEL
);
402 stockSubMenu
->Append(wxID_CDROM
);
403 stockSubMenu
->Append(wxID_CLEAR
);
404 stockSubMenu
->Append(wxID_CLOSE
);
405 stockSubMenu
->Append(wxID_CONVERT
);
406 stockSubMenu
->Append(wxID_COPY
);
407 stockSubMenu
->Append(wxID_CUT
);
408 stockSubMenu
->Append(wxID_DELETE
);
409 stockSubMenu
->Append(wxID_DOWN
);
410 stockSubMenu
->Append(wxID_EXECUTE
);
411 stockSubMenu
->Append(wxID_EXIT
);
412 stockSubMenu
->Append(wxID_FIND
);
413 stockSubMenu
->Append(wxID_FIRST
);
414 stockSubMenu
->Append(wxID_FLOPPY
);
415 stockSubMenu
->Append(wxID_FORWARD
);
416 stockSubMenu
->Append(wxID_HARDDISK
);
417 stockSubMenu
->Append(wxID_HELP
);
418 stockSubMenu
->Append(wxID_HOME
);
419 stockSubMenu
->Append(wxID_INDENT
);
420 stockSubMenu
->Append(wxID_INDEX
);
421 stockSubMenu
->Append(wxID_INFO
);
422 stockSubMenu
->Append(wxID_ITALIC
);
423 stockSubMenu
->Append(wxID_JUMP_TO
);
424 stockSubMenu
->Append(wxID_JUSTIFY_CENTER
);
425 stockSubMenu
->Append(wxID_JUSTIFY_FILL
);
426 stockSubMenu
->Append(wxID_JUSTIFY_LEFT
);
427 stockSubMenu
->Append(wxID_JUSTIFY_RIGHT
);
428 stockSubMenu
->Append(wxID_LAST
);
429 stockSubMenu
->Append(wxID_NETWORK
);
430 stockSubMenu
->Append(wxID_NEW
);
431 stockSubMenu
->Append(wxID_NO
);
432 stockSubMenu
->Append(wxID_OK
);
433 stockSubMenu
->Append(wxID_OPEN
);
434 stockSubMenu
->Append(wxID_PASTE
);
435 stockSubMenu
->Append(wxID_PREFERENCES
);
436 stockSubMenu
->Append(wxID_PREVIEW
);
437 stockSubMenu
->Append(wxID_PRINT
);
438 stockSubMenu
->Append(wxID_PROPERTIES
);
439 stockSubMenu
->Append(wxID_REDO
);
440 stockSubMenu
->Append(wxID_REFRESH
);
441 stockSubMenu
->Append(wxID_REMOVE
);
442 stockSubMenu
->Append(wxID_REPLACE
);
443 stockSubMenu
->Append(wxID_REVERT_TO_SAVED
);
444 stockSubMenu
->Append(wxID_SAVE
);
445 stockSubMenu
->Append(wxID_SAVEAS
);
446 stockSubMenu
->Append(wxID_SELECT_COLOR
);
447 stockSubMenu
->Append(wxID_SELECT_FONT
);
448 stockSubMenu
->Append(wxID_SORT_ASCENDING
);
449 stockSubMenu
->Append(wxID_SORT_DESCENDING
);
450 stockSubMenu
->Append(wxID_SPELL_CHECK
);
451 stockSubMenu
->Append(wxID_STOP
);
452 stockSubMenu
->Append(wxID_STRIKETHROUGH
);
453 stockSubMenu
->Append(wxID_TOP
);
454 stockSubMenu
->Append(wxID_UNDELETE
);
455 stockSubMenu
->Append(wxID_UNDERLINE
);
456 stockSubMenu
->Append(wxID_UNDO
);
457 stockSubMenu
->Append(wxID_UNINDENT
);
458 stockSubMenu
->Append(wxID_UP
);
459 stockSubMenu
->Append(wxID_YES
);
460 stockSubMenu
->Append(wxID_ZOOM_100
);
461 stockSubMenu
->Append(wxID_ZOOM_FIT
);
462 stockSubMenu
->Append(wxID_ZOOM_IN
);
463 stockSubMenu
->Append(wxID_ZOOM_OUT
);
464 fileMenu
->AppendSubMenu(stockSubMenu
, wxT("&Standard items demo"));
467 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
468 wxT("Clear &log\tCtrl-L"));
469 #if wxUSE_OWNER_DRAWN || defined(__WXGTK__)
470 item
->SetBitmap(copy_xpm
);
472 fileMenu
->Append(item
);
473 fileMenu
->AppendSeparator();
474 #endif // USE_LOG_WINDOW
476 fileMenu
->Append(Menu_File_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit menu sample"));
478 wxMenu
*menubarMenu
= new wxMenu
;
479 menubarMenu
->Append(Menu_MenuBar_Append
, wxT("&Append menu\tCtrl-A"),
480 wxT("Append a menu to the menubar"));
481 menubarMenu
->Append(Menu_MenuBar_Insert
, wxT("&Insert menu\tCtrl-I"),
482 wxT("Insert a menu into the menubar"));
483 menubarMenu
->Append(Menu_MenuBar_Delete
, wxT("&Delete menu\tCtrl-D"),
484 wxT("Delete the last menu from the menubar"));
485 menubarMenu
->Append(Menu_MenuBar_Toggle
, wxT("&Toggle menu\tCtrl-T"),
486 wxT("Toggle the first menu in the menubar"), true);
487 menubarMenu
->AppendSeparator();
488 menubarMenu
->Append(Menu_MenuBar_Enable
, wxT("&Enable menu\tCtrl-E"),
489 wxT("Enable or disable the last menu"), true);
490 menubarMenu
->AppendSeparator();
491 menubarMenu
->Append(Menu_MenuBar_GetLabel
, wxT("&Get menu label\tCtrl-G"),
492 wxT("Get the label of the last menu"));
494 menubarMenu
->Append(Menu_MenuBar_SetLabel
, wxT("&Set menu label\tCtrl-S"),
495 wxT("Change the label of the last menu"));
496 menubarMenu
->AppendSeparator();
497 menubarMenu
->Append(Menu_MenuBar_FindMenu
, wxT("&Find menu from label\tCtrl-F"),
498 wxT("Find a menu by searching for its label"));
501 wxMenu
* subMenu
= new wxMenu
;
502 subMenu
->Append(Menu_SubMenu_Normal
, wxT("&Normal submenu item"), wxT("Disabled submenu item"));
503 subMenu
->AppendCheckItem(Menu_SubMenu_Check
, wxT("&Check submenu item"), wxT("Check submenu item"));
504 subMenu
->AppendRadioItem(Menu_SubMenu_Radio1
, wxT("Radio item &1"), wxT("Radio item"));
505 subMenu
->AppendRadioItem(Menu_SubMenu_Radio2
, wxT("Radio item &2"), wxT("Radio item"));
506 subMenu
->AppendRadioItem(Menu_SubMenu_Radio3
, wxT("Radio item &3"), wxT("Radio item"));
508 menubarMenu
->Append(Menu_SubMenu
, wxT("Submenu"), subMenu
);
510 wxMenu
*menuMenu
= new wxMenu
;
511 menuMenu
->Append(Menu_Menu_Append
, wxT("&Append menu item\tAlt-A"),
512 wxT("Append a menu item to the 'Test' menu"));
513 menuMenu
->Append(Menu_Menu_AppendSub
, wxT("&Append sub menu\tAlt-S"),
514 wxT("Append a sub menu to the 'Test' menu"));
515 menuMenu
->Append(Menu_Menu_Insert
, wxT("&Insert menu item\tAlt-I"),
516 wxT("Insert a menu item in head of the 'Test' menu"));
517 menuMenu
->Append(Menu_Menu_Delete
, wxT("&Delete menu item\tAlt-D"),
518 wxT("Delete the last menu item from the 'Test' menu"));
519 menuMenu
->Append(Menu_Menu_DeleteSub
, wxT("Delete last &submenu\tAlt-K"),
520 wxT("Delete the last submenu from the 'Test' menu"));
521 menuMenu
->AppendSeparator();
522 menuMenu
->Append(Menu_Menu_Enable
, wxT("&Enable menu item\tAlt-E"),
523 wxT("Enable or disable the last menu item"), true);
524 menuMenu
->Append(Menu_Menu_Check
, wxT("&Check menu item\tAlt-C"),
525 wxT("Check or uncheck the last menu item"), true);
526 menuMenu
->AppendSeparator();
527 menuMenu
->Append(Menu_Menu_GetInfo
, wxT("Get menu item in&fo\tAlt-F"),
528 wxT("Show the state of the last menu item"));
530 menuMenu
->Append(Menu_Menu_SetLabel
, wxT("Set menu item label\tAlt-L"),
531 wxT("Set the label of a menu item"));
534 menuMenu
->AppendSeparator();
535 menuMenu
->Append(Menu_Menu_FindItem
, wxT("Find menu item from label"),
536 wxT("Find a menu item by searching for its label"));
539 wxMenu
*testMenu
= new wxMenu
;
540 testMenu
->Append(Menu_Test_Normal
, wxT("&Normal item"));
541 testMenu
->AppendSeparator();
542 testMenu
->AppendCheckItem(Menu_Test_Check
, wxT("&Check item"));
543 testMenu
->AppendSeparator();
544 testMenu
->AppendRadioItem(Menu_Test_Radio1
, wxT("Radio item &1"));
545 testMenu
->AppendRadioItem(Menu_Test_Radio2
, wxT("Radio item &2"));
546 testMenu
->AppendRadioItem(Menu_Test_Radio3
, wxT("Radio item &3"));
548 wxMenu
*helpMenu
= new wxMenu
;
549 helpMenu
->Append(Menu_Help_About
, wxT("&About\tF1"), wxT("About menu sample"));
551 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
553 menuBar
->Append(fileMenu
, wxT("&File"));
554 menuBar
->Append(menubarMenu
, wxT("Menu&bar"));
555 menuBar
->Append(menuMenu
, wxT("&Menu"));
556 menuBar
->Append(testMenu
, wxT("&Test"));
557 menuBar
->Append(helpMenu
, wxT("&Help"));
559 // these items should be initially checked
560 menuBar
->Check(Menu_MenuBar_Toggle
, true);
561 menuBar
->Check(Menu_MenuBar_Enable
, true);
562 menuBar
->Check(Menu_Menu_Enable
, true);
563 menuBar
->Check(Menu_Menu_Check
, false);
565 // associate the menu bar with the frame
568 // intercept all menu events and log them in this custom event handler
569 PushEventHandler(new MyEvtHandler(this));
572 // create the log text window
573 m_textctrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
574 wxDefaultPosition
, wxDefaultSize
,
576 m_textctrl
->SetEditable(false);
578 wxLog::DisableTimestamp();
579 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
581 wxLogMessage(wxT("Brief explanations: the commands in the \"Menu\" menu ")
582 wxT("append/insert/delete items to/from the \"Test\" menu.\n")
583 wxT("The commands in the \"Menubar\" menu work with the ")
584 wxT("menubar itself.\n\n")
585 wxT("Right click the band below to test popup menus.\n"));
596 // delete the event handler installed in ctor
597 PopEventHandler(true);
600 // restore old logger
601 delete wxLog::SetActiveTarget(m_logOld
);
605 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
607 wxMenu
*menu
= new wxMenu
;
608 menu
->Append(Menu_Dummy_First
, wxT("&First item\tCtrl-F1"));
609 menu
->AppendSeparator();
610 menu
->AppendCheckItem(Menu_Dummy_Second
, wxT("&Second item\tCtrl-F2"));
614 title
->Printf(wxT("Dummy menu &%u"), (unsigned)++m_countDummy
);
620 wxMenuItem
*MyFrame::GetLastMenuItem() const
622 wxMenuBar
*menubar
= GetMenuBar();
623 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
624 wxCHECK_MSG( menu
, NULL
, wxT("no 'Test' menu?") );
626 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
629 wxLogWarning(wxT("No last item in the 'Test' menu!"));
635 return node
->GetData();
639 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
641 int id
= event
.GetId();
643 wxString msg
= wxString::Format(wxT("Menu command %d"), id
);
645 // catch all checkable menubar items and also the check item from the popup
647 wxMenuItem
*item
= GetMenuBar()->FindItem(id
);
648 if ( (item
&& item
->IsCheckable()) || id
== Menu_Popup_ToBeChecked
)
650 msg
+= wxString::Format(wxT(" (the item is currently %schecked)"),
651 event
.IsChecked() ? wxT("") : wxT("not "));
657 // ----------------------------------------------------------------------------
659 // ----------------------------------------------------------------------------
661 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
668 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
673 void MyFrame::OnClearLogUpdateUI(wxUpdateUIEvent
& event
)
675 // if we only enable this item when the log window is empty, we never see
676 // it in the disable state as a message is logged whenever the menu is
677 // opened, so we disable it if there is not "much" text in the window
678 event
.Enable( m_textctrl
->GetNumberOfLines() > 5 );
681 #endif // USE_LOG_WINDOW
683 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
685 (void)wxMessageBox(wxT("wxWidgets menu sample\n(c) 1999-2001 Vadim Zeitlin"),
686 wxT("About wxWidgets menu sample"),
687 wxOK
| wxICON_INFORMATION
);
690 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
692 wxMenuBar
*mbar
= GetMenuBar();
694 size_t count
= mbar
->GetMenuCount();
697 // don't let delete the first 4 menus
698 wxLogError(wxT("Can't delete any more menus"));
702 delete mbar
->Remove(count
- 1);
706 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
709 wxMenu
*menu
= CreateDummyMenu(&title
);
710 // Insert before the 'Help' menu
711 // Otherwise repeated Deletes will remove the 'Test' menu
712 GetMenuBar()->Insert(4, menu
, title
);
715 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
718 wxMenu
*menu
= CreateDummyMenu(&title
);
719 GetMenuBar()->Append(menu
, title
);
722 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
724 wxMenuBar
*mbar
= GetMenuBar();
728 m_menu
= mbar
->Remove(0);
733 mbar
->Insert(0, m_menu
, wxT("&File"));
738 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
740 wxMenuBar
*mbar
= GetMenuBar();
741 size_t count
= mbar
->GetMenuCount();
743 mbar
->EnableTop(count
- 1, event
.IsChecked());
746 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
748 wxMenuBar
*mbar
= GetMenuBar();
749 size_t count
= mbar
->GetMenuCount();
751 wxCHECK_RET( count
, wxT("no last menu?") );
753 wxLogMessage(wxT("The label of the last menu item is '%s'"),
754 mbar
->GetMenuLabel(count
- 1).c_str());
758 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
760 wxMenuBar
*mbar
= GetMenuBar();
761 size_t count
= mbar
->GetMenuCount();
763 wxCHECK_RET( count
, wxT("no last menu?") );
765 wxString label
= wxGetTextFromUser
767 wxT("Enter new label: "),
768 wxT("Change last menu text"),
769 mbar
->GetMenuLabel(count
- 1),
773 if ( !label
.empty() )
775 mbar
->SetMenuLabel(count
- 1, label
);
779 void MyFrame::OnFindMenu(wxCommandEvent
& WXUNUSED(event
))
781 wxMenuBar
*mbar
= GetMenuBar();
782 size_t count
= mbar
->GetMenuCount();
784 wxCHECK_RET( count
, wxT("no last menu?") );
786 wxString label
= wxGetTextFromUser
788 wxT("Enter label to search for: "),
794 if ( !label
.empty() )
796 int index
= mbar
->FindMenu(label
);
798 if (index
== wxNOT_FOUND
)
800 wxLogWarning(wxT("No menu with label '%s'"), label
.c_str());
804 wxLogMessage(wxT("Menu %d has label '%s'"), index
, label
.c_str());
810 void MyFrame::OnDummy(wxCommandEvent
& event
)
812 wxLogMessage(wxT("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
815 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
817 wxMenuBar
*menubar
= GetMenuBar();
818 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
819 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
821 menu
->AppendSeparator();
822 menu
->Append(Menu_Dummy_Third
, wxT("&Third dummy item\tCtrl-F3"),
823 wxT("Checkable item"), true);
826 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
828 wxMenuBar
*menubar
= GetMenuBar();
829 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
830 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
832 menu
->Append(Menu_Dummy_Last
, wxT("&Dummy sub menu"),
833 CreateDummyMenu(NULL
), wxT("Dummy sub menu help"));
836 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
838 wxMenuBar
*menubar
= GetMenuBar();
839 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
840 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
842 size_t count
= menu
->GetMenuItemCount();
845 wxLogWarning(wxT("No items to delete!"));
849 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
853 void MyFrame::OnDeleteSubMenu(wxCommandEvent
& WXUNUSED(event
))
855 wxMenuBar
*menubar
= GetMenuBar();
856 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
857 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
859 for ( int n
= menu
->GetMenuItemCount() - 1; n
>=0 ; --n
)
861 wxMenuItem
* item
= menu
->FindItemByPosition(n
);
862 if (item
->IsSubMenu())
869 wxLogWarning(wxT("No submenu to delete!"));
872 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
874 wxMenuBar
*menubar
= GetMenuBar();
875 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
876 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
878 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
879 wxT("Fourth dummy item\tCtrl-F4")));
880 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
));
883 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
885 wxMenuItem
*item
= GetLastMenuItem();
889 item
->Enable(!item
->IsEnabled());
893 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
895 wxMenuItem
*item
= GetLastMenuItem();
897 if (item
&& item
->IsCheckable())
903 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
907 wxMenuItem
*item
= GetLastMenuItem();
909 event
.Enable(item
&& item
->IsCheckable());
912 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
914 wxMenuItem
*item
= GetLastMenuItem();
918 wxString label
= item
->GetItemLabel();
919 wxLogMessage(wxT("The label of the last menu item is '%s'"),
925 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
927 wxMenuItem
*item
= GetLastMenuItem();
931 wxString label
= wxGetTextFromUser
933 wxT("Enter new label: "),
934 wxT("Change last menu item text"),
935 item
->GetItemLabel(),
938 label
.Replace( wxT("\\t"), wxT("\t") );
940 if ( !label
.empty() )
942 item
->SetItemLabel(label
);
948 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
950 wxMenuItem
*item
= GetLastMenuItem();
955 msg
<< wxT("The item is ") << (item
->IsEnabled() ? wxT("enabled")
959 if ( item
->IsCheckable() )
961 msg
<< wxT("It is checkable and ") << (item
->IsChecked() ? wxT("") : wxT("un"))
966 wxAcceleratorEntry
*accel
= item
->GetAccel();
969 msg
<< wxT("Its accelerator is ");
971 int flags
= accel
->GetFlags();
972 if ( flags
& wxACCEL_ALT
)
974 if ( flags
& wxACCEL_CTRL
)
976 if ( flags
& wxACCEL_SHIFT
)
977 msg
<< wxT("Shift-");
979 int code
= accel
->GetKeyCode();
994 msg
<< wxT('F') << code
- WXK_F1
+ 1;
997 // if there are any other keys wxGetAccelFromString() may return,
998 // we should process them here
1001 if ( wxIsalnum(code
) )
1003 msg
<< (wxChar
)code
;
1008 wxFAIL_MSG( wxT("unknown keyboard accel") );
1015 msg
<< wxT("It doesn't have an accelerator");
1017 #endif // wxUSE_ACCEL
1024 void MyFrame::OnFindMenuItem(wxCommandEvent
& WXUNUSED(event
))
1026 wxMenuBar
*mbar
= GetMenuBar();
1027 size_t count
= mbar
->GetMenuCount();
1029 wxCHECK_RET( count
, wxT("no last menu?") );
1031 wxString label
= wxGetTextFromUser
1033 wxT("Enter label to search for: "),
1034 wxT("Find menu item"),
1039 if ( !label
.empty() )
1042 int index
= wxNOT_FOUND
;
1044 for (menuindex
= 0; (menuindex
< count
) && (index
== wxNOT_FOUND
); ++menuindex
)
1046 index
= mbar
->FindMenuItem(mbar
->GetMenu(menuindex
)->GetTitle(), label
);
1048 if (index
== wxNOT_FOUND
)
1050 wxLogWarning(wxT("No menu item with label '%s'"), label
.c_str());
1054 wxLogMessage(wxT("Menu item %d in menu %lu has label '%s'"),
1055 index
, (unsigned long)menuindex
, label
.c_str());
1061 void MyFrame::ShowContextMenu(const wxPoint
& pos
)
1065 if ( wxGetKeyState(WXK_SHIFT
) )
1067 // when Shift is pressed, demonstrate the use of a simple function
1068 // returning the id of the item selected in the popup menu
1069 menu
.SetTitle("Choose one of:");
1070 static const char *choices
[] = { "Apple", "Banana", "Cherry" };
1071 for ( size_t n
= 0; n
< WXSIZEOF(choices
); n
++ )
1072 menu
.Append(Menu_PopupChoice
+ n
, choices
[n
]);
1074 const int rc
= GetPopupMenuSelectionFromUser(menu
, pos
);
1075 if ( rc
== wxID_NONE
)
1077 wxLogMessage("No selection");
1081 wxLogMessage("You have selected \"%s\"",
1082 choices
[rc
- Menu_PopupChoice
]);
1085 else // normal case, shift not pressed
1087 menu
.Append(Menu_Help_About
, wxT("&About"));
1088 menu
.Append(Menu_Popup_Submenu
, wxT("&Submenu"), CreateDummyMenu(NULL
));
1089 menu
.Append(Menu_Popup_ToBeDeleted
, wxT("To be &deleted"));
1090 menu
.AppendCheckItem(Menu_Popup_ToBeChecked
, wxT("To be &checked"));
1091 menu
.Append(Menu_Popup_ToBeGreyed
, wxT("To be &greyed"),
1092 wxT("This menu item should be initially greyed out"));
1093 menu
.AppendSeparator();
1094 menu
.Append(Menu_File_Quit
, wxT("E&xit"));
1096 menu
.Delete(Menu_Popup_ToBeDeleted
);
1097 menu
.Check(Menu_Popup_ToBeChecked
, true);
1098 menu
.Enable(Menu_Popup_ToBeGreyed
, false);
1100 PopupMenu(&menu
, pos
);
1102 // test for destroying items in popup menus
1103 #if 0 // doesn't work in wxGTK!
1104 menu
.Destroy(Menu_Popup_Submenu
);
1106 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
1111 void MyFrame::OnTestNormal(wxCommandEvent
& WXUNUSED(event
))
1113 wxLogMessage(wxT("Normal item selected"));
1116 void MyFrame::OnTestCheck(wxCommandEvent
& event
)
1118 wxLogMessage(wxT("Check item %schecked"),
1119 event
.IsChecked() ? wxT("") : wxT("un"));
1122 void MyFrame::OnTestRadio(wxCommandEvent
& event
)
1124 wxLogMessage(wxT("Radio item %d selected"),
1125 event
.GetId() - Menu_Test_Radio1
+ 1);
1129 void MyFrame::LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
)
1133 << ( event
.IsPopup() ? wxT("popup ") : wxT("") )
1134 << wxT("menu has been ")
1138 wxLogStatus(this, msg
.c_str());
1142 void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
)
1144 event
.Enable(false);
1147 void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
)
1152 void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
)
1154 int which
= (event
.GetId() - Menu_SubMenu_Radio1
+ 1);
1161 #if USE_CONTEXT_MENU
1162 void MyFrame::OnContextMenu(wxContextMenuEvent
& event
)
1164 wxPoint point
= event
.GetPosition();
1166 if (point
.x
== -1 && point
.y
== -1) {
1167 wxSize size
= GetSize();
1168 point
.x
= size
.x
/ 2;
1169 point
.y
= size
.y
/ 2;
1171 point
= ScreenToClient(point
);
1173 ShowContextMenu(point
);
1177 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
1183 // leave a band below for popup menu testing
1184 wxSize size
= GetClientSize();
1185 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
1188 // this is really ugly but we have to do it as we can't just call
1189 // event.Skip() because wxFrameBase would make the text control fill the
1190 // entire frame then
1191 #ifdef __WXUNIVERSAL__
1193 #endif // __WXUNIVERSAL__