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