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