]> git.saurik.com Git - wxWidgets.git/blob - samples/menu/menu.cpp
Make storing non-trivial data in wxThreadSpecificInfo possible.
[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 // 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".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
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"
33 #include "wx/textdlg.h"
34 #endif
35
36 #if !wxUSE_MENUS
37 // nice try...
38 #error "menu sample requires wxUSE_MENUS=1"
39 #endif // wxUSE_MENUS
40
41 // not all ports have support for EVT_CONTEXT_MENU yet, don't define
42 // USE_CONTEXT_MENU for those which don't
43 #if defined(__WXMOTIF__) || defined(__WXPM__) || defined(__WXX11__)
44 #define USE_CONTEXT_MENU 0
45 #else
46 #define USE_CONTEXT_MENU 1
47 #endif
48
49 // this sample is useful when a new port is developed
50 // and usually a new port has majority of flags turned off
51 #if wxUSE_LOG && wxUSE_TEXTCTRL
52 #define USE_LOG_WINDOW 1
53 #else
54 #define USE_LOG_WINDOW 0
55 #endif
56
57 #include "copy.xpm"
58
59 #ifndef wxHAS_IMAGES_IN_RESOURCES
60 #include "../sample.xpm"
61 #endif
62
63 // ----------------------------------------------------------------------------
64 // classes
65 // ----------------------------------------------------------------------------
66
67 // Define a new application
68 class MyApp: public wxApp
69 {
70 public:
71 bool OnInit();
72 };
73
74 // Define a new frame
75 class MyFrame: public wxFrame
76 {
77 public:
78 MyFrame();
79
80 virtual ~MyFrame();
81
82 void LogMenuEvent(const wxCommandEvent& event);
83
84 protected:
85 void OnQuit(wxCommandEvent& event);
86 #if USE_LOG_WINDOW
87 void OnClearLog(wxCommandEvent& event);
88 void OnClearLogUpdateUI(wxUpdateUIEvent& event);
89 #endif // USE_LOG_WINDOW
90 void OnShowDialog(wxCommandEvent& event);
91
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);
99 void OnDeleteSubMenu(wxCommandEvent& event);
100 void OnInsertMenuItem(wxCommandEvent& event);
101 void OnCheckMenuItem(wxCommandEvent& event);
102 void OnEnableMenuItem(wxCommandEvent& event);
103 void OnGetLabelMenuItem(wxCommandEvent& event);
104 #if wxUSE_TEXTDLG
105 void OnSetLabelMenuItem(wxCommandEvent& event);
106 #endif
107 void OnGetMenuItemInfo(wxCommandEvent& event);
108 #if wxUSE_TEXTDLG
109 void OnFindMenuItem(wxCommandEvent& event);
110 #endif
111
112 void OnAppendMenu(wxCommandEvent& event);
113 void OnInsertMenu(wxCommandEvent& event);
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);
119 #if wxUSE_TEXTDLG
120 void OnFindMenu(wxCommandEvent& event);
121 #endif
122
123 void OnTestNormal(wxCommandEvent& event);
124 void OnTestCheck(wxCommandEvent& event);
125 void OnTestRadio(wxCommandEvent& event);
126
127 void OnUpdateSubMenuNormal(wxUpdateUIEvent& event);
128 void OnUpdateSubMenuCheck(wxUpdateUIEvent& event);
129 void OnUpdateSubMenuRadio(wxUpdateUIEvent& event);
130
131 #if USE_CONTEXT_MENU
132 void OnContextMenu(wxContextMenuEvent& event);
133 #else
134 void OnRightUp(wxMouseEvent& event)
135 { ShowContextMenu(event.GetPosition()); }
136 #endif
137
138 void OnMenuOpen(wxMenuEvent& event)
139 {
140 #if USE_LOG_WINDOW
141 LogMenuOpenCloseOrHighlight(event, wxT("opened")); event.Skip();
142 #endif
143 }
144 void OnMenuClose(wxMenuEvent& event)
145 {
146 #if USE_LOG_WINDOW
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();
154 #endif
155 }
156
157 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event);
158
159 void OnSize(wxSizeEvent& event);
160
161 private:
162 #if USE_LOG_WINDOW
163 void LogMenuOpenCloseOrHighlight(const wxMenuEvent& event, const wxChar *what);
164 #endif
165 void ShowContextMenu(const wxPoint& pos);
166
167 wxMenu *CreateDummyMenu(wxString *title);
168
169 wxMenuItem *GetLastMenuItem() const;
170
171 // the menu previously detached from the menubar (may be NULL)
172 wxMenu *m_menu;
173
174 // the count of dummy menus already created
175 size_t m_countDummy;
176
177 #if USE_LOG_WINDOW
178 // the control used for logging
179 wxTextCtrl *m_textctrl;
180 #endif
181
182 // the previous log target
183 wxLog *m_logOld;
184
185 DECLARE_EVENT_TABLE()
186 };
187
188 class MyDialog : public wxDialog
189 {
190 public:
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
219 private:
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
233 // A small helper class which intercepts all menu events and logs them
234 class MyEvtHandler : public wxEvtHandler
235 {
236 public:
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
246 private:
247 MyFrame *m_frame;
248
249 DECLARE_EVENT_TABLE()
250 };
251
252 // ----------------------------------------------------------------------------
253 // constants
254 // ----------------------------------------------------------------------------
255
256 enum
257 {
258 Menu_File_Quit = wxID_EXIT,
259 #if USE_LOG_WINDOW
260 Menu_File_ClearLog = 100,
261 #endif
262 Menu_File_ShowDialog,
263
264 Menu_MenuBar_Toggle = 200,
265 Menu_MenuBar_Append,
266 Menu_MenuBar_Insert,
267 Menu_MenuBar_Delete,
268 Menu_MenuBar_Enable,
269 Menu_MenuBar_GetLabel,
270 #if wxUSE_TEXTDLG
271 Menu_MenuBar_SetLabel,
272 Menu_MenuBar_FindMenu,
273 #endif
274
275 Menu_Menu_Append = 300,
276 Menu_Menu_AppendSub,
277 Menu_Menu_Insert,
278 Menu_Menu_Delete,
279 Menu_Menu_DeleteSub,
280 Menu_Menu_Enable,
281 Menu_Menu_Check,
282 Menu_Menu_GetLabel,
283 #if wxUSE_TEXTDLG
284 Menu_Menu_SetLabel,
285 #endif
286 Menu_Menu_GetInfo,
287 #if wxUSE_TEXTDLG
288 Menu_Menu_FindItem,
289 #endif
290
291 Menu_Test_Normal = 400,
292 Menu_Test_Check,
293 Menu_Test_Radio1,
294 Menu_Test_Radio2,
295 Menu_Test_Radio3,
296
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
304 Menu_Dummy_First = 500,
305 Menu_Dummy_Second,
306 Menu_Dummy_Third,
307 Menu_Dummy_Fourth,
308 Menu_Dummy_Last,
309
310 Menu_Help_About = wxID_ABOUT,
311
312 Menu_Popup_ToBeDeleted = 2000,
313 Menu_Popup_ToBeGreyed,
314 Menu_Popup_ToBeChecked,
315 Menu_Popup_Submenu,
316
317 Menu_PopupChoice,
318
319 Menu_Max
320 };
321
322 // ----------------------------------------------------------------------------
323 // event tables
324 // ----------------------------------------------------------------------------
325
326 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
327 EVT_MENU(Menu_File_Quit, MyFrame::OnQuit)
328 #if USE_LOG_WINDOW
329 EVT_MENU(Menu_File_ClearLog, MyFrame::OnClearLog)
330 EVT_UPDATE_UI(Menu_File_ClearLog, MyFrame::OnClearLogUpdateUI)
331 #endif
332 EVT_MENU(Menu_File_ShowDialog, MyFrame::OnShowDialog)
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)
338 EVT_MENU(Menu_MenuBar_Insert, MyFrame::OnInsertMenu)
339 EVT_MENU(Menu_MenuBar_Delete, MyFrame::OnDeleteMenu)
340 EVT_MENU(Menu_MenuBar_Enable, MyFrame::OnEnableMenu)
341 EVT_MENU(Menu_MenuBar_GetLabel, MyFrame::OnGetLabelMenu)
342 #if wxUSE_TEXTDLG
343 EVT_MENU(Menu_MenuBar_SetLabel, MyFrame::OnSetLabelMenu)
344 EVT_MENU(Menu_MenuBar_FindMenu, MyFrame::OnFindMenu)
345 #endif
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)
351 EVT_MENU(Menu_Menu_DeleteSub, MyFrame::OnDeleteSubMenu)
352 EVT_MENU(Menu_Menu_Enable, MyFrame::OnEnableMenuItem)
353 EVT_MENU(Menu_Menu_Check, MyFrame::OnCheckMenuItem)
354 EVT_MENU(Menu_Menu_GetLabel, MyFrame::OnGetLabelMenuItem)
355 #if wxUSE_TEXTDLG
356 EVT_MENU(Menu_Menu_SetLabel, MyFrame::OnSetLabelMenuItem)
357 #endif
358 EVT_MENU(Menu_Menu_GetInfo, MyFrame::OnGetMenuItemInfo)
359 #if wxUSE_TEXTDLG
360 EVT_MENU(Menu_Menu_FindItem, MyFrame::OnFindMenuItem)
361 #endif
362
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
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
375 EVT_MENU_RANGE(Menu_Dummy_First, Menu_Dummy_Last, MyFrame::OnDummy)
376
377 EVT_UPDATE_UI(Menu_Menu_Check, MyFrame::OnUpdateCheckMenuItemUI)
378
379 #if USE_CONTEXT_MENU
380 EVT_CONTEXT_MENU(MyFrame::OnContextMenu)
381 #else
382 EVT_RIGHT_UP(MyFrame::OnRightUp)
383 #endif
384
385 EVT_MENU_OPEN(MyFrame::OnMenuOpen)
386 EVT_MENU_CLOSE(MyFrame::OnMenuClose)
387 EVT_MENU_HIGHLIGHT_ALL(MyFrame::OnMenuHighlight)
388
389 EVT_SIZE(MyFrame::OnSize)
390 END_EVENT_TABLE()
391
392 BEGIN_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)
401 END_EVENT_TABLE()
402
403 BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
404 EVT_MENU(wxID_ANY, MyEvtHandler::OnMenuEvent)
405 END_EVENT_TABLE()
406
407 // ============================================================================
408 // implementation
409 // ============================================================================
410
411 // ----------------------------------------------------------------------------
412 // MyApp
413 // ----------------------------------------------------------------------------
414
415 IMPLEMENT_APP(MyApp)
416
417 // The `main program' equivalent, creating the windows and returning the
418 // main frame
419 bool MyApp::OnInit()
420 {
421 if ( !wxApp::OnInit() )
422 return false;
423
424 // Create the main frame window
425 MyFrame* frame = new MyFrame;
426
427 frame->Show(true);
428
429 #if wxUSE_STATUSBAR
430 frame->SetStatusText(wxT("Welcome to wxWidgets menu sample"));
431 #endif // wxUSE_STATUSBAR
432
433 return true;
434 }
435
436 // ----------------------------------------------------------------------------
437 // MyFrame
438 // ----------------------------------------------------------------------------
439
440 // Define my frame constructor
441 MyFrame::MyFrame()
442 : wxFrame((wxFrame *)NULL, wxID_ANY, wxT("wxWidgets menu sample"))
443 {
444 SetIcon(wxICON(sample));
445
446 #if USE_LOG_WINDOW
447 m_textctrl = NULL;
448 #endif
449 m_menu = NULL;
450 m_countDummy = 0;
451 m_logOld = NULL;
452
453 #if wxUSE_STATUSBAR
454 CreateStatusBar();
455 #endif // wxUSE_STATUSBAR
456
457 // create the menubar
458 wxMenu *fileMenu = new wxMenu;
459
460 wxMenu *stockSubMenu = new wxMenu;
461 stockSubMenu->Append(wxID_ADD);
462 stockSubMenu->Append(wxID_APPLY);
463 stockSubMenu->Append(wxID_BACKWARD);
464 stockSubMenu->Append(wxID_BOLD);
465 stockSubMenu->Append(wxID_BOTTOM);
466 stockSubMenu->Append(wxID_CANCEL);
467 stockSubMenu->Append(wxID_CDROM);
468 stockSubMenu->Append(wxID_CLEAR);
469 stockSubMenu->Append(wxID_CLOSE);
470 stockSubMenu->Append(wxID_CONVERT);
471 stockSubMenu->Append(wxID_COPY);
472 stockSubMenu->Append(wxID_CUT);
473 stockSubMenu->Append(wxID_DELETE);
474 stockSubMenu->Append(wxID_DOWN);
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);
480 stockSubMenu->Append(wxID_FORWARD);
481 stockSubMenu->Append(wxID_HARDDISK);
482 stockSubMenu->Append(wxID_HELP);
483 stockSubMenu->Append(wxID_HOME);
484 stockSubMenu->Append(wxID_INDENT);
485 stockSubMenu->Append(wxID_INDEX);
486 stockSubMenu->Append(wxID_INFO);
487 stockSubMenu->Append(wxID_ITALIC);
488 stockSubMenu->Append(wxID_JUMP_TO);
489 stockSubMenu->Append(wxID_JUSTIFY_CENTER);
490 stockSubMenu->Append(wxID_JUSTIFY_FILL);
491 stockSubMenu->Append(wxID_JUSTIFY_LEFT);
492 stockSubMenu->Append(wxID_JUSTIFY_RIGHT);
493 stockSubMenu->Append(wxID_LAST);
494 stockSubMenu->Append(wxID_NETWORK);
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);
501 stockSubMenu->Append(wxID_PREVIEW);
502 stockSubMenu->Append(wxID_PRINT);
503 stockSubMenu->Append(wxID_PROPERTIES);
504 stockSubMenu->Append(wxID_REDO);
505 stockSubMenu->Append(wxID_REFRESH);
506 stockSubMenu->Append(wxID_REMOVE);
507 stockSubMenu->Append(wxID_REPLACE);
508 stockSubMenu->Append(wxID_REVERT_TO_SAVED);
509 stockSubMenu->Append(wxID_SAVE);
510 stockSubMenu->Append(wxID_SAVEAS);
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);
516 stockSubMenu->Append(wxID_STOP);
517 stockSubMenu->Append(wxID_STRIKETHROUGH);
518 stockSubMenu->Append(wxID_TOP);
519 stockSubMenu->Append(wxID_UNDELETE);
520 stockSubMenu->Append(wxID_UNDERLINE);
521 stockSubMenu->Append(wxID_UNDO);
522 stockSubMenu->Append(wxID_UNINDENT);
523 stockSubMenu->Append(wxID_UP);
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);
529 fileMenu->AppendSubMenu(stockSubMenu, wxT("&Standard items demo"));
530
531 #if USE_LOG_WINDOW
532 wxMenuItem *item = new wxMenuItem(fileMenu, Menu_File_ClearLog,
533 wxT("Clear &log\tCtrl-L"));
534 item->SetBitmap(copy_xpm);
535 fileMenu->Append(item);
536 fileMenu->AppendSeparator();
537 #endif // USE_LOG_WINDOW
538
539 fileMenu->Append(Menu_File_ShowDialog, wxT("Show &Dialog\tCtrl-D"),
540 wxT("Show a dialog"));
541 fileMenu->AppendSeparator();
542
543 fileMenu->Append(Menu_File_Quit, wxT("E&xit\tAlt-X"), wxT("Quit menu sample"));
544
545 wxMenu *menubarMenu = new wxMenu;
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);
554 menubarMenu->AppendSeparator();
555 menubarMenu->Append(Menu_MenuBar_Enable, wxT("&Enable menu\tCtrl-E"),
556 wxT("Enable or disable the last menu"), true);
557 menubarMenu->AppendSeparator();
558 menubarMenu->Append(Menu_MenuBar_GetLabel, wxT("&Get menu label\tCtrl-G"),
559 wxT("Get the label of the last menu"));
560 #if wxUSE_TEXTDLG
561 menubarMenu->Append(Menu_MenuBar_SetLabel, wxT("&Set menu label\tCtrl-S"),
562 wxT("Change the label of the last menu"));
563 menubarMenu->AppendSeparator();
564 menubarMenu->Append(Menu_MenuBar_FindMenu, wxT("&Find menu from label\tCtrl-F"),
565 wxT("Find a menu by searching for its label"));
566 #endif
567
568 wxMenu* subMenu = new wxMenu;
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"));
574
575 menubarMenu->Append(Menu_SubMenu, wxT("Submenu"), subMenu);
576
577 wxMenu *menuMenu = new wxMenu;
578 menuMenu->Append(Menu_Menu_Append, wxT("&Append menu item\tAlt-A"),
579 wxT("Append a menu item to the 'Test' menu"));
580 menuMenu->Append(Menu_Menu_AppendSub, wxT("&Append sub menu\tAlt-S"),
581 wxT("Append a sub menu to the 'Test' menu"));
582 menuMenu->Append(Menu_Menu_Insert, wxT("&Insert menu item\tAlt-I"),
583 wxT("Insert a menu item in head of the 'Test' menu"));
584 menuMenu->Append(Menu_Menu_Delete, wxT("&Delete menu item\tAlt-D"),
585 wxT("Delete the last menu item from the 'Test' menu"));
586 menuMenu->Append(Menu_Menu_DeleteSub, wxT("Delete last &submenu\tAlt-K"),
587 wxT("Delete the last submenu from the 'Test' menu"));
588 menuMenu->AppendSeparator();
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);
593 menuMenu->AppendSeparator();
594 menuMenu->Append(Menu_Menu_GetInfo, wxT("Get menu item in&fo\tAlt-F"),
595 wxT("Show the state of the last menu item"));
596 #if wxUSE_TEXTDLG
597 menuMenu->Append(Menu_Menu_SetLabel, wxT("Set menu item label\tAlt-L"),
598 wxT("Set the label of a menu item"));
599 #endif
600 #if wxUSE_TEXTDLG
601 menuMenu->AppendSeparator();
602 menuMenu->Append(Menu_Menu_FindItem, wxT("Find menu item from label"),
603 wxT("Find a menu item by searching for its label"));
604 #endif
605
606 wxMenu *testMenu = new wxMenu;
607 testMenu->Append(Menu_Test_Normal, wxT("&Normal item"));
608 testMenu->AppendSeparator();
609 testMenu->AppendCheckItem(Menu_Test_Check, wxT("&Check item"));
610 testMenu->AppendSeparator();
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"));
614
615 wxMenu *helpMenu = new wxMenu;
616 helpMenu->Append(Menu_Help_About, wxT("&About\tF1"), wxT("About menu sample"));
617
618 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
619
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"));
625
626 // these items should be initially checked
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);
631
632 // associate the menu bar with the frame
633 SetMenuBar(menuBar);
634
635 // intercept all menu events and log them in this custom event handler
636 PushEventHandler(new MyEvtHandler(this));
637
638 #if USE_LOG_WINDOW
639 // create the log text window
640 m_textctrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
641 wxDefaultPosition, wxDefaultSize,
642 wxTE_MULTILINE);
643 m_textctrl->SetEditable(false);
644
645 wxLog::DisableTimestamp();
646 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl));
647
648 wxLogMessage(wxT("Brief explanations: the commands in the \"Menu\" menu ")
649 wxT("append/insert/delete items to/from the \"Test\" menu.\n")
650 wxT("The commands in the \"Menubar\" menu work with the ")
651 wxT("menubar itself.\n\n")
652 wxT("Right click the band below to test popup menus.\n"));
653 #endif
654 #ifdef __POCKETPC__
655 EnableContextMenu();
656 #endif
657 }
658
659 MyFrame::~MyFrame()
660 {
661 delete m_menu;
662
663 // delete the event handler installed in ctor
664 PopEventHandler(true);
665
666 #if USE_LOG_WINDOW
667 // restore old logger
668 delete wxLog::SetActiveTarget(m_logOld);
669 #endif
670 }
671
672 wxMenu *MyFrame::CreateDummyMenu(wxString *title)
673 {
674 wxMenu *menu = new wxMenu;
675 menu->Append(Menu_Dummy_First, wxT("&First item\tCtrl-F1"));
676 menu->AppendSeparator();
677 menu->AppendCheckItem(Menu_Dummy_Second, wxT("&Second item\tCtrl-F2"));
678
679 if ( title )
680 {
681 title->Printf(wxT("Dummy menu &%u"), (unsigned)++m_countDummy);
682 }
683
684 return menu;
685 }
686
687 wxMenuItem *MyFrame::GetLastMenuItem() const
688 {
689 wxMenuBar *menubar = GetMenuBar();
690 wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
691 wxCHECK_MSG( menu, NULL, wxT("no 'Test' menu?") );
692
693 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetLast();
694 if ( !node )
695 {
696 wxLogWarning(wxT("No last item in the 'Test' menu!"));
697
698 return NULL;
699 }
700 else
701 {
702 return node->GetData();
703 }
704 }
705
706 void MyFrame::LogMenuEvent(const wxCommandEvent& event)
707 {
708 int id = event.GetId();
709
710 wxString msg = wxString::Format(wxT("Menu command %d"), id);
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 )
716 {
717 msg += wxString::Format(wxT(" (the item is currently %schecked)"),
718 event.IsChecked() ? wxT("") : wxT("not "));
719 }
720
721 wxLogMessage(msg);
722 }
723
724 // ----------------------------------------------------------------------------
725 // menu callbacks
726 // ----------------------------------------------------------------------------
727
728 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
729 {
730 Close(true);
731 }
732
733 #if USE_LOG_WINDOW
734
735 void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
736 {
737 m_textctrl->Clear();
738 }
739
740 void 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
749
750 void MyFrame::OnShowDialog(wxCommandEvent& WXUNUSED(event))
751 {
752 MyDialog dlg(this);
753 dlg.ShowModal();
754 }
755
756 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
757 {
758 (void)wxMessageBox(wxT("wxWidgets menu sample\n(c) 1999-2001 Vadim Zeitlin"),
759 wxT("About wxWidgets menu sample"),
760 wxOK | wxICON_INFORMATION);
761 }
762
763 void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event))
764 {
765 wxMenuBar *mbar = GetMenuBar();
766
767 size_t count = mbar->GetMenuCount();
768 if ( count == 4 )
769 {
770 // don't let delete the first 4 menus
771 wxLogError(wxT("Can't delete any more menus"));
772 }
773 else
774 {
775 delete mbar->Remove(count - 1);
776 }
777 }
778
779 void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event))
780 {
781 wxString title;
782 wxMenu *menu = CreateDummyMenu(&title);
783 // Insert before the 'Help' menu
784 // Otherwise repeated Deletes will remove the 'Test' menu
785 GetMenuBar()->Insert(4, menu, title);
786 }
787
788 void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event))
789 {
790 wxString title;
791 wxMenu *menu = CreateDummyMenu(&title);
792 GetMenuBar()->Append(menu, title);
793 }
794
795 void 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
806 mbar->Insert(0, m_menu, wxT("&File"));
807 m_menu = NULL;
808 }
809 }
810
811 void MyFrame::OnEnableMenu(wxCommandEvent& event)
812 {
813 wxMenuBar *mbar = GetMenuBar();
814 size_t count = mbar->GetMenuCount();
815
816 mbar->EnableTop(count - 1, event.IsChecked());
817 }
818
819 void MyFrame::OnGetLabelMenu(wxCommandEvent& WXUNUSED(event))
820 {
821 wxMenuBar *mbar = GetMenuBar();
822 size_t count = mbar->GetMenuCount();
823
824 wxCHECK_RET( count, wxT("no last menu?") );
825
826 wxLogMessage(wxT("The label of the last menu item is '%s'"),
827 mbar->GetMenuLabel(count - 1).c_str());
828 }
829
830 #if wxUSE_TEXTDLG
831 void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event))
832 {
833 wxMenuBar *mbar = GetMenuBar();
834 size_t count = mbar->GetMenuCount();
835
836 wxCHECK_RET( count, wxT("no last menu?") );
837
838 wxString label = wxGetTextFromUser
839 (
840 wxT("Enter new label: "),
841 wxT("Change last menu text"),
842 mbar->GetMenuLabel(count - 1),
843 this
844 );
845
846 if ( !label.empty() )
847 {
848 mbar->SetMenuLabel(count - 1, label);
849 }
850 }
851
852 void MyFrame::OnFindMenu(wxCommandEvent& WXUNUSED(event))
853 {
854 wxMenuBar *mbar = GetMenuBar();
855 size_t count = mbar->GetMenuCount();
856
857 wxCHECK_RET( count, wxT("no last menu?") );
858
859 wxString label = wxGetTextFromUser
860 (
861 wxT("Enter label to search for: "),
862 wxT("Find menu"),
863 wxEmptyString,
864 this
865 );
866
867 if ( !label.empty() )
868 {
869 int index = mbar->FindMenu(label);
870
871 if (index == wxNOT_FOUND)
872 {
873 wxLogWarning(wxT("No menu with label '%s'"), label.c_str());
874 }
875 else
876 {
877 wxLogMessage(wxT("Menu %d has label '%s'"), index, label.c_str());
878 }
879 }
880 }
881 #endif
882
883 void MyFrame::OnDummy(wxCommandEvent& event)
884 {
885 wxLogMessage(wxT("Dummy item #%d"), event.GetId() - Menu_Dummy_First + 1);
886 }
887
888 void MyFrame::OnAppendMenuItem(wxCommandEvent& WXUNUSED(event))
889 {
890 wxMenuBar *menubar = GetMenuBar();
891 wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
892 wxCHECK_RET( menu, wxT("no 'Test' menu?") );
893
894 menu->AppendSeparator();
895 menu->Append(Menu_Dummy_Third, wxT("&Third dummy item\tCtrl-F3"),
896 wxT("Checkable item"), true);
897 }
898
899 void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event))
900 {
901 wxMenuBar *menubar = GetMenuBar();
902 wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
903 wxCHECK_RET( menu, wxT("no 'Test' menu?") );
904
905 menu->Append(Menu_Dummy_Last, wxT("&Dummy sub menu"),
906 CreateDummyMenu(NULL), wxT("Dummy sub menu help"));
907 }
908
909 void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event))
910 {
911 wxMenuBar *menubar = GetMenuBar();
912 wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
913 wxCHECK_RET( menu, wxT("no 'Test' menu?") );
914
915 size_t count = menu->GetMenuItemCount();
916 if ( !count )
917 {
918 wxLogWarning(wxT("No items to delete!"));
919 }
920 else
921 {
922 menu->Destroy(menu->GetMenuItems().Item(count - 1)->GetData());
923 }
924 }
925
926 void MyFrame::OnDeleteSubMenu(wxCommandEvent& WXUNUSED(event))
927 {
928 wxMenuBar *menubar = GetMenuBar();
929 wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
930 wxCHECK_RET( menu, wxT("no 'Test' menu?") );
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
945 void MyFrame::OnInsertMenuItem(wxCommandEvent& WXUNUSED(event))
946 {
947 wxMenuBar *menubar = GetMenuBar();
948 wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test"));
949 wxCHECK_RET( menu, wxT("no 'Test' menu?") );
950
951 menu->Insert(0, wxMenuItem::New(menu, Menu_Dummy_Fourth,
952 wxT("Fourth dummy item\tCtrl-F4")));
953 menu->Insert(1, wxMenuItem::New(menu, wxID_SEPARATOR));
954 }
955
956 void MyFrame::OnEnableMenuItem(wxCommandEvent& WXUNUSED(event))
957 {
958 wxMenuItem *item = GetLastMenuItem();
959
960 if ( item )
961 {
962 item->Enable(!item->IsEnabled());
963 }
964 }
965
966 void MyFrame::OnCheckMenuItem(wxCommandEvent& WXUNUSED(event))
967 {
968 wxMenuItem *item = GetLastMenuItem();
969
970 if (item && item->IsCheckable())
971 {
972 item->Toggle();
973 }
974 }
975
976 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event)
977 {
978 wxLogNull nolog;
979
980 wxMenuItem *item = GetLastMenuItem();
981
982 event.Enable(item && item->IsCheckable());
983 }
984
985 void MyFrame::OnGetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
986 {
987 wxMenuItem *item = GetLastMenuItem();
988
989 if ( item )
990 {
991 wxString label = item->GetItemLabel();
992 wxLogMessage(wxT("The label of the last menu item is '%s'"),
993 label.c_str());
994 }
995 }
996
997 #if wxUSE_TEXTDLG
998 void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event))
999 {
1000 wxMenuItem *item = GetLastMenuItem();
1001
1002 if ( item )
1003 {
1004 wxString label = wxGetTextFromUser
1005 (
1006 wxT("Enter new label: "),
1007 wxT("Change last menu item text"),
1008 item->GetItemLabel(),
1009 this
1010 );
1011 label.Replace( wxT("\\t"), wxT("\t") );
1012
1013 if ( !label.empty() )
1014 {
1015 item->SetItemLabel(label);
1016 }
1017 }
1018 }
1019 #endif
1020
1021 void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event))
1022 {
1023 wxMenuItem *item = GetLastMenuItem();
1024
1025 if ( item )
1026 {
1027 wxString msg;
1028 msg << wxT("The item is ") << (item->IsEnabled() ? wxT("enabled")
1029 : wxT("disabled"))
1030 << wxT('\n');
1031
1032 if ( item->IsCheckable() )
1033 {
1034 msg << wxT("It is checkable and ") << (item->IsChecked() ? wxT("") : wxT("un"))
1035 << wxT("checked\n");
1036 }
1037
1038 #if wxUSE_ACCEL
1039 wxAcceleratorEntry *accel = item->GetAccel();
1040 if ( accel )
1041 {
1042 msg << wxT("Its accelerator is ");
1043
1044 int flags = accel->GetFlags();
1045 if ( flags & wxACCEL_ALT )
1046 msg << wxT("Alt-");
1047 if ( flags & wxACCEL_CTRL )
1048 msg << wxT("Ctrl-");
1049 if ( flags & wxACCEL_SHIFT )
1050 msg << wxT("Shift-");
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:
1067 msg << wxT('F') << code - WXK_F1 + 1;
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
1081 wxFAIL_MSG( wxT("unknown keyboard accel") );
1082 }
1083
1084 delete accel;
1085 }
1086 else
1087 {
1088 msg << wxT("It doesn't have an accelerator");
1089 }
1090 #endif // wxUSE_ACCEL
1091
1092 wxLogMessage(msg);
1093 }
1094 }
1095
1096 #if wxUSE_TEXTDLG
1097 void MyFrame::OnFindMenuItem(wxCommandEvent& WXUNUSED(event))
1098 {
1099 wxMenuBar *mbar = GetMenuBar();
1100 size_t count = mbar->GetMenuCount();
1101
1102 wxCHECK_RET( count, wxT("no last menu?") );
1103
1104 wxString label = wxGetTextFromUser
1105 (
1106 wxT("Enter label to search for: "),
1107 wxT("Find menu item"),
1108 wxEmptyString,
1109 this
1110 );
1111
1112 if ( !label.empty() )
1113 {
1114 size_t menuindex;
1115 int index = wxNOT_FOUND;
1116
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 {
1123 wxLogWarning(wxT("No menu item with label '%s'"), label.c_str());
1124 }
1125 else
1126 {
1127 wxLogMessage(wxT("Menu item %d in menu %lu has label '%s'"),
1128 index, (unsigned long)menuindex, label.c_str());
1129 }
1130 }
1131 }
1132 #endif
1133
1134 void MyFrame::ShowContextMenu(const wxPoint& pos)
1135 {
1136 wxMenu menu;
1137
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 {
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"));
1166 menu.AppendSeparator();
1167 menu.Append(Menu_File_Quit, wxT("E&xit"));
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
1176 #if 0 // doesn't work in wxGTK!
1177 menu.Destroy(Menu_Popup_Submenu);
1178
1179 PopupMenu( &menu, event.GetX(), event.GetY() );
1180 #endif // 0
1181 }
1182 }
1183
1184 void MyFrame::OnTestNormal(wxCommandEvent& WXUNUSED(event))
1185 {
1186 wxLogMessage(wxT("Normal item selected"));
1187 }
1188
1189 void MyFrame::OnTestCheck(wxCommandEvent& event)
1190 {
1191 wxLogMessage(wxT("Check item %schecked"),
1192 event.IsChecked() ? wxT("") : wxT("un"));
1193 }
1194
1195 void MyFrame::OnTestRadio(wxCommandEvent& event)
1196 {
1197 wxLogMessage(wxT("Radio item %d selected"),
1198 event.GetId() - Menu_Test_Radio1 + 1);
1199 }
1200
1201 #if USE_LOG_WINDOW
1202 void MyFrame::LogMenuOpenCloseOrHighlight(const wxMenuEvent& event, const wxChar *what)
1203 {
1204 wxString msg;
1205 msg << wxT("A ")
1206 << ( event.IsPopup() ? wxT("popup ") : wxT("") )
1207 << wxT("menu has been ")
1208 << what;
1209
1210 if ( event.GetEventType() == wxEVT_MENU_HIGHLIGHT )
1211 {
1212 msg << wxT(" (id=") << event.GetId() << wxT(")");
1213 }
1214
1215 msg << wxT(".");
1216
1217 wxLogStatus(this, msg.c_str());
1218 }
1219 #endif
1220
1221 void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent& event)
1222 {
1223 event.Enable(false);
1224 }
1225
1226 void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent& event)
1227 {
1228 event.Enable(true);
1229 }
1230
1231 void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent& event)
1232 {
1233 int which = (event.GetId() - Menu_SubMenu_Radio1 + 1);
1234 if (which == 2)
1235 event.Check(true);
1236 else
1237 event.Check(false);
1238 }
1239
1240 #if USE_CONTEXT_MENU
1241 void 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
1256 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
1257 {
1258 #if USE_LOG_WINDOW
1259 if ( !m_textctrl )
1260 return;
1261
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);
1265 #endif
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
1275 // ----------------------------------------------------------------------------
1276 // MyDialog
1277 // ----------------------------------------------------------------------------
1278
1279 MyDialog::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
1298 void 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
1315 void 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
1330 void 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 }