]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/menu/menu.cpp
compilation fix after r60017
[wxWidgets.git] / samples / menu / menu.cpp
... / ...
CommitLineData
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
71class MyApp: public wxApp
72{
73public:
74 bool OnInit();
75};
76
77// Define a new frame
78class MyFrame: public wxFrame
79{
80public:
81 MyFrame();
82
83 virtual ~MyFrame();
84
85 void LogMenuEvent(const wxCommandEvent& event);
86
87protected:
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, _T("opened")); event.Skip();
143#endif
144 }
145 void OnMenuClose(wxMenuEvent& event)
146 {
147#if USE_LOG_WINDOW
148 LogMenuOpenOrClose(event, _T("closed")); event.Skip();
149#endif
150 }
151
152 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event);
153
154 void OnSize(wxSizeEvent& event);
155
156private:
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
182class MyEvtHandler : public wxEvtHandler
183{
184public:
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
194private:
195 MyFrame *m_frame;
196
197 DECLARE_EVENT_TABLE()
198};
199
200// ----------------------------------------------------------------------------
201// constants
202// ----------------------------------------------------------------------------
203
204enum
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
272BEGIN_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)
333END_EVENT_TABLE()
334
335BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
336 EVT_MENU(wxID_ANY, MyEvtHandler::OnMenuEvent)
337END_EVENT_TABLE()
338
339// ============================================================================
340// implementation
341// ============================================================================
342
343// ----------------------------------------------------------------------------
344// MyApp
345// ----------------------------------------------------------------------------
346
347IMPLEMENT_APP(MyApp)
348
349// The `main program' equivalent, creating the windows and returning the
350// main frame
351bool 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(_T("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
375MyFrame::MyFrame()
376 : wxFrame((wxFrame *)NULL, wxID_ANY, _T("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, _T("&Standard items demo"));
464
465#if USE_LOG_WINDOW
466 wxMenuItem *item = new wxMenuItem(fileMenu, Menu_File_ClearLog,
467 _T("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, _T("E&xit\tAlt-X"), _T("Quit menu sample"));
476
477 wxMenu *menubarMenu = new wxMenu;
478 menubarMenu->Append(Menu_MenuBar_Append, _T("&Append menu\tCtrl-A"),
479 _T("Append a menu to the menubar"));
480 menubarMenu->Append(Menu_MenuBar_Insert, _T("&Insert menu\tCtrl-I"),
481 _T("Insert a menu into the menubar"));
482 menubarMenu->Append(Menu_MenuBar_Delete, _T("&Delete menu\tCtrl-D"),
483 _T("Delete the last menu from the menubar"));
484 menubarMenu->Append(Menu_MenuBar_Toggle, _T("&Toggle menu\tCtrl-T"),
485 _T("Toggle the first menu in the menubar"), true);
486 menubarMenu->AppendSeparator();
487 menubarMenu->Append(Menu_MenuBar_Enable, _T("&Enable menu\tCtrl-E"),
488 _T("Enable or disable the last menu"), true);
489 menubarMenu->AppendSeparator();
490 menubarMenu->Append(Menu_MenuBar_GetLabel, _T("&Get menu label\tCtrl-G"),
491 _T("Get the label of the last menu"));
492#if wxUSE_TEXTDLG
493 menubarMenu->Append(Menu_MenuBar_SetLabel, _T("&Set menu label\tCtrl-S"),
494 _T("Change the label of the last menu"));
495 menubarMenu->AppendSeparator();
496 menubarMenu->Append(Menu_MenuBar_FindMenu, _T("&Find menu from label\tCtrl-F"),
497 _T("Find a menu by searching for its label"));
498#endif
499
500 wxMenu* subMenu = new wxMenu;
501 subMenu->Append(Menu_SubMenu_Normal, _T("&Normal submenu item"), _T("Disabled submenu item"));
502 subMenu->AppendCheckItem(Menu_SubMenu_Check, _T("&Check submenu item"), _T("Check submenu item"));
503 subMenu->AppendRadioItem(Menu_SubMenu_Radio1, _T("Radio item &1"), _T("Radio item"));
504 subMenu->AppendRadioItem(Menu_SubMenu_Radio2, _T("Radio item &2"), _T("Radio item"));
505 subMenu->AppendRadioItem(Menu_SubMenu_Radio3, _T("Radio item &3"), _T("Radio item"));
506
507 menubarMenu->Append(Menu_SubMenu, _T("Submenu"), subMenu);
508
509 wxMenu *menuMenu = new wxMenu;
510 menuMenu->Append(Menu_Menu_Append, _T("&Append menu item\tAlt-A"),
511 _T("Append a menu item to the last menu"));
512 menuMenu->Append(Menu_Menu_AppendSub, _T("&Append sub menu\tAlt-S"),
513 _T("Append a sub menu to the last menu"));
514 menuMenu->Append(Menu_Menu_Insert, _T("&Insert menu item\tAlt-I"),
515 _T("Insert a menu item in head of the last menu"));
516 menuMenu->Append(Menu_Menu_Delete, _T("&Delete menu item\tAlt-D"),
517 _T("Delete the last menu item from the last menu"));
518 menuMenu->AppendSeparator();
519 menuMenu->Append(Menu_Menu_Enable, _T("&Enable menu item\tAlt-E"),
520 _T("Enable or disable the last menu item"), true);
521 menuMenu->Append(Menu_Menu_Check, _T("&Check menu item\tAlt-C"),
522 _T("Check or uncheck the last menu item"), true);
523 menuMenu->AppendSeparator();
524 menuMenu->Append(Menu_Menu_GetInfo, _T("Get menu item in&fo\tAlt-F"),
525 _T("Show the state of the last menu item"));
526#if wxUSE_TEXTDLG
527 menuMenu->Append(Menu_Menu_SetLabel, _T("Set menu item label\tAlt-L"),
528 _T("Set the label of a menu item"));
529#endif
530#if wxUSE_TEXTDLG
531 menuMenu->AppendSeparator();
532 menuMenu->Append(Menu_Menu_FindItem, _T("Find menu item from label"),
533 _T("Find a menu item by searching for its label"));
534#endif
535
536 wxMenu *testMenu = new wxMenu;
537 testMenu->Append(Menu_Test_Normal, _T("&Normal item"));
538 testMenu->AppendSeparator();
539 testMenu->AppendCheckItem(Menu_Test_Check, _T("&Check item"));
540 testMenu->AppendSeparator();
541 testMenu->AppendRadioItem(Menu_Test_Radio1, _T("Radio item &1"));
542 testMenu->AppendRadioItem(Menu_Test_Radio2, _T("Radio item &2"));
543 testMenu->AppendRadioItem(Menu_Test_Radio3, _T("Radio item &3"));
544
545 wxMenu *helpMenu = new wxMenu;
546 helpMenu->Append(Menu_Help_About, _T("&About\tF1"), _T("About menu sample"));
547
548 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
549
550 menuBar->Append(fileMenu, _T("&File"));
551 menuBar->Append(menubarMenu, _T("Menu&bar"));
552 menuBar->Append(menuMenu, _T("&Menu"));
553 menuBar->Append(testMenu, _T("&Test"));
554 menuBar->Append(helpMenu, _T("&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(_T("Brief explanations: the commands or the \"Menu\" menu ")
579 _T("append/insert/delete items to/from the last menu.\n")
580 _T("The commands from \"Menubar\" menu work with the ")
581 _T("menubar itself.\n\n")
582 _T("Right click the band below to test popup menus.\n"));
583#endif
584#ifdef __POCKETPC__
585 EnableContextMenu();
586#endif
587}
588
589MyFrame::~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
602wxMenu *MyFrame::CreateDummyMenu(wxString *title)
603{
604 wxMenu *menu = new wxMenu;
605 menu->Append(Menu_Dummy_First, _T("&First item\tCtrl-F1"));
606 menu->AppendSeparator();
607 menu->AppendCheckItem(Menu_Dummy_Second, _T("&Second item\tCtrl-F2"));
608
609 if ( title )
610 {
611 title->Printf(_T("Dummy menu &%u"), (unsigned)++m_countDummy);
612 }
613
614 return menu;
615}
616
617wxMenuItem *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(_T("No last item in the last menu!"));
626
627 return NULL;
628 }
629 else
630 {
631 return node->GetData();
632 }
633}
634
635void MyFrame::LogMenuEvent(const wxCommandEvent& event)
636{
637 int id = event.GetId();
638
639 wxString msg = wxString::Format(_T("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(_T(" (the item is currently %schecked)"),
647 event.IsChecked() ? _T("") : _T("not "));
648 }
649
650 wxLogMessage(msg);
651}
652
653// ----------------------------------------------------------------------------
654// menu callbacks
655// ----------------------------------------------------------------------------
656
657void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
658{
659 Close(true);
660}
661
662#if USE_LOG_WINDOW
663
664void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
665{
666 m_textctrl->Clear();
667}
668
669void 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
679void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
680{
681 (void)wxMessageBox(_T("wxWidgets menu sample\n(c) 1999-2001 Vadim Zeitlin"),
682 _T("About wxWidgets menu sample"),
683 wxOK | wxICON_INFORMATION);
684}
685
686void 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(_T("Can't delete any more menus"));
695 }
696 else
697 {
698 delete mbar->Remove(count - 1);
699 }
700}
701
702void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event))
703{
704 wxString title;
705 wxMenu *menu = CreateDummyMenu(&title);
706 GetMenuBar()->Insert(0, menu, title);
707}
708
709void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event))
710{
711 wxString title;
712 wxMenu *menu = CreateDummyMenu(&title);
713 GetMenuBar()->Append(menu, title);
714}
715
716void 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, _T("&File"));
728 m_menu = NULL;
729 }
730}
731
732void 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
740void MyFrame::OnGetLabelMenu(wxCommandEvent& WXUNUSED(event))
741{
742 wxMenuBar *mbar = GetMenuBar();
743 size_t count = mbar->GetMenuCount();
744
745 wxCHECK_RET( count, _T("no last menu?") );
746
747 wxLogMessage(_T("The label of the last menu item is '%s'"),
748 mbar->GetMenuLabel(count - 1).c_str());
749}
750
751#if wxUSE_TEXTDLG
752void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event))
753{
754 wxMenuBar *mbar = GetMenuBar();
755 size_t count = mbar->GetMenuCount();
756
757 wxCHECK_RET( count, _T("no last menu?") );
758
759 wxString label = wxGetTextFromUser
760 (
761 _T("Enter new label: "),
762 _T("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
773void MyFrame::OnFindMenu(wxCommandEvent& WXUNUSED(event))
774{
775 wxMenuBar *mbar = GetMenuBar();
776 size_t count = mbar->GetMenuCount();
777
778 wxCHECK_RET( count, _T("no last menu?") );
779
780 wxString label = wxGetTextFromUser
781 (
782 _T("Enter label to search for: "),
783 _T("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(_T("No menu with label '%s'"), label.c_str());
795 }
796 else
797 {
798 wxLogMessage(_T("Menu %d has label '%s'"), index, label.c_str());
799 }
800 }
801}
802#endif
803
804void MyFrame::OnDummy(wxCommandEvent& event)
805{
806 wxLogMessage(_T("Dummy item #%d"), event.GetId() - Menu_Dummy_First + 1);
807}
808
809void 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, _T("&Third dummy item\tCtrl-F3"),
816 _T("Checkable item"), true);
817}
818
819void 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, _T("&Dummy sub menu"),
826 CreateDummyMenu(NULL), _T("Dummy sub menu help"));
827}
828
829void 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(_T("No items to delete!"));
838 }
839 else
840 {
841 menu->Destroy(menu->GetMenuItems().Item(count - 1)->GetData());
842 }
843}
844
845void 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 _T("Fourth dummy item\tCtrl-F4")));
852 menu->Insert(1, wxMenuItem::New(menu, wxID_SEPARATOR));
853}
854
855void MyFrame::OnEnableMenuItem(wxCommandEvent& WXUNUSED(event))
856{
857 wxMenuItem *item = GetLastMenuItem();
858
859 if ( item )
860 {
861 item->Enable(!item->IsEnabled());
862 }
863}
864
865void MyFrame::OnCheckMenuItem(wxCommandEvent& WXUNUSED(event))
866{
867 wxMenuItem *item = GetLastMenuItem();
868
869 item->Toggle();
870}
871
872void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event)
873{
874 wxLogNull nolog;
875
876 wxMenuItem *item = GetLastMenuItem();
877
878 event.Enable(item && item->IsCheckable());
879}
880
881void MyFrame::OnGetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
882{
883 wxMenuItem *item = GetLastMenuItem();
884
885 if ( item )
886 {
887 wxString label = item->GetItemLabel();
888 wxLogMessage(_T("The label of the last menu item is '%s'"),
889 label.c_str());
890 }
891}
892
893#if wxUSE_TEXTDLG
894void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
895{
896 wxMenuItem *item = GetLastMenuItem();
897
898 if ( item )
899 {
900 wxString label = wxGetTextFromUser
901 (
902 _T("Enter new label: "),
903 _T("Change last menu item text"),
904 item->GetItemLabel(),
905 this
906 );
907 label.Replace( _T("\\t"), _T("\t") );
908
909 if ( !label.empty() )
910 {
911 item->SetItemLabel(label);
912 }
913 }
914}
915#endif
916
917void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event))
918{
919 wxMenuItem *item = GetLastMenuItem();
920
921 if ( item )
922 {
923 wxString msg;
924 msg << _T("The item is ") << (item->IsEnabled() ? _T("enabled")
925 : _T("disabled"))
926 << _T('\n');
927
928 if ( item->IsCheckable() )
929 {
930 msg << _T("It is checkable and ") << (item->IsChecked() ? _T("") : _T("un"))
931 << _T("checked\n");
932 }
933
934#if wxUSE_ACCEL
935 wxAcceleratorEntry *accel = item->GetAccel();
936 if ( accel )
937 {
938 msg << _T("Its accelerator is ");
939
940 int flags = accel->GetFlags();
941 if ( flags & wxACCEL_ALT )
942 msg << _T("Alt-");
943 if ( flags & wxACCEL_CTRL )
944 msg << _T("Ctrl-");
945 if ( flags & wxACCEL_SHIFT )
946 msg << _T("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 << _T('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( _T("unknown keyboard accel") );
978 }
979
980 delete accel;
981 }
982 else
983 {
984 msg << _T("It doesn't have an accelerator");
985 }
986#endif // wxUSE_ACCEL
987
988 wxLogMessage(msg);
989 }
990}
991
992#if wxUSE_TEXTDLG
993void MyFrame::OnFindMenuItem(wxCommandEvent& WXUNUSED(event))
994{
995 wxMenuBar *mbar = GetMenuBar();
996 size_t count = mbar->GetMenuCount();
997
998 wxCHECK_RET( count, _T("no last menu?") );
999
1000 wxString label = wxGetTextFromUser
1001 (
1002 _T("Enter label to search for: "),
1003 _T("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(_T("No menu item with label '%s'"), label.c_str());
1020 }
1021 else
1022 {
1023 wxLogMessage(_T("Menu item %d in menu %lu has label '%s'"),
1024 index, (unsigned long)menuindex, label.c_str());
1025 }
1026 }
1027}
1028#endif
1029
1030void 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, _T("&About"));
1057 menu.Append(Menu_Popup_Submenu, _T("&Submenu"), CreateDummyMenu(NULL));
1058 menu.Append(Menu_Popup_ToBeDeleted, _T("To be &deleted"));
1059 menu.AppendCheckItem(Menu_Popup_ToBeChecked, _T("To be &checked"));
1060 menu.Append(Menu_Popup_ToBeGreyed, _T("To be &greyed"),
1061 _T("This menu item should be initially greyed out"));
1062 menu.AppendSeparator();
1063 menu.Append(Menu_File_Quit, _T("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
1080void MyFrame::OnTestNormal(wxCommandEvent& WXUNUSED(event))
1081{
1082 wxLogMessage(_T("Normal item selected"));
1083}
1084
1085void MyFrame::OnTestCheck(wxCommandEvent& event)
1086{
1087 wxLogMessage(_T("Check item %schecked"),
1088 event.IsChecked() ? _T("") : _T("un"));
1089}
1090
1091void MyFrame::OnTestRadio(wxCommandEvent& event)
1092{
1093 wxLogMessage(_T("Radio item %d selected"),
1094 event.GetId() - Menu_Test_Radio1 + 1);
1095}
1096
1097#if USE_LOG_WINDOW
1098void MyFrame::LogMenuOpenOrClose(const wxMenuEvent& event, const wxChar *what)
1099{
1100 wxString msg;
1101 msg << _T("A ")
1102 << ( event.IsPopup() ? _T("popup ") : _T("") )
1103 << _T("menu has been ")
1104 << what
1105 << _T(".");
1106
1107 wxLogStatus(this, msg.c_str());
1108}
1109#endif
1110
1111void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent& event)
1112{
1113 event.Enable(false);
1114}
1115
1116void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent& event)
1117{
1118 event.Enable(true);
1119}
1120
1121void 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
1131void 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
1146void 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