]>
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>
32 #include <wx/textctrl.h>
37 #error "menu sample requires wxUSE_MENUS=1"
42 #ifdef __WXUNIVERSAL__
43 #include "wx/univ/theme.h"
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
);
71 void OnQuit(wxCommandEvent
& event
);
72 void OnClearLog(wxCommandEvent
& event
);
74 void OnAbout(wxCommandEvent
& event
);
76 void OnDummy(wxCommandEvent
& event
);
78 void OnAppendMenuItem(wxCommandEvent
& event
);
79 void OnAppendSubMenu(wxCommandEvent
& event
);
80 void OnDeleteMenuItem(wxCommandEvent
& event
);
81 void OnInsertMenuItem(wxCommandEvent
& event
);
82 void OnCheckMenuItem(wxCommandEvent
& event
);
83 void OnEnableMenuItem(wxCommandEvent
& event
);
84 void OnGetLabelMenuItem(wxCommandEvent
& event
);
85 void OnSetLabelMenuItem(wxCommandEvent
& event
);
86 void OnGetMenuItemInfo(wxCommandEvent
& event
);
88 void OnAppendMenu(wxCommandEvent
& event
);
89 void OnInsertMenu(wxCommandEvent
& event
);
90 void OnDeleteMenu(wxCommandEvent
& event
);
91 void OnToggleMenu(wxCommandEvent
& event
);
92 void OnEnableMenu(wxCommandEvent
& event
);
93 void OnGetLabelMenu(wxCommandEvent
& event
);
94 void OnSetLabelMenu(wxCommandEvent
& event
);
96 void OnRightUp(wxMouseEvent
& event
);
98 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
100 void OnSize(wxSizeEvent
& event
);
103 wxMenu
*CreateDummyMenu(wxString
*title
);
105 wxMenuItem
*GetLastMenuItem() const;
107 // the menu previously detached from the menubar (may be NULL)
110 // the count of dummy menus already created
113 // the control used for logging
114 wxTextCtrl
*m_textctrl
;
116 // the previous log target
119 DECLARE_EVENT_TABLE()
122 // A small helper class which intercepts all menu events and logs them
123 class MyEvtHandler
: public wxEvtHandler
126 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
128 void OnMenuEvent(wxCommandEvent
& event
)
130 m_frame
->LogMenuEvent(event
);
138 DECLARE_EVENT_TABLE()
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
147 Menu_File_Quit
= 100,
150 Menu_MenuBar_Toggle
= 200,
155 Menu_MenuBar_GetLabel
,
156 Menu_MenuBar_SetLabel
,
158 Menu_Menu_Append
= 300,
168 Menu_Dummy_First
= 400,
174 Menu_Help_About
= 1000,
176 Menu_Popup_ToBeDeleted
= 2000,
177 Menu_Popup_ToBeGreyed
,
178 Menu_Popup_ToBeChecked
,
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
189 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
190 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
192 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
194 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
195 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
196 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
197 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
198 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
199 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
200 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
202 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
203 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
204 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
205 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
206 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
207 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
208 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
209 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
210 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
212 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
214 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
216 EVT_RIGHT_UP(MyFrame::OnRightUp
)
218 EVT_SIZE(MyFrame::OnSize
)
221 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
222 EVT_MENU(-1, MyEvtHandler::OnMenuEvent
)
225 // ============================================================================
227 // ============================================================================
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
235 // The `main program' equivalent, creating the windows and returning the
239 // Create the main frame window
240 MyFrame
* frame
= new MyFrame
;
245 frame
->SetStatusText("Welcome to wxWindows menu sample");
246 #endif // wxUSE_STATUSBAR
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
257 // Define my frame constructor
259 : wxFrame((wxFrame
*)NULL
, -1, "wxWindows menu sample",
260 wxDefaultPosition
, wxSize(300, 200))
268 #endif // wxUSE_STATUSBAR
270 // create the menubar
271 wxMenu
*fileMenu
= new wxMenu
;
273 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
274 "Clear &log\tCtrl-L");
275 item
->SetBitmap(copy_xpm
);
276 fileMenu
->Append(item
);
277 fileMenu
->AppendSeparator();
278 fileMenu
->Append(Menu_File_Quit
, "E&xit\tAlt-X", "Quit menu sample");
280 wxMenu
*menubarMenu
= new wxMenu
;
281 menubarMenu
->Append(Menu_MenuBar_Append
, "&Append menu\tCtrl-A",
282 "Append a menu to the menubar");
283 menubarMenu
->Append(Menu_MenuBar_Insert
, "&Insert menu\tCtrl-I",
284 "Insert a menu into the menubar");
285 menubarMenu
->Append(Menu_MenuBar_Delete
, "&Delete menu\tCtrl-D",
286 "Delete the last menu from the menubar");
287 menubarMenu
->Append(Menu_MenuBar_Toggle
, "&Toggle menu\tCtrl-T",
288 "Toggle the first menu in the menubar", TRUE
);
289 menubarMenu
->AppendSeparator();
290 menubarMenu
->Append(Menu_MenuBar_Enable
, "&Enable menu\tCtrl-E",
291 "Enable or disable the last menu", TRUE
);
292 menubarMenu
->AppendSeparator();
293 menubarMenu
->Append(Menu_MenuBar_GetLabel
, "&Get menu label\tCtrl-G",
294 "Get the label of the last menu");
295 menubarMenu
->Append(Menu_MenuBar_SetLabel
, "&Set menu label\tCtrl-S",
296 "Change the label of the last menu");
298 wxMenu
*menuMenu
= new wxMenu
;
299 menuMenu
->Append(Menu_Menu_Append
, "&Append menu item\tAlt-A",
300 "Append a menu item to the last menu");
301 menuMenu
->Append(Menu_Menu_AppendSub
, "&Append sub menu\tAlt-S",
302 "Append a sub menu to the last menu");
303 menuMenu
->Append(Menu_Menu_Insert
, "&Insert menu item\tAlt-I",
304 "Insert a menu item in head of the last menu");
305 menuMenu
->Append(Menu_Menu_Delete
, "&Delete menu item\tAlt-D",
306 "Delete the last menu item from the last menu");
307 menuMenu
->AppendSeparator();
308 menuMenu
->Append(Menu_Menu_Enable
, "&Enable menu item\tAlt-E",
309 "Enable or disable the last menu item", TRUE
);
310 menuMenu
->Append(Menu_Menu_Check
, "&Check menu item\tAlt-C",
311 "Check or uncheck the last menu item", TRUE
);
312 menuMenu
->AppendSeparator();
313 menuMenu
->Append(Menu_Menu_GetLabel
, "&Get menu item label\tAlt-G",
314 "Get the label of the last menu item");
315 menuMenu
->Append(Menu_Menu_SetLabel
, "&Set menu item label\tAlt-S",
316 "Change the label of the last menu item");
317 menuMenu
->AppendSeparator();
318 menuMenu
->Append(Menu_Menu_GetInfo
, "Get menu item in&fo\tAlt-F",
319 "Show the state of the last menu item");
321 wxMenu
*helpMenu
= new wxMenu
;
322 helpMenu
->Append(Menu_Help_About
, "&About\tF1", "About menu sample");
324 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
326 menuBar
->Append(fileMenu
, "&File");
327 menuBar
->Append(menubarMenu
, "Menu&bar");
328 menuBar
->Append(menuMenu
, "&Menu");
329 menuBar
->Append(helpMenu
, "&Help");
331 // these items should be initially checked
332 menuBar
->Check(Menu_MenuBar_Toggle
, TRUE
);
333 menuBar
->Check(Menu_MenuBar_Enable
, TRUE
);
334 menuBar
->Check(Menu_Menu_Enable
, TRUE
);
335 menuBar
->Check(Menu_Menu_Check
, FALSE
);
337 // associate the menu bar with the frame
340 // intercept all menu events and log them in this custom event handler
341 PushEventHandler(new MyEvtHandler(this));
343 // create the log text window
344 m_textctrl
= new wxTextCtrl(this, -1, _T(""),
345 wxDefaultPosition
, wxDefaultSize
,
347 m_textctrl
->SetEditable(FALSE
);
348 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
350 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu\n")
351 _T("append/insert/delete items to/from the last menu.\n")
352 _T("The commands from \"Menubar\" menu work with the\n")
353 _T("menubar itself.\n")
354 _T("Right click the band below to test popup menus.\n"));
361 // delete the event handler installed in ctor
362 PopEventHandler(TRUE
);
364 // restore old logger
365 delete wxLog::SetActiveTarget(m_logOld
);
368 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
370 wxMenu
*menu
= new wxMenu
;
371 menu
->Append(Menu_Dummy_First
, "&First item\tCtrl-F1");
372 menu
->AppendSeparator();
373 menu
->Append(Menu_Dummy_Second
, "&Second item\tCtrl-F2", "", TRUE
);
377 title
->Printf("Dummy menu &%d", ++m_countDummy
);
383 wxMenuItem
*MyFrame::GetLastMenuItem() const
385 wxMenuBar
*menubar
= GetMenuBar();
386 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
388 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
391 wxLogWarning("No last item in the last menu!");
397 return node
->GetData();
401 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
403 int id
= event
.GetId();
404 if ( !GetMenuBar()->FindItem(id
) )
407 wxString msg
= wxString::Format("Menu command %d", id
);
408 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
410 msg
+= wxString::Format(" (the item is currently %schecked)",
411 event
.IsChecked() ? "" : "not ");
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
421 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
426 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
431 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
433 #ifdef __WXUNIVERSAL__
434 // wxMessageBox not implemented yet
435 wxLogMessage("wxWindows menu sample\n© 1999-2001 Vadim Zeitlin");
437 (void)wxMessageBox("wxWindows menu sample\n© 1999-2001 Vadim Zeitlin",
438 "About wxWindows menu sample",
443 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
445 wxMenuBar
*mbar
= GetMenuBar();
447 size_t count
= mbar
->GetMenuCount();
450 // don't let delete the first 2 menus
451 wxLogError("Can't delete any more menus");
455 delete mbar
->Remove(count
- 1);
459 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
462 wxMenu
*menu
= CreateDummyMenu(&title
);
463 GetMenuBar()->Insert(0, menu
, title
);
466 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
469 wxMenu
*menu
= CreateDummyMenu(&title
);
470 GetMenuBar()->Append(menu
, title
);
473 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
475 wxMenuBar
*mbar
= GetMenuBar();
479 m_menu
= mbar
->Remove(0);
484 mbar
->Insert(0, m_menu
, "&File");
489 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
491 wxMenuBar
*mbar
= GetMenuBar();
492 size_t count
= mbar
->GetMenuCount();
494 mbar
->EnableTop(count
- 1, event
.IsChecked());
497 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
499 wxMenuBar
*mbar
= GetMenuBar();
500 size_t count
= mbar
->GetMenuCount();
502 wxLogMessage("The label of the last menu item is '%s'",
503 mbar
->GetLabelTop(count
- 1).c_str());
506 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
508 wxMenuBar
*mbar
= GetMenuBar();
509 size_t count
= mbar
->GetMenuCount();
511 mbar
->SetLabelTop(count
- 1, "Dummy label &0");
514 void MyFrame::OnDummy(wxCommandEvent
& event
)
516 wxLogMessage("Dummy item #%d", event
.GetId() - Menu_Dummy_First
+ 1);
519 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
521 wxMenuBar
*menubar
= GetMenuBar();
522 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
524 menu
->AppendSeparator();
525 menu
->Append(Menu_Dummy_Third
, "&Third dummy item\tCtrl-F3",
526 "Checkable item", TRUE
);
529 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
531 wxMenuBar
*menubar
= GetMenuBar();
533 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
535 menu
->Append(Menu_Dummy_Last
, "&Dummy sub menu",
536 CreateDummyMenu(NULL
), "Dummy sub menu help");
539 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
541 wxMenuBar
*menubar
= GetMenuBar();
542 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
544 size_t count
= menu
->GetMenuItemCount();
547 wxLogWarning("No items to delete!");
551 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
555 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
557 wxMenuBar
*menubar
= GetMenuBar();
558 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
560 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
561 "Fourth dummy item\tCtrl-F4"));
562 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, ""));
565 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
567 wxMenuItem
*item
= GetLastMenuItem();
571 item
->Enable(!item
->IsEnabled());
575 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
577 wxMenuItem
*item
= GetLastMenuItem();
582 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
586 wxMenuItem
*item
= GetLastMenuItem();
588 event
.Enable(item
&& item
->IsCheckable());
591 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
593 wxMenuItem
*item
= GetLastMenuItem();
597 wxLogMessage("The label of the last menu item is '%s'",
598 item
->GetLabel().c_str());
602 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
604 wxMenuItem
*item
= GetLastMenuItem();
608 item
->SetText("Dummy menu item text");
612 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
614 wxMenuItem
*item
= GetLastMenuItem();
619 msg
<< "The item is " << (item
->IsEnabled() ? "enabled"
623 if ( item
->IsCheckable() )
625 msg
<< "It is checkable and " << (item
->IsChecked() ? "" : "un")
630 wxAcceleratorEntry
*accel
= item
->GetAccel();
633 msg
<< "Its accelerator is ";
635 int flags
= accel
->GetFlags();
636 if ( flags
& wxACCEL_ALT
)
638 if ( flags
& wxACCEL_CTRL
)
640 if ( flags
& wxACCEL_SHIFT
)
641 msg
<< wxT("Shift-");
643 int code
= accel
->GetKeyCode();
658 msg
<< wxT('F') << code
- WXK_F1
+ 1;
661 // if there are any other keys wxGetAccelFromString() may return,
662 // we should process them here
665 if ( wxIsalnum(code
) )
672 wxFAIL_MSG( wxT("unknown keyboard accel") );
679 msg
<< "It doesn't have an accelerator";
681 #endif // wxUSE_ACCEL
687 void MyFrame::OnRightUp(wxMouseEvent
&event
)
689 wxMenu
menu("Test popup");
691 menu
.Append(Menu_Help_About
, "&About");
692 menu
.Append(Menu_Popup_Submenu
, "&Submenu", CreateDummyMenu(NULL
));
693 menu
.Append(Menu_Popup_ToBeDeleted
, "To be &deleted");
694 menu
.Append(Menu_Popup_ToBeChecked
, "To be &checked", "", TRUE
);
695 menu
.Append(Menu_Popup_ToBeGreyed
, "To be &greyed");
696 menu
.AppendSeparator();
697 menu
.Append(Menu_File_Quit
, "E&xit");
699 menu
.Delete(Menu_Popup_ToBeDeleted
);
700 menu
.Check(Menu_Popup_ToBeChecked
, TRUE
);
701 menu
.Enable(Menu_Popup_ToBeGreyed
, FALSE
);
703 PopupMenu(&menu
, event
.GetX(), event
.GetY());
705 // test for destroying items in popup menus
706 #if 0 // doesn't work in wxGTK!
707 menu
.Destroy(Menu_Popup_Submenu
);
709 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
713 void MyFrame::OnSize(wxSizeEvent
& event
)
715 // leave a band below for popup menu testing
716 wxSize size
= GetClientSize();
717 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
719 // this is really ugly but we have to do it as we can't just call
720 // event.Skip() because wxFrameBase would make the text control fill the
722 #ifdef __WXUNIVERSAL__
724 #endif // __WXUNIVERSAL__