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