]>
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 wxMenuItem
*itemBitmap
= new wxMenuItem(fileMenu
, Menu_File_Quit
,
257 "Quit with &bitmap\tAlt-Q");
258 itemBitmap
->SetBitmap(wxBitmap(copy_xpm
));
259 fileMenu
->Append(itemBitmap
);
261 wxMenu
*menubarMenu
= new wxMenu
;
262 menubarMenu
->Append(Menu_MenuBar_Append
, "&Append menu\tCtrl-A",
263 "Append a menu to the menubar");
264 menubarMenu
->Append(Menu_MenuBar_Insert
, "&Insert menu\tCtrl-I",
265 "Insert a menu into the menubar");
266 menubarMenu
->Append(Menu_MenuBar_Delete
, "&Delete menu\tCtrl-D",
267 "Delete the last menu from the menubar");
268 menubarMenu
->Append(Menu_MenuBar_Toggle
, "&Toggle menu\tCtrl-T",
269 "Toggle the first menu in the menubar", TRUE
);
270 menubarMenu
->AppendSeparator();
271 menubarMenu
->Append(Menu_MenuBar_Enable
, "&Enable menu\tCtrl-E",
272 "Enable or disable the last menu", TRUE
);
273 menubarMenu
->AppendSeparator();
274 menubarMenu
->Append(Menu_MenuBar_GetLabel
, "&Get menu label\tCtrl-G",
275 "Get the label of the last menu");
276 menubarMenu
->Append(Menu_MenuBar_SetLabel
, "&Set menu label\tCtrl-S",
277 "Change the label of the last menu");
279 wxMenu
*menuMenu
= new wxMenu
;
280 menuMenu
->Append(Menu_Menu_Append
, "&Append menu item\tAlt-A",
281 "Append a menu item to the last menu");
282 menuMenu
->Append(Menu_Menu_AppendSub
, "&Append sub menu\tAlt-S",
283 "Append a sub menu to the last menu");
284 menuMenu
->Append(Menu_Menu_Insert
, "&Insert menu item\tAlt-I",
285 "Insert a menu item in head of the last menu");
286 menuMenu
->Append(Menu_Menu_Delete
, "&Delete menu item\tAlt-D",
287 "Delete the last menu item from the last menu");
288 menuMenu
->AppendSeparator();
289 menuMenu
->Append(Menu_Menu_Enable
, "&Enable menu item\tAlt-E",
290 "Enable or disable the last menu item", TRUE
);
291 menuMenu
->Append(Menu_Menu_Check
, "&Check menu item\tAlt-C",
292 "Check or uncheck the last menu item", TRUE
);
293 menuMenu
->AppendSeparator();
294 menuMenu
->Append(Menu_Menu_GetLabel
, "&Get menu item label\tAlt-G",
295 "Get the label of the last menu item");
296 menuMenu
->Append(Menu_Menu_SetLabel
, "&Set menu item label\tAlt-S",
297 "Change the label of the last menu item");
298 menuMenu
->AppendSeparator();
299 menuMenu
->Append(Menu_Menu_GetInfo
, "Get menu item in&fo\tAlt-F",
300 "Show the state of the last menu item");
302 wxMenu
*helpMenu
= new wxMenu
;
303 helpMenu
->Append(Menu_Help_About
, "&About\tF1", "About menu sample");
305 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
307 menuBar
->Append(fileMenu
, "&File");
308 menuBar
->Append(menubarMenu
, "Menu&bar");
309 menuBar
->Append(menuMenu
, "&Menu");
310 menuBar
->Append(helpMenu
, "&Help");
312 // these items should be initially checked
313 menuBar
->Check(Menu_MenuBar_Toggle
, TRUE
);
314 menuBar
->Check(Menu_MenuBar_Enable
, TRUE
);
315 menuBar
->Check(Menu_Menu_Enable
, TRUE
);
316 menuBar
->Check(Menu_Menu_Check
, FALSE
);
318 // associate the menu bar with the frame
321 // intercept all menu events and log them in this custom event handler
322 PushEventHandler(new MyEvtHandler(this));
329 // delete the event handler installed in ctor
330 PopEventHandler(TRUE
);
333 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
335 wxMenu
*menu
= new wxMenu
;
336 menu
->Append(Menu_Dummy_First
, "First item\tCtrl-F1");
337 menu
->AppendSeparator();
338 menu
->Append(Menu_Dummy_Second
, "Second item\tCtrl-F2", "", TRUE
);
342 title
->Printf("Dummy menu &%d", ++m_countDummy
);
348 wxMenuItem
*MyFrame::GetLastMenuItem() const
350 wxMenuBar
*menubar
= GetMenuBar();
351 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
353 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
356 wxLogWarning("No last item in the last menu!");
362 return node
->GetData();
366 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
368 int id
= event
.GetId();
369 if (!GetMenuBar()->FindItem(id
))
371 wxString msg
= wxString::Format("Menu command %d", id
);
372 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
374 msg
+= wxString::Format(" (the item is currently %schecked)",
375 event
.IsChecked() ? "" : "not ");
379 SetStatusText(msg
, 1);
380 #endif // wxUSE_STATUSBAR
383 // ----------------------------------------------------------------------------
385 // ----------------------------------------------------------------------------
387 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
392 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
394 (void)wxMessageBox("wxWindows toolbar sample",
395 "About wxWindows menu sample",
399 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
401 wxMenuBar
*mbar
= GetMenuBar();
403 size_t count
= mbar
->GetMenuCount();
406 // don't let delete the first 2 menus
407 wxLogError("Can't delete any more menus");
411 delete mbar
->Remove(count
- 1);
415 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
418 wxMenu
*menu
= CreateDummyMenu(&title
);
419 GetMenuBar()->Insert(0, menu
, title
);
422 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
425 wxMenu
*menu
= CreateDummyMenu(&title
);
426 GetMenuBar()->Append(menu
, title
);
429 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
431 wxMenuBar
*mbar
= GetMenuBar();
435 m_menu
= mbar
->Remove(0);
440 mbar
->Insert(0, m_menu
, "&File");
445 void MyFrame::OnEnableMenu(wxCommandEvent
& WXUNUSED(event
))
447 wxMenuBar
*mbar
= GetMenuBar();
448 size_t count
= mbar
->GetMenuCount();
450 static bool s_enabled
= TRUE
;
452 s_enabled
= !s_enabled
;
453 mbar
->EnableTop(count
- 1, s_enabled
);
456 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
458 wxMenuBar
*mbar
= GetMenuBar();
459 size_t count
= mbar
->GetMenuCount();
461 wxLogMessage("The label of the last menu item is '%s'",
462 mbar
->GetLabelTop(count
- 1).c_str());
465 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
467 wxMenuBar
*mbar
= GetMenuBar();
468 size_t count
= mbar
->GetMenuCount();
470 mbar
->SetLabelTop(count
- 1, "Dummy label &0");
473 void MyFrame::OnDummy(wxCommandEvent
& event
)
476 s
.Printf("Dummy item #%d", event
.GetId() - Menu_Dummy_First
+ 1);
477 wxMessageBox(s
, "Menu sample", wxICON_INFORMATION
);
480 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
482 wxMenuBar
*menubar
= GetMenuBar();
483 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
485 menu
->AppendSeparator();
486 menu
->Append(Menu_Dummy_Third
, "Third dummy item\tCtrl-F3",
487 "Checkable item", TRUE
);
490 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
492 wxMenuBar
*menubar
= GetMenuBar();
494 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
496 menu
->Append(Menu_Dummy_Last
, "Dummy sub menu",
497 CreateDummyMenu(NULL
), "Dummy sub menu help");
500 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
502 wxMenuBar
*menubar
= GetMenuBar();
503 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
505 size_t count
= menu
->GetMenuItemCount();
508 wxLogWarning("No items to delete!");
512 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
516 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
518 wxMenuBar
*menubar
= GetMenuBar();
519 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
521 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
522 "Fourth dummy item\tCtrl-F4"));
523 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, ""));
526 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
528 wxMenuItem
*item
= GetLastMenuItem();
532 item
->Enable(!item
->IsEnabled());
536 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
538 wxMenuItem
*item
= GetLastMenuItem();
543 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
547 wxMenuItem
*item
= GetLastMenuItem();
549 event
.Enable(item
&& item
->IsCheckable());
552 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
554 wxMenuItem
*item
= GetLastMenuItem();
558 wxLogMessage("The label of the last menu item is '%s'",
559 item
->GetLabel().c_str());
563 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
565 wxMenuItem
*item
= GetLastMenuItem();
569 item
->SetText("Dummy menu item text");
573 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
575 wxMenuItem
*item
= GetLastMenuItem();
580 msg
<< "The item is " << (item
->IsEnabled() ? "enabled"
584 if ( item
->IsCheckable() )
586 msg
<< "It is checkable and " << (item
->IsChecked() ? "" : "un")
591 wxAcceleratorEntry
*accel
= item
->GetAccel();
594 msg
<< "Its accelerator is ";
596 int flags
= accel
->GetFlags();
597 if ( flags
& wxACCEL_ALT
)
599 if ( flags
& wxACCEL_CTRL
)
601 if ( flags
& wxACCEL_SHIFT
)
602 msg
<< wxT("Shift-");
604 int code
= accel
->GetKeyCode();
619 msg
<< wxT('F') << code
- WXK_F1
+ 1;
622 // if there are any other keys wxGetAccelFromString() may return,
623 // we should process them here
626 if ( wxIsalnum(code
) )
633 wxFAIL_MSG( wxT("unknown keyboard accel") );
640 msg
<< "It doesn't have an accelerator";
642 #endif // wxUSE_ACCEL
648 void MyFrame::OnRightDown(wxMouseEvent
&event
)
650 wxMenu
menu("Test popup");
652 menu
.Append(Menu_Help_About
, "&About");
653 menu
.Append(Menu_Popup_Submenu
, "Submenu", CreateDummyMenu(NULL
));
654 menu
.Append(Menu_Popup_ToBeDeleted
, "To be deleted");
655 menu
.Append(Menu_Popup_ToBeChecked
, "To be checked", "", TRUE
);
656 menu
.Append(Menu_Popup_ToBeGreyed
, "To be greyed");
657 menu
.AppendSeparator();
658 menu
.Append(Menu_File_Quit
, "E&xit");
660 menu
.Delete(Menu_Popup_ToBeDeleted
);
661 menu
.Check(Menu_Popup_ToBeChecked
, TRUE
);
662 menu
.Enable(Menu_Popup_ToBeGreyed
, FALSE
);
664 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
666 // test for destroying items in popup menus
668 menu
.Destroy(Menu_Popup_Submenu
);
670 PopupMenu( &menu
, event
.GetX(), event
.GetY() );