]> git.saurik.com Git - wxWidgets.git/blob - samples/menu/menu.cpp
use wxEvent::ShouldPropagate() instead of its own propgation control mechanism in...
[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/app.h"
29 #include "wx/frame.h"
30 #include "wx/menu.h"
31 #include "wx/msgdlg.h"
32 #include "wx/log.h"
33 #include "wx/textctrl.h"
34 #include "wx/textdlg.h"
35 #endif
36
37 #if !wxUSE_MENUS
38 // nice try...
39 #error "menu sample requires wxUSE_MENUS=1"
40 #endif // wxUSE_MENUS
41
42 #include "copy.xpm"
43
44 // ----------------------------------------------------------------------------
45 // classes
46 // ----------------------------------------------------------------------------
47
48 // Define a new application
49 class MyApp: public wxApp
50 {
51 public:
52 bool OnInit();
53 };
54
55 // Define a new frame
56 class MyFrame: public wxFrame
57 {
58 public:
59 MyFrame();
60
61 virtual ~MyFrame();
62
63 void LogMenuEvent(const wxCommandEvent& event);
64
65 protected:
66 void OnQuit(wxCommandEvent& event);
67 void OnClearLog(wxCommandEvent& event);
68
69 void OnAbout(wxCommandEvent& event);
70
71 void OnDummy(wxCommandEvent& event);
72
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);
82 void OnFindMenuItem(wxCommandEvent& event);
83
84 void OnAppendMenu(wxCommandEvent& event);
85 void OnInsertMenu(wxCommandEvent& event);
86 void OnDeleteMenu(wxCommandEvent& event);
87 void OnToggleMenu(wxCommandEvent& event);
88 void OnEnableMenu(wxCommandEvent& event);
89 void OnGetLabelMenu(wxCommandEvent& event);
90 void OnSetLabelMenu(wxCommandEvent& event);
91 void OnFindMenu(wxCommandEvent& event);
92
93 void OnTestNormal(wxCommandEvent& event);
94 void OnTestCheck(wxCommandEvent& event);
95 void OnTestRadio(wxCommandEvent& event);
96
97 void OnUpdateSubMenuNormal(wxUpdateUIEvent& event);
98 void OnUpdateSubMenuCheck(wxUpdateUIEvent& event);
99 void OnUpdateSubMenuRadio(wxUpdateUIEvent& event);
100
101 #if defined( __WXMSW__ ) || defined( __WXMAC__ )
102 void OnContextMenu(wxContextMenuEvent& event)
103 { ShowContextMenu(ScreenToClient(event.GetPosition())); }
104 #else
105 void OnRightUp(wxMouseEvent& event)
106 { ShowContextMenu(event.GetPosition()); }
107 #endif
108
109 void OnMenuOpen(wxMenuEvent& event)
110 { LogMenuOpenOrClose(event, _T("opened")); }
111 void OnMenuClose(wxMenuEvent& event)
112 { LogMenuOpenOrClose(event, _T("closed")); }
113
114 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event);
115
116 void OnSize(wxSizeEvent& event);
117
118 private:
119 void LogMenuOpenOrClose(const wxMenuEvent& event, const wxChar *what);
120 void ShowContextMenu(const wxPoint& pos);
121
122 wxMenu *CreateDummyMenu(wxString *title);
123
124 wxMenuItem *GetLastMenuItem() const;
125
126 // the menu previously detached from the menubar (may be NULL)
127 wxMenu *m_menu;
128
129 // the count of dummy menus already created
130 size_t m_countDummy;
131
132 // the control used for logging
133 wxTextCtrl *m_textctrl;
134
135 // the previous log target
136 wxLog *m_logOld;
137
138 DECLARE_EVENT_TABLE()
139 };
140
141 // A small helper class which intercepts all menu events and logs them
142 class MyEvtHandler : public wxEvtHandler
143 {
144 public:
145 MyEvtHandler(MyFrame *frame) { m_frame = frame; }
146
147 void OnMenuEvent(wxCommandEvent& event)
148 {
149 m_frame->LogMenuEvent(event);
150
151 event.Skip();
152 }
153
154 private:
155 MyFrame *m_frame;
156
157 DECLARE_EVENT_TABLE()
158 };
159
160 // ----------------------------------------------------------------------------
161 // constants
162 // ----------------------------------------------------------------------------
163
164 enum
165 {
166 Menu_File_Quit = 100,
167 Menu_File_ClearLog,
168
169 Menu_MenuBar_Toggle = 200,
170 Menu_MenuBar_Append,
171 Menu_MenuBar_Insert,
172 Menu_MenuBar_Delete,
173 Menu_MenuBar_Enable,
174 Menu_MenuBar_GetLabel,
175 Menu_MenuBar_SetLabel,
176 Menu_MenuBar_FindMenu,
177
178 Menu_Menu_Append = 300,
179 Menu_Menu_AppendSub,
180 Menu_Menu_Insert,
181 Menu_Menu_Delete,
182 Menu_Menu_Enable,
183 Menu_Menu_Check,
184 Menu_Menu_GetLabel,
185 Menu_Menu_SetLabel,
186 Menu_Menu_GetInfo,
187 Menu_Menu_FindItem,
188
189 Menu_Test_Normal = 400,
190 Menu_Test_Check,
191 Menu_Test_Radio1,
192 Menu_Test_Radio2,
193 Menu_Test_Radio3,
194
195 Menu_SubMenu = 450,
196 Menu_SubMenu_Normal,
197 Menu_SubMenu_Check,
198 Menu_SubMenu_Radio1,
199 Menu_SubMenu_Radio2,
200 Menu_SubMenu_Radio3,
201
202 Menu_Dummy_First = 500,
203 Menu_Dummy_Second,
204 Menu_Dummy_Third,
205 Menu_Dummy_Fourth,
206 Menu_Dummy_Last,
207
208 Menu_Help_About = 1000,
209
210 Menu_Popup_ToBeDeleted = 2000,
211 Menu_Popup_ToBeGreyed,
212 Menu_Popup_ToBeChecked,
213 Menu_Popup_Submenu,
214
215 Menu_Max
216 };
217
218 // ----------------------------------------------------------------------------
219 // event tables
220 // ----------------------------------------------------------------------------
221
222 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
223 EVT_MENU(Menu_File_Quit, MyFrame::OnQuit)
224 EVT_MENU(Menu_File_ClearLog, MyFrame::OnClearLog)
225
226 EVT_MENU(Menu_Help_About, MyFrame::OnAbout)
227
228 EVT_MENU(Menu_MenuBar_Toggle, MyFrame::OnToggleMenu)
229 EVT_MENU(Menu_MenuBar_Append, MyFrame::OnAppendMenu)
230 EVT_MENU(Menu_MenuBar_Insert, MyFrame::OnInsertMenu)
231 EVT_MENU(Menu_MenuBar_Delete, MyFrame::OnDeleteMenu)
232 EVT_MENU(Menu_MenuBar_Enable, MyFrame::OnEnableMenu)
233 EVT_MENU(Menu_MenuBar_GetLabel, MyFrame::OnGetLabelMenu)
234 EVT_MENU(Menu_MenuBar_SetLabel, MyFrame::OnSetLabelMenu)
235 EVT_MENU(Menu_MenuBar_FindMenu, MyFrame::OnFindMenu)
236
237 EVT_MENU(Menu_Menu_Append, MyFrame::OnAppendMenuItem)
238 EVT_MENU(Menu_Menu_AppendSub, MyFrame::OnAppendSubMenu)
239 EVT_MENU(Menu_Menu_Insert, MyFrame::OnInsertMenuItem)
240 EVT_MENU(Menu_Menu_Delete, MyFrame::OnDeleteMenuItem)
241 EVT_MENU(Menu_Menu_Enable, MyFrame::OnEnableMenuItem)
242 EVT_MENU(Menu_Menu_Check, MyFrame::OnCheckMenuItem)
243 EVT_MENU(Menu_Menu_GetLabel, MyFrame::OnGetLabelMenuItem)
244 EVT_MENU(Menu_Menu_SetLabel, MyFrame::OnSetLabelMenuItem)
245 EVT_MENU(Menu_Menu_GetInfo, MyFrame::OnGetMenuItemInfo)
246 EVT_MENU(Menu_Menu_FindItem, MyFrame::OnFindMenuItem)
247
248 EVT_MENU(Menu_Test_Normal, MyFrame::OnTestNormal)
249 EVT_MENU(Menu_Test_Check, MyFrame::OnTestCheck)
250 EVT_MENU(Menu_Test_Radio1, MyFrame::OnTestRadio)
251 EVT_MENU(Menu_Test_Radio2, MyFrame::OnTestRadio)
252 EVT_MENU(Menu_Test_Radio3, MyFrame::OnTestRadio)
253
254 EVT_UPDATE_UI(Menu_SubMenu_Normal, MyFrame::OnUpdateSubMenuNormal)
255 EVT_UPDATE_UI(Menu_SubMenu_Check, MyFrame::OnUpdateSubMenuCheck)
256 EVT_UPDATE_UI(Menu_SubMenu_Radio1, MyFrame::OnUpdateSubMenuRadio)
257 EVT_UPDATE_UI(Menu_SubMenu_Radio2, MyFrame::OnUpdateSubMenuRadio)
258 EVT_UPDATE_UI(Menu_SubMenu_Radio3, MyFrame::OnUpdateSubMenuRadio)
259
260 EVT_MENU_RANGE(Menu_Dummy_First, Menu_Dummy_Last, MyFrame::OnDummy)
261
262 EVT_UPDATE_UI(Menu_Menu_Check, MyFrame::OnUpdateCheckMenuItemUI)
263
264 #if defined( __WXMSW__ ) || defined( __WXMAC__ )
265 EVT_CONTEXT_MENU(MyFrame::OnContextMenu)
266 #else
267 EVT_RIGHT_UP(MyFrame::OnRightUp)
268 #endif
269
270 EVT_MENU_OPEN(MyFrame::OnMenuOpen)
271 EVT_MENU_CLOSE(MyFrame::OnMenuClose)
272
273 EVT_SIZE(MyFrame::OnSize)
274 END_EVENT_TABLE()
275
276 BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
277 EVT_MENU(-1, MyEvtHandler::OnMenuEvent)
278 END_EVENT_TABLE()
279
280 // ============================================================================
281 // implementation
282 // ============================================================================
283
284 // ----------------------------------------------------------------------------
285 // MyApp
286 // ----------------------------------------------------------------------------
287
288 IMPLEMENT_APP(MyApp)
289
290 // The `main program' equivalent, creating the windows and returning the
291 // main frame
292 bool MyApp::OnInit()
293 {
294 // Create the main frame window
295 MyFrame* frame = new MyFrame;
296
297 frame->Show(TRUE);
298
299 #if wxUSE_STATUSBAR
300 frame->SetStatusText(_T("Welcome to wxWindows menu sample"));
301 #endif // wxUSE_STATUSBAR
302
303 SetTopWindow(frame);
304
305 return TRUE;
306 }
307
308 // ----------------------------------------------------------------------------
309 // MyFrame
310 // ----------------------------------------------------------------------------
311
312 // Define my frame constructor
313 MyFrame::MyFrame()
314 : wxFrame((wxFrame *)NULL, -1, _T("wxWindows menu sample"),
315 wxDefaultPosition, wxSize(400, 250))
316 {
317 m_textctrl = NULL;
318 m_menu = NULL;
319 m_countDummy = 0;
320 m_logOld = NULL;
321
322 #if wxUSE_STATUSBAR
323 CreateStatusBar();
324 #endif // wxUSE_STATUSBAR
325
326 // create the menubar
327 wxMenu *fileMenu = new wxMenu;
328
329 wxMenuItem *item = new wxMenuItem(fileMenu, Menu_File_ClearLog,
330 _T("Clear &log\tCtrl-L"));
331 item->SetBitmap(copy_xpm);
332 fileMenu->Append(item);
333 fileMenu->AppendSeparator();
334 fileMenu->Append(Menu_File_Quit, _T("E&xit\tAlt-X"), _T("Quit menu sample"));
335
336 wxMenu *menubarMenu = new wxMenu;
337 menubarMenu->Append(Menu_MenuBar_Append, _T("&Append menu\tCtrl-A"),
338 _T("Append a menu to the menubar"));
339 menubarMenu->Append(Menu_MenuBar_Insert, _T("&Insert menu\tCtrl-I"),
340 _T("Insert a menu into the menubar"));
341 menubarMenu->Append(Menu_MenuBar_Delete, _T("&Delete menu\tCtrl-D"),
342 _T("Delete the last menu from the menubar"));
343 menubarMenu->Append(Menu_MenuBar_Toggle, _T("&Toggle menu\tCtrl-T"),
344 _T("Toggle the first menu in the menubar"), TRUE);
345 menubarMenu->AppendSeparator();
346 menubarMenu->Append(Menu_MenuBar_Enable, _T("&Enable menu\tCtrl-E"),
347 _T("Enable or disable the last menu"), TRUE);
348 menubarMenu->AppendSeparator();
349 menubarMenu->Append(Menu_MenuBar_GetLabel, _T("&Get menu label\tCtrl-G"),
350 _T("Get the label of the last menu"));
351 menubarMenu->Append(Menu_MenuBar_SetLabel, _T("&Set menu label\tCtrl-S"),
352 _T("Change the label of the last menu"));
353 menubarMenu->AppendSeparator();
354 menubarMenu->Append(Menu_MenuBar_FindMenu, _T("&Find menu from label\tCtrl-F"),
355 _T("Find a menu by searching for its label"));
356
357 wxMenu* subMenu = new wxMenu;
358 subMenu->Append(Menu_SubMenu_Normal, _T("&Normal submenu item"), _T("Disabled submenu item"));
359 subMenu->AppendCheckItem(Menu_SubMenu_Check, _T("&Unchecked submenu item"), _T("Unchecked submenu item"));
360 subMenu->AppendRadioItem(Menu_SubMenu_Radio1, _T("&Radio item 1"), _T("Radio item"));
361 subMenu->AppendRadioItem(Menu_SubMenu_Radio2, _T("&Radio item 2"), _T("Radio item"));
362 subMenu->AppendRadioItem(Menu_SubMenu_Radio3, _T("&Radio item 3"), _T("Radio item"));
363
364 menubarMenu->Append(Menu_SubMenu, _T("Submenu"), subMenu);
365
366 wxMenu *menuMenu = new wxMenu;
367 menuMenu->Append(Menu_Menu_Append, _T("&Append menu item\tAlt-A"),
368 _T("Append a menu item to the last menu"));
369 menuMenu->Append(Menu_Menu_AppendSub, _T("&Append sub menu\tAlt-S"),
370 _T("Append a sub menu to the last menu"));
371 menuMenu->Append(Menu_Menu_Insert, _T("&Insert menu item\tAlt-I"),
372 _T("Insert a menu item in head of the last menu"));
373 menuMenu->Append(Menu_Menu_Delete, _T("&Delete menu item\tAlt-D"),
374 _T("Delete the last menu item from the last menu"));
375 menuMenu->AppendSeparator();
376 menuMenu->Append(Menu_Menu_Enable, _T("&Enable menu item\tAlt-E"),
377 _T("Enable or disable the last menu item"), TRUE);
378 menuMenu->Append(Menu_Menu_Check, _T("&Check menu item\tAlt-C"),
379 _T("Check or uncheck the last menu item"), TRUE);
380 menuMenu->AppendSeparator();
381 menuMenu->Append(Menu_Menu_GetInfo, _T("Get menu item in&fo\tAlt-F"),
382 _T("Show the state of the last menu item"));
383 menuMenu->AppendSeparator();
384 menuMenu->Append(Menu_Menu_FindItem, _T("Find menu item from label"),
385 _T("Find a menu item by searching for its label"));
386
387 wxMenu *testMenu = new wxMenu;
388 testMenu->Append(Menu_Test_Normal, _T("&Normal item"));
389 testMenu->AppendSeparator();
390 testMenu->AppendCheckItem(Menu_Test_Check, _T("&Check item"));
391 testMenu->AppendSeparator();
392 testMenu->AppendRadioItem(Menu_Test_Radio1, _T("Radio item &1"));
393 testMenu->AppendRadioItem(Menu_Test_Radio2, _T("Radio item &2"));
394 testMenu->AppendRadioItem(Menu_Test_Radio3, _T("Radio item &3"));
395
396 wxMenu *helpMenu = new wxMenu;
397 helpMenu->Append(Menu_Help_About, _T("&About\tF1"), _T("About menu sample"));
398
399 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
400
401 menuBar->Append(fileMenu, _T("&File"));
402 menuBar->Append(menubarMenu, _T("Menu&bar"));
403 menuBar->Append(menuMenu, _T("&Menu"));
404 menuBar->Append(testMenu, _T("&Test"));
405 menuBar->Append(helpMenu, _T("&Help"));
406
407 // these items should be initially checked
408 menuBar->Check(Menu_MenuBar_Toggle, TRUE);
409 menuBar->Check(Menu_MenuBar_Enable, TRUE);
410 menuBar->Check(Menu_Menu_Enable, TRUE);
411 menuBar->Check(Menu_Menu_Check, FALSE);
412
413 // associate the menu bar with the frame
414 SetMenuBar(menuBar);
415
416 // intercept all menu events and log them in this custom event handler
417 PushEventHandler(new MyEvtHandler(this));
418
419 // create the log text window
420 m_textctrl = new wxTextCtrl(this, -1, _T(""),
421 wxDefaultPosition, wxDefaultSize,
422 wxTE_MULTILINE);
423 m_textctrl->SetEditable(FALSE);
424
425 wxLog::SetTimestamp(NULL);
426 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl));
427
428 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
429 _T("append/insert/delete items to/from the last menu.\n")
430 _T("The commands from \"Menubar\" menu work with the ")
431 _T("menubar itself.\n\n")
432 _T("Right click the band below to test popup menus.\n"));
433 }
434
435 MyFrame::~MyFrame()
436 {
437 delete m_menu;
438
439 // delete the event handler installed in ctor
440 PopEventHandler(TRUE);
441
442 // restore old logger
443 delete wxLog::SetActiveTarget(m_logOld);
444 }
445
446 wxMenu *MyFrame::CreateDummyMenu(wxString *title)
447 {
448 wxMenu *menu = new wxMenu;
449 menu->Append(Menu_Dummy_First, _T("&First item\tCtrl-F1"));
450 menu->AppendSeparator();
451 menu->Append(Menu_Dummy_Second, _T("&Second item\tCtrl-F2"), _T(""), TRUE);
452
453 if ( title )
454 {
455 title->Printf(wxT("Dummy menu &%u"), (unsigned)++m_countDummy);
456 }
457
458 return menu;
459 }
460
461 wxMenuItem *MyFrame::GetLastMenuItem() const
462 {
463 wxMenuBar *menubar = GetMenuBar();
464 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
465
466 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetLast();
467 if ( !node )
468 {
469 wxLogWarning(wxT("No last item in the last menu!"));
470
471 return NULL;
472 }
473 else
474 {
475 return node->GetData();
476 }
477 }
478
479 void MyFrame::LogMenuEvent(const wxCommandEvent& event)
480 {
481 int id = event.GetId();
482 if ( !GetMenuBar()->FindItem(id) )
483 return;
484
485 wxString msg = wxString::Format(wxT("Menu command %d"), id);
486 if ( GetMenuBar()->FindItem(id)->IsCheckable() )
487 {
488 msg += wxString::Format(wxT(" (the item is currently %schecked)"),
489 event.IsChecked() ? "" : "not ");
490 }
491
492 wxLogMessage(msg);
493 }
494
495 // ----------------------------------------------------------------------------
496 // menu callbacks
497 // ----------------------------------------------------------------------------
498
499 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
500 {
501 Close(TRUE);
502 }
503
504 void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
505 {
506 m_textctrl->Clear();
507 }
508
509 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
510 {
511 (void)wxMessageBox(_T("wxWindows menu sample\n© 1999-2001 Vadim Zeitlin"),
512 _T("About wxWindows menu sample"),
513 wxICON_INFORMATION);
514 }
515
516 void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event))
517 {
518 wxMenuBar *mbar = GetMenuBar();
519
520 size_t count = mbar->GetMenuCount();
521 if ( count == 2 )
522 {
523 // don't let delete the first 2 menus
524 wxLogError(wxT("Can't delete any more menus"));
525 }
526 else
527 {
528 delete mbar->Remove(count - 1);
529 }
530 }
531
532 void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event))
533 {
534 wxString title;
535 wxMenu *menu = CreateDummyMenu(&title);
536 GetMenuBar()->Insert(0, menu, title);
537 }
538
539 void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event))
540 {
541 wxString title;
542 wxMenu *menu = CreateDummyMenu(&title);
543 GetMenuBar()->Append(menu, title);
544 }
545
546 void MyFrame::OnToggleMenu(wxCommandEvent& WXUNUSED(event))
547 {
548 wxMenuBar *mbar = GetMenuBar();
549 if ( !m_menu )
550 {
551 // hide the menu
552 m_menu = mbar->Remove(0);
553 }
554 else
555 {
556 // restore it
557 mbar->Insert(0, m_menu, _T("&File"));
558 m_menu = NULL;
559 }
560 }
561
562 void MyFrame::OnEnableMenu(wxCommandEvent& event)
563 {
564 wxMenuBar *mbar = GetMenuBar();
565 size_t count = mbar->GetMenuCount();
566
567 mbar->EnableTop(count - 1, event.IsChecked());
568 }
569
570 void MyFrame::OnGetLabelMenu(wxCommandEvent& WXUNUSED(event))
571 {
572 wxMenuBar *mbar = GetMenuBar();
573 size_t count = mbar->GetMenuCount();
574
575 wxCHECK_RET( count, _T("no last menu?") );
576
577 wxLogMessage(wxT("The label of the last menu item is '%s'"),
578 mbar->GetLabelTop(count - 1).c_str());
579 }
580
581 void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event))
582 {
583 wxMenuBar *mbar = GetMenuBar();
584 size_t count = mbar->GetMenuCount();
585
586 wxCHECK_RET( count, _T("no last menu?") );
587
588 wxString label = wxGetTextFromUser
589 (
590 _T("Enter new label: "),
591 _T("Change last menu text"),
592 mbar->GetLabelTop(count - 1),
593 this
594 );
595
596 if ( !label.empty() )
597 {
598 mbar->SetLabelTop(count - 1, label);
599 }
600 }
601
602 void MyFrame::OnFindMenu(wxCommandEvent& WXUNUSED(event))
603 {
604 wxMenuBar *mbar = GetMenuBar();
605 size_t count = mbar->GetMenuCount();
606
607 wxCHECK_RET( count, _T("no last menu?") );
608
609 wxString label = wxGetTextFromUser
610 (
611 _T("Enter label to search for: "),
612 _T("Find menu"),
613 _T(""),
614 this
615 );
616
617 if ( !label.empty() )
618 {
619 int index = mbar->FindMenu(label);
620
621 if (index == wxNOT_FOUND)
622 {
623 wxLogWarning(wxT("No menu with label '%s'"), label.c_str());
624 }
625 else
626 {
627 wxLogMessage(wxT("Menu %d has label '%s'"), index, label.c_str());
628 }
629 }
630 }
631
632 void MyFrame::OnDummy(wxCommandEvent& event)
633 {
634 wxLogMessage(wxT("Dummy item #%d"), event.GetId() - Menu_Dummy_First + 1);
635 }
636
637 void MyFrame::OnAppendMenuItem(wxCommandEvent& WXUNUSED(event))
638 {
639 wxMenuBar *menubar = GetMenuBar();
640 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
641
642 menu->AppendSeparator();
643 menu->Append(Menu_Dummy_Third, _T("&Third dummy item\tCtrl-F3"),
644 _T("Checkable item"), TRUE);
645 }
646
647 void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event))
648 {
649 wxMenuBar *menubar = GetMenuBar();
650
651 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 2);
652
653 menu->Append(Menu_Dummy_Last, _T("&Dummy sub menu"),
654 CreateDummyMenu(NULL), _T("Dummy sub menu help"));
655 }
656
657 void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event))
658 {
659 wxMenuBar *menubar = GetMenuBar();
660 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
661
662 size_t count = menu->GetMenuItemCount();
663 if ( !count )
664 {
665 wxLogWarning(wxT("No items to delete!"));
666 }
667 else
668 {
669 menu->Destroy(menu->GetMenuItems().Item(count - 1)->GetData());
670 }
671 }
672
673 void MyFrame::OnInsertMenuItem(wxCommandEvent& WXUNUSED(event))
674 {
675 wxMenuBar *menubar = GetMenuBar();
676 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
677
678 menu->Insert(0, wxMenuItem::New(menu, Menu_Dummy_Fourth,
679 _T("Fourth dummy item\tCtrl-F4")));
680 menu->Insert(1, wxMenuItem::New(menu, wxID_SEPARATOR, _T("")));
681 }
682
683 void MyFrame::OnEnableMenuItem(wxCommandEvent& WXUNUSED(event))
684 {
685 wxMenuItem *item = GetLastMenuItem();
686
687 if ( item )
688 {
689 item->Enable(!item->IsEnabled());
690 }
691 }
692
693 void MyFrame::OnCheckMenuItem(wxCommandEvent& WXUNUSED(event))
694 {
695 wxMenuItem *item = GetLastMenuItem();
696
697 item->Toggle();
698 }
699
700 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event)
701 {
702 wxLogNull nolog;
703
704 wxMenuItem *item = GetLastMenuItem();
705
706 event.Enable(item && item->IsCheckable());
707 }
708
709 void MyFrame::OnGetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
710 {
711 wxMenuItem *item = GetLastMenuItem();
712
713 if ( item )
714 {
715 wxLogMessage(wxT("The label of the last menu item is '%s'"),
716 item->GetLabel().c_str());
717 }
718 }
719
720 void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
721 {
722 wxMenuItem *item = GetLastMenuItem();
723
724 if ( item )
725 {
726 wxString label = wxGetTextFromUser
727 (
728 _T("Enter new label: "),
729 _T("Change last menu item text"),
730 item->GetLabel(),
731 this
732 );
733
734 if ( !label.empty() )
735 {
736 item->SetText(label);
737 }
738 }
739 }
740
741 void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event))
742 {
743 wxMenuItem *item = GetLastMenuItem();
744
745 if ( item )
746 {
747 wxString msg;
748 msg << _T("The item is ") << (item->IsEnabled() ? _T("enabled")
749 : _T("disabled"))
750 << '\n';
751
752 if ( item->IsCheckable() )
753 {
754 msg << _T("It is checkable and ") << (item->IsChecked() ? _T("") : _T("un"))
755 << _T("checked\n");
756 }
757
758 #if wxUSE_ACCEL
759 wxAcceleratorEntry *accel = item->GetAccel();
760 if ( accel )
761 {
762 msg << _T("Its accelerator is ");
763
764 int flags = accel->GetFlags();
765 if ( flags & wxACCEL_ALT )
766 msg << wxT("Alt-");
767 if ( flags & wxACCEL_CTRL )
768 msg << wxT("Ctrl-");
769 if ( flags & wxACCEL_SHIFT )
770 msg << wxT("Shift-");
771
772 int code = accel->GetKeyCode();
773 switch ( code )
774 {
775 case WXK_F1:
776 case WXK_F2:
777 case WXK_F3:
778 case WXK_F4:
779 case WXK_F5:
780 case WXK_F6:
781 case WXK_F7:
782 case WXK_F8:
783 case WXK_F9:
784 case WXK_F10:
785 case WXK_F11:
786 case WXK_F12:
787 msg << wxT('F') << code - WXK_F1 + 1;
788 break;
789
790 // if there are any other keys wxGetAccelFromString() may return,
791 // we should process them here
792
793 default:
794 if ( wxIsalnum(code) )
795 {
796 msg << (wxChar)code;
797
798 break;
799 }
800
801 wxFAIL_MSG( wxT("unknown keyboard accel") );
802 }
803
804 delete accel;
805 }
806 else
807 {
808 msg << _T("It doesn't have an accelerator");
809 }
810 #endif // wxUSE_ACCEL
811
812 wxLogMessage(msg);
813 }
814 }
815
816 void MyFrame::OnFindMenuItem(wxCommandEvent& WXUNUSED(event))
817 {
818 wxMenuBar *mbar = GetMenuBar();
819 size_t count = mbar->GetMenuCount();
820
821 wxCHECK_RET( count, _T("no last menu?") );
822
823 wxString label = wxGetTextFromUser
824 (
825 _T("Enter label to search for: "),
826 _T("Find menu item"),
827 _T(""),
828 this
829 );
830
831 if ( !label.empty() )
832 {
833 size_t menuindex = 0;
834 int index = wxNOT_FOUND;
835
836 for (menuindex = 0; (menuindex < count) && (index == wxNOT_FOUND); ++menuindex)
837 {
838 index = mbar->FindMenuItem(mbar->GetMenu(menuindex)->GetTitle(), label);
839 }
840 if (index == wxNOT_FOUND)
841 {
842 wxLogWarning(wxT("No menu item with label '%s'"), label.c_str());
843 }
844 else
845 {
846 wxLogMessage(wxT("Menu item %d in menu %lu has label '%s'"),
847 index, (unsigned long)menuindex, label.c_str());
848 }
849 }
850 }
851
852 void MyFrame::ShowContextMenu(const wxPoint& pos)
853 {
854 wxMenu menu(_T("Test popup"));
855
856 menu.Append(Menu_Help_About, _T("&About"));
857 menu.Append(Menu_Popup_Submenu, _T("&Submenu"), CreateDummyMenu(NULL));
858 menu.Append(Menu_Popup_ToBeDeleted, _T("To be &deleted"));
859 menu.Append(Menu_Popup_ToBeChecked, _T("To be &checked"), _T(""), TRUE);
860 menu.Append(Menu_Popup_ToBeGreyed, _T("To be &greyed"),
861 _T("This menu item should be initially greyed out"));
862 menu.AppendSeparator();
863 menu.Append(Menu_File_Quit, _T("E&xit"));
864
865 menu.Delete(Menu_Popup_ToBeDeleted);
866 menu.Check(Menu_Popup_ToBeChecked, TRUE);
867 menu.Enable(Menu_Popup_ToBeGreyed, FALSE);
868
869 PopupMenu(&menu, pos.x, pos.y);
870
871 // test for destroying items in popup menus
872 #if 0 // doesn't work in wxGTK!
873 menu.Destroy(Menu_Popup_Submenu);
874
875 PopupMenu( &menu, event.GetX(), event.GetY() );
876 #endif // 0
877 }
878
879 void MyFrame::OnTestNormal(wxCommandEvent& WXUNUSED(event))
880 {
881 wxLogMessage(_T("Normal item selected"));
882 }
883
884 void MyFrame::OnTestCheck(wxCommandEvent& event)
885 {
886 wxLogMessage(_T("Check item %schecked"),
887 event.IsChecked() ? _T("") : _T("un"));
888 }
889
890 void MyFrame::OnTestRadio(wxCommandEvent& event)
891 {
892 wxLogMessage(_T("Radio item %d selected"),
893 event.GetId() - Menu_Test_Radio1 + 1);
894 }
895
896 void MyFrame::LogMenuOpenOrClose(const wxMenuEvent& event, const wxChar *what)
897 {
898 wxLogStatus(this, _T("A %smenu has been %s."),
899 event.IsPopup() ? _T("popup ") : _T(""), what);
900 }
901
902 void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent& event)
903 {
904 event.Enable(FALSE);
905 }
906
907 void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent& event)
908 {
909 event.Enable(TRUE);
910 event.Check(TRUE);
911 }
912
913 void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent& event)
914 {
915 int which = (event.GetId() - Menu_SubMenu_Radio1 + 1);
916 if (which == 2)
917 event.Check(TRUE);
918 else
919 event.Check(FALSE);
920 }
921
922 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
923 {
924 if ( !m_textctrl )
925 return;
926
927 // leave a band below for popup menu testing
928 wxSize size = GetClientSize();
929 m_textctrl->SetSize(0, 0, size.x, (3*size.y)/4);
930
931 // this is really ugly but we have to do it as we can't just call
932 // event.Skip() because wxFrameBase would make the text control fill the
933 // entire frame then
934 #ifdef __WXUNIVERSAL__
935 PositionMenuBar();
936 #endif // __WXUNIVERSAL__
937 }
938