]>
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". | |
21 | #include <wx/wxprec.h> | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #include <wx/wx.h> | |
717a57c2 VZ |
29 | #include <wx/log.h> |
30 | #endif | |
31 | ||
6d5b2a57 VZ |
32 | #if !wxUSE_MENUS |
33 | // nice try... | |
34 | #error "menu sample requires wxUSE_MENUS=1" | |
35 | #endif // wxUSE_MENUS | |
36 | ||
fcaf9e70 RR |
37 | #include "copy.xpm" |
38 | ||
6d5b2a57 VZ |
39 | #ifdef __WXUNIVERSAL__ |
40 | #include "wx/univ/theme.h" | |
41 | ||
42 | WX_USE_THEME(win32); | |
43 | WX_USE_THEME(gtk); | |
44 | ||
45 | // not implemented yet | |
46 | #define wxMessageBox | |
47 | #endif // __WXUNIVERSAL__ | |
48 | ||
717a57c2 VZ |
49 | // ---------------------------------------------------------------------------- |
50 | // classes | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | // Define a new application | |
54 | class MyApp: public wxApp | |
55 | { | |
56 | public: | |
57 | bool OnInit(); | |
58 | }; | |
59 | ||
60 | // Define a new frame | |
61 | class MyFrame: public wxFrame | |
62 | { | |
63 | public: | |
64 | MyFrame(); | |
65 | ||
3ca6a5f0 BP |
66 | virtual ~MyFrame(); |
67 | ||
68 | void LogMenuEvent(const wxCommandEvent& event); | |
717a57c2 VZ |
69 | |
70 | void OnQuit(wxCommandEvent& event); | |
71 | void OnAbout(wxCommandEvent& event); | |
72 | ||
73 | void OnDummy(wxCommandEvent& event); | |
74 | ||
75 | void OnAppendMenuItem(wxCommandEvent& event); | |
76 | void OnAppendSubMenu(wxCommandEvent& event); | |
77 | void OnDeleteMenuItem(wxCommandEvent& event); | |
78 | void OnInsertMenuItem(wxCommandEvent& event); | |
79 | void OnCheckMenuItem(wxCommandEvent& event); | |
80 | void OnEnableMenuItem(wxCommandEvent& event); | |
81 | void OnGetLabelMenuItem(wxCommandEvent& event); | |
82 | void OnSetLabelMenuItem(wxCommandEvent& event); | |
a80c322c | 83 | void OnGetMenuItemInfo(wxCommandEvent& event); |
717a57c2 VZ |
84 | |
85 | void OnAppendMenu(wxCommandEvent& event); | |
f03ec224 | 86 | void OnInsertMenu(wxCommandEvent& event); |
717a57c2 VZ |
87 | void OnDeleteMenu(wxCommandEvent& event); |
88 | void OnToggleMenu(wxCommandEvent& event); | |
89 | void OnEnableMenu(wxCommandEvent& event); | |
90 | void OnGetLabelMenu(wxCommandEvent& event); | |
91 | void OnSetLabelMenu(wxCommandEvent& event); | |
92 | ||
93 | void OnRightDown(wxMouseEvent& event); | |
94 | ||
95 | void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event); | |
96 | ||
97 | private: | |
f03ec224 | 98 | wxMenu *CreateDummyMenu(wxString *title); |
717a57c2 VZ |
99 | |
100 | wxMenuItem *GetLastMenuItem() const; | |
101 | ||
102 | wxMenu *m_menu; | |
103 | ||
f03ec224 VZ |
104 | size_t m_countDummy; |
105 | ||
717a57c2 VZ |
106 | DECLARE_EVENT_TABLE() |
107 | }; | |
108 | ||
3ca6a5f0 BP |
109 | // A small helper class which intercepts all menu events and logs them |
110 | class MyEvtHandler : public wxEvtHandler | |
111 | { | |
112 | public: | |
113 | MyEvtHandler(MyFrame *frame) { m_frame = frame; } | |
114 | ||
115 | void OnMenuEvent(wxCommandEvent& event) | |
116 | { | |
117 | m_frame->LogMenuEvent(event); | |
118 | ||
119 | event.Skip(); | |
120 | } | |
121 | ||
122 | private: | |
123 | MyFrame *m_frame; | |
124 | ||
125 | DECLARE_EVENT_TABLE() | |
126 | }; | |
127 | ||
717a57c2 VZ |
128 | // ---------------------------------------------------------------------------- |
129 | // constants | |
130 | // ---------------------------------------------------------------------------- | |
131 | ||
132 | enum | |
133 | { | |
134 | Menu_File_Quit = 100, | |
135 | ||
136 | Menu_MenuBar_Toggle = 200, | |
137 | Menu_MenuBar_Append, | |
f03ec224 | 138 | Menu_MenuBar_Insert, |
717a57c2 VZ |
139 | Menu_MenuBar_Delete, |
140 | Menu_MenuBar_Enable, | |
141 | Menu_MenuBar_GetLabel, | |
142 | Menu_MenuBar_SetLabel, | |
143 | ||
144 | Menu_Menu_Append = 300, | |
145 | Menu_Menu_AppendSub, | |
146 | Menu_Menu_Insert, | |
147 | Menu_Menu_Delete, | |
148 | Menu_Menu_Enable, | |
149 | Menu_Menu_Check, | |
150 | Menu_Menu_GetLabel, | |
151 | Menu_Menu_SetLabel, | |
a80c322c | 152 | Menu_Menu_GetInfo, |
717a57c2 VZ |
153 | |
154 | Menu_Dummy_First = 400, | |
155 | Menu_Dummy_Second, | |
156 | Menu_Dummy_Third, | |
157 | Menu_Dummy_Fourth, | |
158 | Menu_Dummy_Last, | |
159 | ||
160 | Menu_Help_About = 1000, | |
161 | ||
162 | Menu_Popup_ToBeDeleted = 2000, | |
163 | Menu_Popup_ToBeGreyed, | |
164 | Menu_Popup_ToBeChecked, | |
165 | Menu_Popup_Submenu, | |
166 | ||
167 | Menu_Max | |
168 | }; | |
169 | ||
170 | // ---------------------------------------------------------------------------- | |
171 | // event tables | |
172 | // ---------------------------------------------------------------------------- | |
173 | ||
174 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
175 | EVT_MENU(Menu_File_Quit, MyFrame::OnQuit) | |
176 | ||
177 | EVT_MENU(Menu_Help_About, MyFrame::OnAbout) | |
178 | ||
179 | EVT_MENU(Menu_MenuBar_Toggle, MyFrame::OnToggleMenu) | |
180 | EVT_MENU(Menu_MenuBar_Append, MyFrame::OnAppendMenu) | |
f03ec224 | 181 | EVT_MENU(Menu_MenuBar_Insert, MyFrame::OnInsertMenu) |
717a57c2 VZ |
182 | EVT_MENU(Menu_MenuBar_Delete, MyFrame::OnDeleteMenu) |
183 | EVT_MENU(Menu_MenuBar_Enable, MyFrame::OnEnableMenu) | |
184 | EVT_MENU(Menu_MenuBar_GetLabel, MyFrame::OnGetLabelMenu) | |
185 | EVT_MENU(Menu_MenuBar_SetLabel, MyFrame::OnSetLabelMenu) | |
186 | ||
187 | EVT_MENU(Menu_Menu_Append, MyFrame::OnAppendMenuItem) | |
188 | EVT_MENU(Menu_Menu_AppendSub, MyFrame::OnAppendSubMenu) | |
189 | EVT_MENU(Menu_Menu_Insert, MyFrame::OnInsertMenuItem) | |
190 | EVT_MENU(Menu_Menu_Delete, MyFrame::OnDeleteMenuItem) | |
191 | EVT_MENU(Menu_Menu_Enable, MyFrame::OnEnableMenuItem) | |
a80c322c | 192 | EVT_MENU(Menu_Menu_Check, MyFrame::OnCheckMenuItem) |
717a57c2 VZ |
193 | EVT_MENU(Menu_Menu_GetLabel, MyFrame::OnGetLabelMenuItem) |
194 | EVT_MENU(Menu_Menu_SetLabel, MyFrame::OnSetLabelMenuItem) | |
a80c322c | 195 | EVT_MENU(Menu_Menu_GetInfo, MyFrame::OnGetMenuItemInfo) |
717a57c2 VZ |
196 | |
197 | EVT_MENU_RANGE(Menu_Dummy_First, Menu_Dummy_Last, MyFrame::OnDummy) | |
198 | ||
199 | EVT_UPDATE_UI(Menu_Menu_Check, MyFrame::OnUpdateCheckMenuItemUI) | |
200 | ||
201 | EVT_RIGHT_DOWN(MyFrame::OnRightDown) | |
202 | END_EVENT_TABLE() | |
203 | ||
3ca6a5f0 BP |
204 | BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler) |
205 | EVT_MENU(-1, MyEvtHandler::OnMenuEvent) | |
206 | END_EVENT_TABLE() | |
207 | ||
717a57c2 VZ |
208 | // ============================================================================ |
209 | // implementation | |
210 | // ============================================================================ | |
211 | ||
212 | // ---------------------------------------------------------------------------- | |
213 | // MyApp | |
214 | // ---------------------------------------------------------------------------- | |
215 | ||
216 | IMPLEMENT_APP(MyApp) | |
217 | ||
218 | // The `main program' equivalent, creating the windows and returning the | |
219 | // main frame | |
220 | bool MyApp::OnInit() | |
221 | { | |
222 | // Create the main frame window | |
223 | MyFrame* frame = new MyFrame; | |
224 | ||
225 | frame->Show(TRUE); | |
226 | ||
6d5b2a57 | 227 | #if wxUSE_STATUSBAR |
717a57c2 | 228 | frame->SetStatusText("Hello, wxWindows"); |
6d5b2a57 | 229 | #endif // wxUSE_STATUSBAR |
717a57c2 VZ |
230 | |
231 | SetTopWindow(frame); | |
232 | ||
233 | return TRUE; | |
234 | } | |
235 | ||
236 | // ---------------------------------------------------------------------------- | |
237 | // MyFrame | |
238 | // ---------------------------------------------------------------------------- | |
239 | ||
240 | // Define my frame constructor | |
241 | MyFrame::MyFrame() | |
242 | : wxFrame((wxFrame *)NULL, -1, "wxWindows menu sample", | |
243 | wxDefaultPosition, wxSize(300, 200)) | |
244 | { | |
245 | m_menu = NULL; | |
f03ec224 | 246 | m_countDummy = 0; |
717a57c2 | 247 | |
6d5b2a57 | 248 | #if wxUSE_STATUSBAR |
3ca6a5f0 | 249 | CreateStatusBar(2); |
6d5b2a57 | 250 | #endif // wxUSE_STATUSBAR |
717a57c2 VZ |
251 | |
252 | // create the menubar | |
253 | wxMenu *fileMenu = new wxMenu; | |
6d5b2a57 VZ |
254 | fileMenu->Append(Menu_File_Quit, "E&xit\tAlt-X", "Quit toolbar sample"); |
255 | ||
256 | // not supported just yet | |
257 | #ifndef __WXUNIVERSAL__ | |
258 | wxMenuItem *itemBitmap = new wxMenuItem(fileMenu, Menu_File_Quit, | |
259 | "Quit with &bitmap\tAlt-Q"); | |
260 | itemBitmap->SetBitmap(wxBitmap(copy_xpm)); | |
261 | fileMenu->Append(itemBitmap); | |
262 | #endif // __WXUNIVERSAL__ | |
717a57c2 VZ |
263 | |
264 | wxMenu *menubarMenu = new wxMenu; | |
265 | menubarMenu->Append(Menu_MenuBar_Append, "&Append menu\tCtrl-A", | |
266 | "Append a menu to the menubar"); | |
f03ec224 VZ |
267 | menubarMenu->Append(Menu_MenuBar_Insert, "&Insert menu\tCtrl-I", |
268 | "Insert a menu into the menubar"); | |
717a57c2 VZ |
269 | menubarMenu->Append(Menu_MenuBar_Delete, "&Delete menu\tCtrl-D", |
270 | "Delete the last menu from the menubar"); | |
271 | menubarMenu->Append(Menu_MenuBar_Toggle, "&Toggle menu\tCtrl-T", | |
272 | "Toggle the first menu in the menubar", TRUE); | |
273 | menubarMenu->AppendSeparator(); | |
274 | menubarMenu->Append(Menu_MenuBar_Enable, "&Enable menu\tCtrl-E", | |
275 | "Enable or disable the last menu", TRUE); | |
276 | menubarMenu->AppendSeparator(); | |
277 | menubarMenu->Append(Menu_MenuBar_GetLabel, "&Get menu label\tCtrl-G", | |
278 | "Get the label of the last menu"); | |
279 | menubarMenu->Append(Menu_MenuBar_SetLabel, "&Set menu label\tCtrl-S", | |
280 | "Change the label of the last menu"); | |
281 | ||
282 | wxMenu *menuMenu = new wxMenu; | |
283 | menuMenu->Append(Menu_Menu_Append, "&Append menu item\tAlt-A", | |
284 | "Append a menu item to the last menu"); | |
285 | menuMenu->Append(Menu_Menu_AppendSub, "&Append sub menu\tAlt-S", | |
286 | "Append a sub menu to the last menu"); | |
287 | menuMenu->Append(Menu_Menu_Insert, "&Insert menu item\tAlt-I", | |
288 | "Insert a menu item in head of the last menu"); | |
289 | menuMenu->Append(Menu_Menu_Delete, "&Delete menu item\tAlt-D", | |
290 | "Delete the last menu item from the last menu"); | |
291 | menuMenu->AppendSeparator(); | |
292 | menuMenu->Append(Menu_Menu_Enable, "&Enable menu item\tAlt-E", | |
293 | "Enable or disable the last menu item", TRUE); | |
294 | menuMenu->Append(Menu_Menu_Check, "&Check menu item\tAlt-C", | |
295 | "Check or uncheck the last menu item", TRUE); | |
296 | menuMenu->AppendSeparator(); | |
297 | menuMenu->Append(Menu_Menu_GetLabel, "&Get menu item label\tAlt-G", | |
298 | "Get the label of the last menu item"); | |
299 | menuMenu->Append(Menu_Menu_SetLabel, "&Set menu item label\tAlt-S", | |
300 | "Change the label of the last menu item"); | |
a80c322c VZ |
301 | menuMenu->AppendSeparator(); |
302 | menuMenu->Append(Menu_Menu_GetInfo, "Get menu item in&fo\tAlt-F", | |
303 | "Show the state of the last menu item"); | |
717a57c2 VZ |
304 | |
305 | wxMenu *helpMenu = new wxMenu; | |
306 | helpMenu->Append(Menu_Help_About, "&About\tF1", "About menu sample"); | |
307 | ||
308 | wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE ); | |
309 | ||
310 | menuBar->Append(fileMenu, "&File"); | |
311 | menuBar->Append(menubarMenu, "Menu&bar"); | |
312 | menuBar->Append(menuMenu, "&Menu"); | |
313 | menuBar->Append(helpMenu, "&Help"); | |
314 | ||
315 | // these items should be initially checked | |
316 | menuBar->Check(Menu_MenuBar_Toggle, TRUE); | |
317 | menuBar->Check(Menu_MenuBar_Enable, TRUE); | |
318 | menuBar->Check(Menu_Menu_Enable, TRUE); | |
319 | menuBar->Check(Menu_Menu_Check, FALSE); | |
320 | ||
321 | // associate the menu bar with the frame | |
322 | SetMenuBar(menuBar); | |
3ca6a5f0 BP |
323 | |
324 | // intercept all menu events and log them in this custom event handler | |
325 | PushEventHandler(new MyEvtHandler(this)); | |
326 | } | |
327 | ||
328 | MyFrame::~MyFrame() | |
329 | { | |
330 | delete m_menu; | |
331 | ||
332 | // delete the event handler installed in ctor | |
333 | PopEventHandler(TRUE); | |
717a57c2 VZ |
334 | } |
335 | ||
f03ec224 | 336 | wxMenu *MyFrame::CreateDummyMenu(wxString *title) |
717a57c2 VZ |
337 | { |
338 | wxMenu *menu = new wxMenu; | |
339 | menu->Append(Menu_Dummy_First, "First item\tCtrl-F1"); | |
340 | menu->AppendSeparator(); | |
341 | menu->Append(Menu_Dummy_Second, "Second item\tCtrl-F2", "", TRUE); | |
342 | ||
f03ec224 VZ |
343 | if ( title ) |
344 | { | |
345 | title->Printf("Dummy menu &%d", ++m_countDummy); | |
346 | } | |
347 | ||
717a57c2 VZ |
348 | return menu; |
349 | } | |
350 | ||
351 | wxMenuItem *MyFrame::GetLastMenuItem() const | |
352 | { | |
353 | wxMenuBar *menubar = GetMenuBar(); | |
354 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); | |
355 | ||
356 | wxMenuItemList::Node *node = menu->GetMenuItems().GetLast(); | |
357 | if ( !node ) | |
358 | { | |
359 | wxLogWarning("No last item in the last menu!"); | |
360 | ||
361 | return NULL; | |
362 | } | |
363 | else | |
364 | { | |
365 | return node->GetData(); | |
366 | } | |
367 | } | |
368 | ||
3ca6a5f0 BP |
369 | void MyFrame::LogMenuEvent(const wxCommandEvent& event) |
370 | { | |
371 | int id = event.GetId(); | |
d3d69314 JS |
372 | if (!GetMenuBar()->FindItem(id)) |
373 | return; | |
3ca6a5f0 BP |
374 | wxString msg = wxString::Format("Menu command %d", id); |
375 | if ( GetMenuBar()->FindItem(id)->IsCheckable() ) | |
376 | { | |
377 | msg += wxString::Format(" (the item is currently %schecked)", | |
378 | event.IsChecked() ? "" : "not "); | |
379 | } | |
380 | ||
6d5b2a57 | 381 | #if wxUSE_STATUSBAR |
3ca6a5f0 | 382 | SetStatusText(msg, 1); |
6d5b2a57 | 383 | #endif // wxUSE_STATUSBAR |
3ca6a5f0 BP |
384 | } |
385 | ||
386 | // ---------------------------------------------------------------------------- | |
387 | // menu callbacks | |
388 | // ---------------------------------------------------------------------------- | |
389 | ||
717a57c2 VZ |
390 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
391 | { | |
392 | Close(TRUE); | |
393 | } | |
394 | ||
395 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
396 | { | |
397 | (void)wxMessageBox("wxWindows toolbar sample", | |
398 | "About wxWindows menu sample", | |
399 | wxICON_INFORMATION); | |
400 | } | |
401 | ||
402 | void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event)) | |
403 | { | |
404 | wxMenuBar *mbar = GetMenuBar(); | |
405 | ||
406 | size_t count = mbar->GetMenuCount(); | |
407 | if ( count == 2 ) | |
408 | { | |
409 | // don't let delete the first 2 menus | |
410 | wxLogError("Can't delete any more menus"); | |
411 | } | |
412 | else | |
413 | { | |
414 | delete mbar->Remove(count - 1); | |
415 | } | |
416 | } | |
417 | ||
f03ec224 | 418 | void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event)) |
717a57c2 | 419 | { |
717a57c2 | 420 | wxString title; |
f03ec224 VZ |
421 | wxMenu *menu = CreateDummyMenu(&title); |
422 | GetMenuBar()->Insert(0, menu, title); | |
423 | } | |
717a57c2 | 424 | |
f03ec224 VZ |
425 | void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event)) |
426 | { | |
427 | wxString title; | |
428 | wxMenu *menu = CreateDummyMenu(&title); | |
429 | GetMenuBar()->Append(menu, title); | |
717a57c2 VZ |
430 | } |
431 | ||
432 | void MyFrame::OnToggleMenu(wxCommandEvent& WXUNUSED(event)) | |
433 | { | |
434 | wxMenuBar *mbar = GetMenuBar(); | |
435 | if ( !m_menu ) | |
436 | { | |
437 | // hide the menu | |
438 | m_menu = mbar->Remove(0); | |
439 | } | |
440 | else | |
441 | { | |
442 | // restore it | |
443 | mbar->Insert(0, m_menu, "&File"); | |
444 | m_menu = NULL; | |
445 | } | |
446 | } | |
447 | ||
448 | void MyFrame::OnEnableMenu(wxCommandEvent& WXUNUSED(event)) | |
449 | { | |
450 | wxMenuBar *mbar = GetMenuBar(); | |
451 | size_t count = mbar->GetMenuCount(); | |
452 | ||
453 | static bool s_enabled = TRUE; | |
454 | ||
455 | s_enabled = !s_enabled; | |
456 | mbar->EnableTop(count - 1, s_enabled); | |
457 | } | |
458 | ||
459 | void MyFrame::OnGetLabelMenu(wxCommandEvent& WXUNUSED(event)) | |
460 | { | |
461 | wxMenuBar *mbar = GetMenuBar(); | |
462 | size_t count = mbar->GetMenuCount(); | |
463 | ||
464 | wxLogMessage("The label of the last menu item is '%s'", | |
1987af7e | 465 | mbar->GetLabelTop(count - 1).c_str()); |
717a57c2 VZ |
466 | } |
467 | ||
468 | void MyFrame::OnSetLabelMenu(wxCommandEvent& WXUNUSED(event)) | |
469 | { | |
470 | wxMenuBar *mbar = GetMenuBar(); | |
471 | size_t count = mbar->GetMenuCount(); | |
472 | ||
473 | mbar->SetLabelTop(count - 1, "Dummy label &0"); | |
474 | } | |
475 | ||
476 | void MyFrame::OnDummy(wxCommandEvent& event) | |
477 | { | |
478 | wxString s; | |
479 | s.Printf("Dummy item #%d", event.GetId() - Menu_Dummy_First + 1); | |
480 | wxMessageBox(s, "Menu sample", wxICON_INFORMATION); | |
481 | } | |
482 | ||
483 | void MyFrame::OnAppendMenuItem(wxCommandEvent& WXUNUSED(event)) | |
484 | { | |
485 | wxMenuBar *menubar = GetMenuBar(); | |
486 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); | |
487 | ||
488 | menu->AppendSeparator(); | |
489 | menu->Append(Menu_Dummy_Third, "Third dummy item\tCtrl-F3", | |
490 | "Checkable item", TRUE); | |
491 | } | |
492 | ||
493 | void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event)) | |
494 | { | |
495 | wxMenuBar *menubar = GetMenuBar(); | |
f6bcfd97 | 496 | |
717a57c2 | 497 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); |
f6bcfd97 BP |
498 | |
499 | menu->Append(Menu_Dummy_Last, "Dummy sub menu", | |
500 | CreateDummyMenu(NULL), "Dummy sub menu help"); | |
717a57c2 VZ |
501 | } |
502 | ||
503 | void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event)) | |
504 | { | |
505 | wxMenuBar *menubar = GetMenuBar(); | |
506 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); | |
507 | ||
508 | size_t count = menu->GetMenuItemCount(); | |
509 | if ( !count ) | |
510 | { | |
511 | wxLogWarning("No items to delete!"); | |
512 | } | |
513 | else | |
514 | { | |
515 | menu->Destroy(menu->GetMenuItems().Item(count - 1)->GetData()); | |
516 | } | |
517 | } | |
518 | ||
519 | void MyFrame::OnInsertMenuItem(wxCommandEvent& WXUNUSED(event)) | |
520 | { | |
521 | wxMenuBar *menubar = GetMenuBar(); | |
522 | wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); | |
523 | ||
524 | menu->Insert(0, wxMenuItem::New(menu, Menu_Dummy_Fourth, | |
525 | "Fourth dummy item\tCtrl-F4")); | |
526 | menu->Insert(1, wxMenuItem::New(menu, wxID_SEPARATOR, "")); | |
527 | } | |
528 | ||
529 | void MyFrame::OnEnableMenuItem(wxCommandEvent& WXUNUSED(event)) | |
530 | { | |
531 | wxMenuItem *item = GetLastMenuItem(); | |
532 | ||
533 | if ( item ) | |
534 | { | |
535 | item->Enable(!item->IsEnabled()); | |
536 | } | |
537 | } | |
538 | ||
539 | void MyFrame::OnCheckMenuItem(wxCommandEvent& WXUNUSED(event)) | |
540 | { | |
541 | wxMenuItem *item = GetLastMenuItem(); | |
542 | ||
543 | item->Toggle(); | |
544 | } | |
545 | ||
546 | void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event) | |
547 | { | |
32db328c VZ |
548 | wxLogNull nolog; |
549 | ||
717a57c2 VZ |
550 | wxMenuItem *item = GetLastMenuItem(); |
551 | ||
552 | event.Enable(item && item->IsCheckable()); | |
553 | } | |
554 | ||
555 | void MyFrame::OnGetLabelMenuItem(wxCommandEvent& WXUNUSED(event)) | |
556 | { | |
557 | wxMenuItem *item = GetLastMenuItem(); | |
558 | ||
559 | if ( item ) | |
560 | { | |
561 | wxLogMessage("The label of the last menu item is '%s'", | |
1987af7e | 562 | item->GetLabel().c_str()); |
717a57c2 VZ |
563 | } |
564 | } | |
565 | ||
566 | void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event)) | |
567 | { | |
568 | wxMenuItem *item = GetLastMenuItem(); | |
569 | ||
570 | if ( item ) | |
571 | { | |
572 | item->SetText("Dummy menu item text"); | |
573 | } | |
574 | } | |
575 | ||
a80c322c VZ |
576 | void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event)) |
577 | { | |
578 | wxMenuItem *item = GetLastMenuItem(); | |
579 | ||
580 | if ( item ) | |
581 | { | |
582 | wxString msg; | |
583 | msg << "The item is " << (item->IsEnabled() ? "enabled" | |
584 | : "disabled") | |
585 | << '\n'; | |
f6bcfd97 | 586 | |
a80c322c VZ |
587 | if ( item->IsCheckable() ) |
588 | { | |
589 | msg << "It is checkable and " << (item->IsChecked() ? "" : "un") | |
590 | << "checked\n"; | |
591 | } | |
592 | ||
593 | #if wxUSE_ACCEL | |
594 | wxAcceleratorEntry *accel = item->GetAccel(); | |
595 | if ( accel ) | |
596 | { | |
597 | msg << "Its accelerator is "; | |
598 | ||
599 | int flags = accel->GetFlags(); | |
600 | if ( flags & wxACCEL_ALT ) | |
601 | msg << wxT("Alt-"); | |
602 | if ( flags & wxACCEL_CTRL ) | |
603 | msg << wxT("Ctrl-"); | |
604 | if ( flags & wxACCEL_SHIFT ) | |
605 | msg << wxT("Shift-"); | |
606 | ||
607 | int code = accel->GetKeyCode(); | |
608 | switch ( code ) | |
609 | { | |
610 | case WXK_F1: | |
611 | case WXK_F2: | |
612 | case WXK_F3: | |
613 | case WXK_F4: | |
614 | case WXK_F5: | |
615 | case WXK_F6: | |
616 | case WXK_F7: | |
617 | case WXK_F8: | |
618 | case WXK_F9: | |
619 | case WXK_F10: | |
620 | case WXK_F11: | |
621 | case WXK_F12: | |
622 | msg << wxT('F') << code - WXK_F1 + 1; | |
623 | break; | |
624 | ||
625 | // if there are any other keys wxGetAccelFromString() may return, | |
626 | // we should process them here | |
627 | ||
628 | default: | |
629 | if ( wxIsalnum(code) ) | |
630 | { | |
631 | msg << (wxChar)code; | |
632 | ||
633 | break; | |
634 | } | |
635 | ||
636 | wxFAIL_MSG( wxT("unknown keyboard accel") ); | |
637 | } | |
638 | ||
639 | delete accel; | |
640 | } | |
641 | else | |
642 | { | |
643 | msg << "It doesn't have an accelerator"; | |
644 | } | |
645 | #endif // wxUSE_ACCEL | |
646 | ||
647 | wxLogMessage(msg); | |
648 | } | |
649 | } | |
650 | ||
717a57c2 VZ |
651 | void MyFrame::OnRightDown(wxMouseEvent &event ) |
652 | { | |
653 | wxMenu menu("Test popup"); | |
654 | ||
655 | menu.Append(Menu_Help_About, "&About"); | |
f03ec224 | 656 | menu.Append(Menu_Popup_Submenu, "Submenu", CreateDummyMenu(NULL)); |
717a57c2 VZ |
657 | menu.Append(Menu_Popup_ToBeDeleted, "To be deleted"); |
658 | menu.Append(Menu_Popup_ToBeChecked, "To be checked", "", TRUE); | |
659 | menu.Append(Menu_Popup_ToBeGreyed, "To be greyed"); | |
660 | menu.AppendSeparator(); | |
661 | menu.Append(Menu_File_Quit, "E&xit"); | |
662 | ||
f6bcfd97 | 663 | menu.Delete(Menu_Popup_ToBeDeleted); |
717a57c2 VZ |
664 | menu.Check(Menu_Popup_ToBeChecked, TRUE); |
665 | menu.Enable(Menu_Popup_ToBeGreyed, FALSE); | |
666 | ||
667 | PopupMenu( &menu, event.GetX(), event.GetY() ); | |
f6bcfd97 BP |
668 | |
669 | // test for destroying items in popup menus | |
670 | #if 0 | |
671 | menu.Destroy(Menu_Popup_Submenu); | |
672 | ||
673 | PopupMenu( &menu, event.GetX(), event.GetY() ); | |
674 | #endif // 0 | |
717a57c2 | 675 | } |