]>
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
);
83 void OnAppendMenu(wxCommandEvent
& event
);
84 void OnInsertMenu(wxCommandEvent
& event
);
85 void OnDeleteMenu(wxCommandEvent
& event
);
86 void OnToggleMenu(wxCommandEvent
& event
);
87 void OnEnableMenu(wxCommandEvent
& event
);
88 void OnGetLabelMenu(wxCommandEvent
& event
);
89 void OnSetLabelMenu(wxCommandEvent
& event
);
92 void OnContextMenu(wxContextMenuEvent
& event
)
93 { ShowContextMenu(ScreenToClient(event
.GetPosition())); }
95 void OnRightUp(wxMouseEvent
& event
)
96 { ShowContextMenu(event
.GetPosition()); }
99 void OnMenuOpen(wxMenuEvent
& event
)
100 { LogMenuOpenOrClose(event
, _T("opened")); }
101 void OnMenuClose(wxMenuEvent
& event
)
102 { LogMenuOpenOrClose(event
, _T("closed")); }
104 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
106 void OnSize(wxSizeEvent
& event
);
109 void LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
);
110 void ShowContextMenu(const wxPoint
& pos
);
112 wxMenu
*CreateDummyMenu(wxString
*title
);
114 wxMenuItem
*GetLastMenuItem() const;
116 // the menu previously detached from the menubar (may be NULL)
119 // the count of dummy menus already created
122 // the control used for logging
123 wxTextCtrl
*m_textctrl
;
125 // the previous log target
128 DECLARE_EVENT_TABLE()
131 // A small helper class which intercepts all menu events and logs them
132 class MyEvtHandler
: public wxEvtHandler
135 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
137 void OnMenuEvent(wxCommandEvent
& event
)
139 m_frame
->LogMenuEvent(event
);
147 DECLARE_EVENT_TABLE()
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
156 Menu_File_Quit
= 100,
159 Menu_MenuBar_Toggle
= 200,
164 Menu_MenuBar_GetLabel
,
165 Menu_MenuBar_SetLabel
,
167 Menu_Menu_Append
= 300,
177 Menu_Dummy_First
= 400,
183 Menu_Help_About
= 1000,
185 Menu_Popup_ToBeDeleted
= 2000,
186 Menu_Popup_ToBeGreyed
,
187 Menu_Popup_ToBeChecked
,
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
197 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
198 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
199 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
201 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
203 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
204 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
205 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
206 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
207 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
208 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
209 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
211 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
212 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
213 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
214 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
215 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
216 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
217 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
218 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
219 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
221 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
223 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
226 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
228 EVT_RIGHT_UP(MyFrame::OnRightUp
)
231 EVT_MENU_OPEN(MyFrame::OnMenuOpen
)
232 EVT_MENU_CLOSE(MyFrame::OnMenuClose
)
234 EVT_SIZE(MyFrame::OnSize
)
237 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
238 EVT_MENU(-1, MyEvtHandler::OnMenuEvent
)
241 // ============================================================================
243 // ============================================================================
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
251 // The `main program' equivalent, creating the windows and returning the
255 // Create the main frame window
256 MyFrame
* frame
= new MyFrame
;
261 frame
->SetStatusText("Welcome to wxWindows menu sample");
262 #endif // wxUSE_STATUSBAR
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 // Define my frame constructor
275 : wxFrame((wxFrame
*)NULL
, -1, "wxWindows menu sample",
276 wxDefaultPosition
, wxSize(400, 250))
285 #endif // wxUSE_STATUSBAR
287 // create the menubar
288 wxMenu
*fileMenu
= new wxMenu
;
290 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
291 "Clear &log\tCtrl-L");
292 item
->SetBitmap(copy_xpm
);
293 fileMenu
->Append(item
);
294 fileMenu
->AppendSeparator();
295 fileMenu
->Append(Menu_File_Quit
, "E&xit\tAlt-X", "Quit menu sample");
297 wxMenu
*menubarMenu
= new wxMenu
;
298 menubarMenu
->Append(Menu_MenuBar_Append
, "&Append menu\tCtrl-A",
299 "Append a menu to the menubar");
300 menubarMenu
->Append(Menu_MenuBar_Insert
, "&Insert menu\tCtrl-I",
301 "Insert a menu into the menubar");
302 menubarMenu
->Append(Menu_MenuBar_Delete
, "&Delete menu\tCtrl-D",
303 "Delete the last menu from the menubar");
304 menubarMenu
->Append(Menu_MenuBar_Toggle
, "&Toggle menu\tCtrl-T",
305 "Toggle the first menu in the menubar", TRUE
);
306 menubarMenu
->AppendSeparator();
307 menubarMenu
->Append(Menu_MenuBar_Enable
, "&Enable menu\tCtrl-E",
308 "Enable or disable the last menu", TRUE
);
309 menubarMenu
->AppendSeparator();
310 menubarMenu
->Append(Menu_MenuBar_GetLabel
, "&Get menu label\tCtrl-G",
311 "Get the label of the last menu");
312 menubarMenu
->Append(Menu_MenuBar_SetLabel
, "&Set menu label\tCtrl-S",
313 "Change the label of the last menu");
315 wxMenu
*menuMenu
= new wxMenu
;
316 menuMenu
->Append(Menu_Menu_Append
, "&Append menu item\tAlt-A",
317 "Append a menu item to the last menu");
318 menuMenu
->Append(Menu_Menu_AppendSub
, "&Append sub menu\tAlt-S",
319 "Append a sub menu to the last menu");
320 menuMenu
->Append(Menu_Menu_Insert
, "&Insert menu item\tAlt-I",
321 "Insert a menu item in head of the last menu");
322 menuMenu
->Append(Menu_Menu_Delete
, "&Delete menu item\tAlt-D",
323 "Delete the last menu item from the last menu");
324 menuMenu
->AppendSeparator();
325 menuMenu
->Append(Menu_Menu_Enable
, "&Enable menu item\tAlt-E",
326 "Enable or disable the last menu item", TRUE
);
327 menuMenu
->Append(Menu_Menu_Check
, "&Check menu item\tAlt-C",
328 "Check or uncheck the last menu item", TRUE
);
329 menuMenu
->AppendSeparator();
330 menuMenu
->Append(Menu_Menu_GetLabel
, "&Get menu item label\tAlt-G",
331 "Get the label of the last menu item");
332 menuMenu
->Append(Menu_Menu_SetLabel
, "&Set menu item label\tAlt-S",
333 "Change the label of the last menu item");
334 menuMenu
->AppendSeparator();
335 menuMenu
->Append(Menu_Menu_GetInfo
, "Get menu item in&fo\tAlt-F",
336 "Show the state of the last menu item");
338 wxMenu
*helpMenu
= new wxMenu
;
339 helpMenu
->Append(Menu_Help_About
, "&About\tF1", "About menu sample");
341 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
343 menuBar
->Append(fileMenu
, "&File");
344 menuBar
->Append(menubarMenu
, "Menu&bar");
345 menuBar
->Append(menuMenu
, "&Menu");
346 menuBar
->Append(helpMenu
, "&Help");
348 // these items should be initially checked
349 menuBar
->Check(Menu_MenuBar_Toggle
, TRUE
);
350 menuBar
->Check(Menu_MenuBar_Enable
, TRUE
);
351 menuBar
->Check(Menu_Menu_Enable
, TRUE
);
352 menuBar
->Check(Menu_Menu_Check
, FALSE
);
354 // associate the menu bar with the frame
357 // intercept all menu events and log them in this custom event handler
358 PushEventHandler(new MyEvtHandler(this));
360 // create the log text window
361 m_textctrl
= new wxTextCtrl(this, -1, _T(""),
362 wxDefaultPosition
, wxDefaultSize
,
364 m_textctrl
->SetEditable(FALSE
);
366 wxLog::SetTimestamp(NULL
);
367 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
369 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
370 _T("append/insert/delete items to/from the last menu.\n")
371 _T("The commands from \"Menubar\" menu work with the ")
372 _T("menubar itself.\n\n")
373 _T("Right click the band below to test popup menus.\n"));
380 // delete the event handler installed in ctor
381 PopEventHandler(TRUE
);
383 // restore old logger
384 delete wxLog::SetActiveTarget(m_logOld
);
387 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
389 wxMenu
*menu
= new wxMenu
;
390 menu
->Append(Menu_Dummy_First
, "&First item\tCtrl-F1");
391 menu
->AppendSeparator();
392 menu
->Append(Menu_Dummy_Second
, "&Second item\tCtrl-F2", "", TRUE
);
396 title
->Printf(wxT("Dummy menu &%d"), ++m_countDummy
);
402 wxMenuItem
*MyFrame::GetLastMenuItem() const
404 wxMenuBar
*menubar
= GetMenuBar();
405 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
407 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
410 wxLogWarning(wxT("No last item in the last menu!"));
416 return node
->GetData();
420 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
422 int id
= event
.GetId();
423 if ( !GetMenuBar()->FindItem(id
) )
426 wxString msg
= wxString::Format(wxT("Menu command %d"), id
);
427 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
429 msg
+= wxString::Format(wxT(" (the item is currently %schecked)"),
430 event
.IsChecked() ? "" : "not ");
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
445 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
450 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
452 (void)wxMessageBox("wxWindows menu sample\n© 1999-2001 Vadim Zeitlin",
453 "About wxWindows menu sample",
457 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
459 wxMenuBar
*mbar
= GetMenuBar();
461 size_t count
= mbar
->GetMenuCount();
464 // don't let delete the first 2 menus
465 wxLogError(wxT("Can't delete any more menus"));
469 delete mbar
->Remove(count
- 1);
473 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
476 wxMenu
*menu
= CreateDummyMenu(&title
);
477 GetMenuBar()->Insert(0, menu
, title
);
480 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
483 wxMenu
*menu
= CreateDummyMenu(&title
);
484 GetMenuBar()->Append(menu
, title
);
487 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
489 wxMenuBar
*mbar
= GetMenuBar();
493 m_menu
= mbar
->Remove(0);
498 mbar
->Insert(0, m_menu
, "&File");
503 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
505 wxMenuBar
*mbar
= GetMenuBar();
506 size_t count
= mbar
->GetMenuCount();
508 mbar
->EnableTop(count
- 1, event
.IsChecked());
511 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
513 wxMenuBar
*mbar
= GetMenuBar();
514 size_t count
= mbar
->GetMenuCount();
516 wxCHECK_RET( count
, _T("no last menu?") );
518 wxLogMessage(wxT("The label of the last menu item is '%s'"),
519 mbar
->GetLabelTop(count
- 1).c_str());
522 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
524 wxMenuBar
*mbar
= GetMenuBar();
525 size_t count
= mbar
->GetMenuCount();
527 wxCHECK_RET( count
, _T("no last menu?") );
529 wxString label
= wxGetTextFromUser
531 _T("Enter new label: "),
532 _T("Change last menu text"),
533 mbar
->GetLabelTop(count
- 1),
537 if ( !label
.empty() )
539 mbar
->SetLabelTop(count
- 1, label
);
543 void MyFrame::OnDummy(wxCommandEvent
& event
)
545 wxLogMessage(wxT("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
548 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
550 wxMenuBar
*menubar
= GetMenuBar();
551 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
553 menu
->AppendSeparator();
554 menu
->Append(Menu_Dummy_Third
, "&Third dummy item\tCtrl-F3",
555 "Checkable item", TRUE
);
558 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
560 wxMenuBar
*menubar
= GetMenuBar();
562 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
564 menu
->Append(Menu_Dummy_Last
, "&Dummy sub menu",
565 CreateDummyMenu(NULL
), "Dummy sub menu help");
568 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
570 wxMenuBar
*menubar
= GetMenuBar();
571 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
573 size_t count
= menu
->GetMenuItemCount();
576 wxLogWarning(wxT("No items to delete!"));
580 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
584 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
586 wxMenuBar
*menubar
= GetMenuBar();
587 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
589 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
590 "Fourth dummy item\tCtrl-F4"));
591 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, ""));
594 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
596 wxMenuItem
*item
= GetLastMenuItem();
600 item
->Enable(!item
->IsEnabled());
604 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
606 wxMenuItem
*item
= GetLastMenuItem();
611 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
615 wxMenuItem
*item
= GetLastMenuItem();
617 event
.Enable(item
&& item
->IsCheckable());
620 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
622 wxMenuItem
*item
= GetLastMenuItem();
626 wxLogMessage(wxT("The label of the last menu item is '%s'"),
627 item
->GetLabel().c_str());
631 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
633 wxMenuItem
*item
= GetLastMenuItem();
637 wxString label
= wxGetTextFromUser
639 _T("Enter new label: "),
640 _T("Change last menu item text"),
645 if ( !label
.empty() )
647 item
->SetText(label
);
652 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
654 wxMenuItem
*item
= GetLastMenuItem();
659 msg
<< "The item is " << (item
->IsEnabled() ? "enabled"
663 if ( item
->IsCheckable() )
665 msg
<< "It is checkable and " << (item
->IsChecked() ? "" : "un")
670 wxAcceleratorEntry
*accel
= item
->GetAccel();
673 msg
<< "Its accelerator is ";
675 int flags
= accel
->GetFlags();
676 if ( flags
& wxACCEL_ALT
)
678 if ( flags
& wxACCEL_CTRL
)
680 if ( flags
& wxACCEL_SHIFT
)
681 msg
<< wxT("Shift-");
683 int code
= accel
->GetKeyCode();
698 msg
<< wxT('F') << code
- WXK_F1
+ 1;
701 // if there are any other keys wxGetAccelFromString() may return,
702 // we should process them here
705 if ( wxIsalnum(code
) )
712 wxFAIL_MSG( wxT("unknown keyboard accel") );
719 msg
<< "It doesn't have an accelerator";
721 #endif // wxUSE_ACCEL
727 void MyFrame::ShowContextMenu(const wxPoint
& pos
)
729 wxMenu
menu("Test popup");
731 menu
.Append(Menu_Help_About
, "&About");
732 menu
.Append(Menu_Popup_Submenu
, "&Submenu", CreateDummyMenu(NULL
));
733 menu
.Append(Menu_Popup_ToBeDeleted
, "To be &deleted");
734 menu
.Append(Menu_Popup_ToBeChecked
, "To be &checked", "", TRUE
);
735 menu
.Append(Menu_Popup_ToBeGreyed
, "To be &greyed");
736 menu
.AppendSeparator();
737 menu
.Append(Menu_File_Quit
, "E&xit");
739 menu
.Delete(Menu_Popup_ToBeDeleted
);
740 menu
.Check(Menu_Popup_ToBeChecked
, TRUE
);
741 menu
.Enable(Menu_Popup_ToBeGreyed
, FALSE
);
743 PopupMenu(&menu
, pos
.x
, pos
.y
);
745 // test for destroying items in popup menus
746 #if 0 // doesn't work in wxGTK!
747 menu
.Destroy(Menu_Popup_Submenu
);
749 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
753 void MyFrame::LogMenuOpenOrClose(const wxMenuEvent
& event
, const wxChar
*what
)
755 wxLogStatus(this, _T("A %smenu has been %s."),
756 event
.IsPopup() ? _T("popup ") : _T(""), what
);
759 void MyFrame::OnSize(wxSizeEvent
& event
)
764 // leave a band below for popup menu testing
765 wxSize size
= GetClientSize();
766 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
768 // this is really ugly but we have to do it as we can't just call
769 // event.Skip() because wxFrameBase would make the text control fill the
771 #ifdef __WXUNIVERSAL__
773 #endif // __WXUNIVERSAL__