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