]>
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 #error "menu sample requires wxUSE_MENUS=1"
39 #ifdef __WXUNIVERSAL__
40 #include "wx/univ/theme.h"
45 // not implemented yet
47 #endif // __WXUNIVERSAL__
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // Define a new application
54 class MyApp
: public wxApp
61 class MyFrame
: public wxFrame
68 void LogMenuEvent(const wxCommandEvent
& event
);
70 void OnQuit(wxCommandEvent
& event
);
71 void OnAbout(wxCommandEvent
& event
);
73 void OnDummy(wxCommandEvent
& event
);
75 void OnAppendMenuItem(wxCommandEvent
& event
);
76 void OnAppendSubMenu(wxCommandEvent
& event
);
77 void OnDeleteMenuItem(wxCommandEvent
& event
);
78 void OnInsertMenuItem(wxCommandEvent
& event
);
79 void OnCheckMenuItem(wxCommandEvent
& event
);
80 void OnEnableMenuItem(wxCommandEvent
& event
);
81 void OnGetLabelMenuItem(wxCommandEvent
& event
);
82 void OnSetLabelMenuItem(wxCommandEvent
& event
);
83 void OnGetMenuItemInfo(wxCommandEvent
& event
);
85 void OnAppendMenu(wxCommandEvent
& event
);
86 void OnInsertMenu(wxCommandEvent
& event
);
87 void OnDeleteMenu(wxCommandEvent
& event
);
88 void OnToggleMenu(wxCommandEvent
& event
);
89 void OnEnableMenu(wxCommandEvent
& event
);
90 void OnGetLabelMenu(wxCommandEvent
& event
);
91 void OnSetLabelMenu(wxCommandEvent
& event
);
93 void OnRightDown(wxMouseEvent
& event
);
95 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
98 wxMenu
*CreateDummyMenu(wxString
*title
);
100 wxMenuItem
*GetLastMenuItem() const;
106 DECLARE_EVENT_TABLE()
109 // A small helper class which intercepts all menu events and logs them
110 class MyEvtHandler
: public wxEvtHandler
113 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
115 void OnMenuEvent(wxCommandEvent
& event
)
117 m_frame
->LogMenuEvent(event
);
125 DECLARE_EVENT_TABLE()
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
134 Menu_File_Quit
= 100,
136 Menu_MenuBar_Toggle
= 200,
141 Menu_MenuBar_GetLabel
,
142 Menu_MenuBar_SetLabel
,
144 Menu_Menu_Append
= 300,
154 Menu_Dummy_First
= 400,
160 Menu_Help_About
= 1000,
162 Menu_Popup_ToBeDeleted
= 2000,
163 Menu_Popup_ToBeGreyed
,
164 Menu_Popup_ToBeChecked
,
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
175 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
177 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
179 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
180 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
181 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
182 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
183 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
184 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
185 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
187 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
188 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
189 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
190 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
191 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
192 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
193 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
194 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
195 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
197 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
199 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
201 EVT_RIGHT_DOWN(MyFrame::OnRightDown
)
204 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
205 EVT_MENU(-1, MyEvtHandler::OnMenuEvent
)
208 // ============================================================================
210 // ============================================================================
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
218 // The `main program' equivalent, creating the windows and returning the
222 // Create the main frame window
223 MyFrame
* frame
= new MyFrame
;
228 frame
->SetStatusText("Hello, wxWindows");
229 #endif // wxUSE_STATUSBAR
236 // ----------------------------------------------------------------------------
238 // ----------------------------------------------------------------------------
240 // Define my frame constructor
242 : wxFrame((wxFrame
*)NULL
, -1, "wxWindows menu sample",
243 wxDefaultPosition
, wxSize(300, 200))
250 #endif // wxUSE_STATUSBAR
252 // create the menubar
253 wxMenu
*fileMenu
= new wxMenu
;
254 fileMenu
->Append(Menu_File_Quit
, "E&xit\tAlt-X", "Quit toolbar sample");
256 // not supported just yet
257 #ifndef __WXUNIVERSAL__
258 wxMenuItem
*itemBitmap
= new wxMenuItem(fileMenu
, Menu_File_Quit
,
259 "Quit with &bitmap\tAlt-Q");
260 itemBitmap
->SetBitmap(wxBitmap(copy_xpm
));
261 fileMenu
->Append(itemBitmap
);
262 #endif // __WXUNIVERSAL__
264 wxMenu
*menubarMenu
= new wxMenu
;
265 menubarMenu
->Append(Menu_MenuBar_Append
, "&Append menu\tCtrl-A",
266 "Append a menu to the menubar");
267 menubarMenu
->Append(Menu_MenuBar_Insert
, "&Insert menu\tCtrl-I",
268 "Insert a menu into the menubar");
269 menubarMenu
->Append(Menu_MenuBar_Delete
, "&Delete menu\tCtrl-D",
270 "Delete the last menu from the menubar");
271 menubarMenu
->Append(Menu_MenuBar_Toggle
, "&Toggle menu\tCtrl-T",
272 "Toggle the first menu in the menubar", TRUE
);
273 menubarMenu
->AppendSeparator();
274 menubarMenu
->Append(Menu_MenuBar_Enable
, "&Enable menu\tCtrl-E",
275 "Enable or disable the last menu", TRUE
);
276 menubarMenu
->AppendSeparator();
277 menubarMenu
->Append(Menu_MenuBar_GetLabel
, "&Get menu label\tCtrl-G",
278 "Get the label of the last menu");
279 menubarMenu
->Append(Menu_MenuBar_SetLabel
, "&Set menu label\tCtrl-S",
280 "Change the label of the last menu");
282 wxMenu
*menuMenu
= new wxMenu
;
283 menuMenu
->Append(Menu_Menu_Append
, "&Append menu item\tAlt-A",
284 "Append a menu item to the last menu");
285 menuMenu
->Append(Menu_Menu_AppendSub
, "&Append sub menu\tAlt-S",
286 "Append a sub menu to the last menu");
287 menuMenu
->Append(Menu_Menu_Insert
, "&Insert menu item\tAlt-I",
288 "Insert a menu item in head of the last menu");
289 menuMenu
->Append(Menu_Menu_Delete
, "&Delete menu item\tAlt-D",
290 "Delete the last menu item from the last menu");
291 menuMenu
->AppendSeparator();
292 menuMenu
->Append(Menu_Menu_Enable
, "&Enable menu item\tAlt-E",
293 "Enable or disable the last menu item", TRUE
);
294 menuMenu
->Append(Menu_Menu_Check
, "&Check menu item\tAlt-C",
295 "Check or uncheck the last menu item", TRUE
);
296 menuMenu
->AppendSeparator();
297 menuMenu
->Append(Menu_Menu_GetLabel
, "&Get menu item label\tAlt-G",
298 "Get the label of the last menu item");
299 menuMenu
->Append(Menu_Menu_SetLabel
, "&Set menu item label\tAlt-S",
300 "Change the label of the last menu item");
301 menuMenu
->AppendSeparator();
302 menuMenu
->Append(Menu_Menu_GetInfo
, "Get menu item in&fo\tAlt-F",
303 "Show the state of the last menu item");
305 wxMenu
*helpMenu
= new wxMenu
;
306 helpMenu
->Append(Menu_Help_About
, "&About\tF1", "About menu sample");
308 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
310 menuBar
->Append(fileMenu
, "&File");
311 menuBar
->Append(menubarMenu
, "Menu&bar");
312 menuBar
->Append(menuMenu
, "&Menu");
313 menuBar
->Append(helpMenu
, "&Help");
315 // these items should be initially checked
316 menuBar
->Check(Menu_MenuBar_Toggle
, TRUE
);
317 menuBar
->Check(Menu_MenuBar_Enable
, TRUE
);
318 menuBar
->Check(Menu_Menu_Enable
, TRUE
);
319 menuBar
->Check(Menu_Menu_Check
, FALSE
);
321 // associate the menu bar with the frame
324 // intercept all menu events and log them in this custom event handler
325 PushEventHandler(new MyEvtHandler(this));
332 // delete the event handler installed in ctor
333 PopEventHandler(TRUE
);
336 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
338 wxMenu
*menu
= new wxMenu
;
339 menu
->Append(Menu_Dummy_First
, "First item\tCtrl-F1");
340 menu
->AppendSeparator();
341 menu
->Append(Menu_Dummy_Second
, "Second item\tCtrl-F2", "", TRUE
);
345 title
->Printf("Dummy menu &%d", ++m_countDummy
);
351 wxMenuItem
*MyFrame::GetLastMenuItem() const
353 wxMenuBar
*menubar
= GetMenuBar();
354 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
356 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
359 wxLogWarning("No last item in the last menu!");
365 return node
->GetData();
369 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
371 int id
= event
.GetId();
372 if (!GetMenuBar()->FindItem(id
))
374 wxString msg
= wxString::Format("Menu command %d", id
);
375 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
377 msg
+= wxString::Format(" (the item is currently %schecked)",
378 event
.IsChecked() ? "" : "not ");
382 SetStatusText(msg
, 1);
383 #endif // wxUSE_STATUSBAR
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
390 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
395 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
397 (void)wxMessageBox("wxWindows toolbar sample",
398 "About wxWindows menu sample",
402 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
404 wxMenuBar
*mbar
= GetMenuBar();
406 size_t count
= mbar
->GetMenuCount();
409 // don't let delete the first 2 menus
410 wxLogError("Can't delete any more menus");
414 delete mbar
->Remove(count
- 1);
418 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
421 wxMenu
*menu
= CreateDummyMenu(&title
);
422 GetMenuBar()->Insert(0, menu
, title
);
425 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
428 wxMenu
*menu
= CreateDummyMenu(&title
);
429 GetMenuBar()->Append(menu
, title
);
432 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
434 wxMenuBar
*mbar
= GetMenuBar();
438 m_menu
= mbar
->Remove(0);
443 mbar
->Insert(0, m_menu
, "&File");
448 void MyFrame::OnEnableMenu(wxCommandEvent
& WXUNUSED(event
))
450 wxMenuBar
*mbar
= GetMenuBar();
451 size_t count
= mbar
->GetMenuCount();
453 static bool s_enabled
= TRUE
;
455 s_enabled
= !s_enabled
;
456 mbar
->EnableTop(count
- 1, s_enabled
);
459 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
461 wxMenuBar
*mbar
= GetMenuBar();
462 size_t count
= mbar
->GetMenuCount();
464 wxLogMessage("The label of the last menu item is '%s'",
465 mbar
->GetLabelTop(count
- 1).c_str());
468 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
470 wxMenuBar
*mbar
= GetMenuBar();
471 size_t count
= mbar
->GetMenuCount();
473 mbar
->SetLabelTop(count
- 1, "Dummy label &0");
476 void MyFrame::OnDummy(wxCommandEvent
& event
)
479 s
.Printf("Dummy item #%d", event
.GetId() - Menu_Dummy_First
+ 1);
480 wxMessageBox(s
, "Menu sample", wxICON_INFORMATION
);
483 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
485 wxMenuBar
*menubar
= GetMenuBar();
486 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
488 menu
->AppendSeparator();
489 menu
->Append(Menu_Dummy_Third
, "Third dummy item\tCtrl-F3",
490 "Checkable item", TRUE
);
493 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
495 wxMenuBar
*menubar
= GetMenuBar();
497 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
499 menu
->Append(Menu_Dummy_Last
, "Dummy sub menu",
500 CreateDummyMenu(NULL
), "Dummy sub menu help");
503 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
505 wxMenuBar
*menubar
= GetMenuBar();
506 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
508 size_t count
= menu
->GetMenuItemCount();
511 wxLogWarning("No items to delete!");
515 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
519 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
521 wxMenuBar
*menubar
= GetMenuBar();
522 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
524 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
525 "Fourth dummy item\tCtrl-F4"));
526 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, ""));
529 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
531 wxMenuItem
*item
= GetLastMenuItem();
535 item
->Enable(!item
->IsEnabled());
539 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
541 wxMenuItem
*item
= GetLastMenuItem();
546 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
550 wxMenuItem
*item
= GetLastMenuItem();
552 event
.Enable(item
&& item
->IsCheckable());
555 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
557 wxMenuItem
*item
= GetLastMenuItem();
561 wxLogMessage("The label of the last menu item is '%s'",
562 item
->GetLabel().c_str());
566 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
568 wxMenuItem
*item
= GetLastMenuItem();
572 item
->SetText("Dummy menu item text");
576 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
578 wxMenuItem
*item
= GetLastMenuItem();
583 msg
<< "The item is " << (item
->IsEnabled() ? "enabled"
587 if ( item
->IsCheckable() )
589 msg
<< "It is checkable and " << (item
->IsChecked() ? "" : "un")
594 wxAcceleratorEntry
*accel
= item
->GetAccel();
597 msg
<< "Its accelerator is ";
599 int flags
= accel
->GetFlags();
600 if ( flags
& wxACCEL_ALT
)
602 if ( flags
& wxACCEL_CTRL
)
604 if ( flags
& wxACCEL_SHIFT
)
605 msg
<< wxT("Shift-");
607 int code
= accel
->GetKeyCode();
622 msg
<< wxT('F') << code
- WXK_F1
+ 1;
625 // if there are any other keys wxGetAccelFromString() may return,
626 // we should process them here
629 if ( wxIsalnum(code
) )
636 wxFAIL_MSG( wxT("unknown keyboard accel") );
643 msg
<< "It doesn't have an accelerator";
645 #endif // wxUSE_ACCEL
651 void MyFrame::OnRightDown(wxMouseEvent
&event
)
653 wxMenu
menu("Test popup");
655 menu
.Append(Menu_Help_About
, "&About");
656 menu
.Append(Menu_Popup_Submenu
, "Submenu", CreateDummyMenu(NULL
));
657 menu
.Append(Menu_Popup_ToBeDeleted
, "To be deleted");
658 menu
.Append(Menu_Popup_ToBeChecked
, "To be checked", "", TRUE
);
659 menu
.Append(Menu_Popup_ToBeGreyed
, "To be greyed");
660 menu
.AppendSeparator();
661 menu
.Append(Menu_File_Quit
, "E&xit");
663 menu
.Delete(Menu_Popup_ToBeDeleted
);
664 menu
.Check(Menu_Popup_ToBeChecked
, TRUE
);
665 menu
.Enable(Menu_Popup_ToBeGreyed
, FALSE
);
667 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
669 // test for destroying items in popup menus
671 menu
.Destroy(Menu_Popup_Submenu
);
673 PopupMenu( &menu
, event
.GetX(), event
.GetY() );