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