]> git.saurik.com Git - wxWidgets.git/blob - src/msw/menu.cpp
a64d5bcbb151eee3212909b64b8efda0c1e8e1d5
[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 #endif // wxUSE_OWNER_DRAWN
275
276 // create the menu
277 m_hMenu = (WXHMENU)CreatePopupMenu();
278 if ( !m_hMenu )
279 {
280 wxLogLastError(wxT("CreatePopupMenu"));
281 }
282
283 // if we have a title, insert it in the beginning of the menu
284 if ( !m_title.empty() )
285 {
286 Append(idMenuTitle, m_title);
287 AppendSeparator();
288 }
289 }
290
291 // The wxWindow destructor will take care of deleting the submenus.
292 wxMenu::~wxMenu()
293 {
294 // we should free Windows resources only if Windows doesn't do it for us
295 // which happens if we're attached to a menubar or a submenu of another
296 // menu
297 if ( !IsAttached() && !GetParent() )
298 {
299 if ( !::DestroyMenu(GetHmenu()) )
300 {
301 wxLogLastError(wxT("DestroyMenu"));
302 }
303 }
304
305 #if wxUSE_ACCEL
306 // delete accels
307 WX_CLEAR_ARRAY(m_accels);
308 #endif // wxUSE_ACCEL
309 }
310
311 void wxMenu::Break()
312 {
313 // this will take effect during the next call to Append()
314 m_doBreak = true;
315 }
316
317 void wxMenu::Attach(wxMenuBarBase *menubar)
318 {
319 wxMenuBase::Attach(menubar);
320
321 EndRadioGroup();
322 }
323
324 #if wxUSE_ACCEL
325
326 int wxMenu::FindAccel(int id) const
327 {
328 size_t n, count = m_accels.GetCount();
329 for ( n = 0; n < count; n++ )
330 {
331 if ( m_accels[n]->m_command == id )
332 return n;
333 }
334
335 return wxNOT_FOUND;
336 }
337
338 void wxMenu::UpdateAccel(wxMenuItem *item)
339 {
340 if ( item->IsSubMenu() )
341 {
342 wxMenu *submenu = item->GetSubMenu();
343 wxMenuItemList::compatibility_iterator node = submenu->GetMenuItems().GetFirst();
344 while ( node )
345 {
346 UpdateAccel(node->GetData());
347
348 node = node->GetNext();
349 }
350 }
351 else if ( !item->IsSeparator() )
352 {
353 // recurse upwards: we should only modify m_accels of the top level
354 // menus, not of the submenus as wxMenuBar doesn't look at them
355 // (alternative and arguable cleaner solution would be to recurse
356 // downwards in GetAccelCount() and CopyAccels())
357 if ( GetParent() )
358 {
359 GetParent()->UpdateAccel(item);
360 return;
361 }
362
363 // find the (new) accel for this item
364 wxAcceleratorEntry *accel = wxAcceleratorEntry::Create(item->GetItemLabel());
365 if ( accel )
366 accel->m_command = item->GetId();
367
368 // find the old one
369 int n = FindAccel(item->GetId());
370 if ( n == wxNOT_FOUND )
371 {
372 // no old, add new if any
373 if ( accel )
374 m_accels.Add(accel);
375 else
376 return; // skipping RebuildAccelTable() below
377 }
378 else
379 {
380 // replace old with new or just remove the old one if no new
381 delete m_accels[n];
382 if ( accel )
383 m_accels[n] = accel;
384 else
385 m_accels.RemoveAt(n);
386 }
387
388 if ( IsAttached() )
389 {
390 GetMenuBar()->RebuildAccelTable();
391 }
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() && !IsLessThanStdSize(bmpUnchecked)) ||
530 (bmpChecked.Ok() && !IsLessThanStdSize(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 // only update our margin for equals alignment to other item
663 else if ( !updateAllMargins )
664 {
665 pItem->SetMarginWidth(m_maxBitmapWidth);
666 }
667 }
668 }
669 else
670 #endif // wxUSE_OWNER_DRAWN
671 {
672 // item is just a normal string (passed in data parameter)
673 flags |= MF_STRING;
674
675 #ifdef __WXWINCE__
676 itemText = wxMenuItem::GetLabelText(itemText);
677 #endif
678
679 pData = (wxChar*)itemText.wx_str();
680 }
681
682 // item might have already been inserted by InsertMenuItem() above
683 if ( !ok )
684 {
685 if ( !::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData) )
686 {
687 wxLogLastError(wxT("InsertMenu[Item]()"));
688
689 return false;
690 }
691 }
692
693
694 // if we just appended the title, highlight it
695 if ( id == idMenuTitle )
696 {
697 // visually select the menu title
698 SetDefaultMenuItem(GetHmenu(), id);
699 }
700
701 // if we're already attached to the menubar, we must update it
702 if ( IsAttached() && GetMenuBar()->IsAttached() )
703 {
704 GetMenuBar()->Refresh();
705 }
706
707 return true;
708 }
709
710 void wxMenu::EndRadioGroup()
711 {
712 // we're not inside a radio group any longer
713 m_startRadioGroup = -1;
714 }
715
716 wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
717 {
718 wxCHECK_MSG( item, NULL, wxT("NULL item in wxMenu::DoAppend") );
719
720 bool check = false;
721
722 if ( item->GetKind() == wxITEM_RADIO )
723 {
724 int count = GetMenuItemCount();
725
726 if ( m_startRadioGroup == -1 )
727 {
728 // start a new radio group
729 m_startRadioGroup = count;
730
731 // for now it has just one element
732 item->SetAsRadioGroupStart();
733 item->SetRadioGroupEnd(m_startRadioGroup);
734
735 // ensure that we have a checked item in the radio group
736 check = true;
737 }
738 else // extend the current radio group
739 {
740 // we need to update its end item
741 item->SetRadioGroupStart(m_startRadioGroup);
742 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
743
744 if ( node )
745 {
746 node->GetData()->SetRadioGroupEnd(count);
747 }
748 else
749 {
750 wxFAIL_MSG( wxT("where is the radio group start item?") );
751 }
752 }
753 }
754 else // not a radio item
755 {
756 EndRadioGroup();
757 }
758
759 if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
760 {
761 return NULL;
762 }
763
764 if ( check )
765 {
766 // check the item initially
767 item->Check(true);
768 }
769
770 return item;
771 }
772
773 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
774 {
775 if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos))
776 return item;
777 else
778 return NULL;
779 }
780
781 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
782 {
783 // we need to find the item's position in the child list
784 size_t pos;
785 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
786 for ( pos = 0; node; pos++ )
787 {
788 if ( node->GetData() == item )
789 break;
790
791 node = node->GetNext();
792 }
793
794 // DoRemove() (unlike Remove) can only be called for an existing item!
795 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
796
797 #if wxUSE_ACCEL
798 // remove the corresponding accel from the accel table
799 int n = FindAccel(item->GetId());
800 if ( n != wxNOT_FOUND )
801 {
802 delete m_accels[n];
803
804 m_accels.RemoveAt(n);
805 }
806 //else: this item doesn't have an accel, nothing to do
807 #endif // wxUSE_ACCEL
808
809 // remove the item from the menu
810 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
811 {
812 wxLogLastError(wxT("RemoveMenu"));
813 }
814
815 if ( IsAttached() && GetMenuBar()->IsAttached() )
816 {
817 // otherwise, the change won't be visible
818 GetMenuBar()->Refresh();
819 }
820
821 // and from internal data structures
822 return wxMenuBase::DoRemove(item);
823 }
824
825 // ---------------------------------------------------------------------------
826 // accelerator helpers
827 // ---------------------------------------------------------------------------
828
829 #if wxUSE_ACCEL
830
831 // create the wxAcceleratorEntries for our accels and put them into the provided
832 // array - return the number of accels we have
833 size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
834 {
835 size_t count = GetAccelCount();
836 for ( size_t n = 0; n < count; n++ )
837 {
838 *accels++ = *m_accels[n];
839 }
840
841 return count;
842 }
843
844 wxAcceleratorTable *wxMenu::CreateAccelTable() const
845 {
846 const size_t count = m_accels.size();
847 wxScopedArray<wxAcceleratorEntry> accels(new wxAcceleratorEntry[count]);
848 CopyAccels(accels.get());
849
850 return new wxAcceleratorTable(count, accels.get());
851 }
852
853 #endif // wxUSE_ACCEL
854
855 // ---------------------------------------------------------------------------
856 // set wxMenu title
857 // ---------------------------------------------------------------------------
858
859 void wxMenu::SetTitle(const wxString& label)
860 {
861 bool hasNoTitle = m_title.empty();
862 m_title = label;
863
864 HMENU hMenu = GetHmenu();
865
866 if ( hasNoTitle )
867 {
868 if ( !label.empty() )
869 {
870 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
871 idMenuTitle, m_title.wx_str()) ||
872 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
873 {
874 wxLogLastError(wxT("InsertMenu"));
875 }
876 }
877 }
878 else
879 {
880 if ( label.empty() )
881 {
882 // remove the title and the separator after it
883 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
884 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
885 {
886 wxLogLastError(wxT("RemoveMenu"));
887 }
888 }
889 else
890 {
891 // modify the title
892 #ifdef __WXWINCE__
893 MENUITEMINFO info;
894 wxZeroMemory(info);
895 info.cbSize = sizeof(info);
896 info.fMask = MIIM_TYPE;
897 info.fType = MFT_STRING;
898 info.cch = m_title.length();
899 info.dwTypeData = const_cast<wxChar *>(m_title.wx_str());
900 if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) )
901 {
902 wxLogLastError(wxT("SetMenuItemInfo"));
903 }
904 #else
905 if ( !ModifyMenu(hMenu, 0u,
906 MF_BYPOSITION | MF_STRING,
907 idMenuTitle, m_title.wx_str()) )
908 {
909 wxLogLastError(wxT("ModifyMenu"));
910 }
911 #endif
912 }
913 }
914
915 #ifdef __WIN32__
916 // put the title string in bold face
917 if ( !m_title.empty() )
918 {
919 SetDefaultMenuItem(GetHmenu(), idMenuTitle);
920 }
921 #endif // Win32
922 }
923
924 // ---------------------------------------------------------------------------
925 // event processing
926 // ---------------------------------------------------------------------------
927
928 bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id_)
929 {
930 const int id = (signed short)id_;
931
932 // ignore commands from the menu title
933 if ( id != (int)idMenuTitle )
934 {
935 // update the check item when it's clicked
936 wxMenuItem * const item = FindItem(id);
937 if ( item && item->IsCheckable() )
938 item->Toggle();
939
940 // get the status of the menu item: note that it has been just changed
941 // by Toggle() above so here we already get the new state of the item
942 UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND);
943 SendEvent(id, menuState & MF_CHECKED);
944 }
945
946 return true;
947 }
948
949 // ---------------------------------------------------------------------------
950 // other
951 // ---------------------------------------------------------------------------
952
953 wxWindow *wxMenu::GetWindow() const
954 {
955 if ( m_invokingWindow != NULL )
956 return m_invokingWindow;
957 else if ( GetMenuBar() != NULL)
958 return GetMenuBar()->GetFrame();
959
960 return NULL;
961 }
962
963 // ---------------------------------------------------------------------------
964 // Menu Bar
965 // ---------------------------------------------------------------------------
966
967 void wxMenuBar::Init()
968 {
969 m_eventHandler = this;
970 m_hMenu = 0;
971 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
972 m_toolBar = NULL;
973 #endif
974 // Not using a combined wxToolBar/wxMenuBar? then use
975 // a commandbar in WinCE .NET just to implement the
976 // menubar.
977 #if defined(WINCE_WITH_COMMANDBAR)
978 m_commandBar = NULL;
979 m_adornmentsAdded = false;
980 #endif
981 }
982
983 wxMenuBar::wxMenuBar()
984 {
985 Init();
986 }
987
988 wxMenuBar::wxMenuBar( long WXUNUSED(style) )
989 {
990 Init();
991 }
992
993 wxMenuBar::wxMenuBar(size_t count, wxMenu *menus[], const wxString titles[], long WXUNUSED(style))
994 {
995 Init();
996
997 m_titles.Alloc(count);
998
999 for ( size_t i = 0; i < count; i++ )
1000 {
1001 m_menus.Append(menus[i]);
1002 m_titles.Add(titles[i]);
1003
1004 menus[i]->Attach(this);
1005 }
1006 }
1007
1008 wxMenuBar::~wxMenuBar()
1009 {
1010 // In Windows CE (not .NET), the menubar is always associated
1011 // with a toolbar, which destroys the menu implicitly.
1012 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
1013 if (GetToolBar())
1014 {
1015 wxToolMenuBar* toolMenuBar = wxDynamicCast(GetToolBar(), wxToolMenuBar);
1016 if (toolMenuBar)
1017 toolMenuBar->SetMenuBar(NULL);
1018 }
1019 #else
1020 // we should free Windows resources only if Windows doesn't do it for us
1021 // which happens if we're attached to a frame
1022 if (m_hMenu && !IsAttached())
1023 {
1024 #if defined(WINCE_WITH_COMMANDBAR)
1025 ::DestroyWindow((HWND) m_commandBar);
1026 m_commandBar = (WXHWND) NULL;
1027 #else
1028 ::DestroyMenu((HMENU)m_hMenu);
1029 #endif
1030 m_hMenu = (WXHMENU)NULL;
1031 }
1032 #endif
1033 }
1034
1035 // ---------------------------------------------------------------------------
1036 // wxMenuBar helpers
1037 // ---------------------------------------------------------------------------
1038
1039 void wxMenuBar::Refresh()
1040 {
1041 if ( IsFrozen() )
1042 return;
1043
1044 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
1045
1046 #if defined(WINCE_WITHOUT_COMMANDBAR)
1047 if (GetToolBar())
1048 {
1049 CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0);
1050 }
1051 #elif defined(WINCE_WITH_COMMANDBAR)
1052 if (m_commandBar)
1053 DrawMenuBar((HWND) m_commandBar);
1054 #else
1055 DrawMenuBar(GetHwndOf(GetFrame()));
1056 #endif
1057 }
1058
1059 WXHMENU wxMenuBar::Create()
1060 {
1061 // Note: this doesn't work at all on Smartphone,
1062 // since you have to use resources.
1063 // We'll have to find another way to add a menu
1064 // by changing/adding menu items to an existing menu.
1065 #if defined(WINCE_WITHOUT_COMMANDBAR)
1066 if ( m_hMenu != 0 )
1067 return m_hMenu;
1068
1069 wxToolMenuBar * const bar = static_cast<wxToolMenuBar *>(GetToolBar());
1070 if ( !bar )
1071 return NULL;
1072
1073 HWND hCommandBar = GetHwndOf(bar);
1074
1075 // notify comctl32.dll about the version of the headers we use before using
1076 // any other TB_XXX messages
1077 SendMessage(hCommandBar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
1078
1079 TBBUTTON tbButton;
1080 wxZeroMemory(tbButton);
1081 tbButton.iBitmap = I_IMAGENONE;
1082 tbButton.fsState = TBSTATE_ENABLED;
1083 tbButton.fsStyle = TBSTYLE_DROPDOWN |
1084 TBSTYLE_NO_DROPDOWN_ARROW |
1085 TBSTYLE_AUTOSIZE;
1086
1087 for ( unsigned i = 0; i < GetMenuCount(); i++ )
1088 {
1089 HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu();
1090 tbButton.dwData = (DWORD)hPopupMenu;
1091 wxString label = wxStripMenuCodes(GetMenuLabel(i));
1092 tbButton.iString = (int) label.wx_str();
1093
1094 tbButton.idCommand = NewControlId();
1095 if ( !::SendMessage(hCommandBar, TB_INSERTBUTTON, i, (LPARAM)&tbButton) )
1096 {
1097 wxLogLastError(wxT("TB_INSERTBUTTON"));
1098 }
1099 }
1100
1101 m_hMenu = bar->GetHMenu();
1102 return m_hMenu;
1103 #else // !__WXWINCE__
1104 if ( m_hMenu != 0 )
1105 return m_hMenu;
1106
1107 m_hMenu = (WXHMENU)::CreateMenu();
1108
1109 if ( !m_hMenu )
1110 {
1111 wxLogLastError(wxT("CreateMenu"));
1112 }
1113 else
1114 {
1115 size_t count = GetMenuCount(), i;
1116 wxMenuList::iterator it;
1117 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1118 {
1119 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
1120 (UINT_PTR)(*it)->GetHMenu(),
1121 m_titles[i].wx_str()) )
1122 {
1123 wxLogLastError(wxT("AppendMenu"));
1124 }
1125 }
1126 }
1127
1128 return m_hMenu;
1129 #endif // __WXWINCE__/!__WXWINCE__
1130 }
1131
1132 int wxMenuBar::MSWPositionForWxMenu(wxMenu *menu, int wxpos)
1133 {
1134 wxASSERT(menu);
1135 wxASSERT(menu->GetHMenu());
1136 wxASSERT(m_hMenu);
1137
1138 #if defined(__WXWINCE__)
1139 int totalMSWItems = GetMenuCount();
1140 #else
1141 int totalMSWItems = GetMenuItemCount((HMENU)m_hMenu);
1142 #endif
1143
1144 int i; // For old C++ compatibility
1145 for(i=wxpos; i<totalMSWItems; i++)
1146 {
1147 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
1148 return i;
1149 }
1150 for(i=0; i<wxpos; i++)
1151 {
1152 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
1153 return i;
1154 }
1155 wxFAIL;
1156 return -1;
1157 }
1158
1159 // ---------------------------------------------------------------------------
1160 // wxMenuBar functions to work with the top level submenus
1161 // ---------------------------------------------------------------------------
1162
1163 // NB: we don't support owner drawn top level items for now, if we do these
1164 // functions would have to be changed to use wxMenuItem as well
1165
1166 void wxMenuBar::EnableTop(size_t pos, bool enable)
1167 {
1168 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1169 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
1170
1171 int flag = enable ? MF_ENABLED : MF_GRAYED;
1172
1173 EnableMenuItem((HMENU)m_hMenu, MSWPositionForWxMenu(GetMenu(pos),pos), MF_BYPOSITION | flag);
1174
1175 Refresh();
1176 }
1177
1178 void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label)
1179 {
1180 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
1181
1182 m_titles[pos] = label;
1183
1184 if ( !IsAttached() )
1185 {
1186 return;
1187 }
1188 //else: have to modify the existing menu
1189
1190 int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos);
1191
1192 UINT_PTR id;
1193 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION);
1194 if ( flagsOld == 0xFFFFFFFF )
1195 {
1196 wxLogLastError(wxT("GetMenuState"));
1197
1198 return;
1199 }
1200
1201 if ( flagsOld & MF_POPUP )
1202 {
1203 // HIBYTE contains the number of items in the submenu in this case
1204 flagsOld &= 0xff;
1205 id = (UINT_PTR)::GetSubMenu((HMENU)m_hMenu, mswpos);
1206 }
1207 else
1208 {
1209 id = pos;
1210 }
1211
1212 #ifdef __WXWINCE__
1213 MENUITEMINFO info;
1214 wxZeroMemory(info);
1215 info.cbSize = sizeof(info);
1216 info.fMask = MIIM_TYPE;
1217 info.fType = MFT_STRING;
1218 info.cch = label.length();
1219 info.dwTypeData = const_cast<wxChar *>(label.wx_str());
1220 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, &info) )
1221 {
1222 wxLogLastError(wxT("SetMenuItemInfo"));
1223 }
1224
1225 #else
1226 if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld,
1227 id, label.wx_str()) == (int)0xFFFFFFFF )
1228 {
1229 wxLogLastError(wxT("ModifyMenu"));
1230 }
1231 #endif
1232
1233 Refresh();
1234 }
1235
1236 wxString wxMenuBar::GetMenuLabel(size_t pos) const
1237 {
1238 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
1239 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1240
1241 return m_titles[pos];
1242 }
1243
1244 // ---------------------------------------------------------------------------
1245 // wxMenuBar construction
1246 // ---------------------------------------------------------------------------
1247
1248 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1249 {
1250 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
1251 if ( !menuOld )
1252 return NULL;
1253
1254 m_titles[pos] = title;
1255
1256 #if defined(WINCE_WITHOUT_COMMANDBAR)
1257 if (IsAttached())
1258 #else
1259 if (GetHmenu())
1260 #endif
1261 {
1262 int mswpos = MSWPositionForWxMenu(menuOld,pos);
1263
1264 // can't use ModifyMenu() because it deletes the submenu it replaces
1265 if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) )
1266 {
1267 wxLogLastError(wxT("RemoveMenu"));
1268 }
1269
1270 if ( !::InsertMenu(GetHmenu(), (UINT)mswpos,
1271 MF_BYPOSITION | MF_POPUP | MF_STRING,
1272 (UINT_PTR)GetHmenuOf(menu), title.wx_str()) )
1273 {
1274 wxLogLastError(wxT("InsertMenu"));
1275 }
1276
1277 #if wxUSE_ACCEL
1278 if ( menuOld->HasAccels() || menu->HasAccels() )
1279 {
1280 // need to rebuild accell table
1281 RebuildAccelTable();
1282 }
1283 #endif // wxUSE_ACCEL
1284
1285 if (IsAttached())
1286 Refresh();
1287 }
1288
1289 return menuOld;
1290 }
1291
1292 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1293 {
1294 // Find out which MSW item before which we'll be inserting before
1295 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1296 // If IsAttached() is false this won't be used anyway
1297 bool isAttached =
1298 #if defined(WINCE_WITHOUT_COMMANDBAR)
1299 IsAttached();
1300 #else
1301 (GetHmenu() != 0);
1302 #endif
1303
1304 int mswpos = (!isAttached || (pos == m_menus.GetCount()))
1305 ? -1 // append the menu
1306 : MSWPositionForWxMenu(GetMenu(pos),pos);
1307
1308 if ( !wxMenuBarBase::Insert(pos, menu, title) )
1309 return false;
1310
1311 m_titles.Insert(title, pos);
1312
1313 if ( isAttached )
1314 {
1315 #if defined(WINCE_WITHOUT_COMMANDBAR)
1316 if (!GetToolBar())
1317 return false;
1318 TBBUTTON tbButton;
1319 memset(&tbButton, 0, sizeof(TBBUTTON));
1320 tbButton.iBitmap = I_IMAGENONE;
1321 tbButton.fsState = TBSTATE_ENABLED;
1322 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
1323
1324 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1325 tbButton.dwData = (DWORD)hPopupMenu;
1326 wxString label = wxStripMenuCodes(title);
1327 tbButton.iString = (int) label.wx_str();
1328
1329 tbButton.idCommand = NewControlId();
1330 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1331 {
1332 wxLogLastError(wxT("TB_INSERTBUTTON"));
1333 return false;
1334 }
1335 wxUnusedVar(mswpos);
1336 #else
1337 if ( !::InsertMenu(GetHmenu(), mswpos,
1338 MF_BYPOSITION | MF_POPUP | MF_STRING,
1339 (UINT_PTR)GetHmenuOf(menu), title.wx_str()) )
1340 {
1341 wxLogLastError(wxT("InsertMenu"));
1342 }
1343 #endif
1344 #if wxUSE_ACCEL
1345 if ( menu->HasAccels() )
1346 {
1347 // need to rebuild accell table
1348 RebuildAccelTable();
1349 }
1350 #endif // wxUSE_ACCEL
1351
1352 if (IsAttached())
1353 Refresh();
1354 }
1355
1356 return true;
1357 }
1358
1359 bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
1360 {
1361 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
1362 wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") );
1363
1364 if ( !wxMenuBarBase::Append(menu, title) )
1365 return false;
1366
1367 m_titles.Add(title);
1368
1369 #if defined(WINCE_WITHOUT_COMMANDBAR)
1370 if (IsAttached())
1371 #else
1372 if (GetHmenu())
1373 #endif
1374 {
1375 #if defined(WINCE_WITHOUT_COMMANDBAR)
1376 if (!GetToolBar())
1377 return false;
1378 TBBUTTON tbButton;
1379 memset(&tbButton, 0, sizeof(TBBUTTON));
1380 tbButton.iBitmap = I_IMAGENONE;
1381 tbButton.fsState = TBSTATE_ENABLED;
1382 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
1383
1384 size_t pos = GetMenuCount();
1385 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1386 tbButton.dwData = (DWORD)hPopupMenu;
1387 wxString label = wxStripMenuCodes(title);
1388 tbButton.iString = (int) label.wx_str();
1389
1390 tbButton.idCommand = NewControlId();
1391 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1392 {
1393 wxLogLastError(wxT("TB_INSERTBUTTON"));
1394 return false;
1395 }
1396 #else
1397 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
1398 (UINT_PTR)submenu, title.wx_str()) )
1399 {
1400 wxLogLastError(wxT("AppendMenu"));
1401 }
1402 #endif
1403
1404 #if wxUSE_ACCEL
1405 if ( menu->HasAccels() )
1406 {
1407 // need to rebuild accelerator table
1408 RebuildAccelTable();
1409 }
1410 #endif // wxUSE_ACCEL
1411
1412 if (IsAttached())
1413 Refresh();
1414 }
1415
1416 return true;
1417 }
1418
1419 wxMenu *wxMenuBar::Remove(size_t pos)
1420 {
1421 wxMenu *menu = wxMenuBarBase::Remove(pos);
1422 if ( !menu )
1423 return NULL;
1424
1425 #if defined(WINCE_WITHOUT_COMMANDBAR)
1426 if (IsAttached())
1427 #else
1428 if (GetHmenu())
1429 #endif
1430 {
1431 #if defined(WINCE_WITHOUT_COMMANDBAR)
1432 if (GetToolBar())
1433 {
1434 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0))
1435 {
1436 wxLogLastError(wxT("TB_DELETEBUTTON"));
1437 }
1438 }
1439 #else
1440 if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) )
1441 {
1442 wxLogLastError(wxT("RemoveMenu"));
1443 }
1444 #endif
1445
1446 #if wxUSE_ACCEL
1447 if ( menu->HasAccels() )
1448 {
1449 // need to rebuild accell table
1450 RebuildAccelTable();
1451 }
1452 #endif // wxUSE_ACCEL
1453
1454 if (IsAttached())
1455 Refresh();
1456 }
1457
1458 m_titles.RemoveAt(pos);
1459
1460 return menu;
1461 }
1462
1463 #if wxUSE_ACCEL
1464
1465 void wxMenuBar::RebuildAccelTable()
1466 {
1467 // merge the accelerators of all menus into one accel table
1468 size_t nAccelCount = 0;
1469 size_t i, count = GetMenuCount();
1470 wxMenuList::iterator it;
1471 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1472 {
1473 nAccelCount += (*it)->GetAccelCount();
1474 }
1475
1476 if ( nAccelCount )
1477 {
1478 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
1479
1480 nAccelCount = 0;
1481 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1482 {
1483 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
1484 }
1485
1486 SetAcceleratorTable(wxAcceleratorTable(nAccelCount, accelEntries));
1487
1488 delete [] accelEntries;
1489 }
1490 }
1491
1492 #endif // wxUSE_ACCEL
1493
1494 void wxMenuBar::Attach(wxFrame *frame)
1495 {
1496 wxMenuBarBase::Attach(frame);
1497
1498 #if defined(WINCE_WITH_COMMANDBAR)
1499 if (!m_hMenu)
1500 this->Create();
1501 if (!m_commandBar)
1502 m_commandBar = (WXHWND) CommandBar_Create(wxGetInstance(), (HWND) frame->GetHWND(), NewControlId());
1503 if (m_commandBar)
1504 {
1505 if (m_hMenu)
1506 {
1507 if (!CommandBar_InsertMenubarEx((HWND) m_commandBar, NULL, (LPTSTR) m_hMenu, 0))
1508 {
1509 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1510 }
1511 }
1512 }
1513 #endif
1514
1515 #if wxUSE_ACCEL
1516 RebuildAccelTable();
1517 #endif // wxUSE_ACCEL
1518 }
1519
1520 #if defined(WINCE_WITH_COMMANDBAR)
1521 bool wxMenuBar::AddAdornments(long style)
1522 {
1523 if (m_adornmentsAdded || !m_commandBar)
1524 return false;
1525
1526 if (style & wxCLOSE_BOX)
1527 {
1528 if (!CommandBar_AddAdornments((HWND) m_commandBar, 0, 0))
1529 {
1530 wxLogLastError(wxT("CommandBar_AddAdornments"));
1531 }
1532 else
1533 {
1534 return true;
1535 }
1536 }
1537 return false;
1538 }
1539 #endif
1540
1541 void wxMenuBar::Detach()
1542 {
1543 wxMenuBarBase::Detach();
1544 }
1545
1546 #endif // wxUSE_MENUS