]>
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
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 OnInsertMenu(wxCommandEvent
& event
);
69 void OnDeleteMenu(wxCommandEvent
& event
);
70 void OnToggleMenu(wxCommandEvent
& event
);
71 void OnEnableMenu(wxCommandEvent
& event
);
72 void OnGetLabelMenu(wxCommandEvent
& event
);
73 void OnSetLabelMenu(wxCommandEvent
& event
);
75 void OnRightDown(wxMouseEvent
& event
);
77 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
80 wxMenu
*CreateDummyMenu(wxString
*title
);
82 wxMenuItem
*GetLastMenuItem() const;
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
99 Menu_MenuBar_Toggle
= 200,
104 Menu_MenuBar_GetLabel
,
105 Menu_MenuBar_SetLabel
,
107 Menu_Menu_Append
= 300,
117 Menu_Dummy_First
= 400,
123 Menu_Help_About
= 1000,
125 Menu_Popup_ToBeDeleted
= 2000,
126 Menu_Popup_ToBeGreyed
,
127 Menu_Popup_ToBeChecked
,
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
138 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
140 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
142 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
143 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
144 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
145 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
146 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
147 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
148 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
150 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
151 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
152 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
153 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
154 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
155 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
156 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
157 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
158 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
160 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
162 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
164 EVT_RIGHT_DOWN(MyFrame::OnRightDown
)
167 // ============================================================================
169 // ============================================================================
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
177 // The `main program' equivalent, creating the windows and returning the
181 // Create the main frame window
182 MyFrame
* frame
= new MyFrame
;
186 frame
->SetStatusText("Hello, wxWindows");
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
197 // Define my frame constructor
199 : wxFrame((wxFrame
*)NULL
, -1, "wxWindows menu sample",
200 wxDefaultPosition
, wxSize(300, 200))
207 // create the menubar
208 wxMenu
*fileMenu
= new wxMenu
;
209 fileMenu
->Append(Menu_File_Quit
, "E&xit\tAlt-X", "Quit toolbar sample" );
211 wxMenu
*menubarMenu
= new wxMenu
;
212 menubarMenu
->Append(Menu_MenuBar_Append
, "&Append menu\tCtrl-A",
213 "Append a menu to the menubar");
214 menubarMenu
->Append(Menu_MenuBar_Insert
, "&Insert menu\tCtrl-I",
215 "Insert a menu into the menubar");
216 menubarMenu
->Append(Menu_MenuBar_Delete
, "&Delete menu\tCtrl-D",
217 "Delete the last menu from the menubar");
218 menubarMenu
->Append(Menu_MenuBar_Toggle
, "&Toggle menu\tCtrl-T",
219 "Toggle the first menu in the menubar", TRUE
);
220 menubarMenu
->AppendSeparator();
221 menubarMenu
->Append(Menu_MenuBar_Enable
, "&Enable menu\tCtrl-E",
222 "Enable or disable the last menu", TRUE
);
223 menubarMenu
->AppendSeparator();
224 menubarMenu
->Append(Menu_MenuBar_GetLabel
, "&Get menu label\tCtrl-G",
225 "Get the label of the last menu");
226 menubarMenu
->Append(Menu_MenuBar_SetLabel
, "&Set menu label\tCtrl-S",
227 "Change the label of the last menu");
229 wxMenu
*menuMenu
= new wxMenu
;
230 menuMenu
->Append(Menu_Menu_Append
, "&Append menu item\tAlt-A",
231 "Append a menu item to the last menu");
232 menuMenu
->Append(Menu_Menu_AppendSub
, "&Append sub menu\tAlt-S",
233 "Append a sub menu to the last menu");
234 menuMenu
->Append(Menu_Menu_Insert
, "&Insert menu item\tAlt-I",
235 "Insert a menu item in head of the last menu");
236 menuMenu
->Append(Menu_Menu_Delete
, "&Delete menu item\tAlt-D",
237 "Delete the last menu item from the last menu");
238 menuMenu
->AppendSeparator();
239 menuMenu
->Append(Menu_Menu_Enable
, "&Enable menu item\tAlt-E",
240 "Enable or disable the last menu item", TRUE
);
241 menuMenu
->Append(Menu_Menu_Check
, "&Check menu item\tAlt-C",
242 "Check or uncheck the last menu item", TRUE
);
243 menuMenu
->AppendSeparator();
244 menuMenu
->Append(Menu_Menu_GetLabel
, "&Get menu item label\tAlt-G",
245 "Get the label of the last menu item");
246 menuMenu
->Append(Menu_Menu_SetLabel
, "&Set menu item label\tAlt-S",
247 "Change the label of the last menu item");
248 menuMenu
->AppendSeparator();
249 menuMenu
->Append(Menu_Menu_GetInfo
, "Get menu item in&fo\tAlt-F",
250 "Show the state of the last menu item");
252 wxMenu
*helpMenu
= new wxMenu
;
253 helpMenu
->Append(Menu_Help_About
, "&About\tF1", "About menu sample");
255 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
257 menuBar
->Append(fileMenu
, "&File");
258 menuBar
->Append(menubarMenu
, "Menu&bar");
259 menuBar
->Append(menuMenu
, "&Menu");
260 menuBar
->Append(helpMenu
, "&Help");
262 // these items should be initially checked
263 menuBar
->Check(Menu_MenuBar_Toggle
, TRUE
);
264 menuBar
->Check(Menu_MenuBar_Enable
, TRUE
);
265 menuBar
->Check(Menu_Menu_Enable
, TRUE
);
266 menuBar
->Check(Menu_Menu_Check
, FALSE
);
268 // associate the menu bar with the frame
272 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
274 wxMenu
*menu
= new wxMenu
;
275 menu
->Append(Menu_Dummy_First
, "First item\tCtrl-F1");
276 menu
->AppendSeparator();
277 menu
->Append(Menu_Dummy_Second
, "Second item\tCtrl-F2", "", TRUE
);
281 title
->Printf("Dummy menu &%d", ++m_countDummy
);
287 wxMenuItem
*MyFrame::GetLastMenuItem() const
289 wxMenuBar
*menubar
= GetMenuBar();
290 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
292 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
295 wxLogWarning("No last item in the last menu!");
301 return node
->GetData();
305 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
310 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
312 (void)wxMessageBox("wxWindows toolbar sample",
313 "About wxWindows menu sample",
317 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
319 wxMenuBar
*mbar
= GetMenuBar();
321 size_t count
= mbar
->GetMenuCount();
324 // don't let delete the first 2 menus
325 wxLogError("Can't delete any more menus");
329 delete mbar
->Remove(count
- 1);
333 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
336 wxMenu
*menu
= CreateDummyMenu(&title
);
337 GetMenuBar()->Insert(0, menu
, title
);
340 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
343 wxMenu
*menu
= CreateDummyMenu(&title
);
344 GetMenuBar()->Append(menu
, title
);
347 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
349 wxMenuBar
*mbar
= GetMenuBar();
353 m_menu
= mbar
->Remove(0);
358 mbar
->Insert(0, m_menu
, "&File");
363 void MyFrame::OnEnableMenu(wxCommandEvent
& WXUNUSED(event
))
365 wxMenuBar
*mbar
= GetMenuBar();
366 size_t count
= mbar
->GetMenuCount();
368 static bool s_enabled
= TRUE
;
370 s_enabled
= !s_enabled
;
371 mbar
->EnableTop(count
- 1, s_enabled
);
374 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
376 wxMenuBar
*mbar
= GetMenuBar();
377 size_t count
= mbar
->GetMenuCount();
379 wxLogMessage("The label of the last menu item is '%s'",
380 mbar
->GetLabelTop(count
- 1).c_str());
383 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
385 wxMenuBar
*mbar
= GetMenuBar();
386 size_t count
= mbar
->GetMenuCount();
388 mbar
->SetLabelTop(count
- 1, "Dummy label &0");
391 void MyFrame::OnDummy(wxCommandEvent
& event
)
394 s
.Printf("Dummy item #%d", event
.GetId() - Menu_Dummy_First
+ 1);
395 wxMessageBox(s
, "Menu sample", wxICON_INFORMATION
);
398 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
400 wxMenuBar
*menubar
= GetMenuBar();
401 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
403 menu
->AppendSeparator();
404 menu
->Append(Menu_Dummy_Third
, "Third dummy item\tCtrl-F3",
405 "Checkable item", TRUE
);
408 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
410 wxMenuBar
*menubar
= GetMenuBar();
412 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
414 menu
->Append(Menu_Dummy_Last
, "Dummy sub menu\tCtrl-F2",
415 CreateDummyMenu(NULL
));
418 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
420 wxMenuBar
*menubar
= GetMenuBar();
421 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
423 size_t count
= menu
->GetMenuItemCount();
426 wxLogWarning("No items to delete!");
430 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
434 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
436 wxMenuBar
*menubar
= GetMenuBar();
437 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
439 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
440 "Fourth dummy item\tCtrl-F4"));
441 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, ""));
444 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
446 wxMenuItem
*item
= GetLastMenuItem();
450 item
->Enable(!item
->IsEnabled());
454 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
456 wxMenuItem
*item
= GetLastMenuItem();
461 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
465 wxMenuItem
*item
= GetLastMenuItem();
467 event
.Enable(item
&& item
->IsCheckable());
470 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
472 wxMenuItem
*item
= GetLastMenuItem();
476 wxLogMessage("The label of the last menu item is '%s'",
477 item
->GetLabel().c_str());
481 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
483 wxMenuItem
*item
= GetLastMenuItem();
487 item
->SetText("Dummy menu item text");
491 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
493 wxMenuItem
*item
= GetLastMenuItem();
498 msg
<< "The item is " << (item
->IsEnabled() ? "enabled"
502 if ( item
->IsCheckable() )
504 msg
<< "It is checkable and " << (item
->IsChecked() ? "" : "un")
509 wxAcceleratorEntry
*accel
= item
->GetAccel();
512 msg
<< "Its accelerator is ";
514 int flags
= accel
->GetFlags();
515 if ( flags
& wxACCEL_ALT
)
517 if ( flags
& wxACCEL_CTRL
)
519 if ( flags
& wxACCEL_SHIFT
)
520 msg
<< wxT("Shift-");
522 int code
= accel
->GetKeyCode();
537 msg
<< wxT('F') << code
- WXK_F1
+ 1;
540 // if there are any other keys wxGetAccelFromString() may return,
541 // we should process them here
544 if ( wxIsalnum(code
) )
551 wxFAIL_MSG( wxT("unknown keyboard accel") );
558 msg
<< "It doesn't have an accelerator";
560 #endif // wxUSE_ACCEL
566 void MyFrame::OnRightDown(wxMouseEvent
&event
)
568 wxMenu
menu("Test popup");
570 menu
.Append(Menu_Help_About
, "&About");
571 menu
.Append(Menu_Popup_Submenu
, "Submenu", CreateDummyMenu(NULL
));
572 menu
.Append(Menu_Popup_ToBeDeleted
, "To be deleted");
573 menu
.Append(Menu_Popup_ToBeChecked
, "To be checked", "", TRUE
);
574 menu
.Append(Menu_Popup_ToBeGreyed
, "To be greyed");
575 menu
.AppendSeparator();
576 menu
.Append(Menu_File_Quit
, "E&xit");
578 //menu.Delete(Menu_Popup_ToBeDeleted);
579 menu
.Check(Menu_Popup_ToBeChecked
, TRUE
);
580 menu
.Enable(Menu_Popup_ToBeGreyed
, FALSE
);
582 PopupMenu( &menu
, event
.GetX(), event
.GetY() );