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