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__) || defined(__WXMGL__)
45 #define USE_CONTEXT_MENU 0
47 #define USE_CONTEXT_MENU 1
50 // this sample is usefull when new port is developed
51 // and usually new port has majority of flags turned off
52 #if wxUSE_LOG && wxUSE_TEXTCTRL
53 #define USE_LOG_WINDOW 1
55 #define USE_LOG_WINDOW 0
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 OnInsertMenuItem(wxCommandEvent
& event
);
102 void OnCheckMenuItem(wxCommandEvent
& event
);
103 void OnEnableMenuItem(wxCommandEvent
& event
);
104 void OnGetLabelMenuItem(wxCommandEvent
& event
);
106 void OnSetLabelMenuItem(wxCommandEvent
& event
);
108 void OnGetMenuItemInfo(wxCommandEvent
& event
);
110 void OnFindMenuItem(wxCommandEvent
& event
);
113 void OnAppendMenu(wxCommandEvent
& event
);
114 void OnInsertMenu(wxCommandEvent
& event
);
115 void OnDeleteMenu(wxCommandEvent
& event
);
116 void OnToggleMenu(wxCommandEvent
& event
);
117 void OnEnableMenu(wxCommandEvent
& event
);
118 void OnGetLabelMenu(wxCommandEvent
& event
);
119 void OnSetLabelMenu(wxCommandEvent
& event
);
121 void OnFindMenu(wxCommandEvent
& event
);
124 void OnTestNormal(wxCommandEvent
& event
);
125 void OnTestCheck(wxCommandEvent
& event
);
126 void OnTestRadio(wxCommandEvent
& event
);
128 void OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
);
129 void OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
);
130 void OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
);
133 void OnContextMenu(wxContextMenuEvent
& event
);
135 void OnRightUp(wxMouseEvent
& event
)
136 { ShowContextMenu(event
.GetPosition()); }
139 void OnMenuOpen(wxMenuEvent
& event
)
142 LogMenuOpenOrClose(event
, _T("opened")); event
.Skip();
145 void OnMenuClose(wxMenuEvent
& event
)
148 LogMenuOpenOrClose(event
, _T("closed")); event
.Skip();
152 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
154 void OnSize(wxSizeEvent
& event
);
157 void LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
);
158 void ShowContextMenu(const wxPoint
& pos
);
160 wxMenu
*CreateDummyMenu(wxString
*title
);
162 wxMenuItem
*GetLastMenuItem() const;
164 // the menu previously detached from the menubar (may be NULL)
167 // the count of dummy menus already created
171 // the control used for logging
172 wxTextCtrl
*m_textctrl
;
175 // the previous log target
178 DECLARE_EVENT_TABLE()
181 // A small helper class which intercepts all menu events and logs them
182 class MyEvtHandler
: public wxEvtHandler
185 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
187 void OnMenuEvent(wxCommandEvent
& event
)
189 m_frame
->LogMenuEvent(event
);
197 DECLARE_EVENT_TABLE()
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
206 Menu_File_Quit
= wxID_EXIT
,
208 Menu_File_ClearLog
= 100,
211 Menu_MenuBar_Toggle
= 200,
216 Menu_MenuBar_GetLabel
,
218 Menu_MenuBar_SetLabel
,
219 Menu_MenuBar_FindMenu
,
222 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_Enable
, MyFrame::OnEnableMenuItem
)
297 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
298 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
300 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
302 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
304 EVT_MENU(Menu_Menu_FindItem
, MyFrame::OnFindMenuItem
)
307 EVT_MENU(Menu_Test_Normal
, MyFrame::OnTestNormal
)
308 EVT_MENU(Menu_Test_Check
, MyFrame::OnTestCheck
)
309 EVT_MENU(Menu_Test_Radio1
, MyFrame::OnTestRadio
)
310 EVT_MENU(Menu_Test_Radio2
, MyFrame::OnTestRadio
)
311 EVT_MENU(Menu_Test_Radio3
, MyFrame::OnTestRadio
)
313 EVT_UPDATE_UI(Menu_SubMenu_Normal
, MyFrame::OnUpdateSubMenuNormal
)
314 EVT_UPDATE_UI(Menu_SubMenu_Check
, MyFrame::OnUpdateSubMenuCheck
)
315 EVT_UPDATE_UI(Menu_SubMenu_Radio1
, MyFrame::OnUpdateSubMenuRadio
)
316 EVT_UPDATE_UI(Menu_SubMenu_Radio2
, MyFrame::OnUpdateSubMenuRadio
)
317 EVT_UPDATE_UI(Menu_SubMenu_Radio3
, MyFrame::OnUpdateSubMenuRadio
)
319 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
321 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
324 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
326 EVT_RIGHT_UP(MyFrame::OnRightUp
)
329 EVT_MENU_OPEN(MyFrame::OnMenuOpen
)
330 EVT_MENU_CLOSE(MyFrame::OnMenuClose
)
332 EVT_SIZE(MyFrame::OnSize
)
335 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
336 EVT_MENU(wxID_ANY
, MyEvtHandler::OnMenuEvent
)
339 // ============================================================================
341 // ============================================================================
343 // ----------------------------------------------------------------------------
345 // ----------------------------------------------------------------------------
349 // The `main program' equivalent, creating the windows and returning the
353 if ( !wxApp::OnInit() )
356 // Create the main frame window
357 MyFrame
* frame
= new MyFrame
;
362 frame
->SetStatusText(_T("Welcome to wxWidgets menu sample"));
363 #endif // wxUSE_STATUSBAR
370 // ----------------------------------------------------------------------------
372 // ----------------------------------------------------------------------------
374 // Define my frame constructor
376 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, _T("wxWidgets menu sample"))
378 SetIcon(wxICON(sample
));
389 #endif // wxUSE_STATUSBAR
391 // create the menubar
392 wxMenu
*fileMenu
= new wxMenu
;
394 wxMenu
*stockSubMenu
= new wxMenu
;
395 stockSubMenu
->Append(wxID_ADD
);
396 stockSubMenu
->Append(wxID_APPLY
);
397 stockSubMenu
->Append(wxID_BACKWARD
);
398 stockSubMenu
->Append(wxID_BOLD
);
399 stockSubMenu
->Append(wxID_BOTTOM
);
400 stockSubMenu
->Append(wxID_CANCEL
);
401 stockSubMenu
->Append(wxID_CDROM
);
402 stockSubMenu
->Append(wxID_CLEAR
);
403 stockSubMenu
->Append(wxID_CLOSE
);
404 stockSubMenu
->Append(wxID_CONVERT
);
405 stockSubMenu
->Append(wxID_COPY
);
406 stockSubMenu
->Append(wxID_CUT
);
407 stockSubMenu
->Append(wxID_DELETE
);
408 stockSubMenu
->Append(wxID_DOWN
);
409 stockSubMenu
->Append(wxID_EXECUTE
);
410 stockSubMenu
->Append(wxID_EXIT
);
411 stockSubMenu
->Append(wxID_FIND
);
412 stockSubMenu
->Append(wxID_FIRST
);
413 stockSubMenu
->Append(wxID_FLOPPY
);
414 stockSubMenu
->Append(wxID_FORWARD
);
415 stockSubMenu
->Append(wxID_HARDDISK
);
416 stockSubMenu
->Append(wxID_HELP
);
417 stockSubMenu
->Append(wxID_HOME
);
418 stockSubMenu
->Append(wxID_INDENT
);
419 stockSubMenu
->Append(wxID_INDEX
);
420 stockSubMenu
->Append(wxID_INFO
);
421 stockSubMenu
->Append(wxID_ITALIC
);
422 stockSubMenu
->Append(wxID_JUMP_TO
);
423 stockSubMenu
->Append(wxID_JUSTIFY_CENTER
);
424 stockSubMenu
->Append(wxID_JUSTIFY_FILL
);
425 stockSubMenu
->Append(wxID_JUSTIFY_LEFT
);
426 stockSubMenu
->Append(wxID_JUSTIFY_RIGHT
);
427 stockSubMenu
->Append(wxID_LAST
);
428 stockSubMenu
->Append(wxID_NETWORK
);
429 stockSubMenu
->Append(wxID_NEW
);
430 stockSubMenu
->Append(wxID_NO
);
431 stockSubMenu
->Append(wxID_OK
);
432 stockSubMenu
->Append(wxID_OPEN
);
433 stockSubMenu
->Append(wxID_PASTE
);
434 stockSubMenu
->Append(wxID_PREFERENCES
);
435 stockSubMenu
->Append(wxID_PREVIEW
);
436 stockSubMenu
->Append(wxID_PRINT
);
437 stockSubMenu
->Append(wxID_PROPERTIES
);
438 stockSubMenu
->Append(wxID_REDO
);
439 stockSubMenu
->Append(wxID_REFRESH
);
440 stockSubMenu
->Append(wxID_REMOVE
);
441 stockSubMenu
->Append(wxID_REPLACE
);
442 stockSubMenu
->Append(wxID_REVERT_TO_SAVED
);
443 stockSubMenu
->Append(wxID_SAVE
);
444 stockSubMenu
->Append(wxID_SAVEAS
);
445 stockSubMenu
->Append(wxID_SELECT_COLOR
);
446 stockSubMenu
->Append(wxID_SELECT_FONT
);
447 stockSubMenu
->Append(wxID_SORT_ASCENDING
);
448 stockSubMenu
->Append(wxID_SORT_DESCENDING
);
449 stockSubMenu
->Append(wxID_SPELL_CHECK
);
450 stockSubMenu
->Append(wxID_STOP
);
451 stockSubMenu
->Append(wxID_STRIKETHROUGH
);
452 stockSubMenu
->Append(wxID_TOP
);
453 stockSubMenu
->Append(wxID_UNDELETE
);
454 stockSubMenu
->Append(wxID_UNDERLINE
);
455 stockSubMenu
->Append(wxID_UNDO
);
456 stockSubMenu
->Append(wxID_UNINDENT
);
457 stockSubMenu
->Append(wxID_UP
);
458 stockSubMenu
->Append(wxID_YES
);
459 stockSubMenu
->Append(wxID_ZOOM_100
);
460 stockSubMenu
->Append(wxID_ZOOM_FIT
);
461 stockSubMenu
->Append(wxID_ZOOM_IN
);
462 stockSubMenu
->Append(wxID_ZOOM_OUT
);
463 fileMenu
->AppendSubMenu(stockSubMenu
, _T("&Standard items demo"));
466 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
467 _T("Clear &log\tCtrl-L"));
468 #if wxUSE_OWNER_DRAWN || defined(__WXGTK__)
469 item
->SetBitmap(copy_xpm
);
471 fileMenu
->Append(item
);
472 fileMenu
->AppendSeparator();
473 #endif // USE_LOG_WINDOW
475 fileMenu
->Append(Menu_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit menu sample"));
477 wxMenu
*menubarMenu
= new wxMenu
;
478 menubarMenu
->Append(Menu_MenuBar_Append
, _T("&Append menu\tCtrl-A"),
479 _T("Append a menu to the menubar"));
480 menubarMenu
->Append(Menu_MenuBar_Insert
, _T("&Insert menu\tCtrl-I"),
481 _T("Insert a menu into the menubar"));
482 menubarMenu
->Append(Menu_MenuBar_Delete
, _T("&Delete menu\tCtrl-D"),
483 _T("Delete the last menu from the menubar"));
484 menubarMenu
->Append(Menu_MenuBar_Toggle
, _T("&Toggle menu\tCtrl-T"),
485 _T("Toggle the first menu in the menubar"), true);
486 menubarMenu
->AppendSeparator();
487 menubarMenu
->Append(Menu_MenuBar_Enable
, _T("&Enable menu\tCtrl-E"),
488 _T("Enable or disable the last menu"), true);
489 menubarMenu
->AppendSeparator();
490 menubarMenu
->Append(Menu_MenuBar_GetLabel
, _T("&Get menu label\tCtrl-G"),
491 _T("Get the label of the last menu"));
493 menubarMenu
->Append(Menu_MenuBar_SetLabel
, _T("&Set menu label\tCtrl-S"),
494 _T("Change the label of the last menu"));
495 menubarMenu
->AppendSeparator();
496 menubarMenu
->Append(Menu_MenuBar_FindMenu
, _T("&Find menu from label\tCtrl-F"),
497 _T("Find a menu by searching for its label"));
500 wxMenu
* subMenu
= new wxMenu
;
501 subMenu
->Append(Menu_SubMenu_Normal
, _T("&Normal submenu item"), _T("Disabled submenu item"));
502 subMenu
->AppendCheckItem(Menu_SubMenu_Check
, _T("&Check submenu item"), _T("Check submenu item"));
503 subMenu
->AppendRadioItem(Menu_SubMenu_Radio1
, _T("Radio item &1"), _T("Radio item"));
504 subMenu
->AppendRadioItem(Menu_SubMenu_Radio2
, _T("Radio item &2"), _T("Radio item"));
505 subMenu
->AppendRadioItem(Menu_SubMenu_Radio3
, _T("Radio item &3"), _T("Radio item"));
507 menubarMenu
->Append(Menu_SubMenu
, _T("Submenu"), subMenu
);
509 wxMenu
*menuMenu
= new wxMenu
;
510 menuMenu
->Append(Menu_Menu_Append
, _T("&Append menu item\tAlt-A"),
511 _T("Append a menu item to the last menu"));
512 menuMenu
->Append(Menu_Menu_AppendSub
, _T("&Append sub menu\tAlt-S"),
513 _T("Append a sub menu to the last menu"));
514 menuMenu
->Append(Menu_Menu_Insert
, _T("&Insert menu item\tAlt-I"),
515 _T("Insert a menu item in head of the last menu"));
516 menuMenu
->Append(Menu_Menu_Delete
, _T("&Delete menu item\tAlt-D"),
517 _T("Delete the last menu item from the last menu"));
518 menuMenu
->AppendSeparator();
519 menuMenu
->Append(Menu_Menu_Enable
, _T("&Enable menu item\tAlt-E"),
520 _T("Enable or disable the last menu item"), true);
521 menuMenu
->Append(Menu_Menu_Check
, _T("&Check menu item\tAlt-C"),
522 _T("Check or uncheck the last menu item"), true);
523 menuMenu
->AppendSeparator();
524 menuMenu
->Append(Menu_Menu_GetInfo
, _T("Get menu item in&fo\tAlt-F"),
525 _T("Show the state of the last menu item"));
527 menuMenu
->Append(Menu_Menu_SetLabel
, _T("Set menu item label\tAlt-L"),
528 _T("Set the label of a menu item"));
531 menuMenu
->AppendSeparator();
532 menuMenu
->Append(Menu_Menu_FindItem
, _T("Find menu item from label"),
533 _T("Find a menu item by searching for its label"));
536 wxMenu
*testMenu
= new wxMenu
;
537 testMenu
->Append(Menu_Test_Normal
, _T("&Normal item"));
538 testMenu
->AppendSeparator();
539 testMenu
->AppendCheckItem(Menu_Test_Check
, _T("&Check item"));
540 testMenu
->AppendSeparator();
541 testMenu
->AppendRadioItem(Menu_Test_Radio1
, _T("Radio item &1"));
542 testMenu
->AppendRadioItem(Menu_Test_Radio2
, _T("Radio item &2"));
543 testMenu
->AppendRadioItem(Menu_Test_Radio3
, _T("Radio item &3"));
545 wxMenu
*helpMenu
= new wxMenu
;
546 helpMenu
->Append(Menu_Help_About
, _T("&About\tF1"), _T("About menu sample"));
548 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
550 menuBar
->Append(fileMenu
, _T("&File"));
551 menuBar
->Append(menubarMenu
, _T("Menu&bar"));
552 menuBar
->Append(menuMenu
, _T("&Menu"));
553 menuBar
->Append(testMenu
, _T("&Test"));
554 menuBar
->Append(helpMenu
, _T("&Help"));
556 // these items should be initially checked
557 menuBar
->Check(Menu_MenuBar_Toggle
, true);
558 menuBar
->Check(Menu_MenuBar_Enable
, true);
559 menuBar
->Check(Menu_Menu_Enable
, true);
560 menuBar
->Check(Menu_Menu_Check
, false);
562 // associate the menu bar with the frame
565 // intercept all menu events and log them in this custom event handler
566 PushEventHandler(new MyEvtHandler(this));
569 // create the log text window
570 m_textctrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
571 wxDefaultPosition
, wxDefaultSize
,
573 m_textctrl
->SetEditable(false);
575 wxLog::DisableTimestamp();
576 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
578 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
579 _T("append/insert/delete items to/from the last menu.\n")
580 _T("The commands from \"Menubar\" menu work with the ")
581 _T("menubar itself.\n\n")
582 _T("Right click the band below to test popup menus.\n"));
593 // delete the event handler installed in ctor
594 PopEventHandler(true);
597 // restore old logger
598 delete wxLog::SetActiveTarget(m_logOld
);
602 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
604 wxMenu
*menu
= new wxMenu
;
605 menu
->Append(Menu_Dummy_First
, _T("&First item\tCtrl-F1"));
606 menu
->AppendSeparator();
607 menu
->AppendCheckItem(Menu_Dummy_Second
, _T("&Second item\tCtrl-F2"));
611 title
->Printf(_T("Dummy menu &%u"), (unsigned)++m_countDummy
);
617 wxMenuItem
*MyFrame::GetLastMenuItem() const
619 wxMenuBar
*menubar
= GetMenuBar();
620 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
622 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
625 wxLogWarning(_T("No last item in the last menu!"));
631 return node
->GetData();
635 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
637 int id
= event
.GetId();
639 wxString msg
= wxString::Format(_T("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(_T(" (the item is currently %schecked)"),
647 event
.IsChecked() ? _T("") : _T("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(_T("wxWidgets menu sample\n(c) 1999-2001 Vadim Zeitlin"),
682 _T("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 2 menus
694 wxLogError(_T("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 GetMenuBar()->Insert(0, menu
, title
);
709 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
712 wxMenu
*menu
= CreateDummyMenu(&title
);
713 GetMenuBar()->Append(menu
, title
);
716 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
718 wxMenuBar
*mbar
= GetMenuBar();
722 m_menu
= mbar
->Remove(0);
727 mbar
->Insert(0, m_menu
, _T("&File"));
732 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
734 wxMenuBar
*mbar
= GetMenuBar();
735 size_t count
= mbar
->GetMenuCount();
737 mbar
->EnableTop(count
- 1, event
.IsChecked());
740 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
742 wxMenuBar
*mbar
= GetMenuBar();
743 size_t count
= mbar
->GetMenuCount();
745 wxCHECK_RET( count
, _T("no last menu?") );
747 wxLogMessage(_T("The label of the last menu item is '%s'"),
748 mbar
->GetMenuLabel(count
- 1).c_str());
752 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
754 wxMenuBar
*mbar
= GetMenuBar();
755 size_t count
= mbar
->GetMenuCount();
757 wxCHECK_RET( count
, _T("no last menu?") );
759 wxString label
= wxGetTextFromUser
761 _T("Enter new label: "),
762 _T("Change last menu text"),
763 mbar
->GetMenuLabel(count
- 1),
767 if ( !label
.empty() )
769 mbar
->SetMenuLabel(count
- 1, label
);
773 void MyFrame::OnFindMenu(wxCommandEvent
& WXUNUSED(event
))
775 wxMenuBar
*mbar
= GetMenuBar();
776 size_t count
= mbar
->GetMenuCount();
778 wxCHECK_RET( count
, _T("no last menu?") );
780 wxString label
= wxGetTextFromUser
782 _T("Enter label to search for: "),
788 if ( !label
.empty() )
790 int index
= mbar
->FindMenu(label
);
792 if (index
== wxNOT_FOUND
)
794 wxLogWarning(_T("No menu with label '%s'"), label
.c_str());
798 wxLogMessage(_T("Menu %d has label '%s'"), index
, label
.c_str());
804 void MyFrame::OnDummy(wxCommandEvent
& event
)
806 wxLogMessage(_T("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
809 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
811 wxMenuBar
*menubar
= GetMenuBar();
812 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
814 menu
->AppendSeparator();
815 menu
->Append(Menu_Dummy_Third
, _T("&Third dummy item\tCtrl-F3"),
816 _T("Checkable item"), true);
819 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
821 wxMenuBar
*menubar
= GetMenuBar();
823 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 2);
825 menu
->Append(Menu_Dummy_Last
, _T("&Dummy sub menu"),
826 CreateDummyMenu(NULL
), _T("Dummy sub menu help"));
829 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
831 wxMenuBar
*menubar
= GetMenuBar();
832 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
834 size_t count
= menu
->GetMenuItemCount();
837 wxLogWarning(_T("No items to delete!"));
841 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
845 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
847 wxMenuBar
*menubar
= GetMenuBar();
848 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
850 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
851 _T("Fourth dummy item\tCtrl-F4")));
852 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
));
855 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
857 wxMenuItem
*item
= GetLastMenuItem();
861 item
->Enable(!item
->IsEnabled());
865 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
867 wxMenuItem
*item
= GetLastMenuItem();
872 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
876 wxMenuItem
*item
= GetLastMenuItem();
878 event
.Enable(item
&& item
->IsCheckable());
881 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
883 wxMenuItem
*item
= GetLastMenuItem();
887 wxString label
= item
->GetItemLabel();
888 wxLogMessage(_T("The label of the last menu item is '%s'"),
894 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
896 wxMenuItem
*item
= GetLastMenuItem();
900 wxString label
= wxGetTextFromUser
902 _T("Enter new label: "),
903 _T("Change last menu item text"),
904 item
->GetItemLabel(),
907 label
.Replace( _T("\\t"), _T("\t") );
909 if ( !label
.empty() )
911 item
->SetItemLabel(label
);
917 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
919 wxMenuItem
*item
= GetLastMenuItem();
924 msg
<< _T("The item is ") << (item
->IsEnabled() ? _T("enabled")
928 if ( item
->IsCheckable() )
930 msg
<< _T("It is checkable and ") << (item
->IsChecked() ? _T("") : _T("un"))
935 wxAcceleratorEntry
*accel
= item
->GetAccel();
938 msg
<< _T("Its accelerator is ");
940 int flags
= accel
->GetFlags();
941 if ( flags
& wxACCEL_ALT
)
943 if ( flags
& wxACCEL_CTRL
)
945 if ( flags
& wxACCEL_SHIFT
)
948 int code
= accel
->GetKeyCode();
963 msg
<< _T('F') << code
- WXK_F1
+ 1;
966 // if there are any other keys wxGetAccelFromString() may return,
967 // we should process them here
970 if ( wxIsalnum(code
) )
977 wxFAIL_MSG( _T("unknown keyboard accel") );
984 msg
<< _T("It doesn't have an accelerator");
986 #endif // wxUSE_ACCEL
993 void MyFrame::OnFindMenuItem(wxCommandEvent
& WXUNUSED(event
))
995 wxMenuBar
*mbar
= GetMenuBar();
996 size_t count
= mbar
->GetMenuCount();
998 wxCHECK_RET( count
, _T("no last menu?") );
1000 wxString label
= wxGetTextFromUser
1002 _T("Enter label to search for: "),
1003 _T("Find menu item"),
1008 if ( !label
.empty() )
1011 int index
= wxNOT_FOUND
;
1013 for (menuindex
= 0; (menuindex
< count
) && (index
== wxNOT_FOUND
); ++menuindex
)
1015 index
= mbar
->FindMenuItem(mbar
->GetMenu(menuindex
)->GetTitle(), label
);
1017 if (index
== wxNOT_FOUND
)
1019 wxLogWarning(_T("No menu item with label '%s'"), label
.c_str());
1023 wxLogMessage(_T("Menu item %d in menu %lu has label '%s'"),
1024 index
, (unsigned long)menuindex
, label
.c_str());
1030 void MyFrame::ShowContextMenu(const wxPoint
& pos
)
1034 if ( wxGetKeyState(WXK_SHIFT
) )
1036 // when Shift is pressed, demonstrate the use of a simple function
1037 // returning the id of the item selected in the popup menu
1038 menu
.SetTitle("Choose one of:");
1039 static const char *choices
[] = { "Apple", "Banana", "Cherry" };
1040 for ( size_t n
= 0; n
< WXSIZEOF(choices
); n
++ )
1041 menu
.Append(Menu_PopupChoice
+ n
, choices
[n
]);
1043 const int rc
= GetPopupMenuSelectionFromUser(menu
, pos
);
1044 if ( rc
== wxID_NONE
)
1046 wxLogMessage("No selection");
1050 wxLogMessage("You have selected \"%s\"",
1051 choices
[rc
- Menu_PopupChoice
]);
1054 else // normal case, shift not pressed
1056 menu
.Append(Menu_Help_About
, _T("&About"));
1057 menu
.Append(Menu_Popup_Submenu
, _T("&Submenu"), CreateDummyMenu(NULL
));
1058 menu
.Append(Menu_Popup_ToBeDeleted
, _T("To be &deleted"));
1059 menu
.AppendCheckItem(Menu_Popup_ToBeChecked
, _T("To be &checked"));
1060 menu
.Append(Menu_Popup_ToBeGreyed
, _T("To be &greyed"),
1061 _T("This menu item should be initially greyed out"));
1062 menu
.AppendSeparator();
1063 menu
.Append(Menu_File_Quit
, _T("E&xit"));
1065 menu
.Delete(Menu_Popup_ToBeDeleted
);
1066 menu
.Check(Menu_Popup_ToBeChecked
, true);
1067 menu
.Enable(Menu_Popup_ToBeGreyed
, false);
1069 PopupMenu(&menu
, pos
);
1071 // test for destroying items in popup menus
1072 #if 0 // doesn't work in wxGTK!
1073 menu
.Destroy(Menu_Popup_Submenu
);
1075 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
1080 void MyFrame::OnTestNormal(wxCommandEvent
& WXUNUSED(event
))
1082 wxLogMessage(_T("Normal item selected"));
1085 void MyFrame::OnTestCheck(wxCommandEvent
& event
)
1087 wxLogMessage(_T("Check item %schecked"),
1088 event
.IsChecked() ? _T("") : _T("un"));
1091 void MyFrame::OnTestRadio(wxCommandEvent
& event
)
1093 wxLogMessage(_T("Radio item %d selected"),
1094 event
.GetId() - Menu_Test_Radio1
+ 1);
1098 void MyFrame::LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
)
1102 << ( event
.IsPopup() ? _T("popup ") : _T("") )
1103 << _T("menu has been ")
1107 wxLogStatus(this, msg
.c_str());
1111 void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent
& event
)
1113 event
.Enable(false);
1116 void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent
& event
)
1121 void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent
& event
)
1123 int which
= (event
.GetId() - Menu_SubMenu_Radio1
+ 1);
1130 #if USE_CONTEXT_MENU
1131 void MyFrame::OnContextMenu(wxContextMenuEvent
& event
)
1133 wxPoint point
= event
.GetPosition();
1135 if (point
.x
== -1 && point
.y
== -1) {
1136 wxSize size
= GetSize();
1137 point
.x
= size
.x
/ 2;
1138 point
.y
= size
.y
/ 2;
1140 point
= ScreenToClient(point
);
1142 ShowContextMenu(point
);
1146 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
1152 // leave a band below for popup menu testing
1153 wxSize size
= GetClientSize();
1154 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
1157 // this is really ugly but we have to do it as we can't just call
1158 // event.Skip() because wxFrameBase would make the text control fill the
1159 // entire frame then
1160 #ifdef __WXUNIVERSAL__
1162 #endif // __WXUNIVERSAL__