]>
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>
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 wxString msg
= wxString::Format("Menu command %d", id
);
350 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
352 msg
+= wxString::Format(" (the item is currently %schecked)",
353 event
.IsChecked() ? "" : "not ");
356 SetStatusText(msg
, 1);
359 // ----------------------------------------------------------------------------
361 // ----------------------------------------------------------------------------
363 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
368 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
370 (void)wxMessageBox("wxWindows toolbar sample",
371 "About wxWindows menu sample",
375 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
377 wxMenuBar
*mbar
= GetMenuBar();
379 size_t count
= mbar
->GetMenuCount();
382 // don't let delete the first 2 menus
383 wxLogError("Can't delete any more menus");
387 delete mbar
->Remove(count
- 1);
391 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
394 wxMenu
*menu
= CreateDummyMenu(&title
);
395 GetMenuBar()->Insert(0, menu
, title
);
398 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
401 wxMenu
*menu
= CreateDummyMenu(&title
);
402 GetMenuBar()->Append(menu
, title
);
405 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
407 wxMenuBar
*mbar
= GetMenuBar();
411 m_menu
= mbar
->Remove(0);
416 mbar
->Insert(0, m_menu
, "&File");
421 void MyFrame::OnEnableMenu(wxCommandEvent
& WXUNUSED(event
))
423 wxMenuBar
*mbar
= GetMenuBar();
424 size_t count
= mbar
->GetMenuCount();
426 static bool s_enabled
= TRUE
;
428 s_enabled
= !s_enabled
;
429 mbar
->EnableTop(count
- 1, s_enabled
);
432 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
434 wxMenuBar
*mbar
= GetMenuBar();
435 size_t count
= mbar
->GetMenuCount();
437 wxLogMessage("The label of the last menu item is '%s'",
438 mbar
->GetLabelTop(count
- 1).c_str());
441 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
443 wxMenuBar
*mbar
= GetMenuBar();
444 size_t count
= mbar
->GetMenuCount();
446 mbar
->SetLabelTop(count
- 1, "Dummy label &0");
449 void MyFrame::OnDummy(wxCommandEvent
& event
)
452 s
.Printf("Dummy item #%d", event
.GetId() - Menu_Dummy_First
+ 1);
453 wxMessageBox(s
, "Menu sample", wxICON_INFORMATION
);
456 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
458 wxMenuBar
*menubar
= GetMenuBar();
459 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
461 menu
->AppendSeparator();
462 menu
->Append(Menu_Dummy_Third
, "Third dummy item\tCtrl-F3",
463 "Checkable item", TRUE
);
466 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
468 wxMenuBar
*menubar
= GetMenuBar();
470 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
472 menu
->Append(Menu_Dummy_Last
, "Dummy sub menu",
473 CreateDummyMenu(NULL
), "Dummy sub menu help");
476 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
478 wxMenuBar
*menubar
= GetMenuBar();
479 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
481 size_t count
= menu
->GetMenuItemCount();
484 wxLogWarning("No items to delete!");
488 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
492 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
494 wxMenuBar
*menubar
= GetMenuBar();
495 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
497 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
498 "Fourth dummy item\tCtrl-F4"));
499 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, ""));
502 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
504 wxMenuItem
*item
= GetLastMenuItem();
508 item
->Enable(!item
->IsEnabled());
512 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
514 wxMenuItem
*item
= GetLastMenuItem();
519 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
523 wxMenuItem
*item
= GetLastMenuItem();
525 event
.Enable(item
&& item
->IsCheckable());
528 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
530 wxMenuItem
*item
= GetLastMenuItem();
534 wxLogMessage("The label of the last menu item is '%s'",
535 item
->GetLabel().c_str());
539 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
541 wxMenuItem
*item
= GetLastMenuItem();
545 item
->SetText("Dummy menu item text");
549 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
551 wxMenuItem
*item
= GetLastMenuItem();
556 msg
<< "The item is " << (item
->IsEnabled() ? "enabled"
560 if ( item
->IsCheckable() )
562 msg
<< "It is checkable and " << (item
->IsChecked() ? "" : "un")
567 wxAcceleratorEntry
*accel
= item
->GetAccel();
570 msg
<< "Its accelerator is ";
572 int flags
= accel
->GetFlags();
573 if ( flags
& wxACCEL_ALT
)
575 if ( flags
& wxACCEL_CTRL
)
577 if ( flags
& wxACCEL_SHIFT
)
578 msg
<< wxT("Shift-");
580 int code
= accel
->GetKeyCode();
595 msg
<< wxT('F') << code
- WXK_F1
+ 1;
598 // if there are any other keys wxGetAccelFromString() may return,
599 // we should process them here
602 if ( wxIsalnum(code
) )
609 wxFAIL_MSG( wxT("unknown keyboard accel") );
616 msg
<< "It doesn't have an accelerator";
618 #endif // wxUSE_ACCEL
624 void MyFrame::OnRightDown(wxMouseEvent
&event
)
626 wxMenu
menu("Test popup");
628 menu
.Append(Menu_Help_About
, "&About");
629 menu
.Append(Menu_Popup_Submenu
, "Submenu", CreateDummyMenu(NULL
));
630 menu
.Append(Menu_Popup_ToBeDeleted
, "To be deleted");
631 menu
.Append(Menu_Popup_ToBeChecked
, "To be checked", "", TRUE
);
632 menu
.Append(Menu_Popup_ToBeGreyed
, "To be greyed");
633 menu
.AppendSeparator();
634 menu
.Append(Menu_File_Quit
, "E&xit");
636 menu
.Delete(Menu_Popup_ToBeDeleted
);
637 menu
.Check(Menu_Popup_ToBeChecked
, TRUE
);
638 menu
.Enable(Menu_Popup_ToBeGreyed
, FALSE
);
640 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
642 // test for destroying items in popup menus
644 menu
.Destroy(Menu_Popup_Submenu
);
646 PopupMenu( &menu
, event
.GetX(), event
.GetY() );