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