]>
git.saurik.com Git - wxWidgets.git/blob - samples/menu/menu.cpp
bb2e4de3b6d16a553bca316258550a483ad2e311
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>
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // Define a new application
39 class MyApp
: public wxApp
46 class MyFrame
: public wxFrame
53 void LogMenuEvent(const wxCommandEvent
& event
);
55 void OnQuit(wxCommandEvent
& event
);
56 void OnAbout(wxCommandEvent
& event
);
58 void OnDummy(wxCommandEvent
& event
);
60 void OnAppendMenuItem(wxCommandEvent
& event
);
61 void OnAppendSubMenu(wxCommandEvent
& event
);
62 void OnDeleteMenuItem(wxCommandEvent
& event
);
63 void OnInsertMenuItem(wxCommandEvent
& event
);
64 void OnCheckMenuItem(wxCommandEvent
& event
);
65 void OnEnableMenuItem(wxCommandEvent
& event
);
66 void OnGetLabelMenuItem(wxCommandEvent
& event
);
67 void OnSetLabelMenuItem(wxCommandEvent
& event
);
68 void OnGetMenuItemInfo(wxCommandEvent
& event
);
70 void OnAppendMenu(wxCommandEvent
& event
);
71 void OnInsertMenu(wxCommandEvent
& event
);
72 void OnDeleteMenu(wxCommandEvent
& event
);
73 void OnToggleMenu(wxCommandEvent
& event
);
74 void OnEnableMenu(wxCommandEvent
& event
);
75 void OnGetLabelMenu(wxCommandEvent
& event
);
76 void OnSetLabelMenu(wxCommandEvent
& event
);
78 void OnRightDown(wxMouseEvent
& event
);
80 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
83 wxMenu
*CreateDummyMenu(wxString
*title
);
85 wxMenuItem
*GetLastMenuItem() const;
94 // A small helper class which intercepts all menu events and logs them
95 class MyEvtHandler
: public wxEvtHandler
98 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
100 void OnMenuEvent(wxCommandEvent
& event
)
102 m_frame
->LogMenuEvent(event
);
110 DECLARE_EVENT_TABLE()
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
119 Menu_File_Quit
= 100,
121 Menu_MenuBar_Toggle
= 200,
126 Menu_MenuBar_GetLabel
,
127 Menu_MenuBar_SetLabel
,
129 Menu_Menu_Append
= 300,
139 Menu_Dummy_First
= 400,
145 Menu_Help_About
= 1000,
147 Menu_Popup_ToBeDeleted
= 2000,
148 Menu_Popup_ToBeGreyed
,
149 Menu_Popup_ToBeChecked
,
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
160 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
162 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
164 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
165 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
166 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
167 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
168 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
169 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
170 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
172 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
173 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
174 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
175 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
176 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
177 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
178 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
179 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
180 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
182 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
184 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
186 EVT_RIGHT_DOWN(MyFrame::OnRightDown
)
189 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
190 EVT_MENU(-1, MyEvtHandler::OnMenuEvent
)
193 // ============================================================================
195 // ============================================================================
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
203 // The `main program' equivalent, creating the windows and returning the
207 // Create the main frame window
208 MyFrame
* frame
= new MyFrame
;
212 frame
->SetStatusText("Hello, wxWindows");
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 // Define my frame constructor
225 : wxFrame((wxFrame
*)NULL
, -1, "wxWindows menu sample",
226 wxDefaultPosition
, wxSize(300, 200))
233 // create the menubar
234 wxMenu
*fileMenu
= new wxMenu
;
235 fileMenu
->Append(Menu_File_Quit
, "E&xit\tAlt-X", "Quit toolbar sample" );
237 wxMenuItem
*bitmap_menu_item
= new wxMenuItem( fileMenu
, Menu_File_Quit
, "Quit with &bitmap\tAlt-Q" );
238 bitmap_menu_item
->SetBitmap( wxBitmap( copy_xpm
) );
239 fileMenu
->Append( bitmap_menu_item
);
241 wxMenu
*menubarMenu
= new wxMenu
;
242 menubarMenu
->Append(Menu_MenuBar_Append
, "&Append menu\tCtrl-A",
243 "Append a menu to the menubar");
244 menubarMenu
->Append(Menu_MenuBar_Insert
, "&Insert menu\tCtrl-I",
245 "Insert a menu into the menubar");
246 menubarMenu
->Append(Menu_MenuBar_Delete
, "&Delete menu\tCtrl-D",
247 "Delete the last menu from the menubar");
248 menubarMenu
->Append(Menu_MenuBar_Toggle
, "&Toggle menu\tCtrl-T",
249 "Toggle the first menu in the menubar", TRUE
);
250 menubarMenu
->AppendSeparator();
251 menubarMenu
->Append(Menu_MenuBar_Enable
, "&Enable menu\tCtrl-E",
252 "Enable or disable the last menu", TRUE
);
253 menubarMenu
->AppendSeparator();
254 menubarMenu
->Append(Menu_MenuBar_GetLabel
, "&Get menu label\tCtrl-G",
255 "Get the label of the last menu");
256 menubarMenu
->Append(Menu_MenuBar_SetLabel
, "&Set menu label\tCtrl-S",
257 "Change the label of the last menu");
259 wxMenu
*menuMenu
= new wxMenu
;
260 menuMenu
->Append(Menu_Menu_Append
, "&Append menu item\tAlt-A",
261 "Append a menu item to the last menu");
262 menuMenu
->Append(Menu_Menu_AppendSub
, "&Append sub menu\tAlt-S",
263 "Append a sub menu to the last menu");
264 menuMenu
->Append(Menu_Menu_Insert
, "&Insert menu item\tAlt-I",
265 "Insert a menu item in head of the last menu");
266 menuMenu
->Append(Menu_Menu_Delete
, "&Delete menu item\tAlt-D",
267 "Delete the last menu item from the last menu");
268 menuMenu
->AppendSeparator();
269 menuMenu
->Append(Menu_Menu_Enable
, "&Enable menu item\tAlt-E",
270 "Enable or disable the last menu item", TRUE
);
271 menuMenu
->Append(Menu_Menu_Check
, "&Check menu item\tAlt-C",
272 "Check or uncheck the last menu item", TRUE
);
273 menuMenu
->AppendSeparator();
274 menuMenu
->Append(Menu_Menu_GetLabel
, "&Get menu item label\tAlt-G",
275 "Get the label of the last menu item");
276 menuMenu
->Append(Menu_Menu_SetLabel
, "&Set menu item label\tAlt-S",
277 "Change the label of the last menu item");
278 menuMenu
->AppendSeparator();
279 menuMenu
->Append(Menu_Menu_GetInfo
, "Get menu item in&fo\tAlt-F",
280 "Show the state of the last menu item");
282 wxMenu
*helpMenu
= new wxMenu
;
283 helpMenu
->Append(Menu_Help_About
, "&About\tF1", "About menu sample");
285 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
287 menuBar
->Append(fileMenu
, "&File");
288 menuBar
->Append(menubarMenu
, "Menu&bar");
289 menuBar
->Append(menuMenu
, "&Menu");
290 menuBar
->Append(helpMenu
, "&Help");
292 // these items should be initially checked
293 menuBar
->Check(Menu_MenuBar_Toggle
, TRUE
);
294 menuBar
->Check(Menu_MenuBar_Enable
, TRUE
);
295 menuBar
->Check(Menu_Menu_Enable
, TRUE
);
296 menuBar
->Check(Menu_Menu_Check
, FALSE
);
298 // associate the menu bar with the frame
301 // intercept all menu events and log them in this custom event handler
302 PushEventHandler(new MyEvtHandler(this));
309 // delete the event handler installed in ctor
310 PopEventHandler(TRUE
);
313 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
315 wxMenu
*menu
= new wxMenu
;
316 menu
->Append(Menu_Dummy_First
, "First item\tCtrl-F1");
317 menu
->AppendSeparator();
318 menu
->Append(Menu_Dummy_Second
, "Second item\tCtrl-F2", "", TRUE
);
322 title
->Printf("Dummy menu &%d", ++m_countDummy
);
328 wxMenuItem
*MyFrame::GetLastMenuItem() const
330 wxMenuBar
*menubar
= GetMenuBar();
331 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
333 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
336 wxLogWarning("No last item in the last menu!");
342 return node
->GetData();
346 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
348 int id
= event
.GetId();
349 if (!GetMenuBar()->FindItem(id
))
351 wxString msg
= wxString::Format("Menu command %d", id
);
352 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
354 msg
+= wxString::Format(" (the item is currently %schecked)",
355 event
.IsChecked() ? "" : "not ");
358 SetStatusText(msg
, 1);
361 // ----------------------------------------------------------------------------
363 // ----------------------------------------------------------------------------
365 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
370 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
372 (void)wxMessageBox("wxWindows toolbar sample",
373 "About wxWindows menu sample",
377 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
379 wxMenuBar
*mbar
= GetMenuBar();
381 size_t count
= mbar
->GetMenuCount();
384 // don't let delete the first 2 menus
385 wxLogError("Can't delete any more menus");
389 delete mbar
->Remove(count
- 1);
393 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
396 wxMenu
*menu
= CreateDummyMenu(&title
);
397 GetMenuBar()->Insert(0, menu
, title
);
400 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
403 wxMenu
*menu
= CreateDummyMenu(&title
);
404 GetMenuBar()->Append(menu
, title
);
407 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
409 wxMenuBar
*mbar
= GetMenuBar();
413 m_menu
= mbar
->Remove(0);
418 mbar
->Insert(0, m_menu
, "&File");
423 void MyFrame::OnEnableMenu(wxCommandEvent
& WXUNUSED(event
))
425 wxMenuBar
*mbar
= GetMenuBar();
426 size_t count
= mbar
->GetMenuCount();
428 static bool s_enabled
= TRUE
;
430 s_enabled
= !s_enabled
;
431 mbar
->EnableTop(count
- 1, s_enabled
);
434 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
436 wxMenuBar
*mbar
= GetMenuBar();
437 size_t count
= mbar
->GetMenuCount();
439 wxLogMessage("The label of the last menu item is '%s'",
440 mbar
->GetLabelTop(count
- 1).c_str());
443 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
445 wxMenuBar
*mbar
= GetMenuBar();
446 size_t count
= mbar
->GetMenuCount();
448 mbar
->SetLabelTop(count
- 1, "Dummy label &0");
451 void MyFrame::OnDummy(wxCommandEvent
& event
)
454 s
.Printf("Dummy item #%d", event
.GetId() - Menu_Dummy_First
+ 1);
455 wxMessageBox(s
, "Menu sample", wxICON_INFORMATION
);
458 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
460 wxMenuBar
*menubar
= GetMenuBar();
461 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
463 menu
->AppendSeparator();
464 menu
->Append(Menu_Dummy_Third
, "Third dummy item\tCtrl-F3",
465 "Checkable item", TRUE
);
468 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
470 wxMenuBar
*menubar
= GetMenuBar();
472 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
474 menu
->Append(Menu_Dummy_Last
, "Dummy sub menu",
475 CreateDummyMenu(NULL
), "Dummy sub menu help");
478 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
480 wxMenuBar
*menubar
= GetMenuBar();
481 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
483 size_t count
= menu
->GetMenuItemCount();
486 wxLogWarning("No items to delete!");
490 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
494 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
496 wxMenuBar
*menubar
= GetMenuBar();
497 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
499 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
500 "Fourth dummy item\tCtrl-F4"));
501 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, ""));
504 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
506 wxMenuItem
*item
= GetLastMenuItem();
510 item
->Enable(!item
->IsEnabled());
514 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
516 wxMenuItem
*item
= GetLastMenuItem();
521 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
525 wxMenuItem
*item
= GetLastMenuItem();
527 event
.Enable(item
&& item
->IsCheckable());
530 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
532 wxMenuItem
*item
= GetLastMenuItem();
536 wxLogMessage("The label of the last menu item is '%s'",
537 item
->GetLabel().c_str());
541 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
543 wxMenuItem
*item
= GetLastMenuItem();
547 item
->SetText("Dummy menu item text");
551 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
553 wxMenuItem
*item
= GetLastMenuItem();
558 msg
<< "The item is " << (item
->IsEnabled() ? "enabled"
562 if ( item
->IsCheckable() )
564 msg
<< "It is checkable and " << (item
->IsChecked() ? "" : "un")
569 wxAcceleratorEntry
*accel
= item
->GetAccel();
572 msg
<< "Its accelerator is ";
574 int flags
= accel
->GetFlags();
575 if ( flags
& wxACCEL_ALT
)
577 if ( flags
& wxACCEL_CTRL
)
579 if ( flags
& wxACCEL_SHIFT
)
580 msg
<< wxT("Shift-");
582 int code
= accel
->GetKeyCode();
597 msg
<< wxT('F') << code
- WXK_F1
+ 1;
600 // if there are any other keys wxGetAccelFromString() may return,
601 // we should process them here
604 if ( wxIsalnum(code
) )
611 wxFAIL_MSG( wxT("unknown keyboard accel") );
618 msg
<< "It doesn't have an accelerator";
620 #endif // wxUSE_ACCEL
626 void MyFrame::OnRightDown(wxMouseEvent
&event
)
628 wxMenu
menu("Test popup");
630 menu
.Append(Menu_Help_About
, "&About");
631 menu
.Append(Menu_Popup_Submenu
, "Submenu", CreateDummyMenu(NULL
));
632 menu
.Append(Menu_Popup_ToBeDeleted
, "To be deleted");
633 menu
.Append(Menu_Popup_ToBeChecked
, "To be checked", "", TRUE
);
634 menu
.Append(Menu_Popup_ToBeGreyed
, "To be greyed");
635 menu
.AppendSeparator();
636 menu
.Append(Menu_File_Quit
, "E&xit");
638 menu
.Delete(Menu_Popup_ToBeDeleted
);
639 menu
.Check(Menu_Popup_ToBeChecked
, TRUE
);
640 menu
.Enable(Menu_Popup_ToBeGreyed
, FALSE
);
642 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
644 // test for destroying items in popup menus
646 menu
.Destroy(Menu_Popup_Submenu
);
648 PopupMenu( &menu
, event
.GetX(), event
.GetY() );