]> git.saurik.com Git - wxWidgets.git/blob - samples/menu/menu.cpp
generated code updates
[wxWidgets.git] / samples / menu / menu.cpp
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 #if !wxUSE_MENUS
33 // nice try...
34 #error "menu sample requires wxUSE_MENUS=1"
35 #endif // wxUSE_MENUS
36
37 #include "copy.xpm"
38
39 #ifdef __WXUNIVERSAL__
40 #include "wx/univ/theme.h"
41
42 WX_USE_THEME(win32);
43 WX_USE_THEME(gtk);
44
45 // not implemented yet
46 #define wxMessageBox
47 #endif // __WXUNIVERSAL__
48
49 // ----------------------------------------------------------------------------
50 // classes
51 // ----------------------------------------------------------------------------
52
53 // Define a new application
54 class MyApp: public wxApp
55 {
56 public:
57 bool OnInit();
58 };
59
60 // Define a new frame
61 class MyFrame: public wxFrame
62 {
63 public:
64 MyFrame();
65
66 virtual ~MyFrame();
67
68 void LogMenuEvent(const wxCommandEvent& event);
69
70 void OnQuit(wxCommandEvent& event);
71 void OnAbout(wxCommandEvent& event);
72
73 void OnDummy(wxCommandEvent& event);
74
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);
84
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);
92
93 void OnRightDown(wxMouseEvent& event);
94
95 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event);
96
97 private:
98 wxMenu *CreateDummyMenu(wxString *title);
99
100 wxMenuItem *GetLastMenuItem() const;
101
102 wxMenu *m_menu;
103
104 size_t m_countDummy;
105
106 DECLARE_EVENT_TABLE()
107 };
108
109 // A small helper class which intercepts all menu events and logs them
110 class MyEvtHandler : public wxEvtHandler
111 {
112 public:
113 MyEvtHandler(MyFrame *frame) { m_frame = frame; }
114
115 void OnMenuEvent(wxCommandEvent& event)
116 {
117 m_frame->LogMenuEvent(event);
118
119 event.Skip();
120 }
121
122 private:
123 MyFrame *m_frame;
124
125 DECLARE_EVENT_TABLE()
126 };
127
128 // ----------------------------------------------------------------------------
129 // constants
130 // ----------------------------------------------------------------------------
131
132 enum
133 {
134 Menu_File_Quit = 100,
135
136 Menu_MenuBar_Toggle = 200,
137 Menu_MenuBar_Append,
138 Menu_MenuBar_Insert,
139 Menu_MenuBar_Delete,
140 Menu_MenuBar_Enable,
141 Menu_MenuBar_GetLabel,
142 Menu_MenuBar_SetLabel,
143
144 Menu_Menu_Append = 300,
145 Menu_Menu_AppendSub,
146 Menu_Menu_Insert,
147 Menu_Menu_Delete,
148 Menu_Menu_Enable,
149 Menu_Menu_Check,
150 Menu_Menu_GetLabel,
151 Menu_Menu_SetLabel,
152 Menu_Menu_GetInfo,
153
154 Menu_Dummy_First = 400,
155 Menu_Dummy_Second,
156 Menu_Dummy_Third,
157 Menu_Dummy_Fourth,
158 Menu_Dummy_Last,
159
160 Menu_Help_About = 1000,
161
162 Menu_Popup_ToBeDeleted = 2000,
163 Menu_Popup_ToBeGreyed,
164 Menu_Popup_ToBeChecked,
165 Menu_Popup_Submenu,
166
167 Menu_Max
168 };
169
170 // ----------------------------------------------------------------------------
171 // event tables
172 // ----------------------------------------------------------------------------
173
174 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
175 EVT_MENU(Menu_File_Quit, MyFrame::OnQuit)
176
177 EVT_MENU(Menu_Help_About, MyFrame::OnAbout)
178
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)
186
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)
196
197 EVT_MENU_RANGE(Menu_Dummy_First, Menu_Dummy_Last, MyFrame::OnDummy)
198
199 EVT_UPDATE_UI(Menu_Menu_Check, MyFrame::OnUpdateCheckMenuItemUI)
200
201 EVT_RIGHT_DOWN(MyFrame::OnRightDown)
202 END_EVENT_TABLE()
203
204 BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
205 EVT_MENU(-1, MyEvtHandler::OnMenuEvent)
206 END_EVENT_TABLE()
207
208 // ============================================================================
209 // implementation
210 // ============================================================================
211
212 // ----------------------------------------------------------------------------
213 // MyApp
214 // ----------------------------------------------------------------------------
215
216 IMPLEMENT_APP(MyApp)
217
218 // The `main program' equivalent, creating the windows and returning the
219 // main frame
220 bool MyApp::OnInit()
221 {
222 // Create the main frame window
223 MyFrame* frame = new MyFrame;
224
225 frame->Show(TRUE);
226
227 #if wxUSE_STATUSBAR
228 frame->SetStatusText("Hello, wxWindows");
229 #endif // wxUSE_STATUSBAR
230
231 SetTopWindow(frame);
232
233 return TRUE;
234 }
235
236 // ----------------------------------------------------------------------------
237 // MyFrame
238 // ----------------------------------------------------------------------------
239
240 // Define my frame constructor
241 MyFrame::MyFrame()
242 : wxFrame((wxFrame *)NULL, -1, "wxWindows menu sample",
243 wxDefaultPosition, wxSize(300, 200))
244 {
245 m_menu = NULL;
246 m_countDummy = 0;
247
248 #if wxUSE_STATUSBAR
249 CreateStatusBar(2);
250 #endif // wxUSE_STATUSBAR
251
252 // create the menubar
253 wxMenu *fileMenu = new wxMenu;
254 fileMenu->Append(Menu_File_Quit, "E&xit\tAlt-X", "Quit toolbar sample");
255
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);
260
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");
278
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");
301
302 wxMenu *helpMenu = new wxMenu;
303 helpMenu->Append(Menu_Help_About, "&About\tF1", "About menu sample");
304
305 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
306
307 menuBar->Append(fileMenu, "&File");
308 menuBar->Append(menubarMenu, "Menu&bar");
309 menuBar->Append(menuMenu, "&Menu");
310 menuBar->Append(helpMenu, "&Help");
311
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);
317
318 // associate the menu bar with the frame
319 SetMenuBar(menuBar);
320
321 // intercept all menu events and log them in this custom event handler
322 PushEventHandler(new MyEvtHandler(this));
323 }
324
325 MyFrame::~MyFrame()
326 {
327 delete m_menu;
328
329 // delete the event handler installed in ctor
330 PopEventHandler(TRUE);
331 }
332
333 wxMenu *MyFrame::CreateDummyMenu(wxString *title)
334 {
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);
339
340 if ( title )
341 {
342 title->Printf("Dummy menu &%d", ++m_countDummy);
343 }
344
345 return menu;
346 }
347
348 wxMenuItem *MyFrame::GetLastMenuItem() const
349 {
350 wxMenuBar *menubar = GetMenuBar();
351 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
352
353 wxMenuItemList::Node *node = menu->GetMenuItems().GetLast();
354 if ( !node )
355 {
356 wxLogWarning("No last item in the last menu!");
357
358 return NULL;
359 }
360 else
361 {
362 return node->GetData();
363 }
364 }
365
366 void MyFrame::LogMenuEvent(const wxCommandEvent& event)
367 {
368 int id = event.GetId();
369 if (!GetMenuBar()->FindItem(id))
370 return;
371 wxString msg = wxString::Format("Menu command %d", id);
372 if ( GetMenuBar()->FindItem(id)->IsCheckable() )
373 {
374 msg += wxString::Format(" (the item is currently %schecked)",
375 event.IsChecked() ? "" : "not ");
376 }
377
378 #if wxUSE_STATUSBAR
379 SetStatusText(msg, 1);
380 #endif // wxUSE_STATUSBAR
381 }
382
383 // ----------------------------------------------------------------------------
384 // menu callbacks
385 // ----------------------------------------------------------------------------
386
387 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
388 {
389 Close(TRUE);
390 }
391
392 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
393 {
394 (void)wxMessageBox("wxWindows toolbar sample",
395 "About wxWindows menu sample",
396 wxICON_INFORMATION);
397 }
398
399 void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event))
400 {
401 wxMenuBar *mbar = GetMenuBar();
402
403 size_t count = mbar->GetMenuCount();
404 if ( count == 2 )
405 {
406 // don't let delete the first 2 menus
407 wxLogError("Can't delete any more menus");
408 }
409 else
410 {
411 delete mbar->Remove(count - 1);
412 }
413 }
414
415 void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event))
416 {
417 wxString title;
418 wxMenu *menu = CreateDummyMenu(&title);
419 GetMenuBar()->Insert(0, menu, title);
420 }
421
422 void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event))
423 {
424 wxString title;
425 wxMenu *menu = CreateDummyMenu(&title);
426 GetMenuBar()->Append(menu, title);
427 }
428
429 void MyFrame::OnToggleMenu(wxCommandEvent& WXUNUSED(event))
430 {
431 wxMenuBar *mbar = GetMenuBar();
432 if ( !m_menu )
433 {
434 // hide the menu
435 m_menu = mbar->Remove(0);
436 }
437 else
438 {
439 // restore it
440 mbar->Insert(0, m_menu, "&File");
441 m_menu = NULL;
442 }
443 }
444
445 void MyFrame::OnEnableMenu(wxCommandEvent& WXUNUSED(event))
446 {
447 wxMenuBar *mbar = GetMenuBar();
448 size_t count = mbar->GetMenuCount();
449
450 static bool s_enabled = TRUE;
451
452 s_enabled = !s_enabled;
453 mbar->EnableTop(count - 1, s_enabled);
454 }
455
456 void MyFrame::OnGetLabelMenu(wxCommandEvent& WXUNUSED(event))
457 {
458 wxMenuBar *mbar = GetMenuBar();
459 size_t count = mbar->GetMenuCount();
460
461 wxLogMessage("The label of the last menu item is '%s'",
462 mbar->GetLabelTop(count - 1).c_str());
463 }
464
465 void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event))
466 {
467 wxMenuBar *mbar = GetMenuBar();
468 size_t count = mbar->GetMenuCount();
469
470 mbar->SetLabelTop(count - 1, "Dummy label &0");
471 }
472
473 void MyFrame::OnDummy(wxCommandEvent& event)
474 {
475 wxString s;
476 s.Printf("Dummy item #%d", event.GetId() - Menu_Dummy_First + 1);
477 wxMessageBox(s, "Menu sample", wxICON_INFORMATION);
478 }
479
480 void MyFrame::OnAppendMenuItem(wxCommandEvent& WXUNUSED(event))
481 {
482 wxMenuBar *menubar = GetMenuBar();
483 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
484
485 menu->AppendSeparator();
486 menu->Append(Menu_Dummy_Third, "Third dummy item\tCtrl-F3",
487 "Checkable item", TRUE);
488 }
489
490 void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event))
491 {
492 wxMenuBar *menubar = GetMenuBar();
493
494 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
495
496 menu->Append(Menu_Dummy_Last, "Dummy sub menu",
497 CreateDummyMenu(NULL), "Dummy sub menu help");
498 }
499
500 void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event))
501 {
502 wxMenuBar *menubar = GetMenuBar();
503 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
504
505 size_t count = menu->GetMenuItemCount();
506 if ( !count )
507 {
508 wxLogWarning("No items to delete!");
509 }
510 else
511 {
512 menu->Destroy(menu->GetMenuItems().Item(count - 1)->GetData());
513 }
514 }
515
516 void MyFrame::OnInsertMenuItem(wxCommandEvent& WXUNUSED(event))
517 {
518 wxMenuBar *menubar = GetMenuBar();
519 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
520
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, ""));
524 }
525
526 void MyFrame::OnEnableMenuItem(wxCommandEvent& WXUNUSED(event))
527 {
528 wxMenuItem *item = GetLastMenuItem();
529
530 if ( item )
531 {
532 item->Enable(!item->IsEnabled());
533 }
534 }
535
536 void MyFrame::OnCheckMenuItem(wxCommandEvent& WXUNUSED(event))
537 {
538 wxMenuItem *item = GetLastMenuItem();
539
540 item->Toggle();
541 }
542
543 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event)
544 {
545 wxLogNull nolog;
546
547 wxMenuItem *item = GetLastMenuItem();
548
549 event.Enable(item && item->IsCheckable());
550 }
551
552 void MyFrame::OnGetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
553 {
554 wxMenuItem *item = GetLastMenuItem();
555
556 if ( item )
557 {
558 wxLogMessage("The label of the last menu item is '%s'",
559 item->GetLabel().c_str());
560 }
561 }
562
563 void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
564 {
565 wxMenuItem *item = GetLastMenuItem();
566
567 if ( item )
568 {
569 item->SetText("Dummy menu item text");
570 }
571 }
572
573 void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event))
574 {
575 wxMenuItem *item = GetLastMenuItem();
576
577 if ( item )
578 {
579 wxString msg;
580 msg << "The item is " << (item->IsEnabled() ? "enabled"
581 : "disabled")
582 << '\n';
583
584 if ( item->IsCheckable() )
585 {
586 msg << "It is checkable and " << (item->IsChecked() ? "" : "un")
587 << "checked\n";
588 }
589
590 #if wxUSE_ACCEL
591 wxAcceleratorEntry *accel = item->GetAccel();
592 if ( accel )
593 {
594 msg << "Its accelerator is ";
595
596 int flags = accel->GetFlags();
597 if ( flags & wxACCEL_ALT )
598 msg << wxT("Alt-");
599 if ( flags & wxACCEL_CTRL )
600 msg << wxT("Ctrl-");
601 if ( flags & wxACCEL_SHIFT )
602 msg << wxT("Shift-");
603
604 int code = accel->GetKeyCode();
605 switch ( code )
606 {
607 case WXK_F1:
608 case WXK_F2:
609 case WXK_F3:
610 case WXK_F4:
611 case WXK_F5:
612 case WXK_F6:
613 case WXK_F7:
614 case WXK_F8:
615 case WXK_F9:
616 case WXK_F10:
617 case WXK_F11:
618 case WXK_F12:
619 msg << wxT('F') << code - WXK_F1 + 1;
620 break;
621
622 // if there are any other keys wxGetAccelFromString() may return,
623 // we should process them here
624
625 default:
626 if ( wxIsalnum(code) )
627 {
628 msg << (wxChar)code;
629
630 break;
631 }
632
633 wxFAIL_MSG( wxT("unknown keyboard accel") );
634 }
635
636 delete accel;
637 }
638 else
639 {
640 msg << "It doesn't have an accelerator";
641 }
642 #endif // wxUSE_ACCEL
643
644 wxLogMessage(msg);
645 }
646 }
647
648 void MyFrame::OnRightDown(wxMouseEvent &event )
649 {
650 wxMenu menu("Test popup");
651
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");
659
660 menu.Delete(Menu_Popup_ToBeDeleted);
661 menu.Check(Menu_Popup_ToBeChecked, TRUE);
662 menu.Enable(Menu_Popup_ToBeGreyed, FALSE);
663
664 PopupMenu( &menu, event.GetX(), event.GetY() );
665
666 // test for destroying items in popup menus
667 #if 0
668 menu.Destroy(Menu_Popup_Submenu);
669
670 PopupMenu( &menu, event.GetX(), event.GetY() );
671 #endif // 0
672 }