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