]>
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
);
66 void OnAppendMenu(wxCommandEvent
& event
);
67 void OnDeleteMenu(wxCommandEvent
& event
);
68 void OnToggleMenu(wxCommandEvent
& event
);
69 void OnEnableMenu(wxCommandEvent
& event
);
70 void OnGetLabelMenu(wxCommandEvent
& event
);
71 void OnSetLabelMenu(wxCommandEvent
& event
);
73 void OnRightDown(wxMouseEvent
& event
);
75 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
78 wxMenu
*CreateDummyMenu();
80 wxMenuItem
*GetLastMenuItem() const;
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
95 Menu_MenuBar_Toggle
= 200,
99 Menu_MenuBar_GetLabel
,
100 Menu_MenuBar_SetLabel
,
102 Menu_Menu_Append
= 300,
111 Menu_Dummy_First
= 400,
117 Menu_Help_About
= 1000,
119 Menu_Popup_ToBeDeleted
= 2000,
120 Menu_Popup_ToBeGreyed
,
121 Menu_Popup_ToBeChecked
,
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
132 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
134 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
136 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
137 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
138 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
139 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
140 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
141 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
143 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
144 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
145 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
146 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
147 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
148 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
149 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
150 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
152 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
154 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
156 EVT_RIGHT_DOWN(MyFrame::OnRightDown
)
159 // ============================================================================
161 // ============================================================================
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
169 // The `main program' equivalent, creating the windows and returning the
173 // Create the main frame window
174 MyFrame
* frame
= new MyFrame
;
178 frame
->SetStatusText("Hello, wxWindows");
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 // Define my frame constructor
191 : wxFrame((wxFrame
*)NULL
, -1, "wxWindows menu sample",
192 wxDefaultPosition
, wxSize(300, 200))
198 // create the menubar
199 wxMenu
*fileMenu
= new wxMenu
;
200 fileMenu
->Append(Menu_File_Quit
, "E&xit\tAlt-X", "Quit toolbar sample" );
202 wxMenu
*menubarMenu
= new wxMenu
;
203 menubarMenu
->Append(Menu_MenuBar_Append
, "&Append menu\tCtrl-A",
204 "Append a menu to the menubar");
205 menubarMenu
->Append(Menu_MenuBar_Delete
, "&Delete menu\tCtrl-D",
206 "Delete the last menu from the menubar");
207 menubarMenu
->Append(Menu_MenuBar_Toggle
, "&Toggle menu\tCtrl-T",
208 "Toggle the first menu in the menubar", TRUE
);
209 menubarMenu
->AppendSeparator();
210 menubarMenu
->Append(Menu_MenuBar_Enable
, "&Enable menu\tCtrl-E",
211 "Enable or disable the last menu", TRUE
);
212 menubarMenu
->AppendSeparator();
213 menubarMenu
->Append(Menu_MenuBar_GetLabel
, "&Get menu label\tCtrl-G",
214 "Get the label of the last menu");
215 menubarMenu
->Append(Menu_MenuBar_SetLabel
, "&Set menu label\tCtrl-S",
216 "Change the label of the last menu");
218 wxMenu
*menuMenu
= new wxMenu
;
219 menuMenu
->Append(Menu_Menu_Append
, "&Append menu item\tAlt-A",
220 "Append a menu item to the last menu");
221 menuMenu
->Append(Menu_Menu_AppendSub
, "&Append sub menu\tAlt-S",
222 "Append a sub menu to the last menu");
223 menuMenu
->Append(Menu_Menu_Insert
, "&Insert menu item\tAlt-I",
224 "Insert a menu item in head of the last menu");
225 menuMenu
->Append(Menu_Menu_Delete
, "&Delete menu item\tAlt-D",
226 "Delete the last menu item from the last menu");
227 menuMenu
->AppendSeparator();
228 menuMenu
->Append(Menu_Menu_Enable
, "&Enable menu item\tAlt-E",
229 "Enable or disable the last menu item", TRUE
);
230 menuMenu
->Append(Menu_Menu_Check
, "&Check menu item\tAlt-C",
231 "Check or uncheck the last menu item", TRUE
);
232 menuMenu
->AppendSeparator();
233 menuMenu
->Append(Menu_Menu_GetLabel
, "&Get menu item label\tAlt-G",
234 "Get the label of the last menu item");
235 menuMenu
->Append(Menu_Menu_SetLabel
, "&Set menu item label\tAlt-S",
236 "Change the label of the last menu item");
238 wxMenu
*helpMenu
= new wxMenu
;
239 helpMenu
->Append(Menu_Help_About
, "&About\tF1", "About menu sample");
241 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
243 menuBar
->Append(fileMenu
, "&File");
244 menuBar
->Append(menubarMenu
, "Menu&bar");
245 menuBar
->Append(menuMenu
, "&Menu");
246 menuBar
->Append(helpMenu
, "&Help");
248 // these items should be initially checked
249 menuBar
->Check(Menu_MenuBar_Toggle
, TRUE
);
250 menuBar
->Check(Menu_MenuBar_Enable
, TRUE
);
251 menuBar
->Check(Menu_Menu_Enable
, TRUE
);
252 menuBar
->Check(Menu_Menu_Check
, FALSE
);
254 // associate the menu bar with the frame
258 wxMenu
*MyFrame::CreateDummyMenu()
260 wxMenu
*menu
= new wxMenu
;
261 menu
->Append(Menu_Dummy_First
, "First item\tCtrl-F1");
262 menu
->AppendSeparator();
263 menu
->Append(Menu_Dummy_Second
, "Second item\tCtrl-F2", "", TRUE
);
268 wxMenuItem
*MyFrame::GetLastMenuItem() const
270 wxMenuBar
*menubar
= GetMenuBar();
271 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
273 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
276 wxLogWarning("No last item in the last menu!");
282 return node
->GetData();
286 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
291 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
293 (void)wxMessageBox("wxWindows toolbar sample",
294 "About wxWindows menu sample",
298 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
300 wxMenuBar
*mbar
= GetMenuBar();
302 size_t count
= mbar
->GetMenuCount();
305 // don't let delete the first 2 menus
306 wxLogError("Can't delete any more menus");
310 delete mbar
->Remove(count
- 1);
314 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
316 static int s_count
= 0;
319 title
.Printf("Dummy menu &%d", ++s_count
);
321 GetMenuBar()->Append(CreateDummyMenu(), title
);
324 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
326 wxMenuBar
*mbar
= GetMenuBar();
330 m_menu
= mbar
->Remove(0);
335 mbar
->Insert(0, m_menu
, "&File");
340 void MyFrame::OnEnableMenu(wxCommandEvent
& WXUNUSED(event
))
342 wxMenuBar
*mbar
= GetMenuBar();
343 size_t count
= mbar
->GetMenuCount();
345 static bool s_enabled
= TRUE
;
347 s_enabled
= !s_enabled
;
348 mbar
->EnableTop(count
- 1, s_enabled
);
351 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
353 wxMenuBar
*mbar
= GetMenuBar();
354 size_t count
= mbar
->GetMenuCount();
356 wxLogMessage("The label of the last menu item is '%s'",
357 mbar
->GetLabelTop(count
- 1).c_str());
360 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
362 wxMenuBar
*mbar
= GetMenuBar();
363 size_t count
= mbar
->GetMenuCount();
365 mbar
->SetLabelTop(count
- 1, "Dummy label &0");
368 void MyFrame::OnDummy(wxCommandEvent
& event
)
371 s
.Printf("Dummy item #%d", event
.GetId() - Menu_Dummy_First
+ 1);
372 wxMessageBox(s
, "Menu sample", wxICON_INFORMATION
);
375 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
377 wxMenuBar
*menubar
= GetMenuBar();
378 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
380 menu
->AppendSeparator();
381 menu
->Append(Menu_Dummy_Third
, "Third dummy item\tCtrl-F3",
382 "Checkable item", TRUE
);
385 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
387 wxMenuBar
*menubar
= GetMenuBar();
388 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
390 menu
->Append(Menu_Dummy_Last
, "Dummy sub menu\tCtrl-F12",
394 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
396 wxMenuBar
*menubar
= GetMenuBar();
397 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
399 size_t count
= menu
->GetMenuItemCount();
402 wxLogWarning("No items to delete!");
406 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
410 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
412 wxMenuBar
*menubar
= GetMenuBar();
413 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
415 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
416 "Fourth dummy item\tCtrl-F4"));
417 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, ""));
420 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
422 wxMenuItem
*item
= GetLastMenuItem();
426 item
->Enable(!item
->IsEnabled());
430 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
432 wxMenuItem
*item
= GetLastMenuItem();
437 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
439 wxMenuItem
*item
= GetLastMenuItem();
441 event
.Enable(item
&& item
->IsCheckable());
444 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
446 wxMenuItem
*item
= GetLastMenuItem();
450 wxLogMessage("The label of the last menu item is '%s'",
451 item
->GetLabel().c_str());
455 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
457 wxMenuItem
*item
= GetLastMenuItem();
461 item
->SetText("Dummy menu item text");
465 void MyFrame::OnRightDown(wxMouseEvent
&event
)
467 wxMenu
menu("Test popup");
469 menu
.Append(Menu_Help_About
, "&About");
470 menu
.Append(Menu_Popup_Submenu
, "Submenu", CreateDummyMenu());
471 menu
.Append(Menu_Popup_ToBeDeleted
, "To be deleted");
472 menu
.Append(Menu_Popup_ToBeChecked
, "To be checked", "", TRUE
);
473 menu
.Append(Menu_Popup_ToBeGreyed
, "To be greyed");
474 menu
.AppendSeparator();
475 menu
.Append(Menu_File_Quit
, "E&xit");
477 menu
.Delete(Menu_Popup_ToBeDeleted
);
478 menu
.Check(Menu_Popup_ToBeChecked
, TRUE
);
479 menu
.Enable(Menu_Popup_ToBeGreyed
, FALSE
);
481 PopupMenu( &menu
, event
.GetX(), event
.GetY() );