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
60 #ifndef wxHAS_IMAGES_IN_RESOURCES
61 #include "../sample.xpm"
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // Define a new application
69 class MyApp
: public wxApp
76 class MyFrame
: public wxFrame
83 void LogMenuEvent(const wxCommandEvent
& event
);
86 void OnQuit(wxCommandEvent
& event
);
88 void OnClearLog(wxCommandEvent
& event
);
89 void OnClearLogUpdateUI(wxUpdateUIEvent
& event
);
90 #endif // USE_LOG_WINDOW
92 void OnAbout(wxCommandEvent
& event
);
94 void OnDummy(wxCommandEvent
& event
);
96 void OnAppendMenuItem(wxCommandEvent
& event
);
97 void OnAppendSubMenu(wxCommandEvent
& event
);
98 void OnDeleteMenuItem(wxCommandEvent
& event
);
99 void OnDeleteSubMenu(wxCommandEvent
& event
);
100 void OnInsertMenuItem(wxCommandEvent
& event
);
101 void OnCheckMenuItem(wxCommandEvent
& event
);
102 void OnEnableMenuItem(wxCommandEvent
& event
);
103 void OnGetLabelMenuItem(wxCommandEvent
& event
);
105 void OnSetLabelMenuItem(wxCommandEvent
& event
);
107 void OnGetMenuItemInfo(wxCommandEvent
& event
);
109 void OnFindMenuItem(wxCommandEvent
& event
);
112 void OnAppendMenu(wxCommandEvent
& event
);
113 void OnInsertMenu(wxCommandEvent
& event
);
114 void OnDeleteMenu(wxCommandEvent
& event
);
115 void OnToggleMenu(wxCommandEvent
& event
);
116 void OnEnableMenu(wxCommandEvent
& event
);
117 void OnGetLabelMenu(wxCommandEvent
& event
);
118 void OnSetLabelMenu(wxCommandEvent
& event
);
120 void OnFindMenu(wxCommandEvent
& event
);
123 void OnTestNormal(wxCommandEvent
& event
);
124 void OnTestCheck(wxCommandEvent
& event
);
125 void OnTestRadio(wxCommandEvent
& event
);
127 void OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
);
128 void OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
);
129 void OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
);
132 void OnContextMenu(wxContextMenuEvent
& event
);
134 void OnRightUp(wxMouseEvent
& event
)
135 { ShowContextMenu(event
.GetPosition()); }
138 void OnMenuOpen(wxMenuEvent
& event
)
141 LogMenuOpenOrClose(event
, wxT("opened")); event
.Skip();
144 void OnMenuClose(wxMenuEvent
& event
)
147 LogMenuOpenOrClose(event
, wxT("closed")); event
.Skip();
151 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
153 void OnSize(wxSizeEvent
& event
);
156 void LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
);
157 void ShowContextMenu(const wxPoint
& pos
);
159 wxMenu
*CreateDummyMenu(wxString
*title
);
161 wxMenuItem
*GetLastMenuItem() const;
163 // the menu previously detached from the menubar (may be NULL)
166 // the count of dummy menus already created
170 // the control used for logging
171 wxTextCtrl
*m_textctrl
;
174 // the previous log target
177 DECLARE_EVENT_TABLE()
180 // A small helper class which intercepts all menu events and logs them
181 class MyEvtHandler
: public wxEvtHandler
184 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
186 void OnMenuEvent(wxCommandEvent
& event
)
188 m_frame
->LogMenuEvent(event
);
196 DECLARE_EVENT_TABLE()
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
205 Menu_File_Quit
= wxID_EXIT
,
207 Menu_File_ClearLog
= 100,
210 Menu_MenuBar_Toggle
= 200,
215 Menu_MenuBar_GetLabel
,
217 Menu_MenuBar_SetLabel
,
218 Menu_MenuBar_FindMenu
,
221 Menu_Menu_Append
= 300,
237 Menu_Test_Normal
= 400,
250 Menu_Dummy_First
= 500,
256 Menu_Help_About
= wxID_ABOUT
,
258 Menu_Popup_ToBeDeleted
= 2000,
259 Menu_Popup_ToBeGreyed
,
260 Menu_Popup_ToBeChecked
,
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
273 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
275 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
276 EVT_UPDATE_UI(Menu_File_ClearLog
, MyFrame::OnClearLogUpdateUI
)
279 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
281 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
282 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
283 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
284 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
285 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
286 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
288 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
289 EVT_MENU(Menu_MenuBar_FindMenu
, MyFrame::OnFindMenu
)
292 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
293 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
294 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
295 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
296 EVT_MENU(Menu_Menu_DeleteSub
, MyFrame::OnDeleteSubMenu
)
297 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
298 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
299 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
301 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
303 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
305 EVT_MENU(Menu_Menu_FindItem
, MyFrame::OnFindMenuItem
)
308 EVT_MENU(Menu_Test_Normal
, MyFrame::OnTestNormal
)
309 EVT_MENU(Menu_Test_Check
, MyFrame::OnTestCheck
)
310 EVT_MENU(Menu_Test_Radio1
, MyFrame::OnTestRadio
)
311 EVT_MENU(Menu_Test_Radio2
, MyFrame::OnTestRadio
)
312 EVT_MENU(Menu_Test_Radio3
, MyFrame::OnTestRadio
)
314 EVT_UPDATE_UI(Menu_SubMenu_Normal
, MyFrame::OnUpdateSubMenuNormal
)
315 EVT_UPDATE_UI(Menu_SubMenu_Check
, MyFrame::OnUpdateSubMenuCheck
)
316 EVT_UPDATE_UI(Menu_SubMenu_Radio1
, MyFrame::OnUpdateSubMenuRadio
)
317 EVT_UPDATE_UI(Menu_SubMenu_Radio2
, MyFrame::OnUpdateSubMenuRadio
)
318 EVT_UPDATE_UI(Menu_SubMenu_Radio3
, MyFrame::OnUpdateSubMenuRadio
)
320 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
322 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
325 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
327 EVT_RIGHT_UP(MyFrame::OnRightUp
)
330 EVT_MENU_OPEN(MyFrame::OnMenuOpen
)
331 EVT_MENU_CLOSE(MyFrame::OnMenuClose
)
333 EVT_SIZE(MyFrame::OnSize
)
336 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
337 EVT_MENU(wxID_ANY
, MyEvtHandler::OnMenuEvent
)
340 // ============================================================================
342 // ============================================================================
344 // ----------------------------------------------------------------------------
346 // ----------------------------------------------------------------------------
350 // The `main program' equivalent, creating the windows and returning the
354 if ( !wxApp::OnInit() )
357 // Create the main frame window
358 MyFrame
* frame
= new MyFrame
;
363 frame
->SetStatusText(wxT("Welcome to wxWidgets menu sample"));
364 #endif // wxUSE_STATUSBAR
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
373 // Define my frame constructor
375 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, wxT("wxWidgets menu sample"))
377 SetIcon(wxICON(sample
));
388 #endif // wxUSE_STATUSBAR
390 // create the menubar
391 wxMenu
*fileMenu
= new wxMenu
;
393 wxMenu
*stockSubMenu
= new wxMenu
;
394 stockSubMenu
->Append(wxID_ADD
);
395 stockSubMenu
->Append(wxID_APPLY
);
396 stockSubMenu
->Append(wxID_BACKWARD
);
397 stockSubMenu
->Append(wxID_BOLD
);
398 stockSubMenu
->Append(wxID_BOTTOM
);
399 stockSubMenu
->Append(wxID_CANCEL
);
400 stockSubMenu
->Append(wxID_CDROM
);
401 stockSubMenu
->Append(wxID_CLEAR
);
402 stockSubMenu
->Append(wxID_CLOSE
);
403 stockSubMenu
->Append(wxID_CONVERT
);
404 stockSubMenu
->Append(wxID_COPY
);
405 stockSubMenu
->Append(wxID_CUT
);
406 stockSubMenu
->Append(wxID_DELETE
);
407 stockSubMenu
->Append(wxID_DOWN
);
408 stockSubMenu
->Append(wxID_EXECUTE
);
409 stockSubMenu
->Append(wxID_EXIT
);
410 stockSubMenu
->Append(wxID_FIND
);
411 stockSubMenu
->Append(wxID_FIRST
);
412 stockSubMenu
->Append(wxID_FLOPPY
);
413 stockSubMenu
->Append(wxID_FORWARD
);
414 stockSubMenu
->Append(wxID_HARDDISK
);
415 stockSubMenu
->Append(wxID_HELP
);
416 stockSubMenu
->Append(wxID_HOME
);
417 stockSubMenu
->Append(wxID_INDENT
);
418 stockSubMenu
->Append(wxID_INDEX
);
419 stockSubMenu
->Append(wxID_INFO
);
420 stockSubMenu
->Append(wxID_ITALIC
);
421 stockSubMenu
->Append(wxID_JUMP_TO
);
422 stockSubMenu
->Append(wxID_JUSTIFY_CENTER
);
423 stockSubMenu
->Append(wxID_JUSTIFY_FILL
);
424 stockSubMenu
->Append(wxID_JUSTIFY_LEFT
);
425 stockSubMenu
->Append(wxID_JUSTIFY_RIGHT
);
426 stockSubMenu
->Append(wxID_LAST
);
427 stockSubMenu
->Append(wxID_NETWORK
);
428 stockSubMenu
->Append(wxID_NEW
);
429 stockSubMenu
->Append(wxID_NO
);
430 stockSubMenu
->Append(wxID_OK
);
431 stockSubMenu
->Append(wxID_OPEN
);
432 stockSubMenu
->Append(wxID_PASTE
);
433 stockSubMenu
->Append(wxID_PREFERENCES
);
434 stockSubMenu
->Append(wxID_PREVIEW
);
435 stockSubMenu
->Append(wxID_PRINT
);
436 stockSubMenu
->Append(wxID_PROPERTIES
);
437 stockSubMenu
->Append(wxID_REDO
);
438 stockSubMenu
->Append(wxID_REFRESH
);
439 stockSubMenu
->Append(wxID_REMOVE
);
440 stockSubMenu
->Append(wxID_REPLACE
);
441 stockSubMenu
->Append(wxID_REVERT_TO_SAVED
);
442 stockSubMenu
->Append(wxID_SAVE
);
443 stockSubMenu
->Append(wxID_SAVEAS
);
444 stockSubMenu
->Append(wxID_SELECT_COLOR
);
445 stockSubMenu
->Append(wxID_SELECT_FONT
);
446 stockSubMenu
->Append(wxID_SORT_ASCENDING
);
447 stockSubMenu
->Append(wxID_SORT_DESCENDING
);
448 stockSubMenu
->Append(wxID_SPELL_CHECK
);
449 stockSubMenu
->Append(wxID_STOP
);
450 stockSubMenu
->Append(wxID_STRIKETHROUGH
);
451 stockSubMenu
->Append(wxID_TOP
);
452 stockSubMenu
->Append(wxID_UNDELETE
);
453 stockSubMenu
->Append(wxID_UNDERLINE
);
454 stockSubMenu
->Append(wxID_UNDO
);
455 stockSubMenu
->Append(wxID_UNINDENT
);
456 stockSubMenu
->Append(wxID_UP
);
457 stockSubMenu
->Append(wxID_YES
);
458 stockSubMenu
->Append(wxID_ZOOM_100
);
459 stockSubMenu
->Append(wxID_ZOOM_FIT
);
460 stockSubMenu
->Append(wxID_ZOOM_IN
);
461 stockSubMenu
->Append(wxID_ZOOM_OUT
);
462 fileMenu
->AppendSubMenu(stockSubMenu
, wxT("&Standard items demo"));
465 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
466 wxT("Clear &log\tCtrl-L"));
467 item
->SetBitmap(copy_xpm
);
468 fileMenu
->Append(item
);
469 fileMenu
->AppendSeparator();
470 #endif // USE_LOG_WINDOW
472 fileMenu
->Append(Menu_File_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit menu sample"));
474 wxMenu
*menubarMenu
= new wxMenu
;
475 menubarMenu
->Append(Menu_MenuBar_Append
, wxT("&Append menu\tCtrl-A"),
476 wxT("Append a menu to the menubar"));
477 menubarMenu
->Append(Menu_MenuBar_Insert
, wxT("&Insert menu\tCtrl-I"),
478 wxT("Insert a menu into the menubar"));
479 menubarMenu
->Append(Menu_MenuBar_Delete
, wxT("&Delete menu\tCtrl-D"),
480 wxT("Delete the last menu from the menubar"));
481 menubarMenu
->Append(Menu_MenuBar_Toggle
, wxT("&Toggle menu\tCtrl-T"),
482 wxT("Toggle the first menu in the menubar"), true);
483 menubarMenu
->AppendSeparator();
484 menubarMenu
->Append(Menu_MenuBar_Enable
, wxT("&Enable menu\tCtrl-E"),
485 wxT("Enable or disable the last menu"), true);
486 menubarMenu
->AppendSeparator();
487 menubarMenu
->Append(Menu_MenuBar_GetLabel
, wxT("&Get menu label\tCtrl-G"),
488 wxT("Get the label of the last menu"));
490 menubarMenu
->Append(Menu_MenuBar_SetLabel
, wxT("&Set menu label\tCtrl-S"),
491 wxT("Change the label of the last menu"));
492 menubarMenu
->AppendSeparator();
493 menubarMenu
->Append(Menu_MenuBar_FindMenu
, wxT("&Find menu from label\tCtrl-F"),
494 wxT("Find a menu by searching for its label"));
497 wxMenu
* subMenu
= new wxMenu
;
498 subMenu
->Append(Menu_SubMenu_Normal
, wxT("&Normal submenu item"), wxT("Disabled submenu item"));
499 subMenu
->AppendCheckItem(Menu_SubMenu_Check
, wxT("&Check submenu item"), wxT("Check submenu item"));
500 subMenu
->AppendRadioItem(Menu_SubMenu_Radio1
, wxT("Radio item &1"), wxT("Radio item"));
501 subMenu
->AppendRadioItem(Menu_SubMenu_Radio2
, wxT("Radio item &2"), wxT("Radio item"));
502 subMenu
->AppendRadioItem(Menu_SubMenu_Radio3
, wxT("Radio item &3"), wxT("Radio item"));
504 menubarMenu
->Append(Menu_SubMenu
, wxT("Submenu"), subMenu
);
506 wxMenu
*menuMenu
= new wxMenu
;
507 menuMenu
->Append(Menu_Menu_Append
, wxT("&Append menu item\tAlt-A"),
508 wxT("Append a menu item to the 'Test' menu"));
509 menuMenu
->Append(Menu_Menu_AppendSub
, wxT("&Append sub menu\tAlt-S"),
510 wxT("Append a sub menu to the 'Test' menu"));
511 menuMenu
->Append(Menu_Menu_Insert
, wxT("&Insert menu item\tAlt-I"),
512 wxT("Insert a menu item in head of the 'Test' menu"));
513 menuMenu
->Append(Menu_Menu_Delete
, wxT("&Delete menu item\tAlt-D"),
514 wxT("Delete the last menu item from the 'Test' menu"));
515 menuMenu
->Append(Menu_Menu_DeleteSub
, wxT("Delete last &submenu\tAlt-K"),
516 wxT("Delete the last submenu from the 'Test' menu"));
517 menuMenu
->AppendSeparator();
518 menuMenu
->Append(Menu_Menu_Enable
, wxT("&Enable menu item\tAlt-E"),
519 wxT("Enable or disable the last menu item"), true);
520 menuMenu
->Append(Menu_Menu_Check
, wxT("&Check menu item\tAlt-C"),
521 wxT("Check or uncheck the last menu item"), true);
522 menuMenu
->AppendSeparator();
523 menuMenu
->Append(Menu_Menu_GetInfo
, wxT("Get menu item in&fo\tAlt-F"),
524 wxT("Show the state of the last menu item"));
526 menuMenu
->Append(Menu_Menu_SetLabel
, wxT("Set menu item label\tAlt-L"),
527 wxT("Set the label of a menu item"));
530 menuMenu
->AppendSeparator();
531 menuMenu
->Append(Menu_Menu_FindItem
, wxT("Find menu item from label"),
532 wxT("Find a menu item by searching for its label"));
535 wxMenu
*testMenu
= new wxMenu
;
536 testMenu
->Append(Menu_Test_Normal
, wxT("&Normal item"));
537 testMenu
->AppendSeparator();
538 testMenu
->AppendCheckItem(Menu_Test_Check
, wxT("&Check item"));
539 testMenu
->AppendSeparator();
540 testMenu
->AppendRadioItem(Menu_Test_Radio1
, wxT("Radio item &1"));
541 testMenu
->AppendRadioItem(Menu_Test_Radio2
, wxT("Radio item &2"));
542 testMenu
->AppendRadioItem(Menu_Test_Radio3
, wxT("Radio item &3"));
544 wxMenu
*helpMenu
= new wxMenu
;
545 helpMenu
->Append(Menu_Help_About
, wxT("&About\tF1"), wxT("About menu sample"));
547 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
549 menuBar
->Append(fileMenu
, wxT("&File"));
550 menuBar
->Append(menubarMenu
, wxT("Menu&bar"));
551 menuBar
->Append(menuMenu
, wxT("&Menu"));
552 menuBar
->Append(testMenu
, wxT("&Test"));
553 menuBar
->Append(helpMenu
, wxT("&Help"));
555 // these items should be initially checked
556 menuBar
->Check(Menu_MenuBar_Toggle
, true);
557 menuBar
->Check(Menu_MenuBar_Enable
, true);
558 menuBar
->Check(Menu_Menu_Enable
, true);
559 menuBar
->Check(Menu_Menu_Check
, false);
561 // associate the menu bar with the frame
564 // intercept all menu events and log them in this custom event handler
565 PushEventHandler(new MyEvtHandler(this));
568 // create the log text window
569 m_textctrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
570 wxDefaultPosition
, wxDefaultSize
,
572 m_textctrl
->SetEditable(false);
574 wxLog::DisableTimestamp();
575 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
577 wxLogMessage(wxT("Brief explanations: the commands in the \"Menu\" menu ")
578 wxT("append/insert/delete items to/from the \"Test\" menu.\n")
579 wxT("The commands in the \"Menubar\" menu work with the ")
580 wxT("menubar itself.\n\n")
581 wxT("Right click the band below to test popup menus.\n"));
592 // delete the event handler installed in ctor
593 PopEventHandler(true);
596 // restore old logger
597 delete wxLog::SetActiveTarget(m_logOld
);
601 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
603 wxMenu
*menu
= new wxMenu
;
604 menu
->Append(Menu_Dummy_First
, wxT("&First item\tCtrl-F1"));
605 menu
->AppendSeparator();
606 menu
->AppendCheckItem(Menu_Dummy_Second
, wxT("&Second item\tCtrl-F2"));
610 title
->Printf(wxT("Dummy menu &%u"), (unsigned)++m_countDummy
);
616 wxMenuItem
*MyFrame::GetLastMenuItem() const
618 wxMenuBar
*menubar
= GetMenuBar();
619 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
620 wxCHECK_MSG( menu
, NULL
, wxT("no 'Test' menu?") );
622 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
625 wxLogWarning(wxT("No last item in the 'Test' menu!"));
631 return node
->GetData();
635 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
637 int id
= event
.GetId();
639 wxString msg
= wxString::Format(wxT("Menu command %d"), id
);
641 // catch all checkable menubar items and also the check item from the popup
643 wxMenuItem
*item
= GetMenuBar()->FindItem(id
);
644 if ( (item
&& item
->IsCheckable()) || id
== Menu_Popup_ToBeChecked
)
646 msg
+= wxString::Format(wxT(" (the item is currently %schecked)"),
647 event
.IsChecked() ? wxT("") : wxT("not "));
653 // ----------------------------------------------------------------------------
655 // ----------------------------------------------------------------------------
657 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
664 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
669 void MyFrame::OnClearLogUpdateUI(wxUpdateUIEvent
& event
)
671 // if we only enable this item when the log window is empty, we never see
672 // it in the disable state as a message is logged whenever the menu is
673 // opened, so we disable it if there is not "much" text in the window
674 event
.Enable( m_textctrl
->GetNumberOfLines() > 5 );
677 #endif // USE_LOG_WINDOW
679 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
681 (void)wxMessageBox(wxT("wxWidgets menu sample\n(c) 1999-2001 Vadim Zeitlin"),
682 wxT("About wxWidgets menu sample"),
683 wxOK
| wxICON_INFORMATION
);
686 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
688 wxMenuBar
*mbar
= GetMenuBar();
690 size_t count
= mbar
->GetMenuCount();
693 // don't let delete the first 4 menus
694 wxLogError(wxT("Can't delete any more menus"));
698 delete mbar
->Remove(count
- 1);
702 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
705 wxMenu
*menu
= CreateDummyMenu(&title
);
706 // Insert before the 'Help' menu
707 // Otherwise repeated Deletes will remove the 'Test' menu
708 GetMenuBar()->Insert(4, menu
, title
);
711 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
714 wxMenu
*menu
= CreateDummyMenu(&title
);
715 GetMenuBar()->Append(menu
, title
);
718 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
720 wxMenuBar
*mbar
= GetMenuBar();
724 m_menu
= mbar
->Remove(0);
729 mbar
->Insert(0, m_menu
, wxT("&File"));
734 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
736 wxMenuBar
*mbar
= GetMenuBar();
737 size_t count
= mbar
->GetMenuCount();
739 mbar
->EnableTop(count
- 1, event
.IsChecked());
742 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
744 wxMenuBar
*mbar
= GetMenuBar();
745 size_t count
= mbar
->GetMenuCount();
747 wxCHECK_RET( count
, wxT("no last menu?") );
749 wxLogMessage(wxT("The label of the last menu item is '%s'"),
750 mbar
->GetMenuLabel(count
- 1).c_str());
754 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
756 wxMenuBar
*mbar
= GetMenuBar();
757 size_t count
= mbar
->GetMenuCount();
759 wxCHECK_RET( count
, wxT("no last menu?") );
761 wxString label
= wxGetTextFromUser
763 wxT("Enter new label: "),
764 wxT("Change last menu text"),
765 mbar
->GetMenuLabel(count
- 1),
769 if ( !label
.empty() )
771 mbar
->SetMenuLabel(count
- 1, label
);
775 void MyFrame::OnFindMenu(wxCommandEvent
& WXUNUSED(event
))
777 wxMenuBar
*mbar
= GetMenuBar();
778 size_t count
= mbar
->GetMenuCount();
780 wxCHECK_RET( count
, wxT("no last menu?") );
782 wxString label
= wxGetTextFromUser
784 wxT("Enter label to search for: "),
790 if ( !label
.empty() )
792 int index
= mbar
->FindMenu(label
);
794 if (index
== wxNOT_FOUND
)
796 wxLogWarning(wxT("No menu with label '%s'"), label
.c_str());
800 wxLogMessage(wxT("Menu %d has label '%s'"), index
, label
.c_str());
806 void MyFrame::OnDummy(wxCommandEvent
& event
)
808 wxLogMessage(wxT("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
811 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
813 wxMenuBar
*menubar
= GetMenuBar();
814 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
815 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
817 menu
->AppendSeparator();
818 menu
->Append(Menu_Dummy_Third
, wxT("&Third dummy item\tCtrl-F3"),
819 wxT("Checkable item"), true);
822 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
824 wxMenuBar
*menubar
= GetMenuBar();
825 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
826 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
828 menu
->Append(Menu_Dummy_Last
, wxT("&Dummy sub menu"),
829 CreateDummyMenu(NULL
), wxT("Dummy sub menu help"));
832 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
834 wxMenuBar
*menubar
= GetMenuBar();
835 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
836 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
838 size_t count
= menu
->GetMenuItemCount();
841 wxLogWarning(wxT("No items to delete!"));
845 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
849 void MyFrame::OnDeleteSubMenu(wxCommandEvent
& WXUNUSED(event
))
851 wxMenuBar
*menubar
= GetMenuBar();
852 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
853 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
855 for ( int n
= menu
->GetMenuItemCount() - 1; n
>=0 ; --n
)
857 wxMenuItem
* item
= menu
->FindItemByPosition(n
);
858 if (item
->IsSubMenu())
865 wxLogWarning(wxT("No submenu to delete!"));
868 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
870 wxMenuBar
*menubar
= GetMenuBar();
871 wxMenu
*menu
= menubar
->GetMenu(menubar
->FindMenu("Test"));
872 wxCHECK_RET( menu
, wxT("no 'Test' menu?") );
874 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
875 wxT("Fourth dummy item\tCtrl-F4")));
876 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
));
879 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
881 wxMenuItem
*item
= GetLastMenuItem();
885 item
->Enable(!item
->IsEnabled());
889 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
891 wxMenuItem
*item
= GetLastMenuItem();
893 if (item
&& item
->IsCheckable())
899 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
903 wxMenuItem
*item
= GetLastMenuItem();
905 event
.Enable(item
&& item
->IsCheckable());
908 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
910 wxMenuItem
*item
= GetLastMenuItem();
914 wxString label
= item
->GetItemLabel();
915 wxLogMessage(wxT("The label of the last menu item is '%s'"),
921 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
923 wxMenuItem
*item
= GetLastMenuItem();
927 wxString label
= wxGetTextFromUser
929 wxT("Enter new label: "),
930 wxT("Change last menu item text"),
931 item
->GetItemLabel(),
934 label
.Replace( wxT("\\t"), wxT("\t") );
936 if ( !label
.empty() )
938 item
->SetItemLabel(label
);
944 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
946 wxMenuItem
*item
= GetLastMenuItem();
951 msg
<< wxT("The item is ") << (item
->IsEnabled() ? wxT("enabled")
955 if ( item
->IsCheckable() )
957 msg
<< wxT("It is checkable and ") << (item
->IsChecked() ? wxT("") : wxT("un"))
962 wxAcceleratorEntry
*accel
= item
->GetAccel();
965 msg
<< wxT("Its accelerator is ");
967 int flags
= accel
->GetFlags();
968 if ( flags
& wxACCEL_ALT
)
970 if ( flags
& wxACCEL_CTRL
)
972 if ( flags
& wxACCEL_SHIFT
)
973 msg
<< wxT("Shift-");
975 int code
= accel
->GetKeyCode();
990 msg
<< wxT('F') << code
- WXK_F1
+ 1;
993 // if there are any other keys wxGetAccelFromString() may return,
994 // we should process them here
997 if ( wxIsalnum(code
) )
1004 wxFAIL_MSG( wxT("unknown keyboard accel") );
1011 msg
<< wxT("It doesn't have an accelerator");
1013 #endif // wxUSE_ACCEL
1020 void MyFrame::OnFindMenuItem(wxCommandEvent
& WXUNUSED(event
))
1022 wxMenuBar
*mbar
= GetMenuBar();
1023 size_t count
= mbar
->GetMenuCount();
1025 wxCHECK_RET( count
, wxT("no last menu?") );
1027 wxString label
= wxGetTextFromUser
1029 wxT("Enter label to search for: "),
1030 wxT("Find menu item"),
1035 if ( !label
.empty() )
1038 int index
= wxNOT_FOUND
;
1040 for (menuindex
= 0; (menuindex
< count
) && (index
== wxNOT_FOUND
); ++menuindex
)
1042 index
= mbar
->FindMenuItem(mbar
->GetMenu(menuindex
)->GetTitle(), label
);
1044 if (index
== wxNOT_FOUND
)
1046 wxLogWarning(wxT("No menu item with label '%s'"), label
.c_str());
1050 wxLogMessage(wxT("Menu item %d in menu %lu has label '%s'"),
1051 index
, (unsigned long)menuindex
, label
.c_str());
1057 void MyFrame::ShowContextMenu(const wxPoint
& pos
)
1061 if ( wxGetKeyState(WXK_SHIFT
) )
1063 // when Shift is pressed, demonstrate the use of a simple function
1064 // returning the id of the item selected in the popup menu
1065 menu
.SetTitle("Choose one of:");
1066 static const char *choices
[] = { "Apple", "Banana", "Cherry" };
1067 for ( size_t n
= 0; n
< WXSIZEOF(choices
); n
++ )
1068 menu
.Append(Menu_PopupChoice
+ n
, choices
[n
]);
1070 const int rc
= GetPopupMenuSelectionFromUser(menu
, pos
);
1071 if ( rc
== wxID_NONE
)
1073 wxLogMessage("No selection");
1077 wxLogMessage("You have selected \"%s\"",
1078 choices
[rc
- Menu_PopupChoice
]);
1081 else // normal case, shift not pressed
1083 menu
.Append(Menu_Help_About
, wxT("&About"));
1084 menu
.Append(Menu_Popup_Submenu
, wxT("&Submenu"), CreateDummyMenu(NULL
));
1085 menu
.Append(Menu_Popup_ToBeDeleted
, wxT("To be &deleted"));
1086 menu
.AppendCheckItem(Menu_Popup_ToBeChecked
, wxT("To be &checked"));
1087 menu
.Append(Menu_Popup_ToBeGreyed
, wxT("To be &greyed"),
1088 wxT("This menu item should be initially greyed out"));
1089 menu
.AppendSeparator();
1090 menu
.Append(Menu_File_Quit
, wxT("E&xit"));
1092 menu
.Delete(Menu_Popup_ToBeDeleted
);
1093 menu
.Check(Menu_Popup_ToBeChecked
, true);
1094 menu
.Enable(Menu_Popup_ToBeGreyed
, false);
1096 PopupMenu(&menu
, pos
);
1098 // test for destroying items in popup menus
1099 #if 0 // doesn't work in wxGTK!
1100 menu
.Destroy(Menu_Popup_Submenu
);
1102 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
1107 void MyFrame::OnTestNormal(wxCommandEvent
& WXUNUSED(event
))
1109 wxLogMessage(wxT("Normal item selected"));
1112 void MyFrame::OnTestCheck(wxCommandEvent
& event
)
1114 wxLogMessage(wxT("Check item %schecked"),
1115 event
.IsChecked() ? wxT("") : wxT("un"));
1118 void MyFrame::OnTestRadio(wxCommandEvent
& event
)
1120 wxLogMessage(wxT("Radio item %d selected"),
1121 event
.GetId() - Menu_Test_Radio1
+ 1);
1125 void MyFrame::LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
)
1129 << ( event
.IsPopup() ? wxT("popup ") : wxT("") )
1130 << wxT("menu has been ")
1134 wxLogStatus(this, msg
.c_str());
1138 void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
)
1140 event
.Enable(false);
1143 void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
)
1148 void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
)
1150 int which
= (event
.GetId() - Menu_SubMenu_Radio1
+ 1);
1157 #if USE_CONTEXT_MENU
1158 void MyFrame::OnContextMenu(wxContextMenuEvent
& event
)
1160 wxPoint point
= event
.GetPosition();
1162 if (point
.x
== -1 && point
.y
== -1) {
1163 wxSize size
= GetSize();
1164 point
.x
= size
.x
/ 2;
1165 point
.y
= size
.y
/ 2;
1167 point
= ScreenToClient(point
);
1169 ShowContextMenu(point
);
1173 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
1179 // leave a band below for popup menu testing
1180 wxSize size
= GetClientSize();
1181 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
1184 // this is really ugly but we have to do it as we can't just call
1185 // event.Skip() because wxFrameBase would make the text control fill the
1186 // entire frame then
1187 #ifdef __WXUNIVERSAL__
1189 #endif // __WXUNIVERSAL__