]> git.saurik.com Git - wxWidgets.git/blob - src/msw/menu.cpp
Applied #10892: Remove wxDocManager instance check
[wxWidgets.git] / src / msw / menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_MENUS
28
29 #include "wx/menu.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/frame.h"
33 #include "wx/utils.h"
34 #include "wx/intl.h"
35 #include "wx/log.h"
36 #include "wx/image.h"
37 #endif
38
39 #if wxUSE_OWNER_DRAWN
40 #include "wx/ownerdrw.h"
41 #endif
42
43 #include "wx/scopedarray.h"
44
45 #include "wx/msw/private.h"
46 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
47
48 #ifdef __WXWINCE__
49 #include <windows.h>
50 #include <windowsx.h>
51 #include <tchar.h>
52 #include <ole2.h>
53 #include <shellapi.h>
54 #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__)
55 #include <aygshell.h>
56 #endif
57
58 #include "wx/msw/wince/missing.h"
59
60 #endif
61
62 // other standard headers
63 #include <string.h>
64
65 #if wxUSE_OWNER_DRAWN
66 #include "wx/dynlib.h"
67 #endif
68
69 #ifndef MNS_CHECKORBMP
70 #define MNS_CHECKORBMP 0x04000000
71 #endif
72 #ifndef MIM_STYLE
73 #define MIM_STYLE 0x00000010
74 #endif
75
76 // ----------------------------------------------------------------------------
77 // global variables
78 // ----------------------------------------------------------------------------
79
80 // ----------------------------------------------------------------------------
81 // constants
82 // ----------------------------------------------------------------------------
83
84 // the (popup) menu title has this special id
85 static const UINT idMenuTitle = (UINT)-3;
86
87 // ----------------------------------------------------------------------------
88 // private functions
89 // ----------------------------------------------------------------------------
90
91 namespace
92 {
93
94 // make the given menu item default
95 void SetDefaultMenuItem(HMENU WXUNUSED_IN_WINCE(hmenu),
96 UINT WXUNUSED_IN_WINCE(id))
97 {
98 #ifndef __WXWINCE__
99 MENUITEMINFO mii;
100 wxZeroMemory(mii);
101 mii.cbSize = sizeof(MENUITEMINFO);
102 mii.fMask = MIIM_STATE;
103 mii.fState = MFS_DEFAULT;
104
105 if ( !::SetMenuItemInfo(hmenu, id, FALSE, &mii) )
106 {
107 wxLogLastError(wxT("SetMenuItemInfo"));
108 }
109 #endif
110 }
111
112 #ifdef __WXWINCE__
113 UINT GetMenuState(HMENU hMenu, UINT id, UINT flags)
114 {
115 MENUITEMINFO info;
116 wxZeroMemory(info);
117 info.cbSize = sizeof(info);
118 info.fMask = MIIM_STATE;
119 // MF_BYCOMMAND is zero so test MF_BYPOSITION
120 if ( !::GetMenuItemInfo(hMenu, id, flags & MF_BYPOSITION ? TRUE : FALSE , & info) )
121 {
122 wxLogLastError(wxT("GetMenuItemInfo"));
123 }
124 return info.fState;
125 }
126 #endif // __WXWINCE__
127
128 bool IsLessThanStdSize(const wxBitmap& bmp)
129 {
130 // FIXME: these +4 are chosen so that 16*16 bitmaps pass this test with
131 // default SM_CXMENUCHECK value but I have no idea what do we really
132 // need to use here
133 return bmp.GetWidth() < ::GetSystemMetrics(SM_CXMENUCHECK) + 4 &&
134 bmp.GetHeight() < ::GetSystemMetrics(SM_CYMENUCHECK) + 4;
135 }
136
137 } // anonymous namespace
138
139 // ============================================================================
140 // implementation
141 // ============================================================================
142
143 #include "wx/listimpl.cpp"
144
145 WX_DEFINE_LIST( wxMenuInfoList )
146
147 #if wxUSE_EXTENDED_RTTI
148
149 WX_DEFINE_FLAGS( wxMenuStyle )
150
151 wxBEGIN_FLAGS( wxMenuStyle )
152 wxFLAGS_MEMBER(wxMENU_TEAROFF)
153 wxEND_FLAGS( wxMenuStyle )
154
155 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu, wxEvtHandler,"wx/menu.h")
156
157 wxCOLLECTION_TYPE_INFO( wxMenuItem * , wxMenuItemList ) ;
158
159 template<> void wxCollectionToVariantArray( wxMenuItemList const &theList, wxxVariantArray &value)
160 {
161 wxListCollectionToVariantArray<wxMenuItemList::compatibility_iterator>( theList , value ) ;
162 }
163
164 wxBEGIN_PROPERTIES_TABLE(wxMenu)
165 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_MENU_SELECTED , wxCommandEvent)
166 wxPROPERTY( Title, wxString , SetTitle, GetTitle, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
167 wxREADONLY_PROPERTY_FLAGS( MenuStyle , wxMenuStyle , long , GetStyle , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
168 wxPROPERTY_COLLECTION( MenuItems , wxMenuItemList , wxMenuItem* , Append , GetMenuItems , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
169 wxEND_PROPERTIES_TABLE()
170
171 wxBEGIN_HANDLERS_TABLE(wxMenu)
172 wxEND_HANDLERS_TABLE()
173
174 wxDIRECT_CONSTRUCTOR_2( wxMenu , wxString , Title , long , MenuStyle )
175
176 WX_DEFINE_FLAGS( wxMenuBarStyle )
177
178 wxBEGIN_FLAGS( wxMenuBarStyle )
179 wxFLAGS_MEMBER(wxMB_DOCKABLE)
180 wxEND_FLAGS( wxMenuBarStyle )
181
182 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
183 bool wxMenuBarStreamingCallback( const wxObject *WXUNUSED(object), wxWriter * , wxPersister * , wxxVariantArray & )
184 {
185 return true ;
186 }
187
188 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar, wxWindow ,"wx/menu.h",wxMenuBarStreamingCallback)
189
190 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo, wxObject , "wx/menu.h" )
191
192 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo)
193 wxREADONLY_PROPERTY( Menu , wxMenu* , GetMenu , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
194 wxREADONLY_PROPERTY( Title , wxString , GetTitle , wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
195 wxEND_PROPERTIES_TABLE()
196
197 wxBEGIN_HANDLERS_TABLE(wxMenuInfo)
198 wxEND_HANDLERS_TABLE()
199
200 wxCONSTRUCTOR_2( wxMenuInfo , wxMenu* , Menu , wxString , Title )
201
202 wxCOLLECTION_TYPE_INFO( wxMenuInfo * , wxMenuInfoList ) ;
203
204 template<> void wxCollectionToVariantArray( wxMenuInfoList const &theList, wxxVariantArray &value)
205 {
206 wxListCollectionToVariantArray<wxMenuInfoList::compatibility_iterator>( theList , value ) ;
207 }
208
209 wxBEGIN_PROPERTIES_TABLE(wxMenuBar)
210 wxPROPERTY_COLLECTION( MenuInfos , wxMenuInfoList , wxMenuInfo* , Append , GetMenuInfos , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
211 wxEND_PROPERTIES_TABLE()
212
213 wxBEGIN_HANDLERS_TABLE(wxMenuBar)
214 wxEND_HANDLERS_TABLE()
215
216 wxCONSTRUCTOR_DUMMY( wxMenuBar )
217
218 #else
219 IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
220 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow)
221 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo, wxObject)
222 #endif
223
224 const wxMenuInfoList& wxMenuBar::GetMenuInfos() const
225 {
226 wxMenuInfoList* list = const_cast< wxMenuInfoList* >( &m_menuInfos ) ;
227 WX_CLEAR_LIST( wxMenuInfoList , *list ) ;
228 for( size_t i = 0 ; i < GetMenuCount() ; ++i )
229 {
230 wxMenuInfo* info = new wxMenuInfo() ;
231 info->Create( const_cast<wxMenuBar*>(this)->GetMenu(i) , GetMenuLabel(i) ) ;
232 list->Append( info ) ;
233 }
234 return m_menuInfos ;
235 }
236
237 // ---------------------------------------------------------------------------
238 // wxMenu construction, adding and removing menu items
239 // ---------------------------------------------------------------------------
240
241 // Construct a menu with optional title (then use append)
242 void wxMenu::Init()
243 {
244 m_doBreak = false;
245 m_startRadioGroup = -1;
246
247 // create the menu
248 m_hMenu = (WXHMENU)CreatePopupMenu();
249 if ( !m_hMenu )
250 {
251 wxLogLastError(wxT("CreatePopupMenu"));
252 }
253
254 // if we have a title, insert it in the beginning of the menu
255 if ( !m_title.empty() )
256 {
257 Append(idMenuTitle, m_title);
258 AppendSeparator();
259 }
260 }
261
262 // The wxWindow destructor will take care of deleting the submenus.
263 wxMenu::~wxMenu()
264 {
265 // we should free Windows resources only if Windows doesn't do it for us
266 // which happens if we're attached to a menubar or a submenu of another
267 // menu
268 if ( !IsAttached() && !GetParent() )
269 {
270 if ( !::DestroyMenu(GetHmenu()) )
271 {
272 wxLogLastError(wxT("DestroyMenu"));
273 }
274 }
275
276 #if wxUSE_ACCEL
277 // delete accels
278 WX_CLEAR_ARRAY(m_accels);
279 #endif // wxUSE_ACCEL
280 }
281
282 void wxMenu::Break()
283 {
284 // this will take effect during the next call to Append()
285 m_doBreak = true;
286 }
287
288 void wxMenu::Attach(wxMenuBarBase *menubar)
289 {
290 wxMenuBase::Attach(menubar);
291
292 EndRadioGroup();
293 }
294
295 #if wxUSE_ACCEL
296
297 int wxMenu::FindAccel(int id) const
298 {
299 size_t n, count = m_accels.GetCount();
300 for ( n = 0; n < count; n++ )
301 {
302 if ( m_accels[n]->m_command == id )
303 return n;
304 }
305
306 return wxNOT_FOUND;
307 }
308
309 void wxMenu::UpdateAccel(wxMenuItem *item)
310 {
311 if ( item->IsSubMenu() )
312 {
313 wxMenu *submenu = item->GetSubMenu();
314 wxMenuItemList::compatibility_iterator node = submenu->GetMenuItems().GetFirst();
315 while ( node )
316 {
317 UpdateAccel(node->GetData());
318
319 node = node->GetNext();
320 }
321 }
322 else if ( !item->IsSeparator() )
323 {
324 // recurse upwards: we should only modify m_accels of the top level
325 // menus, not of the submenus as wxMenuBar doesn't look at them
326 // (alternative and arguable cleaner solution would be to recurse
327 // downwards in GetAccelCount() and CopyAccels())
328 if ( GetParent() )
329 {
330 GetParent()->UpdateAccel(item);
331 return;
332 }
333
334 // find the (new) accel for this item
335 wxAcceleratorEntry *accel = wxAcceleratorEntry::Create(item->GetItemLabel());
336 if ( accel )
337 accel->m_command = item->GetId();
338
339 // find the old one
340 int n = FindAccel(item->GetId());
341 if ( n == wxNOT_FOUND )
342 {
343 // no old, add new if any
344 if ( accel )
345 m_accels.Add(accel);
346 else
347 return; // skipping RebuildAccelTable() below
348 }
349 else
350 {
351 // replace old with new or just remove the old one if no new
352 delete m_accels[n];
353 if ( accel )
354 m_accels[n] = accel;
355 else
356 m_accels.RemoveAt(n);
357 }
358
359 if ( IsAttached() )
360 {
361 GetMenuBar()->RebuildAccelTable();
362 }
363 }
364 //else: it is a separator, they can't have accels, nothing to do
365 }
366
367 #endif // wxUSE_ACCEL
368
369 namespace
370 {
371
372 // helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO
373 HBITMAP GetHBitmapForMenu(wxMenuItem *pItem, bool checked = true)
374 {
375 // Under versions of Windows older than Vista we can't pass HBITMAP
376 // directly as hbmpItem for 2 reasons:
377 // 1. We can't draw it with transparency then (this is not
378 // very important now but would be with themed menu bg)
379 // 2. Worse, Windows inverts the bitmap for the selected
380 // item and this looks downright ugly
381 //
382 // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using
383 // HBMMENU_CALLBACK when inserting it
384 //
385 // However under Vista using HBMMENU_CALLBACK causes the entire menu to be
386 // drawn using the classic theme instead of the current one and it does
387 // handle transparency just fine so do use the real bitmap there
388 #if wxUSE_IMAGE
389 if ( wxGetWinVersion() >= wxWinVersion_Vista )
390 {
391 wxBitmap bmp = pItem->GetBitmap(checked);
392 if ( bmp.IsOk() )
393 {
394 // we must use PARGB DIB for the menu bitmaps so ensure that we do
395 wxImage img(bmp.ConvertToImage());
396 if ( !img.HasAlpha() )
397 {
398 img.InitAlpha();
399 pItem->SetBitmap(img, checked);
400 }
401
402 return GetHbitmapOf(pItem->GetBitmap());
403 }
404 }
405 #endif // wxUSE_IMAGE
406
407 return HBMMENU_CALLBACK;
408 }
409
410 } // anonymous namespace
411
412 // append a new item or submenu to the menu
413 bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos)
414 {
415 #if wxUSE_ACCEL
416 UpdateAccel(pItem);
417 #endif // wxUSE_ACCEL
418
419 // we should support disabling the item even prior to adding it to the menu
420 UINT flags = pItem->IsEnabled() ? MF_ENABLED : MF_GRAYED;
421
422 // if "Break" has just been called, insert a menu break before this item
423 // (and don't forget to reset the flag)
424 if ( m_doBreak ) {
425 flags |= MF_MENUBREAK;
426 m_doBreak = false;
427 }
428
429 if ( pItem->IsSeparator() ) {
430 flags |= MF_SEPARATOR;
431 }
432
433 // id is the numeric id for normal menu items and HMENU for submenus as
434 // required by ::AppendMenu() API
435 UINT_PTR id;
436 wxMenu *submenu = pItem->GetSubMenu();
437 if ( submenu != NULL ) {
438 wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
439
440 submenu->SetParent(this);
441
442 id = (UINT_PTR)submenu->GetHMenu();
443
444 flags |= MF_POPUP;
445 }
446 else {
447 id = pItem->GetMSWId();
448 }
449
450
451 // prepare to insert the item in the menu
452 wxString itemText = pItem->GetItemLabel();
453 LPCTSTR pData = NULL;
454 if ( pos == (size_t)-1 )
455 {
456 // append at the end (note that the item is already appended to
457 // internal data structures)
458 pos = GetMenuItemCount() - 1;
459 }
460
461 // adjust position to account for the title, if any
462 if ( !m_title.empty() )
463 pos += 2; // for the title itself and its separator
464
465 BOOL ok = false;
466
467 #if wxUSE_OWNER_DRAWN
468 // Under older systems mixing owner-drawn and non-owner-drawn items results
469 // in inconsistent margins, so we force this one to be owner-drawn if any
470 // other items already are. Later we might want to use a boolean in the
471 // wxMenu to avoid search. Also we might make this fix unnecessary by
472 // getting the correct margin using NONCLIENTMETRICS.
473 static const wxWinVersion winver = wxGetWinVersion();
474 if ( winver < wxWinVersion_XP &&
475 !pItem->IsOwnerDrawn() && !pItem->IsSeparator() )
476 {
477 // Check if any other items are ownerdrawn, and make ownerdrawn if so
478 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
479 while (node)
480 {
481 if (node->GetData()->IsOwnerDrawn())
482 {
483 pItem->SetOwnerDrawn(true);
484 break;
485 }
486 node = node->GetNext();
487 }
488 }
489 #endif // wxUSE_OWNER_DRAWN
490
491 // check if we have something more than a simple text item
492 #if wxUSE_OWNER_DRAWN
493 if ( pItem->IsOwnerDrawn() )
494 {
495 #ifndef __DMC__
496 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
497 // drawn item under the previous versions and we also have to use them
498 // in any case if the item has custom colours or font
499 bool mustUseOwnerDrawn = winver < wxWinVersion_98 ||
500 pItem->GetTextColour().Ok() ||
501 pItem->GetBackgroundColour().Ok() ||
502 pItem->GetFont().Ok();
503 if ( !mustUseOwnerDrawn )
504 {
505 const wxBitmap& bmpUnchecked = pItem->GetBitmap(false),
506 bmpChecked = pItem->GetBitmap(true);
507 if ( (bmpUnchecked.Ok() && !IsLessThanStdSize(bmpUnchecked)) ||
508 (bmpChecked.Ok() && !IsLessThanStdSize(bmpChecked)) )
509 {
510 mustUseOwnerDrawn = true;
511 }
512 }
513
514 // use InsertMenuItem() if possible as it's guaranteed to look correct
515 // while our owner-drawn code is not
516 if ( !mustUseOwnerDrawn )
517 {
518 WinStruct<MENUITEMINFO> mii;
519 mii.fMask = MIIM_STRING | MIIM_DATA;
520
521 if ( pItem->GetBitmap().IsOk() )
522 {
523 mii.fMask |= MIIM_BITMAP;
524 mii.hbmpItem = GetHBitmapForMenu(pItem);
525 }
526
527 if ( pItem->IsCheckable() )
528 {
529 mii.fMask |= MIIM_CHECKMARKS;
530 mii.hbmpChecked = GetHBitmapForMenu(pItem, true);
531 mii.hbmpUnchecked = GetHBitmapForMenu(pItem, false);
532 }
533
534 mii.cch = itemText.length();
535 mii.dwTypeData = const_cast<wxChar *>(itemText.wx_str());
536
537 if ( flags & MF_POPUP )
538 {
539 mii.fMask |= MIIM_SUBMENU;
540 mii.hSubMenu = GetHmenuOf(pItem->GetSubMenu());
541 }
542 else
543 {
544 mii.fMask |= MIIM_ID;
545 mii.wID = id;
546 }
547
548 mii.dwItemData = reinterpret_cast<ULONG_PTR>(pItem);
549
550 ok = ::InsertMenuItem(GetHmenu(), pos, TRUE /* by pos */, &mii);
551 if ( !ok )
552 {
553 wxLogLastError(wxT("InsertMenuItem()"));
554 }
555 else // InsertMenuItem() ok
556 {
557 // we need to remove the extra indent which is reserved for
558 // the checkboxes by default as it looks ugly unless check
559 // boxes are used together with bitmaps and this is not the
560 // case in wx API
561 WinStruct<MENUINFO> mi;
562
563 // don't call SetMenuInfo() directly, this would prevent
564 // the app from starting up under Windows 95/NT 4
565 typedef BOOL (WINAPI *SetMenuInfo_t)(HMENU, MENUINFO *);
566
567 wxDynamicLibrary dllUser(wxT("user32"));
568 wxDYNLIB_FUNCTION(SetMenuInfo_t, SetMenuInfo, dllUser);
569 if ( pfnSetMenuInfo )
570 {
571 mi.fMask = MIM_STYLE;
572 mi.dwStyle = MNS_CHECKORBMP;
573 if ( !(*pfnSetMenuInfo)(GetHmenu(), &mi) )
574 {
575 wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)"));
576 }
577 }
578
579 // tell the item that it's not really owner-drawn but only
580 // needs to draw its bitmap, the rest is done by Windows
581 pItem->ResetOwnerDrawn();
582 }
583 }
584 #endif // __DMC__
585
586 if ( !ok )
587 {
588 // item draws itself, pass pointer to it in data parameter
589 flags |= MF_OWNERDRAW;
590 pData = (LPCTSTR)pItem;
591 }
592 }
593 else
594 #endif // wxUSE_OWNER_DRAWN
595 {
596 // item is just a normal string (passed in data parameter)
597 flags |= MF_STRING;
598
599 #ifdef __WXWINCE__
600 itemText = wxMenuItem::GetLabelText(itemText);
601 #endif
602
603 pData = (wxChar*)itemText.wx_str();
604 }
605
606 // item might have already been inserted by InsertMenuItem() above
607 if ( !ok )
608 {
609 if ( !::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData) )
610 {
611 wxLogLastError(wxT("InsertMenu[Item]()"));
612
613 return false;
614 }
615 }
616
617
618 // if we just appended the title, highlight it
619 if ( id == idMenuTitle )
620 {
621 // visually select the menu title
622 SetDefaultMenuItem(GetHmenu(), id);
623 }
624
625 // if we're already attached to the menubar, we must update it
626 if ( IsAttached() && GetMenuBar()->IsAttached() )
627 {
628 GetMenuBar()->Refresh();
629 }
630
631 return true;
632 }
633
634 void wxMenu::EndRadioGroup()
635 {
636 // we're not inside a radio group any longer
637 m_startRadioGroup = -1;
638 }
639
640 wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
641 {
642 wxCHECK_MSG( item, NULL, wxT("NULL item in wxMenu::DoAppend") );
643
644 bool check = false;
645
646 if ( item->GetKind() == wxITEM_RADIO )
647 {
648 int count = GetMenuItemCount();
649
650 if ( m_startRadioGroup == -1 )
651 {
652 // start a new radio group
653 m_startRadioGroup = count;
654
655 // for now it has just one element
656 item->SetAsRadioGroupStart();
657 item->SetRadioGroupEnd(m_startRadioGroup);
658
659 // ensure that we have a checked item in the radio group
660 check = true;
661 }
662 else // extend the current radio group
663 {
664 // we need to update its end item
665 item->SetRadioGroupStart(m_startRadioGroup);
666 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
667
668 if ( node )
669 {
670 node->GetData()->SetRadioGroupEnd(count);
671 }
672 else
673 {
674 wxFAIL_MSG( wxT("where is the radio group start item?") );
675 }
676 }
677 }
678 else // not a radio item
679 {
680 EndRadioGroup();
681 }
682
683 if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
684 {
685 return NULL;
686 }
687
688 if ( check )
689 {
690 // check the item initially
691 item->Check(true);
692 }
693
694 return item;
695 }
696
697 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
698 {
699 if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos))
700 return item;
701 else
702 return NULL;
703 }
704
705 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
706 {
707 // we need to find the item's position in the child list
708 size_t pos;
709 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
710 for ( pos = 0; node; pos++ )
711 {
712 if ( node->GetData() == item )
713 break;
714
715 node = node->GetNext();
716 }
717
718 // DoRemove() (unlike Remove) can only be called for an existing item!
719 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
720
721 #if wxUSE_ACCEL
722 // remove the corresponding accel from the accel table
723 int n = FindAccel(item->GetId());
724 if ( n != wxNOT_FOUND )
725 {
726 delete m_accels[n];
727
728 m_accels.RemoveAt(n);
729 }
730 //else: this item doesn't have an accel, nothing to do
731 #endif // wxUSE_ACCEL
732
733 // remove the item from the menu
734 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
735 {
736 wxLogLastError(wxT("RemoveMenu"));
737 }
738
739 if ( IsAttached() && GetMenuBar()->IsAttached() )
740 {
741 // otherwise, the change won't be visible
742 GetMenuBar()->Refresh();
743 }
744
745 // and from internal data structures
746 return wxMenuBase::DoRemove(item);
747 }
748
749 // ---------------------------------------------------------------------------
750 // accelerator helpers
751 // ---------------------------------------------------------------------------
752
753 #if wxUSE_ACCEL
754
755 // create the wxAcceleratorEntries for our accels and put them into the provided
756 // array - return the number of accels we have
757 size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
758 {
759 size_t count = GetAccelCount();
760 for ( size_t n = 0; n < count; n++ )
761 {
762 *accels++ = *m_accels[n];
763 }
764
765 return count;
766 }
767
768 wxAcceleratorTable *wxMenu::CreateAccelTable() const
769 {
770 const size_t count = m_accels.size();
771 wxScopedArray<wxAcceleratorEntry> accels(new wxAcceleratorEntry[count]);
772 CopyAccels(accels.get());
773
774 return new wxAcceleratorTable(count, accels.get());
775 }
776
777 #endif // wxUSE_ACCEL
778
779 // ---------------------------------------------------------------------------
780 // set wxMenu title
781 // ---------------------------------------------------------------------------
782
783 void wxMenu::SetTitle(const wxString& label)
784 {
785 bool hasNoTitle = m_title.empty();
786 m_title = label;
787
788 HMENU hMenu = GetHmenu();
789
790 if ( hasNoTitle )
791 {
792 if ( !label.empty() )
793 {
794 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
795 idMenuTitle, m_title.wx_str()) ||
796 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
797 {
798 wxLogLastError(wxT("InsertMenu"));
799 }
800 }
801 }
802 else
803 {
804 if ( label.empty() )
805 {
806 // remove the title and the separator after it
807 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
808 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
809 {
810 wxLogLastError(wxT("RemoveMenu"));
811 }
812 }
813 else
814 {
815 // modify the title
816 #ifdef __WXWINCE__
817 MENUITEMINFO info;
818 wxZeroMemory(info);
819 info.cbSize = sizeof(info);
820 info.fMask = MIIM_TYPE;
821 info.fType = MFT_STRING;
822 info.cch = m_title.length();
823 info.dwTypeData = const_cast<wxChar *>(m_title.wx_str());
824 if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) )
825 {
826 wxLogLastError(wxT("SetMenuItemInfo"));
827 }
828 #else
829 if ( !ModifyMenu(hMenu, 0u,
830 MF_BYPOSITION | MF_STRING,
831 idMenuTitle, m_title.wx_str()) )
832 {
833 wxLogLastError(wxT("ModifyMenu"));
834 }
835 #endif
836 }
837 }
838
839 #ifdef __WIN32__
840 // put the title string in bold face
841 if ( !m_title.empty() )
842 {
843 SetDefaultMenuItem(GetHmenu(), idMenuTitle);
844 }
845 #endif // Win32
846 }
847
848 // ---------------------------------------------------------------------------
849 // event processing
850 // ---------------------------------------------------------------------------
851
852 bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id_)
853 {
854 const int id = (signed short)id_;
855
856 // ignore commands from the menu title
857 if ( id != (int)idMenuTitle )
858 {
859 // update the check item when it's clicked
860 wxMenuItem * const item = FindItem(id);
861 if ( item && item->IsCheckable() )
862 item->Toggle();
863
864 // get the status of the menu item: note that it has been just changed
865 // by Toggle() above so here we already get the new state of the item
866 UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND);
867 SendEvent(id, menuState & MF_CHECKED);
868 }
869
870 return true;
871 }
872
873 // ---------------------------------------------------------------------------
874 // other
875 // ---------------------------------------------------------------------------
876
877 wxWindow *wxMenu::GetWindow() const
878 {
879 if ( m_invokingWindow != NULL )
880 return m_invokingWindow;
881 else if ( GetMenuBar() != NULL)
882 return GetMenuBar()->GetFrame();
883
884 return NULL;
885 }
886
887 // ---------------------------------------------------------------------------
888 // Menu Bar
889 // ---------------------------------------------------------------------------
890
891 void wxMenuBar::Init()
892 {
893 m_eventHandler = this;
894 m_hMenu = 0;
895 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
896 m_toolBar = NULL;
897 #endif
898 // Not using a combined wxToolBar/wxMenuBar? then use
899 // a commandbar in WinCE .NET just to implement the
900 // menubar.
901 #if defined(WINCE_WITH_COMMANDBAR)
902 m_commandBar = NULL;
903 m_adornmentsAdded = false;
904 #endif
905 }
906
907 wxMenuBar::wxMenuBar()
908 {
909 Init();
910 }
911
912 wxMenuBar::wxMenuBar( long WXUNUSED(style) )
913 {
914 Init();
915 }
916
917 wxMenuBar::wxMenuBar(size_t count, wxMenu *menus[], const wxString titles[], long WXUNUSED(style))
918 {
919 Init();
920
921 m_titles.Alloc(count);
922
923 for ( size_t i = 0; i < count; i++ )
924 {
925 m_menus.Append(menus[i]);
926 m_titles.Add(titles[i]);
927
928 menus[i]->Attach(this);
929 }
930 }
931
932 wxMenuBar::~wxMenuBar()
933 {
934 // In Windows CE (not .NET), the menubar is always associated
935 // with a toolbar, which destroys the menu implicitly.
936 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
937 if (GetToolBar())
938 {
939 wxToolMenuBar* toolMenuBar = wxDynamicCast(GetToolBar(), wxToolMenuBar);
940 if (toolMenuBar)
941 toolMenuBar->SetMenuBar(NULL);
942 }
943 #else
944 // we should free Windows resources only if Windows doesn't do it for us
945 // which happens if we're attached to a frame
946 if (m_hMenu && !IsAttached())
947 {
948 #if defined(WINCE_WITH_COMMANDBAR)
949 ::DestroyWindow((HWND) m_commandBar);
950 m_commandBar = (WXHWND) NULL;
951 #else
952 ::DestroyMenu((HMENU)m_hMenu);
953 #endif
954 m_hMenu = (WXHMENU)NULL;
955 }
956 #endif
957 }
958
959 // ---------------------------------------------------------------------------
960 // wxMenuBar helpers
961 // ---------------------------------------------------------------------------
962
963 void wxMenuBar::Refresh()
964 {
965 if ( IsFrozen() )
966 return;
967
968 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
969
970 #if defined(WINCE_WITHOUT_COMMANDBAR)
971 if (GetToolBar())
972 {
973 CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0);
974 }
975 #elif defined(WINCE_WITH_COMMANDBAR)
976 if (m_commandBar)
977 DrawMenuBar((HWND) m_commandBar);
978 #else
979 DrawMenuBar(GetHwndOf(GetFrame()));
980 #endif
981 }
982
983 WXHMENU wxMenuBar::Create()
984 {
985 // Note: this doesn't work at all on Smartphone,
986 // since you have to use resources.
987 // We'll have to find another way to add a menu
988 // by changing/adding menu items to an existing menu.
989 #if defined(WINCE_WITHOUT_COMMANDBAR)
990 if ( m_hMenu != 0 )
991 return m_hMenu;
992
993 wxToolMenuBar * const bar = static_cast<wxToolMenuBar *>(GetToolBar());
994 if ( !bar )
995 return NULL;
996
997 HWND hCommandBar = GetHwndOf(bar);
998
999 // notify comctl32.dll about the version of the headers we use before using
1000 // any other TB_XXX messages
1001 SendMessage(hCommandBar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
1002
1003 TBBUTTON tbButton;
1004 wxZeroMemory(tbButton);
1005 tbButton.iBitmap = I_IMAGENONE;
1006 tbButton.fsState = TBSTATE_ENABLED;
1007 tbButton.fsStyle = TBSTYLE_DROPDOWN |
1008 TBSTYLE_NO_DROPDOWN_ARROW |
1009 TBSTYLE_AUTOSIZE;
1010
1011 for ( unsigned i = 0; i < GetMenuCount(); i++ )
1012 {
1013 HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu();
1014 tbButton.dwData = (DWORD)hPopupMenu;
1015 wxString label = wxStripMenuCodes(GetMenuLabel(i));
1016 tbButton.iString = (int) label.wx_str();
1017
1018 tbButton.idCommand = NewControlId();
1019 if ( !::SendMessage(hCommandBar, TB_INSERTBUTTON, i, (LPARAM)&tbButton) )
1020 {
1021 wxLogLastError(wxT("TB_INSERTBUTTON"));
1022 }
1023 }
1024
1025 m_hMenu = bar->GetHMenu();
1026 return m_hMenu;
1027 #else // !__WXWINCE__
1028 if ( m_hMenu != 0 )
1029 return m_hMenu;
1030
1031 m_hMenu = (WXHMENU)::CreateMenu();
1032
1033 if ( !m_hMenu )
1034 {
1035 wxLogLastError(wxT("CreateMenu"));
1036 }
1037 else
1038 {
1039 size_t count = GetMenuCount(), i;
1040 wxMenuList::iterator it;
1041 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1042 {
1043 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
1044 (UINT_PTR)(*it)->GetHMenu(),
1045 m_titles[i].wx_str()) )
1046 {
1047 wxLogLastError(wxT("AppendMenu"));
1048 }
1049 }
1050 }
1051
1052 return m_hMenu;
1053 #endif // __WXWINCE__/!__WXWINCE__
1054 }
1055
1056 int wxMenuBar::MSWPositionForWxMenu(wxMenu *menu, int wxpos)
1057 {
1058 wxASSERT(menu);
1059 wxASSERT(menu->GetHMenu());
1060 wxASSERT(m_hMenu);
1061
1062 #if defined(__WXWINCE__)
1063 int totalMSWItems = GetMenuCount();
1064 #else
1065 int totalMSWItems = GetMenuItemCount((HMENU)m_hMenu);
1066 #endif
1067
1068 int i; // For old C++ compatibility
1069 for(i=wxpos; i<totalMSWItems; i++)
1070 {
1071 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
1072 return i;
1073 }
1074 for(i=0; i<wxpos; i++)
1075 {
1076 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
1077 return i;
1078 }
1079 wxFAIL;
1080 return -1;
1081 }
1082
1083 // ---------------------------------------------------------------------------
1084 // wxMenuBar functions to work with the top level submenus
1085 // ---------------------------------------------------------------------------
1086
1087 // NB: we don't support owner drawn top level items for now, if we do these
1088 // functions would have to be changed to use wxMenuItem as well
1089
1090 void wxMenuBar::EnableTop(size_t pos, bool enable)
1091 {
1092 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1093 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
1094
1095 int flag = enable ? MF_ENABLED : MF_GRAYED;
1096
1097 EnableMenuItem((HMENU)m_hMenu, MSWPositionForWxMenu(GetMenu(pos),pos), MF_BYPOSITION | flag);
1098
1099 Refresh();
1100 }
1101
1102 void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label)
1103 {
1104 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
1105
1106 m_titles[pos] = label;
1107
1108 if ( !IsAttached() )
1109 {
1110 return;
1111 }
1112 //else: have to modify the existing menu
1113
1114 int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos);
1115
1116 UINT_PTR id;
1117 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION);
1118 if ( flagsOld == 0xFFFFFFFF )
1119 {
1120 wxLogLastError(wxT("GetMenuState"));
1121
1122 return;
1123 }
1124
1125 if ( flagsOld & MF_POPUP )
1126 {
1127 // HIBYTE contains the number of items in the submenu in this case
1128 flagsOld &= 0xff;
1129 id = (UINT_PTR)::GetSubMenu((HMENU)m_hMenu, mswpos);
1130 }
1131 else
1132 {
1133 id = pos;
1134 }
1135
1136 #ifdef __WXWINCE__
1137 MENUITEMINFO info;
1138 wxZeroMemory(info);
1139 info.cbSize = sizeof(info);
1140 info.fMask = MIIM_TYPE;
1141 info.fType = MFT_STRING;
1142 info.cch = label.length();
1143 info.dwTypeData = const_cast<wxChar *>(label.wx_str());
1144 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, &info) )
1145 {
1146 wxLogLastError(wxT("SetMenuItemInfo"));
1147 }
1148
1149 #else
1150 if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld,
1151 id, label.wx_str()) == (int)0xFFFFFFFF )
1152 {
1153 wxLogLastError(wxT("ModifyMenu"));
1154 }
1155 #endif
1156
1157 Refresh();
1158 }
1159
1160 wxString wxMenuBar::GetMenuLabel(size_t pos) const
1161 {
1162 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
1163 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1164
1165 return m_titles[pos];
1166 }
1167
1168 // ---------------------------------------------------------------------------
1169 // wxMenuBar construction
1170 // ---------------------------------------------------------------------------
1171
1172 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1173 {
1174 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
1175 if ( !menuOld )
1176 return NULL;
1177
1178 m_titles[pos] = title;
1179
1180 #if defined(WINCE_WITHOUT_COMMANDBAR)
1181 if (IsAttached())
1182 #else
1183 if (GetHmenu())
1184 #endif
1185 {
1186 int mswpos = MSWPositionForWxMenu(menuOld,pos);
1187
1188 // can't use ModifyMenu() because it deletes the submenu it replaces
1189 if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) )
1190 {
1191 wxLogLastError(wxT("RemoveMenu"));
1192 }
1193
1194 if ( !::InsertMenu(GetHmenu(), (UINT)mswpos,
1195 MF_BYPOSITION | MF_POPUP | MF_STRING,
1196 (UINT_PTR)GetHmenuOf(menu), title.wx_str()) )
1197 {
1198 wxLogLastError(wxT("InsertMenu"));
1199 }
1200
1201 #if wxUSE_ACCEL
1202 if ( menuOld->HasAccels() || menu->HasAccels() )
1203 {
1204 // need to rebuild accell table
1205 RebuildAccelTable();
1206 }
1207 #endif // wxUSE_ACCEL
1208
1209 if (IsAttached())
1210 Refresh();
1211 }
1212
1213 return menuOld;
1214 }
1215
1216 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1217 {
1218 // Find out which MSW item before which we'll be inserting before
1219 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1220 // If IsAttached() is false this won't be used anyway
1221 bool isAttached =
1222 #if defined(WINCE_WITHOUT_COMMANDBAR)
1223 IsAttached();
1224 #else
1225 (GetHmenu() != 0);
1226 #endif
1227
1228 int mswpos = (!isAttached || (pos == m_menus.GetCount()))
1229 ? -1 // append the menu
1230 : MSWPositionForWxMenu(GetMenu(pos),pos);
1231
1232 if ( !wxMenuBarBase::Insert(pos, menu, title) )
1233 return false;
1234
1235 m_titles.Insert(title, pos);
1236
1237 if ( isAttached )
1238 {
1239 #if defined(WINCE_WITHOUT_COMMANDBAR)
1240 if (!GetToolBar())
1241 return false;
1242 TBBUTTON tbButton;
1243 memset(&tbButton, 0, sizeof(TBBUTTON));
1244 tbButton.iBitmap = I_IMAGENONE;
1245 tbButton.fsState = TBSTATE_ENABLED;
1246 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
1247
1248 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1249 tbButton.dwData = (DWORD)hPopupMenu;
1250 wxString label = wxStripMenuCodes(title);
1251 tbButton.iString = (int) label.wx_str();
1252
1253 tbButton.idCommand = NewControlId();
1254 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1255 {
1256 wxLogLastError(wxT("TB_INSERTBUTTON"));
1257 return false;
1258 }
1259 wxUnusedVar(mswpos);
1260 #else
1261 if ( !::InsertMenu(GetHmenu(), mswpos,
1262 MF_BYPOSITION | MF_POPUP | MF_STRING,
1263 (UINT_PTR)GetHmenuOf(menu), title.wx_str()) )
1264 {
1265 wxLogLastError(wxT("InsertMenu"));
1266 }
1267 #endif
1268 #if wxUSE_ACCEL
1269 if ( menu->HasAccels() )
1270 {
1271 // need to rebuild accell table
1272 RebuildAccelTable();
1273 }
1274 #endif // wxUSE_ACCEL
1275
1276 if (IsAttached())
1277 Refresh();
1278 }
1279
1280 return true;
1281 }
1282
1283 bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
1284 {
1285 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
1286 wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") );
1287
1288 if ( !wxMenuBarBase::Append(menu, title) )
1289 return false;
1290
1291 m_titles.Add(title);
1292
1293 #if defined(WINCE_WITHOUT_COMMANDBAR)
1294 if (IsAttached())
1295 #else
1296 if (GetHmenu())
1297 #endif
1298 {
1299 #if defined(WINCE_WITHOUT_COMMANDBAR)
1300 if (!GetToolBar())
1301 return false;
1302 TBBUTTON tbButton;
1303 memset(&tbButton, 0, sizeof(TBBUTTON));
1304 tbButton.iBitmap = I_IMAGENONE;
1305 tbButton.fsState = TBSTATE_ENABLED;
1306 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
1307
1308 size_t pos = GetMenuCount();
1309 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1310 tbButton.dwData = (DWORD)hPopupMenu;
1311 wxString label = wxStripMenuCodes(title);
1312 tbButton.iString = (int) label.wx_str();
1313
1314 tbButton.idCommand = NewControlId();
1315 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1316 {
1317 wxLogLastError(wxT("TB_INSERTBUTTON"));
1318 return false;
1319 }
1320 #else
1321 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
1322 (UINT_PTR)submenu, title.wx_str()) )
1323 {
1324 wxLogLastError(wxT("AppendMenu"));
1325 }
1326 #endif
1327
1328 #if wxUSE_ACCEL
1329 if ( menu->HasAccels() )
1330 {
1331 // need to rebuild accelerator table
1332 RebuildAccelTable();
1333 }
1334 #endif // wxUSE_ACCEL
1335
1336 if (IsAttached())
1337 Refresh();
1338 }
1339
1340 return true;
1341 }
1342
1343 wxMenu *wxMenuBar::Remove(size_t pos)
1344 {
1345 wxMenu *menu = wxMenuBarBase::Remove(pos);
1346 if ( !menu )
1347 return NULL;
1348
1349 #if defined(WINCE_WITHOUT_COMMANDBAR)
1350 if (IsAttached())
1351 #else
1352 if (GetHmenu())
1353 #endif
1354 {
1355 #if defined(WINCE_WITHOUT_COMMANDBAR)
1356 if (GetToolBar())
1357 {
1358 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0))
1359 {
1360 wxLogLastError(wxT("TB_DELETEBUTTON"));
1361 }
1362 }
1363 #else
1364 if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) )
1365 {
1366 wxLogLastError(wxT("RemoveMenu"));
1367 }
1368 #endif
1369
1370 #if wxUSE_ACCEL
1371 if ( menu->HasAccels() )
1372 {
1373 // need to rebuild accell table
1374 RebuildAccelTable();
1375 }
1376 #endif // wxUSE_ACCEL
1377
1378 if (IsAttached())
1379 Refresh();
1380 }
1381
1382 m_titles.RemoveAt(pos);
1383
1384 return menu;
1385 }
1386
1387 #if wxUSE_ACCEL
1388
1389 void wxMenuBar::RebuildAccelTable()
1390 {
1391 // merge the accelerators of all menus into one accel table
1392 size_t nAccelCount = 0;
1393 size_t i, count = GetMenuCount();
1394 wxMenuList::iterator it;
1395 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1396 {
1397 nAccelCount += (*it)->GetAccelCount();
1398 }
1399
1400 if ( nAccelCount )
1401 {
1402 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
1403
1404 nAccelCount = 0;
1405 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1406 {
1407 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
1408 }
1409
1410 SetAcceleratorTable(wxAcceleratorTable(nAccelCount, accelEntries));
1411
1412 delete [] accelEntries;
1413 }
1414 }
1415
1416 #endif // wxUSE_ACCEL
1417
1418 void wxMenuBar::Attach(wxFrame *frame)
1419 {
1420 wxMenuBarBase::Attach(frame);
1421
1422 #if defined(WINCE_WITH_COMMANDBAR)
1423 if (!m_hMenu)
1424 this->Create();
1425 if (!m_commandBar)
1426 m_commandBar = (WXHWND) CommandBar_Create(wxGetInstance(), (HWND) frame->GetHWND(), NewControlId());
1427 if (m_commandBar)
1428 {
1429 if (m_hMenu)
1430 {
1431 if (!CommandBar_InsertMenubarEx((HWND) m_commandBar, NULL, (LPTSTR) m_hMenu, 0))
1432 {
1433 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1434 }
1435 }
1436 }
1437 #endif
1438
1439 #if wxUSE_ACCEL
1440 RebuildAccelTable();
1441 #endif // wxUSE_ACCEL
1442 }
1443
1444 #if defined(WINCE_WITH_COMMANDBAR)
1445 bool wxMenuBar::AddAdornments(long style)
1446 {
1447 if (m_adornmentsAdded || !m_commandBar)
1448 return false;
1449
1450 if (style & wxCLOSE_BOX)
1451 {
1452 if (!CommandBar_AddAdornments((HWND) m_commandBar, 0, 0))
1453 {
1454 wxLogLastError(wxT("CommandBar_AddAdornments"));
1455 }
1456 else
1457 {
1458 return true;
1459 }
1460 }
1461 return false;
1462 }
1463 #endif
1464
1465 void wxMenuBar::Detach()
1466 {
1467 wxMenuBarBase::Detach();
1468 }
1469
1470 #endif // wxUSE_MENUS