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