]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: menu.cpp | |
3 | // Purpose: wxMenu, wxMenuBar, wxMenuItem | |
4 | // Author: Julian Smart | |
5 | // Modified by: Vadim Zeitlin | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // =========================================================================== | |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "menu.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/frame.h" | |
33 | #include "wx/menu.h" | |
34 | #include "wx/utils.h" | |
35 | #include "wx/intl.h" | |
36 | #endif | |
37 | ||
38 | #if wxUSE_OWNER_DRAWN | |
39 | #include "wx/ownerdrw.h" | |
40 | #endif | |
41 | ||
42 | #include "wx/msw/private.h" | |
43 | #include "wx/msw/menu.h" | |
44 | #include "wx/menuitem.h" | |
45 | #include "wx/log.h" | |
46 | ||
47 | // other standard headers | |
48 | #include <string.h> | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // global variables | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | extern wxMenu *wxCurrentPopupMenu; | |
55 | ||
56 | // ---------------------------------------------------------------------------- | |
57 | // constants | |
58 | // ---------------------------------------------------------------------------- | |
59 | ||
60 | // the (popup) menu title has this special id | |
61 | static const int idMenuTitle = -2; | |
62 | ||
63 | // ---------------------------------------------------------------------------- | |
64 | // macros | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
67 | #if !USE_SHARED_LIBRARY | |
68 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) | |
69 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) | |
70 | #endif | |
71 | ||
72 | // ============================================================================ | |
73 | // implementation | |
74 | // ============================================================================ | |
75 | ||
76 | // --------------------------------------------------------------------------- | |
77 | // wxMenu construction, adding and removing menu items | |
78 | // --------------------------------------------------------------------------- | |
79 | ||
80 | // Construct a menu with optional title (then use append) | |
81 | wxMenu::wxMenu(const wxString& title, const wxFunction func) | |
82 | : m_title(title) | |
83 | { | |
84 | m_parent = NULL; | |
85 | m_eventHandler = this; | |
86 | m_pInvokingWindow = NULL; | |
87 | m_doBreak = FALSE ; | |
88 | m_noItems = 0; | |
89 | m_menuBar = NULL; | |
90 | m_hMenu = (WXHMENU) CreatePopupMenu(); | |
91 | m_savehMenu = 0 ; | |
92 | m_topLevelMenu = this; | |
93 | m_clientData = (void*) NULL; | |
94 | ||
95 | if ( !!m_title ) | |
96 | { | |
97 | Append(idMenuTitle, m_title) ; | |
98 | AppendSeparator() ; | |
99 | } | |
100 | ||
101 | #if WXWIN_COMPATIBILITY | |
102 | Callback(func); | |
103 | #endif | |
104 | } | |
105 | ||
106 | // The wxWindow destructor will take care of deleting the submenus. | |
107 | wxMenu::~wxMenu() | |
108 | { | |
109 | // free Windows resources | |
110 | if ( m_hMenu ) | |
111 | { | |
112 | ::DestroyMenu((HMENU)m_hMenu); | |
113 | m_hMenu = 0; | |
114 | } | |
115 | ||
116 | // delete submenus | |
117 | wxNode *node = m_menuItems.First(); | |
118 | while ( node ) | |
119 | { | |
120 | wxMenuItem *item = (wxMenuItem *)node->Data(); | |
121 | ||
122 | // Delete child menus. | |
123 | // Beware: they must not be appended to children list!!! | |
124 | // (because order of delete is significant) | |
125 | if ( item->IsSubMenu() ) | |
126 | item->DeleteSubMenu(); | |
127 | ||
128 | wxNode *next = node->Next(); | |
129 | delete item; | |
130 | delete node; | |
131 | node = next; | |
132 | } | |
133 | } | |
134 | ||
135 | void wxMenu::Break() | |
136 | { | |
137 | m_doBreak = TRUE; | |
138 | } | |
139 | ||
140 | // function appends a new item or submenu to the menu | |
141 | void wxMenu::Append(wxMenuItem *pItem) | |
142 | { | |
143 | wxCHECK_RET( pItem != NULL, _T("can't append NULL item to the menu") ); | |
144 | ||
145 | #if wxUSE_ACCEL | |
146 | // check for accelerators: they are given after '\t' | |
147 | wxString label = pItem->GetName(); | |
148 | int posTab = label.Find(_T('\t')); | |
149 | if ( posTab != wxNOT_FOUND ) { | |
150 | // parse the accelerator string | |
151 | int keyCode = 0; | |
152 | int accelFlags = wxACCEL_NORMAL; | |
153 | wxString current; | |
154 | for ( size_t n = (size_t)posTab + 1; n < label.Len(); n++ ) { | |
155 | if ( (label[n] == '+') || (label[n] == '-') ) { | |
156 | if ( current == _("ctrl") ) | |
157 | accelFlags |= wxACCEL_CTRL; | |
158 | else if ( current == _("alt") ) | |
159 | accelFlags |= wxACCEL_ALT; | |
160 | else if ( current == _("shift") ) | |
161 | accelFlags |= wxACCEL_SHIFT; | |
162 | else { | |
163 | wxLogDebug(_T("Unknown accel modifier: '%s'"), | |
164 | current.c_str()); | |
165 | } | |
166 | ||
167 | current.Empty(); | |
168 | } | |
169 | else { | |
170 | current += wxTolower(label[n]); | |
171 | } | |
172 | } | |
173 | ||
174 | if ( current.IsEmpty() ) { | |
175 | wxLogDebug(_T("No accel key found, accel string ignored.")); | |
176 | } | |
177 | else { | |
178 | if ( current.Len() == 1 ) { | |
179 | // it's a letter | |
180 | keyCode = wxToupper(current[0U]); | |
181 | } | |
182 | else { | |
183 | // it should be a function key | |
184 | if ( current[0U] == 'f' && isdigit(current[1U]) && | |
185 | (current.Len() == 2 || | |
186 | (current.Len() == 3 && isdigit(current[2U]))) ) { | |
187 | int n; | |
188 | wxSscanf(current.c_str() + 1, _T("%d"), &n); | |
189 | ||
190 | keyCode = VK_F1 + n - 1; | |
191 | } | |
192 | else { | |
193 | wxLogDebug(_T("Unrecognized accel key '%s', accel " | |
194 | "string ignored."), current.c_str()); | |
195 | } | |
196 | } | |
197 | } | |
198 | ||
199 | if ( keyCode ) { | |
200 | // do add an entry | |
201 | m_accelKeyCodes.Add(keyCode); | |
202 | m_accelFlags.Add(accelFlags); | |
203 | m_accelIds.Add(pItem->GetId()); | |
204 | } | |
205 | } | |
206 | #endif // wxUSE_ACCEL | |
207 | ||
208 | UINT flags = 0; | |
209 | ||
210 | // if "Break" has just been called, insert a menu break before this item | |
211 | // (and don't forget to reset the flag) | |
212 | if ( m_doBreak ) { | |
213 | flags |= MF_MENUBREAK; | |
214 | m_doBreak = FALSE; | |
215 | } | |
216 | ||
217 | if ( pItem->IsSeparator() ) { | |
218 | flags |= MF_SEPARATOR; | |
219 | } | |
220 | ||
221 | // id is the numeric id for normal menu items and HMENU for submenus as | |
222 | // required by ::AppendMenu() API | |
223 | UINT id; | |
224 | wxMenu *submenu = pItem->GetSubMenu(); | |
225 | if ( submenu != NULL ) { | |
226 | wxASSERT( submenu->GetHMenu() != (WXHMENU) NULL ); | |
227 | ||
228 | id = (UINT)submenu->GetHMenu(); | |
229 | submenu->m_topLevelMenu = m_topLevelMenu; | |
230 | submenu->m_parent = this; | |
231 | submenu->m_savehMenu = (WXHMENU)id; | |
232 | submenu->m_hMenu = 0; | |
233 | ||
234 | flags |= MF_POPUP; | |
235 | } | |
236 | else { | |
237 | id = pItem->GetId(); | |
238 | } | |
239 | ||
240 | LPCTSTR pData; | |
241 | ||
242 | #if wxUSE_OWNER_DRAWN | |
243 | if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages? | |
244 | // item draws itself, pass pointer to it in data parameter | |
245 | flags |= MF_OWNERDRAW; | |
246 | pData = (LPCTSTR)pItem; | |
247 | } | |
248 | else | |
249 | #endif | |
250 | { | |
251 | // menu is just a normal string (passed in data parameter) | |
252 | flags |= MF_STRING; | |
253 | pData = label; | |
254 | } | |
255 | ||
256 | if ( !::AppendMenu(GetHmenu(), flags, id, pData) ) | |
257 | { | |
258 | wxLogLastError("AppendMenu"); | |
259 | } | |
260 | else | |
261 | { | |
262 | #ifdef __WIN32__ | |
263 | if ( id == idMenuTitle ) | |
264 | { | |
265 | // visually select the menu title | |
266 | MENUITEMINFO mii; | |
267 | mii.cbSize = sizeof(mii); | |
268 | mii.fMask = MIIM_STATE; | |
269 | mii.fState = MFS_DEFAULT; | |
270 | ||
271 | if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) ) | |
272 | { | |
273 | wxLogLastError(_T("SetMenuItemInfo")); | |
274 | } | |
275 | } | |
276 | #endif // __WIN32__ | |
277 | ||
278 | m_menuItems.Append(pItem); | |
279 | m_noItems++; | |
280 | } | |
281 | } | |
282 | ||
283 | void wxMenu::AppendSeparator() | |
284 | { | |
285 | Append(new wxMenuItem(this, ID_SEPARATOR)); | |
286 | } | |
287 | ||
288 | // Pullright item | |
289 | void wxMenu::Append(int id, | |
290 | const wxString& label, | |
291 | wxMenu *SubMenu, | |
292 | const wxString& helpString) | |
293 | { | |
294 | Append(new wxMenuItem(this, id, label, helpString, FALSE, SubMenu)); | |
295 | } | |
296 | ||
297 | // Ordinary menu item | |
298 | void wxMenu::Append(int id, | |
299 | const wxString& label, | |
300 | const wxString& helpString, | |
301 | bool checkable) | |
302 | { | |
303 | // 'checkable' parameter is useless for Windows. | |
304 | Append(new wxMenuItem(this, id, label, helpString, checkable)); | |
305 | } | |
306 | ||
307 | // delete item by id | |
308 | void wxMenu::Delete(int id) | |
309 | { | |
310 | wxMenuItem *item = NULL; | |
311 | int pos; | |
312 | wxNode *node; | |
313 | for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++) | |
314 | { | |
315 | item = (wxMenuItem *)node->Data(); | |
316 | if ( item->GetId() == id ) | |
317 | break; | |
318 | } | |
319 | ||
320 | wxCHECK_RET( node, _T("wxMenu::Delete(): item doesn't exist") ); | |
321 | ||
322 | HMENU menu = GetHmenu(); | |
323 | ||
324 | wxMenu *pSubMenu = item->GetSubMenu(); | |
325 | if ( pSubMenu != NULL ) { | |
326 | RemoveMenu(menu, (UINT)pos, MF_BYPOSITION); | |
327 | pSubMenu->m_hMenu = pSubMenu->m_savehMenu; | |
328 | pSubMenu->m_savehMenu = 0; | |
329 | pSubMenu->m_parent = NULL; | |
330 | // RemoveChild(item->subMenu); | |
331 | pSubMenu->m_topLevelMenu = NULL; | |
332 | // TODO: Why isn't subMenu deleted here??? | |
333 | // Will put this in for now. Assuming this is supposed | |
334 | // to delete the menu, not just remove it. | |
335 | item->DeleteSubMenu(); | |
336 | } | |
337 | else { | |
338 | DeleteMenu(menu, (UINT)pos, MF_BYPOSITION); | |
339 | } | |
340 | ||
341 | m_menuItems.DeleteNode(node); | |
342 | delete item; | |
343 | } | |
344 | ||
345 | #if wxUSE_ACCEL | |
346 | ||
347 | // --------------------------------------------------------------------------- | |
348 | // accelerator helpers | |
349 | // --------------------------------------------------------------------------- | |
350 | ||
351 | // create the wxAcceleratorEntries for our accels and put them into provided | |
352 | // array - return the number of accels we have | |
353 | size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const | |
354 | { | |
355 | size_t count = GetAccelCount(); | |
356 | for ( size_t n = 0; n < count; n++ ) | |
357 | { | |
358 | (*accels++).Set(m_accelFlags[n], m_accelKeyCodes[n], m_accelIds[n]); | |
359 | } | |
360 | ||
361 | return count; | |
362 | } | |
363 | ||
364 | #endif // wxUSE_ACCEL | |
365 | ||
366 | // --------------------------------------------------------------------------- | |
367 | // wxMenu functions implemented in wxMenuItem | |
368 | // --------------------------------------------------------------------------- | |
369 | ||
370 | void wxMenu::Enable(int id, bool Flag) | |
371 | { | |
372 | wxMenuItem *item = FindItemForId(id); | |
373 | wxCHECK_RET( item != NULL, _T("can't enable non-existing menu item") ); | |
374 | ||
375 | item->Enable(Flag); | |
376 | } | |
377 | ||
378 | bool wxMenu::IsEnabled(int id) const | |
379 | { | |
380 | wxMenuItem *item = FindItemForId(id); | |
381 | wxCHECK_MSG( item != NULL, FALSE, _T("invalid item id") ); | |
382 | ||
383 | return item->IsEnabled(); | |
384 | } | |
385 | ||
386 | void wxMenu::Check(int id, bool Flag) | |
387 | { | |
388 | wxMenuItem *item = FindItemForId(id); | |
389 | wxCHECK_RET( item != NULL, _T("can't get status of non-existing menu item") ); | |
390 | ||
391 | item->Check(Flag); | |
392 | } | |
393 | ||
394 | bool wxMenu::IsChecked(int id) const | |
395 | { | |
396 | wxMenuItem *item = FindItemForId(id); | |
397 | wxCHECK_MSG( item != NULL, FALSE, _T("invalid item id") ); | |
398 | ||
399 | return item->IsChecked(); | |
400 | } | |
401 | ||
402 | void wxMenu::SetLabel(int id, const wxString& label) | |
403 | { | |
404 | wxMenuItem *item = FindItemForId(id) ; | |
405 | wxCHECK_RET( item, _T("wxMenu::SetLabel: no such item") ); | |
406 | ||
407 | item->SetName(label); | |
408 | } | |
409 | ||
410 | wxString wxMenu::GetLabel(int id) const | |
411 | { | |
412 | wxString label; | |
413 | wxMenuItem *pItem = FindItemForId(id) ; | |
414 | if (pItem) | |
415 | label = pItem->GetName() ; | |
416 | else | |
417 | wxFAIL_MSG(_T("wxMenu::GetLabel: item doesn't exist")); | |
418 | ||
419 | return label; | |
420 | } | |
421 | ||
422 | void wxMenu::SetHelpString(int itemId, const wxString& helpString) | |
423 | { | |
424 | wxMenuItem *item = FindItemForId (itemId); | |
425 | if (item) | |
426 | item->SetHelp(helpString); | |
427 | else | |
428 | wxFAIL_MSG(_T("wxMenu::SetHelpString: item doesn't exist")); | |
429 | } | |
430 | ||
431 | wxString wxMenu::GetHelpString (int itemId) const | |
432 | { | |
433 | wxString help; | |
434 | wxMenuItem *item = FindItemForId (itemId); | |
435 | if (item) | |
436 | help = item->GetHelp(); | |
437 | else | |
438 | wxFAIL_MSG(_T("wxMenu::GetHelpString: item doesn't exist")); | |
439 | ||
440 | return help; | |
441 | } | |
442 | ||
443 | // --------------------------------------------------------------------------- | |
444 | // wxMenu title | |
445 | // --------------------------------------------------------------------------- | |
446 | ||
447 | void wxMenu::SetTitle(const wxString& label) | |
448 | { | |
449 | bool hasNoTitle = m_title.IsEmpty(); | |
450 | m_title = label; | |
451 | ||
452 | HMENU hMenu = GetHmenu(); | |
453 | ||
454 | if ( hasNoTitle ) | |
455 | { | |
456 | if ( !label.IsEmpty() ) | |
457 | { | |
458 | if ( !InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING, | |
459 | (unsigned)idMenuTitle, m_title) || | |
460 | !InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) ) | |
461 | { | |
462 | wxLogLastError(_T("InsertMenu")); | |
463 | } | |
464 | } | |
465 | } | |
466 | else | |
467 | { | |
468 | if ( label.IsEmpty() ) | |
469 | { | |
470 | // remove the title and the separator after it | |
471 | if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) || | |
472 | !RemoveMenu(hMenu, 0, MF_BYPOSITION) ) | |
473 | { | |
474 | wxLogLastError("RemoveMenu"); | |
475 | } | |
476 | } | |
477 | else | |
478 | { | |
479 | // modify the title | |
480 | if ( !ModifyMenu(hMenu, 0u, | |
481 | MF_BYPOSITION | MF_STRING, | |
482 | (unsigned)idMenuTitle, m_title) ) | |
483 | { | |
484 | wxLogLastError("ModifyMenu"); | |
485 | } | |
486 | } | |
487 | } | |
488 | ||
489 | #ifdef __WIN32__ | |
490 | // put the title string in bold face | |
491 | if ( !m_title.IsEmpty() ) | |
492 | { | |
493 | MENUITEMINFO mii; | |
494 | mii.cbSize = sizeof(mii); | |
495 | mii.fMask = MIIM_STATE; | |
496 | mii.fState = MFS_DEFAULT; | |
497 | ||
498 | if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) ) | |
499 | { | |
500 | wxLogLastError("SetMenuItemInfo"); | |
501 | } | |
502 | } | |
503 | #endif | |
504 | } | |
505 | ||
506 | const wxString wxMenu::GetTitle() const | |
507 | { | |
508 | return m_title; | |
509 | } | |
510 | ||
511 | // --------------------------------------------------------------------------- | |
512 | // event processing | |
513 | // --------------------------------------------------------------------------- | |
514 | ||
515 | bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id) | |
516 | { | |
517 | // ignore commands from the menu title | |
518 | ||
519 | // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!! | |
520 | if ( id != (WXWORD)idMenuTitle ) | |
521 | { | |
522 | wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED); | |
523 | event.SetEventObject( this ); | |
524 | event.SetId( id ); | |
525 | event.SetInt( id ); | |
526 | ProcessCommand(event); | |
527 | } | |
528 | ||
529 | return TRUE; | |
530 | } | |
531 | ||
532 | bool wxMenu::ProcessCommand(wxCommandEvent & event) | |
533 | { | |
534 | bool processed = FALSE; | |
535 | ||
536 | #if WXWIN_COMPATIBILITY | |
537 | // Try a callback | |
538 | if (m_callback) | |
539 | { | |
540 | (void)(*(m_callback))(*this, event); | |
541 | processed = TRUE; | |
542 | } | |
543 | #endif // WXWIN_COMPATIBILITY | |
544 | ||
545 | // Try the menu's event handler | |
546 | if ( !processed && GetEventHandler()) | |
547 | { | |
548 | processed = GetEventHandler()->ProcessEvent(event); | |
549 | } | |
550 | ||
551 | // Try the window the menu was popped up from (and up through the | |
552 | // hierarchy) | |
553 | wxWindow *win = GetInvokingWindow(); | |
554 | if ( !processed && win ) | |
555 | processed = win->GetEventHandler()->ProcessEvent(event); | |
556 | ||
557 | return processed; | |
558 | } | |
559 | ||
560 | // --------------------------------------------------------------------------- | |
561 | // Item search | |
562 | // --------------------------------------------------------------------------- | |
563 | ||
564 | // Finds the item id matching the given string, -1 if not found. | |
565 | int wxMenu::FindItem (const wxString& itemString) const | |
566 | { | |
567 | wxString itemLabel = wxStripMenuCodes(itemString); | |
568 | for ( wxNode *node = m_menuItems.First(); node; node = node->Next() ) | |
569 | { | |
570 | wxMenuItem *item = (wxMenuItem *)node->Data(); | |
571 | if ( item->IsSubMenu() ) | |
572 | { | |
573 | int ans = item->GetSubMenu()->FindItem(itemString); | |
574 | if ( ans != wxNOT_FOUND ) | |
575 | return ans; | |
576 | } | |
577 | else if ( !item->IsSeparator() ) | |
578 | { | |
579 | wxString label = wxStripMenuCodes(item->GetName()); | |
580 | if ( itemLabel == label ) | |
581 | return item->GetId(); | |
582 | } | |
583 | } | |
584 | ||
585 | return wxNOT_FOUND; | |
586 | } | |
587 | ||
588 | wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const | |
589 | { | |
590 | if ( itemMenu ) | |
591 | *itemMenu = NULL; | |
592 | ||
593 | wxMenuItem *item = NULL; | |
594 | for ( wxNode *node = m_menuItems.First(); node && !item; node = node->Next() ) | |
595 | { | |
596 | item = (wxMenuItem *)node->Data(); | |
597 | ||
598 | if ( item->GetId() == itemId ) | |
599 | { | |
600 | if (itemMenu) | |
601 | *itemMenu = (wxMenu *)this; | |
602 | } | |
603 | else if ( item->IsSubMenu() ) | |
604 | { | |
605 | item = item->GetSubMenu()->FindItemForId(itemId, itemMenu); | |
606 | } | |
607 | else | |
608 | { | |
609 | // don't exit the loop | |
610 | item = NULL; | |
611 | } | |
612 | } | |
613 | ||
614 | return item; | |
615 | } | |
616 | ||
617 | // --------------------------------------------------------------------------- | |
618 | // other | |
619 | // --------------------------------------------------------------------------- | |
620 | ||
621 | void wxMenu::Attach(wxMenuBar *menubar) | |
622 | { | |
623 | // menu can be in at most one menubar because otherwise they would both | |
624 | // delete the menu pointer | |
625 | wxASSERT_MSG( !m_menuBar, _T("menu belongs to 2 menubars, expect a crash") ); | |
626 | ||
627 | m_menuBar = menubar; | |
628 | m_savehMenu = m_hMenu; | |
629 | m_hMenu = 0; | |
630 | } | |
631 | ||
632 | void wxMenu::Detach() | |
633 | { | |
634 | wxASSERT_MSG( m_menuBar, _T("can't detach menu if it's not attached") ); | |
635 | ||
636 | m_hMenu = m_savehMenu; | |
637 | m_savehMenu = 0; | |
638 | } | |
639 | ||
640 | // --------------------------------------------------------------------------- | |
641 | // Menu Bar | |
642 | // --------------------------------------------------------------------------- | |
643 | ||
644 | void wxMenuBar::Init() | |
645 | { | |
646 | m_eventHandler = this; | |
647 | m_menuCount = 0; | |
648 | m_menus = NULL; | |
649 | m_titles = NULL; | |
650 | m_menuBarFrame = NULL; | |
651 | m_hMenu = 0; | |
652 | } | |
653 | ||
654 | wxMenuBar::wxMenuBar() | |
655 | { | |
656 | Init(); | |
657 | } | |
658 | ||
659 | wxMenuBar::wxMenuBar( long WXUNUSED(style) ) | |
660 | { | |
661 | Init(); | |
662 | } | |
663 | ||
664 | wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[]) | |
665 | { | |
666 | Init(); | |
667 | ||
668 | m_menuCount = count; | |
669 | m_menus = menus; | |
670 | m_titles = new wxString[count]; | |
671 | ||
672 | int i; | |
673 | for ( i = 0; i < count; i++ ) | |
674 | m_titles[i] = titles[i]; | |
675 | ||
676 | for ( i = 0; i < count; i++ ) | |
677 | m_menus[i]->Attach(this); | |
678 | } | |
679 | ||
680 | wxMenuBar::~wxMenuBar() | |
681 | { | |
682 | for ( int i = 0; i < m_menuCount; i++ ) | |
683 | { | |
684 | delete m_menus[i]; | |
685 | } | |
686 | ||
687 | delete[] m_menus; | |
688 | delete[] m_titles; | |
689 | } | |
690 | ||
691 | // --------------------------------------------------------------------------- | |
692 | // wxMenuBar helpers | |
693 | // --------------------------------------------------------------------------- | |
694 | ||
695 | void wxMenuBar::Refresh() | |
696 | { | |
697 | wxCHECK_RET( m_menuBarFrame, _T("can't refresh a menubar withotu a frame") ); | |
698 | ||
699 | DrawMenuBar((HWND)m_menuBarFrame->GetHWND()) ; | |
700 | } | |
701 | ||
702 | WXHMENU wxMenuBar::Create() | |
703 | { | |
704 | wxCHECK_MSG( !m_hMenu, TRUE, _T("menubar already created") ); | |
705 | ||
706 | m_hMenu = (WXHMENU)::CreateMenu(); | |
707 | ||
708 | if ( !m_hMenu ) | |
709 | { | |
710 | wxLogLastError("CreateMenu"); | |
711 | } | |
712 | else | |
713 | { | |
714 | for ( int i = 0; i < m_menuCount; i++ ) | |
715 | { | |
716 | if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING, | |
717 | (UINT)m_menus[i]->GetHMenu(), | |
718 | m_titles[i]) ) | |
719 | { | |
720 | wxLogLastError("AppendMenu"); | |
721 | } | |
722 | } | |
723 | } | |
724 | ||
725 | return m_hMenu; | |
726 | } | |
727 | ||
728 | // --------------------------------------------------------------------------- | |
729 | // wxMenuBar functions forwarded to wxMenuItem | |
730 | // --------------------------------------------------------------------------- | |
731 | ||
732 | // Must only be used AFTER menu has been attached to frame, | |
733 | // otherwise use individual menus to enable/disable items | |
734 | void wxMenuBar::Enable(int id, bool enable) | |
735 | { | |
736 | wxMenu *itemMenu = NULL; | |
737 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
738 | ||
739 | wxCHECK_RET( item, _T("attempt to enable an item which doesn't exist") ); | |
740 | ||
741 | item->Enable(enable); | |
742 | } | |
743 | ||
744 | void wxMenuBar::EnableTop(int pos, bool enable) | |
745 | { | |
746 | int flag = enable ? MF_ENABLED : MF_GRAYED;; | |
747 | ||
748 | EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag); | |
749 | } | |
750 | ||
751 | // Must only be used AFTER menu has been attached to frame, | |
752 | // otherwise use individual menus | |
753 | void wxMenuBar::Check(int id, bool check) | |
754 | { | |
755 | wxMenu *itemMenu = NULL; | |
756 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
757 | ||
758 | wxCHECK_RET( item, _T("attempt to check an item which doesn't exist") ); | |
759 | wxCHECK_RET( item->IsCheckable(), _T("attempt to check an uncheckable item") ); | |
760 | ||
761 | item->Check(check); | |
762 | } | |
763 | ||
764 | bool wxMenuBar::IsChecked(int id) const | |
765 | { | |
766 | wxMenu *itemMenu = NULL; | |
767 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
768 | ||
769 | wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsChecked(): no such item") ); | |
770 | ||
771 | int flag = ::GetMenuState(GetHmenuOf(itemMenu), id, MF_BYCOMMAND); | |
772 | ||
773 | return (flag & MF_CHECKED) != 0; | |
774 | } | |
775 | ||
776 | bool wxMenuBar::IsEnabled(int id) const | |
777 | { | |
778 | wxMenu *itemMenu = NULL; | |
779 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
780 | ||
781 | wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsEnabled(): no such item") ); | |
782 | ||
783 | int flag = ::GetMenuState(GetHmenuOf(itemMenu), id, MF_BYCOMMAND) ; | |
784 | ||
785 | return (flag & MF_ENABLED) != 0; | |
786 | } | |
787 | ||
788 | void wxMenuBar::SetLabel(int id, const wxString& label) | |
789 | { | |
790 | wxMenu *itemMenu = NULL; | |
791 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
792 | ||
793 | wxCHECK_RET( item, _T("wxMenuBar::SetLabel(): no such item") ); | |
794 | ||
795 | item->SetName(label); | |
796 | } | |
797 | ||
798 | wxString wxMenuBar::GetLabel(int id) const | |
799 | { | |
800 | wxMenu *itemMenu = NULL; | |
801 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
802 | ||
803 | wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetLabel(): no such item") ); | |
804 | ||
805 | return item->GetName(); | |
806 | } | |
807 | ||
808 | void wxMenuBar::SetHelpString (int id, const wxString& helpString) | |
809 | { | |
810 | wxMenu *itemMenu = NULL; | |
811 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
812 | ||
813 | wxCHECK_RET( item, _T("wxMenuBar::SetHelpString(): no such item") ); | |
814 | ||
815 | item->SetHelp(helpString); | |
816 | } | |
817 | ||
818 | wxString wxMenuBar::GetHelpString (int id) const | |
819 | { | |
820 | wxMenu *itemMenu = NULL; | |
821 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
822 | ||
823 | wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetHelpString(): no such item") ); | |
824 | ||
825 | return item->GetHelp(); | |
826 | } | |
827 | ||
828 | // --------------------------------------------------------------------------- | |
829 | // wxMenuBar functions to work with the top level submenus | |
830 | // --------------------------------------------------------------------------- | |
831 | ||
832 | // NB: we don't support owner drawn top level items for now, if we do these | |
833 | // functions would have to be changed to use wxMenuItem as well | |
834 | ||
835 | void wxMenuBar::SetLabelTop(int pos, const wxString& label) | |
836 | { | |
837 | UINT id; | |
838 | UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION); | |
839 | if ( flagsOld == 0xFFFFFFFF ) | |
840 | { | |
841 | wxLogLastError(_T("GetMenuState")); | |
842 | ||
843 | return; | |
844 | } | |
845 | ||
846 | if ( flagsOld & MF_POPUP ) | |
847 | { | |
848 | // HIBYTE contains the number of items in the submenu in this case | |
849 | flagsOld &= 0xff ; | |
850 | id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos) ; | |
851 | } | |
852 | else | |
853 | { | |
854 | id = pos; | |
855 | } | |
856 | ||
857 | if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld, | |
858 | id, label) == 0xFFFFFFFF ) | |
859 | { | |
860 | wxLogLastError("ModifyMenu"); | |
861 | } | |
862 | } | |
863 | ||
864 | wxString wxMenuBar::GetLabelTop(int pos) const | |
865 | { | |
866 | int len = ::GetMenuString((HMENU)m_hMenu, pos, NULL, 0, MF_BYCOMMAND); | |
867 | ||
868 | len++; // for the NUL character | |
869 | wxString label; | |
870 | ::GetMenuString(GetHmenu(), pos, label.GetWriteBuf(len), len, MF_BYCOMMAND); | |
871 | label.UngetWriteBuf(); | |
872 | ||
873 | return label; | |
874 | } | |
875 | ||
876 | // --------------------------------------------------------------------------- | |
877 | // wxMenuBar notifications | |
878 | // --------------------------------------------------------------------------- | |
879 | ||
880 | bool wxMenuBar::OnDelete(wxMenu *a_menu, int pos) | |
881 | { | |
882 | if ( !m_menuBarFrame ) | |
883 | return TRUE; | |
884 | ||
885 | if ( ::RemoveMenu((HMENU)m_hMenu, (UINT)pos, MF_BYPOSITION) ) | |
886 | { | |
887 | // VZ: I'm not sure about what's going on here, so I leave an assert | |
888 | wxASSERT_MSG( m_menus[pos] == a_menu, _T("what is this parameter for??") ); | |
889 | ||
890 | a_menu->Detach(); | |
891 | ||
892 | if ( m_menuBarFrame ) | |
893 | Refresh(); | |
894 | ||
895 | return TRUE; | |
896 | } | |
897 | else | |
898 | { | |
899 | wxLogLastError("RemoveMenu"); | |
900 | } | |
901 | ||
902 | return FALSE; | |
903 | } | |
904 | ||
905 | bool wxMenuBar::OnAppend(wxMenu *a_menu, const wxChar *title) | |
906 | { | |
907 | WXHMENU submenu = a_menu->GetHMenu(); | |
908 | if ( !submenu ) | |
909 | return FALSE; | |
910 | ||
911 | if ( !m_menuBarFrame ) | |
912 | return TRUE; | |
913 | ||
914 | a_menu->Attach(this); | |
915 | ||
916 | if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING, | |
917 | (UINT)submenu, title) ) | |
918 | { | |
919 | wxLogLastError(_T("AppendMenu")); | |
920 | } | |
921 | ||
922 | Refresh(); | |
923 | ||
924 | return TRUE; | |
925 | } | |
926 | ||
927 | // --------------------------------------------------------------------------- | |
928 | // wxMenuBar construction | |
929 | // --------------------------------------------------------------------------- | |
930 | ||
931 | void wxMenuBar::Append (wxMenu * menu, const wxString& title) | |
932 | { | |
933 | if (!OnAppend(menu, title)) | |
934 | return; | |
935 | ||
936 | m_menuCount ++; | |
937 | wxMenu **new_menus = new wxMenu *[m_menuCount]; | |
938 | wxString *new_titles = new wxString[m_menuCount]; | |
939 | int i; | |
940 | ||
941 | for (i = 0; i < m_menuCount - 1; i++) | |
942 | { | |
943 | new_menus[i] = m_menus[i]; | |
944 | m_menus[i] = NULL; | |
945 | new_titles[i] = m_titles[i]; | |
946 | m_titles[i] = _T(""); | |
947 | } | |
948 | if (m_menus) | |
949 | { | |
950 | delete[]m_menus; | |
951 | delete[]m_titles; | |
952 | } | |
953 | m_menus = new_menus; | |
954 | m_titles = new_titles; | |
955 | ||
956 | m_menus[m_menuCount - 1] = (wxMenu *)menu; | |
957 | m_titles[m_menuCount - 1] = title; | |
958 | ||
959 | menu->SetParent(this); | |
960 | } | |
961 | ||
962 | void wxMenuBar::Delete(wxMenu * menu, int i) | |
963 | { | |
964 | int j; | |
965 | int ii = (int) i; | |
966 | ||
967 | if (menu != 0) { | |
968 | for (ii = 0; ii < m_menuCount; ii++) { | |
969 | if (m_menus[ii] == menu) | |
970 | break; | |
971 | } | |
972 | if (ii >= m_menuCount) | |
973 | return; | |
974 | } else { | |
975 | if (ii < 0 || ii >= m_menuCount) | |
976 | return; | |
977 | menu = m_menus[ii]; | |
978 | } | |
979 | ||
980 | if (!OnDelete(menu, ii)) | |
981 | return; | |
982 | ||
983 | menu->SetParent(NULL); | |
984 | ||
985 | -- m_menuCount; | |
986 | for (j = ii; j < m_menuCount; j++) { | |
987 | m_menus[j] = m_menus[j + 1]; | |
988 | m_titles[j] = m_titles[j + 1]; | |
989 | } | |
990 | } | |
991 | ||
992 | void wxMenuBar::Attach(wxFrame *frame) | |
993 | { | |
994 | wxASSERT_MSG( !m_menuBarFrame, _T("menubar already attached!") ); | |
995 | ||
996 | m_menuBarFrame = frame; | |
997 | ||
998 | #if wxUSE_ACCEL | |
999 | // create the accel table - we consider that the menubar construction is | |
1000 | // finished | |
1001 | size_t nAccelCount = 0; | |
1002 | int i; | |
1003 | for ( i = 0; i < m_menuCount; i++ ) | |
1004 | { | |
1005 | nAccelCount += m_menus[i]->GetAccelCount(); | |
1006 | } | |
1007 | ||
1008 | if ( nAccelCount ) | |
1009 | { | |
1010 | wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount]; | |
1011 | ||
1012 | nAccelCount = 0; | |
1013 | for ( i = 0; i < m_menuCount; i++ ) | |
1014 | { | |
1015 | nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]); | |
1016 | } | |
1017 | ||
1018 | m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries); | |
1019 | ||
1020 | delete [] accelEntries; | |
1021 | } | |
1022 | #endif // wxUSE_ACCEL | |
1023 | } | |
1024 | ||
1025 | // --------------------------------------------------------------------------- | |
1026 | // wxMenuBar searching for menu items | |
1027 | // --------------------------------------------------------------------------- | |
1028 | ||
1029 | // Find the itemString in menuString, and return the item id or wxNOT_FOUND | |
1030 | int wxMenuBar::FindMenuItem(const wxString& menuString, | |
1031 | const wxString& itemString) const | |
1032 | { | |
1033 | wxString menuLabel = wxStripMenuCodes(menuString); | |
1034 | for ( int i = 0; i < m_menuCount; i++ ) | |
1035 | { | |
1036 | wxString title = wxStripMenuCodes(m_titles[i]); | |
1037 | if ( menuString == title ) | |
1038 | return m_menus[i]->FindItem(itemString); | |
1039 | } | |
1040 | ||
1041 | return wxNOT_FOUND; | |
1042 | } | |
1043 | ||
1044 | wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu **itemMenu) const | |
1045 | { | |
1046 | if ( itemMenu ) | |
1047 | *itemMenu = NULL; | |
1048 | ||
1049 | wxMenuItem *item = NULL; | |
1050 | for ( int i = 0; !item && (i < m_menuCount); i++ ) | |
1051 | { | |
1052 | item = m_menus[i]->FindItemForId(id, itemMenu); | |
1053 | } | |
1054 | ||
1055 | return item; | |
1056 | } | |
1057 | ||
1058 | ||
1059 | // ---------------------------------------------------------------------------- | |
1060 | // helper functions | |
1061 | // ---------------------------------------------------------------------------- | |
1062 | ||
1063 | wxWindow *wxMenu::GetWindow() const | |
1064 | { | |
1065 | if ( m_pInvokingWindow != NULL ) | |
1066 | return m_pInvokingWindow; | |
1067 | else if ( m_menuBar != NULL) | |
1068 | return m_menuBar->GetFrame(); | |
1069 | ||
1070 | return NULL; | |
1071 | } | |
1072 | ||
1073 | WXHMENU wxMenu::GetHMenu() const | |
1074 | { | |
1075 | if ( m_hMenu != 0 ) | |
1076 | return m_hMenu; | |
1077 | else if ( m_savehMenu != 0 ) | |
1078 | return m_savehMenu; | |
1079 | ||
1080 | wxFAIL_MSG(_T("wxMenu without HMENU")); | |
1081 | ||
1082 | return 0; | |
1083 | } | |
1084 | ||
1085 | // Update a menu and all submenus recursively. source is the object that has | |
1086 | // the update event handlers defined for it. If NULL, the menu or associated | |
1087 | // window will be used. | |
1088 | void wxMenu::UpdateUI(wxEvtHandler* source) | |
1089 | { | |
1090 | if (!source && GetInvokingWindow()) | |
1091 | source = GetInvokingWindow()->GetEventHandler(); | |
1092 | if (!source) | |
1093 | source = GetEventHandler(); | |
1094 | if (!source) | |
1095 | source = this; | |
1096 | ||
1097 | wxNode* node = GetItems().First(); | |
1098 | while (node) | |
1099 | { | |
1100 | wxMenuItem* item = (wxMenuItem*) node->Data(); | |
1101 | if ( !item->IsSeparator() ) | |
1102 | { | |
1103 | wxWindowID id = item->GetId(); | |
1104 | wxUpdateUIEvent event(id); | |
1105 | event.SetEventObject( source ); | |
1106 | ||
1107 | if (source->ProcessEvent(event)) | |
1108 | { | |
1109 | if (event.GetSetText()) | |
1110 | SetLabel(id, event.GetText()); | |
1111 | if (event.GetSetChecked()) | |
1112 | Check(id, event.GetChecked()); | |
1113 | if (event.GetSetEnabled()) | |
1114 | Enable(id, event.GetEnabled()); | |
1115 | } | |
1116 | ||
1117 | if (item->GetSubMenu()) | |
1118 | item->GetSubMenu()->UpdateUI(source); | |
1119 | } | |
1120 | node = node->Next(); | |
1121 | } | |
1122 | } |