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