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