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