]>
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"
38 #error "menu sample requires wxUSE_MENUS=1"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // Define a new application
48 class MyApp
: public wxApp
55 class MyFrame
: public wxFrame
62 void LogMenuEvent(const wxCommandEvent
& event
);
65 void OnQuit(wxCommandEvent
& event
);
66 void OnClearLog(wxCommandEvent
& event
);
68 void OnAbout(wxCommandEvent
& event
);
70 void OnDummy(wxCommandEvent
& event
);
72 void OnAppendMenuItem(wxCommandEvent
& event
);
73 void OnAppendSubMenu(wxCommandEvent
& event
);
74 void OnDeleteMenuItem(wxCommandEvent
& event
);
75 void OnInsertMenuItem(wxCommandEvent
& event
);
76 void OnCheckMenuItem(wxCommandEvent
& event
);
77 void OnEnableMenuItem(wxCommandEvent
& event
);
78 void OnGetLabelMenuItem(wxCommandEvent
& event
);
79 void OnSetLabelMenuItem(wxCommandEvent
& event
);
80 void OnGetMenuItemInfo(wxCommandEvent
& event
);
82 void OnAppendMenu(wxCommandEvent
& event
);
83 void OnInsertMenu(wxCommandEvent
& event
);
84 void OnDeleteMenu(wxCommandEvent
& event
);
85 void OnToggleMenu(wxCommandEvent
& event
);
86 void OnEnableMenu(wxCommandEvent
& event
);
87 void OnGetLabelMenu(wxCommandEvent
& event
);
88 void OnSetLabelMenu(wxCommandEvent
& event
);
90 void OnRightUp(wxMouseEvent
& event
);
92 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
94 void OnSize(wxSizeEvent
& event
);
97 wxMenu
*CreateDummyMenu(wxString
*title
);
99 wxMenuItem
*GetLastMenuItem() const;
101 // the menu previously detached from the menubar (may be NULL)
104 // the count of dummy menus already created
107 // the control used for logging
108 wxTextCtrl
*m_textctrl
;
110 // the previous log target
113 DECLARE_EVENT_TABLE()
116 // A small helper class which intercepts all menu events and logs them
117 class MyEvtHandler
: public wxEvtHandler
120 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
122 void OnMenuEvent(wxCommandEvent
& event
)
124 m_frame
->LogMenuEvent(event
);
132 DECLARE_EVENT_TABLE()
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
141 Menu_File_Quit
= 100,
144 Menu_MenuBar_Toggle
= 200,
149 Menu_MenuBar_GetLabel
,
150 Menu_MenuBar_SetLabel
,
152 Menu_Menu_Append
= 300,
162 Menu_Dummy_First
= 400,
168 Menu_Help_About
= 1000,
170 Menu_Popup_ToBeDeleted
= 2000,
171 Menu_Popup_ToBeGreyed
,
172 Menu_Popup_ToBeChecked
,
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
183 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
184 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
186 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
188 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
189 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
190 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
191 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
192 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
193 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
194 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
196 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
197 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
198 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
199 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
200 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
201 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
202 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
203 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
204 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
206 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
208 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
210 EVT_RIGHT_UP(MyFrame::OnRightUp
)
212 EVT_SIZE(MyFrame::OnSize
)
215 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
216 EVT_MENU(-1, MyEvtHandler::OnMenuEvent
)
219 // ============================================================================
221 // ============================================================================
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
229 // The `main program' equivalent, creating the windows and returning the
233 // Create the main frame window
234 MyFrame
* frame
= new MyFrame
;
239 frame
->SetStatusText("Welcome to wxWindows menu sample");
240 #endif // wxUSE_STATUSBAR
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 // Define my frame constructor
253 : wxFrame((wxFrame
*)NULL
, -1, "wxWindows menu sample",
254 wxDefaultPosition
, wxSize(300, 200))
263 #endif // wxUSE_STATUSBAR
265 // create the menubar
266 wxMenu
*fileMenu
= new wxMenu
;
268 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
269 "Clear &log\tCtrl-L");
270 item
->SetBitmap(copy_xpm
);
271 fileMenu
->Append(item
);
272 fileMenu
->AppendSeparator();
273 fileMenu
->Append(Menu_File_Quit
, "E&xit\tAlt-X", "Quit menu sample");
275 wxMenu
*menubarMenu
= new wxMenu
;
276 menubarMenu
->Append(Menu_MenuBar_Append
, "&Append menu\tCtrl-A",
277 "Append a menu to the menubar");
278 menubarMenu
->Append(Menu_MenuBar_Insert
, "&Insert menu\tCtrl-I",
279 "Insert a menu into the menubar");
280 menubarMenu
->Append(Menu_MenuBar_Delete
, "&Delete menu\tCtrl-D",
281 "Delete the last menu from the menubar");
282 menubarMenu
->Append(Menu_MenuBar_Toggle
, "&Toggle menu\tCtrl-T",
283 "Toggle the first menu in the menubar", TRUE
);
284 menubarMenu
->AppendSeparator();
285 menubarMenu
->Append(Menu_MenuBar_Enable
, "&Enable menu\tCtrl-E",
286 "Enable or disable the last menu", TRUE
);
287 menubarMenu
->AppendSeparator();
288 menubarMenu
->Append(Menu_MenuBar_GetLabel
, "&Get menu label\tCtrl-G",
289 "Get the label of the last menu");
290 menubarMenu
->Append(Menu_MenuBar_SetLabel
, "&Set menu label\tCtrl-S",
291 "Change the label of the last menu");
293 wxMenu
*menuMenu
= new wxMenu
;
294 menuMenu
->Append(Menu_Menu_Append
, "&Append menu item\tAlt-A",
295 "Append a menu item to the last menu");
296 menuMenu
->Append(Menu_Menu_AppendSub
, "&Append sub menu\tAlt-S",
297 "Append a sub menu to the last menu");
298 menuMenu
->Append(Menu_Menu_Insert
, "&Insert menu item\tAlt-I",
299 "Insert a menu item in head of the last menu");
300 menuMenu
->Append(Menu_Menu_Delete
, "&Delete menu item\tAlt-D",
301 "Delete the last menu item from the last menu");
302 menuMenu
->AppendSeparator();
303 menuMenu
->Append(Menu_Menu_Enable
, "&Enable menu item\tAlt-E",
304 "Enable or disable the last menu item", TRUE
);
305 menuMenu
->Append(Menu_Menu_Check
, "&Check menu item\tAlt-C",
306 "Check or uncheck the last menu item", TRUE
);
307 menuMenu
->AppendSeparator();
308 menuMenu
->Append(Menu_Menu_GetLabel
, "&Get menu item label\tAlt-G",
309 "Get the label of the last menu item");
310 menuMenu
->Append(Menu_Menu_SetLabel
, "&Set menu item label\tAlt-S",
311 "Change the label of the last menu item");
312 menuMenu
->AppendSeparator();
313 menuMenu
->Append(Menu_Menu_GetInfo
, "Get menu item in&fo\tAlt-F",
314 "Show the state of the last menu item");
316 wxMenu
*helpMenu
= new wxMenu
;
317 helpMenu
->Append(Menu_Help_About
, "&About\tF1", "About menu sample");
319 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
321 menuBar
->Append(fileMenu
, "&File");
322 menuBar
->Append(menubarMenu
, "Menu&bar");
323 menuBar
->Append(menuMenu
, "&Menu");
324 menuBar
->Append(helpMenu
, "&Help");
326 // these items should be initially checked
327 menuBar
->Check(Menu_MenuBar_Toggle
, TRUE
);
328 menuBar
->Check(Menu_MenuBar_Enable
, TRUE
);
329 menuBar
->Check(Menu_Menu_Enable
, TRUE
);
330 menuBar
->Check(Menu_Menu_Check
, FALSE
);
332 // associate the menu bar with the frame
335 // intercept all menu events and log them in this custom event handler
336 PushEventHandler(new MyEvtHandler(this));
338 // create the log text window
339 m_textctrl
= new wxTextCtrl(this, -1, _T(""),
340 wxDefaultPosition
, wxDefaultSize
,
342 m_textctrl
->SetEditable(FALSE
);
343 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
345 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu\n")
346 _T("append/insert/delete items to/from the last menu.\n")
347 _T("The commands from \"Menubar\" menu work with the\n")
348 _T("menubar itself.\n")
349 _T("Right click the band below to test popup menus.\n"));
356 // delete the event handler installed in ctor
357 PopEventHandler(TRUE
);
359 // restore old logger
360 delete wxLog::SetActiveTarget(m_logOld
);
363 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
365 wxMenu
*menu
= new wxMenu
;
366 menu
->Append(Menu_Dummy_First
, "&First item\tCtrl-F1");
367 menu
->AppendSeparator();
368 menu
->Append(Menu_Dummy_Second
, "&Second item\tCtrl-F2", "", TRUE
);
372 title
->Printf(wxT("Dummy menu &%d"), ++m_countDummy
);
378 wxMenuItem
*MyFrame::GetLastMenuItem() const
380 wxMenuBar
*menubar
= GetMenuBar();
381 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
383 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
386 wxLogWarning(wxT("No last item in the last menu!"));
392 return node
->GetData();
396 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
398 int id
= event
.GetId();
399 if ( !GetMenuBar()->FindItem(id
) )
402 wxString msg
= wxString::Format(wxT("Menu command %d"), id
);
403 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
405 msg
+= wxString::Format(wxT(" (the item is currently %schecked)"),
406 event
.IsChecked() ? "" : "not ");
412 // ----------------------------------------------------------------------------
414 // ----------------------------------------------------------------------------
416 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
421 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
426 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
428 (void)wxMessageBox("wxWindows menu sample\n© 1999-2001 Vadim Zeitlin",
429 "About wxWindows menu sample",
433 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
435 wxMenuBar
*mbar
= GetMenuBar();
437 size_t count
= mbar
->GetMenuCount();
440 // don't let delete the first 2 menus
441 wxLogError(wxT("Can't delete any more menus"));
445 delete mbar
->Remove(count
- 1);
449 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
452 wxMenu
*menu
= CreateDummyMenu(&title
);
453 GetMenuBar()->Insert(0, menu
, title
);
456 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
459 wxMenu
*menu
= CreateDummyMenu(&title
);
460 GetMenuBar()->Append(menu
, title
);
463 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
465 wxMenuBar
*mbar
= GetMenuBar();
469 m_menu
= mbar
->Remove(0);
474 mbar
->Insert(0, m_menu
, "&File");
479 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
481 wxMenuBar
*mbar
= GetMenuBar();
482 size_t count
= mbar
->GetMenuCount();
484 mbar
->EnableTop(count
- 1, event
.IsChecked());
487 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
489 wxMenuBar
*mbar
= GetMenuBar();
490 size_t count
= mbar
->GetMenuCount();
492 wxLogMessage(wxT("The label of the last menu item is '%s'"),
493 mbar
->GetLabelTop(count
- 1).c_str());
496 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
498 wxMenuBar
*mbar
= GetMenuBar();
499 size_t count
= mbar
->GetMenuCount();
501 mbar
->SetLabelTop(count
- 1, "Dummy label &0");
504 void MyFrame::OnDummy(wxCommandEvent
& event
)
506 wxLogMessage(wxT("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
509 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
511 wxMenuBar
*menubar
= GetMenuBar();
512 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
514 menu
->AppendSeparator();
515 menu
->Append(Menu_Dummy_Third
, "&Third dummy item\tCtrl-F3",
516 "Checkable item", TRUE
);
519 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
521 wxMenuBar
*menubar
= GetMenuBar();
523 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
525 menu
->Append(Menu_Dummy_Last
, "&Dummy sub menu",
526 CreateDummyMenu(NULL
), "Dummy sub menu help");
529 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
531 wxMenuBar
*menubar
= GetMenuBar();
532 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
534 size_t count
= menu
->GetMenuItemCount();
537 wxLogWarning(wxT("No items to delete!"));
541 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
545 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
547 wxMenuBar
*menubar
= GetMenuBar();
548 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
550 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
551 "Fourth dummy item\tCtrl-F4"));
552 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, ""));
555 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
557 wxMenuItem
*item
= GetLastMenuItem();
561 item
->Enable(!item
->IsEnabled());
565 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
567 wxMenuItem
*item
= GetLastMenuItem();
572 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
576 wxMenuItem
*item
= GetLastMenuItem();
578 event
.Enable(item
&& item
->IsCheckable());
581 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
583 wxMenuItem
*item
= GetLastMenuItem();
587 wxLogMessage(wxT("The label of the last menu item is '%s'"),
588 item
->GetLabel().c_str());
592 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
594 wxMenuItem
*item
= GetLastMenuItem();
598 item
->SetText("Dummy menu item text");
602 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
604 wxMenuItem
*item
= GetLastMenuItem();
609 msg
<< "The item is " << (item
->IsEnabled() ? "enabled"
613 if ( item
->IsCheckable() )
615 msg
<< "It is checkable and " << (item
->IsChecked() ? "" : "un")
620 wxAcceleratorEntry
*accel
= item
->GetAccel();
623 msg
<< "Its accelerator is ";
625 int flags
= accel
->GetFlags();
626 if ( flags
& wxACCEL_ALT
)
628 if ( flags
& wxACCEL_CTRL
)
630 if ( flags
& wxACCEL_SHIFT
)
631 msg
<< wxT("Shift-");
633 int code
= accel
->GetKeyCode();
648 msg
<< wxT('F') << code
- WXK_F1
+ 1;
651 // if there are any other keys wxGetAccelFromString() may return,
652 // we should process them here
655 if ( wxIsalnum(code
) )
662 wxFAIL_MSG( wxT("unknown keyboard accel") );
669 msg
<< "It doesn't have an accelerator";
671 #endif // wxUSE_ACCEL
677 void MyFrame::OnRightUp(wxMouseEvent
&event
)
679 wxMenu
menu("Test popup");
681 menu
.Append(Menu_Help_About
, "&About");
682 menu
.Append(Menu_Popup_Submenu
, "&Submenu", CreateDummyMenu(NULL
));
683 menu
.Append(Menu_Popup_ToBeDeleted
, "To be &deleted");
684 menu
.Append(Menu_Popup_ToBeChecked
, "To be &checked", "", TRUE
);
685 menu
.Append(Menu_Popup_ToBeGreyed
, "To be &greyed");
686 menu
.AppendSeparator();
687 menu
.Append(Menu_File_Quit
, "E&xit");
689 menu
.Delete(Menu_Popup_ToBeDeleted
);
690 menu
.Check(Menu_Popup_ToBeChecked
, TRUE
);
691 menu
.Enable(Menu_Popup_ToBeGreyed
, FALSE
);
693 PopupMenu(&menu
, event
.GetX(), event
.GetY());
695 // test for destroying items in popup menus
696 #if 0 // doesn't work in wxGTK!
697 menu
.Destroy(Menu_Popup_Submenu
);
699 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
703 void MyFrame::OnSize(wxSizeEvent
& event
)
708 // leave a band below for popup menu testing
709 wxSize size
= GetClientSize();
710 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
712 // this is really ugly but we have to do it as we can't just call
713 // event.Skip() because wxFrameBase would make the text control fill the
715 #ifdef __WXUNIVERSAL__
717 #endif // __WXUNIVERSAL__