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