]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/menu/menu.cpp
Compilation fixes.
[wxWidgets.git] / samples / menu / menu.cpp
... / ...
CommitLineData
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#include "copy.xpm"
43
44// ----------------------------------------------------------------------------
45// classes
46// ----------------------------------------------------------------------------
47
48// Define a new application
49class MyApp: public wxApp
50{
51public:
52 bool OnInit();
53};
54
55// Define a new frame
56class MyFrame: public wxFrame
57{
58public:
59 MyFrame();
60
61 virtual ~MyFrame();
62
63 void LogMenuEvent(const wxCommandEvent& event);
64
65protected:
66 void OnQuit(wxCommandEvent& event);
67 void OnClearLog(wxCommandEvent& event);
68
69 void OnAbout(wxCommandEvent& event);
70
71 void OnDummy(wxCommandEvent& event);
72
73 void OnAppendMenuItem(wxCommandEvent& event);
74 void OnAppendSubMenu(wxCommandEvent& event);
75 void OnDeleteMenuItem(wxCommandEvent& event);
76 void OnInsertMenuItem(wxCommandEvent& event);
77 void OnCheckMenuItem(wxCommandEvent& event);
78 void OnEnableMenuItem(wxCommandEvent& event);
79 void OnGetLabelMenuItem(wxCommandEvent& event);
80 void OnSetLabelMenuItem(wxCommandEvent& event);
81 void OnGetMenuItemInfo(wxCommandEvent& event);
82 void OnFindMenuItem(wxCommandEvent& event);
83
84 void OnAppendMenu(wxCommandEvent& event);
85 void OnInsertMenu(wxCommandEvent& event);
86 void OnDeleteMenu(wxCommandEvent& event);
87 void OnToggleMenu(wxCommandEvent& event);
88 void OnEnableMenu(wxCommandEvent& event);
89 void OnGetLabelMenu(wxCommandEvent& event);
90 void OnSetLabelMenu(wxCommandEvent& event);
91 void OnFindMenu(wxCommandEvent& event);
92
93 void OnTestNormal(wxCommandEvent& event);
94 void OnTestCheck(wxCommandEvent& event);
95 void OnTestRadio(wxCommandEvent& event);
96
97 void OnUpdateSubMenuNormal(wxUpdateUIEvent& event);
98 void OnUpdateSubMenuCheck(wxUpdateUIEvent& event);
99 void OnUpdateSubMenuRadio(wxUpdateUIEvent& event);
100
101#if defined( __WXMSW__ ) || defined( __WXMAC__ )
102 void OnContextMenu(wxContextMenuEvent& event)
103 { ShowContextMenu(ScreenToClient(event.GetPosition())); }
104#else
105 void OnRightUp(wxMouseEvent& event)
106 { ShowContextMenu(event.GetPosition()); }
107#endif
108
109 void OnMenuOpen(wxMenuEvent& event)
110 { LogMenuOpenOrClose(event, _T("opened")); event.Skip(); }
111 void OnMenuClose(wxMenuEvent& event)
112 { LogMenuOpenOrClose(event, _T("closed")); event.Skip(); }
113
114 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event);
115
116 void OnSize(wxSizeEvent& event);
117
118private:
119 void LogMenuOpenOrClose(const wxMenuEvent& event, const wxChar *what);
120 void ShowContextMenu(const wxPoint& pos);
121
122 wxMenu *CreateDummyMenu(wxString *title);
123
124 wxMenuItem *GetLastMenuItem() const;
125
126 // the menu previously detached from the menubar (may be NULL)
127 wxMenu *m_menu;
128
129 // the count of dummy menus already created
130 size_t m_countDummy;
131
132 // the control used for logging
133 wxTextCtrl *m_textctrl;
134
135 // the previous log target
136 wxLog *m_logOld;
137
138 DECLARE_EVENT_TABLE()
139};
140
141// A small helper class which intercepts all menu events and logs them
142class MyEvtHandler : public wxEvtHandler
143{
144public:
145 MyEvtHandler(MyFrame *frame) { m_frame = frame; }
146
147 void OnMenuEvent(wxCommandEvent& event)
148 {
149 m_frame->LogMenuEvent(event);
150
151 event.Skip();
152 }
153
154private:
155 MyFrame *m_frame;
156
157 DECLARE_EVENT_TABLE()
158};
159
160// ----------------------------------------------------------------------------
161// constants
162// ----------------------------------------------------------------------------
163
164enum
165{
166 Menu_File_Quit = 100,
167 Menu_File_ClearLog,
168
169 Menu_MenuBar_Toggle = 200,
170 Menu_MenuBar_Append,
171 Menu_MenuBar_Insert,
172 Menu_MenuBar_Delete,
173 Menu_MenuBar_Enable,
174 Menu_MenuBar_GetLabel,
175 Menu_MenuBar_SetLabel,
176 Menu_MenuBar_FindMenu,
177
178 Menu_Menu_Append = 300,
179 Menu_Menu_AppendSub,
180 Menu_Menu_Insert,
181 Menu_Menu_Delete,
182 Menu_Menu_Enable,
183 Menu_Menu_Check,
184 Menu_Menu_GetLabel,
185 Menu_Menu_SetLabel,
186 Menu_Menu_GetInfo,
187 Menu_Menu_FindItem,
188
189 Menu_Test_Normal = 400,
190 Menu_Test_Check,
191 Menu_Test_Radio1,
192 Menu_Test_Radio2,
193 Menu_Test_Radio3,
194
195 Menu_SubMenu = 450,
196 Menu_SubMenu_Normal,
197 Menu_SubMenu_Check,
198 Menu_SubMenu_Radio1,
199 Menu_SubMenu_Radio2,
200 Menu_SubMenu_Radio3,
201
202 Menu_Dummy_First = 500,
203 Menu_Dummy_Second,
204 Menu_Dummy_Third,
205 Menu_Dummy_Fourth,
206 Menu_Dummy_Last,
207
208 Menu_Help_About = 1000,
209
210 Menu_Popup_ToBeDeleted = 2000,
211 Menu_Popup_ToBeGreyed,
212 Menu_Popup_ToBeChecked,
213 Menu_Popup_Submenu,
214
215 Menu_Max
216};
217
218// ----------------------------------------------------------------------------
219// event tables
220// ----------------------------------------------------------------------------
221
222BEGIN_EVENT_TABLE(MyFrame, wxFrame)
223 EVT_MENU(Menu_File_Quit, MyFrame::OnQuit)
224 EVT_MENU(Menu_File_ClearLog, MyFrame::OnClearLog)
225
226 EVT_MENU(Menu_Help_About, MyFrame::OnAbout)
227
228 EVT_MENU(Menu_MenuBar_Toggle, MyFrame::OnToggleMenu)
229 EVT_MENU(Menu_MenuBar_Append, MyFrame::OnAppendMenu)
230 EVT_MENU(Menu_MenuBar_Insert, MyFrame::OnInsertMenu)
231 EVT_MENU(Menu_MenuBar_Delete, MyFrame::OnDeleteMenu)
232 EVT_MENU(Menu_MenuBar_Enable, MyFrame::OnEnableMenu)
233 EVT_MENU(Menu_MenuBar_GetLabel, MyFrame::OnGetLabelMenu)
234 EVT_MENU(Menu_MenuBar_SetLabel, MyFrame::OnSetLabelMenu)
235 EVT_MENU(Menu_MenuBar_FindMenu, MyFrame::OnFindMenu)
236
237 EVT_MENU(Menu_Menu_Append, MyFrame::OnAppendMenuItem)
238 EVT_MENU(Menu_Menu_AppendSub, MyFrame::OnAppendSubMenu)
239 EVT_MENU(Menu_Menu_Insert, MyFrame::OnInsertMenuItem)
240 EVT_MENU(Menu_Menu_Delete, MyFrame::OnDeleteMenuItem)
241 EVT_MENU(Menu_Menu_Enable, MyFrame::OnEnableMenuItem)
242 EVT_MENU(Menu_Menu_Check, MyFrame::OnCheckMenuItem)
243 EVT_MENU(Menu_Menu_GetLabel, MyFrame::OnGetLabelMenuItem)
244 EVT_MENU(Menu_Menu_SetLabel, MyFrame::OnSetLabelMenuItem)
245 EVT_MENU(Menu_Menu_GetInfo, MyFrame::OnGetMenuItemInfo)
246 EVT_MENU(Menu_Menu_FindItem, MyFrame::OnFindMenuItem)
247
248 EVT_MENU(Menu_Test_Normal, MyFrame::OnTestNormal)
249 EVT_MENU(Menu_Test_Check, MyFrame::OnTestCheck)
250 EVT_MENU(Menu_Test_Radio1, MyFrame::OnTestRadio)
251 EVT_MENU(Menu_Test_Radio2, MyFrame::OnTestRadio)
252 EVT_MENU(Menu_Test_Radio3, MyFrame::OnTestRadio)
253
254 EVT_UPDATE_UI(Menu_SubMenu_Normal, MyFrame::OnUpdateSubMenuNormal)
255 EVT_UPDATE_UI(Menu_SubMenu_Check, MyFrame::OnUpdateSubMenuCheck)
256 EVT_UPDATE_UI(Menu_SubMenu_Radio1, MyFrame::OnUpdateSubMenuRadio)
257 EVT_UPDATE_UI(Menu_SubMenu_Radio2, MyFrame::OnUpdateSubMenuRadio)
258 EVT_UPDATE_UI(Menu_SubMenu_Radio3, MyFrame::OnUpdateSubMenuRadio)
259
260 EVT_MENU_RANGE(Menu_Dummy_First, Menu_Dummy_Last, MyFrame::OnDummy)
261
262 EVT_UPDATE_UI(Menu_Menu_Check, MyFrame::OnUpdateCheckMenuItemUI)
263
264#if defined( __WXMSW__ ) || defined( __WXMAC__ )
265 EVT_CONTEXT_MENU(MyFrame::OnContextMenu)
266#else
267 EVT_RIGHT_UP(MyFrame::OnRightUp)
268#endif
269
270 EVT_MENU_OPEN(MyFrame::OnMenuOpen)
271 EVT_MENU_CLOSE(MyFrame::OnMenuClose)
272
273 EVT_SIZE(MyFrame::OnSize)
274END_EVENT_TABLE()
275
276BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
277 EVT_MENU(-1, MyEvtHandler::OnMenuEvent)
278END_EVENT_TABLE()
279
280// ============================================================================
281// implementation
282// ============================================================================
283
284// ----------------------------------------------------------------------------
285// MyApp
286// ----------------------------------------------------------------------------
287
288IMPLEMENT_APP(MyApp)
289
290// The `main program' equivalent, creating the windows and returning the
291// main frame
292bool MyApp::OnInit()
293{
294 // Create the main frame window
295 MyFrame* frame = new MyFrame;
296
297 frame->Show(true);
298
299#if wxUSE_STATUSBAR
300 frame->SetStatusText(_T("Welcome to wxWindows menu sample"));
301#endif // wxUSE_STATUSBAR
302
303 SetTopWindow(frame);
304
305 return true;
306}
307
308// ----------------------------------------------------------------------------
309// MyFrame
310// ----------------------------------------------------------------------------
311
312// Define my frame constructor
313MyFrame::MyFrame()
314 : wxFrame((wxFrame *)NULL, -1, _T("wxWindows menu sample"),
315 wxDefaultPosition, wxSize(400, 250))
316{
317 m_textctrl = NULL;
318 m_menu = NULL;
319 m_countDummy = 0;
320 m_logOld = NULL;
321
322#if wxUSE_STATUSBAR
323 CreateStatusBar();
324#endif // wxUSE_STATUSBAR
325
326 // create the menubar
327 wxMenu *fileMenu = new wxMenu;
328
329 wxMenuItem *item = new wxMenuItem(fileMenu, Menu_File_ClearLog,
330 _T("Clear &log\tCtrl-L"));
331 item->SetBitmap(copy_xpm);
332 fileMenu->Append(item);
333 fileMenu->AppendSeparator();
334 fileMenu->Append(Menu_File_Quit, _T("E&xit\tAlt-X"), _T("Quit menu sample"));
335
336 wxMenu *menubarMenu = new wxMenu;
337 menubarMenu->Append(Menu_MenuBar_Append, _T("&Append menu\tCtrl-A"),
338 _T("Append a menu to the menubar"));
339 menubarMenu->Append(Menu_MenuBar_Insert, _T("&Insert menu\tCtrl-I"),
340 _T("Insert a menu into the menubar"));
341 menubarMenu->Append(Menu_MenuBar_Delete, _T("&Delete menu\tCtrl-D"),
342 _T("Delete the last menu from the menubar"));
343 menubarMenu->Append(Menu_MenuBar_Toggle, _T("&Toggle menu\tCtrl-T"),
344 _T("Toggle the first menu in the menubar"), true);
345 menubarMenu->AppendSeparator();
346 menubarMenu->Append(Menu_MenuBar_Enable, _T("&Enable menu\tCtrl-E"),
347 _T("Enable or disable the last menu"), true);
348 menubarMenu->AppendSeparator();
349 menubarMenu->Append(Menu_MenuBar_GetLabel, _T("&Get menu label\tCtrl-G"),
350 _T("Get the label of the last menu"));
351 menubarMenu->Append(Menu_MenuBar_SetLabel, _T("&Set menu label\tCtrl-S"),
352 _T("Change the label of the last menu"));
353 menubarMenu->AppendSeparator();
354 menubarMenu->Append(Menu_MenuBar_FindMenu, _T("&Find menu from label\tCtrl-F"),
355 _T("Find a menu by searching for its label"));
356
357 wxMenu* subMenu = new wxMenu;
358 subMenu->Append(Menu_SubMenu_Normal, _T("&Normal submenu item"), _T("Disabled submenu item"));
359 subMenu->AppendCheckItem(Menu_SubMenu_Check, _T("&Unchecked submenu item"), _T("Unchecked submenu item"));
360 subMenu->AppendRadioItem(Menu_SubMenu_Radio1, _T("&Radio item 1"), _T("Radio item"));
361 subMenu->AppendRadioItem(Menu_SubMenu_Radio2, _T("&Radio item 2"), _T("Radio item"));
362 subMenu->AppendRadioItem(Menu_SubMenu_Radio3, _T("&Radio item 3"), _T("Radio item"));
363
364 menubarMenu->Append(Menu_SubMenu, _T("Submenu"), subMenu);
365
366 wxMenu *menuMenu = new wxMenu;
367 menuMenu->Append(Menu_Menu_Append, _T("&Append menu item\tAlt-A"),
368 _T("Append a menu item to the last menu"));
369 menuMenu->Append(Menu_Menu_AppendSub, _T("&Append sub menu\tAlt-S"),
370 _T("Append a sub menu to the last menu"));
371 menuMenu->Append(Menu_Menu_Insert, _T("&Insert menu item\tAlt-I"),
372 _T("Insert a menu item in head of the last menu"));
373 menuMenu->Append(Menu_Menu_Delete, _T("&Delete menu item\tAlt-D"),
374 _T("Delete the last menu item from the last menu"));
375 menuMenu->AppendSeparator();
376 menuMenu->Append(Menu_Menu_Enable, _T("&Enable menu item\tAlt-E"),
377 _T("Enable or disable the last menu item"), true);
378 menuMenu->Append(Menu_Menu_Check, _T("&Check menu item\tAlt-C"),
379 _T("Check or uncheck the last menu item"), true);
380 menuMenu->AppendSeparator();
381 menuMenu->Append(Menu_Menu_GetInfo, _T("Get menu item in&fo\tAlt-F"),
382 _T("Show the state of the last menu item"));
383 menuMenu->AppendSeparator();
384 menuMenu->Append(Menu_Menu_FindItem, _T("Find menu item from label"),
385 _T("Find a menu item by searching for its label"));
386
387 wxMenu *testMenu = new wxMenu;
388 testMenu->Append(Menu_Test_Normal, _T("&Normal item"));
389 testMenu->AppendSeparator();
390 testMenu->AppendCheckItem(Menu_Test_Check, _T("&Check item"));
391 testMenu->AppendSeparator();
392 testMenu->AppendRadioItem(Menu_Test_Radio1, _T("Radio item &1"));
393 testMenu->AppendRadioItem(Menu_Test_Radio2, _T("Radio item &2"));
394 testMenu->AppendRadioItem(Menu_Test_Radio3, _T("Radio item &3"));
395
396 wxMenu *helpMenu = new wxMenu;
397 helpMenu->Append(Menu_Help_About, _T("&About\tF1"), _T("About menu sample"));
398
399 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
400
401 menuBar->Append(fileMenu, _T("&File"));
402 menuBar->Append(menubarMenu, _T("Menu&bar"));
403 menuBar->Append(menuMenu, _T("&Menu"));
404 menuBar->Append(testMenu, _T("&Test"));
405 menuBar->Append(helpMenu, _T("&Help"));
406
407 // these items should be initially checked
408 menuBar->Check(Menu_MenuBar_Toggle, true);
409 menuBar->Check(Menu_MenuBar_Enable, true);
410 menuBar->Check(Menu_Menu_Enable, true);
411 menuBar->Check(Menu_Menu_Check, false);
412
413 // associate the menu bar with the frame
414 SetMenuBar(menuBar);
415
416 // intercept all menu events and log them in this custom event handler
417 PushEventHandler(new MyEvtHandler(this));
418
419 // create the log text window
420 m_textctrl = new wxTextCtrl(this, -1, _T(""),
421 wxDefaultPosition, wxDefaultSize,
422 wxTE_MULTILINE);
423 m_textctrl->SetEditable(false);
424
425 wxLog::SetTimestamp(NULL);
426 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl));
427
428 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
429 _T("append/insert/delete items to/from the last menu.\n")
430 _T("The commands from \"Menubar\" menu work with the ")
431 _T("menubar itself.\n\n")
432 _T("Right click the band below to test popup menus.\n"));
433}
434
435MyFrame::~MyFrame()
436{
437 delete m_menu;
438
439 // delete the event handler installed in ctor
440 PopEventHandler(true);
441
442 // restore old logger
443 delete wxLog::SetActiveTarget(m_logOld);
444}
445
446wxMenu *MyFrame::CreateDummyMenu(wxString *title)
447{
448 wxMenu *menu = new wxMenu;
449 menu->Append(Menu_Dummy_First, _T("&First item\tCtrl-F1"));
450 menu->AppendSeparator();
451 menu->Append(Menu_Dummy_Second, _T("&Second item\tCtrl-F2"), _T(""), true);
452
453 if ( title )
454 {
455 title->Printf(_T("Dummy menu &%u"), (unsigned)++m_countDummy);
456 }
457
458 return menu;
459}
460
461wxMenuItem *MyFrame::GetLastMenuItem() const
462{
463 wxMenuBar *menubar = GetMenuBar();
464 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
465
466 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetLast();
467 if ( !node )
468 {
469 wxLogWarning(_T("No last item in the last menu!"));
470
471 return NULL;
472 }
473 else
474 {
475 return node->GetData();
476 }
477}
478
479void MyFrame::LogMenuEvent(const wxCommandEvent& event)
480{
481 int id = event.GetId();
482 if ( !GetMenuBar()->FindItem(id) )
483 return;
484
485 wxString msg = wxString::Format(_T("Menu command %d"), id);
486 if ( GetMenuBar()->FindItem(id)->IsCheckable() )
487 {
488 msg += wxString::Format(_T(" (the item is currently %schecked)"),
489 event.IsChecked() ? _T("") : _T("not "));
490 }
491
492 wxLogMessage(msg);
493}
494
495// ----------------------------------------------------------------------------
496// menu callbacks
497// ----------------------------------------------------------------------------
498
499void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
500{
501 Close(true);
502}
503
504void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
505{
506 m_textctrl->Clear();
507}
508
509void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
510{
511