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