X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2cb5d2d2b70566c40be5578fe3db29cb3c3a34ad..ec2df34e27ba41f202ecbf096cdfed082a9ddb8f:/src/osx/carbon/menu.cpp diff --git a/src/osx/carbon/menu.cpp b/src/osx/carbon/menu.cpp index 5db7b7bc50..76f34d84e0 100644 --- a/src/osx/carbon/menu.cpp +++ b/src/osx/carbon/menu.cpp @@ -37,10 +37,10 @@ // under carbon there's no such thing as a MenuItemRef, everything is done // on the 'parent' menu via index APIs (first line having index 1 !) -// so to make things still work, we store the wxMenuItemImpl instance as a +// so to make things still work, we store the wxMenuItemImpl instance as a // RefCon at the respective menu line -class wxMenuItemCarbonImpl : public wxMenuItemImpl +class wxMenuItemCarbonImpl : public wxMenuItemImpl { public : wxMenuItemCarbonImpl( wxMenuItem* peer ) : wxMenuItemImpl(peer) @@ -48,15 +48,15 @@ public : // the parent menu ref is only set, once the item has been attached m_parentMenuRef = NULL; } - + ~wxMenuItemCarbonImpl(); - + void SetBitmap( const wxBitmap& bitmap ) { MenuItemIndex i = FindMenuItemIndex() ; if ( i > 0 ) { - if ( bitmap.Ok() ) + if ( bitmap.IsOk() ) { #if wxUSE_BMPBUTTON ControlButtonContentInfo info ; @@ -74,21 +74,21 @@ public : #endif } } - } - - void Enable( bool enable ) + } + + void Enable( bool enable ) { MenuItemIndex i = FindMenuItemIndex() ; if ( i > 0 ) { - + if ( GetWXPeer()->GetId() == wxApp::s_macPreferencesMenuItemId) { if ( enable ) EnableMenuCommand( NULL , kHICommandPreferences ) ; else DisableMenuCommand( NULL , kHICommandPreferences ) ; - } + } else if ( GetWXPeer()->GetId() == wxApp::s_macExitMenuItemId) { if ( enable ) @@ -96,20 +96,20 @@ public : else DisableMenuCommand( NULL , kHICommandQuit ) ; } - + if ( enable ) EnableMenuItem(m_parentMenuRef , i); else DisableMenuItem(m_parentMenuRef , i); - + if ( GetWXPeer()->IsSubMenu() ) { UMAEnableMenuItem( GetWXPeer()->GetSubMenu()->GetHMenu() , 0 , enable ) ; } } - } - - void Check( bool check ) + } + + void Check( bool check ) { MenuItemIndex i = FindMenuItemIndex() ; if ( i > 0 ) @@ -119,9 +119,9 @@ public : else ::SetItemMark( m_parentMenuRef, i, 0 ) ; // no mark } - } + } - void Hide( bool hide ) + void Hide( bool hide ) { MenuItemIndex i = FindMenuItemIndex() ; if ( i > 0 ) @@ -131,9 +131,9 @@ public : else ChangeMenuItemAttributes( m_parentMenuRef, i, 0 , kMenuItemAttrHidden ); } - } - - void SetLabel( const wxString& text, wxAcceleratorEntry *entry ) + } + + void SetLabel( const wxString& text, wxAcceleratorEntry *entry ) { MenuItemIndex i = FindMenuItemIndex() ; if ( i > 0 ) @@ -142,16 +142,16 @@ public : UMASetMenuItemShortcut( m_parentMenuRef, i , entry ) ; } } - + void * GetHMenuItem() { return NULL; } - + // Carbon Only - - void AttachToParent( MenuRef parentMenuRef, MenuItemIndex index ) + + void AttachToParent( MenuRef parentMenuRef, MenuItemIndex index ) { m_parentMenuRef = parentMenuRef; if ( m_parentMenuRef && index > 0 ) - SetMenuItemRefCon( m_parentMenuRef, index, (URefCon) this ); + SetMenuItemRefCon( m_parentMenuRef, index, (URefCon) m_peer ); } MenuItemIndex FindMenuItemIndex() @@ -163,7 +163,7 @@ public : { URefCon storedRef = 0; GetMenuItemRefCon(m_parentMenuRef, i, &storedRef ); - if ( storedRef == (URefCon) this ) + if ( storedRef == (URefCon) m_peer ) { hit = i; break; @@ -180,16 +180,17 @@ protected : // wxMenuImpl // -class wxMenuCarbonImpl : public wxMenuImpl +class wxMenuCarbonImpl : public wxMenuImpl { public : - wxMenuCarbonImpl( wxMenu* peer , MenuRef menu) : wxMenuImpl(peer), m_osxMenu(menu) + wxMenuCarbonImpl( wxMenu* peer , MenuRef menu , MenuRef oldMenu , SInt16 menuId) + : wxMenuImpl(peer), m_osxMenu(menu), m_oldMenuRef(oldMenu), m_menuId(menuId) { } virtual ~wxMenuCarbonImpl(); - - virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos) + + virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos) { // MacOS counts menu items from 1 and inserts after, therefore having the // same effect as wx 0 based and inserting before, we must correct pos @@ -198,14 +199,14 @@ public : MenuItemIndex index = pos; if ( pos == (size_t) -1 ) index = CountMenuItems(m_osxMenu); - + if ( pItem->IsSeparator() ) { InsertMenuItemTextWithCFString( m_osxMenu, CFSTR(""), index, kMenuItemAttrSeparator, 0); // now switch to the Carbon 1 based counting index += 1 ; } - else + else { InsertMenuItemTextWithCFString( m_osxMenu, CFSTR("placeholder"), index, 0, 0 ); @@ -223,7 +224,7 @@ public : SetMenuItemCommandID( m_osxMenu, index , wxIdToMacCommand(pItem->GetId()) ) ; } } - + wxMenuItemCarbonImpl* impl = (wxMenuItemCarbonImpl*) pItem->GetPeer(); impl->AttachToParent( m_osxMenu, index ); // only now can all settings be updated correctly @@ -231,8 +232,8 @@ public : pItem->UpdateItemStatus(); pItem->UpdateItemBitmap(); } - - virtual void Remove( wxMenuItem *pItem ) + + virtual void Remove( wxMenuItem *pItem ) { wxMenuItemCarbonImpl* impl = (wxMenuItemCarbonImpl*) pItem->GetPeer(); if ( impl ) @@ -245,7 +246,7 @@ public : } } } - + virtual void MakeRoot() { SetRootMenu( m_osxMenu ); @@ -280,6 +281,8 @@ public : static wxMenuImpl* CreateRootMenu( wxMenu* peer ); protected : wxCFRef m_osxMenu; + MenuRef m_oldMenuRef; + SInt16 m_menuId; } ; // static const short kwxMacAppleMenuId = 1 ; @@ -325,21 +328,35 @@ void wxRemoveMacMenuAssociation(wxMenu *menu) wxMenuCarbonImpl::~wxMenuCarbonImpl() { wxRemoveMacMenuAssociation( GetWXPeer() ); + // restore previous menu + m_osxMenu.reset(); + if ( m_menuId > 0 ) + MacDeleteMenu(m_menuId); + if ( m_oldMenuRef ) + MacInsertMenu(m_oldMenuRef, -1); } wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title ) { // create the menu static SInt16 s_macNextMenuId = 3; + SInt16 menuId = s_macNextMenuId++; + // save existing menu in case we're embedding into an application + // or sharing outside UI elements. + WXHMENU oldMenu = GetMenuHandle(menuId); + if ( oldMenu ) + MacDeleteMenu(menuId); WXHMENU menu = NULL; - CreateNewMenu( s_macNextMenuId++ , 0 , &menu ) ; + CreateNewMenu( menuId , 0 , &menu ) ; if ( !menu ) { wxLogLastError(wxT("CreateNewMenu failed")); + if ( oldMenu ) + MacInsertMenu(oldMenu, -1); return NULL; } - wxMenuImpl* c = new wxMenuCarbonImpl( peer, menu ); + wxMenuImpl* c = new wxMenuCarbonImpl( peer, menu, oldMenu, menuId ); c->SetTitle(title); wxAssociateMenuWithMacMenu( menu , peer ) ; return c; @@ -354,7 +371,7 @@ wxMenuItemCarbonImpl::~wxMenuItemCarbonImpl() } -wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, +wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu * WXUNUSED(pParentMenu), int WXUNUSED(id), const wxString& WXUNUSED(text),