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