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