]> git.saurik.com Git - wxWidgets.git/blob - samples/menu/menu.cpp
fixed wxUSE_MAC not being defined under Unix => -D_REENTRANT was missing resulting...
[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 #include "copy.xpm"
33
34 // ----------------------------------------------------------------------------
35 // classes
36 // ----------------------------------------------------------------------------
37
38 // Define a new application
39 class MyApp: public wxApp
40 {
41 public:
42 bool OnInit();
43 };
44
45 // Define a new frame
46 class MyFrame: public wxFrame
47 {
48 public:
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
82 private:
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
95 class MyEvtHandler : public wxEvtHandler
96 {
97 public:
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
107 private:
108 MyFrame *m_frame;
109
110 DECLARE_EVENT_TABLE()
111 };
112
113 // ----------------------------------------------------------------------------
114 // constants
115 // ----------------------------------------------------------------------------
116
117 enum
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
159 BEGIN_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)
187 END_EVENT_TABLE()
188
189 BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
190 EVT_MENU(-1, MyEvtHandler::OnMenuEvent)
191 END_EVENT_TABLE()
192
193 // ============================================================================
194 // implementation
195 // ============================================================================
196
197 // ----------------------------------------------------------------------------
198 // MyApp
199 // ----------------------------------------------------------------------------
200
201 IMPLEMENT_APP(MyApp)
202
203 // The `main program' equivalent, creating the windows and returning the
204 // main frame
205 bool 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
224 MyFrame::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\tAlt-Q" );
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
305 MyFrame::~MyFrame()
306 {
307 delete m_menu;
308
309 // delete the event handler installed in ctor
310 PopEventHandler(TRUE);
311 }
312
313 wxMenu *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
328 wxMenuItem *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
346 void MyFrame::LogMenuEvent(const wxCommandEvent& event)
347 {
348 int id = event.GetId();
349 if (!GetMenuBar()->FindItem(id))
350 return;
351 wxString msg = wxString::Format("Menu command %d", id);
352 if ( GetMenuBar()->FindItem(id)->IsCheckable() )
353 {
354 msg += wxString::Format(" (the item is currently %schecked)",
355 event.IsChecked() ? "" : "not ");
356 }
357
358 SetStatusText(msg, 1);
359 }
360
361 // ----------------------------------------------------------------------------
362 // menu callbacks
363 // ----------------------------------------------------------------------------
364
365 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
366 {
367 Close(TRUE);
368 }
369
370 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
371 {
372 (void)wxMessageBox("wxWindows toolbar sample",
373 "About wxWindows menu sample",
374 wxICON_INFORMATION);
375 }
376
377 void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event))
378 {
379 wxMenuBar *mbar = GetMenuBar();
380
381 size_t count = mbar->GetMenuCount();
382 if ( count == 2 )
383 {
384 // don't let delete the first 2 menus
385 wxLogError("Can't delete any more menus");
386 }
387 else
388 {
389 delete mbar->Remove(count - 1);
390 }
391 }
392
393 void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event))
394 {
395 wxString title;
396 wxMenu *menu = CreateDummyMenu(&title);
397 GetMenuBar()->Insert(0, menu, title);
398 }
399
400 void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event))
401 {
402 wxString title;
403 wxMenu *menu = CreateDummyMenu(&title);
404 GetMenuBar()->Append(menu, title);
405 }
406
407 void MyFrame::OnToggleMenu(wxCommandEvent& WXUNUSED(event))
408 {
409 wxMenuBar *mbar = GetMenuBar();
410 if ( !m_menu )
411 {
412 // hide the menu
413 m_menu = mbar->Remove(0);
414 }
415 else
416 {
417 // restore it
418 mbar->Insert(0, m_menu, "&File");
419 m_menu = NULL;
420 }
421 }
422
423 void MyFrame::OnEnableMenu(wxCommandEvent& WXUNUSED(event))
424 {
425 wxMenuBar *mbar = GetMenuBar();
426 size_t count = mbar->GetMenuCount();
427
428 static bool s_enabled = TRUE;
429
430 s_enabled = !s_enabled;
431 mbar->EnableTop(count - 1, s_enabled);
432 }
433
434 void MyFrame::OnGetLabelMenu(wxCommandEvent& WXUNUSED(event))
435 {
436 wxMenuBar *mbar = GetMenuBar();
437 size_t count = mbar->GetMenuCount();
438
439 wxLogMessage("The label of the last menu item is '%s'",
440 mbar->GetLabelTop(count - 1).c_str());
441 }
442
443 void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event))
444 {
445 wxMenuBar *mbar = GetMenuBar();
446 size_t count = mbar->GetMenuCount();
447
448 mbar->SetLabelTop(count - 1, "Dummy label &0");
449 }
450
451 void MyFrame::OnDummy(wxCommandEvent& event)
452 {
453 wxString s;
454 s.Printf("Dummy item #%d", event.GetId() - Menu_Dummy_First + 1);
455 wxMessageBox(s, "Menu sample", wxICON_INFORMATION);
456 }
457
458 void MyFrame::OnAppendMenuItem(wxCommandEvent& WXUNUSED(event))
459 {
460 wxMenuBar *menubar = GetMenuBar();
461 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
462
463 menu->AppendSeparator();
464 menu->Append(Menu_Dummy_Third, "Third dummy item\tCtrl-F3",
465 "Checkable item", TRUE);
466 }
467
468 void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event))
469 {
470 wxMenuBar *menubar = GetMenuBar();
471
472 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
473
474 menu->Append(Menu_Dummy_Last, "Dummy sub menu",
475 CreateDummyMenu(NULL), "Dummy sub menu help");
476 }
477
478 void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event))
479 {
480 wxMenuBar *menubar = GetMenuBar();
481 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
482
483 size_t count = menu->GetMenuItemCount();
484 if ( !count )
485 {
486 wxLogWarning("No items to delete!");
487 }
488 else
489 {
490 menu->Destroy(menu->GetMenuItems().Item(count - 1)->GetData());
491 }
492 }
493
494 void MyFrame::OnInsertMenuItem(wxCommandEvent& WXUNUSED(event))
495 {
496 wxMenuBar *menubar = GetMenuBar();
497 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
498
499 menu->Insert(0, wxMenuItem::New(menu, Menu_Dummy_Fourth,
500 "Fourth dummy item\tCtrl-F4"));
501 menu->Insert(1, wxMenuItem::New(menu, wxID_SEPARATOR, ""));
502 }
503
504 void MyFrame::OnEnableMenuItem(wxCommandEvent& WXUNUSED(event))
505 {
506 wxMenuItem *item = GetLastMenuItem();
507
508 if ( item )
509 {
510 item->Enable(!item->IsEnabled());
511 }
512 }
513
514 void MyFrame::OnCheckMenuItem(wxCommandEvent& WXUNUSED(event))
515 {
516 wxMenuItem *item = GetLastMenuItem();
517
518 item->Toggle();
519 }
520
521 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event)
522 {
523 wxLogNull nolog;
524
525 wxMenuItem *item = GetLastMenuItem();
526
527 event.Enable(item && item->IsCheckable());
528 }
529
530 void MyFrame::OnGetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
531 {
532 wxMenuItem *item = GetLastMenuItem();
533
534 if ( item )
535 {
536 wxLogMessage("The label of the last menu item is '%s'",
537 item->GetLabel().c_str());
538 }
539 }
540
541 void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
542 {
543 wxMenuItem *item = GetLastMenuItem();
544
545 if ( item )
546 {
547 item->SetText("Dummy menu item text");
548 }
549 }
550
551 void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event))
552 {
553 wxMenuItem *item = GetLastMenuItem();
554
555 if ( item )
556 {
557 wxString msg;
558 msg << "The item is " << (item->IsEnabled() ? "enabled"
559 : "disabled")
560 << '\n';
561
562 if ( item->IsCheckable() )
563 {
564 msg << "It is checkable and " << (item->IsChecked() ? "" : "un")
565 << "checked\n";
566 }
567
568 #if wxUSE_ACCEL
569 wxAcceleratorEntry *accel = item->GetAccel();
570 if ( accel )
571 {
572 msg << "Its accelerator is ";
573
574 int flags = accel->GetFlags();
575 if ( flags & wxACCEL_ALT )
576 msg << wxT("Alt-");
577 if ( flags & wxACCEL_CTRL )
578 msg << wxT("Ctrl-");
579 if ( flags & wxACCEL_SHIFT )
580 msg << wxT("Shift-");
581
582 int code = accel->GetKeyCode();
583 switch ( code )
584 {
585 case WXK_F1:
586 case WXK_F2:
587 case WXK_F3:
588 case WXK_F4:
589 case WXK_F5:
590 case WXK_F6:
591 case WXK_F7:
592 case WXK_F8:
593 case WXK_F9:
594 case WXK_F10:
595 case WXK_F11:
596 case WXK_F12:
597 msg << wxT('F') << code - WXK_F1 + 1;
598 break;
599
600 // if there are any other keys wxGetAccelFromString() may return,
601 // we should process them here
602
603 default:
604 if ( wxIsalnum(code) )
605 {
606 msg << (wxChar)code;
607
608 break;
609 }
610
611 wxFAIL_MSG( wxT("unknown keyboard accel") );
612 }
613
614 delete accel;
615 }
616 else
617 {
618 msg << "It doesn't have an accelerator";
619 }
620 #endif // wxUSE_ACCEL
621
622 wxLogMessage(msg);
623 }
624 }
625
626 void MyFrame::OnRightDown(wxMouseEvent &event )
627 {
628 wxMenu menu("Test popup");
629
630 menu.Append(Menu_Help_About, "&About");
631 menu.Append(Menu_Popup_Submenu, "Submenu", CreateDummyMenu(NULL));
632 menu.Append(Menu_Popup_ToBeDeleted, "To be deleted");
633 menu.Append(Menu_Popup_ToBeChecked, "To be checked", "", TRUE);
634 menu.Append(Menu_Popup_ToBeGreyed, "To be greyed");
635 menu.AppendSeparator();
636 menu.Append(Menu_File_Quit, "E&xit");
637
638 menu.Delete(Menu_Popup_ToBeDeleted);
639 menu.Check(Menu_Popup_ToBeChecked, TRUE);
640 menu.Enable(Menu_Popup_ToBeGreyed, FALSE);
641
642 PopupMenu( &menu, event.GetX(), event.GetY() );
643
644 // test for destroying items in popup menus
645 #if 0
646 menu.Destroy(Menu_Popup_Submenu);
647
648 PopupMenu( &menu, event.GetX(), event.GetY() );
649 #endif // 0
650 }