]>
git.saurik.com Git - wxWidgets.git/blob - samples/menu/menu.cpp
b4fa149155cd2f8ca796f9fd7f9897218f8d08cb
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
50 virtual ~MyFrame() { delete m_menu
; }
52 void OnQuit(wxCommandEvent
& event
);
53 void OnAbout(wxCommandEvent
& event
);
55 void OnDummy(wxCommandEvent
& event
);
57 void OnAppendMenuItem(wxCommandEvent
& event
);
58 void OnAppendSubMenu(wxCommandEvent
& event
);
59 void OnDeleteMenuItem(wxCommandEvent
& event
);
60 void OnInsertMenuItem(wxCommandEvent
& event
);
61 void OnCheckMenuItem(wxCommandEvent
& event
);
62 void OnEnableMenuItem(wxCommandEvent
& event
);
63 void OnGetLabelMenuItem(wxCommandEvent
& event
);
64 void OnSetLabelMenuItem(wxCommandEvent
& event
);
65 void OnGetMenuItemInfo(wxCommandEvent
& event
);
67 void OnAppendMenu(wxCommandEvent
& event
);
68 void OnDeleteMenu(wxCommandEvent
& event
);
69 void OnToggleMenu(wxCommandEvent
& event
);
70 void OnEnableMenu(wxCommandEvent
& event
);
71 void OnGetLabelMenu(wxCommandEvent
& event
);
72 void OnSetLabelMenu(wxCommandEvent
& event
);
74 void OnRightDown(wxMouseEvent
& event
);
76 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
79 wxMenu
*CreateDummyMenu();
81 wxMenuItem
*GetLastMenuItem() const;
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
96 Menu_MenuBar_Toggle
= 200,
100 Menu_MenuBar_GetLabel
,
101 Menu_MenuBar_SetLabel
,
103 Menu_Menu_Append
= 300,
113 Menu_Dummy_First
= 400,
119 Menu_Help_About
= 1000,
121 Menu_Popup_ToBeDeleted
= 2000,
122 Menu_Popup_ToBeGreyed
,
123 Menu_Popup_ToBeChecked
,
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
134 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
136 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
138 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
139 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
140 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
141 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
142 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
143 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
145 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
146 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
147 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
148 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
149 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
150 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
151 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
152 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
153 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
155 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
157 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
159 EVT_RIGHT_DOWN(MyFrame::OnRightDown
)
162 // ============================================================================
164 // ============================================================================
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
172 // The `main program' equivalent, creating the windows and returning the
176 // Create the main frame window
177 MyFrame
* frame
= new MyFrame
;
181 frame
->SetStatusText("Hello, wxWindows");
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 // Define my frame constructor
194 : wxFrame((wxFrame
*)NULL
, -1, "wxWindows menu sample",
195 wxDefaultPosition
, wxSize(300, 200))
201 // create the menubar
202 wxMenu
*fileMenu
= new wxMenu
;
203 fileMenu
->Append(Menu_File_Quit
, "E&xit\tAlt-X", "Quit toolbar sample" );
205 wxMenu
*menubarMenu
= new wxMenu
;
206 menubarMenu
->Append(Menu_MenuBar_Append
, "&Append menu\tCtrl-A",
207 "Append a menu to the menubar");
208 menubarMenu
->Append(Menu_MenuBar_Delete
, "&Delete menu\tCtrl-D",
209 "Delete the last menu from the menubar");
210 menubarMenu
->Append(Menu_MenuBar_Toggle
, "&Toggle menu\tCtrl-T",
211 "Toggle the first menu in the menubar", TRUE
);
212 menubarMenu
->AppendSeparator();
213 menubarMenu
->Append(Menu_MenuBar_Enable
, "&Enable menu\tCtrl-E",
214 "Enable or disable the last menu", TRUE
);
215 menubarMenu
->AppendSeparator();
216 menubarMenu
->Append(Menu_MenuBar_GetLabel
, "&Get menu label\tCtrl-G",
217 "Get the label of the last menu");
218 menubarMenu
->Append(Menu_MenuBar_SetLabel
, "&Set menu label\tCtrl-S",
219 "Change the label of the last menu");
221 wxMenu
*menuMenu
= new wxMenu
;
222 menuMenu
->Append(Menu_Menu_Append
, "&Append menu item\tAlt-A",
223 "Append a menu item to the last menu");
224 menuMenu
->Append(Menu_Menu_AppendSub
, "&Append sub menu\tAlt-S",
225 "Append a sub menu to the last menu");
226 menuMenu
->Append(Menu_Menu_Insert
, "&Insert menu item\tAlt-I",
227 "Insert a menu item in head of the last menu");
228 menuMenu
->Append(Menu_Menu_Delete
, "&Delete menu item\tAlt-D",
229 "Delete the last menu item from the last menu");
230 menuMenu
->AppendSeparator();
231 menuMenu
->Append(Menu_Menu_Enable
, "&Enable menu item\tAlt-E",
232 "Enable or disable the last menu item", TRUE
);
233 menuMenu
->Append(Menu_Menu_Check
, "&Check menu item\tAlt-C",
234 "Check or uncheck the last menu item", TRUE
);
235 menuMenu
->AppendSeparator();
236 menuMenu
->Append(Menu_Menu_GetLabel
, "&Get menu item label\tAlt-G",
237 "Get the label of the last menu item");
238 menuMenu
->Append(Menu_Menu_SetLabel
, "&Set menu item label\tAlt-S",
239 "Change the label of the last menu item");
240 menuMenu
->AppendSeparator();
241 menuMenu
->Append(Menu_Menu_GetInfo
, "Get menu item in&fo\tAlt-F",
242 "Show the state of the last menu item");
244 wxMenu
*helpMenu
= new wxMenu
;
245 helpMenu
->Append(Menu_Help_About
, "&About\tF1", "About menu sample");
247 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
249 menuBar
->Append(fileMenu
, "&File");
250 menuBar
->Append(menubarMenu
, "Menu&bar");
251 menuBar
->Append(menuMenu
, "&Menu");
252 menuBar
->Append(helpMenu
, "&Help");
254 // these items should be initially checked
255 menuBar
->Check(Menu_MenuBar_Toggle
, TRUE
);
256 menuBar
->Check(Menu_MenuBar_Enable
, TRUE
);
257 menuBar
->Check(Menu_Menu_Enable
, TRUE
);
258 menuBar
->Check(Menu_Menu_Check
, FALSE
);
260 // associate the menu bar with the frame
264 wxMenu
*MyFrame::CreateDummyMenu()
266 wxMenu
*menu
= new wxMenu
;
267 menu
->Append(Menu_Dummy_First
, "First item\tCtrl-F1");
268 menu
->AppendSeparator();
269 menu
->Append(Menu_Dummy_Second
, "Second item\tCtrl-F2", "", TRUE
);
274 wxMenuItem
*MyFrame::GetLastMenuItem() const
276 wxMenuBar
*menubar
= GetMenuBar();
277 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
279 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
282 wxLogWarning("No last item in the last menu!");
288 return node
->GetData();
292 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
297 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
299 (void)wxMessageBox("wxWindows toolbar sample",
300 "About wxWindows menu sample",
304 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
306 wxMenuBar
*mbar
= GetMenuBar();
308 size_t count
= mbar
->GetMenuCount();
311 // don't let delete the first 2 menus
312 wxLogError("Can't delete any more menus");
316 delete mbar
->Remove(count
- 1);
320 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
322 static int s_count
= 0;
325 title
.Printf("Dummy menu &%d", ++s_count
);
327 GetMenuBar()->Append(CreateDummyMenu(), title
);
330 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
332 wxMenuBar
*mbar
= GetMenuBar();
336 m_menu
= mbar
->Remove(0);
341 mbar
->Insert(0, m_menu
, "&File");
346 void MyFrame::OnEnableMenu(wxCommandEvent
& WXUNUSED(event
))
348 wxMenuBar
*mbar
= GetMenuBar();
349 size_t count
= mbar
->GetMenuCount();
351 static bool s_enabled
= TRUE
;
353 s_enabled
= !s_enabled
;
354 mbar
->EnableTop(count
- 1, s_enabled
);
357 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
359 wxMenuBar
*mbar
= GetMenuBar();
360 size_t count
= mbar
->GetMenuCount();
362 wxLogMessage("The label of the last menu item is '%s'",
363 mbar
->GetLabelTop(count
- 1).c_str());
366 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
368 wxMenuBar
*mbar
= GetMenuBar();
369 size_t count
= mbar
->GetMenuCount();
371 mbar
->SetLabelTop(count
- 1, "Dummy label &0");
374 void MyFrame::OnDummy(wxCommandEvent
& event
)
377 s
.Printf("Dummy item #%d", event
.GetId() - Menu_Dummy_First
+ 1);
378 wxMessageBox(s
, "Menu sample", wxICON_INFORMATION
);
381 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
383 wxMenuBar
*menubar
= GetMenuBar();
384 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
386 menu
->AppendSeparator();
387 menu
->Append(Menu_Dummy_Third
, "Third dummy item\tCtrl-F3",
388 "Checkable item", TRUE
);
391 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
393 wxMenuBar
*menubar
= GetMenuBar();
395 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
397 menu
->Append(Menu_Dummy_Last
, "Dummy sub menu\tCtrl-F2",
401 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
403 wxMenuBar
*menubar
= GetMenuBar();
404 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
406 size_t count
= menu
->GetMenuItemCount();
409 wxLogWarning("No items to delete!");
413 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
417 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
419 wxMenuBar
*menubar
= GetMenuBar();
420 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
422 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
423 "Fourth dummy item\tCtrl-F4"));
424 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, ""));
427 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
429 wxMenuItem
*item
= GetLastMenuItem();
433 item
->Enable(!item
->IsEnabled());
437 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
439 wxMenuItem
*item
= GetLastMenuItem();
444 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
446 wxMenuItem
*item
= GetLastMenuItem();
448 event
.Enable(item
&& item
->IsCheckable());
451 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
453 wxMenuItem
*item
= GetLastMenuItem();
457 wxLogMessage("The label of the last menu item is '%s'",
458 item
->GetLabel().c_str());
462 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
464 wxMenuItem
*item
= GetLastMenuItem();
468 item
->SetText("Dummy menu item text");
472 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
474 wxMenuItem
*item
= GetLastMenuItem();
479 msg
<< "The item is " << (item
->IsEnabled() ? "enabled"
483 if ( item
->IsCheckable() )
485 msg
<< "It is checkable and " << (item
->IsChecked() ? "" : "un")
490 wxAcceleratorEntry
*accel
= item
->GetAccel();
493 msg
<< "Its accelerator is ";
495 int flags
= accel
->GetFlags();
496 if ( flags
& wxACCEL_ALT
)
498 if ( flags
& wxACCEL_CTRL
)
500 if ( flags
& wxACCEL_SHIFT
)
501 msg
<< wxT("Shift-");
503 int code
= accel
->GetKeyCode();
518 msg
<< wxT('F') << code
- WXK_F1
+ 1;
521 // if there are any other keys wxGetAccelFromString() may return,
522 // we should process them here
525 if ( wxIsalnum(code
) )
532 wxFAIL_MSG( wxT("unknown keyboard accel") );
539 msg
<< "It doesn't have an accelerator";
541 #endif // wxUSE_ACCEL
547 void MyFrame::OnRightDown(wxMouseEvent
&event
)
549 wxMenu
menu("Test popup");
551 menu
.Append(Menu_Help_About
, "&About");
552 menu
.Append(Menu_Popup_Submenu
, "Submenu", CreateDummyMenu());
553 menu
.Append(Menu_Popup_ToBeDeleted
, "To be deleted");
554 menu
.Append(Menu_Popup_ToBeChecked
, "To be checked", "", TRUE
);
555 menu
.Append(Menu_Popup_ToBeGreyed
, "To be greyed");
556 menu
.AppendSeparator();
557 menu
.Append(Menu_File_Quit
, "E&xit");
559 //menu.Delete(Menu_Popup_ToBeDeleted);
560 menu
.Check(Menu_Popup_ToBeChecked
, TRUE
);
561 menu
.Enable(Menu_Popup_ToBeGreyed
, FALSE
);
563 PopupMenu( &menu
, event
.GetX(), event
.GetY() );