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