]> git.saurik.com Git - wxWidgets.git/blob - src/msw/menu.cpp
Set up menu bitmaps correctly for checkable items.
[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(checked));
403 }
404 //else: bitmap is not set
405
406 return NULL;
407 }
408 #endif // wxUSE_IMAGE
409
410 return HBMMENU_CALLBACK;
411 }
412
413 } // anonymous namespace
414
415 // append a new item or submenu to the menu
416 bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos)
417 {
418 #if wxUSE_ACCEL
419 UpdateAccel(pItem);
420 #endif // wxUSE_ACCEL
421
422 // we should support disabling the item even prior to adding it to the menu
423 UINT flags = pItem->IsEnabled() ? MF_ENABLED : MF_GRAYED;
424
425 // if "Break" has just been called, insert a menu break before this item
426 // (and don't forget to reset the flag)
427 if ( m_doBreak ) {
428 flags |= MF_MENUBREAK;
429 m_doBreak = false;
430 }
431
432 if ( pItem->IsSeparator() ) {
433 flags |= MF_SEPARATOR;
434 }
435
436 // id is the numeric id for normal menu items and HMENU for submenus as
437 // required by ::AppendMenu() API
438 UINT_PTR id;
439 wxMenu *submenu = pItem->GetSubMenu();
440 if ( submenu != NULL ) {
441 wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
442
443 submenu->SetParent(this);
444
445 id = (UINT_PTR)submenu->GetHMenu();
446
447 flags |= MF_POPUP;
448 }
449 else {
450 id = pItem->GetMSWId();
451 }
452
453
454 // prepare to insert the item in the menu
455 wxString itemText = pItem->GetItemLabel();
456 LPCTSTR pData = NULL;
457 if ( pos == (size_t)-1 )
458 {
459 // append at the end (note that the item is already appended to
460 // internal data structures)
461 pos = GetMenuItemCount() - 1;
462 }
463
464 // adjust position to account for the title, if any
465 if ( !m_title.empty() )
466 pos += 2; // for the title itself and its separator
467
468 BOOL ok = false;
469
470 #if wxUSE_OWNER_DRAWN
471 // Under older systems mixing owner-drawn and non-owner-drawn items results
472 // in inconsistent margins, so we force this one to be owner-drawn if any
473 // other items already are. Later we might want to use a boolean in the
474 // wxMenu to avoid search. Also we might make this fix unnecessary by
475 // getting the correct margin using NONCLIENTMETRICS.
476 static const wxWinVersion winver = wxGetWinVersion();
477 if ( winver < wxWinVersion_XP &&
478 !pItem->IsOwnerDrawn() && !pItem->IsSeparator() )
479 {
480 // Check if any other items are ownerdrawn, and make ownerdrawn if so
481 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
482 while (node)
483 {
484 if (node->GetData()->IsOwnerDrawn())
485 {
486 pItem->SetOwnerDrawn(true);
487 break;
488 }
489 node = node->GetNext();
490 }
491 }
492 #endif // wxUSE_OWNER_DRAWN
493
494 // check if we have something more than a simple text item
495 #if wxUSE_OWNER_DRAWN
496 if ( pItem->IsOwnerDrawn() )
497 {
498 #ifndef __DMC__
499 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
500 // drawn item under the previous versions and we also have to use them
501 // in any case if the item has custom colours or font
502 bool mustUseOwnerDrawn = winver < wxWinVersion_98 ||
503 pItem->GetTextColour().Ok() ||
504 pItem->GetBackgroundColour().Ok() ||
505 pItem->GetFont().Ok();
506 if ( !mustUseOwnerDrawn )
507 {
508 const wxBitmap& bmpUnchecked = pItem->GetBitmap(false),
509 bmpChecked = pItem->GetBitmap(true);
510 if ( (bmpUnchecked.Ok() && !IsLessThanStdSize(bmpUnchecked)) ||
511 (bmpChecked.Ok() && !IsLessThanStdSize(bmpChecked)) )
512 {
513 mustUseOwnerDrawn = true;
514 }
515 }
516
517 // use InsertMenuItem() if possible as it's guaranteed to look correct
518 // while our owner-drawn code is not
519 if ( !mustUseOwnerDrawn )
520 {
521 WinStruct<MENUITEMINFO> mii;
522 mii.fMask = MIIM_STRING | MIIM_DATA;
523
524 // don't set hbmpItem for the checkable items as it would
525 // be used for both checked and unchecked state
526 if ( pItem->IsCheckable() )
527 {
528 mii.fMask |= MIIM_CHECKMARKS;
529 mii.hbmpChecked = GetHBitmapForMenu(pItem, true);
530 mii.hbmpUnchecked = GetHBitmapForMenu(pItem, false);
531 }
532 else if ( pItem->GetBitmap().IsOk() )
533 {
534 mii.fMask |= MIIM_BITMAP;
535 mii.hbmpItem = GetHBitmapForMenu(pItem);
536 }
537
538 mii.cch = itemText.length();
539 mii.dwTypeData = const_cast<wxChar *>(itemText.wx_str());
540
541 if ( flags & MF_POPUP )
542 {
543 mii.fMask |= MIIM_SUBMENU;
544 mii.hSubMenu = GetHmenuOf(pItem->GetSubMenu());
545 }
546 else
547 {
548 mii.fMask |= MIIM_ID;
549 mii.wID = id;
550 }
551
552 mii.dwItemData = reinterpret_cast<ULONG_PTR>(pItem);
553
554 ok = ::InsertMenuItem(GetHmenu(), pos, TRUE /* by pos */, &mii);
555 if ( !ok )
556 {
557 wxLogLastError(wxT("InsertMenuItem()"));
558 }
559 else // InsertMenuItem() ok
560 {
561 // we need to remove the extra indent which is reserved for
562 // the checkboxes by default as it looks ugly unless check
563 // boxes are used together with bitmaps and this is not the
564 // case in wx API
565 WinStruct<MENUINFO> mi;
566
567 // don't call SetMenuInfo() directly, this would prevent
568 // the app from starting up under Windows 95/NT 4
569 typedef BOOL (WINAPI *SetMenuInfo_t)(HMENU, MENUINFO *);
570
571 wxDynamicLibrary dllUser(wxT("user32"));
572 wxDYNLIB_FUNCTION(SetMenuInfo_t, SetMenuInfo, dllUser);
573 if ( pfnSetMenuInfo )
574 {
575 mi.fMask = MIM_STYLE;
576 mi.dwStyle = MNS_CHECKORBMP;
577 if ( !(*pfnSetMenuInfo)(GetHmenu(), &mi) )
578 {
579 wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)"));
580 }
581 }
582
583 // tell the item that it's not really owner-drawn but only
584 // needs to draw its bitmap, the rest is done by Windows
585 pItem->SetOwnerDrawn(false);
586 }
587 }
588 #endif // __DMC__
589
590 if ( !ok )
591 {
592 // item draws itself, pass pointer to it in data parameter
593 flags |= MF_OWNERDRAW;
594 pData = (LPCTSTR)pItem;
595 }
596 }
597 else
598 #endif // wxUSE_OWNER_DRAWN
599 {
600 // item is just a normal string (passed in data parameter)
601 flags |= MF_STRING;
602
603 #ifdef __WXWINCE__
604 itemText = wxMenuItem::GetLabelText(itemText);
605 #endif
606
607 pData = (wxChar*)itemText.wx_str();
608 }
609
610 // item might have already been inserted by InsertMenuItem() above
611 if ( !ok )
612 {
613 if ( !::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData) )
614 {
615 wxLogLastError(wxT("InsertMenu[Item]()"));
616
617 return false;
618 }
619 }
620
621
622 // if we just appended the title, highlight it
623 if ( id == idMenuTitle )
624 {
625 // visually select the menu title
626 SetDefaultMenuItem(GetHmenu(), id);
627 }
628
629 // if we're already attached to the menubar, we must update it
630 if ( IsAttached() && GetMenuBar()->IsAttached() )
631 {
632 GetMenuBar()->Refresh();
633 }
634
635 return true;
636 }
637
638 void wxMenu::EndRadioGroup()
639 {
640 // we're not inside a radio group any longer
641 m_startRadioGroup = -1;
642 }
643
644 wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
645 {
646 wxCHECK_MSG( item, NULL, wxT("NULL item in wxMenu::DoAppend") );
647
648 bool check = false;
649
650 if ( item->GetKind() == wxITEM_RADIO )
651 {
652 int count = GetMenuItemCount();
653
654 if ( m_startRadioGroup == -1 )
655 {
656 // start a new radio group
657 m_startRadioGroup = count;
658
659 // for now it has just one element
660 item->SetAsRadioGroupStart();
661 item->SetRadioGroupEnd(m_startRadioGroup);
662
663 // ensure that we have a checked item in the radio group
664 check = true;
665 }
666 else // extend the current radio group
667 {
668 // we need to update its end item
669 item->SetRadioGroupStart(m_startRadioGroup);
670 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
671
672 if ( node )
673 {
674 node->GetData()->SetRadioGroupEnd(count);
675 }
676 else
677 {
678 wxFAIL_MSG( wxT("where is the radio group start item?") );
679 }
680 }
681 }
682 else // not a radio item
683 {
684 EndRadioGroup();
685 }
686
687 if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
688 {
689 return NULL;
690 }
691
692 if ( check )
693 {
694 // check the item initially
695 item->Check(true);
696 }
697
698 return item;
699 }
700
701 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
702 {
703 if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos))
704 return item;
705 else
706 return NULL;
707 }
708
709 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
710 {
711 // we need to find the item's position in the child list
712 size_t pos;
713 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
714 for ( pos = 0; node; pos++ )
715 {
716 if ( node->GetData() == item )
717 break;
718
719 node = node->GetNext();
720 }
721
722 // DoRemove() (unlike Remove) can only be called for an existing item!
723 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
724
725 #if wxUSE_ACCEL
726 // remove the corresponding accel from the accel table
727 int n = FindAccel(item->GetId());
728 if ( n != wxNOT_FOUND )
729 {
730 delete m_accels[n];
731
732 m_accels.RemoveAt(n);
733 }
734 //else: this item doesn't have an accel, nothing to do
735 #endif // wxUSE_ACCEL
736
737 // remove the item from the menu
738 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
739 {
740 wxLogLastError(wxT("RemoveMenu"));
741 }
742
743 if ( IsAttached() && GetMenuBar()->IsAttached() )
744 {
745 // otherwise, the change won't be visible
746 GetMenuBar()->Refresh();
747 }
748
749 // and from internal data structures
750 return wxMenuBase::DoRemove(item);
751 }
752
753 // ---------------------------------------------------------------------------
754 // accelerator helpers
755 // ---------------------------------------------------------------------------
756
757 #if wxUSE_ACCEL
758
759 // create the wxAcceleratorEntries for our accels and put them into the provided
760 // array - return the number of accels we have
761 size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
762 {
763 size_t count = GetAccelCount();
764 for ( size_t n = 0; n < count; n++ )
765 {
766 *accels++ = *m_accels[n];
767 }
768
769 return count;
770 }
771
772 wxAcceleratorTable *wxMenu::CreateAccelTable() const
773 {
774 const size_t count = m_accels.size();
775 wxScopedArray<wxAcceleratorEntry> accels(new wxAcceleratorEntry[count]);
776 CopyAccels(accels.get());
777
778 return new wxAcceleratorTable(count, accels.get());
779 }
780
781 #endif // wxUSE_ACCEL
782
783 // ---------------------------------------------------------------------------
784 // set wxMenu title
785 // ---------------------------------------------------------------------------
786
787 void wxMenu::SetTitle(const wxString& label)
788 {
789 bool hasNoTitle = m_title.empty();
790 m_title = label;
791
792 HMENU hMenu = GetHmenu();
793
794 if ( hasNoTitle )
795 {
796 if ( !label.empty() )
797 {
798 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
799 idMenuTitle, m_title.wx_str()) ||
800 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
801 {
802 wxLogLastError(wxT("InsertMenu"));
803 }
804 }
805 }
806 else
807 {
808 if ( label.empty() )
809 {
810 // remove the title and the separator after it
811 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
812 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
813 {
814 wxLogLastError(wxT("RemoveMenu"));
815 }
816 }
817 else
818 {
819 // modify the title
820 #ifdef __WXWINCE__
821 MENUITEMINFO info;
822 wxZeroMemory(info);
823 info.cbSize = sizeof(info);
824 info.fMask = MIIM_TYPE;
825 info.fType = MFT_STRING;
826 info.cch = m_title.length();
827 info.dwTypeData = const_cast<wxChar *>(m_title.wx_str());
828 if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) )
829 {
830 wxLogLastError(wxT("SetMenuItemInfo"));
831 }
832 #else
833 if ( !ModifyMenu(hMenu, 0u,
834 MF_BYPOSITION | MF_STRING,
835 idMenuTitle, m_title.wx_str()) )
836 {
837 wxLogLastError(wxT("ModifyMenu"));
838 }
839 #endif
840 }
841 }
842
843 #ifdef __WIN32__
844 // put the title string in bold face
845 if ( !m_title.empty() )
846 {
847 SetDefaultMenuItem(GetHmenu(), idMenuTitle);
848 }
849 #endif // Win32
850 }
851
852 // ---------------------------------------------------------------------------
853 // event processing
854 // ---------------------------------------------------------------------------
855
856 bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id_)
857 {
858 const int id = (signed short)id_;
859
860 // ignore commands from the menu title
861 if ( id != (int)idMenuTitle )
862 {
863 // update the check item when it's clicked
864 wxMenuItem * const item = FindItem(id);
865 if ( item && item->IsCheckable() )
866 item->Toggle();
867
868 // get the status of the menu item: note that it has been just changed
869 // by Toggle() above so here we already get the new state of the item
870 UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND);
871 SendEvent(id, menuState & MF_CHECKED);
872 }
873
874 return true;
875 }
876
877 // ---------------------------------------------------------------------------
878 // other
879 // ---------------------------------------------------------------------------
880
881 wxWindow *wxMenu::GetWindow() const
882 {
883 if ( m_invokingWindow != NULL )
884 return m_invokingWindow;
885 else if ( GetMenuBar() != NULL)
886 return GetMenuBar()->GetFrame();
887
888 return NULL;
889 }
890
891 // ---------------------------------------------------------------------------
892 // Menu Bar
893 // ---------------------------------------------------------------------------
894
895 void wxMenuBar::Init()
896 {
897 m_eventHandler = this;
898 m_hMenu = 0;
899 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
900 m_toolBar = NULL;
901 #endif
902 // Not using a combined wxToolBar/wxMenuBar? then use
903 // a commandbar in WinCE .NET just to implement the
904 // menubar.
905 #if defined(WINCE_WITH_COMMANDBAR)
906 m_commandBar = NULL;
907 m_adornmentsAdded = false;
908 #endif
909 }
910
911 wxMenuBar::wxMenuBar()
912 {
913 Init();
914 }
915
916 wxMenuBar::wxMenuBar( long WXUNUSED(style) )
917 {
918 Init();
919 }
920
921 wxMenuBar::wxMenuBar(size_t count, wxMenu *menus[], const wxString titles[], long WXUNUSED(style))
922 {
923 Init();
924
925 m_titles.Alloc(count);
926
927 for ( size_t i = 0; i < count; i++ )
928 {
929 m_menus.Append(menus[i]);
930 m_titles.Add(titles[i]);
931
932 menus[i]->Attach(this);
933 }
934 }
935
936 wxMenuBar::~wxMenuBar()
937 {
938 // In Windows CE (not .NET), the menubar is always associated
939 // with a toolbar, which destroys the menu implicitly.
940 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
941 if (GetToolBar())
942 {
943 wxToolMenuBar* toolMenuBar = wxDynamicCast(GetToolBar(), wxToolMenuBar);
944 if (toolMenuBar)
945 toolMenuBar->SetMenuBar(NULL);
946 }
947 #else
948 // we should free Windows resources only if Windows doesn't do it for us
949 // which happens if we're attached to a frame
950 if (m_hMenu && !IsAttached())
951 {
952 #if defined(WINCE_WITH_COMMANDBAR)
953 ::DestroyWindow((HWND) m_commandBar);
954 m_commandBar = (WXHWND) NULL;
955 #else
956 ::DestroyMenu((HMENU)m_hMenu);
957 #endif
958 m_hMenu = (WXHMENU)NULL;
959 }
960 #endif
961 }
962
963 // ---------------------------------------------------------------------------
964 // wxMenuBar helpers
965 // ---------------------------------------------------------------------------
966
967 void wxMenuBar::Refresh()
968 {
969 if ( IsFrozen() )
970 return;
971
972 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
973
974 #if defined(WINCE_WITHOUT_COMMANDBAR)
975 if (GetToolBar())
976 {
977 CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0);
978 }
979 #elif defined(WINCE_WITH_COMMANDBAR)
980 if (m_commandBar)
981 DrawMenuBar((HWND) m_commandBar);
982 #else
983 DrawMenuBar(GetHwndOf(GetFrame()));
984 #endif
985 }
986
987 WXHMENU wxMenuBar::Create()
988 {
989 // Note: this doesn't work at all on Smartphone,
990 // since you have to use resources.
991 // We'll have to find another way to add a menu
992 // by changing/adding menu items to an existing menu.
993 #if defined(WINCE_WITHOUT_COMMANDBAR)
994 if ( m_hMenu != 0 )
995 return m_hMenu;
996
997 wxToolMenuBar * const bar = static_cast<wxToolMenuBar *>(GetToolBar());
998 if ( !bar )
999 return NULL;
1000
1001 HWND hCommandBar = GetHwndOf(bar);
1002
1003 // notify comctl32.dll about the version of the headers we use before using
1004 // any other TB_XXX messages
1005 SendMessage(hCommandBar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
1006
1007 TBBUTTON tbButton;
1008 wxZeroMemory(tbButton);
1009 tbButton.iBitmap = I_IMAGENONE;
1010 tbButton.fsState = TBSTATE_ENABLED;
1011 tbButton.fsStyle = TBSTYLE_DROPDOWN |
1012 TBSTYLE_NO_DROPDOWN_ARROW |
1013 TBSTYLE_AUTOSIZE;
1014
1015 for ( unsigned i = 0; i < GetMenuCount(); i++ )
1016 {
1017 HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu();
1018 tbButton.dwData = (DWORD)hPopupMenu;
1019 wxString label = wxStripMenuCodes(GetMenuLabel(i));
1020 tbButton.iString = (int) label.wx_str();
1021
1022 tbButton.idCommand = NewControlId();
1023 if ( !::SendMessage(hCommandBar, TB_INSERTBUTTON, i, (LPARAM)&tbButton) )
1024 {
1025 wxLogLastError(wxT("TB_INSERTBUTTON"));
1026 }
1027 }
1028
1029 m_hMenu = bar->GetHMenu();
1030 return m_hMenu;
1031 #else // !__WXWINCE__
1032 if ( m_hMenu != 0 )
1033 return m_hMenu;
1034
1035 m_hMenu = (WXHMENU)::CreateMenu();
1036
1037 if ( !m_hMenu )
1038 {
1039 wxLogLastError(wxT("CreateMenu"));
1040 }
1041 else
1042 {
1043 size_t count = GetMenuCount(), i;
1044 wxMenuList::iterator it;
1045 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1046 {
1047 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
1048 (UINT_PTR)(*it)->GetHMenu(),
1049 m_titles[i].wx_str()) )
1050 {
1051 wxLogLastError(wxT("AppendMenu"));
1052 }
1053 }
1054 }
1055
1056 return m_hMenu;
1057 #endif // __WXWINCE__/!__WXWINCE__
1058 }
1059
1060 int wxMenuBar::MSWPositionForWxMenu(wxMenu *menu, int wxpos)
1061 {
1062 wxASSERT(menu);
1063 wxASSERT(menu->GetHMenu());
1064 wxASSERT(m_hMenu);
1065
1066 #if defined(__WXWINCE__)
1067 int totalMSWItems = GetMenuCount();
1068 #else
1069 int totalMSWItems = GetMenuItemCount((HMENU)m_hMenu);
1070 #endif
1071
1072 int i; // For old C++ compatibility
1073 for(i=wxpos; i<totalMSWItems; i++)
1074 {
1075 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
1076 return i;
1077 }
1078 for(i=0; i<wxpos; i++)
1079 {
1080 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
1081 return i;
1082 }
1083 wxFAIL;
1084 return -1;
1085 }
1086
1087 // ---------------------------------------------------------------------------
1088 // wxMenuBar functions to work with the top level submenus
1089 // ---------------------------------------------------------------------------
1090
1091 // NB: we don't support owner drawn top level items for now, if we do these
1092 // functions would have to be changed to use wxMenuItem as well
1093
1094 void wxMenuBar::EnableTop(size_t pos, bool enable)
1095 {
1096 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1097 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
1098
1099 int flag = enable ? MF_ENABLED : MF_GRAYED;
1100
1101 EnableMenuItem((HMENU)m_hMenu, MSWPositionForWxMenu(GetMenu(pos),pos), MF_BYPOSITION | flag);
1102
1103 Refresh();
1104 }
1105
1106 void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label)
1107 {
1108 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
1109
1110 m_titles[pos] = label;
1111
1112 if ( !IsAttached() )
1113 {
1114 return;
1115 }
1116 //else: have to modify the existing menu
1117
1118 int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos);
1119
1120 UINT_PTR id;
1121 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION);
1122 if ( flagsOld == 0xFFFFFFFF )
1123 {
1124 wxLogLastError(wxT("GetMenuState"));
1125
1126 return;
1127 }
1128
1129 if ( flagsOld & MF_POPUP )
1130 {
1131 // HIBYTE contains the number of items in the submenu in this case
1132 flagsOld &= 0xff;
1133 id = (UINT_PTR)::GetSubMenu((HMENU)m_hMenu, mswpos);
1134 }
1135 else
1136 {
1137 id = pos;
1138 }
1139
1140 #ifdef __WXWINCE__
1141 MENUITEMINFO info;
1142 wxZeroMemory(info);
1143 info.cbSize = sizeof(info);
1144 info.fMask = MIIM_TYPE;
1145 info.fType = MFT_STRING;
1146 info.cch = label.length();
1147 info.dwTypeData = const_cast<wxChar *>(label.wx_str());
1148 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, &info) )
1149 {
1150 wxLogLastError(wxT("SetMenuItemInfo"));
1151 }
1152
1153 #else
1154 if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld,
1155 id, label.wx_str()) == (int)0xFFFFFFFF )
1156 {
1157 wxLogLastError(wxT("ModifyMenu"));
1158 }
1159 #endif
1160
1161 Refresh();
1162 }
1163
1164 wxString wxMenuBar::GetMenuLabel(size_t pos) const
1165 {
1166 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
1167 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1168
1169 return m_titles[pos];
1170 }
1171
1172 // ---------------------------------------------------------------------------
1173 // wxMenuBar construction
1174 // ---------------------------------------------------------------------------
1175
1176 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1177 {
1178 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
1179 if ( !menuOld )
1180 return NULL;
1181
1182 m_titles[pos] = title;
1183
1184 #if defined(WINCE_WITHOUT_COMMANDBAR)
1185 if (IsAttached())
1186 #else
1187 if (GetHmenu())
1188 #endif
1189 {
1190 int mswpos = MSWPositionForWxMenu(menuOld,pos);
1191
1192 // can't use ModifyMenu() because it deletes the submenu it replaces
1193 if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) )
1194 {
1195 wxLogLastError(wxT("RemoveMenu"));
1196 }
1197
1198 if ( !::InsertMenu(GetHmenu(), (UINT)mswpos,
1199 MF_BYPOSITION | MF_POPUP | MF_STRING,
1200 (UINT_PTR)GetHmenuOf(menu), title.wx_str()) )
1201 {
1202 wxLogLastError(wxT("InsertMenu"));
1203 }
1204
1205 #if wxUSE_ACCEL
1206 if ( menuOld->HasAccels() || menu->HasAccels() )
1207 {
1208 // need to rebuild accell table
1209 RebuildAccelTable();
1210 }
1211 #endif // wxUSE_ACCEL
1212
1213 if (IsAttached())
1214 Refresh();
1215 }
1216
1217 return menuOld;
1218 }
1219
1220 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1221 {
1222 // Find out which MSW item before which we'll be inserting before
1223 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1224 // If IsAttached() is false this won't be used anyway
1225 bool isAttached =
1226 #if defined(WINCE_WITHOUT_COMMANDBAR)
1227 IsAttached();
1228 #else
1229 (GetHmenu() != 0);
1230 #endif
1231
1232 int mswpos = (!isAttached || (pos == m_menus.GetCount()))
1233 ? -1 // append the menu
1234 : MSWPositionForWxMenu(GetMenu(pos),pos);
1235
1236 if ( !wxMenuBarBase::Insert(pos, menu, title) )
1237 return false;
1238
1239 m_titles.Insert(title, pos);
1240
1241 if ( isAttached )
1242 {
1243 #if defined(WINCE_WITHOUT_COMMANDBAR)
1244 if (!GetToolBar())
1245 return false;
1246 TBBUTTON tbButton;
1247 memset(&tbButton, 0, sizeof(TBBUTTON));
1248 tbButton.iBitmap = I_IMAGENONE;
1249 tbButton.fsState = TBSTATE_ENABLED;
1250 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
1251
1252 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1253 tbButton.dwData = (DWORD)hPopupMenu;
1254 wxString label = wxStripMenuCodes(title);
1255 tbButton.iString = (int) label.wx_str();
1256
1257 tbButton.idCommand = NewControlId();
1258 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1259 {
1260 wxLogLastError(wxT("TB_INSERTBUTTON"));
1261 return false;
1262 }
1263 wxUnusedVar(mswpos);
1264 #else
1265 if ( !::InsertMenu(GetHmenu(), mswpos,
1266 MF_BYPOSITION | MF_POPUP | MF_STRING,
1267 (UINT_PTR)GetHmenuOf(menu), title.wx_str()) )
1268 {
1269 wxLogLastError(wxT("InsertMenu"));
1270 }
1271 #endif
1272 #if wxUSE_ACCEL
1273 if ( menu->HasAccels() )
1274 {
1275 // need to rebuild accell table
1276 RebuildAccelTable();
1277 }
1278 #endif // wxUSE_ACCEL
1279
1280 if (IsAttached())
1281 Refresh();
1282 }
1283
1284 return true;
1285 }
1286
1287 bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
1288 {
1289 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
1290 wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") );
1291
1292 if ( !wxMenuBarBase::Append(menu, title) )
1293 return false;
1294
1295 m_titles.Add(title);
1296
1297 #if defined(WINCE_WITHOUT_COMMANDBAR)
1298 if (IsAttached())
1299 #else
1300 if (GetHmenu())
1301 #endif
1302 {
1303 #if defined(WINCE_WITHOUT_COMMANDBAR)
1304 if (!GetToolBar())
1305 return false;
1306 TBBUTTON tbButton;
1307 memset(&tbButton, 0, sizeof(TBBUTTON));
1308 tbButton.iBitmap = I_IMAGENONE;
1309 tbButton.fsState = TBSTATE_ENABLED;
1310 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
1311
1312 size_t pos = GetMenuCount();
1313 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1314 tbButton.dwData = (DWORD)hPopupMenu;
1315 wxString label = wxStripMenuCodes(title);
1316 tbButton.iString = (int) label.wx_str();
1317
1318 tbButton.idCommand = NewControlId();
1319 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1320 {
1321 wxLogLastError(wxT("TB_INSERTBUTTON"));
1322 return false;
1323 }
1324 #else
1325 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
1326 (UINT_PTR)submenu, title.wx_str()) )
1327 {
1328 wxLogLastError(wxT("AppendMenu"));
1329 }
1330 #endif
1331
1332 #if wxUSE_ACCEL
1333 if ( menu->HasAccels() )
1334 {
1335 // need to rebuild accelerator table
1336 RebuildAccelTable();
1337 }
1338 #endif // wxUSE_ACCEL
1339
1340 if (IsAttached())
1341 Refresh();
1342 }
1343
1344 return true;
1345 }
1346
1347 wxMenu *wxMenuBar::Remove(size_t pos)
1348 {
1349 wxMenu *menu = wxMenuBarBase::Remove(pos);
1350 if ( !menu )
1351 return NULL;
1352
1353 #if defined(WINCE_WITHOUT_COMMANDBAR)
1354 if (IsAttached())
1355 #else
1356 if (GetHmenu())
1357 #endif
1358 {
1359 #if defined(WINCE_WITHOUT_COMMANDBAR)
1360 if (GetToolBar())
1361 {
1362 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0))
1363 {
1364 wxLogLastError(wxT("TB_DELETEBUTTON"));
1365 }
1366 }
1367 #else
1368 if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) )
1369 {
1370 wxLogLastError(wxT("RemoveMenu"));
1371 }
1372 #endif
1373
1374 #if wxUSE_ACCEL
1375 if ( menu->HasAccels() )
1376 {
1377 // need to rebuild accell table
1378 RebuildAccelTable();
1379 }
1380 #endif // wxUSE_ACCEL
1381
1382 if (IsAttached())
1383 Refresh();
1384 }
1385
1386 m_titles.RemoveAt(pos);
1387
1388 return menu;
1389 }
1390
1391 #if wxUSE_ACCEL
1392
1393 void wxMenuBar::RebuildAccelTable()
1394 {
1395 // merge the accelerators of all menus into one accel table
1396 size_t nAccelCount = 0;
1397 size_t i, count = GetMenuCount();
1398 wxMenuList::iterator it;
1399 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1400 {
1401 nAccelCount += (*it)->GetAccelCount();
1402 }
1403
1404 if ( nAccelCount )
1405 {
1406 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
1407
1408 nAccelCount = 0;
1409 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1410 {
1411 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
1412 }
1413
1414 SetAcceleratorTable(wxAcceleratorTable(nAccelCount, accelEntries));
1415
1416 delete [] accelEntries;
1417 }
1418 }
1419
1420 #endif // wxUSE_ACCEL
1421
1422 void wxMenuBar::Attach(wxFrame *frame)
1423 {
1424 wxMenuBarBase::Attach(frame);
1425
1426 #if defined(WINCE_WITH_COMMANDBAR)
1427 if (!m_hMenu)
1428 this->Create();
1429 if (!m_commandBar)
1430 m_commandBar = (WXHWND) CommandBar_Create(wxGetInstance(), (HWND) frame->GetHWND(), NewControlId());
1431 if (m_commandBar)
1432 {
1433 if (m_hMenu)
1434 {
1435 if (!CommandBar_InsertMenubarEx((HWND) m_commandBar, NULL, (LPTSTR) m_hMenu, 0))
1436 {
1437 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1438 }
1439 }
1440 }
1441 #endif
1442
1443 #if wxUSE_ACCEL
1444 RebuildAccelTable();
1445 #endif // wxUSE_ACCEL
1446 }
1447
1448 #if defined(WINCE_WITH_COMMANDBAR)
1449 bool wxMenuBar::AddAdornments(long style)
1450 {
1451 if (m_adornmentsAdded || !m_commandBar)
1452 return false;
1453
1454 if (style & wxCLOSE_BOX)
1455 {
1456 if (!CommandBar_AddAdornments((HWND) m_commandBar, 0, 0))
1457 {
1458 wxLogLastError(wxT("CommandBar_AddAdornments"));
1459 }
1460 else
1461 {
1462 return true;
1463 }
1464 }
1465 return false;
1466 }
1467 #endif
1468
1469 void wxMenuBar::Detach()
1470 {
1471 wxMenuBarBase::Detach();
1472 }
1473
1474 #endif // wxUSE_MENUS