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