]> git.saurik.com Git - wxWidgets.git/blob - src/msw/menu.cpp
attempt to fix compilation for old imagehlp.h header
[wxWidgets.git] / src / msw / menu.cpp
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
9 // Licence: wxWindows licence
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 #if wxUSE_MENUS
32
33 #ifndef WX_PRECOMP
34 #include "wx/frame.h"
35 #include "wx/menu.h"
36 #include "wx/utils.h"
37 #include "wx/intl.h"
38 #include "wx/log.h"
39 #endif
40
41 #if wxUSE_OWNER_DRAWN
42 #include "wx/ownerdrw.h"
43 #endif
44
45 #include "wx/msw/private.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 // private functions
65 // ----------------------------------------------------------------------------
66
67 // make the given menu item default
68 static void SetDefaultMenuItem(HMENU hmenu, UINT id)
69 {
70 #ifndef __WXWINCE__
71 MENUITEMINFO mii;
72 wxZeroMemory(mii);
73 mii.cbSize = sizeof(MENUITEMINFO);
74 mii.fMask = MIIM_STATE;
75 mii.fState = MFS_DEFAULT;
76
77 if ( !::SetMenuItemInfo(hmenu, id, FALSE, &mii) )
78 {
79 wxLogLastError(wxT("SetMenuItemInfo"));
80 }
81 #endif
82 }
83
84 #ifdef __WXWINCE__
85 UINT GetMenuState(HMENU hMenu, UINT id, UINT flags)
86 {
87 MENUITEMINFO info;
88 wxZeroMemory(info);
89 info.cbSize = sizeof(info);
90 info.fMask = MIIM_STATE;
91 if ( !GetMenuItemInfo(hMenu, id, flags & MF_BYCOMMAND ? FALSE : TRUE, & info) )
92 wxLogLastError(wxT("GetMenuItemInfo"));
93 return info.fState;
94 }
95 #endif
96
97 // ============================================================================
98 // implementation
99 // ============================================================================
100
101 IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
102 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow)
103
104 // ---------------------------------------------------------------------------
105 // wxMenu construction, adding and removing menu items
106 // ---------------------------------------------------------------------------
107
108 // Construct a menu with optional title (then use append)
109 void wxMenu::Init()
110 {
111 m_doBreak = FALSE;
112 m_startRadioGroup = -1;
113
114 // create the menu
115 m_hMenu = (WXHMENU)CreatePopupMenu();
116 if ( !m_hMenu )
117 {
118 wxLogLastError(wxT("CreatePopupMenu"));
119 }
120
121 // if we have a title, insert it in the beginning of the menu
122 if ( !!m_title )
123 {
124 Append(idMenuTitle, m_title);
125 AppendSeparator();
126 }
127 }
128
129 // The wxWindow destructor will take care of deleting the submenus.
130 wxMenu::~wxMenu()
131 {
132 // we should free Windows resources only if Windows doesn't do it for us
133 // which happens if we're attached to a menubar or a submenu of another
134 // menu
135 if ( !IsAttached() && !GetParent() )
136 {
137 if ( !::DestroyMenu(GetHmenu()) )
138 {
139 wxLogLastError(wxT("DestroyMenu"));
140 }
141 }
142
143 #if wxUSE_ACCEL
144 // delete accels
145 WX_CLEAR_ARRAY(m_accels);
146 #endif // wxUSE_ACCEL
147 }
148
149 void wxMenu::Break()
150 {
151 // this will take effect during the next call to Append()
152 m_doBreak = TRUE;
153 }
154
155 void wxMenu::Attach(wxMenuBarBase *menubar)
156 {
157 wxMenuBase::Attach(menubar);
158
159 EndRadioGroup();
160 }
161
162 #if wxUSE_ACCEL
163
164 int wxMenu::FindAccel(int id) const
165 {
166 size_t n, count = m_accels.GetCount();
167 for ( n = 0; n < count; n++ )
168 {
169 if ( m_accels[n]->m_command == id )
170 return n;
171 }
172
173 return wxNOT_FOUND;
174 }
175
176 void wxMenu::UpdateAccel(wxMenuItem *item)
177 {
178 if ( item->IsSubMenu() )
179 {
180 wxMenu *submenu = item->GetSubMenu();
181 wxMenuItemList::compatibility_iterator node = submenu->GetMenuItems().GetFirst();
182 while ( node )
183 {
184 UpdateAccel(node->GetData());
185
186 node = node->GetNext();
187 }
188 }
189 else if ( !item->IsSeparator() )
190 {
191 // find the (new) accel for this item
192 wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText());
193 if ( accel )
194 accel->m_command = item->GetId();
195
196 // find the old one
197 int n = FindAccel(item->GetId());
198 if ( n == wxNOT_FOUND )
199 {
200 // no old, add new if any
201 if ( accel )
202 m_accels.Add(accel);
203 else
204 return; // skipping RebuildAccelTable() below
205 }
206 else
207 {
208 // replace old with new or just remove the old one if no new
209 delete m_accels[n];
210 if ( accel )
211 m_accels[n] = accel;
212 else
213 m_accels.RemoveAt(n);
214 }
215
216 if ( IsAttached() )
217 {
218 m_menuBar->RebuildAccelTable();
219 }
220 }
221 //else: it is a separator, they can't have accels, nothing to do
222 }
223
224 #endif // wxUSE_ACCEL
225
226 // append a new item or submenu to the menu
227 bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos)
228 {
229 #if wxUSE_ACCEL
230 UpdateAccel(pItem);
231 #endif // wxUSE_ACCEL
232
233 UINT flags = 0;
234
235 // if "Break" has just been called, insert a menu break before this item
236 // (and don't forget to reset the flag)
237 if ( m_doBreak ) {
238 flags |= MF_MENUBREAK;
239 m_doBreak = FALSE;
240 }
241
242 if ( pItem->IsSeparator() ) {
243 flags |= MF_SEPARATOR;
244 }
245
246 // id is the numeric id for normal menu items and HMENU for submenus as
247 // required by ::AppendMenu() API
248 UINT id;
249 wxMenu *submenu = pItem->GetSubMenu();
250 if ( submenu != NULL ) {
251 wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
252
253 submenu->SetParent(this);
254
255 id = (UINT)submenu->GetHMenu();
256
257 flags |= MF_POPUP;
258 }
259 else {
260 id = pItem->GetId();
261 }
262
263 LPCTSTR pData;
264
265 #if wxUSE_OWNER_DRAWN
266 if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
267 // item draws itself, pass pointer to it in data parameter
268 flags |= MF_OWNERDRAW;
269 pData = (LPCTSTR)pItem;
270 }
271 else
272 #endif
273 {
274 // menu is just a normal string (passed in data parameter)
275 flags |= MF_STRING;
276
277 pData = (wxChar*)pItem->GetText().c_str();
278 }
279
280 BOOL ok;
281 if ( pos == (size_t)-1 )
282 {
283 ok = ::AppendMenu(GetHmenu(), flags, id, pData);
284 }
285 else
286 {
287 ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData);
288 }
289
290 if ( !ok )
291 {
292 wxLogLastError(wxT("Insert or AppendMenu"));
293
294 return FALSE;
295 }
296
297 // if we just appended the title, highlight it
298 #ifdef __WIN32__
299 if ( (int)id == idMenuTitle )
300 {
301 // visually select the menu title
302 SetDefaultMenuItem(GetHmenu(), id);
303 }
304 #endif // __WIN32__
305
306 // if we're already attached to the menubar, we must update it
307 if ( IsAttached() && m_menuBar->IsAttached() )
308 {
309 m_menuBar->Refresh();
310 }
311
312 return TRUE;
313 }
314
315 void wxMenu::EndRadioGroup()
316 {
317 // we're not inside a radio group any longer
318 m_startRadioGroup = -1;
319 }
320
321 bool wxMenu::DoAppend(wxMenuItem *item)
322 {
323 wxCHECK_MSG( item, FALSE, _T("NULL item in wxMenu::DoAppend") );
324
325 bool check = FALSE;
326
327 if ( item->GetKind() == wxITEM_RADIO )
328 {
329 int count = GetMenuItemCount();
330
331 if ( m_startRadioGroup == -1 )
332 {
333 // start a new radio group
334 m_startRadioGroup = count;
335
336 // for now it has just one element
337 item->SetAsRadioGroupStart();
338 item->SetRadioGroupEnd(m_startRadioGroup);
339
340 // ensure that we have a checked item in the radio group
341 check = TRUE;
342 }
343 else // extend the current radio group
344 {
345 // we need to update its end item
346 item->SetRadioGroupStart(m_startRadioGroup);
347 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
348
349 if ( node )
350 {
351 node->GetData()->SetRadioGroupEnd(count);
352 }
353 else
354 {
355 wxFAIL_MSG( _T("where is the radio group start item?") );
356 }
357 }
358 }
359 else // not a radio item
360 {
361 EndRadioGroup();
362 }
363
364 if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
365 {
366 return FALSE;
367 }
368
369 if ( check )
370 {
371 // check the item initially
372 item->Check(TRUE);
373 }
374
375 return TRUE;
376 }
377
378 bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
379 {
380 return wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos);
381 }
382
383 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
384 {
385 // we need to find the items position in the child list
386 size_t pos;
387 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
388 for ( pos = 0; node; pos++ )
389 {
390 if ( node->GetData() == item )
391 break;
392
393 node = node->GetNext();
394 }
395
396 // DoRemove() (unlike Remove) can only be called for existing item!
397 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
398
399 #if wxUSE_ACCEL
400 // remove the corresponding accel from the accel table
401 int n = FindAccel(item->GetId());
402 if ( n != wxNOT_FOUND )
403 {
404 delete m_accels[n];
405
406 m_accels.RemoveAt(n);
407 }
408 //else: this item doesn't have an accel, nothing to do
409 #endif // wxUSE_ACCEL
410
411 // remove the item from the menu
412 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
413 {
414 wxLogLastError(wxT("RemoveMenu"));
415 }
416
417 if ( IsAttached() && m_menuBar->IsAttached() )
418 {
419 // otherwise, the chane won't be visible
420 m_menuBar->Refresh();
421 }
422
423 // and from internal data structures
424 return wxMenuBase::DoRemove(item);
425 }
426
427 // ---------------------------------------------------------------------------
428 // accelerator helpers
429 // ---------------------------------------------------------------------------
430
431 #if wxUSE_ACCEL
432
433 // create the wxAcceleratorEntries for our accels and put them into provided
434 // array - return the number of accels we have
435 size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
436 {
437 size_t count = GetAccelCount();
438 for ( size_t n = 0; n < count; n++ )
439 {
440 *accels++ = *m_accels[n];
441 }
442
443 return count;
444 }
445
446 #endif // wxUSE_ACCEL
447
448 // ---------------------------------------------------------------------------
449 // set wxMenu title
450 // ---------------------------------------------------------------------------
451
452 void wxMenu::SetTitle(const wxString& label)
453 {
454 bool hasNoTitle = m_title.IsEmpty();
455 m_title = label;
456
457 HMENU hMenu = GetHmenu();
458
459 if ( hasNoTitle )
460 {
461 if ( !label.IsEmpty() )
462 {
463 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
464 (unsigned)idMenuTitle, m_title) ||
465 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
466 {
467 wxLogLastError(wxT("InsertMenu"));
468 }
469 }
470 }
471 else
472 {
473 if ( label.IsEmpty() )
474 {
475 // remove the title and the separator after it
476 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
477 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
478 {
479 wxLogLastError(wxT("RemoveMenu"));
480 }
481 }
482 else
483 {
484 // modify the title
485 #ifdef __WXWINCE__
486 MENUITEMINFO info;
487 wxZeroMemory(info);
488 info.cbSize = sizeof(info);
489 info.fMask = MIIM_TYPE;
490 info.fType = MFT_STRING;
491 info.cch = m_title.Length();
492 info.dwTypeData = (LPTSTR) m_title.c_str();
493 if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) )
494 {
495 wxLogLastError(wxT("SetMenuItemInfo"));
496 }
497 #else
498 if ( !ModifyMenu(hMenu, 0u,
499 MF_BYPOSITION | MF_STRING,
500 (unsigned)idMenuTitle, m_title) )
501 {
502 wxLogLastError(wxT("ModifyMenu"));
503 }
504 #endif
505 }
506 }
507
508 #ifdef __WIN32__
509 // put the title string in bold face
510 if ( !m_title.IsEmpty() )
511 {
512 SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle);
513 }
514 #endif // Win32
515 }
516
517 // ---------------------------------------------------------------------------
518 // event processing
519 // ---------------------------------------------------------------------------
520
521 bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
522 {
523 // ignore commands from the menu title
524
525 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
526 if ( id != (WXWORD)idMenuTitle )
527 {
528 // VZ: previosuly, the command int was set to id too which was quite
529 // useless anyhow (as it could be retrieved using GetId()) and
530 // uncompatible with wxGTK, so now we use the command int instead
531 // to pass the checked status
532 UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND) ;
533 SendEvent(id, menuState & MF_CHECKED);
534 }
535
536 return TRUE;
537 }
538
539 // ---------------------------------------------------------------------------
540 // other
541 // ---------------------------------------------------------------------------
542
543 wxWindow *wxMenu::GetWindow() const
544 {
545 if ( m_invokingWindow != NULL )
546 return m_invokingWindow;
547 else if ( m_menuBar != NULL)
548 return m_menuBar->GetFrame();
549
550 return NULL;
551 }
552
553 // ---------------------------------------------------------------------------
554 // Menu Bar
555 // ---------------------------------------------------------------------------
556
557 void wxMenuBar::Init()
558 {
559 m_eventHandler = this;
560 m_hMenu = 0;
561 }
562
563 wxMenuBar::wxMenuBar()
564 {
565 Init();
566 }
567
568 wxMenuBar::wxMenuBar( long WXUNUSED(style) )
569 {
570 Init();
571 }
572
573 wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
574 {
575 Init();
576
577 m_titles.Alloc(count);
578
579 for ( int i = 0; i < count; i++ )
580 {
581 m_menus.Append(menus[i]);
582 m_titles.Add(titles[i]);
583
584 menus[i]->Attach(this);
585 }
586 }
587
588 wxMenuBar::~wxMenuBar()
589 {
590 // we should free Windows resources only if Windows doesn't do it for us
591 // which happens if we're attached to a frame
592 if (m_hMenu && !IsAttached())
593 {
594 ::DestroyMenu((HMENU)m_hMenu);
595 m_hMenu = (WXHMENU)NULL;
596 }
597 }
598
599 // ---------------------------------------------------------------------------
600 // wxMenuBar helpers
601 // ---------------------------------------------------------------------------
602
603 void wxMenuBar::Refresh()
604 {
605 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
606
607 DrawMenuBar(GetHwndOf(GetFrame()));
608 }
609
610 WXHMENU wxMenuBar::Create()
611 {
612 if ( m_hMenu != 0 )
613 return m_hMenu;
614
615 m_hMenu = (WXHMENU)::CreateMenu();
616
617 if ( !m_hMenu )
618 {
619 wxLogLastError(wxT("CreateMenu"));
620 }
621 else
622 {
623 size_t count = GetMenuCount(), i;
624 wxMenuList::iterator it;
625 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
626 {
627 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
628 (UINT)(*it)->GetHMenu(),
629 m_titles[i]) )
630 {
631 wxLogLastError(wxT("AppendMenu"));
632 }
633 }
634 }
635
636 return m_hMenu;
637 }
638
639 // ---------------------------------------------------------------------------
640 // wxMenuBar functions to work with the top level submenus
641 // ---------------------------------------------------------------------------
642
643 // NB: we don't support owner drawn top level items for now, if we do these
644 // functions would have to be changed to use wxMenuItem as well
645
646 void wxMenuBar::EnableTop(size_t pos, bool enable)
647 {
648 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
649
650 int flag = enable ? MF_ENABLED : MF_GRAYED;
651
652 EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
653
654 Refresh();
655 }
656
657 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
658 {
659 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
660
661 m_titles[pos] = label;
662
663 if ( !IsAttached() )
664 {
665 return;
666 }
667 //else: have to modify the existing menu
668
669 UINT id;
670 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
671 if ( flagsOld == 0xFFFFFFFF )
672 {
673 wxLogLastError(wxT("GetMenuState"));
674
675 return;
676 }
677
678 if ( flagsOld & MF_POPUP )
679 {
680 // HIBYTE contains the number of items in the submenu in this case
681 flagsOld &= 0xff;
682 id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos);
683 }
684 else
685 {
686 id = pos;
687 }
688
689 #ifdef __WXWINCE__
690 MENUITEMINFO info;
691 wxZeroMemory(info);
692 info.cbSize = sizeof(info);
693 info.fMask = MIIM_TYPE;
694 info.fType = MFT_STRING;
695 info.cch = label.Length();
696 info.dwTypeData = (LPTSTR) label.c_str();
697 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, & info) )
698 {
699 wxLogLastError(wxT("SetMenuItemInfo"));
700 }
701
702 #else
703 if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
704 id, label) == (int)0xFFFFFFFF )
705 {
706 wxLogLastError(wxT("ModifyMenu"));
707 }
708 #endif
709
710 Refresh();
711 }
712
713 wxString wxMenuBar::GetLabelTop(size_t pos) const
714 {
715 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
716 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
717
718 return m_titles[pos];
719 }
720
721 // ---------------------------------------------------------------------------
722 // wxMenuBar construction
723 // ---------------------------------------------------------------------------
724
725 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
726 {
727 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
728 if ( !menuOld )
729 return NULL;
730
731 m_titles[pos] = title;
732
733 if ( IsAttached() )
734 {
735 // can't use ModifyMenu() because it deletes the submenu it replaces
736 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
737 {
738 wxLogLastError(wxT("RemoveMenu"));
739 }
740
741 if ( !::InsertMenu(GetHmenu(), (UINT)pos,
742 MF_BYPOSITION | MF_POPUP | MF_STRING,
743 (UINT)GetHmenuOf(menu), title) )
744 {
745 wxLogLastError(wxT("InsertMenu"));
746 }
747
748 #if wxUSE_ACCEL
749 if ( menuOld->HasAccels() || menu->HasAccels() )
750 {
751 // need to rebuild accell table
752 RebuildAccelTable();
753 }
754 #endif // wxUSE_ACCEL
755
756 Refresh();
757 }
758
759 return menuOld;
760 }
761
762 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
763 {
764 if ( !wxMenuBarBase::Insert(pos, menu, title) )
765 return FALSE;
766
767 m_titles.Insert(title, pos);
768
769 if ( IsAttached() )
770 {
771 if ( !::InsertMenu(GetHmenu(), pos,
772 MF_BYPOSITION | MF_POPUP | MF_STRING,
773 (UINT)GetHmenuOf(menu), title) )
774 {
775 wxLogLastError(wxT("InsertMenu"));
776 }
777
778 #if wxUSE_ACCEL
779 if ( menu->HasAccels() )
780 {
781 // need to rebuild accell table
782 RebuildAccelTable();
783 }
784 #endif // wxUSE_ACCEL
785
786 Refresh();
787 }
788
789 return TRUE;
790 }
791
792 bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
793 {
794 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
795 wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") );
796
797 if ( !wxMenuBarBase::Append(menu, title) )
798 return FALSE;
799
800 m_titles.Add(title);
801
802 if ( IsAttached() )
803 {
804 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
805 (UINT)submenu, title) )
806 {
807 wxLogLastError(wxT("AppendMenu"));
808 }
809
810 #if wxUSE_ACCEL
811 if ( menu->HasAccels() )
812 {
813 // need to rebuild accell table
814 RebuildAccelTable();
815 }
816 #endif // wxUSE_ACCEL
817
818 Refresh();
819 }
820
821 return TRUE;
822 }
823
824 wxMenu *wxMenuBar::Remove(size_t pos)
825 {
826 wxMenu *menu = wxMenuBarBase::Remove(pos);
827 if ( !menu )
828 return NULL;
829
830 if ( IsAttached() )
831 {
832 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
833 {
834 wxLogLastError(wxT("RemoveMenu"));
835 }
836
837 #if wxUSE_ACCEL
838 if ( menu->HasAccels() )
839 {
840 // need to rebuild accell table
841 RebuildAccelTable();
842 }
843 #endif // wxUSE_ACCEL
844
845 Refresh();
846 }
847
848 m_titles.RemoveAt(pos);
849
850 return menu;
851 }
852
853 #if wxUSE_ACCEL
854
855 void wxMenuBar::RebuildAccelTable()
856 {
857 // merge the accelerators of all menus into one accel table
858 size_t nAccelCount = 0;
859 size_t i, count = GetMenuCount();
860 wxMenuList::iterator it;
861 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
862 {
863 nAccelCount += (*it)->GetAccelCount();
864 }
865
866 if ( nAccelCount )
867 {
868 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
869
870 nAccelCount = 0;
871 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
872 {
873 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
874 }
875
876 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
877
878 delete [] accelEntries;
879 }
880 }
881
882 #endif // wxUSE_ACCEL
883
884 void wxMenuBar::Attach(wxFrame *frame)
885 {
886 wxMenuBarBase::Attach(frame);
887
888 #if wxUSE_ACCEL
889 RebuildAccelTable();
890 #endif // wxUSE_ACCEL
891 }
892
893 void wxMenuBar::Detach()
894 {
895 wxMenuBarBase::Detach();
896 }
897
898 #endif // wxUSE_MENUS