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