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