]>
Commit | Line | Data |
---|---|---|
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 | ||
62ad15a5 VZ |
42 | // not all ports have support for EVT_CONTEXT_MENU yet, don't define |
43 | // USE_CONTEXT_MENU for those which don't | |
aaf996e5 | 44 | #if defined(__WXMOTIF__) || defined(__WXPM__) || defined(__WXX11__) || defined(__WXMGL__) |
51a9eb73 VZ |
45 | #define USE_CONTEXT_MENU 0 |
46 | #else | |
47 | #define USE_CONTEXT_MENU 1 | |
62ad15a5 VZ |
48 | #endif |
49 | ||
1242c2d9 WS |
50 | // this sample is usefull when new port is developed |
51 | // and usually new port has majority of flags turned off | |
52 | #if wxUSE_LOG && wxUSE_TEXTCTRL | |
53 | #define USE_LOG_WINDOW 1 | |
54 | #else | |
55 | #define USE_LOG_WINDOW 0 | |
56 | #endif | |
57 | ||
67160991 | 58 | #if wxUSE_OWNER_DRAWN || defined(__WXGTK__) |
fcaf9e70 | 59 | #include "copy.xpm" |
c1a48eaf | 60 | #endif |
fcaf9e70 | 61 | |
717a57c2 VZ |
62 | // ---------------------------------------------------------------------------- |
63 | // classes | |
64 | // ---------------------------------------------------------------------------- | |
65 | ||
66 | // Define a new application | |
67 | class MyApp: public wxApp | |
68 | { | |
69 | public: | |
70 | bool OnInit(); | |
71 | }; | |
72 | ||
73 | // Define a new frame | |
74 | class MyFrame: public wxFrame | |
75 | { | |
76 | public: | |
77 | MyFrame(); | |
78 | ||
3ca6a5f0 BP |
79 | virtual ~MyFrame(); |
80 | ||
81 | void LogMenuEvent(const wxCommandEvent& event); | |
717a57c2 | 82 | |
e421922f | 83 | protected: |
717a57c2 | 84 | void OnQuit(wxCommandEvent& event); |
1242c2d9 | 85 | #if USE_LOG_WINDOW |
e421922f | 86 | void OnClearLog(wxCommandEvent& event); |
1242c2d9 | 87 | #endif |
e421922f | 88 | |
717a57c2 VZ |
89 | void OnAbout(wxCommandEvent& event); |
90 | ||
91 | void OnDummy(wxCommandEvent& event); | |
92 | ||
93 | void OnAppendMenuItem(wxCommandEvent& event); | |
94 | void OnAppendSubMenu(wxCommandEvent& event); | |
95 | void OnDeleteMenuItem(wxCommandEvent& event); | |
96 | void OnInsertMenuItem(wxCommandEvent& event); | |
97 | void OnCheckMenuItem(wxCommandEvent& event); | |
98 | void OnEnableMenuItem(wxCommandEvent& event); | |
99 | void OnGetLabelMenuItem(wxCommandEvent& event); | |
1242c2d9 | 100 | #if wxUSE_TEXTDLG |
717a57c2 | 101 | void OnSetLabelMenuItem(wxCommandEvent& event); |
1242c2d9 | 102 | #endif |
a80c322c | 103 | void OnGetMenuItemInfo(wxCommandEvent& event); |
1242c2d9 | 104 | #if wxUSE_TEXTDLG |
f6d90fb9 | 105 | void OnFindMenuItem(wxCommandEvent& event); |
1242c2d9 | 106 | #endif |
717a57c2 VZ |
107 | |
108 | void OnAppendMenu(wxCommandEvent& event); | |
f03ec224 | 109 | void OnInsertMenu(wxCommandEvent& event); |
717a57c2 VZ |
110 | void OnDeleteMenu(wxCommandEvent& event); |
111 | void OnToggleMenu(wxCommandEvent& event); | |
112 | void OnEnableMenu(wxCommandEvent& event); | |
113 | void OnGetLabelMenu(wxCommandEvent& event); | |
114 | void OnSetLabelMenu(wxCommandEvent& event); | |
1242c2d9 | 115 | #if wxUSE_TEXTDLG |
f6d90fb9 | 116 | void OnFindMenu(wxCommandEvent& event); |
1242c2d9 | 117 | #endif |
717a57c2 | 118 | |
d65c269b VZ |
119 | void OnTestNormal(wxCommandEvent& event); |
120 | void OnTestCheck(wxCommandEvent& event); | |
121 | void OnTestRadio(wxCommandEvent& event); | |
122 | ||
c377f1be JS |
123 | void OnUpdateSubMenuNormal(wxUpdateUIEvent& event); |
124 | void OnUpdateSubMenuCheck(wxUpdateUIEvent& event); | |
125 | void OnUpdateSubMenuRadio(wxUpdateUIEvent& event); | |
126 | ||
62ad15a5 | 127 | #if USE_CONTEXT_MENU |
ac103441 | 128 | void OnContextMenu(wxContextMenuEvent& event); |
ccef86c7 VZ |
129 | #else |
130 | void OnRightUp(wxMouseEvent& event) | |
131 | { ShowContextMenu(event.GetPosition()); } | |
132 | #endif | |
133 | ||
134 | void OnMenuOpen(wxMenuEvent& event) | |
1242c2d9 WS |
135 | { |
136 | #if USE_LOG_WINDOW | |
137 | LogMenuOpenOrClose(event, _T("opened")); event.Skip(); | |
138 | #endif | |
139 | } | |
ccef86c7 | 140 | void OnMenuClose(wxMenuEvent& event) |
1242c2d9 WS |
141 | { |
142 | #if USE_LOG_WINDOW | |
143 | LogMenuOpenOrClose(event, _T("closed")); event.Skip(); | |
144 | #endif | |
145 | } | |
717a57c2 VZ |
146 | |
147 | void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event); | |
148 | ||
e421922f VZ |
149 | void OnSize(wxSizeEvent& event); |
150 | ||
717a57c2 | 151 | private: |
ccef86c7 VZ |
152 | void LogMenuOpenOrClose(const wxMenuEvent& event, const wxChar *what); |
153 | void ShowContextMenu(const wxPoint& pos); | |
154 | ||
f03ec224 | 155 | wxMenu *CreateDummyMenu(wxString *title); |
717a57c2 VZ |
156 | |
157 | wxMenuItem *GetLastMenuItem() const; | |
158 | ||
e421922f VZ |
159 | // the menu previously detached from the menubar (may be NULL) |
160 | wxMenu *m_menu; | |
717a57c2 | 161 | |
e421922f | 162 | // the count of dummy menus already created |
f03ec224 VZ |
163 | size_t m_countDummy; |
164 | ||
1242c2d9 | 165 | #if USE_LOG_WINDOW |
e421922f VZ |
166 | // the control used for logging |
167 | wxTextCtrl *m_textctrl; | |
1242c2d9 | 168 | #endif |
e421922f VZ |
169 | |
170 | // the previous log target | |
171 | wxLog *m_logOld; | |
172 | ||
717a57c2 VZ |
173 | DECLARE_EVENT_TABLE() |
174 | }; | |
175 | ||
3ca6a5f0 BP |
176 | // A small helper class which intercepts all menu events and logs them |
177 | class MyEvtHandler : public wxEvtHandler | |
178 | { | |
179 | public: | |
180 | MyEvtHandler(MyFrame *frame) { m_frame = frame; } | |
181 | ||
182 | void OnMenuEvent(wxCommandEvent& event) | |
183 | { | |
184 | m_frame->LogMenuEvent(event); | |
185 | ||
186 | event.Skip(); | |
187 | } | |
188 | ||
189 | private: | |
190 | MyFrame *m_frame; | |
191 | ||
192 | DECLARE_EVENT_TABLE() | |
193 | }; | |
194 | ||
717a57c2 VZ |
195 | // ---------------------------------------------------------------------------- |
196 | // constants | |
197 | // ---------------------------------------------------------------------------- | |
198 | ||
199 | enum | |
200 | { | |
1242c2d9 WS |
201 | Menu_File_Quit = wxID_EXIT, |
202 | #if USE_LOG_WINDOW | |
e421922f | 203 | Menu_File_ClearLog, |
1242c2d9 | 204 | #endif |
717a57c2 VZ |
205 | |
206 | Menu_MenuBar_Toggle = 200, | |
207 | Menu_MenuBar_Append, | |
f03ec224 | 208 | Menu_MenuBar_Insert, |
717a57c2 VZ |
209 | Menu_MenuBar_Delete, |
210 | Menu_MenuBar_Enable, | |
211 | Menu_MenuBar_GetLabel, | |
1242c2d9 | 212 | #if wxUSE_TEXTDLG |
717a57c2 | 213 | Menu_MenuBar_SetLabel, |
f6d90fb9 | 214 | Menu_MenuBar_FindMenu, |
1242c2d9 | 215 | #endif |
717a57c2 VZ |
216 | |
217 | Menu_Menu_Append = 300, | |
218 | Menu_Menu_AppendSub, | |
219 | Menu_Menu_Insert, | |
220 | Menu_Menu_Delete, | |
221 | Menu_Menu_Enable, | |
222 | Menu_Menu_Check, | |
223 | Menu_Menu_GetLabel, | |
1242c2d9 | 224 | #if wxUSE_TEXTDLG |
717a57c2 | 225 | Menu_Menu_SetLabel, |
1242c2d9 | 226 | #endif |
a80c322c | 227 | Menu_Menu_GetInfo, |
1242c2d9 | 228 | #if wxUSE_TEXTDLG |
f6d90fb9 | 229 | Menu_Menu_FindItem, |
1242c2d9 | 230 | #endif |
717a57c2 | 231 | |
d65c269b VZ |
232 | Menu_Test_Normal = 400, |
233 | Menu_Test_Check, | |
234 | Menu_Test_Radio1, | |
235 | Menu_Test_Radio2, | |
236 | Menu_Test_Radio3, | |
237 | ||
c377f1be JS |
238 | Menu_SubMenu = 450, |
239 | Menu_SubMenu_Normal, | |
240 | Menu_SubMenu_Check, | |
241 | Menu_SubMenu_Radio1, | |
242 | Menu_SubMenu_Radio2, | |
243 | Menu_SubMenu_Radio3, | |
244 | ||
d65c269b | 245 | Menu_Dummy_First = 500, |
717a57c2 VZ |
246 | Menu_Dummy_Second, |
247 | Menu_Dummy_Third, | |
248 | Menu_Dummy_Fourth, | |
249 | Menu_Dummy_Last, | |
250 | ||
1242c2d9 | 251 | Menu_Help_About = wxID_ABOUT, |
717a57c2 VZ |
252 | |
253 | Menu_Popup_ToBeDeleted = 2000, | |
254 | Menu_Popup_ToBeGreyed, | |
255 | Menu_Popup_ToBeChecked, | |
256 | Menu_Popup_Submenu, | |
257 | ||
258 | Menu_Max | |
259 | }; | |
260 | ||
261 | // ---------------------------------------------------------------------------- | |
262 | // event tables | |
263 | // ---------------------------------------------------------------------------- | |
264 | ||
265 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
e421922f | 266 | EVT_MENU(Menu_File_Quit, MyFrame::OnQuit) |
1242c2d9 | 267 | #if USE_LOG_WINDOW |
e421922f | 268 | EVT_MENU(Menu_File_ClearLog, MyFrame::OnClearLog) |
1242c2d9 | 269 | #endif |
717a57c2 VZ |
270 | |
271 | EVT_MENU(Menu_Help_About, MyFrame::OnAbout) | |
272 | ||
273 | EVT_MENU(Menu_MenuBar_Toggle, MyFrame::OnToggleMenu) | |
274 | EVT_MENU(Menu_MenuBar_Append, MyFrame::OnAppendMenu) | |
f03ec224 | 275 | EVT_MENU(Menu_MenuBar_Insert, MyFrame::OnInsertMenu) |
717a57c2 VZ |
276 | EVT_MENU(Menu_MenuBar_Delete, MyFrame::OnDeleteMenu) |
277 | EVT_MENU(Menu_MenuBar_Enable, MyFrame::OnEnableMenu) | |
278 | EVT_MENU(Menu_MenuBar_GetLabel, MyFrame::OnGetLabelMenu) | |
1242c2d9 | 279 | #if wxUSE_TEXTDLG |
717a57c2 | 280 | EVT_MENU(Menu_MenuBar_SetLabel, MyFrame::OnSetLabelMenu) |
f6d90fb9 | 281 | EVT_MENU(Menu_MenuBar_FindMenu, MyFrame::OnFindMenu) |
1242c2d9 | 282 | #endif |
717a57c2 VZ |
283 | |
284 | EVT_MENU(Menu_Menu_Append, MyFrame::OnAppendMenuItem) | |
285 | EVT_MENU(Menu_Menu_AppendSub, MyFrame::OnAppendSubMenu) | |
286 | EVT_MENU(Menu_Menu_Insert, MyFrame::OnInsertMenuItem) | |
287 | EVT_MENU(Menu_Menu_Delete, MyFrame::OnDeleteMenuItem) | |
288 | EVT_MENU(Menu_Menu_Enable, MyFrame::OnEnableMenuItem) | |
a80c322c | 289 | EVT_MENU(Menu_Menu_Check, MyFrame::OnCheckMenuItem) |
717a57c2 | 290 | EVT_MENU(Menu_Menu_GetLabel, MyFrame::OnGetLabelMenuItem) |
1242c2d9 | 291 | #if wxUSE_TEXTDLG |
717a57c2 | 292 | EVT_MENU(Menu_Menu_SetLabel, MyFrame::OnSetLabelMenuItem) |
1242c2d9 | 293 | #endif |
a80c322c | 294 | EVT_MENU(Menu_Menu_GetInfo, MyFrame::OnGetMenuItemInfo) |
1242c2d9 | 295 | #if wxUSE_TEXTDLG |
f6d90fb9 | 296 | EVT_MENU(Menu_Menu_FindItem, MyFrame::OnFindMenuItem) |
1242c2d9 | 297 | #endif |
717a57c2 | 298 | |
d65c269b VZ |
299 | EVT_MENU(Menu_Test_Normal, MyFrame::OnTestNormal) |
300 | EVT_MENU(Menu_Test_Check, MyFrame::OnTestCheck) | |
301 | EVT_MENU(Menu_Test_Radio1, MyFrame::OnTestRadio) | |
302 | EVT_MENU(Menu_Test_Radio2, MyFrame::OnTestRadio) | |
303 | EVT_MENU(Menu_Test_Radio3, MyFrame::OnTestRadio) | |
304 | ||
c377f1be JS |
305 | EVT_UPDATE_UI(Menu_SubMenu_Normal, MyFrame::OnUpdateSubMenuNormal) |
306 | EVT_UPDATE_UI(Menu_SubMenu_Check, MyFrame::OnUpdateSubMenuCheck) | |
307 | EVT_UPDATE_UI(Menu_SubMenu_Radio1, MyFrame::OnUpdateSubMenuRadio) | |
308 | EVT_UPDATE_UI(Menu_SubMenu_Radio2, MyFrame::OnUpdateSubMenuRadio) | |
309 | EVT_UPDATE_UI(Menu_SubMenu_Radio3, MyFrame::OnUpdateSubMenuRadio) | |
310 | ||
717a57c2 VZ |
311 | EVT_MENU_RANGE(Menu_Dummy_First, Menu_Dummy_Last, MyFrame::OnDummy) |
312 | ||
313 | EVT_UPDATE_UI(Menu_Menu_Check, MyFrame::OnUpdateCheckMenuItemUI) | |
314 | ||
62ad15a5 | 315 | #if USE_CONTEXT_MENU |
ccef86c7 VZ |
316 | EVT_CONTEXT_MENU(MyFrame::OnContextMenu) |
317 | #else | |
e421922f | 318 | EVT_RIGHT_UP(MyFrame::OnRightUp) |
ccef86c7 VZ |
319 | #endif |
320 | ||
321 | EVT_MENU_OPEN(MyFrame::OnMenuOpen) | |
322 | EVT_MENU_CLOSE(MyFrame::OnMenuClose) | |
e421922f VZ |
323 | |
324 | EVT_SIZE(MyFrame::OnSize) | |
717a57c2 VZ |
325 | END_EVENT_TABLE() |
326 | ||
3ca6a5f0 | 327 | BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler) |
1242c2d9 | 328 | EVT_MENU(wxID_ANY, MyEvtHandler::OnMenuEvent) |
3ca6a5f0 BP |
329 | END_EVENT_TABLE() |
330 | ||
717a57c2 VZ |
331 | // ============================================================================ |
332 | // implementation | |
333 | // ============================================================================ | |
334 | ||
335 | // ---------------------------------------------------------------------------- | |
336 | // MyApp | |
337 | // ---------------------------------------------------------------------------- | |
338 | ||
339 | IMPLEMENT_APP(MyApp) | |
340 | ||
341 | // The `main program' equivalent, creating the windows and returning the | |
342 | // main frame | |
343 | bool MyApp::OnInit() | |
344 | { | |
345 | // Create the main frame window | |
346 | MyFrame* frame = new MyFrame; | |
347 | ||
6ee9b7b5 | 348 | frame->Show(true); |
717a57c2 | 349 | |
6d5b2a57 | 350 | #if wxUSE_STATUSBAR |
be5a51fb | 351 | frame->SetStatusText(_T("Welcome to wxWidgets menu sample")); |
6d5b2a57 | 352 | #endif // wxUSE_STATUSBAR |
717a57c2 VZ |
353 | |
354 | SetTopWindow(frame); | |
355 | ||
6ee9b7b5 | 356 | return true; |
717a57c2 VZ |
357 | } |
358 | ||
359 | // ---------------------------------------------------------------------------- | |
360 | // MyFrame | |
361 | // ---------------------------------------------------------------------------- | |
362 | ||
363 | // Define my frame constructor | |
364 | MyFrame::MyFrame() | |
be5a51fb | 365 | : wxFrame((wxFrame *)NULL, wxID_ANY, _T("wxWidgets menu sample")) |
717a57c2 | 366 | { |
1242c2d9 | 367 | #if USE_LOG_WINDOW |
268766dd | 368 | m_textctrl = NULL; |
1242c2d9 | 369 | #endif |
717a57c2 | 370 | m_menu = NULL; |
f03ec224 | 371 | m_countDummy = 0; |
e421922f | 372 | m_logOld = NULL; |
717a57c2 | 373 | |
6d5b2a57 | 374 | #if wxUSE_STATUSBAR |
e421922f | 375 | CreateStatusBar(); |
6d5b2a57 | 376 | #endif // wxUSE_STATUSBAR |
717a57c2 VZ |
377 | |
378 | // create the menubar | |
379 | wxMenu *fileMenu = new wxMenu; | |
6d5b2a57 | 380 | |
ab73fe8d | 381 | wxMenu *stockSubMenu = new wxMenu; |
ee0a94cf RR |
382 | stockSubMenu->Append(wxID_ADD); |
383 | stockSubMenu->Append(wxID_APPLY); | |
384 | stockSubMenu->Append(wxID_BOLD); | |
385 | stockSubMenu->Append(wxID_CANCEL); | |
386 | stockSubMenu->Append(wxID_CLEAR); | |
387 | stockSubMenu->Append(wxID_CLOSE); | |
388 | stockSubMenu->Append(wxID_COPY); | |
389 | stockSubMenu->Append(wxID_CUT); | |
390 | stockSubMenu->Append(wxID_DELETE); | |
391 | stockSubMenu->Append(wxID_FIND); | |
392 | stockSubMenu->Append(wxID_REPLACE); | |
393 | stockSubMenu->Append(wxID_BACKWARD); | |
394 | stockSubMenu->Append(wxID_DOWN); | |
395 | stockSubMenu->Append(wxID_FORWARD); | |
396 | stockSubMenu->Append(wxID_UP); | |
397 | stockSubMenu->Append(wxID_HELP); | |
398 | stockSubMenu->Append(wxID_HOME); | |
399 | stockSubMenu->Append(wxID_INDENT); | |
400 | stockSubMenu->Append(wxID_INDEX); | |
401 | stockSubMenu->Append(wxID_ITALIC); | |
402 | stockSubMenu->Append(wxID_JUSTIFY_CENTER); | |
403 | stockSubMenu->Append(wxID_JUSTIFY_FILL); | |
404 | stockSubMenu->Append(wxID_JUSTIFY_LEFT); | |
405 | stockSubMenu->Append(wxID_JUSTIFY_RIGHT); | |
406 | stockSubMenu->Append(wxID_NEW); | |
407 | stockSubMenu->Append(wxID_NO); | |
408 | stockSubMenu->Append(wxID_OK); | |
409 | stockSubMenu->Append(wxID_OPEN); | |
410 | stockSubMenu->Append(wxID_PASTE); | |
411 | stockSubMenu->Append(wxID_PREFERENCES); | |
412 | stockSubMenu->Append(wxID_PRINT); | |
413 | stockSubMenu->Append(wxID_PREVIEW); | |
414 | stockSubMenu->Append(wxID_PROPERTIES); | |
415 | stockSubMenu->Append(wxID_EXIT); | |
416 | stockSubMenu->Append(wxID_REDO); | |
417 | stockSubMenu->Append(wxID_REFRESH); | |
418 | stockSubMenu->Append(wxID_REMOVE); | |
419 | stockSubMenu->Append(wxID_REVERT_TO_SAVED); | |
420 | stockSubMenu->Append(wxID_SAVE); | |
421 | stockSubMenu->Append(wxID_SAVEAS); | |
422 | stockSubMenu->Append(wxID_STOP); | |
423 | stockSubMenu->Append(wxID_UNDELETE); | |
424 | stockSubMenu->Append(wxID_UNDERLINE); | |
425 | stockSubMenu->Append(wxID_UNDO); | |
426 | stockSubMenu->Append(wxID_UNINDENT); | |
427 | stockSubMenu->Append(wxID_YES); | |
428 | stockSubMenu->Append(wxID_ZOOM_100); | |
429 | stockSubMenu->Append(wxID_ZOOM_FIT); | |
430 | stockSubMenu->Append(wxID_ZOOM_IN); | |
431 | stockSubMenu->Append(wxID_ZOOM_OUT); | |
ab73fe8d VZ |
432 | fileMenu->AppendSubMenu(stockSubMenu, _T("&Standard items demo")); |
433 | ||
1242c2d9 | 434 | #if USE_LOG_WINDOW |
e421922f | 435 | wxMenuItem *item = new wxMenuItem(fileMenu, Menu_File_ClearLog, |
42ed7532 | 436 | _T("Clear &log\tCtrl-L")); |
67160991 | 437 | #if wxUSE_OWNER_DRAWN || defined(__WXGTK__) |
e421922f | 438 | item->SetBitmap(copy_xpm); |
1242c2d9 | 439 | #endif |
e421922f VZ |
440 | fileMenu->Append(item); |
441 | fileMenu->AppendSeparator(); | |
ab73fe8d VZ |
442 | #endif // USE_LOG_WINDOW |
443 | ||
42ed7532 | 444 | fileMenu->Append(Menu_File_Quit, _T("E&xit\tAlt-X"), _T("Quit menu sample")); |
717a57c2 VZ |
445 | |
446 | wxMenu *menubarMenu = new wxMenu; | |
42ed7532 MB |
447 | menubarMenu->Append(Menu_MenuBar_Append, _T("&Append menu\tCtrl-A"), |
448 | _T("Append a menu to the menubar")); | |
449 | menubarMenu->Append(Menu_MenuBar_Insert, _T("&Insert menu\tCtrl-I"), | |
450 | _T("Insert a menu into the menubar")); | |
451 | menubarMenu->Append(Menu_MenuBar_Delete, _T("&Delete menu\tCtrl-D"), | |
452 | _T("Delete the last menu from the menubar")); | |
453 | menubarMenu->Append(Menu_MenuBar_Toggle, _T("&Toggle menu\tCtrl-T"), | |
6ee9b7b5 | 454 | _T("Toggle the first menu in the menubar"), true); |
717a57c2 | 455 | menubarMenu->AppendSeparator(); |
42ed7532 | 456 | menubarMenu->Append(Menu_MenuBar_Enable, _T("&Enable menu\tCtrl-E"), |
6ee9b7b5 | 457 | _T("Enable or disable the last menu"), true); |
717a57c2 | 458 | menubarMenu->AppendSeparator(); |
42ed7532 MB |
459 | menubarMenu->Append(Menu_MenuBar_GetLabel, _T("&Get menu label\tCtrl-G"), |
460 | _T("Get the label of the last menu")); | |
1242c2d9 | 461 | #if wxUSE_TEXTDLG |
42ed7532 MB |
462 | menubarMenu->Append(Menu_MenuBar_SetLabel, _T("&Set menu label\tCtrl-S"), |
463 | _T("Change the label of the last menu")); | |
f6d90fb9 | 464 | menubarMenu->AppendSeparator(); |
42ed7532 MB |
465 | menubarMenu->Append(Menu_MenuBar_FindMenu, _T("&Find menu from label\tCtrl-F"), |
466 | _T("Find a menu by searching for its label")); | |
1242c2d9 | 467 | #endif |
717a57c2 | 468 | |
c377f1be JS |
469 | wxMenu* subMenu = new wxMenu; |
470 | subMenu->Append(Menu_SubMenu_Normal, _T("&Normal submenu item"), _T("Disabled submenu item")); | |
8e1b6d43 | 471 | subMenu->AppendCheckItem(Menu_SubMenu_Check, _T("&Check submenu item"), _T("Check submenu item")); |
9f486b32 VZ |
472 | subMenu->AppendRadioItem(Menu_SubMenu_Radio1, _T("Radio item &1"), _T("Radio item")); |
473 | subMenu->AppendRadioItem(Menu_SubMenu_Radio2, _T("Radio item &2"), _T("Radio item")); | |
474 | subMenu->AppendRadioItem(Menu_SubMenu_Radio3, _T("Radio item &3"), _T("Radio item")); | |
c377f1be JS |
475 | |
476 | menubarMenu->Append(Menu_SubMenu, _T("Submenu"), subMenu); | |
477 | ||
717a57c2 | 478 | wxMenu *menuMenu = new wxMenu; |
42ed7532 MB |
479 | menuMenu->Append(Menu_Menu_Append, _T("&Append menu item\tAlt-A"), |
480 | _T("Append a menu item to the last menu")); | |
481 | menuMenu->Append(Menu_Menu_AppendSub, _T("&Append sub menu\tAlt-S"), | |
482 | _T("Append a sub menu to the last menu")); | |
483 | menuMenu->Append(Menu_Menu_Insert, _T("&Insert menu item\tAlt-I"), | |
484 | _T("Insert a menu item in head of the last menu")); | |
485 | menuMenu->Append(Menu_Menu_Delete, _T("&Delete menu item\tAlt-D"), | |
486 | _T("Delete the last menu item from the last menu")); | |
717a57c2 | 487 | menuMenu->AppendSeparator(); |
42ed7532 | 488 | menuMenu->Append(Menu_Menu_Enable, _T("&Enable menu item\tAlt-E"), |
6ee9b7b5 | 489 | _T("Enable or disable the last menu item"), true); |
42ed7532 | 490 | menuMenu->Append(Menu_Menu_Check, _T("&Check menu item\tAlt-C"), |
6ee9b7b5 | 491 | _T("Check or uncheck the last menu item"), true); |
717a57c2 | 492 | menuMenu->AppendSeparator(); |
42ed7532 MB |
493 | menuMenu->Append(Menu_Menu_GetInfo, _T("Get menu item in&fo\tAlt-F"), |
494 | _T("Show the state of the last menu item")); | |
532d575b | 495 | #if wxUSE_TEXTDLG |
98f29783 RR |
496 | menuMenu->Append(Menu_Menu_SetLabel, _T("Set menu item label\tAlt-L"), |
497 | _T("Set the label of a menu item")); | |
532d575b | 498 | #endif |
1242c2d9 | 499 | #if wxUSE_TEXTDLG |
a80c322c | 500 | menuMenu->AppendSeparator(); |
42ed7532 MB |
501 | menuMenu->Append(Menu_Menu_FindItem, _T("Find menu item from label"), |
502 | _T("Find a menu item by searching for its label")); | |
1242c2d9 | 503 | #endif |
717a57c2 | 504 | |
d65c269b | 505 | wxMenu *testMenu = new wxMenu; |
42ed7532 | 506 | testMenu->Append(Menu_Test_Normal, _T("&Normal item")); |
d65c269b | 507 | testMenu->AppendSeparator(); |
42ed7532 | 508 | testMenu->AppendCheckItem(Menu_Test_Check, _T("&Check item")); |
d65c269b | 509 | testMenu->AppendSeparator(); |
42ed7532 MB |
510 | testMenu->AppendRadioItem(Menu_Test_Radio1, _T("Radio item &1")); |
511 | testMenu->AppendRadioItem(Menu_Test_Radio2, _T("Radio item &2")); | |
512 | testMenu->AppendRadioItem(Menu_Test_Radio3, _T("Radio item &3")); | |
d65c269b | 513 | |
717a57c2 | 514 | wxMenu *helpMenu = new wxMenu; |
42ed7532 | 515 | helpMenu->Append(Menu_Help_About, _T("&About\tF1"), _T("About menu sample")); |
717a57c2 VZ |
516 | |
517 | wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE ); | |
518 | ||
42ed7532 MB |
519 | menuBar->Append(fileMenu, _T("&File")); |
520 | menuBar->Append(menubarMenu, _T("Menu&bar")); | |
521 | menuBar->Append(menuMenu, _T("&Menu")); | |
522 | menuBar->Append(testMenu, _T("&Test")); | |
523 | menuBar->Append(helpMenu, _T("&Help")); | |
717a57c2 VZ |
524 | |
525 | // these items should be initially checked | |
6ee9b7b5 JS |
526 | menuBar->Check(Menu_MenuBar_Toggle, true); |
527 | menuBar->Check(Menu_MenuBar_Enable, true); | |
528 | menuBar->Check(Menu_Menu_Enable, true); | |
529 | menuBar->Check(Menu_Menu_Check, false); | |
717a57c2 VZ |
530 | |
531 | // associate the menu bar with the frame | |
532 | SetMenuBar(menuBar); | |
3ca6a5f0 BP |
533 | |
534 | // intercept all menu events and log them in this custom event handler | |
535 | PushEventHandler(new MyEvtHandler(this)); | |
e421922f | 536 | |
1242c2d9 | 537 | #if USE_LOG_WINDOW |
e421922f | 538 | // create the log text window |
532d575b | 539 | m_textctrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, |
e421922f VZ |
540 | wxDefaultPosition, wxDefaultSize, |
541 | wxTE_MULTILINE); | |
6ee9b7b5 | 542 | m_textctrl->SetEditable(false); |
5c7766de VZ |
543 | |
544 | wxLog::SetTimestamp(NULL); | |
e421922f VZ |
545 | m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl)); |
546 | ||
5c7766de | 547 | wxLogMessage(_T("Brief explanations: the commands or the \"Menu\" menu ") |
e421922f | 548 | _T("append/insert/delete items to/from the last menu.\n") |
5c7766de VZ |
549 | _T("The commands from \"Menubar\" menu work with the ") |
550 | _T("menubar itself.\n\n") | |
e421922f | 551 | _T("Right click the band below to test popup menus.\n")); |
1242c2d9 | 552 | #endif |
92f07cf8 JS |
553 | #ifdef __POCKETPC__ |
554 | EnableContextMenu(); | |
555 | #endif | |
3ca6a5f0 BP |
556 | } |
557 | ||
558 | MyFrame::~MyFrame() | |
559 | { | |
560 | delete m_menu; | |
561 | ||
562 | // delete the event handler installed in ctor | |
6ee9b7b5 | 563 | PopEventHandler(true); |
e421922f | 564 | |
1242c2d9 | 565 | #if USE_LOG_WINDOW |
e421922f VZ |
566 | // restore old logger |
567 | delete wxLog::SetActiveTarget(m_logOld); | |
1242c2d9 | 568 | #endif |
717a57c2 VZ |
569 | } |
570 | ||
f03ec224 | 571 | wxMenu *MyFrame::CreateDummyMenu(wxString *title) |
717a57c2 VZ |
572 | { |
573 | wxMenu *menu = new wxMenu; | |
42ed7532 | 574 | menu->Append(Menu_Dummy_First, _T("&First item\tCtrl-F1")); |
717a57c2 | 575 | menu->AppendSeparator(); |
2153bf89 | 576 | menu->AppendCheckItem(Menu_Dummy_Second, _T("&Second item\tCtrl-F2")); |
717a57c2 | 577 | |
f03ec224 VZ |
578 | if ( title ) |
579 | { | |
6ee9b7b5 | 580 | title->Printf(_T("Dummy menu &%u"), (unsigned)++m_countDummy); |
f03ec224 VZ |
581 | } |
582 | ||
717a57c2 VZ |
583 | return menu; |
584 | } | |
585 | ||
586 | wxMenuItem *MyFrame::GetLastMenuItem() const | |
587 | { | |
588 | wxMenuBar *menubar = GetMenuBar(); | |
589 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); | |
590 | ||
65c0217e | 591 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetLast(); |
717a57c2 VZ |
592 | if ( !node ) |
593 | { | |
6ee9b7b5 | 594 | wxLogWarning(_T("No last item in the last menu!")); |
717a57c2 VZ |
595 | |
596 | return NULL; | |
597 | } | |
598 | else | |
599 | { | |
600 | return node->GetData(); | |
601 | } | |
602 | } | |
603 | ||
3ca6a5f0 BP |
604 | void MyFrame::LogMenuEvent(const wxCommandEvent& event) |
605 | { | |
606 | int id = event.GetId(); | |
e421922f | 607 | |
6ee9b7b5 | 608 | wxString msg = wxString::Format(_T("Menu command %d"), id); |
030f50ef VZ |
609 | |
610 | // catch all checkable menubar items and also the check item from the popup | |
611 | // menu | |
612 | wxMenuItem *item = GetMenuBar()->FindItem(id); | |
613 | if ( (item && item->IsCheckable()) || id == Menu_Popup_ToBeChecked ) | |
3ca6a5f0 | 614 | { |
6ee9b7b5 JS |
615 | msg += wxString::Format(_T(" (the item is currently %schecked)"), |
616 | event.IsChecked() ? _T("") : _T("not ")); | |
3ca6a5f0 BP |
617 | } |
618 | ||
e421922f | 619 | wxLogMessage(msg); |
3ca6a5f0 BP |
620 | } |
621 | ||
622 | // ---------------------------------------------------------------------------- | |
623 | // menu callbacks | |
624 | // ---------------------------------------------------------------------------- | |
625 | ||
717a57c2 VZ |
626 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
627 | { | |
6ee9b7b5 | 628 | Close(true); |
717a57c2 VZ |
629 | } |
630 | ||
1242c2d9 | 631 | #if USE_LOG_WINDOW |
e421922f VZ |
632 | void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event)) |
633 | { | |
634 | m_textctrl->Clear(); | |
635 | } | |
1242c2d9 | 636 | #endif |
e421922f | 637 | |
717a57c2 VZ |
638 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
639 | { | |
232ba4c3 | 640 | (void)wxMessageBox(_T("wxWidgets menu sample\n(c) 1999-2001 Vadim Zeitlin"), |
be5a51fb | 641 | _T("About wxWidgets menu sample"), |
717a57c2 VZ |
642 | wxICON_INFORMATION); |
643 | } | |
644 | ||
645 | void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event)) | |
646 | { | |
647 | wxMenuBar *mbar = GetMenuBar(); | |
648 | ||
649 | size_t count = mbar->GetMenuCount(); | |
650 | if ( count == 2 ) | |
651 | { | |
652 | // don't let delete the first 2 menus | |
6ee9b7b5 | 653 | wxLogError(_T("Can't delete any more menus")); |
717a57c2 VZ |
654 | } |
655 | else | |
656 | { | |
657 | delete mbar->Remove(count - 1); | |
658 | } | |
659 | } | |
660 | ||
f03ec224 | 661 | void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event)) |
717a57c2 | 662 | { |
717a57c2 | 663 | wxString title; |
f03ec224 VZ |
664 | wxMenu *menu = CreateDummyMenu(&title); |
665 | GetMenuBar()->Insert(0, menu, title); | |
666 | } | |
717a57c2 | 667 | |
f03ec224 VZ |
668 | void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event)) |
669 | { | |
670 | wxString title; | |
671 | wxMenu *menu = CreateDummyMenu(&title); | |
672 | GetMenuBar()->Append(menu, title); | |
717a57c2 VZ |
673 | } |
674 | ||
675 | void MyFrame::OnToggleMenu(wxCommandEvent& WXUNUSED(event)) | |
676 | { | |
677 | wxMenuBar *mbar = GetMenuBar(); | |
678 | if ( !m_menu ) | |
679 | { | |
680 | // hide the menu | |
681 | m_menu = mbar->Remove(0); | |
682 | } | |
683 | else | |
684 | { | |
685 | // restore it | |
42ed7532 | 686 | mbar->Insert(0, m_menu, _T("&File")); |
717a57c2 VZ |
687 | m_menu = NULL; |
688 | } | |
689 | } | |
690 | ||
e421922f | 691 | void MyFrame::OnEnableMenu(wxCommandEvent& event) |
717a57c2 VZ |
692 | { |
693 | wxMenuBar *mbar = GetMenuBar(); | |
694 | size_t count = mbar->GetMenuCount(); | |
695 | ||
e421922f | 696 | mbar->EnableTop(count - 1, event.IsChecked()); |
717a57c2 VZ |
697 | } |
698 | ||
699 | void MyFrame::OnGetLabelMenu(wxCommandEvent& WXUNUSED(event)) | |
700 | { | |
701 | wxMenuBar *mbar = GetMenuBar(); | |
702 | size_t count = mbar->GetMenuCount(); | |
703 | ||
5c7766de VZ |
704 | wxCHECK_RET( count, _T("no last menu?") ); |
705 | ||
6ee9b7b5 | 706 | wxLogMessage(_T("The label of the last menu item is '%s'"), |
1987af7e | 707 | mbar->GetLabelTop(count - 1).c_str()); |
717a57c2 VZ |
708 | } |
709 | ||
1242c2d9 | 710 | #if wxUSE_TEXTDLG |
717a57c2 VZ |
711 | void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event)) |
712 | { | |
713 | wxMenuBar *mbar = GetMenuBar(); | |
714 | size_t count = mbar->GetMenuCount(); | |
715 | ||
5c7766de VZ |
716 | wxCHECK_RET( count, _T("no last menu?") ); |
717 | ||
718 | wxString label = wxGetTextFromUser | |
719 | ( | |
720 | _T("Enter new label: "), | |
721 | _T("Change last menu text"), | |
722 | mbar->GetLabelTop(count - 1), | |
723 | this | |
724 | ); | |
725 | ||
726 | if ( !label.empty() ) | |
727 | { | |
728 | mbar->SetLabelTop(count - 1, label); | |
729 | } | |
717a57c2 VZ |
730 | } |
731 | ||
f6d90fb9 GD |
732 | void MyFrame::OnFindMenu(wxCommandEvent& WXUNUSED(event)) |
733 | { | |
734 | wxMenuBar *mbar = GetMenuBar(); | |
735 | size_t count = mbar->GetMenuCount(); | |
736 | ||
737 | wxCHECK_RET( count, _T("no last menu?") ); | |
738 | ||
739 | wxString label = wxGetTextFromUser | |
740 | ( | |
741 | _T("Enter label to search for: "), | |
742 | _T("Find menu"), | |
532d575b | 743 | wxEmptyString, |
f6d90fb9 GD |
744 | this |
745 | ); | |
746 | ||
747 | if ( !label.empty() ) | |
748 | { | |
749 | int index = mbar->FindMenu(label); | |
750 | ||
751 | if (index == wxNOT_FOUND) | |
752 | { | |
6ee9b7b5 | 753 | wxLogWarning(_T("No menu with label '%s'"), label.c_str()); |
f6d90fb9 GD |
754 | } |
755 | else | |
756 | { | |
6ee9b7b5 | 757 | wxLogMessage(_T("Menu %d has label '%s'"), index, label.c_str()); |
f6d90fb9 GD |
758 | } |
759 | } | |
760 | } | |
1242c2d9 | 761 | #endif |
f6d90fb9 | 762 | |
717a57c2 VZ |
763 | void MyFrame::OnDummy(wxCommandEvent& event) |
764 | { | |
6ee9b7b5 | 765 | wxLogMessage(_T("Dummy item #%d"), event.GetId() - Menu_Dummy_First + 1); |
717a57c2 VZ |
766 | } |
767 | ||
768 | void MyFrame::OnAppendMenuItem(wxCommandEvent& WXUNUSED(event)) | |
769 | { | |
770 | wxMenuBar *menubar = GetMenuBar(); | |
771 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); | |
772 | ||
773 | menu->AppendSeparator(); | |
42ed7532 | 774 | menu->Append(Menu_Dummy_Third, _T("&Third dummy item\tCtrl-F3"), |
6ee9b7b5 | 775 | _T("Checkable item"), true); |
717a57c2 VZ |
776 | } |
777 | ||
778 | void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event)) | |
779 | { | |
780 | wxMenuBar *menubar = GetMenuBar(); | |
f6bcfd97 | 781 | |
0c2d3577 | 782 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 2); |
f6bcfd97 | 783 | |
42ed7532 MB |
784 | menu->Append(Menu_Dummy_Last, _T("&Dummy sub menu"), |
785 | CreateDummyMenu(NULL), _T("Dummy sub menu help")); | |
717a57c2 VZ |
786 | } |
787 | ||
788 | void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event)) | |
789 | { | |
790 | wxMenuBar *menubar = GetMenuBar(); | |
791 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); | |
792 | ||
793 | size_t count = menu->GetMenuItemCount(); | |
794 | if ( !count ) | |
795 | { | |
6ee9b7b5 | 796 | wxLogWarning(_T("No items to delete!")); |
717a57c2 VZ |
797 | } |
798 | else | |
799 | { | |
800 | menu->Destroy(menu->GetMenuItems().Item(count - 1)->GetData()); | |
801 | } | |
802 | } | |
803 | ||
804 | void MyFrame::OnInsertMenuItem(wxCommandEvent& WXUNUSED(event)) | |
805 | { | |
806 | wxMenuBar *menubar = GetMenuBar(); | |
807 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); | |
808 | ||
809 | menu->Insert(0, wxMenuItem::New(menu, Menu_Dummy_Fourth, | |
42ed7532 | 810 | _T("Fourth dummy item\tCtrl-F4"))); |
532d575b | 811 | menu->Insert(1, wxMenuItem::New(menu, wxID_SEPARATOR)); |
717a57c2 VZ |
812 | } |
813 | ||
814 | void MyFrame::OnEnableMenuItem(wxCommandEvent& WXUNUSED(event)) | |
815 | { | |
816 | wxMenuItem *item = GetLastMenuItem(); | |
817 | ||
818 | if ( item ) | |
819 | { | |
820 | item->Enable(!item->IsEnabled()); | |
821 | } | |
822 | } | |
823 | ||
824 | void MyFrame::OnCheckMenuItem(wxCommandEvent& WXUNUSED(event)) | |
825 | { | |
826 | wxMenuItem *item = GetLastMenuItem(); | |
827 | ||
828 | item->Toggle(); | |
829 | } | |
830 | ||
831 | void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event) | |
832 | { | |
32db328c VZ |
833 | wxLogNull nolog; |
834 | ||
717a57c2 VZ |
835 | wxMenuItem *item = GetLastMenuItem(); |
836 | ||
837 | event.Enable(item && item->IsCheckable()); | |
838 | } | |
839 | ||
840 | void MyFrame::OnGetLabelMenuItem(wxCommandEvent& WXUNUSED(event)) | |
841 | { | |
842 | wxMenuItem *item = GetLastMenuItem(); | |
843 | ||
844 | if ( item ) | |
845 | { | |
6ee9b7b5 | 846 | wxLogMessage(_T("The label of the last menu item is '%s'"), |
1987af7e | 847 | item->GetLabel().c_str()); |
717a57c2 VZ |
848 | } |
849 | } | |
850 | ||
1242c2d9 | 851 | #if wxUSE_TEXTDLG |
717a57c2 VZ |
852 | void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event)) |
853 | { | |
854 | wxMenuItem *item = GetLastMenuItem(); | |
855 | ||
856 | if ( item ) | |
857 | { | |
5c7766de VZ |
858 | wxString label = wxGetTextFromUser |
859 | ( | |
860 | _T("Enter new label: "), | |
861 | _T("Change last menu item text"), | |
862 | item->GetLabel(), | |
863 | this | |
864 | ); | |
98f29783 | 865 | label.Replace( _T("\\t"), _T("\t") ); |
5c7766de VZ |
866 | |
867 | if ( !label.empty() ) | |
868 | { | |
869 | item->SetText(label); | |
870 | } | |
717a57c2 VZ |
871 | } |
872 | } | |
1242c2d9 | 873 | #endif |
717a57c2 | 874 | |
a80c322c VZ |
875 | void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event)) |
876 | { | |
877 | wxMenuItem *item = GetLastMenuItem(); | |
878 | ||
879 | if ( item ) | |
880 | { | |
881 | wxString msg; | |
42ed7532 MB |
882 | msg << _T("The item is ") << (item->IsEnabled() ? _T("enabled") |
883 | : _T("disabled")) | |
6ee9b7b5 | 884 | << _T('\n'); |
f6bcfd97 | 885 | |
a80c322c VZ |
886 | if ( item->IsCheckable() ) |
887 | { | |
42ed7532 MB |
888 | msg << _T("It is checkable and ") << (item->IsChecked() ? _T("") : _T("un")) |
889 | << _T("checked\n"); | |
a80c322c VZ |
890 | } |
891 | ||
892 | #if wxUSE_ACCEL | |
893 | wxAcceleratorEntry *accel = item->GetAccel(); | |
894 | if ( accel ) | |
895 | { | |
42ed7532 | 896 | msg << _T("Its accelerator is "); |
a80c322c VZ |
897 | |
898 | int flags = accel->GetFlags(); | |
899 | if ( flags & wxACCEL_ALT ) | |
6ee9b7b5 | 900 | msg << _T("Alt-"); |
a80c322c | 901 | if ( flags & wxACCEL_CTRL ) |
6ee9b7b5 | 902 | msg << _T("Ctrl-"); |
a80c322c | 903 | if ( flags & wxACCEL_SHIFT ) |
6ee9b7b5 | 904 | msg << _T("Shift-"); |
a80c322c VZ |
905 | |
906 | int code = accel->GetKeyCode(); | |
907 | switch ( code ) | |
908 | { | |
909 | case WXK_F1: | |
910 | case WXK_F2: | |
911 | case WXK_F3: | |
912 | case WXK_F4: | |
913 | case WXK_F5: | |
914 | case WXK_F6: | |
915 | case WXK_F7: | |
916 | case WXK_F8: | |
917 | case WXK_F9: | |
918 | case WXK_F10: | |
919 | case WXK_F11: | |
920 | case WXK_F12: | |
6ee9b7b5 | 921 | msg << _T('F') << code - WXK_F1 + 1; |
a80c322c VZ |
922 | break; |
923 | ||
924 | // if there are any other keys wxGetAccelFromString() may return, | |
925 | // we should process them here | |
926 | ||
927 | default: | |
928 | if ( wxIsalnum(code) ) | |
929 | { | |
930 | msg << (wxChar)code; | |
931 | ||
932 | break; | |
933 | } | |
934 | ||
6ee9b7b5 | 935 | wxFAIL_MSG( _T("unknown keyboard accel") ); |
a80c322c VZ |
936 | } |
937 | ||
938 | delete accel; | |
939 | } | |
940 | else | |
941 | { | |
42ed7532 | 942 | msg << _T("It doesn't have an accelerator"); |
a80c322c VZ |
943 | } |
944 | #endif // wxUSE_ACCEL | |
945 | ||
946 | wxLogMessage(msg); | |
947 | } | |
948 | } | |
949 | ||
1242c2d9 | 950 | #if wxUSE_TEXTDLG |
f6d90fb9 GD |
951 | void MyFrame::OnFindMenuItem(wxCommandEvent& WXUNUSED(event)) |
952 | { | |
953 | wxMenuBar *mbar = GetMenuBar(); | |
954 | size_t count = mbar->GetMenuCount(); | |
955 | ||
956 | wxCHECK_RET( count, _T("no last menu?") ); | |
957 | ||
958 | wxString label = wxGetTextFromUser | |
959 | ( | |
960 | _T("Enter label to search for: "), | |
961 | _T("Find menu item"), | |
532d575b | 962 | wxEmptyString, |
f6d90fb9 GD |
963 | this |
964 | ); | |
965 | ||
966 | if ( !label.empty() ) | |
967 | { | |
6ee9b7b5 | 968 | size_t menuindex; |
f6d90fb9 | 969 | int index = wxNOT_FOUND; |
62ad15a5 | 970 | |
f6d90fb9 GD |
971 | for (menuindex = 0; (menuindex < count) && (index == wxNOT_FOUND); ++menuindex) |
972 | { | |
973 | index = mbar->FindMenuItem(mbar->GetMenu(menuindex)->GetTitle(), label); | |
974 | } | |
975 | if (index == wxNOT_FOUND) | |
976 | { | |
6ee9b7b5 | 977 | wxLogWarning(_T("No menu item with label '%s'"), label.c_str()); |
f6d90fb9 GD |
978 | } |
979 | else | |
980 | { | |
6ee9b7b5 | 981 | wxLogMessage(_T("Menu item %d in menu %lu has label '%s'"), |
2e491df9 | 982 | index, (unsigned long)menuindex, label.c_str()); |
f6d90fb9 GD |
983 | } |
984 | } | |
985 | } | |
1242c2d9 | 986 | #endif |
f6d90fb9 | 987 | |
ccef86c7 | 988 | void MyFrame::ShowContextMenu(const wxPoint& pos) |
717a57c2 | 989 | { |
42b16af4 | 990 | wxMenu menu; |
717a57c2 | 991 | |
42ed7532 MB |
992 | menu.Append(Menu_Help_About, _T("&About")); |
993 | menu.Append(Menu_Popup_Submenu, _T("&Submenu"), CreateDummyMenu(NULL)); | |
994 | menu.Append(Menu_Popup_ToBeDeleted, _T("To be &deleted")); | |
2153bf89 | 995 | menu.AppendCheckItem(Menu_Popup_ToBeChecked, _T("To be &checked")); |
4760fa91 VZ |
996 | menu.Append(Menu_Popup_ToBeGreyed, _T("To be &greyed"), |
997 | _T("This menu item should be initially greyed out")); | |
717a57c2 | 998 | menu.AppendSeparator(); |
42ed7532 | 999 | menu.Append(Menu_File_Quit, _T("E&xit")); |
717a57c2 | 1000 | |
f6bcfd97 | 1001 | menu.Delete(Menu_Popup_ToBeDeleted); |
6ee9b7b5 JS |
1002 | menu.Check(Menu_Popup_ToBeChecked, true); |
1003 | menu.Enable(Menu_Popup_ToBeGreyed, false); | |
717a57c2 | 1004 | |
ccef86c7 | 1005 | PopupMenu(&menu, pos.x, pos.y); |
f6bcfd97 BP |
1006 | |
1007 | // test for destroying items in popup menus | |
e421922f | 1008 | #if 0 // doesn't work in wxGTK! |
f6bcfd97 BP |
1009 | menu.Destroy(Menu_Popup_Submenu); |
1010 | ||
1011 | PopupMenu( &menu, event.GetX(), event.GetY() ); | |
1012 | #endif // 0 | |
717a57c2 | 1013 | } |
e421922f | 1014 | |
256b8649 | 1015 | void MyFrame::OnTestNormal(wxCommandEvent& WXUNUSED(event)) |
d65c269b VZ |
1016 | { |
1017 | wxLogMessage(_T("Normal item selected")); | |
1018 | } | |
1019 | ||
1020 | void MyFrame::OnTestCheck(wxCommandEvent& event) | |
1021 | { | |
1022 | wxLogMessage(_T("Check item %schecked"), | |
1023 | event.IsChecked() ? _T("") : _T("un")); | |
1024 | } | |
1025 | ||
1026 | void MyFrame::OnTestRadio(wxCommandEvent& event) | |
1027 | { | |
1028 | wxLogMessage(_T("Radio item %d selected"), | |
1029 | event.GetId() - Menu_Test_Radio1 + 1); | |
1030 | } | |
1031 | ||
1242c2d9 | 1032 | #if USE_LOG_WINDOW |
ccef86c7 VZ |
1033 | void MyFrame::LogMenuOpenOrClose(const wxMenuEvent& event, const wxChar *what) |
1034 | { | |
6ee9b7b5 JS |
1035 | wxString msg; |
1036 | msg << _T("A ") | |
1037 | << ( event.IsPopup() ? _T("popup ") : _T("") ) | |
1038 | << _T("menu has been ") | |
1039 | << what | |
1040 | << _T("."); | |
62ad15a5 | 1041 | |
6ee9b7b5 | 1042 | wxLogStatus(this, msg.c_str()); |
ccef86c7 | 1043 | } |
1242c2d9 | 1044 | #endif |
ccef86c7 | 1045 | |
c377f1be JS |
1046 | void MyFrame::OnUpdateSubMenuNormal(wxUpdateUIEvent& event) |
1047 | { | |
6ee9b7b5 | 1048 | event.Enable(false); |
c377f1be JS |
1049 | } |
1050 | ||
1051 | void MyFrame::OnUpdateSubMenuCheck(wxUpdateUIEvent& event) | |
1052 | { | |
6ee9b7b5 | 1053 | event.Enable(true); |
c377f1be JS |
1054 | } |
1055 | ||
1056 | void MyFrame::OnUpdateSubMenuRadio(wxUpdateUIEvent& event) | |
1057 | { | |
1058 | int which = (event.GetId() - Menu_SubMenu_Radio1 + 1); | |
1059 | if (which == 2) | |
6ee9b7b5 | 1060 | event.Check(true); |
c377f1be | 1061 | else |
6ee9b7b5 | 1062 | event.Check(false); |
c377f1be JS |
1063 | } |
1064 | ||
ac103441 RR |
1065 | #if USE_CONTEXT_MENU |
1066 | void MyFrame::OnContextMenu(wxContextMenuEvent& event) | |
1067 | { | |
1068 | wxPoint point = event.GetPosition(); | |
1069 | // If from keyboard | |
1070 | if (point.x == -1 && point.y == -1) { | |
1071 | wxSize size = GetSize(); | |
1072 | point.x = size.x / 2; | |
1073 | point.y = size.y / 2; | |
1074 | } else { | |
1075 | point = ScreenToClient(point); | |
1076 | } | |
1077 | ShowContextMenu(point); | |
1078 | } | |
1079 | #endif | |
1080 | ||
256b8649 | 1081 | void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event)) |
e421922f | 1082 | { |
1242c2d9 | 1083 | #if USE_LOG_WINDOW |
268766dd VZ |
1084 | if ( !m_textctrl ) |
1085 | return; | |
1086 | ||
e421922f VZ |
1087 | // leave a band below for popup menu testing |
1088 | wxSize size = GetClientSize(); | |
1089 | m_textctrl->SetSize(0, 0, size.x, (3*size.y)/4); | |
1242c2d9 | 1090 | #endif |
e421922f VZ |
1091 | |
1092 | // this is really ugly but we have to do it as we can't just call | |
1093 | // event.Skip() because wxFrameBase would make the text control fill the | |
1094 | // entire frame then | |
1095 | #ifdef __WXUNIVERSAL__ | |
1096 | PositionMenuBar(); | |
1097 | #endif // __WXUNIVERSAL__ | |
1098 | } | |
1099 |