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