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