]>
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>
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // Define a new application
38 class MyApp
: public wxApp
45 class MyFrame
: public wxFrame
52 void LogMenuEvent(const wxCommandEvent
& event
);
54 void OnQuit(wxCommandEvent
& event
);
55 void OnAbout(wxCommandEvent
& event
);
57 void OnDummy(wxCommandEvent
& event
);
59 void OnAppendMenuItem(wxCommandEvent
& event
);
60 void OnAppendSubMenu(wxCommandEvent
& event
);
61 void OnDeleteMenuItem(wxCommandEvent
& event
);
62 void OnInsertMenuItem(wxCommandEvent
& event
);
63 void OnCheckMenuItem(wxCommandEvent
& event
);
64 void OnEnableMenuItem(wxCommandEvent
& event
);
65 void OnGetLabelMenuItem(wxCommandEvent
& event
);
66 void OnSetLabelMenuItem(wxCommandEvent
& event
);
67 void OnGetMenuItemInfo(wxCommandEvent
& event
);
69 void OnAppendMenu(wxCommandEvent
& event
);
70 void OnInsertMenu(wxCommandEvent
& event
);
71 void OnDeleteMenu(wxCommandEvent
& event
);
72 void OnToggleMenu(wxCommandEvent
& event
);
73 void OnEnableMenu(wxCommandEvent
& event
);
74 void OnGetLabelMenu(wxCommandEvent
& event
);
75 void OnSetLabelMenu(wxCommandEvent
& event
);
77 void OnRightDown(wxMouseEvent
& event
);
79 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
82 wxMenu
*CreateDummyMenu(wxString
*title
);
84 wxMenuItem
*GetLastMenuItem() const;
93 // A small helper class which intercepts all menu events and logs them
94 class MyEvtHandler
: public wxEvtHandler
97 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
99 void OnMenuEvent(wxCommandEvent
& event
)
101 m_frame
->LogMenuEvent(event
);
109 DECLARE_EVENT_TABLE()
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
118 Menu_File_Quit
= 100,
120 Menu_MenuBar_Toggle
= 200,
125 Menu_MenuBar_GetLabel
,
126 Menu_MenuBar_SetLabel
,
128 Menu_Menu_Append
= 300,
138 Menu_Dummy_First
= 400,
144 Menu_Help_About
= 1000,
146 Menu_Popup_ToBeDeleted
= 2000,
147 Menu_Popup_ToBeGreyed
,
148 Menu_Popup_ToBeChecked
,
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
159 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
161 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
163 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
164 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
165 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
166 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
167 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
168 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
169 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
171 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
172 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
173 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
174 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
175 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
176 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
177 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
178 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
179 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
181 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
183 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
185 EVT_RIGHT_DOWN(MyFrame::OnRightDown
)
188 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
189 EVT_MENU(-1, MyEvtHandler::OnMenuEvent
)
192 // ============================================================================
194 // ============================================================================
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
202 // The `main program' equivalent, creating the windows and returning the
206 // Create the main frame window
207 MyFrame
* frame
= new MyFrame
;
211 frame
->SetStatusText("Hello, wxWindows");
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 // Define my frame constructor
224 : wxFrame((wxFrame
*)NULL
, -1, "wxWindows menu sample",
225 wxDefaultPosition
, wxSize(300, 200))
232 // create the menubar
233 wxMenu
*fileMenu
= new wxMenu
;
234 fileMenu
->Append(Menu_File_Quit
, "E&xit\tAlt-X", "Quit toolbar sample" );
236 wxMenu
*menubarMenu
= new wxMenu
;
237 menubarMenu
->Append(Menu_MenuBar_Append
, "&Append menu\tCtrl-A",
238 "Append a menu to the menubar");
239 menubarMenu
->Append(Menu_MenuBar_Insert
, "&Insert menu\tCtrl-I",
240 "Insert a menu into the menubar");
241 menubarMenu
->Append(Menu_MenuBar_Delete
, "&Delete menu\tCtrl-D",
242 "Delete the last menu from the menubar");
243 menubarMenu
->Append(Menu_MenuBar_Toggle
, "&Toggle menu\tCtrl-T",
244 "Toggle the first menu in the menubar", TRUE
);
245 menubarMenu
->AppendSeparator();
246 menubarMenu
->Append(Menu_MenuBar_Enable
, "&Enable menu\tCtrl-E",
247 "Enable or disable the last menu", TRUE
);
248 menubarMenu
->AppendSeparator();
249 menubarMenu
->Append(Menu_MenuBar_GetLabel
, "&Get menu label\tCtrl-G",
250 "Get the label of the last menu");
251 menubarMenu
->Append(Menu_MenuBar_SetLabel
, "&Set menu label\tCtrl-S",
252 "Change the label of the last menu");
254 wxMenu
*menuMenu
= new wxMenu
;
255 menuMenu
->Append(Menu_Menu_Append
, "&Append menu item\tAlt-A",
256 "Append a menu item to the last menu");
257 menuMenu
->Append(Menu_Menu_AppendSub
, "&Append sub menu\tAlt-S",
258 "Append a sub menu to the last menu");
259 menuMenu
->Append(Menu_Menu_Insert
, "&Insert menu item\tAlt-I",
260 "Insert a menu item in head of the last menu");
261 menuMenu
->Append(Menu_Menu_Delete
, "&Delete menu item\tAlt-D",
262 "Delete the last menu item from the last menu");
263 menuMenu
->AppendSeparator();
264 menuMenu
->Append(Menu_Menu_Enable
, "&Enable menu item\tAlt-E",
265 "Enable or disable the last menu item", TRUE
);
266 menuMenu
->Append(Menu_Menu_Check
, "&Check menu item\tAlt-C",
267 "Check or uncheck the last menu item", TRUE
);
268 menuMenu
->AppendSeparator();
269 menuMenu
->Append(Menu_Menu_GetLabel
, "&Get menu item label\tAlt-G",
270 "Get the label of the last menu item");
271 menuMenu
->Append(Menu_Menu_SetLabel
, "&Set menu item label\tAlt-S",
272 "Change the label of the last menu item");
273 menuMenu
->AppendSeparator();
274 menuMenu
->Append(Menu_Menu_GetInfo
, "Get menu item in&fo\tAlt-F",
275 "Show the state of the last menu item");
277 wxMenu
*helpMenu
= new wxMenu
;
278 helpMenu
->Append(Menu_Help_About
, "&About\tF1", "About menu sample");
280 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
282 menuBar
->Append(fileMenu
, "&File");
283 menuBar
->Append(menubarMenu
, "Menu&bar");
284 menuBar
->Append(menuMenu
, "&Menu");
285 menuBar
->Append(helpMenu
, "&Help");
287 // these items should be initially checked
288 menuBar
->Check(Menu_MenuBar_Toggle
, TRUE
);
289 menuBar
->Check(Menu_MenuBar_Enable
, TRUE
);
290 menuBar
->Check(Menu_Menu_Enable
, TRUE
);
291 menuBar
->Check(Menu_Menu_Check
, FALSE
);
293 // associate the menu bar with the frame
296 // intercept all menu events and log them in this custom event handler
297 PushEventHandler(new MyEvtHandler(this));
304 // delete the event handler installed in ctor
305 PopEventHandler(TRUE
);
308 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
310 wxMenu
*menu
= new wxMenu
;
311 menu
->Append(Menu_Dummy_First
, "First item\tCtrl-F1");
312 menu
->AppendSeparator();
313 menu
->Append(Menu_Dummy_Second
, "Second item\tCtrl-F2", "", TRUE
);
317 title
->Printf("Dummy menu &%d", ++m_countDummy
);
323 wxMenuItem
*MyFrame::GetLastMenuItem() const
325 wxMenuBar
*menubar
= GetMenuBar();
326 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
328 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
331 wxLogWarning("No last item in the last menu!");
337 return node
->GetData();
341 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
343 int id
= event
.GetId();
344 wxString msg
= wxString::Format("Menu command %d", id
);
345 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
347 msg
+= wxString::Format(" (the item is currently %schecked)",
348 event
.IsChecked() ? "" : "not ");
351 SetStatusText(msg
, 1);
354 // ----------------------------------------------------------------------------
356 // ----------------------------------------------------------------------------
358 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
363 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
365 (void)wxMessageBox("wxWindows toolbar sample",
366 "About wxWindows menu sample",
370 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
372 wxMenuBar
*mbar
= GetMenuBar();
374 size_t count
= mbar
->GetMenuCount();
377 // don't let delete the first 2 menus
378 wxLogError("Can't delete any more menus");
382 delete mbar
->Remove(count
- 1);
386 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
389 wxMenu
*menu
= CreateDummyMenu(&title
);
390 GetMenuBar()->Insert(0, menu
, title
);
393 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
396 wxMenu
*menu
= CreateDummyMenu(&title
);
397 GetMenuBar()->Append(menu
, title
);
400 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
402 wxMenuBar
*mbar
= GetMenuBar();
406 m_menu
= mbar
->Remove(0);
411 mbar
->Insert(0, m_menu
, "&File");
416 void MyFrame::OnEnableMenu(wxCommandEvent
& WXUNUSED(event
))
418 wxMenuBar
*mbar
= GetMenuBar();
419 size_t count
= mbar
->GetMenuCount();
421 static bool s_enabled
= TRUE
;
423 s_enabled
= !s_enabled
;
424 mbar
->EnableTop(count
- 1, s_enabled
);
427 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
429 wxMenuBar
*mbar
= GetMenuBar();
430 size_t count
= mbar
->GetMenuCount();
432 wxLogMessage("The label of the last menu item is '%s'",
433 mbar
->GetLabelTop(count
- 1).c_str());
436 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
438 wxMenuBar
*mbar
= GetMenuBar();
439 size_t count
= mbar
->GetMenuCount();
441 mbar
->SetLabelTop(count
- 1, "Dummy label &0");
444 void MyFrame::OnDummy(wxCommandEvent
& event
)
447 s
.Printf("Dummy item #%d", event
.GetId() - Menu_Dummy_First
+ 1);
448 wxMessageBox(s
, "Menu sample", wxICON_INFORMATION
);
451 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
453 wxMenuBar
*menubar
= GetMenuBar();
454 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
456 menu
->AppendSeparator();
457 menu
->Append(Menu_Dummy_Third
, "Third dummy item\tCtrl-F3",
458 "Checkable item", TRUE
);
461 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
463 wxMenuBar
*menubar
= GetMenuBar();
465 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
467 menu
->Append(Menu_Dummy_Last
, "Dummy sub menu",
468 CreateDummyMenu(NULL
), "Dummy sub menu help");
471 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
473 wxMenuBar
*menubar
= GetMenuBar();
474 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
476 size_t count
= menu
->GetMenuItemCount();
479 wxLogWarning("No items to delete!");
483 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
487 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
489 wxMenuBar
*menubar
= GetMenuBar();
490 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
492 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
493 "Fourth dummy item\tCtrl-F4"));
494 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, ""));
497 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
499 wxMenuItem
*item
= GetLastMenuItem();
503 item
->Enable(!item
->IsEnabled());
507 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
509 wxMenuItem
*item
= GetLastMenuItem();
514 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
518 wxMenuItem
*item
= GetLastMenuItem();
520 event
.Enable(item
&& item
->IsCheckable());
523 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
525 wxMenuItem
*item
= GetLastMenuItem();
529 wxLogMessage("The label of the last menu item is '%s'",
530 item
->GetLabel().c_str());
534 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
536 wxMenuItem
*item
= GetLastMenuItem();
540 item
->SetText("Dummy menu item text");
544 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
546 wxMenuItem
*item
= GetLastMenuItem();
551 msg
<< "The item is " << (item
->IsEnabled() ? "enabled"
555 if ( item
->IsCheckable() )
557 msg
<< "It is checkable and " << (item
->IsChecked() ? "" : "un")
562 wxAcceleratorEntry
*accel
= item
->GetAccel();
565 msg
<< "Its accelerator is ";
567 int flags
= accel
->GetFlags();
568 if ( flags
& wxACCEL_ALT
)
570 if ( flags
& wxACCEL_CTRL
)
572 if ( flags
& wxACCEL_SHIFT
)
573 msg
<< wxT("Shift-");
575 int code
= accel
->GetKeyCode();
590 msg
<< wxT('F') << code
- WXK_F1
+ 1;
593 // if there are any other keys wxGetAccelFromString() may return,
594 // we should process them here
597 if ( wxIsalnum(code
) )
604 wxFAIL_MSG( wxT("unknown keyboard accel") );
611 msg
<< "It doesn't have an accelerator";
613 #endif // wxUSE_ACCEL
619 void MyFrame::OnRightDown(wxMouseEvent
&event
)
621 wxMenu
menu("Test popup");
623 menu
.Append(Menu_Help_About
, "&About");
624 menu
.Append(Menu_Popup_Submenu
, "Submenu", CreateDummyMenu(NULL
));
625 menu
.Append(Menu_Popup_ToBeDeleted
, "To be deleted");
626 menu
.Append(Menu_Popup_ToBeChecked
, "To be checked", "", TRUE
);
627 menu
.Append(Menu_Popup_ToBeGreyed
, "To be greyed");
628 menu
.AppendSeparator();
629 menu
.Append(Menu_File_Quit
, "E&xit");
631 menu
.Delete(Menu_Popup_ToBeDeleted
);
632 menu
.Check(Menu_Popup_ToBeChecked
, TRUE
);
633 menu
.Enable(Menu_Popup_ToBeGreyed
, FALSE
);
635 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
637 // test for destroying items in popup menus
639 menu
.Destroy(Menu_Popup_Submenu
);
641 PopupMenu( &menu
, event
.GetX(), event
.GetY() );