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