]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/menu/menu.cpp
Updated PyCrust from Patrick O'Brien
[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
83 void OnAppendMenu(wxCommandEvent& event);
84 void OnInsertMenu(wxCommandEvent& event);
85 void OnDeleteMenu(wxCommandEvent& event);
86 void OnToggleMenu(wxCommandEvent& event);
87 void OnEnableMenu(wxCommandEvent& event);
88 void OnGetLabelMenu(wxCommandEvent& event);
89 void OnSetLabelMenu(wxCommandEvent& event);
90
91 void OnRightUp(wxMouseEvent& event);
92
93 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event);
94
95 void OnSize(wxSizeEvent& event);
96
97private:
98 wxMenu *CreateDummyMenu(wxString *title);
99
100 wxMenuItem *GetLastMenuItem() const;
101
102 // the menu previously detached from the menubar (may be NULL)
103 wxMenu *m_menu;
104
105 // the count of dummy menus already created
106 size_t m_countDummy;
107
108 // the control used for logging
109 wxTextCtrl *m_textctrl;
110
111 // the previous log target
112 wxLog *m_logOld;
113
114 DECLARE_EVENT_TABLE()
115};
116
117// A small helper class which intercepts all menu events and logs them
118class MyEvtHandler : public wxEvtHandler
119{
120public:
121 MyEvtHandler(MyFrame *frame) { m_frame = frame; }
122
123 void OnMenuEvent(wxCommandEvent& event)
124 {
125 m_frame->LogMenuEvent(event);
126
127 event.Skip();
128 }
129
130private:
131 MyFrame *m_frame;
132
133 DECLARE_EVENT_TABLE()
134};
135
136// ----------------------------------------------------------------------------
137// constants
138// ----------------------------------------------------------------------------
139
140enum
141{
142 Menu_File_Quit = 100,
143 Menu_File_ClearLog,
144
145 Menu_MenuBar_Toggle = 200,
146 Menu_MenuBar_Append,
147 Menu_MenuBar_Insert,
148 Menu_MenuBar_Delete,
149 Menu_MenuBar_Enable,
150 Menu_MenuBar_GetLabel,
151 Menu_MenuBar_SetLabel,
152
153 Menu_Menu_Append = 300,
154 Menu_Menu_AppendSub,
155 Menu_Menu_Insert,
156 Menu_Menu_Delete,
157 Menu_Menu_Enable,
158 Menu_Menu_Check,
159 Menu_Menu_GetLabel,
160 Menu_Menu_SetLabel,
161 Menu_Menu_GetInfo,
162
163 Menu_Dummy_First = 400,
164 Menu_Dummy_Second,
165 Menu_Dummy_Third,
166 Menu_Dummy_Fourth,
167 Menu_Dummy_Last,
168
169 Menu_Help_About = 1000,
170
171 Menu_Popup_ToBeDeleted = 2000,
172 Menu_Popup_ToBeGreyed,
173 Menu_Popup_ToBeChecked,
174 Menu_Popup_Submenu,
175
176 Menu_Max
177};
178
179// ----------------------------------------------------------------------------
180// event tables
181// ----------------------------------------------------------------------------
182
183BEGIN_EVENT_TABLE(MyFrame, wxFrame)
184 EVT_MENU(Menu_File_Quit, MyFrame::OnQuit)
185 EVT_MENU(Menu_File_ClearLog, MyFrame::OnClearLog)
186
187 EVT_MENU(Menu_Help_About, MyFrame::OnAbout)
188
189 EVT_MENU(Menu_MenuBar_Toggle, MyFrame::OnToggleMenu)
190 EVT_MENU(Menu_MenuBar_Append, MyFrame::OnAppendMenu)
191 EVT_MENU(Menu_MenuBar_Insert, MyFrame::OnInsertMenu)
192 EVT_MENU(Menu_MenuBar_Delete, MyFrame::OnDeleteMenu)
193 EVT_MENU(Menu_MenuBar_Enable, MyFrame::OnEnableMenu)
194 EVT_MENU(Menu_MenuBar_GetLabel, MyFrame::OnGetLabelMenu)
195 EVT_MENU(Menu_MenuBar_SetLabel, MyFrame::OnSetLabelMenu)
196
197 EVT_MENU(Menu_Menu_Append, MyFrame::OnAppendMenuItem)
198 EVT_MENU(Menu_Menu_AppendSub, MyFrame::OnAppendSubMenu)
199 EVT_MENU(Menu_Menu_Insert, MyFrame::OnInsertMenuItem)
200 EVT_MENU(Menu_Menu_Delete, MyFrame::OnDeleteMenuItem)
201 EVT_MENU(Menu_Menu_Enable, MyFrame::OnEnableMenuItem)
202 EVT_MENU(Menu_Menu_Check, MyFrame::OnCheckMenuItem)
203 EVT_MENU(Menu_Menu_GetLabel, MyFrame::OnGetLabelMenuItem)
204 EVT_MENU(Menu_Menu_SetLabel, MyFrame::OnSetLabelMenuItem)
205 EVT_MENU(Menu_Menu_GetInfo, MyFrame::OnGetMenuItemInfo)
206
207 EVT_MENU_RANGE(Menu_Dummy_First, Menu_Dummy_Last, MyFrame::OnDummy)
208
209 EVT_UPDATE_UI(Menu_Menu_Check, MyFrame::OnUpdateCheckMenuItemUI)
210
211 EVT_RIGHT_UP(MyFrame::OnRightUp)
212
213 EVT_SIZE(MyFrame::OnSize)
214END_EVENT_TABLE()
215
216BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
217 EVT_MENU(-1, MyEvtHandler::OnMenuEvent)
218END_EVENT_TABLE()
219
220// ============================================================================
221// implementation
222// ============================================================================
223
224// ----------------------------------------------------------------------------
225// MyApp
226// ----------------------------------------------------------------------------
227
228IMPLEMENT_APP(MyApp)
229
230// The `main program' equivalent, creating the windows and returning the
231// main frame
232bool MyApp::OnInit()
233{
234 // Create the main frame window
235 MyFrame* frame = new MyFrame;
236
237 frame->Show(TRUE);
238
239#if wxUSE_STATUSBAR
240 frame->SetStatusText("Welcome to wxWindows menu sample");
241#endif // wxUSE_STATUSBAR
242
243 SetTopWindow(frame);
244
245 return TRUE;
246}
247
248// ----------------------------------------------------------------------------
249// MyFrame
250// ----------------------------------------------------------------------------
251
252// Define my frame constructor
253MyFrame::MyFrame()
254 : wxFrame((wxFrame *)NULL, -1, "wxWindows menu sample",
255 wxDefaultPosition, wxSize(400, 250))
256{
257 m_textctrl = NULL;
258 m_menu = NULL;
259 m_countDummy = 0;
260 m_logOld = NULL;
261
262#if wxUSE_STATUSBAR
263 CreateStatusBar();
264#endif // wxUSE_STATUSBAR
265
266 // create the menubar
267 wxMenu *fileMenu = new wxMenu;
268
269 wxMenuItem *item = new wxMenuItem(fileMenu, Menu_File_ClearLog,
270 "Clear &log\tCtrl-L");
271 item->SetBitmap(copy_xpm);
272 fileMenu->Append(item);
273 fileMenu->AppendSeparator();
274 fileMenu->Append(Menu_File_Quit, "E&xit\tAlt-X", "Quit menu sample");
275
276 wxMenu *menubarMenu = new wxMenu;
277 menubarMenu->Append(Menu_MenuBar_Append, "&Append menu\tCtrl-A",
278 "Append a menu to the menubar");
279 menubarMenu->Append(Menu_MenuBar_Insert, "&Insert menu\tCtrl-I",
280 "Insert a menu into the menubar");
281 menubarMenu->Append(Menu_MenuBar_Delete, "&Delete menu\tCtrl-D",
282 "Delete the last menu from the menubar");
283 menubarMenu->Append(Menu_MenuBar_Toggle, "&Toggle menu\tCtrl-T",
284 "Toggle the first menu in the menubar", TRUE);
285 menubarMenu->AppendSeparator();
286 menubarMenu->Append(Menu_MenuBar_Enable, "&Enable menu\tCtrl-E",
287 "Enable or disable the last menu", TRUE);
288 menubarMenu->AppendSeparator();
289 menubarMenu->Append(Menu_MenuBar_GetLabel, "&Get menu label\tCtrl-G",
290 "Get the label of the last menu");
291 menubarMenu->Append(Menu_MenuBar_SetLabel, "&Set menu label\tCtrl-S",
292 "Change the label of the last menu");
293
294 wxMenu *menuMenu = new wxMenu;
295 menuMenu->Append(Menu_Menu_Append, "&Append menu item\tAlt-A",
296 "Append a menu item to the last menu");
297 menuMenu->Append(Menu_Menu_AppendSub, "&Append sub menu\tAlt-S",
298 "Append a sub menu to the last menu");
299 menuMenu->Append(Menu_Menu_Insert, "&Insert menu item\tAlt-I",
300 "Insert a menu item in head of the last menu");
301 menuMenu->Append(Menu_Menu_Delete, "&Delete menu item\tAlt-D",
302 "Delete the last menu item from the last menu");
303 menuMenu->AppendSeparator();
304 menuMenu->Append(Menu_Menu_Enable, "&Enable menu item\tAlt-E",
305 "Enable or disable the last menu item", TRUE);
306 menuMenu->Append(Menu_Menu_Check, "&Check menu item\tAlt-C",
307 "Check or uncheck the last menu item", TRUE);
308 menuMenu->AppendSeparator();
309 menuMenu->Append(Menu_Menu_GetLabel, "&Get menu item label\tAlt-G",
310 "Get the label of the last menu item");
311 menuMenu->Append(Menu_Menu_SetLabel, "&Set menu item label\tAlt-S",
312 "Change the label of the last menu item");
313 menuMenu->AppendSeparator();
314 menuMenu->Append(Menu_Menu_GetInfo, "Get menu item in&fo\tAlt-F",
315 "Show the state of the last menu item");
316
317 wxMenu *helpMenu = new wxMenu;
318 helpMenu->Append(Menu_Help_About, "&About\tF1", "About menu sample");
319
320 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
321
322 menuBar->Append(fileMenu, "&File");
323 menuBar->Append(menubarMenu, "Menu&bar");
324 menuBar->Append(menuMenu, "&Menu");
325 menuBar->Append(helpMenu, "&Help");
326
327 // these items should be initially checked
328 menuBar->Check(Menu_MenuBar_Toggle, TRUE);
329 menuBar->Check(Menu_MenuBar_Enable, TRUE);
330 menuBar->Check(Menu_Menu_Enable, TRUE);
331 menuBar->Check(Menu_Menu_Check, FALSE);
332
333 // associate the menu bar with the frame
334 SetMenuBar(menuBar);
335
336 // intercept all menu events and log them in this custom event handler
337 PushEventHandler(new MyEvtHandler(this));
338
339 // create the log text window
340 m_textctrl = new wxTextCtrl(this, -1, _T(""),
341 wxDefaultPosition, wxDefaultSize,
342 wxTE_MULTILINE);
343 m_textctrl->SetEditable(FALSE);
344
345 wxLog::SetTimestamp(NULL);
346 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl));
347
348 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
349 _T("append/insert/delete items to/from the last menu.\n")
350 _T("The commands from \"Menubar\" menu work with the ")
351 _T("menubar itself.\n\n")
352 _T("Right click the band below to test popup menus.\n"));
353}
354
355MyFrame::~MyFrame()
356{
357 delete m_menu;
358
359 // delete the event handler installed in ctor
360 PopEventHandler(TRUE);
361
362 // restore old logger
363 delete wxLog::SetActiveTarget(m_logOld);
364}
365
366wxMenu *MyFrame::CreateDummyMenu(wxString *title)
367{
368 wxMenu *menu = new wxMenu;
369 menu->Append(Menu_Dummy_First, "&First item\tCtrl-F1");
370 menu->AppendSeparator();
371 menu->Append(Menu_Dummy_Second, "&Second item\tCtrl-F2", "", TRUE);
372
373 if ( title )
374 {
375 title->Printf(wxT("Dummy menu &%d"), ++m_countDummy);
376 }
377
378 return menu;
379}
380
381wxMenuItem *MyFrame::GetLastMenuItem() const
382{
383 wxMenuBar *menubar = GetMenuBar();
384 wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
385
386 wxMenuItemList::Node *node = menu->GetMenuItems().GetLast();
387 if ( !node )
388 {
389 wxLogWarning(wxT("No last item in the last menu!"));
390
391 return NULL;
392 }
393 else
394 {
395 return node->GetData();
396 }
397}
398
399void MyFrame::LogMenuEvent(const wxCommandEvent& event)
400{
401 int id = event.GetId();
402 if ( !GetMenuBar()->FindItem(id) )
403 return;
404
405 wxString msg = wxString::Format(wxT("Menu command %d"), id);
406 if ( GetMenuBar()->FindItem(id)->IsCheckable() )
407 {
408 msg += wxString::Format(wxT(" (the item is currently %schecked)"),
409 event.IsChecked() ? "" : "not ");
410 }
411
412 wxLogMessage(msg);
413}
414
415// ----------------------------------------------------------------------------
416// menu callbacks
417// ----------------------------------------------------------------------------
418
419void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
420{
421 Close(TRUE);
422}
423
424void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
425{
426 m_textctrl->Clear();
427}
428
429void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
430{
431