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