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