]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/menu/menu.cpp
wxListMainWindow::HitTest no longer tests 'flags' which is garbage
[wxWidgets.git] / samples / menu / menu.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: samples/menu.cpp
3// Purpose: wxMenu/wxMenuBar sample
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 01.11.99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Vadim Zeitlin
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx/wx.h".
21#include <wx/wxprec.h>
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#ifndef WX_PRECOMP
28 #include <wx/wx.h>
29 #include <wx/log.h>
30#endif
31
32#include "copy.xpm"
33
34// ----------------------------------------------------------------------------
35// classes
36// ----------------------------------------------------------------------------
37
38// Define a new application
39class MyApp: public wxApp
40{
41public:
42 bool OnInit();
43};
44
45// Define a new frame
46class MyFrame: public wxFrame
47{
48public:
49 MyFrame();
50
51 virtual ~MyFrame();
52
53 void LogMenuEvent(const wxCommandEvent& event);
54
55 void OnQuit(wxCommandEvent& event);
56 void OnAbout(wxCommandEvent& event);
57
58 void OnDummy(wxCommandEvent& event);
59
60 void OnAppendMenuItem(wxCommandEvent& event);
61 void OnAppendSubMenu(wxCommandEvent& event);
62 void OnDeleteMenuItem(wxCommandEvent& event);
63 void OnInsertMenuItem(wxCommandEvent& event);
64 void OnCheckMenuItem(wxCommandEvent& event);
65 void OnEnableMenuItem(wxCommandEvent& event);
66 void OnGetLabelMenuItem(wxCommandEvent& event);
67 void OnSetLabelMenuItem(wxCommandEvent& event);
68 void OnGetMenuItemInfo(wxCommandEvent& event);
69
70 void OnAppendMenu(wxCommandEvent& event);
71 void OnInsertMenu(wxCommandEvent& event);
72 void OnDeleteMenu(wxCommandEvent& event);
73 void OnToggleMenu(wxCommandEvent& event);
74 void OnEnableMenu(wxCommandEvent& event);
75 void OnGetLabelMenu(wxCommandEvent& event);
76 void OnSetLabelMenu(wxCommandEvent& event);
77
78 void OnRightDown(wxMouseEvent& event);
79
80 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event);
81
82private:
83 wxMenu *CreateDummyMenu(wxString *title);
84
85 wxMenuItem *GetLastMenuItem() const;
86
87 wxMenu *m_menu;
88
89 size_t m_countDummy;
90
91 DECLARE_EVENT_TABLE()
92};
93
94// A small helper class which intercepts all menu events and logs them
95class MyEvtHandler : public wxEvtHandler
96{
97public:
98 MyEvtHandler(MyFrame *frame) { m_frame = frame; }
99
100 void OnMenuEvent(wxCommandEvent& event)
101 {
102 m_frame->LogMenuEvent(event);
103
104 event.Skip();
105 }
106
107private:
108 MyFrame *m_frame;
109
110 DECLARE_EVENT_TABLE()
111};
112
113// ----------------------------------------------------------------------------
114// constants
115// ----------------------------------------------------------------------------
116
117enum
118{
119 Menu_File_Quit = 100,
120
121 Menu_MenuBar_Toggle = 200,
122 Menu_MenuBar_Append,
123 Menu_MenuBar_Insert,
124 Menu_MenuBar_Delete,
125 Menu_MenuBar_Enable,
126 Menu_MenuBar_GetLabel,
127 Menu_MenuBar_SetLabel,
128
129 Menu_Menu_Append = 300,
130 Menu_Menu_AppendSub,
131 Menu_Menu_Insert,
132 Menu_Menu_Delete,
133 Menu_Menu_Enable,
134 Menu_Menu_Check,
135 Menu_Menu_GetLabel,
136 Menu_Menu_SetLabel,
137 Menu_Menu_GetInfo,
138
139 Menu_Dummy_First = 400,
140 Menu_Dummy_Second,
141 Menu_Dummy_Third,
142 Menu_Dummy_Fourth,
143 Menu_Dummy_Last,
144
145 Menu_Help_About = 1000,
146
147 Menu_Popup_ToBeDeleted = 2000,
148 Menu_Popup_ToBeGreyed,
149 Menu_Popup_ToBeChecked,
150 Menu_Popup_Submenu,
151
152 Menu_Max
153};
154
155// ----------------------------------------------------------------------------
156// event tables
157// ----------------------------------------------------------------------------
158
159BEGIN_EVENT_TABLE(MyFrame, wxFrame)
160 EVT_MENU(Menu_File_Quit, MyFrame::OnQuit)
161
162 EVT_MENU(Menu_Help_About, MyFrame::OnAbout)
163
164 EVT_MENU(Menu_MenuBar_Toggle, MyFrame::OnToggleMenu)
165 EVT_MENU(Menu_MenuBar_Append, MyFrame::OnAppendMenu)
166 EVT_MENU(Menu_MenuBar_Insert, MyFrame::OnInsertMenu)
167 EVT_MENU(Menu_MenuBar_Delete, MyFrame::OnDeleteMenu)
168 EVT_MENU(Menu_MenuBar_Enable, MyFrame::OnEnableMenu)
169 EVT_MENU(Menu_MenuBar_GetLabel, MyFrame::OnGetLabelMenu)
170 EVT_MENU(Menu_MenuBar_SetLabel, MyFrame::OnSetLabelMenu)
171
172 EVT_MENU(Menu_Menu_Append, MyFrame::OnAppendMenuItem)
173 EVT_MENU(Menu_Menu_AppendSub, MyFrame::OnAppendSubMenu)
174 EVT_MENU(Menu_Menu_Insert, MyFrame::OnInsertMenuItem)
175 EVT_MENU(Menu_Menu_Delete, MyFrame::OnDeleteMenuItem)
176 EVT_MENU(Menu_Menu_Enable, MyFrame::OnEnableMenuItem)
177 EVT_MENU(Menu_Menu_Check, MyFrame::OnCheckMenuItem)
178 EVT_MENU(Menu_Menu_GetLabel, MyFrame::OnGetLabelMenuItem)
179 EVT_MENU(Menu_Menu_SetLabel, MyFrame::OnSetLabelMenuItem)
180 EVT_MENU(Menu_Menu_GetInfo, MyFrame::OnGetMenuItemInfo)
181
182 EVT_MENU_RANGE(Menu_Dummy_First, Menu_Dummy_Last, MyFrame::OnDummy)
183
184 EVT_UPDATE_UI(Menu_Menu_Check, MyFrame::OnUpdateCheckMenuItemUI)
185
186 EVT_RIGHT_DOWN(MyFrame::OnRightDown)
187END_EVENT_TABLE()
188
189BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
190 EVT_MENU(-1, MyEvtHandler::OnMenuEvent)
191END_EVENT_TABLE()
192
193// ============================================================================
194// implementation
195// ============================================================================
196
197// ----------------------------------------------------------------------------
198// MyApp
199// ----------------------------------------------------------------------------
200
201IMPLEMENT_APP(MyApp)
202
203// The `main program' equivalent, creating the windows and returning the
204// main frame
205bool MyApp::OnInit()
206{
207 // Create the main frame window
208 MyFrame* frame = new MyFrame;
209
210 frame->Show(TRUE);
211
212 frame->SetStatusText("Hello, wxWindows");
213
214 SetTopWindow(frame);
215
216 return TRUE;
217}
218
219// ----------------------------------------------------------------------------
220// MyFrame
221// ----------------------------------------------------------------------------
222
223// Define my frame constructor
224MyFrame::MyFrame()
225 : wxFrame((wxFrame *)NULL, -1, "wxWindows menu sample",
226 wxDefaultPosition, wxSize(300, 200))
227{
228 m_menu = NULL;
229 m_countDummy = 0;
230
231 CreateStatusBar(2);
232
233 // create the menubar
234 wxMenu *fileMenu = new wxMenu;
235 fileMenu->Append(Menu_File_Quit, "E&xit\tAlt-X", "Quit toolbar sample" );
236
237 wxMenuItem *bitmap_menu_item = new wxMenuItem( fileMenu, Menu_File_Quit, "Quit with &bitmap" );
238 bitmap_menu_item->SetBitmap( wxBitmap( copy_xpm ) );
239 fileMenu->Append( bitmap_menu_item );
240
241 wxMenu *menubarMenu = new wxMenu;
242 menubarMenu->Append(Menu_MenuBar_Append, "&Append menu\tCtrl-A",
243 "Append a menu to the menubar");
244 menubarMenu->Append(Menu_MenuBar_Insert, "&Insert menu\tCtrl-I",
245 "Insert a menu into the menubar");
246 menubarMenu->Append(Menu_MenuBar_Delete, "&Delete menu\tCtrl-D",
247 "Delete the last menu from the menubar");
248 menubarMenu->Append(Menu_MenuBar_Toggle, "&Toggle menu\tCtrl-T",
249 "Toggle the first menu in the menubar", TRUE);
250 menubarMenu->AppendSeparator();
251 menubarMenu->Append(Menu_MenuBar_Enable, "&Enable menu\tCtrl-E",
252 "Enable or disable the last menu", TRUE);
253 menubarMenu->AppendSeparator();
254 menubarMenu->Append(Menu_MenuBar_GetLabel, "&Get menu label\tCtrl-G",
255 "Get the label of the last menu");
256 menubarMenu->Append(Menu_MenuBar_SetLabel, "&Set menu label\tCtrl-S",
257 "Change the label of the last menu");
258
259 wxMenu *menuMenu = new wxMenu;
260 menuMenu->Append(Menu_Menu_Append, "&Append menu item\tAlt-A",
261 "Append a menu item to the last menu");
262 menuMenu->Append(Menu_Menu_AppendSub, "&Append sub menu\tAlt-S",
263 "Append a sub menu to the last menu");
264 menuMenu->Append(Menu_Menu_Insert, "&Insert menu item\tAlt-I",
265 "Insert a menu item in head of the last menu");
266 menuMenu->Append(Menu_Menu_Delete, "&Delete menu item\tAlt-D",
267 "Delete the last menu item from the last menu");
268 menuMenu->AppendSeparator();
269 menuMenu->Append(Menu_Menu_Enable, "&Enable menu item\tAlt-E",
270 "Enable or disable the last menu item", TRUE);
271 menuMenu->Append(Menu_Menu_Check, "&Check menu item\tAlt-C",
272 "Check or uncheck the last menu item", TRUE);
273 menuMenu->AppendSeparator();
274 menuMenu->Append(Menu_Menu_GetLabel, "&Get menu item label\tAlt-G",
275 "Get the label of the last menu item");
276 menuMenu->Append(Menu_Menu_SetLabel, "&Set menu item label\tAlt-S",
277 "Change the label of the last menu item");
278 menuMenu->AppendSeparator();
279 menuMenu->Append(Menu_Menu_GetInfo, "Get menu item in&fo\tAlt-F",
280 "Show the state of the last menu item");
281
282 wxMenu *helpMenu = new wxMenu;
283 helpMenu->Append(Menu_Help_About, "&About\tF1", "About menu sample");
284
285 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
286
287 menuBar->Append(fileMenu, "&File");
288 menuBar->Append(menubarMenu, "Menu&bar");
289 menuBar->Append(menuMenu, "&Menu");
290 menuBar->Append(helpMenu, "&Help");
291
292 // these items should be initially checked
293 menuBar->Check(Menu_MenuBar_Toggle, TRUE);
294 menuBar->Check(Menu_MenuBar_Enable, TRUE);
295 menuBar->Check(Menu_Menu_Enable, TRUE);
296 menuBar->Check(Menu_Menu_Check, FALSE);
297
298 // associate the menu bar with the frame
299 SetMenuBar(menuBar);
300
301 // intercept all menu events and log them in this custom event handler
302 PushEventHandler(new MyEvtHandler(this));
303}
304
305MyFrame::~MyFrame()
306{
307 delete m_menu;
308
309 // delete the event handler installed in ctor
310 PopEventHandler(TRUE);
311}
312
313wxMenu *MyFrame::CreateDummyMenu(wxString *title)
314{
315 wxMenu *menu = new wxMenu;
316 menu->Append(Menu_Dummy_First, "First item\tCtrl-F1");
317 menu->AppendSeparator();
318 menu->Append(Menu_Dummy_Second, "Second item\tCtrl-F2", "", TRUE);
319
320 if ( title )
321 {
322 title->Printf("Dummy menu &%d", ++m_countDummy);
323 }
324
325 return menu;
326}
327
328wxMenuItem *MyFrame::GetLastMenuItem() const
329{
330 wxMenuBar *menubar = GetMenuBar();
331 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
332
333 wxMenuItemList::Node *node = menu->GetMenuItems().GetLast();
334 if ( !node )
335 {
336 wxLogWarning("No last item in the last menu!");
337
338 return NULL;
339 }
340 else
341 {
342 return node->GetData();
343 }
344}
345
346void MyFrame::LogMenuEvent(const wxCommandEvent& event)
347{
348 int id = event.GetId();
349 wxString msg = wxString::Format("Menu command %d", id);
350 if ( GetMenuBar()->FindItem(id)->IsCheckable() )
351 {
352 msg += wxString::Format(" (the item is currently %schecked)",
353 event.IsChecked() ? "" : "not ");
354 }
355
356 SetStatusText(msg, 1);
357}
358
359// ----------------------------------------------------------------------------
360// menu callbacks
361// ----------------------------------------------------------------------------
362
363void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
364{
365 Close(TRUE);
366}
367
368void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
369{
370 (void)wxMessageBox("wxWindows toolbar sample",
371 "About wxWindows menu sample",
372 wxICON_INFORMATION);
373}
374
375void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event))
376{
377 wxMenuBar *mbar = GetMenuBar();
378
379 size_t count = mbar->GetMenuCount();
380 if ( count == 2 )
381 {
382 // don't let delete the first 2 menus
383 wxLogError("Can't delete any more menus");
384 }
385 else
386 {
387 delete mbar->Remove(count - 1);
388 }
389}
390
391void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event))
392{
393 wxString title;
394 wxMenu *menu = CreateDummyMenu(&title);
395 GetMenuBar()->Insert(0, menu, title);
396}
397
398void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event))
399{
400 wxString title;
401 wxMenu *menu = CreateDummyMenu(&title);
402 GetMenuBar()->Append(menu, title);
403}
404
405void MyFrame::OnToggleMenu(wxCommandEvent& WXUNUSED(event))
406{
407 wxMenuBar *mbar = GetMenuBar();
408 if ( !m_menu )
409 {
410 // hide the menu
411 m_menu = mbar->Remove(0);
412 }
413 else
414 {
415 // restore it
416 mbar->Insert(0, m_menu, "&File");
417 m_menu = NULL;
418 }
419}
420
421void MyFrame::OnEnableMenu(wxCommandEvent& WXUNUSED(event))
422{
423 wxMenuBar *mbar = GetMenuBar();
424 size_t count = mbar->GetMenuCount();
425
426 static bool s_enabled = TRUE;
427
428 s_enabled = !s_enabled;
429 mbar->EnableTop(count - 1, s_enabled);
430}
431
432void MyFrame::OnGetLabelMenu(wxCommandEvent& WXUNUSED(event))
433{
434 wxMenuBar *mbar = GetMenuBar();
435 size_t count = mbar->GetMenuCount();
436
437 wxLogMessage("The label of the last menu item is '%s'",
438 mbar->GetLabelTop(count - 1).c_str());
439}
440
441void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event))
442{
443 wxMenuBar *mbar = GetMenuBar();
444 size_t count = mbar->GetMenuCount();
445
446 mbar->SetLabelTop(count - 1, "Dummy label &0");
447}
448
449void MyFrame::OnDummy(wxCommandEvent& event)
450{
451 wxString s;
452 s.Printf("Dummy item #%d", event.GetId() - Menu_Dummy_First + 1);
453 wxMessageBox(s, "Menu sample", wxICON_INFORMATION);
454}
455
456void MyFrame::OnAppendMenuItem(wxCommandEvent& WXUNUSED(event))
457{
458 wxMenuBar *menubar = GetMenuBar();
459 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
460
461 menu->AppendSeparator();
462 menu->Append(Menu_Dummy_Third, "Third dummy item\tCtrl-F3",
463 "Checkable item", TRUE);
464}
465
466void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event))
467{
468 wxMenuBar *menubar = GetMenuBar();
469
470 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
471
472 menu->Append(Menu_Dummy_Last, "Dummy sub menu",
473 CreateDummyMenu(NULL), "Dummy sub menu help");
474}
475
476void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event))
477{
478 wxMenuBar *menubar = GetMenuBar();
479 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
480
481 size_t count = menu->GetMenuItemCount();
482 if ( !count )
483 {
484 wxLogWarning("No items to delete!");
485 }
486 else
487 {
488 menu->Destroy(menu->GetMenuItems().Item(count - 1)->GetData());
489 }
490}
491
492void MyFrame::OnInsertMenuItem(wxCommandEvent& WXUNUSED(event))
493{
494 wxMenuBar *menubar = GetMenuBar();
495 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
496
497 menu->Insert(0, wxMenuItem::New(menu, Menu_Dummy_Fourth,
498 "Fourth dummy item\tCtrl-F4"));
499 menu->Insert(1, wxMenuItem::New(menu, wxID_SEPARATOR, ""));
500}
501
502void MyFrame::OnEnableMenuItem(wxCommandEvent& WXUNUSED(event))
503{
504 wxMenuItem *item = GetLastMenuItem();
505
506 if ( item )
507 {
508 item->Enable(!item->IsEnabled());
509 }
510}
511
512void MyFrame::OnCheckMenuItem(wxCommandEvent& WXUNUSED(event))
513{
514 wxMenuItem *item = GetLastMenuItem();
515
516 item->Toggle();
517}
518
519void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event)
520{
521 wxLogNull nolog;
522
523 wxMenuItem *item = GetLastMenuItem();
524
525 event.Enable(item && item->IsCheckable());
526}
527
528void MyFrame::OnGetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
529{
530 wxMenuItem *item = GetLastMenuItem();
531
532 if ( item )
533 {
534 wxLogMessage("The label of the last menu item is '%s'",
535 item->GetLabel().c_str());
536 }
537}
538
539void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
540{
541 wxMenuItem *item = GetLastMenuItem();
542
543 if ( item )
544 {
545 item->SetText("Dummy menu item text");
546 }
547}
548
549void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event))
550{
551 wxMenuItem *item = GetLastMenuItem();
552
553 if ( item )
554 {
555 wxString msg;
556 msg << "The item is " << (item->IsEnabled() ? "enabled"
557 : "disabled")
558 << '\n';
559
560 if ( item->IsCheckable() )
561 {
562 msg << "It is checkable and " << (item->IsChecked() ? "" : "un")
563 << "checked\n";
564 }
565
566#if wxUSE_ACCEL
567 wxAcceleratorEntry *accel = item->GetAccel();
568 if ( accel )
569 {
570 msg << "Its accelerator is ";
571
572 int flags = accel->GetFlags();
573 if ( flags & wxACCEL_ALT )
574 msg << wxT("Alt-");
575 if ( flags & wxACCEL_CTRL )
576 msg << wxT("Ctrl-");
577 if ( flags & wxACCEL_SHIFT )
578 msg << wxT("Shift-");
579
580 int code = accel->GetKeyCode();
581 switch ( code )
582 {
583 case WXK_F1:
584 case WXK_F2:
585 case WXK_F3:
586 case WXK_F4:
587 case WXK_F5:
588 case WXK_F6:
589 case WXK_F7:
590 case WXK_F8:
591 case WXK_F9:
592 case WXK_F10:
593 case WXK_F11:
594 case WXK_F12:
595 msg << wxT('F') << code - WXK_F1 + 1;
596 break;
597
598 // if there are any other keys wxGetAccelFromString() may return,
599 // we should process them here
600
601 default:
602 if ( wxIsalnum(code) )
603 {
604 msg << (wxChar)code;
605
606 break;
607 }
608
609 wxFAIL_MSG( wxT("unknown keyboard accel") );
610 }
611
612 delete accel;
613 }
614 else
615 {
616 msg << "It doesn't have an accelerator";
617 }
618#endif // wxUSE_ACCEL
619
620 wxLogMessage(msg);
621 }
622}
623
624void MyFrame::OnRightDown(wxMouseEvent &event )
625{
626 wxMenu menu("Test popup");
627
628 menu.Append(Menu_Help_About, "&About");
629 menu.Append(Menu_Popup_Submenu, "Submenu", CreateDummyMenu(NULL));
630 menu.Append(Menu_Popup_ToBeDeleted, "To be deleted");
631 menu.Append(Menu_Popup_ToBeChecked, "To be checked", "", TRUE);
632 menu.Append(Menu_Popup_ToBeGreyed, "To be greyed");
633 menu.AppendSeparator();
634 menu.Append(Menu_File_Quit, "E&xit");
635
636 menu.Delete(Menu_Popup_ToBeDeleted);
637 menu.Check(Menu_Popup_ToBeChecked, TRUE);
638 menu.Enable(Menu_Popup_ToBeGreyed, FALSE);
639
640 PopupMenu( &menu, event.GetX(), event.GetY() );
641
642 // test for destroying items in popup menus
643#if 0
644 menu.Destroy(Menu_Popup_Submenu);
645
646 PopupMenu( &menu, event.GetX(), event.GetY() );
647#endif // 0
648}