]>
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"
31 #include "wx/msgdlg.h"
33 #include "wx/textctrl.h"
34 #include "wx/textdlg.h"
39 #error "menu sample requires wxUSE_MENUS=1"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // Define a new application
49 class MyApp
: public wxApp
56 class MyFrame
: public wxFrame
63 void LogMenuEvent(const wxCommandEvent
& event
);
66 void OnQuit(wxCommandEvent
& event
);
67 void OnClearLog(wxCommandEvent
& event
);
69 void OnAbout(wxCommandEvent
& event
);
71 void OnDummy(wxCommandEvent
& event
);
73 void OnAppendMenuItem(wxCommandEvent
& event
);
74 void OnAppendSubMenu(wxCommandEvent
& event
);
75 void OnDeleteMenuItem(wxCommandEvent
& event
);
76 void OnInsertMenuItem(wxCommandEvent
& event
);
77 void OnCheckMenuItem(wxCommandEvent
& event
);
78 void OnEnableMenuItem(wxCommandEvent
& event
);
79 void OnGetLabelMenuItem(wxCommandEvent
& event
);
80 void OnSetLabelMenuItem(wxCommandEvent
& event
);
81 void OnGetMenuItemInfo(wxCommandEvent
& event
);
83 void OnAppendMenu(wxCommandEvent
& event
);
84 void OnInsertMenu(wxCommandEvent
& event
);
85 void OnDeleteMenu(wxCommandEvent
& event
);
86 void OnToggleMenu(wxCommandEvent
& event
);
87 void OnEnableMenu(wxCommandEvent
& event
);
88 void OnGetLabelMenu(wxCommandEvent
& event
);
89 void OnSetLabelMenu(wxCommandEvent
& event
);
91 void OnRightUp(wxMouseEvent
& event
);
93 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
);
95 void OnSize(wxSizeEvent
& event
);
98 wxMenu
*CreateDummyMenu(wxString
*title
);
100 wxMenuItem
*GetLastMenuItem() const;
102 // the menu previously detached from the menubar (may be NULL)
105 // the count of dummy menus already created
108 // the control used for logging
109 wxTextCtrl
*m_textctrl
;
111 // the previous log target
114 DECLARE_EVENT_TABLE()
117 // A small helper class which intercepts all menu events and logs them
118 class MyEvtHandler
: public wxEvtHandler
121 MyEvtHandler(MyFrame
*frame
) { m_frame
= frame
; }
123 void OnMenuEvent(wxCommandEvent
& event
)
125 m_frame
->LogMenuEvent(event
);
133 DECLARE_EVENT_TABLE()
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
142 Menu_File_Quit
= 100,
145 Menu_MenuBar_Toggle
= 200,
150 Menu_MenuBar_GetLabel
,
151 Menu_MenuBar_SetLabel
,
153 Menu_Menu_Append
= 300,
163 Menu_Dummy_First
= 400,
169 Menu_Help_About
= 1000,
171 Menu_Popup_ToBeDeleted
= 2000,
172 Menu_Popup_ToBeGreyed
,
173 Menu_Popup_ToBeChecked
,
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
184 EVT_MENU(Menu_File_Quit
, MyFrame::OnQuit
)
185 EVT_MENU(Menu_File_ClearLog
, MyFrame::OnClearLog
)
187 EVT_MENU(Menu_Help_About
, MyFrame::OnAbout
)
189 EVT_MENU(Menu_MenuBar_Toggle
, MyFrame::OnToggleMenu
)
190 EVT_MENU(Menu_MenuBar_Append
, MyFrame::OnAppendMenu
)
191 EVT_MENU(Menu_MenuBar_Insert
, MyFrame::OnInsertMenu
)
192 EVT_MENU(Menu_MenuBar_Delete
, MyFrame::OnDeleteMenu
)
193 EVT_MENU(Menu_MenuBar_Enable
, MyFrame::OnEnableMenu
)
194 EVT_MENU(Menu_MenuBar_GetLabel
, MyFrame::OnGetLabelMenu
)
195 EVT_MENU(Menu_MenuBar_SetLabel
, MyFrame::OnSetLabelMenu
)
197 EVT_MENU(Menu_Menu_Append
, MyFrame::OnAppendMenuItem
)
198 EVT_MENU(Menu_Menu_AppendSub
, MyFrame::OnAppendSubMenu
)
199 EVT_MENU(Menu_Menu_Insert
, MyFrame::OnInsertMenuItem
)
200 EVT_MENU(Menu_Menu_Delete
, MyFrame::OnDeleteMenuItem
)
201 EVT_MENU(Menu_Menu_Enable
, MyFrame::OnEnableMenuItem
)
202 EVT_MENU(Menu_Menu_Check
, MyFrame::OnCheckMenuItem
)
203 EVT_MENU(Menu_Menu_GetLabel
, MyFrame::OnGetLabelMenuItem
)
204 EVT_MENU(Menu_Menu_SetLabel
, MyFrame::OnSetLabelMenuItem
)
205 EVT_MENU(Menu_Menu_GetInfo
, MyFrame::OnGetMenuItemInfo
)
207 EVT_MENU_RANGE(Menu_Dummy_First
, Menu_Dummy_Last
, MyFrame::OnDummy
)
209 EVT_UPDATE_UI(Menu_Menu_Check
, MyFrame::OnUpdateCheckMenuItemUI
)
211 EVT_RIGHT_UP(MyFrame::OnRightUp
)
213 EVT_SIZE(MyFrame::OnSize
)
216 BEGIN_EVENT_TABLE(MyEvtHandler
, wxEvtHandler
)
217 EVT_MENU(-1, MyEvtHandler::OnMenuEvent
)
220 // ============================================================================
222 // ============================================================================
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
230 // The `main program' equivalent, creating the windows and returning the
234 // Create the main frame window
235 MyFrame
* frame
= new MyFrame
;
240 frame
->SetStatusText("Welcome to wxWindows menu sample");
241 #endif // wxUSE_STATUSBAR
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 // Define my frame constructor
254 : wxFrame((wxFrame
*)NULL
, -1, "wxWindows menu sample",
255 wxDefaultPosition
, wxSize(400, 250))
264 #endif // wxUSE_STATUSBAR
266 // create the menubar
267 wxMenu
*fileMenu
= new wxMenu
;
269 wxMenuItem
*item
= new wxMenuItem(fileMenu
, Menu_File_ClearLog
,
270 "Clear &log\tCtrl-L");
271 item
->SetBitmap(copy_xpm
);
272 fileMenu
->Append(item
);
273 fileMenu
->AppendSeparator();
274 fileMenu
->Append(Menu_File_Quit
, "E&xit\tAlt-X", "Quit menu sample");
276 wxMenu
*menubarMenu
= new wxMenu
;
277 menubarMenu
->Append(Menu_MenuBar_Append
, "&Append menu\tCtrl-A",
278 "Append a menu to the menubar");
279 menubarMenu
->Append(Menu_MenuBar_Insert
, "&Insert menu\tCtrl-I",
280 "Insert a menu into the menubar");
281 menubarMenu
->Append(Menu_MenuBar_Delete
, "&Delete menu\tCtrl-D",
282 "Delete the last menu from the menubar");
283 menubarMenu
->Append(Menu_MenuBar_Toggle
, "&Toggle menu\tCtrl-T",
284 "Toggle the first menu in the menubar", TRUE
);
285 menubarMenu
->AppendSeparator();
286 menubarMenu
->Append(Menu_MenuBar_Enable
, "&Enable menu\tCtrl-E",
287 "Enable or disable the last menu", TRUE
);
288 menubarMenu
->AppendSeparator();
289 menubarMenu
->Append(Menu_MenuBar_GetLabel
, "&Get menu label\tCtrl-G",
290 "Get the label of the last menu");
291 menubarMenu
->Append(Menu_MenuBar_SetLabel
, "&Set menu label\tCtrl-S",
292 "Change the label of the last menu");
294 wxMenu
*menuMenu
= new wxMenu
;
295 menuMenu
->Append(Menu_Menu_Append
, "&Append menu item\tAlt-A",
296 "Append a menu item to the last menu");
297 menuMenu
->Append(Menu_Menu_AppendSub
, "&Append sub menu\tAlt-S",
298 "Append a sub menu to the last menu");
299 menuMenu
->Append(Menu_Menu_Insert
, "&Insert menu item\tAlt-I",
300 "Insert a menu item in head of the last menu");
301 menuMenu
->Append(Menu_Menu_Delete
, "&Delete menu item\tAlt-D",
302 "Delete the last menu item from the last menu");
303 menuMenu
->AppendSeparator();
304 menuMenu
->Append(Menu_Menu_Enable
, "&Enable menu item\tAlt-E",
305 "Enable or disable the last menu item", TRUE
);
306 menuMenu
->Append(Menu_Menu_Check
, "&Check menu item\tAlt-C",
307 "Check or uncheck the last menu item", TRUE
);
308 menuMenu
->AppendSeparator();
309 menuMenu
->Append(Menu_Menu_GetLabel
, "&Get menu item label\tAlt-G",
310 "Get the label of the last menu item");
311 menuMenu
->Append(Menu_Menu_SetLabel
, "&Set menu item label\tAlt-S",
312 "Change the label of the last menu item");
313 menuMenu
->AppendSeparator();
314 menuMenu
->Append(Menu_Menu_GetInfo
, "Get menu item in&fo\tAlt-F",
315 "Show the state of the last menu item");
317 wxMenu
*helpMenu
= new wxMenu
;
318 helpMenu
->Append(Menu_Help_About
, "&About\tF1", "About menu sample");
320 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
322 menuBar
->Append(fileMenu
, "&File");
323 menuBar
->Append(menubarMenu
, "Menu&bar");
324 menuBar
->Append(menuMenu
, "&Menu");
325 menuBar
->Append(helpMenu
, "&Help");
327 // these items should be initially checked
328 menuBar
->Check(Menu_MenuBar_Toggle
, TRUE
);
329 menuBar
->Check(Menu_MenuBar_Enable
, TRUE
);
330 menuBar
->Check(Menu_Menu_Enable
, TRUE
);
331 menuBar
->Check(Menu_Menu_Check
, FALSE
);
333 // associate the menu bar with the frame
336 // intercept all menu events and log them in this custom event handler
337 PushEventHandler(new MyEvtHandler(this));
339 // create the log text window
340 m_textctrl
= new wxTextCtrl(this, -1, _T(""),
341 wxDefaultPosition
, wxDefaultSize
,
343 m_textctrl
->SetEditable(FALSE
);
345 wxLog::SetTimestamp(NULL
);
346 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl
));
348 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
349 _T("append/insert/delete items to/from the last menu.\n")
350 _T("The commands from \"Menubar\" menu work with the ")
351 _T("menubar itself.\n\n")
352 _T("Right click the band below to test popup menus.\n"));
359 // delete the event handler installed in ctor
360 PopEventHandler(TRUE
);
362 // restore old logger
363 delete wxLog::SetActiveTarget(m_logOld
);
366 wxMenu
*MyFrame::CreateDummyMenu(wxString
*title
)
368 wxMenu
*menu
= new wxMenu
;
369 menu
->Append(Menu_Dummy_First
, "&First item\tCtrl-F1");
370 menu
->AppendSeparator();
371 menu
->Append(Menu_Dummy_Second
, "&Second item\tCtrl-F2", "", TRUE
);
375 title
->Printf(wxT("Dummy menu &%d"), ++m_countDummy
);
381 wxMenuItem
*MyFrame::GetLastMenuItem() const
383 wxMenuBar
*menubar
= GetMenuBar();
384 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
386 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
389 wxLogWarning(wxT("No last item in the last menu!"));
395 return node
->GetData();
399 void MyFrame::LogMenuEvent(const wxCommandEvent
& event
)
401 int id
= event
.GetId();
402 if ( !GetMenuBar()->FindItem(id
) )
405 wxString msg
= wxString::Format(wxT("Menu command %d"), id
);
406 if ( GetMenuBar()->FindItem(id
)->IsCheckable() )
408 msg
+= wxString::Format(wxT(" (the item is currently %schecked)"),
409 event
.IsChecked() ? "" : "not ");
415 // ----------------------------------------------------------------------------
417 // ----------------------------------------------------------------------------
419 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
424 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
429 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
431 (void)wxMessageBox("wxWindows menu sample\n© 1999-2001 Vadim Zeitlin",
432 "About wxWindows menu sample",
436 void MyFrame::OnDeleteMenu(wxCommandEvent
& WXUNUSED(event
))
438 wxMenuBar
*mbar
= GetMenuBar();
440 size_t count
= mbar
->GetMenuCount();
443 // don't let delete the first 2 menus
444 wxLogError(wxT("Can't delete any more menus"));
448 delete mbar
->Remove(count
- 1);
452 void MyFrame::OnInsertMenu(wxCommandEvent
& WXUNUSED(event
))
455 wxMenu
*menu
= CreateDummyMenu(&title
);
456 GetMenuBar()->Insert(0, menu
, title
);
459 void MyFrame::OnAppendMenu(wxCommandEvent
& WXUNUSED(event
))
462 wxMenu
*menu
= CreateDummyMenu(&title
);
463 GetMenuBar()->Append(menu
, title
);
466 void MyFrame::OnToggleMenu(wxCommandEvent
& WXUNUSED(event
))
468 wxMenuBar
*mbar
= GetMenuBar();
472 m_menu
= mbar
->Remove(0);
477 mbar
->Insert(0, m_menu
, "&File");
482 void MyFrame::OnEnableMenu(wxCommandEvent
& event
)
484 wxMenuBar
*mbar
= GetMenuBar();
485 size_t count
= mbar
->GetMenuCount();
487 mbar
->EnableTop(count
- 1, event
.IsChecked());
490 void MyFrame::OnGetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
492 wxMenuBar
*mbar
= GetMenuBar();
493 size_t count
= mbar
->GetMenuCount();
495 wxCHECK_RET( count
, _T("no last menu?") );
497 wxLogMessage(wxT("The label of the last menu item is '%s'"),
498 mbar
->GetLabelTop(count
- 1).c_str());
501 void MyFrame::OnSetLabelMenu(wxCommandEvent
& WXUNUSED(event
))
503 wxMenuBar
*mbar
= GetMenuBar();
504 size_t count
= mbar
->GetMenuCount();
506 wxCHECK_RET( count
, _T("no last menu?") );
508 wxString label
= wxGetTextFromUser
510 _T("Enter new label: "),
511 _T("Change last menu text"),
512 mbar
->GetLabelTop(count
- 1),
516 if ( !label
.empty() )
518 mbar
->SetLabelTop(count
- 1, label
);
522 void MyFrame::OnDummy(wxCommandEvent
& event
)
524 wxLogMessage(wxT("Dummy item #%d"), event
.GetId() - Menu_Dummy_First
+ 1);
527 void MyFrame::OnAppendMenuItem(wxCommandEvent
& WXUNUSED(event
))
529 wxMenuBar
*menubar
= GetMenuBar();
530 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
532 menu
->AppendSeparator();
533 menu
->Append(Menu_Dummy_Third
, "&Third dummy item\tCtrl-F3",
534 "Checkable item", TRUE
);
537 void MyFrame::OnAppendSubMenu(wxCommandEvent
& WXUNUSED(event
))
539 wxMenuBar
*menubar
= GetMenuBar();
541 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
543 menu
->Append(Menu_Dummy_Last
, "&Dummy sub menu",
544 CreateDummyMenu(NULL
), "Dummy sub menu help");
547 void MyFrame::OnDeleteMenuItem(wxCommandEvent
& WXUNUSED(event
))
549 wxMenuBar
*menubar
= GetMenuBar();
550 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
552 size_t count
= menu
->GetMenuItemCount();
555 wxLogWarning(wxT("No items to delete!"));
559 menu
->Destroy(menu
->GetMenuItems().Item(count
- 1)->GetData());
563 void MyFrame::OnInsertMenuItem(wxCommandEvent
& WXUNUSED(event
))
565 wxMenuBar
*menubar
= GetMenuBar();
566 wxMenu
*menu
= menubar
->GetMenu(menubar
->GetMenuCount() - 1);
568 menu
->Insert(0, wxMenuItem::New(menu
, Menu_Dummy_Fourth
,
569 "Fourth dummy item\tCtrl-F4"));
570 menu
->Insert(1, wxMenuItem::New(menu
, wxID_SEPARATOR
, ""));
573 void MyFrame::OnEnableMenuItem(wxCommandEvent
& WXUNUSED(event
))
575 wxMenuItem
*item
= GetLastMenuItem();
579 item
->Enable(!item
->IsEnabled());
583 void MyFrame::OnCheckMenuItem(wxCommandEvent
& WXUNUSED(event
))
585 wxMenuItem
*item
= GetLastMenuItem();
590 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent
& event
)
594 wxMenuItem
*item
= GetLastMenuItem();
596 event
.Enable(item
&& item
->IsCheckable());
599 void MyFrame::OnGetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
601 wxMenuItem
*item
= GetLastMenuItem();
605 wxLogMessage(wxT("The label of the last menu item is '%s'"),
606 item
->GetLabel().c_str());
610 void MyFrame::OnSetLabelMenuItem(wxCommandEvent
& WXUNUSED(event
))
612 wxMenuItem
*item
= GetLastMenuItem();
616 wxString label
= wxGetTextFromUser
618 _T("Enter new label: "),
619 _T("Change last menu item text"),
624 if ( !label
.empty() )
626 item
->SetText(label
);
631 void MyFrame::OnGetMenuItemInfo(wxCommandEvent
& WXUNUSED(event
))
633 wxMenuItem
*item
= GetLastMenuItem();
638 msg
<< "The item is " << (item
->IsEnabled() ? "enabled"
642 if ( item
->IsCheckable() )
644 msg
<< "It is checkable and " << (item
->IsChecked() ? "" : "un")
649 wxAcceleratorEntry
*accel
= item
->GetAccel();
652 msg
<< "Its accelerator is ";
654 int flags
= accel
->GetFlags();
655 if ( flags
& wxACCEL_ALT
)
657 if ( flags
& wxACCEL_CTRL
)
659 if ( flags
& wxACCEL_SHIFT
)
660 msg
<< wxT("Shift-");
662 int code
= accel
->GetKeyCode();
677 msg
<< wxT('F') << code
- WXK_F1
+ 1;
680 // if there are any other keys wxGetAccelFromString() may return,
681 // we should process them here
684 if ( wxIsalnum(code
) )
691 wxFAIL_MSG( wxT("unknown keyboard accel") );
698 msg
<< "It doesn't have an accelerator";
700 #endif // wxUSE_ACCEL
706 void MyFrame::OnRightUp(wxMouseEvent
&event
)
708 wxMenu
menu("Test popup");
710 menu
.Append(Menu_Help_About
, "&About");
711 menu
.Append(Menu_Popup_Submenu
, "&Submenu", CreateDummyMenu(NULL
));
712 menu
.Append(Menu_Popup_ToBeDeleted
, "To be &deleted");
713 menu
.Append(Menu_Popup_ToBeChecked
, "To be &checked", "", TRUE
);
714 menu
.Append(Menu_Popup_ToBeGreyed
, "To be &greyed");
715 menu
.AppendSeparator();
716 menu
.Append(Menu_File_Quit
, "E&xit");
718 menu
.Delete(Menu_Popup_ToBeDeleted
);
719 menu
.Check(Menu_Popup_ToBeChecked
, TRUE
);
720 menu
.Enable(Menu_Popup_ToBeGreyed
, FALSE
);
722 PopupMenu(&menu
, event
.GetX(), event
.GetY());
724 // test for destroying items in popup menus
725 #if 0 // doesn't work in wxGTK!
726 menu
.Destroy(Menu_Popup_Submenu
);
728 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
732 void MyFrame::OnSize(wxSizeEvent
& event
)
737 // leave a band below for popup menu testing
738 wxSize size
= GetClientSize();
739 m_textctrl
->SetSize(0, 0, size
.x
, (3*size
.y
)/4);
741 // this is really ugly but we have to do it as we can't just call
742 // event.Skip() because wxFrameBase would make the text control fill the
744 #ifdef __WXUNIVERSAL__
746 #endif // __WXUNIVERSAL__