]> git.saurik.com Git - wxWidgets.git/blame - samples/menu/menu.cpp
Updated PyCrust from Patrick O'Brien
[wxWidgets.git] / samples / menu / menu.cpp
CommitLineData
717a57c2
VZ
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".
92a19c2e 21#include "wx/wxprec.h"
717a57c2
VZ
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#ifndef WX_PRECOMP
f91bf72f
GD
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"
5c7766de 34 #include "wx/textdlg.h"
717a57c2
VZ
35#endif
36
6d5b2a57
VZ
37#if !wxUSE_MENUS
38 // nice try...
39 #error "menu sample requires wxUSE_MENUS=1"
40#endif // wxUSE_MENUS
41
fcaf9e70
RR
42#include "copy.xpm"
43
717a57c2
VZ
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
3ca6a5f0
BP
61 virtual ~MyFrame();
62
63 void LogMenuEvent(const wxCommandEvent& event);
717a57c2 64
e421922f 65protected:
717a57c2 66 void OnQuit(wxCommandEvent& event);
e421922f
VZ
67 void OnClearLog(wxCommandEvent& event);
68
717a57c2
VZ
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);
a80c322c 81 void OnGetMenuItemInfo(wxCommandEvent& event);
717a57c2
VZ
82
83 void OnAppendMenu(wxCommandEvent& event);
f03ec224 84 void OnInsertMenu(wxCommandEvent& event);
717a57c2
VZ
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
e421922f 91 void OnRightUp(wxMouseEvent& event);
717a57c2
VZ
92
93 void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event);
94
e421922f
VZ
95 void OnSize(wxSizeEvent& event);
96
717a57c2 97private:
f03ec224 98 wxMenu *CreateDummyMenu(wxString *title);
717a57c2
VZ
99
100 wxMenuItem *GetLastMenuItem() const;
101
e421922f
VZ
102 // the menu previously detached from the menubar (may be NULL)
103 wxMenu *m_menu;
717a57c2 104
e421922f 105 // the count of dummy menus already created
f03ec224
VZ
106 size_t m_countDummy;
107
e421922f
VZ
108 // the control used for logging
109 wxTextCtrl *m_textctrl;
110
111 // the previous log target
112 wxLog *m_logOld;
113
717a57c2
VZ
114 DECLARE_EVENT_TABLE()
115};
116
3ca6a5f0
BP
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
717a57c2
VZ
136// ----------------------------------------------------------------------------
137// constants
138// ----------------------------------------------------------------------------
139
140enum
141{
142 Menu_File_Quit = 100,
e421922f 143 Menu_File_ClearLog,
717a57c2
VZ
144
145 Menu_MenuBar_Toggle = 200,
146 Menu_MenuBar_Append,
f03ec224 147 Menu_MenuBar_Insert,
717a57c2
VZ
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,
a80c322c 161 Menu_Menu_GetInfo,
717a57c2
VZ
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)
e421922f
VZ
184 EVT_MENU(Menu_File_Quit, MyFrame::OnQuit)
185 EVT_MENU(Menu_File_ClearLog, MyFrame::OnClearLog)
717a57c2
VZ
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)
f03ec224 191 EVT_MENU(Menu_MenuBar_Insert, MyFrame::OnInsertMenu)
717a57c2
VZ
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)
a80c322c 202 EVT_MENU(Menu_Menu_Check, MyFrame::OnCheckMenuItem)
717a57c2
VZ
203 EVT_MENU(Menu_Menu_GetLabel, MyFrame::OnGetLabelMenuItem)
204 EVT_MENU(Menu_Menu_SetLabel, MyFrame::OnSetLabelMenuItem)
a80c322c 205 EVT_MENU(Menu_Menu_GetInfo, MyFrame::OnGetMenuItemInfo)
717a57c2
VZ
206
207 EVT_MENU_RANGE(Menu_Dummy_First, Menu_Dummy_Last, MyFrame::OnDummy)
208
209 EVT_UPDATE_UI(Menu_Menu_Check, MyFrame::OnUpdateCheckMenuItemUI)
210
e421922f
VZ
211 EVT_RIGHT_UP(MyFrame::OnRightUp)
212
213 EVT_SIZE(MyFrame::OnSize)
717a57c2
VZ
214END_EVENT_TABLE()
215
3ca6a5f0
BP
216BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
217 EVT_MENU(-1, MyEvtHandler::OnMenuEvent)
218END_EVENT_TABLE()
219
717a57c2
VZ
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
6d5b2a57 239#if wxUSE_STATUSBAR
e421922f 240 frame->SetStatusText("Welcome to wxWindows menu sample");
6d5b2a57 241#endif // wxUSE_STATUSBAR
717a57c2
VZ
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",
5c7766de 255 wxDefaultPosition, wxSize(400, 250))
717a57c2 256{
268766dd 257 m_textctrl = NULL;
717a57c2 258 m_menu = NULL;
f03ec224 259 m_countDummy = 0;
e421922f 260 m_logOld = NULL;
717a57c2 261
6d5b2a57 262#if wxUSE_STATUSBAR
e421922f 263 CreateStatusBar();
6d5b2a57 264#endif // wxUSE_STATUSBAR
717a57c2
VZ
265
266 // create the menubar
267 wxMenu *fileMenu = new wxMenu;
6d5b2a57 268
e421922f
VZ
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");
717a57c2
VZ
275
276 wxMenu *menubarMenu = new wxMenu;
277 menubarMenu->Append(Menu_MenuBar_Append, "&Append menu\tCtrl-A",
278 "Append a menu to the menubar");
f03ec224
VZ
279 menubarMenu->Append(Menu_MenuBar_Insert, "&Insert menu\tCtrl-I",
280 "Insert a menu into the menubar");
717a57c2
VZ
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");
a80c322c
VZ
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");
717a57c2
VZ
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);
3ca6a5f0
BP
335
336 // intercept all menu events and log them in this custom event handler
337 PushEventHandler(new MyEvtHandler(this));
e421922f
VZ
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);
5c7766de
VZ
344
345 wxLog::SetTimestamp(NULL);
e421922f
VZ
346 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl));
347
5c7766de 348 wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ")
e421922f 349 _T("append/insert/delete items to/from the last menu.\n")
5c7766de
VZ
350 _T("The commands from \"Menubar\" menu work with the ")
351 _T("menubar itself.\n\n")
e421922f 352 _T("Right click the band below to test popup menus.\n"));
3ca6a5f0
BP
353}
354
355MyFrame::~MyFrame()
356{
357 delete m_menu;
358
359 // delete the event handler installed in ctor
360 PopEventHandler(TRUE);
e421922f
VZ
361
362 // restore old logger
363 delete wxLog::SetActiveTarget(m_logOld);
717a57c2
VZ
364}
365
f03ec224 366wxMenu *MyFrame::CreateDummyMenu(wxString *title)
717a57c2
VZ
367{
368 wxMenu *menu = new wxMenu;
e421922f 369 menu->Append(Menu_Dummy_First, "&First item\tCtrl-F1");
717a57c2 370 menu->AppendSeparator();
e421922f 371 menu->Append(Menu_Dummy_Second, "&Second item\tCtrl-F2", "", TRUE);
717a57c2 372
f03ec224
VZ
373 if ( title )
374 {
4693b20c 375 title->Printf(wxT("Dummy menu &%d"), ++m_countDummy);
f03ec224
VZ
376 }
377
717a57c2
VZ
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 {
4693b20c 389 wxLogWarning(wxT("No last item in the last menu!"));
717a57c2
VZ
390
391 return NULL;
392 }
393 else
394 {
395 return node->GetData();
396 }
397}
398
3ca6a5f0
BP
399void MyFrame::LogMenuEvent(const wxCommandEvent& event)
400{
401 int id = event.GetId();
e421922f 402 if ( !GetMenuBar()->FindItem(id) )
d3d69314 403 return;
e421922f 404
4693b20c 405 wxString msg = wxString::Format(wxT("Menu command %d"), id);
3ca6a5f0
BP
406 if ( GetMenuBar()->FindItem(id)->IsCheckable() )
407 {
4693b20c 408 msg += wxString::Format(wxT(" (the item is currently %schecked)"),
3ca6a5f0
BP
409 event.IsChecked() ? "" : "not ");
410 }
411
e421922f 412 wxLogMessage(msg);
3ca6a5f0
BP
413}
414
415// ----------------------------------------------------------------------------
416// menu callbacks
417// ----------------------------------------------------------------------------
418
717a57c2
VZ
419void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
420{
421 Close(TRUE);
422}
423
e421922f
VZ
424void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
425{
426 m_textctrl->Clear();
427}
428
717a57c2
VZ
429void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
430{
e421922f 431