From 84f68aa764a8151a7529186b18b21fe3e584d55d Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Mon, 10 Sep 2018 18:59:17 +0200 Subject: [PATCH] Fixes and Cleanup --- include/wx/bitmap.h | 3 + include/wx/osx/bitmap.h | 8 +- include/wx/osx/icon.h | 12 ++- src/osx/carbon/icon.cpp | 178 ++++++++++++++++++++++++-------------- src/osx/core/bitmap.cpp | 76 +++++++--------- src/osx/core/dcmemory.cpp | 2 +- 6 files changed, 160 insertions(+), 119 deletions(-) diff --git a/include/wx/bitmap.h b/include/wx/bitmap.h index 23103dcb85..adb95b9c50 100644 --- a/include/wx/bitmap.h +++ b/include/wx/bitmap.h @@ -224,9 +224,12 @@ public: virtual bool CopyFromIcon(const wxIcon& icon) = 0; // implementation: +#if WXWIN_COMPATIBILITY_3_0 + // deprecated virtual void SetHeight(int height) = 0; virtual void SetWidth(int width) = 0; virtual void SetDepth(int depth) = 0; +#endif // Format handling static inline wxList& GetHandlers() { return sm_handlers; } diff --git a/include/wx/osx/bitmap.h b/include/wx/osx/bitmap.h index b3fa967ef9..621e94bcc6 100644 --- a/include/wx/osx/bitmap.h +++ b/include/wx/osx/bitmap.h @@ -175,13 +175,11 @@ public: #if WXWIN_COMPATIBILITY_3_0 wxDEPRECATED_MSG("this value is determined during creation, this method could lead to inconsistencies") - void SetWidth(int w); + void SetWidth(int width); wxDEPRECATED_MSG("this value is determined during creation, this method could lead to inconsistencies") - void SetHeight(int h); + void SetHeight(int height); wxDEPRECATED_MSG("this value is determined during creation, this method could lead to inconsistencies") - void SetDepth(int d); - wxDEPRECATED_MSG("this value is determined during creation, this method could lead to inconsistencies") - void SetOk(bool isOk); + void SetDepth(int depth); #endif #if wxUSE_PALETTE diff --git a/include/wx/osx/icon.h b/include/wx/osx/icon.h index a06afeeccc..bc493392db 100644 --- a/include/wx/osx/icon.h +++ b/include/wx/osx/icon.h @@ -44,10 +44,14 @@ public: int GetWidth() const; int GetHeight() const; int GetDepth() const; - void SetWidth(int w); - void SetHeight(int h); - void SetDepth(int d); - void SetOk(bool isOk); +#if WXWIN_COMPATIBILITY_3_0 + wxDEPRECATED_MSG("this value is determined during creation, this method could lead to inconsistencies") + void SetWidth(int width); + wxDEPRECATED_MSG("this value is determined during creation, this method could lead to inconsistencies") + void SetHeight(int height); + wxDEPRECATED_MSG("this value is determined during creation, this method could lead to inconsistencies") + void SetDepth(int depth); +#endif wxSize GetSize() const { return wxSize(GetWidth(), GetHeight()); } diff --git a/src/osx/carbon/icon.cpp b/src/osx/carbon/icon.cpp index 2ae7bde730..c1e902259a 100644 --- a/src/osx/carbon/icon.cpp +++ b/src/osx/carbon/icon.cpp @@ -24,22 +24,22 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject); #define M_ICONDATA ((wxIconRefData *)m_refData) +#define wxOSX_ICON_USE_NSIMAGE wxOSX_BITMAP_NATIVE_ACCESS + +#if wxOSX_ICON_USE_NSIMAGE + +// implementation based on NSImage + class WXDLLEXPORT wxIconRefData : public wxGDIRefData { public: wxIconRefData() { Init(); } -#if wxOSX_USE_ICONREF - wxIconRefData( WXHICON iconref, int desiredWidth, int desiredHeight ); -#else wxIconRefData( WX_NSImage image, int desiredWidth, int desiredHeight ); -#endif + wxIconRefData( WXHICON iconref, int desiredWidth, int desiredHeight ); + virtual ~wxIconRefData() { Free(); } -#if wxOSX_USE_ICONREF - virtual bool IsOk() const wxOVERRIDE { return m_iconRef != NULL; } -#else virtual bool IsOk() const wxOVERRIDE { return m_nsImage != NULL; } -#endif virtual void Free(); void SetWidth( int width ) { m_width = width; } @@ -48,22 +48,13 @@ public: int GetWidth() const { return m_width; } int GetHeight() const { return m_height; } -#if wxOSX_USE_ICONREF - WXHICON GetHICON() const { return (WXHICON) m_iconRef; } -#endif -#if wxOSX_USE_COCOA WX_NSImage GetNSImage() const; -#endif private: void Init(); + void Create( NSImage* icon, int desiredWidth, int desiredHeight ); + NSImage* m_nsImage; -#if wxOSX_USE_ICONREF - IconRef m_iconRef; -#endif -#if wxOSX_USE_COCOA - mutable NSImage* m_nsImage; -#endif int m_width; int m_height; @@ -71,21 +62,24 @@ private: wxDECLARE_NO_COPY_CLASS(wxIconRefData); }; -#if wxOSX_USE_ICONREF -wxIconRefData::wxIconRefData( WXHICON icon, int desiredWidth, int desiredHeight ) -{ - Init(); - m_iconRef = (IconRef)( icon ) ; - // Standard sizes - SetWidth( desiredWidth == -1 ? 32 : desiredWidth ) ; - SetHeight( desiredHeight == -1 ? 32 : desiredHeight ) ; -} -#else wxIconRefData::wxIconRefData( NSImage* icon, int desiredWidth, int desiredHeight ) { Init(); + Create(icon, desiredWidth, desiredHeight); +} + +wxIconRefData::wxIconRefData( WXHICON iconref, int desiredWidth, int desiredHeight ) +{ + Init(); + + Create(wxOSXGetNSImageFromIconRef(iconref), desiredWidth, desiredHeight); + ReleaseIconRef(iconref); +} + +void wxIconRefData::Create( NSImage* icon, int desiredWidth, int desiredHeight ) +{ if ( icon ) { m_nsImage = icon; @@ -96,53 +90,113 @@ wxIconRefData::wxIconRefData( NSImage* icon, int desiredWidth, int desiredHeight SetWidth( desiredWidth == -1 ? 32 : desiredWidth ) ; SetHeight( desiredHeight == -1 ? 32 : desiredHeight ) ; } -#endif void wxIconRefData::Init() { -#if wxOSX_USE_ICONREF - m_iconRef = NULL ; -#endif -#if wxOSX_USE_COCOA m_nsImage = NULL; -#endif m_width = m_height = 0; } void wxIconRefData::Free() { -#if wxOSX_USE_ICONREF + if ( m_nsImage ) + { + wxMacCocoaRelease(m_nsImage); + } +} + +WX_NSImage wxIconRefData::GetNSImage() const +{ + wxASSERT( IsOk() ); + + return m_nsImage; +} + +#else // !wxOSX_ICON_USE_NSIMAGE + +// implementation based on IconRef + +class WXDLLEXPORT wxIconRefData : public wxGDIRefData +{ +public: + wxIconRefData() { Init(); } + wxIconRefData( WXHICON iconref, int desiredWidth, int desiredHeight ); + virtual ~wxIconRefData() { Free(); } + + virtual bool IsOk() const wxOVERRIDE { return m_iconRef != NULL; } + virtual void Free(); + + void SetWidth( int width ) { m_width = width; } + void SetHeight( int height ) { m_height = height; } + + int GetWidth() const { return m_width; } + int GetHeight() const { return m_height; } + + WXHICON GetHICON() const { return (WXHICON) m_iconRef; } + + WX_NSImage GetNSImage() const; + +private: + void Init(); + + IconRef m_iconRef; + + mutable NSImage* m_nsImage; + + int m_width; + int m_height; + + // We can (easily) copy m_iconRef so we don't implement the copy ctor. + wxDECLARE_NO_COPY_CLASS(wxIconRefData); +}; + +wxIconRefData::wxIconRefData( WXHICON icon, int desiredWidth, int desiredHeight ) +{ + Init(); + m_iconRef = (IconRef)( icon ) ; + + // Standard sizes + SetWidth( desiredWidth == -1 ? 32 : desiredWidth ) ; + SetHeight( desiredHeight == -1 ? 32 : desiredHeight ) ; +} + +void wxIconRefData::Init() +{ + m_iconRef = NULL ; + m_nsImage = NULL; + + m_width = + m_height = 0; +} + +void wxIconRefData::Free() +{ if ( m_iconRef ) { ReleaseIconRef( m_iconRef ) ; m_iconRef = NULL ; } -#endif - -#if wxOSX_USE_COCOA + if ( m_nsImage ) { wxMacCocoaRelease(m_nsImage); } -#endif } -#if wxOSX_USE_COCOA WX_NSImage wxIconRefData::GetNSImage() const { wxASSERT( IsOk() ); - -#if wxOSX_USE_ICONREF + if ( m_nsImage == 0 ) { m_nsImage = wxOSXGetNSImageFromIconRef(m_iconRef); CFRetain(m_nsImage); } -#endif - + return m_nsImage; } + #endif // @@ -182,6 +236,13 @@ wxIcon::wxIcon(WXHICON icon, const wxSize& size) m_refData = new wxIconRefData( icon, size.x, size.y ) ; } + +WXHICON wxIcon::GetHICON() const +{ + wxASSERT( IsOk() ) ; + + return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ; +} #endif wxIcon::~wxIcon() @@ -201,15 +262,6 @@ wxIcon::CloneGDIRefData(const wxGDIRefData * WXUNUSED(data)) const return new wxIconRefData; } -#if wxOSX_USE_ICONREF -WXHICON wxIcon::GetHICON() const -{ - wxASSERT( IsOk() ) ; - - return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ; -} -#endif - int wxIcon::GetWidth() const { wxCHECK_MSG( IsOk(), -1, wxT("invalid icon") ); @@ -229,15 +281,14 @@ int wxIcon::GetDepth() const return 32; } -#if wxOSX_USE_COCOA WX_NSImage wxIcon::GetNSImage() const { wxCHECK_MSG( IsOk(), NULL, wxT("invalid icon") ); return M_ICONDATA->GetNSImage() ; } -#endif +#if WXWIN_COMPATIBILITY_3_0 void wxIcon::SetDepth( int WXUNUSED(depth) ) { } @@ -249,6 +300,7 @@ void wxIcon::SetWidth( int WXUNUSED(width) ) void wxIcon::SetHeight( int WXUNUSED(height) ) { } +#endif // Load an icon based on resource name or filel name // Return true on success, false otherwise @@ -279,7 +331,6 @@ bool wxIcon::LoadIconFromSystemResource(const wxString& resourceName, int desire { UnRef(); -#if wxOSX_USE_ICONREF OSType theId = 0 ; if ( resourceName == wxT("wxICON_INFORMATION") ) @@ -368,13 +419,10 @@ bool wxIcon::LoadIconFromSystemResource(const wxString& resourceName, int desire IconRef iconRef = NULL ; __Verify_noErr(GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef )) ; if ( iconRef ) - { m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ; - return true ; - } } -#endif - return false; + + return IsOk(); } // Load an icon of type 'icns' by resource by name @@ -528,7 +576,9 @@ void wxIcon::CopyFromBitmap( const wxBitmap& bmp ) { UnRef() ; -#if wxOSX_USE_ICONREF +#if wxOSX_ICON_USE_NSIMAGE + m_refData = new wxIconRefData( bmp.GetNSImage() , bmp.GetWidth(), bmp.GetHeight() ) ; +#else // as the bitmap owns that ref, we have to acquire it as well int w = bmp.GetWidth() ; @@ -544,8 +594,6 @@ void wxIcon::CopyFromBitmap( const wxBitmap& bmp ) { m_refData = new wxIconRefData( (WXHICON) bmp.CreateIconRef() , bmp.GetWidth(), bmp.GetHeight() ) ; } -#else - m_refData = new wxIconRefData( bmp.GetNSImage() , bmp.GetWidth(), bmp.GetHeight() ) ; #endif } diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index 900173547e..709613cf69 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -60,16 +60,22 @@ public: virtual bool IsOk() const wxOVERRIDE { return m_ok; } void Free(); - void SetOk( bool isOk) { m_ok = isOk; } #if wxOSX_BITMAP_NATIVE_ACCESS - int GetWidth() const { return CGBitmapContextGetWidth(m_hBitmap); } - int GetHeight() const { return CGBitmapContextGetHeight(m_hBitmap); } - int GetDepth() const { return CGBitmapContextGetBitsPerPixel(m_hBitmap); } - int GetBytesPerRow() const { return CGBitmapContextGetBytesPerRow(m_hBitmap); } - bool HasAlpha() const { + int GetWidth() const + { return CGBitmapContextGetWidth(m_hBitmap); } + int GetHeight() const + { return CGBitmapContextGetHeight(m_hBitmap); } + int GetDepth() const + { return CGBitmapContextGetBitsPerPixel(m_hBitmap); } + int GetBytesPerRow() const + { return CGBitmapContextGetBytesPerRow(m_hBitmap); } + bool HasAlpha() const + { CGImageAlphaInfo alpha = CGBitmapContextGetAlphaInfo(m_hBitmap); - return !( alpha == kCGImageAlphaNone || alpha == kCGImageAlphaNoneSkipFirst || alpha == kCGImageAlphaNoneSkipLast) ; + return !(alpha == kCGImageAlphaNone || + alpha == kCGImageAlphaNoneSkipFirst || + alpha == kCGImageAlphaNoneSkipLast); } #else void SetWidth( int width ) { m_width = width; } @@ -109,12 +115,10 @@ public: // rescaled to 16 x 16 bool HasNativeSize(); -#ifndef __WXOSX_IPHONE__ #if wxOSX_USE_ICONREF // caller should increase ref count if needed longer // than the bitmap exists IconRef GetIconRef(); -#endif #endif CGContextRef GetBitmapContext() const; @@ -127,6 +131,7 @@ private : bool Create( CGImageRef image, double scale ); bool Create( CGContextRef bitmapcontext); void Init(); + void FreeDerivedRepresentations(); #if !wxOSX_BITMAP_NATIVE_ACCESS int m_width; @@ -141,10 +146,8 @@ private : mutable CGImageRef m_cgImageRef; bool m_isTemplate; -#ifndef __WXOSX_IPHONE__ #if wxOSX_USE_ICONREF IconRef m_iconRef; -#endif #endif wxCFRef m_hBitmap; @@ -190,10 +193,8 @@ void wxBitmapRefData::Init() m_cgImageRef = NULL ; m_isTemplate = false; -#ifndef __WXOSX_IPHONE__ #if wxOSX_USE_ICONREF m_iconRef = NULL ; -#endif #endif m_hBitmap = NULL ; @@ -391,7 +392,7 @@ void wxBitmapRefData::UseAlpha( bool use ) #endif wxASSERT_MSG( hBitmap , wxT("Unable to create CGBitmapContext context") ) ; CGContextTranslateCTM( hBitmap, 0, GetHeight() ); - CGContextScaleCTM( hBitmap, 1*GetScaleFactor(), -1*GetScaleFactor() ); + CGContextScaleCTM( hBitmap, GetScaleFactor(), -GetScaleFactor() ); m_hBitmap.reset(hBitmap); } @@ -408,13 +409,7 @@ const void *wxBitmapRefData::GetRawAccess() const void *wxBitmapRefData::GetRawAccess() { - wxCHECK_MSG( IsOk(), NULL , wxT("invalid bitmap") ) ; - wxASSERT_MSG( m_rawAccessCount == 1, "Direct write access must be within Begin/EndRawAccess" ) ; -#if !wxOSX_BITMAP_NATIVE_ACCESS - return m_memBuf.GetData() ; -#else - return CGBitmapContextGetData(m_hBitmap); -#endif + return const_cast(const_cast(this)->GetRawAccess()); } @@ -422,21 +417,17 @@ void *wxBitmapRefData::BeginRawAccess() { wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ) ; wxASSERT( m_rawAccessCount == 0 ) ; -#ifndef __WXOSX_IPHONE__ + #if wxOSX_USE_ICONREF wxASSERT_MSG( m_iconRef == NULL , wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ; #endif -#endif + ++m_rawAccessCount ; // we must destroy an existing cached image, as // the bitmap data may change now - if ( m_cgImageRef ) - { - CGImageRelease( m_cgImageRef ) ; - m_cgImageRef = NULL ; - } + FreeDerivedRepresentations(); return GetRawAccess() ; } @@ -458,7 +449,6 @@ bool wxBitmapRefData::HasNativeSize() return ( sz == 128 || sz == 48 || sz == 32 || sz == 16 ); } -#ifndef __WXOSX_IPHONE__ #if wxOSX_USE_ICONREF IconRef wxBitmapRefData::GetIconRef() { @@ -671,8 +661,7 @@ IconRef wxBitmapRefData::GetIconRef() return m_iconRef ; } -#endif -#endif +#endif // wxOSX_USE_ICONREF CGImageRef wxBitmapRefData::CreateCGImage() const { @@ -821,25 +810,28 @@ wxDC *wxBitmapRefData::GetSelectedInto() const return m_selectedInto; } - -void wxBitmapRefData::Free() +void wxBitmapRefData::FreeDerivedRepresentations() { - wxASSERT_MSG( m_rawAccessCount == 0 , wxT("Bitmap still selected when destroyed") ) ; - if ( m_cgImageRef ) { CGImageRelease( m_cgImageRef ) ; m_cgImageRef = NULL ; } -#ifndef __WXOSX_IPHONE__ #if wxOSX_USE_ICONREF if ( m_iconRef ) { ReleaseIconRef( m_iconRef ) ; m_iconRef = NULL ; } -#endif -#endif +#endif // wxOSX_USE_ICONREF +} + +void wxBitmapRefData::Free() +{ + wxASSERT_MSG( m_rawAccessCount == 0 , wxT("Bitmap still selected when destroyed") ) ; + + FreeDerivedRepresentations(); + m_hBitmap.reset(); wxDELETE(m_bitmapMask); } @@ -1479,6 +1471,7 @@ bool wxBitmap::HasAlpha() const return GetBitmapData()->HasAlpha() ; } +#if WXWIN_COMPATIBILITY_3_0 void wxBitmap::SetWidth(int w) { AllocExclusive(); @@ -1496,12 +1489,7 @@ void wxBitmap::SetDepth(int d) AllocExclusive(); wxASSERT_MSG( d == -1 || GetDepth() == d, "Changing the bitmap depth is not supported"); } - -void wxBitmap::SetOk(bool isOk) -{ - AllocExclusive(); - GetBitmapData()->SetOk(isOk); -} +#endif #if wxUSE_PALETTE wxPalette *wxBitmap::GetPalette() const diff --git a/src/osx/core/dcmemory.cpp b/src/osx/core/dcmemory.cpp index 4aba6ca8a0..1f68889bd9 100644 --- a/src/osx/core/dcmemory.cpp +++ b/src/osx/core/dcmemory.cpp @@ -74,7 +74,7 @@ void wxMemoryDCImpl::DoSelect( const wxBitmap& bitmap ) { wxASSERT_MSG( !bitmap.GetSelectedInto() || (bitmap.GetSelectedInto() == GetOwner()), - wxT("Bitmap is selected in another wxMemoryDC, delete the first wxMemoryDC or use SelectObject(NULL)") ); + "Bitmap is selected in another wxMemoryDC, delete the first wxMemoryDC or use SelectObject(NULL)" ); m_selected.SetSelectedInto(GetOwner()); m_width = bitmap.GetScaledWidth();