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