1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/msw/menu.cpp 
   3 // Purpose:     wxMenu, wxMenuBar, wxMenuItem 
   4 // Author:      Julian Smart 
   5 // Modified by: Vadim Zeitlin 
   8 // Copyright:   (c) Julian Smart 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // =========================================================================== 
  14 // =========================================================================== 
  16 // --------------------------------------------------------------------------- 
  18 // --------------------------------------------------------------------------- 
  20 // For compilers that support precompilation, includes "wx.h". 
  21 #include "wx/wxprec.h" 
  39     #include "wx/ownerdrw.h" 
  42 #include "wx/msw/private.h" 
  44 // include <commctrl.h> "properly" 
  45 #include "wx/msw/wrapcctl.h" 
  53 #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__) 
  57 #include "wx/msw/wince/missing.h" 
  61 // other standard headers 
  64 #if wxUSE_OWNER_DRAWN && defined(MIIM_BITMAP) 
  65     #include "wx/dynlib.h" 
  68 #ifndef MNS_CHECKORBMP 
  69     #define MNS_CHECKORBMP 0x04000000 
  72     #define MIM_STYLE 0x00000010 
  75 // ---------------------------------------------------------------------------- 
  77 // ---------------------------------------------------------------------------- 
  79 // ---------------------------------------------------------------------------- 
  81 // ---------------------------------------------------------------------------- 
  83 // the (popup) menu title has this special id 
  84 static const int idMenuTitle 
= -3; 
  86 // ---------------------------------------------------------------------------- 
  88 // ---------------------------------------------------------------------------- 
  90 // make the given menu item default 
  91 static void SetDefaultMenuItem(HMENU 
WXUNUSED_IN_WINCE(hmenu
), 
  92                                UINT 
WXUNUSED_IN_WINCE(id
)) 
  97     mii
.cbSize 
= sizeof(MENUITEMINFO
); 
  98     mii
.fMask 
= MIIM_STATE
; 
  99     mii
.fState 
= MFS_DEFAULT
; 
 101     if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) ) 
 103         wxLogLastError(wxT("SetMenuItemInfo")); 
 109 UINT 
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
) 
 113     info
.cbSize 
= sizeof(info
); 
 114     info
.fMask 
= MIIM_STATE
; 
 115     // MF_BYCOMMAND is zero so test MF_BYPOSITION 
 116     if ( !::GetMenuItemInfo(hMenu
, id
, flags 
& MF_BYPOSITION 
? TRUE 
: FALSE 
, & info
) ) 
 117         wxLogLastError(wxT("GetMenuItemInfo")); 
 122 // ============================================================================ 
 124 // ============================================================================ 
 126 #include "wx/listimpl.cpp" 
 128 WX_DEFINE_LIST( wxMenuInfoList 
) 
 130 #if wxUSE_EXTENDED_RTTI 
 132 WX_DEFINE_FLAGS( wxMenuStyle 
) 
 134 wxBEGIN_FLAGS( wxMenuStyle 
) 
 135     wxFLAGS_MEMBER(wxMENU_TEAROFF
) 
 136 wxEND_FLAGS( wxMenuStyle 
) 
 138 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h") 
 140 wxCOLLECTION_TYPE_INFO( wxMenuItem 
* , wxMenuItemList 
) ; 
 142 template<> void wxCollectionToVariantArray( wxMenuItemList 
const &theList
, wxxVariantArray 
&value
) 
 144     wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList 
, value 
) ; 
 147 wxBEGIN_PROPERTIES_TABLE(wxMenu
) 
 148     wxEVENT_PROPERTY( Select 
, wxEVT_COMMAND_MENU_SELECTED 
, wxCommandEvent
) 
 149     wxPROPERTY( Title
, wxString 
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) 
 150     wxREADONLY_PROPERTY_FLAGS( MenuStyle 
, wxMenuStyle 
, long , GetStyle 
, EMPTY_MACROVALUE 
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style 
 151     wxPROPERTY_COLLECTION( MenuItems 
, wxMenuItemList 
, wxMenuItem
* , Append 
, GetMenuItems 
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) 
 152 wxEND_PROPERTIES_TABLE() 
 154 wxBEGIN_HANDLERS_TABLE(wxMenu
) 
 155 wxEND_HANDLERS_TABLE() 
 157 wxDIRECT_CONSTRUCTOR_2( wxMenu 
, wxString 
, Title 
, long , MenuStyle  
) 
 159 WX_DEFINE_FLAGS( wxMenuBarStyle 
) 
 161 wxBEGIN_FLAGS( wxMenuBarStyle 
) 
 162     wxFLAGS_MEMBER(wxMB_DOCKABLE
) 
 163 wxEND_FLAGS( wxMenuBarStyle 
) 
 165 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise 
 166 bool wxMenuBarStreamingCallback( const wxObject 
*WXUNUSED(object
), wxWriter 
* , wxPersister 
* , wxxVariantArray 
& ) 
 171 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow 
,"wx/menu.h",wxMenuBarStreamingCallback
) 
 173 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject 
, "wx/menu.h" ) 
 175 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
) 
 176     wxREADONLY_PROPERTY( Menu 
, wxMenu
* , GetMenu 
, EMPTY_MACROVALUE 
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) 
 177     wxREADONLY_PROPERTY( Title 
, wxString 
, GetTitle 
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) 
 178 wxEND_PROPERTIES_TABLE() 
 180 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
) 
 181 wxEND_HANDLERS_TABLE() 
 183 wxCONSTRUCTOR_2( wxMenuInfo 
, wxMenu
* , Menu 
, wxString 
, Title 
) 
 185 wxCOLLECTION_TYPE_INFO( wxMenuInfo 
* , wxMenuInfoList 
) ; 
 187 template<> void wxCollectionToVariantArray( wxMenuInfoList 
const &theList
, wxxVariantArray 
&value
) 
 189     wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList 
, value 
) ; 
 192 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
) 
 193     wxPROPERTY_COLLECTION( MenuInfos 
, wxMenuInfoList 
, wxMenuInfo
* , Append 
, GetMenuInfos 
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) 
 194 wxEND_PROPERTIES_TABLE() 
 196 wxBEGIN_HANDLERS_TABLE(wxMenuBar
) 
 197 wxEND_HANDLERS_TABLE() 
 199 wxCONSTRUCTOR_DUMMY( wxMenuBar 
) 
 202 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
) 
 203 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
) 
 204 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
) 
 207 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const 
 209     wxMenuInfoList
* list 
= const_cast< wxMenuInfoList
* >( &m_menuInfos 
) ; 
 210     WX_CLEAR_LIST( wxMenuInfoList 
, *list 
) ; 
 211     for( size_t i 
= 0 ; i 
< GetMenuCount() ; ++i 
) 
 213         wxMenuInfo
* info 
= new wxMenuInfo() ; 
 214         info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetLabelTop(i
) ) ; 
 215         list
->Append( info 
) ; 
 220 // --------------------------------------------------------------------------- 
 221 // wxMenu construction, adding and removing menu items 
 222 // --------------------------------------------------------------------------- 
 224 // Construct a menu with optional title (then use append) 
 228     m_startRadioGroup 
= -1; 
 231     m_hMenu 
= (WXHMENU
)CreatePopupMenu(); 
 234         wxLogLastError(wxT("CreatePopupMenu")); 
 237     // if we have a title, insert it in the beginning of the menu 
 238     if ( !m_title
.empty() ) 
 240         Append(idMenuTitle
, m_title
); 
 245 // The wxWindow destructor will take care of deleting the submenus. 
 248     // we should free Windows resources only if Windows doesn't do it for us 
 249     // which happens if we're attached to a menubar or a submenu of another 
 251     if ( !IsAttached() && !GetParent() ) 
 253         if ( !::DestroyMenu(GetHmenu()) ) 
 255             wxLogLastError(wxT("DestroyMenu")); 
 261     WX_CLEAR_ARRAY(m_accels
); 
 262 #endif // wxUSE_ACCEL 
 267     // this will take effect during the next call to Append() 
 271 void wxMenu::Attach(wxMenuBarBase 
*menubar
) 
 273     wxMenuBase::Attach(menubar
); 
 280 int wxMenu::FindAccel(int id
) const 
 282     size_t n
, count 
= m_accels
.GetCount(); 
 283     for ( n 
= 0; n 
< count
; n
++ ) 
 285         if ( m_accels
[n
]->m_command 
== id 
) 
 292 void wxMenu::UpdateAccel(wxMenuItem 
*item
) 
 294     if ( item
->IsSubMenu() ) 
 296         wxMenu 
*submenu 
= item
->GetSubMenu(); 
 297         wxMenuItemList::compatibility_iterator node 
= submenu
->GetMenuItems().GetFirst(); 
 300             UpdateAccel(node
->GetData()); 
 302             node 
= node
->GetNext(); 
 305     else if ( !item
->IsSeparator() ) 
 307         // recurse upwards: we should only modify m_accels of the top level 
 308         // menus, not of the submenus as wxMenuBar doesn't look at them 
 309         // (alternative and arguable cleaner solution would be to recurse 
 310         // downwards in GetAccelCount() and CopyAccels()) 
 313             GetParent()->UpdateAccel(item
); 
 317         // find the (new) accel for this item 
 318         wxAcceleratorEntry 
*accel 
= wxGetAccelFromString(item
->GetText()); 
 320             accel
->m_command 
= item
->GetId(); 
 323         int n 
= FindAccel(item
->GetId()); 
 324         if ( n 
== wxNOT_FOUND 
) 
 326             // no old, add new if any 
 330                 return;     // skipping RebuildAccelTable() below 
 334             // replace old with new or just remove the old one if no new 
 339                 m_accels
.RemoveAt(n
); 
 344             GetMenuBar()->RebuildAccelTable(); 
 347     //else: it is a separator, they can't have accels, nothing to do 
 350 #endif // wxUSE_ACCEL 
 352 // append a new item or submenu to the menu 
 353 bool wxMenu::DoInsertOrAppend(wxMenuItem 
*pItem
, size_t pos
) 
 357 #endif // wxUSE_ACCEL 
 361     // if "Break" has just been called, insert a menu break before this item 
 362     // (and don't forget to reset the flag) 
 364         flags 
|= MF_MENUBREAK
; 
 368     if ( pItem
->IsSeparator() ) { 
 369         flags 
|= MF_SEPARATOR
; 
 372     // id is the numeric id for normal menu items and HMENU for submenus as 
 373     // required by ::AppendMenu() API 
 375     wxMenu 
*submenu 
= pItem
->GetSubMenu(); 
 376     if ( submenu 
!= NULL 
) { 
 377         wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") ); 
 379         submenu
->SetParent(this); 
 381         id 
= (UINT
)submenu
->GetHMenu(); 
 390     // prepare to insert the item in the menu 
 391     wxString itemText 
= pItem
->GetText(); 
 392     LPCTSTR pData 
= NULL
; 
 393     if ( pos 
== (size_t)-1 ) 
 395         // append at the end (note that the item is already appended to 
 396         // internal data structures) 
 397         pos 
= GetMenuItemCount() - 1; 
 400     // adjust position to account for the title, if any 
 401     if ( !m_title
.empty() ) 
 402         pos 
+= 2; // for the title itself and its separator 
 406     // check if we have something more than a simple text item 
 407 #if wxUSE_OWNER_DRAWN 
 408     if ( pItem
->IsOwnerDrawn() ) 
 410         // is the item owner-drawn just because of the bitmap? 
 411         if ( pItem
->GetBitmap().Ok() && 
 412                 !pItem
->GetTextColour().Ok() && 
 413                     !pItem
->GetBackgroundColour().Ok() && 
 414                         !pItem
->GetFont().Ok() && 
 415                             !pItem
->GetBitmap(true).Ok() ) 
 417             // try to use InsertMenuItem() as it's guaranteed to look correctly 
 418             // while our owner-drawning code is not 
 420             // first compile-time check 
 422             WinStruct
<MENUITEMINFO
> mii
; 
 424             // now run-time one: MIIM_BITMAP only works under WinME/2000+ 
 425             if ( wxGetWinVersion() >= wxWinVersion_98 
) 
 427                 mii
.fMask 
= MIIM_STRING 
| MIIM_DATA 
| MIIM_BITMAP
; 
 428                 mii
.cch 
= itemText
.length(); 
 429                 mii
.dwTypeData 
= wx_const_cast(wxChar 
*, itemText
.c_str()); 
 431                 if (flags 
& MF_POPUP
) 
 433                     mii
.fMask 
|= MIIM_SUBMENU
; 
 434                     mii
.hSubMenu 
= (HMENU
)pItem
->GetSubMenu()->GetHMenu(); 
 438                     mii
.fMask 
|= MIIM_ID
; 
 442                 // we can't pass HBITMAP directly as hbmpItem for 2 reasons: 
 443                 //  1. we can't draw it with transparency then (this is not 
 444                 //     very important now but would be with themed menu bg) 
 445                 //  2. worse, Windows inverses the bitmap for the selected 
 446                 //     item and this looks downright ugly 
 448                 // so instead draw it ourselves in MSWOnDrawItem() 
 449                 mii
.dwItemData 
= wx_reinterpret_cast(ULONG_PTR
, pItem
); 
 450                 mii
.hbmpItem 
= HBMMENU_CALLBACK
; 
 452                 ok 
= ::InsertMenuItem(GetHmenu(), pos
, TRUE 
/* by pos */, &mii
); 
 455                     wxLogLastError(wxT("InsertMenuItem()")); 
 457                 else // InsertMenuItem() ok 
 459                     // we need to remove the extra indent which is reserved for 
 460                     // the checkboxes by default as it looks ugly unless check 
 461                     // boxes are used together with bitmaps and this is not the 
 463                     WinStruct
<MENUINFO
> mi
; 
 465                     // don't call SetMenuInfo() directly, this would prevent 
 466                     // the app from starting up under Windows 95/NT 4 
 467                     typedef BOOL (WINAPI 
*SetMenuInfo_t
)(HMENU
, MENUINFO 
*); 
 469                     wxDynamicLibrary 
dllUser(_T("user32")); 
 470                     wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
); 
 471                     if ( pfnSetMenuInfo 
) 
 473                         mi
.fMask 
= MIM_STYLE
; 
 474                         mi
.dwStyle 
= MNS_CHECKORBMP
; 
 475                         if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) ) 
 476                             wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)")); 
 479                     // tell the item that it's not really owner-drawn but only 
 480                     // needs to draw its bitmap, the rest is done by Windows 
 481                     pItem
->ResetOwnerDrawn(); 
 484 #endif // MIIM_BITMAP 
 489             // item draws itself, pass pointer to it in data parameter 
 490             flags 
|= MF_OWNERDRAW
; 
 491             pData 
= (LPCTSTR
)pItem
; 
 495 #endif // wxUSE_OWNER_DRAWN 
 497         // menu is just a normal string (passed in data parameter) 
 501         itemText 
= wxMenuItem::GetLabelFromText(itemText
); 
 504         pData 
= (wxChar
*)itemText
.c_str(); 
 507     // item might have been already inserted by InsertMenuItem() above 
 510         if ( !::InsertMenu(GetHmenu(), pos
, flags 
| MF_BYPOSITION
, id
, pData
) ) 
 512             wxLogLastError(wxT("InsertMenu[Item]()")); 
 519     // if we just appended the title, highlight it 
 520     if ( (int)id 
== idMenuTitle 
) 
 522         // visually select the menu title 
 523         SetDefaultMenuItem(GetHmenu(), id
); 
 526     // if we're already attached to the menubar, we must update it 
 527     if ( IsAttached() && GetMenuBar()->IsAttached() ) 
 529         GetMenuBar()->Refresh(); 
 535 void wxMenu::EndRadioGroup() 
 537     // we're not inside a radio group any longer 
 538     m_startRadioGroup 
= -1; 
 541 wxMenuItem
* wxMenu::DoAppend(wxMenuItem 
*item
) 
 543     wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") ); 
 547     if ( item
->GetKind() == wxITEM_RADIO 
) 
 549         int count 
= GetMenuItemCount(); 
 551         if ( m_startRadioGroup 
== -1 ) 
 553             // start a new radio group 
 554             m_startRadioGroup 
= count
; 
 556             // for now it has just one element 
 557             item
->SetAsRadioGroupStart(); 
 558             item
->SetRadioGroupEnd(m_startRadioGroup
); 
 560             // ensure that we have a checked item in the radio group 
 563         else // extend the current radio group 
 565             // we need to update its end item 
 566             item
->SetRadioGroupStart(m_startRadioGroup
); 
 567             wxMenuItemList::compatibility_iterator node 
= GetMenuItems().Item(m_startRadioGroup
); 
 571                 node
->GetData()->SetRadioGroupEnd(count
); 
 575                 wxFAIL_MSG( _T("where is the radio group start item?") ); 
 579     else // not a radio item 
 584     if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) ) 
 591         // check the item initially 
 598 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem 
*item
) 
 600     if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
)) 
 606 wxMenuItem 
*wxMenu::DoRemove(wxMenuItem 
*item
) 
 608     // we need to find the items position in the child list 
 610     wxMenuItemList::compatibility_iterator node 
= GetMenuItems().GetFirst(); 
 611     for ( pos 
= 0; node
; pos
++ ) 
 613         if ( node
->GetData() == item 
) 
 616         node 
= node
->GetNext(); 
 619     // DoRemove() (unlike Remove) can only be called for existing item! 
 620     wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") ); 
 623     // remove the corresponding accel from the accel table 
 624     int n 
= FindAccel(item
->GetId()); 
 625     if ( n 
!= wxNOT_FOUND 
) 
 629         m_accels
.RemoveAt(n
); 
 631     //else: this item doesn't have an accel, nothing to do 
 632 #endif // wxUSE_ACCEL 
 634     // remove the item from the menu 
 635     if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) ) 
 637         wxLogLastError(wxT("RemoveMenu")); 
 640     if ( IsAttached() && GetMenuBar()->IsAttached() ) 
 642         // otherwise, the chane won't be visible 
 643         GetMenuBar()->Refresh(); 
 646     // and from internal data structures 
 647     return wxMenuBase::DoRemove(item
); 
 650 // --------------------------------------------------------------------------- 
 651 // accelerator helpers 
 652 // --------------------------------------------------------------------------- 
 656 // create the wxAcceleratorEntries for our accels and put them into provided 
 657 // array - return the number of accels we have 
 658 size_t wxMenu::CopyAccels(wxAcceleratorEntry 
*accels
) const 
 660     size_t count 
= GetAccelCount(); 
 661     for ( size_t n 
= 0; n 
< count
; n
++ ) 
 663         *accels
++ = *m_accels
[n
]; 
 669 #endif // wxUSE_ACCEL 
 671 // --------------------------------------------------------------------------- 
 673 // --------------------------------------------------------------------------- 
 675 void wxMenu::SetTitle(const wxString
& label
) 
 677     bool hasNoTitle 
= m_title
.empty(); 
 680     HMENU hMenu 
= GetHmenu(); 
 684         if ( !label
.empty() ) 
 686             if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION 
| MF_STRING
, 
 687                                (unsigned)idMenuTitle
, m_title
) || 
 688                  !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) ) 
 690                 wxLogLastError(wxT("InsertMenu")); 
 698             // remove the title and the separator after it 
 699             if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) || 
 700                  !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ) 
 702                 wxLogLastError(wxT("RemoveMenu")); 
 711             info
.cbSize 
= sizeof(info
); 
 712             info
.fMask 
= MIIM_TYPE
; 
 713             info
.fType 
= MFT_STRING
; 
 714             info
.cch 
= m_title
.length(); 
 715             info
.dwTypeData 
= (LPTSTR
) m_title
.c_str(); 
 716             if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) ) 
 718                 wxLogLastError(wxT("SetMenuItemInfo")); 
 721             if ( !ModifyMenu(hMenu
, 0u, 
 722                              MF_BYPOSITION 
| MF_STRING
, 
 723                              (unsigned)idMenuTitle
, m_title
) ) 
 725                 wxLogLastError(wxT("ModifyMenu")); 
 732     // put the title string in bold face 
 733     if ( !m_title
.empty() ) 
 735         SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
); 
 740 // --------------------------------------------------------------------------- 
 742 // --------------------------------------------------------------------------- 
 744 bool wxMenu::MSWCommand(WXUINT 
WXUNUSED(param
), WXWORD id
) 
 746     // ignore commands from the menu title 
 747     if ( id 
!= (WXWORD
)idMenuTitle 
) 
 749         // get the checked status of the command: notice that menuState is the 
 750         // old state of the menu, so the test for MF_CHECKED must be inverted 
 751         UINT menuState 
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
); 
 752         SendEvent(id
, !(menuState 
& MF_CHECKED
)); 
 758 // --------------------------------------------------------------------------- 
 760 // --------------------------------------------------------------------------- 
 762 wxWindow 
*wxMenu::GetWindow() const 
 764     if ( m_invokingWindow 
!= NULL 
) 
 765         return m_invokingWindow
; 
 766     else if ( GetMenuBar() != NULL
) 
 767         return GetMenuBar()->GetFrame(); 
 772 // --------------------------------------------------------------------------- 
 774 // --------------------------------------------------------------------------- 
 776 void wxMenuBar::Init() 
 778     m_eventHandler 
= this; 
 780 #if wxUSE_TOOLBAR && defined(__WXWINCE__) 
 783     // Not using a combined wxToolBar/wxMenuBar? then use 
 784     // a commandbar in WinCE .NET just to implement the 
 786 #if defined(WINCE_WITH_COMMANDBAR) 
 788     m_adornmentsAdded 
= false; 
 792 wxMenuBar::wxMenuBar() 
 797 wxMenuBar::wxMenuBar( long WXUNUSED(style
) ) 
 802 wxMenuBar::wxMenuBar(size_t count
, wxMenu 
*menus
[], const wxString titles
[], long WXUNUSED(style
)) 
 806     m_titles
.Alloc(count
); 
 808     for ( size_t i 
= 0; i 
< count
; i
++ ) 
 810         m_menus
.Append(menus
[i
]); 
 811         m_titles
.Add(titles
[i
]); 
 813         menus
[i
]->Attach(this); 
 817 wxMenuBar::~wxMenuBar() 
 819     // In Windows CE (not .NET), the menubar is always associated 
 820     // with a toolbar, which destroys the menu implicitly. 
 821 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__) 
 824         wxToolMenuBar
* toolMenuBar 
= wxDynamicCast(GetToolBar(), wxToolMenuBar
); 
 826             toolMenuBar
->SetMenuBar(NULL
); 
 829     // we should free Windows resources only if Windows doesn't do it for us 
 830     // which happens if we're attached to a frame 
 831     if (m_hMenu 
&& !IsAttached()) 
 833 #if defined(WINCE_WITH_COMMANDBAR) 
 834         ::DestroyWindow((HWND
) m_commandBar
); 
 835         m_commandBar 
= (WXHWND
) NULL
; 
 837         ::DestroyMenu((HMENU
)m_hMenu
); 
 839         m_hMenu 
= (WXHMENU
)NULL
; 
 844 // --------------------------------------------------------------------------- 
 846 // --------------------------------------------------------------------------- 
 848 void wxMenuBar::Refresh() 
 850     wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") ); 
 852 #if defined(WINCE_WITHOUT_COMMANDBAR) 
 855         CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0); 
 857 #elif defined(WINCE_WITH_COMMANDBAR) 
 859         DrawMenuBar((HWND
) m_commandBar
); 
 861     DrawMenuBar(GetHwndOf(GetFrame())); 
 865 WXHMENU 
wxMenuBar::Create() 
 867     // Note: this totally doesn't work on Smartphone, 
 868     // since you have to use resources. 
 869     // We'll have to find another way to add a menu 
 870     // by changing/adding menu items to an existing menu. 
 871 #if defined(WINCE_WITHOUT_COMMANDBAR) 
 878     HWND hCommandBar 
= (HWND
) GetToolBar()->GetHWND(); 
 879     HMENU hMenu 
= (HMENU
)::SendMessage(hCommandBar
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0); 
 881     // hMenu may be zero on Windows Mobile 5. So add the menus anyway. 
 885         memset(&tbButton
, 0, sizeof(TBBUTTON
)); 
 886         tbButton
.iBitmap 
= I_IMAGENONE
; 
 887         tbButton
.fsState 
= TBSTATE_ENABLED
; 
 888         tbButton
.fsStyle 
= TBSTYLE_DROPDOWN 
| TBSTYLE_NO_DROPDOWN_ARROW 
| TBSTYLE_AUTOSIZE
; 
 891         for (i 
= 0; i 
< GetMenuCount(); i
++) 
 893             HMENU hPopupMenu 
= (HMENU
) GetMenu(i
)->GetHMenu() ; 
 894             tbButton
.dwData 
= (DWORD
)hPopupMenu
; 
 895             wxString label 
= wxStripMenuCodes(GetLabelTop(i
)); 
 896             tbButton
.iString 
= (int) label
.c_str(); 
 900             tbButton
.idCommand 
= NewControlId(); 
 901             if (!::SendMessage(hCommandBar
, TB_INSERTBUTTON
, position
, (LPARAM
)&tbButton
)) 
 903                 wxLogLastError(wxT("TB_INSERTBUTTON")); 
 907     m_hMenu 
= (WXHMENU
) hMenu
; 
 913     m_hMenu 
= (WXHMENU
)::CreateMenu(); 
 917         wxLogLastError(wxT("CreateMenu")); 
 921         size_t count 
= GetMenuCount(), i
; 
 922         wxMenuList::iterator it
; 
 923         for ( i 
= 0, it 
= m_menus
.begin(); i 
< count
; i
++, it
++ ) 
 925             if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP 
| MF_STRING
, 
 926                                (UINT
)(*it
)->GetHMenu(), 
 929                 wxLogLastError(wxT("AppendMenu")); 
 938 int wxMenuBar::MSWPositionForWxMenu(wxMenu 
*menu
, int wxpos
) 
 941     wxASSERT(menu
->GetHMenu()); 
 944 #if defined(__WXWINCE__) 
 945     int totalMSWItems 
= GetMenuCount(); 
 947     int totalMSWItems 
= GetMenuItemCount((HMENU
)m_hMenu
); 
 950     int i
; // For old C++ compatibility 
 951     for(i
=wxpos
; i
<totalMSWItems
; i
++) 
 953         if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu()) 
 956     for(i
=0; i
<wxpos
; i
++) 
 958         if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu()) 
 965 // --------------------------------------------------------------------------- 
 966 // wxMenuBar functions to work with the top level submenus 
 967 // --------------------------------------------------------------------------- 
 969 // NB: we don't support owner drawn top level items for now, if we do these 
 970 //     functions would have to be changed to use wxMenuItem as well 
 972 void wxMenuBar::EnableTop(size_t pos
, bool enable
) 
 974     wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") ); 
 975     wxCHECK_RET( pos 
< GetMenuCount(), wxT("invalid menu index") ); 
 977     int flag 
= enable 
? MF_ENABLED 
: MF_GRAYED
; 
 979     EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION 
| flag
); 
 984 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
) 
 986     wxCHECK_RET( pos 
< GetMenuCount(), wxT("invalid menu index") ); 
 988     m_titles
[pos
] = label
; 
 994     //else: have to modify the existing menu 
 996     int mswpos 
= MSWPositionForWxMenu(GetMenu(pos
),pos
); 
 999     UINT flagsOld 
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
); 
1000     if ( flagsOld 
== 0xFFFFFFFF ) 
1002         wxLogLastError(wxT("GetMenuState")); 
1007     if ( flagsOld 
& MF_POPUP 
) 
1009         // HIBYTE contains the number of items in the submenu in this case 
1011         id 
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
); 
1021     info
.cbSize 
= sizeof(info
); 
1022     info
.fMask 
= MIIM_TYPE
; 
1023     info
.fType 
= MFT_STRING
; 
1024     info
.cch 
= label
.length(); 
1025     info
.dwTypeData 
= (LPTSTR
) label
.c_str(); 
1026     if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) ) 
1028         wxLogLastError(wxT("SetMenuItemInfo")); 
1032     if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION 
| MF_STRING 
| flagsOld
, 
1033         id
, label
) == (int)0xFFFFFFFF ) 
1035         wxLogLastError(wxT("ModifyMenu")); 
1042 wxString 
wxMenuBar::GetLabelTop(size_t pos
) const 
1044     wxCHECK_MSG( pos 
< GetMenuCount(), wxEmptyString
, 
1045                  wxT("invalid menu index in wxMenuBar::GetLabelTop") ); 
1047     return wxMenuItem::GetLabelFromText(m_titles
[pos
]); 
1050 // --------------------------------------------------------------------------- 
1051 // wxMenuBar construction 
1052 // --------------------------------------------------------------------------- 
1054 wxMenu 
*wxMenuBar::Replace(size_t pos
, wxMenu 
*menu
, const wxString
& title
) 
1056     wxMenu 
*menuOld 
= wxMenuBarBase::Replace(pos
, menu
, title
); 
1060     m_titles
[pos
] = title
; 
1062 #if defined(WINCE_WITHOUT_COMMANDBAR) 
1068         int mswpos 
= MSWPositionForWxMenu(menuOld
,pos
); 
1070         // can't use ModifyMenu() because it deletes the submenu it replaces 
1071         if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) ) 
1073             wxLogLastError(wxT("RemoveMenu")); 
1076         if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
, 
1077                            MF_BYPOSITION 
| MF_POPUP 
| MF_STRING
, 
1078                            (UINT
)GetHmenuOf(menu
), title
) ) 
1080             wxLogLastError(wxT("InsertMenu")); 
1084         if ( menuOld
->HasAccels() || menu
->HasAccels() ) 
1086             // need to rebuild accell table 
1087             RebuildAccelTable(); 
1089 #endif // wxUSE_ACCEL 
1098 bool wxMenuBar::Insert(size_t pos
, wxMenu 
*menu
, const wxString
& title
) 
1100     // Find out which MSW item before which we'll be inserting before 
1101     // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu. 
1102     // If IsAttached() is false this won't be used anyway 
1104 #if defined(WINCE_WITHOUT_COMMANDBAR) 
1110     int mswpos 
= (!isAttached 
|| (pos 
== m_menus
.GetCount())) 
1111         ?   -1 // append the menu 
1112         :   MSWPositionForWxMenu(GetMenu(pos
),pos
); 
1114     if ( !wxMenuBarBase::Insert(pos
, menu
, title
) ) 
1117     m_titles
.Insert(title
, pos
); 
1121 #if defined(WINCE_WITHOUT_COMMANDBAR) 
1125         memset(&tbButton
, 0, sizeof(TBBUTTON
)); 
1126         tbButton
.iBitmap 
= I_IMAGENONE
; 
1127         tbButton
.fsState 
= TBSTATE_ENABLED
; 
1128         tbButton
.fsStyle 
= TBSTYLE_DROPDOWN 
| TBSTYLE_NO_DROPDOWN_ARROW 
| TBSTYLE_AUTOSIZE
; 
1130         HMENU hPopupMenu 
= (HMENU
) menu
->GetHMenu() ; 
1131         tbButton
.dwData 
= (DWORD
)hPopupMenu
; 
1132         wxString label 
= wxStripMenuCodes(title
); 
1133         tbButton
.iString 
= (int) label
.c_str(); 
1135         tbButton
.idCommand 
= NewControlId(); 
1136         if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
)) 
1138             wxLogLastError(wxT("TB_INSERTBUTTON")); 
1141         wxUnusedVar(mswpos
); 
1143         if ( !::InsertMenu(GetHmenu(), mswpos
, 
1144                            MF_BYPOSITION 
| MF_POPUP 
| MF_STRING
, 
1145                            (UINT
)GetHmenuOf(menu
), title
) ) 
1147             wxLogLastError(wxT("InsertMenu")); 
1151         if ( menu
->HasAccels() ) 
1153             // need to rebuild accell table 
1154             RebuildAccelTable(); 
1156 #endif // wxUSE_ACCEL 
1165 bool wxMenuBar::Append(wxMenu 
*menu
, const wxString
& title
) 
1167     WXHMENU submenu 
= menu 
? menu
->GetHMenu() : 0; 
1168     wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") ); 
1170     if ( !wxMenuBarBase::Append(menu
, title
) ) 
1173     m_titles
.Add(title
); 
1175 #if defined(WINCE_WITHOUT_COMMANDBAR) 
1181 #if defined(WINCE_WITHOUT_COMMANDBAR) 
1185         memset(&tbButton
, 0, sizeof(TBBUTTON
)); 
1186         tbButton
.iBitmap 
= I_IMAGENONE
; 
1187         tbButton
.fsState 
= TBSTATE_ENABLED
; 
1188         tbButton
.fsStyle 
= TBSTYLE_DROPDOWN 
| TBSTYLE_NO_DROPDOWN_ARROW 
| TBSTYLE_AUTOSIZE
; 
1190         size_t pos 
= GetMenuCount(); 
1191         HMENU hPopupMenu 
= (HMENU
) menu
->GetHMenu() ; 
1192         tbButton
.dwData 
= (DWORD
)hPopupMenu
; 
1193         wxString label 
= wxStripMenuCodes(title
); 
1194         tbButton
.iString 
= (int) label
.c_str(); 
1196         tbButton
.idCommand 
= NewControlId(); 
1197         if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
)) 
1199             wxLogLastError(wxT("TB_INSERTBUTTON")); 
1203         if ( !::AppendMenu(GetHmenu(), MF_POPUP 
| MF_STRING
, 
1204                            (UINT
)submenu
, title
) ) 
1206             wxLogLastError(wxT("AppendMenu")); 
1211         if ( menu
->HasAccels() ) 
1213             // need to rebuild accelerator table 
1214             RebuildAccelTable(); 
1216 #endif // wxUSE_ACCEL 
1225 wxMenu 
*wxMenuBar::Remove(size_t pos
) 
1227     wxMenu 
*menu 
= wxMenuBarBase::Remove(pos
); 
1231 #if defined(WINCE_WITHOUT_COMMANDBAR) 
1237 #if defined(WINCE_WITHOUT_COMMANDBAR) 
1240             if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0)) 
1242                 wxLogLastError(wxT("TB_DELETEBUTTON")); 
1246         if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) ) 
1248             wxLogLastError(wxT("RemoveMenu")); 
1253         if ( menu
->HasAccels() ) 
1255             // need to rebuild accell table 
1256             RebuildAccelTable(); 
1258 #endif // wxUSE_ACCEL 
1264     m_titles
.RemoveAt(pos
); 
1271 void wxMenuBar::RebuildAccelTable() 
1273     // merge the accelerators of all menus into one accel table 
1274     size_t nAccelCount 
= 0; 
1275     size_t i
, count 
= GetMenuCount(); 
1276     wxMenuList::iterator it
; 
1277     for ( i 
= 0, it 
= m_menus
.begin(); i 
< count
; i
++, it
++ ) 
1279         nAccelCount 
+= (*it
)->GetAccelCount(); 
1284         wxAcceleratorEntry 
*accelEntries 
= new wxAcceleratorEntry
[nAccelCount
]; 
1287         for ( i 
= 0, it 
= m_menus
.begin(); i 
< count
; i
++, it
++ ) 
1289             nAccelCount 
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]); 
1292         m_accelTable 
= wxAcceleratorTable(nAccelCount
, accelEntries
); 
1294         delete [] accelEntries
; 
1298 #endif // wxUSE_ACCEL 
1300 void wxMenuBar::Attach(wxFrame 
*frame
) 
1302     wxMenuBarBase::Attach(frame
); 
1304 #if defined(WINCE_WITH_COMMANDBAR) 
1308         m_commandBar 
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId()); 
1313             if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0)) 
1315                 wxLogLastError(wxT("CommandBar_InsertMenubarEx")); 
1322     RebuildAccelTable(); 
1323 #endif // wxUSE_ACCEL 
1326 #if defined(WINCE_WITH_COMMANDBAR) 
1327 bool wxMenuBar::AddAdornments(long style
) 
1329     if (m_adornmentsAdded 
|| !m_commandBar
) 
1332     if (style 
& wxCLOSE_BOX
) 
1334         if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0)) 
1335             wxLogLastError(wxT("CommandBar_AddAdornments")); 
1343 void wxMenuBar::Detach() 
1345     wxMenuBarBase::Detach(); 
1348 #endif // wxUSE_MENUS