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