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