]> git.saurik.com Git - wxWidgets.git/blame - samples/menu/menu.cpp
Allow showing the print preview frame non modally.
[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
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
41f02b9a
FM
62#ifndef __WXMSW__
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"));
67160991 469#if wxUSE_OWNER_DRAWN || defined(__WXGTK__)
e421922f 470 item->SetBitmap(copy_xpm);
1242c2d9 471#endif
e421922f
VZ
472 fileMenu->Append(item);
473 fileMenu->AppendSeparator();
ab73fe8d
VZ
474#endif // USE_LOG_WINDOW
475
9a83f860 476 fileMenu->Append(Menu_File_Quit, wxT("E&xit\tAlt-X"), wxT("Quit menu sample"));
717a57c2
VZ
477
478 wxMenu *menubarMenu = new wxMenu;
9a83f860
VZ
479 menubarMenu->Append(Menu_MenuBar_Append, wxT("&Append menu\tCtrl-A"),
480 wxT("Append a menu to the menubar"));
481 menubarMenu->Append(Menu_MenuBar_Insert, wxT("&Insert menu\tCtrl-I"),
482 wxT("Insert a menu into the menubar"));
483 menubarMenu->Append(Menu_MenuBar_Delete, wxT("&Delete menu\tCtrl-D"),
484 wxT("Delete the last menu from the menubar"));
485 menubarMenu->Append(Menu_MenuBar_Toggle, wxT("&Toggle menu\tCtrl-T"),
486 wxT("Toggle the first menu in the menubar"), true);
717a57c2 487 menubarMenu->AppendSeparator();
9a83f860
VZ
488 menubarMenu->Append(Menu_MenuBar_Enable, wxT("&Enable menu\tCtrl-E"),
489 wxT("Enable or disable the last menu"), true);
717a57c2 490 menubarMenu->AppendSeparator();
9a83f860
VZ
491 menubarMenu->Append(Menu_MenuBar_GetLabel, wxT("&Get menu label\tCtrl-G"),
492 wxT("Get the label of the last menu"));
1242c2d9 493#if wxUSE_TEXTDLG
9a83f860
VZ
494 menubarMenu->Append(Menu_MenuBar_SetLabel, wxT("&Set menu label\tCtrl-S"),
495 wxT("Change the label of the last menu"));
f6d90fb9 496 menubarMenu->AppendSeparator();
9a83f860
VZ
497 menubarMenu->Append(Menu_MenuBar_FindMenu, wxT("&Find menu from label\tCtrl-F"),
498 wxT("Find a menu by searching for its label"));
1242c2d9 499#endif
717a57c2 500
c377f1be 501 wxMenu* subMenu = new wxMenu;
9a83f860
VZ
502 subMenu->Append(Menu_SubMenu_Normal, wxT("&Normal submenu item"), wxT("Disabled submenu item"));
503 subMenu->AppendCheckItem(Menu_SubMenu_Check, wxT("&Check submenu item"), wxT("Check submenu item"));
504 subMenu->AppendRadioItem(Menu_SubMenu_Radio1, wxT("Radio item &1"), wxT("Radio item"));
505 subMenu->AppendRadioItem(Menu_SubMenu_Radio2, wxT("Radio item &2"), wxT("Radio item"));
506 subMenu->AppendRadioItem(Menu_SubMenu_Radio3, wxT("Radio item &3"), wxT("Radio item"));
c377f1be 507
9a83f860 508 menubarMenu->Append(Menu_SubMenu, wxT("Submenu"), subMenu);
c377f1be 509
717a57c2 510 wxMenu *menuMenu = new wxMenu;
9a83f860 511 menuMenu->Append(Menu_Menu_Append, wxT("&Append menu item\tAlt-A"),
553d6728 512 wxT("Append a menu item to the 'Test' menu"));
9a83f860 513 menuMenu->Append(Menu_Menu_AppendSub, wxT("&Append sub menu\tAlt-S"),
553d6728 514 wxT("Append a sub menu to the 'Test' menu"));
9a83f860 515 menuMenu->Append(Menu_Menu_Insert, wxT("&Insert menu item\tAlt-I"),
553d6728 516 wxT("Insert a menu item in head of the 'Test' menu"));
9a83f860 517 menuMenu->Append(Menu_Menu_Delete, wxT("&Delete menu item\tAlt-D"),
553d6728 518 wxT("Delete the last menu item from the 'Test' menu"));
08e78a54 519 menuMenu->Append(Menu_Menu_DeleteSub, wxT("Delete last &submenu\tAlt-K"),
553d6728 520 wxT("Delete the last submenu from the 'Test' menu"));
717a57c2 521 menuMenu->AppendSeparator();
9a83f860
VZ
522 menuMenu->Append(Menu_Menu_Enable, wxT("&Enable menu item\tAlt-E"),
523 wxT("Enable or disable the last menu item"), true);
524 menuMenu->Append(Menu_Menu_Check, wxT("&Check menu item\tAlt-C"),
525 wxT("Check or uncheck the last menu item"), true);
717a57c2 526 menuMenu->AppendSeparator();
9a83f860
VZ
527 menuMenu->Append(Menu_Menu_GetInfo, wxT("Get menu item in&fo\tAlt-F"),
528 wxT("Show the state of the last menu item"));
532d575b 529#if wxUSE_TEXTDLG
9a83f860
VZ
530 menuMenu->Append(Menu_Menu_SetLabel, wxT("Set menu item label\tAlt-L"),
531 wxT("Set the label of a menu item"));
532d575b 532#endif
1242c2d9 533#if wxUSE_TEXTDLG
a80c322c 534 menuMenu->AppendSeparator();
9a83f860
VZ
535 menuMenu->Append(Menu_Menu_FindItem, wxT("Find menu item from label"),
536 wxT("Find a menu item by searching for its label"));
1242c2d9 537#endif
717a57c2 538
d65c269b 539 wxMenu *testMenu = new wxMenu;
9a83f860 540 testMenu->Append(Menu_Test_Normal, wxT("&Normal item"));
d65c269b 541 testMenu->AppendSeparator();
9a83f860 542 testMenu->AppendCheckItem(Menu_Test_Check, wxT("&Check item"));
d65c269b 543 testMenu->AppendSeparator();
9a83f860
VZ
544 testMenu->AppendRadioItem(Menu_Test_Radio1, wxT("Radio item &1"));
545 testMenu->AppendRadioItem(Menu_Test_Radio2, wxT("Radio item &2"));
546 testMenu->AppendRadioItem(Menu_Test_Radio3, wxT("Radio item &3"));
d65c269b 547
717a57c2 548 wxMenu *helpMenu = new wxMenu;
9a83f860 549 helpMenu->Append(Menu_Help_About, wxT("&About\tF1"), wxT("About menu sample"));
717a57c2
VZ
550
551 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
552
9a83f860
VZ
553 menuBar->Append(fileMenu, wxT("&File"));
554 menuBar->Append(menubarMenu, wxT("Menu&bar"));
555 menuBar->Append(menuMenu, wxT("&Menu"));
556 menuBar->Append(testMenu, wxT("&Test"));
557 menuBar->Append(helpMenu, wxT("&Help"));
717a57c2
VZ
558
559 // these items should be initially checked
6ee9b7b5
JS
560 menuBar->Check(Menu_MenuBar_Toggle, true);
561 menuBar->Check(Menu_MenuBar_Enable, true);
562 menuBar->Check(Menu_Menu_Enable, true);
563 menuBar->Check(Menu_Menu_Check, false);
717a57c2
VZ
564
565 // associate the menu bar with the frame
566 SetMenuBar(menuBar);
3ca6a5f0
BP
567
568 // intercept all menu events and log them in this custom event handler
569 PushEventHandler(new MyEvtHandler(this));
e421922f 570
1242c2d9 571#if USE_LOG_WINDOW
e421922f 572 // create the log text window
532d575b 573 m_textctrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
e421922f
VZ
574 wxDefaultPosition, wxDefaultSize,
575 wxTE_MULTILINE);
6ee9b7b5 576 m_textctrl->SetEditable(false);
5c7766de 577
7f2bcd06 578 wxLog::DisableTimestamp();
e421922f
VZ
579 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl));
580
08e78a54 581 wxLogMessage(wxT("Brief explanations: the commands in the \"Menu\" menu ")
553d6728 582 wxT("append/insert/delete items to/from the \"Test\" menu.\n")
08e78a54 583 wxT("The commands in the \"Menubar\" menu work with the ")
9a83f860
VZ
584 wxT("menubar itself.\n\n")
585 wxT("Right click the band below to test popup menus.\n"));
1242c2d9 586#endif
92f07cf8
JS
587#ifdef __POCKETPC__
588 EnableContextMenu();
589#endif
3ca6a5f0
BP
590}
591
592MyFrame::~MyFrame()
593{
594 delete m_menu;
595
596 // delete the event handler installed in ctor
6ee9b7b5 597 PopEventHandler(true);
e421922f 598
1242c2d9 599#if USE_LOG_WINDOW
e421922f
VZ
600 // restore old logger
601 delete wxLog::SetActiveTarget(m_logOld);
1242c2d9 602#endif
717a57c2
VZ
603}
604
f03ec224 605wxMenu *MyFrame::CreateDummyMenu(wxString *title)
717a57c2
VZ
606{
607 wxMenu *menu = new wxMenu;
9a83f860 608 menu->Append(Menu_Dummy_First, wxT("&First item\tCtrl-F1"));
717a57c2 609 menu->AppendSeparator();
9a83f860 610 menu->AppendCheckItem(Menu_Dummy_Second, wxT("&Second item\tCtrl-F2"));
717a57c2 611
f03ec224
VZ
612 if ( title )
613 {
9a83f860 614 title->Printf(wxT("Dummy menu &%u"), (unsigned)++m_countDummy);
f03ec224
VZ
615 }
616
717a57c2
VZ
617 return menu;
618}
619
620wxMenuItem *MyFrame::GetLastMenuItem() const
621{
622 wxMenuBar *menubar = GetMenuBar();
553d6728
VZ
623 wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
624 wxCHECK_MSG( menu, NULL, wxT("no 'Test' menu?") );
717a57c2 625
65c0217e 626 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetLast();
717a57c2
VZ
627 if ( !node )
628 {
553d6728 629 wxLogWarning(wxT("No last item in the 'Test' menu!"));
717a57c2
VZ
630
631 return NULL;
632 }
633 else
634 {
635 return node->GetData();
636 }
637}
638
3ca6a5f0
BP
639void MyFrame::LogMenuEvent(const wxCommandEvent& event)
640{
641 int id = event.GetId();
e421922f 642
9a83f860 643 wxString msg = wxString::Format(wxT("Menu command %d"), id);
030f50ef
VZ
644
645 // catch all checkable menubar items and also the check item from the popup
646 // menu
647 wxMenuItem *item = GetMenuBar()->FindItem(id);
648 if ( (item && item->IsCheckable()) || id == Menu_Popup_ToBeChecked )
3ca6a5f0 649 {
9a83f860
VZ
650 msg += wxString::Format(wxT(" (the item is currently %schecked)"),
651 event.IsChecked() ? wxT("") : wxT("not "));
3ca6a5f0
BP
652 }
653
e421922f 654 wxLogMessage(msg);
3ca6a5f0
BP
655}
656
657// ----------------------------------------------------------------------------
658// menu callbacks
659// ----------------------------------------------------------------------------
660
717a57c2
VZ
661void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
662{
6ee9b7b5 663 Close(true);
717a57c2
VZ
664}
665
1242c2d9 666#if USE_LOG_WINDOW
cfbf301d 667
e421922f
VZ
668void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
669{
670 m_textctrl->Clear();
671}
cfbf301d
VZ
672
673void MyFrame::OnClearLogUpdateUI(wxUpdateUIEvent& event)
674{
675 // if we only enable this item when the log window is empty, we never see
676 // it in the disable state as a message is logged whenever the menu is
677 // opened, so we disable it if there is not "much" text in the window
678 event.Enable( m_textctrl->GetNumberOfLines() > 5 );
679}
680
681#endif // USE_LOG_WINDOW
e421922f 682
717a57c2
VZ
683void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
684{
9a83f860
VZ
685 (void)wxMessageBox(wxT("wxWidgets menu sample\n(c) 1999-2001 Vadim Zeitlin"),
686 wxT("About wxWidgets menu sample"),
e8c0d573 687 wxOK | wxICON_INFORMATION);
717a57c2
VZ
688}
689
690void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event))
691{
692 wxMenuBar *mbar = GetMenuBar();
693
694 size_t count = mbar->GetMenuCount();
553d6728 695 if ( count == 4 )
717a57c2 696 {
553d6728 697 // don't let delete the first 4 menus
9a83f860 698 wxLogError(wxT("Can't delete any more menus"));
717a57c2
VZ
699 }
700 else
701 {
702 delete mbar->Remove(count - 1);
703 }
704}
705
f03ec224 706void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event))
717a57c2 707{
717a57c2 708 wxString title;
f03ec224 709 wxMenu *menu = CreateDummyMenu(&title);
553d6728 710 // Insert before the 'Help' menu
4c51a665 711 // Otherwise repeated Deletes will remove the 'Test' menu
553d6728 712 GetMenuBar()->Insert(4, menu, title);
f03ec224 713}
717a57c2 714
f03ec224
VZ
715void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event))
716{
717 wxString title;
718 wxMenu *menu = CreateDummyMenu(&title);
719 GetMenuBar()->Append(menu, title);
717a57c2
VZ
720}
721
722void MyFrame::OnToggleMenu(wxCommandEvent& WXUNUSED(event))
723{
724 wxMenuBar *mbar = GetMenuBar();
725 if ( !m_menu )
726 {
727 // hide the menu
728 m_menu = mbar->Remove(0);
729 }
730 else
731 {
732 // restore it
9a83f860 733 mbar->Insert(0, m_menu, wxT("&File"));
717a57c2
VZ
734 m_menu = NULL;
735 }
736}
737
e421922f 738void MyFrame::OnEnableMenu(wxCommandEvent& event)
717a57c2
VZ
739{
740 wxMenuBar *mbar = GetMenuBar();
741 size_t count = mbar->GetMenuCount();
742
e421922f 743 mbar->EnableTop(count - 1, event.IsChecked());
717a57c2
VZ
744}
745
746void MyFrame::OnGetLabelMenu(wxCommandEvent& WXUNUSED(event))
747{
748 wxMenuBar *mbar = GetMenuBar();
749 size_t count = mbar->GetMenuCount();
750
9a83f860 751 wxCHECK_RET( count, wxT("no last menu?") );
5c7766de 752
9a83f860 753 wxLogMessage(wxT("The label of the last menu item is '%s'"),
52af3158 754 mbar->GetMenuLabel(count - 1).c_str());
717a57c2
VZ
755}
756
1242c2d9 757#if wxUSE_TEXTDLG
717a57c2
VZ
758void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event))
759{
760 wxMenuBar *mbar = GetMenuBar();
761 size_t count = mbar->GetMenuCount();
762
9a83f860 763 wxCHECK_RET( count, wxT("no last menu?") );
5c7766de
VZ
764
765 wxString label = wxGetTextFromUser
766 (
9a83f860
VZ
767 wxT("Enter new label: "),
768 wxT("Change last menu text"),
52af3158 769 mbar->GetMenuLabel(count - 1),
5c7766de
VZ
770 this
771 );
772
773 if ( !label.empty() )
774 {
52af3158 775 mbar->SetMenuLabel(count - 1, label);
5c7766de 776 }
717a57c2
VZ
777}
778
f6d90fb9
GD
779void MyFrame::OnFindMenu(wxCommandEvent& WXUNUSED(event))
780{
781 wxMenuBar *mbar = GetMenuBar();
782 size_t count = mbar->GetMenuCount();
783
9a83f860 784 wxCHECK_RET( count, wxT("no last menu?") );
f6d90fb9
GD
785
786 wxString label = wxGetTextFromUser
787 (
9a83f860
VZ
788 wxT("Enter label to search for: "),
789 wxT("Find menu"),
532d575b 790 wxEmptyString,
f6d90fb9
GD
791 this
792 );
793
794 if ( !label.empty() )
795 {
796 int index = mbar->FindMenu(label);
797
798 if (index == wxNOT_FOUND)
799 {
9a83f860 800 wxLogWarning(wxT("No menu with label '%s'"), label.c_str());
f6d90fb9
GD
801 }
802 else
803 {
9a83f860 804 wxLogMessage(wxT("Menu %d has label '%s'"), index, label.c_str());
f6d90fb9
GD
805 }
806 }
807}
1242c2d9 808#endif
f6d90fb9 809
717a57c2
VZ
810void MyFrame::OnDummy(wxCommandEvent& event)
811{
9a83f860 812 wxLogMessage(wxT("Dummy item #%d"), event.GetId() - Menu_Dummy_First + 1);
717a57c2
VZ
813}
814
815void MyFrame::OnAppendMenuItem(wxCommandEvent& WXUNUSED(event))
816{
817 wxMenuBar *menubar = GetMenuBar();
553d6728
VZ
818 wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
819 wxCHECK_RET( menu, wxT("no 'Test' menu?") );
717a57c2
VZ
820
821 menu->AppendSeparator();
9a83f860
VZ
822 menu->Append(Menu_Dummy_Third, wxT("&Third dummy item\tCtrl-F3"),
823 wxT("Checkable item"), true);
717a57c2
VZ
824}
825
826void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event))
827{
828 wxMenuBar *menubar = GetMenuBar();
553d6728
VZ
829 wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
830 wxCHECK_RET( menu, wxT("no 'Test' menu?") );
f6bcfd97 831
9a83f860
VZ
832 menu->Append(Menu_Dummy_Last, wxT("&Dummy sub menu"),
833 CreateDummyMenu(NULL), wxT("Dummy sub menu help"));
717a57c2
VZ
834}
835
836void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event))
837{
838 wxMenuBar *menubar = GetMenuBar();
553d6728
VZ
839 wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
840 wxCHECK_RET( menu, wxT("no 'Test' menu?") );
717a57c2
VZ
841
842 size_t count = menu->GetMenuItemCount();
843 if ( !count )
844 {
9a83f860 845 wxLogWarning(wxT("No items to delete!"));
717a57c2
VZ
846 }
847 else
848 {
849 menu->Destroy(menu->GetMenuItems().Item(count - 1)->GetData());
850 }
851}
852
08e78a54
VZ
853void MyFrame::OnDeleteSubMenu(wxCommandEvent& WXUNUSED(event))
854{
855 wxMenuBar *menubar = GetMenuBar();
553d6728
VZ
856 wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
857 wxCHECK_RET( menu, wxT("no 'Test' menu?") );
08e78a54
VZ
858
859 for ( int n = menu->GetMenuItemCount() - 1; n >=0 ; --n )
860 {
861 wxMenuItem* item = menu->FindItemByPosition(n);
862 if (item->IsSubMenu())
863 {
864 menu->Destroy(item);
865 return;
866 }
867 }
868
869 wxLogWarning(wxT("No submenu to delete!"));
870}
871
717a57c2
VZ
872void MyFrame::OnInsertMenuItem(wxCommandEvent& WXUNUSED(event))
873{
874 wxMenuBar *menubar = GetMenuBar();
553d6728
VZ
875 wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
876 wxCHECK_RET( menu, wxT("no 'Test' menu?") );
717a57c2
VZ
877
878 menu->Insert(0, wxMenuItem::New(menu, Menu_Dummy_Fourth,
9a83f860 879 wxT("Fourth dummy item\tCtrl-F4")));
532d575b 880 menu->Insert(1, wxMenuItem::New(menu, wxID_SEPARATOR));
717a57c2
VZ
881}
882
883void MyFrame::OnEnableMenuItem(wxCommandEvent& WXUNUSED(event))
884{
885 wxMenuItem *item = GetLastMenuItem();
886
887 if ( item )
888 {
889 item->Enable(!item->IsEnabled());
890 }
891}
892
893void MyFrame::OnCheckMenuItem(wxCommandEvent& WXUNUSED(event))
894{
895 wxMenuItem *item = GetLastMenuItem();
896
553d6728
VZ
897 if (item && item->IsCheckable())
898 {
717a57c2
VZ
899 item->Toggle();
900}
553d6728 901}
717a57c2
VZ
902
903void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event)
904{
32db328c
VZ
905 wxLogNull nolog;
906
717a57c2
VZ
907 wxMenuItem *item = GetLastMenuItem();
908
909 event.Enable(item && item->IsCheckable());
910}
911
912void MyFrame::OnGetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
913{
914 wxMenuItem *item = GetLastMenuItem();
915
916 if ( item )
917 {
68bc148c 918 wxString label = item->GetItemLabel();
9a83f860 919 wxLogMessage(wxT("The label of the last menu item is '%s'"),
68bc148c 920 label.c_str());
717a57c2
VZ
921 }
922}
923
1242c2d9 924#if wxUSE_TEXTDLG
717a57c2
VZ
925void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
926{
927 wxMenuItem *item = GetLastMenuItem();
928
929 if ( item )
930 {
5c7766de
VZ
931 wxString label = wxGetTextFromUser
932 (
9a83f860
VZ
933 wxT("Enter new label: "),
934 wxT("Change last menu item text"),
52af3158 935 item->GetItemLabel(),
5c7766de
VZ
936 this
937 );
9a83f860 938 label.Replace( wxT("\\t"), wxT("\t") );
5c7766de
VZ
939
940 if ( !label.empty() )
941 {
52af3158 942 item->SetItemLabel(label);
5c7766de 943 }
717a57c2
VZ
944 }
945}
1242c2d9 946#endif
717a57c2 947
a80c322c
VZ
948void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event))
949{
950 wxMenuItem *item = GetLastMenuItem();
951
952 if ( item )
953 {
954 wxString msg;
9a83f860
VZ
955 msg << wxT("The item is ") << (item->IsEnabled() ? wxT("enabled")
956 : wxT("disabled"))
957 << wxT('\n');
f6bcfd97 958
a80c322c
VZ
959 if ( item->IsCheckable() )
960 {
9a83f860
VZ
961 msg << wxT("It is checkable and ") << (item->IsChecked() ? wxT("") : wxT("un"))
962 << wxT("checked\n");
a80c322c
VZ
963 }
964
965#if wxUSE_ACCEL
966 wxAcceleratorEntry *accel = item->GetAccel();
967 if ( accel )
968 {
9a83f860 969 msg << wxT("Its accelerator is ");
a80c322c
VZ
970
971 int flags = accel->GetFlags();
972 if ( flags & wxACCEL_ALT )
9a83f860 973 msg << wxT("Alt-");
a80c322c 974 if ( flags & wxACCEL_CTRL )
9a83f860 975 msg << wxT("Ctrl-");
a80c322c 976 if ( flags & wxACCEL_SHIFT )
9a83f860 977 msg << wxT("Shift-");
a80c322c
VZ
978
979 int code = accel->GetKeyCode();
980 switch ( code )
981 {
982 case WXK_F1:
983 case WXK_F2:
984 case WXK_F3:
985 case WXK_F4:
986 case WXK_F5:
987 case WXK_F6:
988 case WXK_F7:
989 case WXK_F8:
990 case WXK_F9:
991 case WXK_F10:
992 case WXK_F11:
993 case WXK_F12:
9a83f860 994 msg << wxT('F') << code - WXK_F1 + 1;
a80c322c
VZ
995 break;
996
997 // if there are any other keys wxGetAccelFromString() may return,
998 // we should process them here
999
1000 default:
1001 if ( wxIsalnum(code) )
1002 {
1003 msg << (wxChar)code;
1004
1005 break;
1006 }
1007
9a83f860 1008 wxFAIL_MSG( wxT("unknown keyboard accel") );
a80c322c
VZ
1009 }
1010
1011 delete accel;
1012 }
1013 else
1014 {
9a83f860 1015 msg << wxT("It doesn't have an accelerator");
a80c322c
VZ
1016 }
1017#endif // wxUSE_ACCEL
1018
1019 wxLogMessage(msg);
1020 }
1021}
1022
1242c2d9 1023#if wxUSE_TEXTDLG
f6d90fb9
GD
1024void MyFrame::OnFindMenuItem(wxCommandEvent& WXUNUSED(event))
1025{
1026 wxMenuBar *mbar = GetMenuBar();
1027 size_t count = mbar->GetMenuCount();
1028
9a83f860 1029 wxCHECK_RET( count, wxT("no last menu?") );
f6d90fb9
GD
1030
1031 wxString label = wxGetTextFromUser
1032 (
9a83f860
VZ
1033 wxT("Enter label to search for: "),
1034 wxT("Find menu item"),
532d575b 1035 wxEmptyString,
f6d90fb9
GD
1036 this
1037 );
1038
1039 if ( !label.empty() )
1040 {
6ee9b7b5 1041 size_t menuindex;
f6d90fb9 1042 int index = wxNOT_FOUND;
62ad15a5 1043
f6d90fb9
GD
1044 for (menuindex = 0; (menuindex < count) && (index == wxNOT_FOUND); ++menuindex)
1045 {
1046 index = mbar->FindMenuItem(mbar->GetMenu(menuindex)->GetTitle(), label);
1047 }
1048 if (index == wxNOT_FOUND)
1049 {
9a83f860 1050 wxLogWarning(wxT("No menu item with label '%s'"), label.c_str());
f6d90fb9
GD
1051 }
1052 else
1053 {
9a83f860 1054 wxLogMessage(wxT("Menu item %d in menu %lu has label '%s'"),
2e491df9 1055 index, (unsigned long)menuindex, label.c_str());
f6d90fb9
GD
1056 }
1057 }
1058}
1242c2d9 1059#endif
f6d90fb9 1060
ccef86c7 1061void MyFrame::ShowContextMenu(const wxPoint& pos)
717a57c2 1062{
42b16af4 1063 wxMenu menu;
717a57c2 1064
00a77b7c
VZ
1065 if ( wxGetKeyState(WXK_SHIFT) )
1066 {
1067 // when Shift is pressed, demonstrate the use of a simple function
1068 // returning the id of the item selected in the popup menu
1069 menu.SetTitle("Choose one of:");
1070 static const char *choices[] = { "Apple", "Banana", "Cherry" };
1071 for ( size_t n = 0; n < WXSIZEOF(choices); n++ )
1072 menu.Append(Menu_PopupChoice + n, choices[n]);
1073
1074 const int rc = GetPopupMenuSelectionFromUser(menu, pos);
1075 if ( rc == wxID_NONE )
1076 {
1077 wxLogMessage("No selection");
1078 }
1079 else
1080 {
1081 wxLogMessage("You have selected \"%s\"",
1082 choices[rc - Menu_PopupChoice]);
1083 }
1084 }
1085 else // normal case, shift not pressed
1086 {
9a83f860
VZ
1087 menu.Append(Menu_Help_About, wxT("&About"));
1088 menu.Append(Menu_Popup_Submenu, wxT("&Submenu"), CreateDummyMenu(NULL));
1089 menu.Append(Menu_Popup_ToBeDeleted, wxT("To be &deleted"));
1090 menu.AppendCheckItem(Menu_Popup_ToBeChecked, wxT("To be &checked"));
1091 menu.Append(Menu_Popup_ToBeGreyed, wxT("To be &greyed"),
1092 wxT("This menu item should be initially greyed out"));
00a77b7c 1093 menu.AppendSeparator();
9a83f860 1094 menu.Append(Menu_File_Quit, wxT("E&xit"));
00a77b7c
VZ
1095
1096 menu.Delete(Menu_Popup_ToBeDeleted);
1097 menu.Check(Menu_Popup_ToBeChecked, true);
1098 menu.Enable(Menu_Popup_ToBeGreyed, false);
1099
1100 PopupMenu(&menu, pos);
1101
1102 // test for destroying items in popup menus
e421922f 1103#if 0 // doesn't work in wxGTK!
00a77b7c 1104 menu.Destroy(Menu_Popup_Submenu);
f6bcfd97 1105
00a77b7c 1106 PopupMenu( &menu, event.GetX(), event.GetY() );
f6bcfd97 1107#endif // 0
00a77b7c 1108 }
717a57c2 1109}
e421922f 1110
256b8649 1111void MyFrame::OnTestNormal(wxCommandEvent& WXUNUSED(event))
d65c269b 1112{
9a83f860 1113 wxLogMessage(wxT("Normal item selected"));
d65c269b
VZ
1114}
1115
1116void MyFrame::OnTestCheck(wxCommandEvent& event)
1117{
9a83f860
VZ
1118 wxLogMessage(wxT("Check item %schecked"),
1119 event.IsChecked() ? wxT("") : wxT("un"));
d65c269b
VZ
1120}
1121
1122void MyFrame::OnTestRadio(wxCommandEvent& event)
1123{
9a83f860 1124 wxLogMessage(wxT("Radio item %d selected"),
d65c269b
VZ
1125 event.GetId() - Menu_Test_Radio1 + 1);
1126}
1127
1242c2d9 1128#if USE_LOG_WINDOW
ccef86c7
VZ
1129void MyFrame::LogMenuOpenOrClose(const wxMenuEvent& event, const wxChar *what)
1130{
6ee9b7b5 1131 wxString msg;
9a83f860
VZ
1132 msg << wxT("A ")
1133 << ( event.IsPopup() ? wxT("popup ") : wxT("") )
1134 << wxT("menu has been ")
6ee9b7b5 1135 << what
9a83f860 1136 << wxT(".");
62ad15a5 1137
6ee9b7b5 1138 wxLogStatus(this, msg.c_str());
ccef86c7 1139}
1242c2d9 1140#endif
ccef86c7 1141
c377f1be
JS
1142void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent& event)
1143{
6ee9b7b5 1144 event.Enable(false);
c377f1be
JS
1145}
1146
1147void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent& event)
1148{
6ee9b7b5 1149 event.Enable(true);
c377f1be
JS
1150}
1151
1152void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent& event)
1153{
1154 int which = (event.GetId() - Menu_SubMenu_Radio1 + 1);
1155 if (which == 2)
6ee9b7b5 1156 event.Check(true);
c377f1be 1157 else
6ee9b7b5 1158 event.Check(false);
c377f1be
JS
1159}
1160
ac103441
RR
1161#if USE_CONTEXT_MENU
1162void MyFrame::OnContextMenu(wxContextMenuEvent& event)
1163{
1164 wxPoint point = event.GetPosition();
1165 // If from keyboard
1166 if (point.x == -1 && point.y == -1) {
1167 wxSize size = GetSize();
1168 point.x = size.x / 2;
1169 point.y = size.y / 2;
1170 } else {
1171 point = ScreenToClient(point);
1172 }
1173 ShowContextMenu(point);
1174}
1175#endif
1176
256b8649 1177void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
e421922f 1178{
1242c2d9 1179#if USE_LOG_WINDOW
268766dd
VZ
1180 if ( !m_textctrl )
1181 return;
1182
e421922f
VZ
1183 // leave a band below for popup menu testing
1184 wxSize size = GetClientSize();
1185 m_textctrl->SetSize(0, 0, size.x, (3*size.y)/4);
1242c2d9 1186#endif
e421922f
VZ
1187
1188 // this is really ugly but we have to do it as we can't just call
1189 // event.Skip() because wxFrameBase would make the text control fill the
1190 // entire frame then
1191#ifdef __WXUNIVERSAL__
1192 PositionMenuBar();
1193#endif // __WXUNIVERSAL__
1194}
1195