From 87e9b3963e8292cff637a9f1bb8e7e2d5a5ee3f6 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Mon, 3 Sep 2018 09:34:03 +0200 Subject: [PATCH 01/18] Fixing creation from CGContextRef Our native buffer must be updated as well, otherwise RawAccess will fail --- src/osx/core/bitmap.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index 52d161b17b..2bad5c9462 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -340,6 +340,12 @@ bool wxBitmapRefData::Create(CGContextRef context) // our own contexts conform to this, always. wxASSERT( m_depth == 32 ); + size_t size = m_bytesPerRow * m_height ; + void* data = m_memBuf.GetWriteBuf( size ) ; + + memcpy( data , CGBitmapContextGetData(context) , size ) ; + m_memBuf.UngetWriteBuf( size ) ; + // determine content scale CGRect userrect = CGRectMake(0, 0, 10, 10); CGRect devicerect; From a5aa044a7bffd4e2e73e96c64bd7305f335f583e Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Fri, 7 Sep 2018 20:33:02 +0200 Subject: [PATCH 02/18] Update deprecation in docs --- interface/wx/bitmap.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/interface/wx/bitmap.h b/interface/wx/bitmap.h index 500002ae4a..9f0405e5b5 100644 --- a/interface/wx/bitmap.h +++ b/interface/wx/bitmap.h @@ -707,6 +707,9 @@ public: const wxPalette* palette = NULL) const; /** + @deprecated This function is deprecated since version 3.1.2, dimensions + and depth can only be set at construction time. + Sets the depth member (does not affect the bitmap data). @todo since these functions do not affect the bitmap data, @@ -714,10 +717,14 @@ public: @param depth Bitmap depth. + */ virtual void SetDepth(int depth); /** + @deprecated This function is deprecated since version 3.1.2, dimensions + and depth can only be set at construction time. + Sets the height member (does not affect the bitmap data). @param height @@ -745,6 +752,9 @@ public: virtual void SetPalette(const wxPalette& palette); /** + @deprecated This function is deprecated since version 3.1.2, dimensions + and depth can only be set at construction time. + Sets the width member (does not affect the bitmap data). @param width From e7d21f6638a6d5f715bea5dcef461f9d19e4d979 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Fri, 7 Sep 2018 21:44:19 +0200 Subject: [PATCH 03/18] Initial Commit of full native bitmap macOS implementation Right now, to better test regressions, with dual implementation --- include/wx/defs.h | 2 +- include/wx/osx/bitmap.h | 58 ++- include/wx/osx/cocoa/private.h | 3 + include/wx/osx/icon.h | 2 + include/wx/osx/setup0.h | 7 + src/osx/carbon/cursor.cpp | 2 +- src/osx/carbon/icon.cpp | 58 ++- src/osx/carbon/utilscocoa.mm | 27 +- src/osx/core/bitmap.cpp | 665 +++++++++++++++++++-------------- src/osx/core/dcmemory.cpp | 10 +- 10 files changed, 532 insertions(+), 302 deletions(-) diff --git a/include/wx/defs.h b/include/wx/defs.h index 6181458f7f..c2336f6223 100644 --- a/include/wx/defs.h +++ b/include/wx/defs.h @@ -2584,7 +2584,6 @@ typedef int (* LINKAGEMODE wxListIterateFunction)(void *current); #define WX_OPAQUE_TYPE( name ) struct wxOpaque##name -typedef void* WXHBITMAP; typedef void* WXHCURSOR; typedef void* WXRECTPTR; typedef void* WXPOINTPTR; @@ -2628,6 +2627,7 @@ DECLARE_WXOSX_OPAQUE_CGREF( CGFont ) typedef CGColorRef WXCOLORREF; typedef CGImageRef WXCGIMAGEREF; typedef CGContextRef WXHDC; +typedef CGContextRef WXHBITMAP; /* * carbon diff --git a/include/wx/osx/bitmap.h b/include/wx/osx/bitmap.h index f5cec1dac4..b3fa967ef9 100644 --- a/include/wx/osx/bitmap.h +++ b/include/wx/osx/bitmap.h @@ -30,7 +30,7 @@ class WXDLLIMPEXP_FWD_CORE wxPixelDataBase; // 8 bit is chosen only for performance reasons, note also that this is the inverse value range // from alpha, where 0 = invisible , 255 = fully drawn -class WXDLLIMPEXP_CORE wxMask: public wxObject +class WXDLLIMPEXP_CORE wxMask: public wxMaskBase { wxDECLARE_DYNAMIC_CLASS(wxMask); @@ -52,8 +52,6 @@ public: virtual ~wxMask(); - bool Create(const wxBitmap& bitmap, const wxColour& colour); - bool Create(const wxBitmap& bitmap); bool Create(const wxMemoryBuffer& buf, int width , int height , int bytesPerRow ) ; wxBitmap GetBitmap() const; @@ -64,20 +62,36 @@ public: // a 8 bit depth mask void* GetRawAccess() const; - int GetBytesPerRow() const { return m_bytesPerRow ; } + int GetBytesPerRow() const; + int GetWidth() const; + int GetHeight() const; + // renders/updates native representation when necessary void RealizeNative() ; WXHBITMAP GetHBITMAP() const ; +protected: + // this function is called from Create() to free the existing mask data + virtual void FreeData() wxOVERRIDE; + + // these functions must be overridden to implement the corresponding public + // Create() methods, they shouldn't call FreeData() as it's already called + // by the public wrappers + virtual bool InitFromColour(const wxBitmap& bitmap, + const wxColour& colour) wxOVERRIDE; + virtual bool InitFromMonoBitmap(const wxBitmap& bitmap) wxOVERRIDE; private: +#if !wxOSX_BITMAP_NATIVE_ACCESS wxMemoryBuffer m_memBuf ; int m_bytesPerRow ; int m_width ; int m_height ; +#endif + void DoCreateMaskBitmap(int width, int height, int bytesPerRow = -1); - WXHBITMAP m_maskBitmap ; + wxCFRef m_maskBitmap ; }; @@ -146,7 +160,10 @@ public: virtual bool LoadFile(const wxString& name, wxBitmapType type = wxBITMAP_DEFAULT_TYPE); virtual bool SaveFile(const wxString& name, wxBitmapType type, const wxPalette *cmap = NULL) const; - wxBitmapRefData *GetBitmapData() const + const wxBitmapRefData *GetBitmapData() const + { return (const wxBitmapRefData *)m_refData; } + + wxBitmapRefData *GetBitmapData() { return (wxBitmapRefData *)m_refData; } // copies the contents and mask of the given (colour) icon to the bitmap @@ -155,10 +172,17 @@ public: int GetWidth() const; int GetHeight() const; int GetDepth() const; + +#if WXWIN_COMPATIBILITY_3_0 + wxDEPRECATED_MSG("this value is determined during creation, this method could lead to inconsistencies") void SetWidth(int w); + wxDEPRECATED_MSG("this value is determined during creation, this method could lead to inconsistencies") void SetHeight(int h); + 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); +#endif #if wxUSE_PALETTE wxPalette* GetPalette() const; @@ -193,18 +217,34 @@ public: // returns an autoreleased version of the image WX_UIImage GetUIImage() const; #endif + +#if WXWIN_COMPATIBILITY_3_0 + +#if wxOSX_USE_ICONREF // returns a IconRef which must be retained before and released after usage + wxDEPRECATED_MSG("IconRefs are deprecated, this will be removed in the future") IconRef GetIconRef() const; // returns a IconRef which must be released after usage + wxDEPRECATED_MSG("IconRefs are deprecated, this will be removed in the future") IconRef CreateIconRef() const; +#endif + // get read only access to the underlying buffer - void *GetRawAccess() const ; + wxDEPRECATED_MSG("use GetRawData for accessing the buffer") + const void *GetRawAccess() const; // brackets to the underlying OS structure for read/write access // makes sure that no cached images will be constructed until terminated - void *BeginRawAccess() ; - void EndRawAccess() ; + wxDEPRECATED_MSG("use GetRawData for accessing the buffer") + void *BeginRawAccess(); + wxDEPRECATED_MSG("use GetRawData for accessing the buffer") + void EndRawAccess(); +#endif double GetScaleFactor() const; + + void SetSelectedInto(wxDC *dc); + wxDC *GetSelectedInto() const; + protected: virtual wxGDIRefData *CreateGDIRefData() const; virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const; diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index ed70b80794..bb0f15d06c 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -32,8 +32,11 @@ OSStatus WXDLLIMPEXP_CORE wxMacDrawCGImage( CGContextRef inContext, const CGRect * inBounds, CGImageRef inImage) ; +WX_NSImage WXDLLIMPEXP_CORE wxOSXGetSystemImage(const wxString& name); WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromCGImage( CGImageRef image, double scale = 1.0, bool isTemplate = false); WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromIconRef( WXHICON iconref ); +WX_NSImage WXDLLIMPEXP_CORE wxOSXGetIconForType(OSType type ); +void WXDLLIMPEXP_CORE wxOSXSetImageSize(WX_NSImage image, CGFloat width, CGFloat height); CGImageRef WXDLLIMPEXP_CORE wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage, double *scale = NULL ); CGImageRef WXDLLIMPEXP_CORE wxOSXGetCGImageFromNSImage( WX_NSImage nsimage, CGRect* r, CGContextRef cg); CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage nsimage, bool *isTemplate = NULL); diff --git a/include/wx/osx/icon.h b/include/wx/osx/icon.h index 2540a346d8..a06afeeccc 100644 --- a/include/wx/osx/icon.h +++ b/include/wx/osx/icon.h @@ -51,7 +51,9 @@ public: wxSize GetSize() const { return wxSize(GetWidth(), GetHeight()); } +#if wxOSX_USE_ICONREF WXHICON GetHICON() const; +#endif #if wxOSX_USE_COCOA WX_NSImage GetNSImage() const ; diff --git a/include/wx/osx/setup0.h b/include/wx/osx/setup0.h index a1307bf4d4..49bf354be5 100644 --- a/include/wx/osx/setup0.h +++ b/include/wx/osx/setup0.h @@ -1622,6 +1622,13 @@ // make sure we have the proper dispatcher for the console event loop #define wxUSE_SELECT_DISPATCHER 1 #define wxUSE_EPOLL_DISPATCHER 0 + +// set to 1 if you have older code that still needs icon refs +#define wxOSX_USE_ICONREF 0 + +// set to 0 if you have code that has problems with the new bitmap implementation +#define wxOSX_BITMAP_NATIVE_ACCESS 1 + /* --- end OSX options --- */ #endif diff --git a/src/osx/carbon/cursor.cpp b/src/osx/carbon/cursor.cpp index 9f6e7f69bf..7b83407bcb 100644 --- a/src/osx/carbon/cursor.cpp +++ b/src/osx/carbon/cursor.cpp @@ -256,7 +256,7 @@ void wxCursor::CreateFromImage(const wxImage & image) int hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y); #if wxOSX_USE_COCOA wxBitmap bmp( image ); - CGImageRef cgimage = wxMacCreateCGImageFromBitmap(bmp); + CGImageRef cgimage = bmp.CreateCGImage(); if ( cgimage ) { M_CURSORDATA->m_hCursor = wxMacCocoaCreateCursorFromCGImage( cgimage, hotSpotX, hotSpotY ); diff --git a/src/osx/carbon/icon.cpp b/src/osx/carbon/icon.cpp index 1d74771428..2ae7bde730 100644 --- a/src/osx/carbon/icon.cpp +++ b/src/osx/carbon/icon.cpp @@ -28,11 +28,18 @@ 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 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; } @@ -41,7 +48,9 @@ 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 @@ -49,7 +58,9 @@ public: private: void Init(); +#if wxOSX_USE_ICONREF IconRef m_iconRef; +#endif #if wxOSX_USE_COCOA mutable NSImage* m_nsImage; #endif @@ -60,7 +71,7 @@ private: wxDECLARE_NO_COPY_CLASS(wxIconRefData); }; - +#if wxOSX_USE_ICONREF wxIconRefData::wxIconRefData( WXHICON icon, int desiredWidth, int desiredHeight ) { Init(); @@ -70,10 +81,28 @@ wxIconRefData::wxIconRefData( WXHICON icon, int desiredWidth, int desiredHeight SetWidth( desiredWidth == -1 ? 32 : desiredWidth ) ; SetHeight( desiredHeight == -1 ? 32 : desiredHeight ) ; } +#else +wxIconRefData::wxIconRefData( NSImage* icon, int desiredWidth, int desiredHeight ) +{ + Init(); + + if ( icon ) + { + m_nsImage = icon; + wxMacCocoaRetain(icon); + } + + // Standard sizes + 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 @@ -83,16 +112,18 @@ void wxIconRefData::Init() void wxIconRefData::Free() { +#if wxOSX_USE_ICONREF if ( m_iconRef ) { ReleaseIconRef( m_iconRef ) ; m_iconRef = NULL ; } +#endif #if wxOSX_USE_COCOA if ( m_nsImage ) { - CFRelease(m_nsImage); + wxMacCocoaRelease(m_nsImage); } #endif } @@ -102,11 +133,13 @@ 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; } @@ -139,6 +172,7 @@ wxIcon::wxIcon( LoadFile( icon_file, flags, desiredWidth, desiredHeight ); } +#if wxOSX_USE_ICONREF wxIcon::wxIcon(WXHICON icon, const wxSize& size) : wxGDIObject() { @@ -148,6 +182,7 @@ wxIcon::wxIcon(WXHICON icon, const wxSize& size) m_refData = new wxIconRefData( icon, size.x, size.y ) ; } +#endif wxIcon::~wxIcon() { @@ -166,12 +201,14 @@ 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 { @@ -242,6 +279,7 @@ bool wxIcon::LoadIconFromSystemResource(const wxString& resourceName, int desire { UnRef(); +#if wxOSX_USE_ICONREF OSType theId = 0 ; if ( resourceName == wxT("wxICON_INFORMATION") ) @@ -335,7 +373,7 @@ bool wxIcon::LoadIconFromSystemResource(const wxString& resourceName, int desire return true ; } } - +#endif return false; } @@ -347,6 +385,7 @@ bool wxIcon::LoadIconFromBundleResource(const wxString& resourceName, int desire { UnRef(); +#if wxOSX_USE_ICONREF IconRef iconRef = NULL ; // first look in the resource fork @@ -395,7 +434,7 @@ bool wxIcon::LoadIconFromBundleResource(const wxString& resourceName, int desire m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ); return true; } - +#endif return false; } @@ -406,9 +445,10 @@ bool wxIcon::LoadIconFromFile(const wxString& filename, int desiredWidth, int de { UnRef(); - OSStatus err; bool result = false; +#if wxOSX_USE_ICONREF + OSStatus err; // Get a file system reference FSRef fsRef; err = FSPathMakeRef( (const wxUint8*)filename.utf8_str().data(), &fsRef, NULL ); @@ -436,6 +476,7 @@ bool wxIcon::LoadIconFromFile(const wxString& filename, int desiredWidth, int de // Release the iconFamily before returning ReleaseResource( (Handle) iconFamily ); +#endif return result; } @@ -487,6 +528,7 @@ void wxIcon::CopyFromBitmap( const wxBitmap& bmp ) { UnRef() ; +#if wxOSX_USE_ICONREF // as the bitmap owns that ref, we have to acquire it as well int w = bmp.GetWidth() ; @@ -502,7 +544,9 @@ 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 } wxIMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler); diff --git a/src/osx/carbon/utilscocoa.mm b/src/osx/carbon/utilscocoa.mm index 9b70c90360..99d05565bf 100644 --- a/src/osx/carbon/utilscocoa.mm +++ b/src/osx/carbon/utilscocoa.mm @@ -203,10 +203,23 @@ double wxOSXGetMainScreenContentScaleFactor() #if wxOSX_USE_COCOA -wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &WXUNUSED(client), const wxSize& WXUNUSED(size)) +WX_NSImage wxOSXGetSystemImage(const wxString& name) { wxCFStringRef cfname(name); - return wxBitmap( [NSImage imageNamed:cfname.AsNSString()] ); + NSImage* nsimage = [NSImage imageNamed:cfname.AsNSString()]; + return nsimage; +} + +wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &client, const wxSize& sizeHint) +{ + NSImage* nsimage = wxOSXGetSystemImage(name); + if ( nsimage ) + { + // if ( sizeHint != wxDefaultSize ) + // [nsimage setSize:NSMakeSize(sizeHint.GetHeight(), sizeHint.GetWidth())]; + return wxBitmap( nsimage ); + } + return wxNullBitmap; } WX_NSImage wxOSXGetNSImageFromCGImage( CGImageRef image, double scaleFactor, bool isTemplate ) @@ -274,6 +287,16 @@ double wxOSXGetMainScreenContentScaleFactor() return [[NSScreen mainScreen] backingScaleFactor]; } +WX_NSImage wxOSXGetIconForType(OSType type ) +{ + return [[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode(type)]; +} + +void wxOSXSetImageSize(WX_NSImage image, CGFloat width, CGFloat height) +{ + [image setSize:NSMakeSize(width, height)]; +} + CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage, double *scaleptr ) { // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index 2bad5c9462..900173547e 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -62,6 +62,16 @@ public: 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 { + CGImageAlphaInfo alpha = CGBitmapContextGetAlphaInfo(m_hBitmap); + return !( alpha == kCGImageAlphaNone || alpha == kCGImageAlphaNoneSkipFirst || alpha == kCGImageAlphaNoneSkipLast) ; + } +#else void SetWidth( int width ) { m_width = width; } void SetHeight( int height ) { m_height = height; } void SetDepth( int depth ) { m_depth = depth; } @@ -69,12 +79,16 @@ public: int GetWidth() const { return m_width; } int GetHeight() const { return m_height; } int GetDepth() const { return m_depth; } + int GetBytesPerRow() const { return m_bytesPerRow; } + bool HasAlpha() const { return m_hasAlpha; } +#endif double GetScaleFactor() const { return m_scaleFactor; } - void *GetRawAccess() const; + + const void *GetRawAccess() const; + void *GetRawAccess(); void *BeginRawAccess(); void EndRawAccess(); - bool HasAlpha() const { return m_hasAlpha; } void UseAlpha( bool useAlpha ); bool IsTemplate() const { return m_isTemplate; } @@ -96,37 +110,46 @@ public: 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; - int GetBytesPerRow() const { return m_bytesPerRow; } - private : + void SetSelectedInto(wxDC *dc); + wxDC *GetSelectedInto() const; + +private : bool Create(int width , int height , int depth, double logicalscale); bool Create( CGImageRef image, double scale ); bool Create( CGContextRef bitmapcontext); void Init(); +#if !wxOSX_BITMAP_NATIVE_ACCESS int m_width; int m_height; int m_bytesPerRow; int m_depth; bool m_hasAlpha; wxMemoryBuffer m_memBuf; +#endif int m_rawAccessCount; bool m_ok; mutable CGImageRef m_cgImageRef; bool m_isTemplate; #ifndef __WXOSX_IPHONE__ +#if wxOSX_USE_ICONREF IconRef m_iconRef; +#endif #endif - CGContextRef m_hBitmap; + wxCFRef m_hBitmap; double m_scaleFactor; + wxDC* m_selectedInto; }; @@ -143,122 +166,55 @@ static int GetBestBytesPerRow( int rawBytes ) // this is used for more controls than just the wxBitmap button, also for notebooks etc -void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType ) -{ - memset( info , 0 , sizeof(ControlButtonContentInfo) ) ; - if ( bitmap.IsOk() ) - { - wxBitmapRefData * bmap = bitmap.GetBitmapData() ; - if ( bmap == NULL ) - return ; - - if ( forceType == 0 ) - { - forceType = kControlContentCGImageRef; - } - - if ( forceType == kControlContentIconRef ) - { - wxBitmap scaleBmp ; - wxBitmapRefData* bmp = bmap ; - - if ( !bmap->HasNativeSize() ) - { - // as PICT conversion will only result in a 16x16 icon, let's attempt - // a few scales for better results - - int w = bitmap.GetWidth() ; - int h = bitmap.GetHeight() ; - int sz = wxMax( w , h ) ; - if ( sz == 24 || sz == 64 ) - { - scaleBmp = wxBitmap( bitmap.ConvertToImage().Scale( w * 2 , h * 2 ) ) ; - bmp = scaleBmp.GetBitmapData() ; - } - } - - info->contentType = kControlContentIconRef ; - info->u.iconRef = bmp->GetIconRef() ; - AcquireIconRef( info->u.iconRef ) ; - } - else if ( forceType == kControlContentCGImageRef ) - { - info->contentType = kControlContentCGImageRef ; - info->u.imageRef = (CGImageRef) bmap->CreateCGImage() ; - } - } -} - CGImageRef wxMacCreateCGImageFromBitmap( const wxBitmap& bitmap ) { - wxBitmapRefData * bmap = bitmap.GetBitmapData() ; + const wxBitmapRefData * bmap = bitmap.GetBitmapData() ; if ( bmap == NULL ) return NULL ; return (CGImageRef) bmap->CreateCGImage(); } -void wxMacReleaseBitmapButton( ControlButtonContentInfo*info ) -{ - if ( info->contentType == kControlContentIconRef ) - { - ReleaseIconRef( info->u.iconRef ) ; - } - else if ( info->contentType == kControlNoContent ) - { - // there's no bitmap at all, fall through silently - } - else if ( info->contentType == kControlContentPictHandle ) - { - // owned by the bitmap, no release here - } - else if ( info->contentType == kControlContentCGImageRef ) - { - CGImageRelease( info->u.imageRef ) ; - } - else - { - wxFAIL_MSG(wxT("Unexpected bitmap type") ) ; - } -} - #endif //wxUSE_BMPBUTTON -#define M_BITMAPDATA ((wxBitmapRefData *)m_refData) - void wxBitmapRefData::Init() { +#if !wxOSX_BITMAP_NATIVE_ACCESS m_width = 0 ; m_height = 0 ; m_depth = 0 ; m_bytesPerRow = 0; + m_hasAlpha = false; +#endif m_ok = false ; m_bitmapMask = NULL ; m_cgImageRef = NULL ; m_isTemplate = false; #ifndef __WXOSX_IPHONE__ +#if wxOSX_USE_ICONREF m_iconRef = NULL ; +#endif #endif m_hBitmap = NULL ; m_rawAccessCount = 0 ; - m_hasAlpha = false; m_scaleFactor = 1.0; + m_selectedInto = NULL; } wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData &tocopy) : wxGDIRefData() { Init(); - Create(tocopy.m_width, tocopy.m_height, tocopy.m_depth, tocopy.m_scaleFactor); + Create(tocopy.GetWidth(), tocopy.GetHeight(), tocopy.GetDepth(), tocopy.GetScaleFactor()); if (tocopy.m_bitmapMask) m_bitmapMask = new wxMask(*tocopy.m_bitmapMask); - else if (tocopy.m_hasAlpha) + else if (tocopy.HasAlpha()) UseAlpha(true); unsigned char* dest = (unsigned char*)GetRawAccess(); unsigned char* source = (unsigned char*)tocopy.GetRawAccess(); - size_t numbytes = m_bytesPerRow * m_height; + size_t numbytes = GetBytesPerRow() * GetHeight(); memcpy( dest, source, numbytes ); } @@ -283,48 +239,59 @@ wxBitmapRefData::wxBitmapRefData(CGImageRef image, double scale) { Init(); Create( image, scale ); -} +} // code from Technical Q&A QA1509 bool wxBitmapRefData::Create(CGImageRef image, double scale) { if ( image != NULL ) { +#if wxOSX_BITMAP_NATIVE_ACCESS + int m_width, m_height, m_depth, m_bytesPerRow; +#endif m_width = CGImageGetWidth(image); m_height = CGImageGetHeight(image); m_depth = 32; m_hBitmap = NULL; m_scaleFactor = scale; - + m_bytesPerRow = GetBestBytesPerRow( m_width * 4 ) ; +#if !wxOSX_BITMAP_NATIVE_ACCESS size_t size = m_bytesPerRow * m_height ; void* data = m_memBuf.GetWriteBuf( size ) ; if ( data != NULL ) { memset( data , 0 , size ) ; m_memBuf.UngetWriteBuf( size ) ; +#else + void* data = NULL; +#endif CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image); if ( alpha == kCGImageAlphaNone || alpha == kCGImageAlphaNoneSkipFirst || alpha == kCGImageAlphaNoneSkipLast ) { m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst ); } - else + else { +#if !wxOSX_BITMAP_NATIVE_ACCESS m_hasAlpha = true; +#endif m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst ); } CGRect rect = CGRectMake(0,0,m_width,m_height); CGContextDrawImage(m_hBitmap, rect, image); - + wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ; CGContextTranslateCTM( m_hBitmap, 0, m_height ); CGContextScaleCTM( m_hBitmap, 1*m_scaleFactor, -1*m_scaleFactor ); +#if !wxOSX_BITMAP_NATIVE_ACCESS } /* data != NULL */ +#endif } m_ok = ( m_hBitmap != NULL ) ; - + return m_ok ; - + } bool wxBitmapRefData::Create(CGContextRef context) @@ -332,35 +299,40 @@ bool wxBitmapRefData::Create(CGContextRef context) if ( context != NULL && CGBitmapContextGetData(context) ) { m_hBitmap = context; +#if !wxOSX_BITMAP_NATIVE_ACCESS m_bytesPerRow = CGBitmapContextGetBytesPerRow(context); m_width = CGBitmapContextGetWidth(context); m_height = CGBitmapContextGetHeight(context); m_depth = CGBitmapContextGetBitsPerPixel(context) ; - +#endif // our own contexts conform to this, always. - wxASSERT( m_depth == 32 ); - - size_t size = m_bytesPerRow * m_height ; + wxASSERT( GetDepth() == 32 ); + +#if !wxOSX_BITMAP_NATIVE_ACCESS + size_t size = GetBytesPerRow() * GetHeight() ; void* data = m_memBuf.GetWriteBuf( size ) ; memcpy( data , CGBitmapContextGetData(context) , size ) ; m_memBuf.UngetWriteBuf( size ) ; +#endif // determine content scale CGRect userrect = CGRectMake(0, 0, 10, 10); CGRect devicerect; devicerect = CGContextConvertRectToDeviceSpace(context, userrect); m_scaleFactor = devicerect.size.height / userrect.size.height; - + CGImageAlphaInfo alpha = CGBitmapContextGetAlphaInfo(context); - + if ( alpha == kCGImageAlphaNone || alpha == kCGImageAlphaNoneSkipFirst || alpha == kCGImageAlphaNoneSkipLast ) { // no alpha } else { +#if !wxOSX_BITMAP_NATIVE_ACCESS m_hasAlpha = true; +#endif } } m_ok = ( m_hBitmap != NULL ) ; @@ -370,6 +342,9 @@ bool wxBitmapRefData::Create(CGContextRef context) bool wxBitmapRefData::Create( int w , int h , int d, double logicalscale ) { +#if wxOSX_BITMAP_NATIVE_ACCESS + int m_width, m_height, m_depth, m_bytesPerRow; +#endif m_width = wxMax(1, w); m_height = wxMax(1, h); m_depth = d ; @@ -377,18 +352,23 @@ bool wxBitmapRefData::Create( int w , int h , int d, double logicalscale ) m_hBitmap = NULL ; m_bytesPerRow = GetBestBytesPerRow( m_width * 4 ) ; +#if !wxOSX_BITMAP_NATIVE_ACCESS size_t size = m_bytesPerRow * m_height ; void* data = m_memBuf.GetWriteBuf( size ) ; if ( data != NULL ) { memset( data , 0 , size ) ; m_memBuf.UngetWriteBuf( size ) ; - +#else + void* data = NULL; +#endif m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst ); wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ; CGContextTranslateCTM( m_hBitmap, 0, m_height ); - CGContextScaleCTM( m_hBitmap, 1*m_scaleFactor, -1*m_scaleFactor ); + CGContextScaleCTM( m_hBitmap, 1*GetScaleFactor(), -1*GetScaleFactor() ); +#if !wxOSX_BITMAP_NATIVE_ACCESS } /* data != NULL */ +#endif m_ok = ( m_hBitmap != NULL ) ; return m_ok ; @@ -396,31 +376,57 @@ bool wxBitmapRefData::Create( int w , int h , int d, double logicalscale ) void wxBitmapRefData::UseAlpha( bool use ) { - if ( m_hasAlpha == use ) + if ( HasAlpha() == use ) return ; +#if !wxOSX_BITMAP_NATIVE_ACCESS m_hasAlpha = use ; + void *data = m_memBuf.GetData(); +#else + void *data = NULL; +#endif + CGContextRef hBitmap = CGBitmapContextCreate(data, GetWidth(), GetHeight(), 8, GetBytesPerRow(), wxMacGetGenericRGBColorSpace(), use ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst ); +#if wxOSX_BITMAP_NATIVE_ACCESS + memcpy(CGBitmapContextGetData(hBitmap),CGBitmapContextGetData(m_hBitmap),GetBytesPerRow()*GetHeight()); +#endif + wxASSERT_MSG( hBitmap , wxT("Unable to create CGBitmapContext context") ) ; + CGContextTranslateCTM( hBitmap, 0, GetHeight() ); + CGContextScaleCTM( hBitmap, 1*GetScaleFactor(), -1*GetScaleFactor() ); - CGContextRelease( m_hBitmap ); - m_hBitmap = CGBitmapContextCreate((char*) m_memBuf.GetData(), m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), m_hasAlpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst ); - wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ; - CGContextTranslateCTM( m_hBitmap, 0, m_height ); - CGContextScaleCTM( m_hBitmap, 1*m_scaleFactor, -1*m_scaleFactor ); + m_hBitmap.reset(hBitmap); } -void *wxBitmapRefData::GetRawAccess() const +const void *wxBitmapRefData::GetRawAccess() const { wxCHECK_MSG( IsOk(), NULL , wxT("invalid bitmap") ) ; +#if !wxOSX_BITMAP_NATIVE_ACCESS return m_memBuf.GetData() ; +#else + return CGBitmapContextGetData(m_hBitmap); +#endif } +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 +} + + 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 ; @@ -432,7 +438,7 @@ void *wxBitmapRefData::BeginRawAccess() m_cgImageRef = NULL ; } - return m_memBuf.GetData() ; + return GetRawAccess() ; } void wxBitmapRefData::EndRawAccess() @@ -453,6 +459,7 @@ bool wxBitmapRefData::HasNativeSize() } #ifndef __WXOSX_IPHONE__ +#if wxOSX_USE_ICONREF IconRef wxBitmapRefData::GetIconRef() { if ( m_iconRef == NULL ) @@ -534,7 +541,7 @@ IconRef wxBitmapRefData::GetIconRef() unsigned char * sourcePtr = (unsigned char*) GetRawAccess() ; unsigned char * masksourcePtr = mask ? (unsigned char*) mask->GetRawAccess() : NULL ; - for ( int y = 0 ; y < h ; ++y, sourcePtr += m_bytesPerRow , masksourcePtr += mask ? mask->GetBytesPerRow() : 0 ) + for ( int y = 0 ; y < h ; ++y, sourcePtr += GetBytesPerRow() , masksourcePtr += mask ? mask->GetBytesPerRow() : 0 ) { unsigned char * source = sourcePtr; unsigned char * masksource = masksourcePtr; @@ -610,7 +617,7 @@ IconRef wxBitmapRefData::GetIconRef() unsigned char * sourcePtr = (unsigned char*) GetRawAccess() ; unsigned char * masksourcePtr = mask ? (unsigned char*) mask->GetRawAccess() : NULL ; - for ( int y = 0 ; y < h ; ++y, sourcePtr += m_bytesPerRow , masksourcePtr += mask ? mask->GetBytesPerRow() : 0 ) + for ( int y = 0 ; y < h ; ++y, sourcePtr += GetBytesPerRow() , masksourcePtr += mask ? mask->GetBytesPerRow() : 0 ) { unsigned char * source = sourcePtr; unsigned char * masksource = masksourcePtr; @@ -664,7 +671,7 @@ IconRef wxBitmapRefData::GetIconRef() return m_iconRef ; } - +#endif #endif CGImageRef wxBitmapRefData::CreateCGImage() const @@ -674,7 +681,7 @@ CGImageRef wxBitmapRefData::CreateCGImage() const CGImageRef image ; if ( m_rawAccessCount > 0 || m_cgImageRef == NULL ) { - if ( m_depth != 1 && m_bitmapMask == NULL ) + if ( GetDepth() != 1 && m_bitmapMask == NULL ) { #if 0 // in order for this code to work properly, wxMask would have to invert black and white @@ -693,10 +700,10 @@ CGImageRef wxBitmapRefData::CreateCGImage() const } else { - size_t imageSize = m_height * m_bytesPerRow ; - void * dataBuffer = m_memBuf.GetData() ; - int w = m_width ; - int h = m_height ; + size_t imageSize = GetHeight() * GetBytesPerRow() ; + + int w = GetWidth() ; + int h = GetHeight() ; CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ; wxMemoryBuffer membuf; @@ -704,10 +711,10 @@ CGImageRef wxBitmapRefData::CreateCGImage() const { alphaInfo = kCGImageAlphaFirst ; unsigned char *destalphastart = (unsigned char*) membuf.GetWriteBuf( imageSize ) ; - memcpy( destalphastart , dataBuffer , imageSize ) ; + memcpy( destalphastart , GetRawAccess() , imageSize ) ; unsigned char *sourcemaskstart = (unsigned char *) m_bitmapMask->GetRawAccess() ; int maskrowbytes = m_bitmapMask->GetBytesPerRow() ; - for ( int y = 0 ; y < h ; ++y , destalphastart += m_bytesPerRow, sourcemaskstart += maskrowbytes) + for ( int y = 0 ; y < h ; ++y , destalphastart += GetBytesPerRow(), sourcemaskstart += maskrowbytes) { unsigned char *sourcemask = sourcemaskstart ; unsigned char *destalpha = destalphastart ; @@ -720,7 +727,7 @@ CGImageRef wxBitmapRefData::CreateCGImage() const } else { - if ( m_hasAlpha ) + if ( HasAlpha() ) { #if wxOSX_USE_PREMULTIPLIED_ALPHA alphaInfo = kCGImageAlphaPremultipliedFirst ; @@ -728,33 +735,35 @@ CGImageRef wxBitmapRefData::CreateCGImage() const alphaInfo = kCGImageAlphaFirst ; #endif } - - membuf = m_memBuf; +#if wxOSX_BITMAP_NATIVE_ACCESS + memcpy(membuf.GetWriteBuf( imageSize ),GetRawAccess(),imageSize); + membuf.UngetWriteBuf(imageSize); +#endif } CGDataProviderRef dataProvider = NULL ; - if ( m_depth == 1 ) + if ( GetDepth() == 1 ) { // TODO CHECK ALIGNMENT wxMemoryBuffer maskBuf; - unsigned char * maskBufData = (unsigned char*) maskBuf.GetWriteBuf( m_width * m_height ); + unsigned char * maskBufData = (unsigned char*) maskBuf.GetWriteBuf( GetWidth() * GetHeight() ); unsigned char * bufData = (unsigned char *) membuf.GetData() ; // copy one color component size_t i = 0; - for( int y = 0 ; y < m_height ; bufData+= m_bytesPerRow, ++y ) + for( int y = 0 ; y < GetHeight() ; bufData+= GetBytesPerRow(), ++y ) { unsigned char *bufDataIter = bufData+3; - for ( int x = 0 ; x < m_width ; bufDataIter += 4, ++x, ++i ) + for ( int x = 0 ; x < GetWidth() ; bufDataIter += 4, ++x, ++i ) { maskBufData[i] = *bufDataIter; } } - maskBuf.UngetWriteBuf( m_width * m_height ); + maskBuf.UngetWriteBuf( GetWidth() * GetHeight() ); dataProvider = wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf ); - image = ::CGImageMaskCreate( w, h, 8, 8, m_width , dataProvider, NULL, false ); + image = ::CGImageMaskCreate( w, h, 8, 8, GetWidth() , dataProvider, NULL, false ); } else { @@ -762,7 +771,7 @@ CGImageRef wxBitmapRefData::CreateCGImage() const dataProvider = wxMacCGDataProviderCreateWithMemoryBuffer( membuf ); image = ::CGImageCreate( - w, h, 8 , 32 , m_bytesPerRow , colorSpace, alphaInfo , + w, h, 8 , 32 , GetBytesPerRow() , colorSpace, alphaInfo , dataProvider, NULL , false , kCGRenderingIntentDefault ); } CGDataProviderRelease( dataProvider); @@ -789,6 +798,30 @@ CGContextRef wxBitmapRefData::GetBitmapContext() const return m_hBitmap; } +void wxBitmapRefData::SetSelectedInto(wxDC *dc) +{ + if ( dc == NULL ) + { + if ( m_selectedInto != NULL ) + EndRawAccess(); + } + else + { + wxASSERT_MSG( m_selectedInto == NULL || m_selectedInto == dc, "Bitmap already selected into a different dc"); + + if ( m_selectedInto == NULL ) + (void) BeginRawAccess(); + } + + m_selectedInto = dc; +} + +wxDC *wxBitmapRefData::GetSelectedInto() const +{ + return m_selectedInto; +} + + void wxBitmapRefData::Free() { wxASSERT_MSG( m_rawAccessCount == 0 , wxT("Bitmap still selected when destroyed") ) ; @@ -799,18 +832,15 @@ void wxBitmapRefData::Free() m_cgImageRef = NULL ; } #ifndef __WXOSX_IPHONE__ +#if wxOSX_USE_ICONREF if ( m_iconRef ) { ReleaseIconRef( m_iconRef ) ; m_iconRef = NULL ; } #endif - if ( m_hBitmap ) - { - CGContextRelease(m_hBitmap); - m_hBitmap = NULL ; - } - +#endif + m_hBitmap.reset(); wxDELETE(m_bitmapMask); } @@ -848,11 +878,9 @@ bool wxBitmap::CopyFromIcon(const wxIcon& icon) wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) { - wxBitmapRefData* bitmapRefData; + m_refData = new wxBitmapRefData( the_width , the_height , no_bits ) ; - m_refData = bitmapRefData = new wxBitmapRefData( the_width , the_height , no_bits ) ; - - if (bitmapRefData->IsOk()) + if (GetBitmapData()->IsOk()) { if ( no_bits == 1 ) { @@ -861,9 +889,9 @@ wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits linesize++; unsigned char* linestart = (unsigned char*) bits ; - unsigned char* destptr = (unsigned char*) BeginRawAccess() ; + unsigned char* destptr = (unsigned char*) GetBitmapData()->BeginRawAccess() ; - for ( int y = 0 ; y < the_height ; ++y , linestart += linesize, destptr += M_BITMAPDATA->GetBytesPerRow() ) + for ( int y = 0 ; y < the_height ; ++y , linestart += linesize, destptr += GetBitmapData()->GetBytesPerRow() ) { unsigned char* destination = destptr; int index, bit, mask; @@ -891,13 +919,13 @@ wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits } } - EndRawAccess() ; + GetBitmapData()->EndRawAccess() ; } else { wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented")); } - } /* bitmapRefData->IsOk() */ + } } wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth) @@ -930,40 +958,43 @@ wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const return new wxBitmapRefData(*static_cast(data)); } -void * wxBitmap::GetRawAccess() const +#if WXWIN_COMPATIBILITY_3_0 +const void * wxBitmap::GetRawAccess() const { wxCHECK_MSG( IsOk() , NULL , wxT("invalid bitmap") ) ; - return M_BITMAPDATA->GetRawAccess() ; + return GetBitmapData()->GetRawAccess() ; } void * wxBitmap::BeginRawAccess() { wxCHECK_MSG( IsOk() , NULL , wxT("invalid bitmap") ) ; - return M_BITMAPDATA->BeginRawAccess() ; + return GetBitmapData()->BeginRawAccess() ; } void wxBitmap::EndRawAccess() { wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ; - M_BITMAPDATA->EndRawAccess() ; + GetBitmapData()->EndRawAccess() ; } +#endif CGImageRef wxBitmap::CreateCGImage() const { wxCHECK_MSG( IsOk(), NULL , wxT("invalid bitmap") ) ; - return M_BITMAPDATA->CreateCGImage() ; + return GetBitmapData()->CreateCGImage() ; } -#ifndef __WXOSX_IPHONE__ +#if wxOSX_USE_ICONREF + IconRef wxBitmap::GetIconRef() const { wxCHECK_MSG( IsOk(), NULL , wxT("invalid bitmap") ) ; - return M_BITMAPDATA->GetIconRef() ; + return GetBitmapData()->GetIconRef() ; } IconRef wxBitmap::CreateIconRef() const @@ -986,7 +1017,7 @@ bool wxBitmap::Create(WX_NSImage image) bool isTemplate; if (!Create(wxOSXCreateBitmapContextFromNSImage(image, &isTemplate))) return false; - M_BITMAPDATA->SetTemplate(isTemplate); + GetBitmapData()->SetTemplate(isTemplate); return true; } @@ -998,16 +1029,16 @@ wxBitmap::wxBitmap(CGContextRef bitmapcontext) bool wxBitmap::Create(CGContextRef bitmapcontext) { UnRef(); - + m_refData = new wxBitmapRefData( bitmapcontext ); - - return M_BITMAPDATA->IsOk() ; + + return GetBitmapData()->IsOk() ; } WX_NSImage wxBitmap::GetNSImage() const { wxCFRef< CGImageRef > cgimage(CreateCGImage()); - return wxOSXGetNSImageFromCGImage( cgimage, GetScaleFactor(), M_BITMAPDATA->IsTemplate() ); + return wxOSXGetNSImageFromCGImage( cgimage, GetScaleFactor(), GetBitmapData()->IsTemplate() ); } #endif @@ -1038,8 +1069,8 @@ wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const int destheight = rect.height*scale ; { - unsigned char *sourcedata = (unsigned char*) GetRawAccess() ; - unsigned char *destdata = (unsigned char*) ret.BeginRawAccess() ; + unsigned char *sourcedata = (unsigned char*) GetBitmapData()->GetRawAccess() ; + unsigned char *destdata = (unsigned char*) ret.GetBitmapData()->BeginRawAccess() ; wxASSERT((sourcedata != NULL) && (destdata != NULL)); if ( (sourcedata != NULL) && (destdata != NULL) ) @@ -1054,20 +1085,19 @@ wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const memcpy( dest , source , destlinesize ) ; } } - ret.EndRawAccess() ; + ret.GetBitmapData()->EndRawAccess() ; } - - if ( M_BITMAPDATA->m_bitmapMask ) + if ( GetBitmapData()->m_bitmapMask ) { wxMemoryBuffer maskbuf ; int rowBytes = GetBestBytesPerRow( destwidth * kMaskBytesPerPixel ); size_t maskbufsize = rowBytes * destheight ; - int sourcelinesize = M_BITMAPDATA->m_bitmapMask->GetBytesPerRow() ; + int sourcelinesize = GetBitmapData()->m_bitmapMask->GetBytesPerRow() ; int destlinesize = rowBytes ; - unsigned char *source = (unsigned char *) M_BITMAPDATA->m_bitmapMask->GetRawAccess() ; + unsigned char *source = (unsigned char *) GetBitmapData()->m_bitmapMask->GetRawAccess() ; unsigned char *destdata = (unsigned char * ) maskbuf.GetWriteBuf( maskbufsize ) ; wxASSERT( (source != NULL) && (destdata != NULL) ) ; @@ -1102,7 +1132,7 @@ bool wxBitmap::Create(int w, int h, int d) m_refData = new wxBitmapRefData( w , h , d ); - return M_BITMAPDATA->IsOk() ; + return GetBitmapData()->IsOk() ; } bool wxBitmap::Create(int w, int h, const wxDC& dc) @@ -1114,22 +1144,22 @@ bool wxBitmap::Create(int w, int h, const wxDC& dc) bool wxBitmap::CreateScaled(int w, int h, int d, double logicalScale) { UnRef(); - + if ( d < 0 ) d = wxDisplayDepth() ; - + m_refData = new wxBitmapRefData( w*logicalScale , h*logicalScale , d, logicalScale ); - - return M_BITMAPDATA->IsOk() ; + + return GetBitmapData()->IsOk() ; } bool wxBitmap::Create(CGImageRef image, double scale) { UnRef(); - + m_refData = new wxBitmapRefData( image, scale ); - - return M_BITMAPDATA->IsOk() ; + + return GetBitmapData()->IsOk() ; } bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) @@ -1149,7 +1179,7 @@ bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) #if wxUSE_IMAGE double scale = 1.0; wxString fname = filename; - + if ( type == wxBITMAP_TYPE_PNG ) { if ( wxOSXGetMainScreenContentScaleFactor() > 1.9 ) @@ -1157,7 +1187,7 @@ bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) wxFileName fn(filename); fn.MakeAbsolute(); fn.SetName(fn.GetName()+"@2x"); - + if ( fn.Exists() ) { fname = fn.GetFullPath(); @@ -1210,7 +1240,7 @@ wxBitmap::wxBitmap(const wxImage& image, int depth, double scale) int height = image.GetHeight(); // we always use 32 bit internally here depth = 32; - + wxBitmapRefData* bitmapRefData; m_refData = bitmapRefData = new wxBitmapRefData( width, height, depth, scale) ; @@ -1233,12 +1263,12 @@ wxBitmap::wxBitmap(const wxImage& image, int depth, double scale) if ( hasAlpha ) UseAlpha() ; - unsigned char* destinationstart = (unsigned char*) BeginRawAccess() ; + unsigned char* destinationstart = (unsigned char*) GetBitmapData()->BeginRawAccess() ; unsigned char* data = image.GetData(); if ( destinationstart != NULL && data != NULL ) { const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ; - for (int y = 0; y < height; destinationstart += M_BITMAPDATA->GetBytesPerRow(), y++) + for (int y = 0; y < height; destinationstart += GetBitmapData()->GetBytesPerRow(), y++) { unsigned char * destination = destinationstart; for (int x = 0; x < width; x++) @@ -1268,11 +1298,11 @@ wxBitmap::wxBitmap(const wxImage& image, int depth, double scale) } } - EndRawAccess() ; + GetBitmapData()->EndRawAccess() ; } if ( image.HasMask() ) SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ; - } /* bitmapRefData->IsOk() */ + } } wxImage wxBitmap::ConvertToImage() const @@ -1289,7 +1319,7 @@ wxImage wxBitmap::ConvertToImage() const unsigned char *data = image.GetData(); wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") ); - unsigned char* sourcestart = (unsigned char*) GetRawAccess() ; + unsigned char* sourcestart = (unsigned char*) GetBitmapData()->GetRawAccess() ; bool hasAlpha = false ; bool hasMask = false ; @@ -1323,7 +1353,7 @@ wxImage wxBitmap::ConvertToImage() const static const int MASK_BLUE = 3; static const int MASK_BLUE_REPLACEMENT = 2; - for (int yy = 0; yy < height; yy++ , sourcestart += M_BITMAPDATA->GetBytesPerRow() , mask += maskBytesPerRow ) + for (int yy = 0; yy < height; yy++ , sourcestart += GetBitmapData()->GetBytesPerRow() , mask += maskBytesPerRow ) { unsigned char * maskp = mask ; const wxUint32 * source = (wxUint32*)sourcestart; @@ -1411,66 +1441,66 @@ int wxBitmap::GetHeight() const { wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); - return M_BITMAPDATA->GetHeight(); + return GetBitmapData()->GetHeight(); } int wxBitmap::GetWidth() const { wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); - return M_BITMAPDATA->GetWidth() ; + return GetBitmapData()->GetWidth() ; } double wxBitmap::GetScaleFactor() const { wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); - - return M_BITMAPDATA->GetScaleFactor() ; + + return GetBitmapData()->GetScaleFactor() ; } int wxBitmap::GetDepth() const { wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); - return M_BITMAPDATA->GetDepth(); + return GetBitmapData()->GetDepth(); } wxMask *wxBitmap::GetMask() const { wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ); - return M_BITMAPDATA->m_bitmapMask; + return GetBitmapData()->m_bitmapMask; } bool wxBitmap::HasAlpha() const { wxCHECK_MSG( IsOk(), false , wxT("invalid bitmap") ); - return M_BITMAPDATA->HasAlpha() ; + return GetBitmapData()->HasAlpha() ; } void wxBitmap::SetWidth(int w) { AllocExclusive(); - M_BITMAPDATA->SetWidth(w); + wxASSERT_MSG( GetWidth() == w, "Changing the bitmap width is not supported"); } void wxBitmap::SetHeight(int h) { AllocExclusive(); - M_BITMAPDATA->SetHeight(h); + wxASSERT_MSG( GetHeight() == h, "Changing the bitmap height is not supported"); } void wxBitmap::SetDepth(int d) { AllocExclusive(); - M_BITMAPDATA->SetDepth(d); + wxASSERT_MSG( d == -1 || GetDepth() == d, "Changing the bitmap depth is not supported"); } void wxBitmap::SetOk(bool isOk) { AllocExclusive(); - M_BITMAPDATA->SetOk(isOk); + GetBitmapData()->SetOk(isOk); } #if wxUSE_PALETTE @@ -1478,13 +1508,13 @@ wxPalette *wxBitmap::GetPalette() const { wxCHECK_MSG( IsOk(), NULL, wxT("Invalid bitmap GetPalette()") ); - return &M_BITMAPDATA->m_bitmapPalette; + return & const_cast(GetBitmapData())->m_bitmapPalette; } void wxBitmap::SetPalette(const wxPalette& palette) { AllocExclusive(); - M_BITMAPDATA->m_bitmapPalette = palette ; + GetBitmapData()->m_bitmapPalette = palette ; } #endif // wxUSE_PALETTE @@ -1492,16 +1522,16 @@ void wxBitmap::SetMask(wxMask *mask) { AllocExclusive(); // Remove existing mask if there is one. - delete M_BITMAPDATA->m_bitmapMask; + delete GetBitmapData()->m_bitmapMask; - M_BITMAPDATA->m_bitmapMask = mask ; + GetBitmapData()->m_bitmapMask = mask ; } WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const { wxUnusedVar(mask); - return WXHBITMAP(M_BITMAPDATA->GetBitmapContext()); + return WXHBITMAP(GetBitmapData()->GetBitmapContext()); } // ---------------------------------------------------------------------------- @@ -1513,20 +1543,13 @@ wxMask::wxMask() Init() ; } -wxMask::wxMask(const wxMask &tocopy) : wxObject() +wxMask::wxMask(const wxMask &tocopy) : wxMaskBase() { Init(); - m_bytesPerRow = tocopy.m_bytesPerRow; - m_width = tocopy.m_width; - m_height = tocopy.m_height; - - size_t size = m_bytesPerRow * m_height; - unsigned char* dest = (unsigned char*)m_memBuf.GetWriteBuf( size ); - unsigned char* source = (unsigned char*)tocopy.m_memBuf.GetData(); - memcpy( dest, source, size ); - m_memBuf.UngetWriteBuf( size ) ; - RealizeNative() ; + DoCreateMaskBitmap( tocopy.GetWidth(), tocopy.GetHeight(), tocopy.GetBytesPerRow()); + int size = tocopy.GetHeight() * tocopy.GetBytesPerRow(); + memcpy( GetRawAccess(), tocopy.GetRawAccess(), size); } // Construct a mask from a bitmap and a colour indicating @@ -1534,14 +1557,14 @@ wxMask::wxMask(const wxMask &tocopy) : wxObject() wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) { Init() ; - Create( bitmap, colour ); + wxMaskBase::Create( bitmap, colour ); } // Construct a mask from a mono bitmap (copies the bitmap). wxMask::wxMask( const wxBitmap& bitmap ) { Init() ; - Create( bitmap ); + wxMaskBase::Create( bitmap ); } // Construct a mask from a mono bitmap (copies the bitmap). @@ -1554,29 +1577,82 @@ wxMask::wxMask( const wxMemoryBuffer& data, int width , int height , int bytesPe wxMask::~wxMask() { - if ( m_maskBitmap ) - { - CGContextRelease( (CGContextRef) m_maskBitmap ); - m_maskBitmap = NULL ; - } } void wxMask::Init() { +#if !wxOSX_BITMAP_NATIVE_ACCESS m_width = m_height = m_bytesPerRow = 0 ; - m_maskBitmap = NULL ; +#endif } void *wxMask::GetRawAccess() const { + wxCHECK_MSG( m_maskBitmap, NULL , wxT("invalid mask") ) ; +#if !wxOSX_BITMAP_NATIVE_ACCESS return m_memBuf.GetData() ; +#else + return CGBitmapContextGetData(m_maskBitmap); +#endif } +int wxMask::GetBytesPerRow() const +{ +#if !wxOSX_BITMAP_NATIVE_ACCESS + return m_bytesPerRow ; +#else + return CGBitmapContextGetBytesPerRow(m_maskBitmap); +#endif +} + +int wxMask::GetWidth() const +{ +#if !wxOSX_BITMAP_NATIVE_ACCESS + return m_width ; +#else + return CGBitmapContextGetWidth(m_maskBitmap); +#endif +} + +int wxMask::GetHeight() const +{ +#if !wxOSX_BITMAP_NATIVE_ACCESS + return m_height ; +#else + return CGBitmapContextGetHeight(m_maskBitmap); +#endif +} + + // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well +void wxMask::DoCreateMaskBitmap(int width, int height, int bytesPerRow) +{ + wxCFRef colorspace = CGColorSpaceCreateDeviceGray(); + + if ( bytesPerRow < 0 ) + bytesPerRow = GetBestBytesPerRow(width * kMaskBytesPerPixel); +#if !wxOSX_BITMAP_NATIVE_ACCESS + m_width = width; + m_height = height; + m_bytesPerRow = bytesPerRow; + size_t size = m_bytesPerRow * m_height; + unsigned char* data = (unsigned char*)m_memBuf.GetWriteBuf(size); +#else + unsigned char* data = NULL; +#endif + m_maskBitmap = CGBitmapContextCreate(data, width, height, kMaskBytesPerPixel * 8, bytesPerRow, colorspace, + kCGImageAlphaNone); +#if !wxOSX_BITMAP_NATIVE_ACCESS + m_memBuf.UngetWriteBuf(size); +#endif + wxASSERT_MSG(m_maskBitmap, wxT("Unable to create CGBitmapContext context")); +} + void wxMask::RealizeNative() { +#if 0 if ( m_maskBitmap ) { CGContextRelease( (CGContextRef) m_maskBitmap ); @@ -1592,104 +1668,103 @@ void wxMask::RealizeNative() kCGImageAlphaNone ); CGColorSpaceRelease( colorspace ); wxASSERT_MSG( m_maskBitmap , wxT("Unable to create CGBitmapContext context") ) ; +#endif } // Create a mask from a mono bitmap (copies the bitmap). bool wxMask::Create(const wxMemoryBuffer& data,int width , int height , int bytesPerRow) { - m_memBuf = data ; - m_width = width ; - m_height = height ; - m_bytesPerRow = bytesPerRow ; - wxASSERT( data.GetDataLen() == (size_t)(height * bytesPerRow) ) ; + DoCreateMaskBitmap(width, height, bytesPerRow); + RealizeNative() ; return true ; } -// Create a mask from a mono bitmap (copies the bitmap). -bool wxMask::Create(const wxBitmap& bitmap) +bool wxMask::InitFromMonoBitmap(const wxBitmap& bitmap) { +#if wxOSX_BITMAP_NATIVE_ACCESS + int m_width, m_height, m_bytesPerRow; +#endif m_width = bitmap.GetWidth() ; m_height = bitmap.GetHeight() ; - m_bytesPerRow = GetBestBytesPerRow( m_width * kMaskBytesPerPixel ) ; - size_t size = m_bytesPerRow * m_height ; - unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ; + DoCreateMaskBitmap(m_width, m_height); +#if wxOSX_BITMAP_NATIVE_ACCESS + m_bytesPerRow = GetBytesPerRow(); +#endif + + // pixel access needs a non-const bitmap currently + wxBitmap bmp(bitmap); + wxNativePixelData data(bmp); + + wxNativePixelData::Iterator p(data); + + unsigned char * destdatabase = (unsigned char*) GetRawAccess(); wxASSERT( destdatabase != NULL ) ; - - if ( destdatabase ) + if ( destdatabase != NULL) { - memset( destdatabase , 0 , size ) ; - unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ; - - for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow ) + for ( int y = 0 ; y < m_height; ++y, destdatabase += m_bytesPerRow ) { + wxNativePixelData::Iterator rowStart = p; unsigned char *destdata = destdatabase ; - unsigned char r, g, b; - - for ( int x = 0 ; x < m_width ; ++x ) + for ( int x = 0 ; x < m_width ; ++x, ++p ) { - srcdata++ ; - r = *srcdata++ ; - g = *srcdata++ ; - b = *srcdata++ ; - - if ( ( r + g + b ) > 0x10 ) + if ( ( p.Red() + p.Green() + p.Blue() ) > 0x10 ) *destdata++ = 0xFF ; else *destdata++ = 0x00 ; } + p = rowStart; + p.OffsetY(data, 1); } } - m_memBuf.UngetWriteBuf( size ) ; - RealizeNative() ; - return true; } -// Create a mask from a bitmap and a colour indicating -// the transparent area -bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) +bool wxMask::InitFromColour(const wxBitmap& bitmap, const wxColour& colour) { +#if wxOSX_BITMAP_NATIVE_ACCESS + int m_width, m_height, m_bytesPerRow; +#endif m_width = bitmap.GetWidth() ; m_height = bitmap.GetHeight() ; - m_bytesPerRow = GetBestBytesPerRow( m_width * kMaskBytesPerPixel ) ; - size_t size = m_bytesPerRow * m_height ; - unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ; + DoCreateMaskBitmap(m_width, m_height); +#if wxOSX_BITMAP_NATIVE_ACCESS + m_bytesPerRow = GetBytesPerRow(); +#endif + + // pixel access needs a non-const bitmap currently + wxBitmap bmp(bitmap); + wxNativePixelData data(bmp); + + wxNativePixelData::Iterator p(data); + + unsigned char * destdatabase = (unsigned char*) GetRawAccess(); wxASSERT( destdatabase != NULL ) ; if ( destdatabase != NULL) { - memset( destdatabase , 0 , size ) ; - unsigned char * srcdatabase = (unsigned char*) bitmap.GetRawAccess() ; - size_t sourceBytesRow = bitmap.GetBitmapData()->GetBytesPerRow(); - - for ( int y = 0 ; y < m_height ; ++y , srcdatabase+= sourceBytesRow, destdatabase += m_bytesPerRow) + for ( int y = 0 ; y < m_height; ++y, destdatabase += m_bytesPerRow ) { - unsigned char *srcdata = srcdatabase ; + wxNativePixelData::Iterator rowStart = p; unsigned char *destdata = destdatabase ; - unsigned char r, g, b; - - for ( int x = 0 ; x < m_width ; ++x ) + for ( int x = 0 ; x < m_width ; ++x, ++p ) { - srcdata++ ; - r = *srcdata++ ; - g = *srcdata++ ; - b = *srcdata++ ; - - if ( colour == wxColour( r , g , b ) ) + if ( wxColour( p.Red(), p.Green(), p.Blue() ) == colour ) *destdata++ = 0xFF ; else *destdata++ = 0x00 ; - } + } + p = rowStart; + p.OffsetY(data, 1); } } - m_memBuf.UngetWriteBuf( size ) ; + RealizeNative() ; return true; @@ -1697,23 +1772,36 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) wxBitmap wxMask::GetBitmap() const { - wxBitmap bitmap(m_width, m_height, 1); - unsigned char* dst = static_cast(bitmap.BeginRawAccess()); - const int dst_stride = bitmap.GetBitmapData()->GetBytesPerRow(); - const unsigned char* src = static_cast(GetRawAccess()); - for (int j = 0; j < m_height; j++, src += m_bytesPerRow, dst += dst_stride) +#if wxOSX_BITMAP_NATIVE_ACCESS + int m_width, m_height, m_bytesPerRow; + m_width = GetWidth(); + m_height = GetHeight(); + m_bytesPerRow = GetBytesPerRow(); +#endif + + wxBitmap bitmap(m_width, m_height, 32); + wxNativePixelData data(bitmap); + + wxNativePixelData::Iterator p(data); + + const unsigned char* srcbase = static_cast(GetRawAccess()); + + for (int y = 0; y < m_height; ++y, srcbase += m_bytesPerRow) { - unsigned char* d = dst; - for (int i = 0; i < m_width; i++) + wxNativePixelData::Iterator rowStart = p; + const unsigned char* src = srcbase; + for (int x = 0; x < m_width; ++x, ++p, ++src) { - const unsigned char byte = src[i]; - *d++ = 0xff; - *d++ = byte; - *d++ = byte; - *d++ = byte; + const unsigned char byte = *src; + p.Alpha() = 0xff; + p.Red() = byte; + p.Green() = byte; + p.Blue() = byte; } + p = rowStart; + p.OffsetY(data, 1); } - bitmap.EndRawAccess(); + return bitmap; } @@ -1722,6 +1810,12 @@ WXHBITMAP wxMask::GetHBITMAP() const return m_maskBitmap ; } +void wxMask::FreeData() +{ + return m_maskBitmap.reset(); +} + + // ---------------------------------------------------------------------------- // Standard Handlers // ---------------------------------------------------------------------------- @@ -1729,12 +1823,12 @@ WXHBITMAP wxMask::GetHBITMAP() const class WXDLLEXPORT wxBundleResourceHandler: public wxBitmapHandler { wxDECLARE_ABSTRACT_CLASS(wxBundleResourceHandler); - + public: inline wxBundleResourceHandler() { } - + virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, wxBitmapType type, @@ -1847,27 +1941,40 @@ void wxBitmap::InitStandardHandlers() // raw bitmap access support // ---------------------------------------------------------------------------- -void *wxBitmap::GetRawData(wxPixelDataBase& data, int WXUNUSED(bpp)) +void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) { if ( !IsOk() ) // no bitmap, no data (raw or otherwise) return NULL; + if ( bpp != GetDepth() ) + return NULL; + data.m_width = GetWidth() ; data.m_height = GetHeight() ; data.m_stride = GetBitmapData()->GetBytesPerRow() ; - return BeginRawAccess() ; + return GetBitmapData()->BeginRawAccess() ; } void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(dataBase)) { - EndRawAccess() ; + GetBitmapData()->EndRawAccess() ; } void wxBitmap::UseAlpha(bool use ) { // remember that we are using alpha channel: // we'll need to create a proper mask in UngetRawData() - M_BITMAPDATA->UseAlpha( use ); + GetBitmapData()->UseAlpha( use ); +} + +void wxBitmap::SetSelectedInto(wxDC *dc) +{ + GetBitmapData()->SetSelectedInto(dc) ; +} + +wxDC* wxBitmap::GetSelectedInto() const +{ + return GetBitmapData()->GetSelectedInto() ; } diff --git a/src/osx/core/dcmemory.cpp b/src/osx/core/dcmemory.cpp index 4d66d1cef9..4aba6ca8a0 100644 --- a/src/osx/core/dcmemory.cpp +++ b/src/osx/core/dcmemory.cpp @@ -56,7 +56,7 @@ wxMemoryDCImpl::~wxMemoryDCImpl() { if ( m_selected.IsOk() ) { - m_selected.EndRawAccess() ; + m_selected.SetSelectedInto(NULL); wxDELETE(m_graphicContext); } } @@ -65,14 +65,18 @@ void wxMemoryDCImpl::DoSelect( const wxBitmap& bitmap ) { if ( m_selected.IsOk() ) { - m_selected.EndRawAccess() ; + m_selected.SetSelectedInto(NULL); wxDELETE(m_graphicContext); } m_selected = bitmap; if (m_selected.IsOk()) { - m_selected.BeginRawAccess() ; + wxASSERT_MSG( !bitmap.GetSelectedInto() || + (bitmap.GetSelectedInto() == GetOwner()), + wxT("Bitmap is selected in another wxMemoryDC, delete the first wxMemoryDC or use SelectObject(NULL)") ); + + m_selected.SetSelectedInto(GetOwner()); m_width = bitmap.GetScaledWidth(); m_height = bitmap.GetScaledHeight(); m_contentScaleFactor = bitmap.GetScaleFactor(); From 84f68aa764a8151a7529186b18b21fe3e584d55d Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Mon, 10 Sep 2018 18:59:17 +0200 Subject: [PATCH 04/18] 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(); From 148732993a5d566112f68c16686e3663ccdd5956 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Mon, 10 Sep 2018 21:20:35 +0200 Subject: [PATCH 05/18] only use native NSImage dimension --- include/wx/osx/cocoa/private.h | 1 + src/osx/carbon/icon.cpp | 18 +++--------------- src/osx/carbon/utilscocoa.mm | 5 +++++ 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index bb0f15d06c..c460861a20 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -37,6 +37,7 @@ WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromCGImage( CGImageRef image, double WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromIconRef( WXHICON iconref ); WX_NSImage WXDLLIMPEXP_CORE wxOSXGetIconForType(OSType type ); void WXDLLIMPEXP_CORE wxOSXSetImageSize(WX_NSImage image, CGFloat width, CGFloat height); +CGSize WXDLLIMPEXP_CORE wxOSXGetImageSize(WX_NSImage image); CGImageRef WXDLLIMPEXP_CORE wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage, double *scale = NULL ); CGImageRef WXDLLIMPEXP_CORE wxOSXGetCGImageFromNSImage( WX_NSImage nsimage, CGRect* r, CGContextRef cg); CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage nsimage, bool *isTemplate = NULL); diff --git a/src/osx/carbon/icon.cpp b/src/osx/carbon/icon.cpp index c1e902259a..2a6a3da20d 100644 --- a/src/osx/carbon/icon.cpp +++ b/src/osx/carbon/icon.cpp @@ -42,11 +42,8 @@ public: virtual bool IsOk() const wxOVERRIDE { return m_nsImage != 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; } + int GetWidth() const { return (int) wxOSXGetImageSize(m_nsImage).width; } + int GetHeight() const { return (int) wxOSXGetImageSize(m_nsImage).height; } WX_NSImage GetNSImage() const; @@ -55,9 +52,6 @@ private: void Create( NSImage* icon, int desiredWidth, int desiredHeight ); 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); }; @@ -78,24 +72,18 @@ wxIconRefData::wxIconRefData( WXHICON iconref, int desiredWidth, int desiredHeig ReleaseIconRef(iconref); } -void wxIconRefData::Create( NSImage* icon, int desiredWidth, int desiredHeight ) +void wxIconRefData::Create( NSImage* icon, int WXUNUSED(desiredWidth), int WXUNUSED(desiredHeight) ) { if ( icon ) { m_nsImage = icon; wxMacCocoaRetain(icon); } - - // Standard sizes - SetWidth( desiredWidth == -1 ? 32 : desiredWidth ) ; - SetHeight( desiredHeight == -1 ? 32 : desiredHeight ) ; } void wxIconRefData::Init() { m_nsImage = NULL; - m_width = - m_height = 0; } void wxIconRefData::Free() diff --git a/src/osx/carbon/utilscocoa.mm b/src/osx/carbon/utilscocoa.mm index 99d05565bf..07d6b6440c 100644 --- a/src/osx/carbon/utilscocoa.mm +++ b/src/osx/carbon/utilscocoa.mm @@ -297,6 +297,11 @@ void wxOSXSetImageSize(WX_NSImage image, CGFloat width, CGFloat height) [image setSize:NSMakeSize(width, height)]; } +CGSize wxOSXGetImageSize(WX_NSImage image) +{ + return NSSizeToCGSize([image size]); +} + CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage, double *scaleptr ) { // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html From 88a15bea484a3715db129b690c093828b58088af Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Thu, 20 Sep 2018 20:53:24 +0200 Subject: [PATCH 06/18] fixing graphics state stack error --- src/osx/carbon/utilscocoa.mm | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/osx/carbon/utilscocoa.mm b/src/osx/carbon/utilscocoa.mm index 07d6b6440c..e6b5884157 100644 --- a/src/osx/carbon/utilscocoa.mm +++ b/src/osx/carbon/utilscocoa.mm @@ -266,13 +266,11 @@ CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage ns hbitmap = CGBitmapContextCreate(NULL, imageSize.width*scale, imageSize.height*scale, 8, 0, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst); CGContextScaleCTM( hbitmap, scale, scale ); + CGContextClearRect(hbitmap,CGRectMake(0, 0, imageSize.width, imageSize.height)); NSGraphicsContext *previousContext = [NSGraphicsContext currentContext]; NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:hbitmap flipped:NO]; - [NSGraphicsContext saveGraphicsState]; [NSGraphicsContext setCurrentContext:nsGraphicsContext]; - [[NSColor whiteColor] setFill]; - NSRectFill(NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height)); [nsimage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; [NSGraphicsContext setCurrentContext:previousContext]; From 3e6d927eabb6f2685678d4c20e966b5c021d8e57 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Thu, 20 Sep 2018 20:55:00 +0200 Subject: [PATCH 07/18] using newer API --- src/osx/carbon/utilscocoa.mm | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/osx/carbon/utilscocoa.mm b/src/osx/carbon/utilscocoa.mm index e6b5884157..9b0fc6baa9 100644 --- a/src/osx/carbon/utilscocoa.mm +++ b/src/osx/carbon/utilscocoa.mm @@ -302,22 +302,13 @@ CGSize wxOSXGetImageSize(WX_NSImage image) CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage, double *scaleptr ) { - // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html - CGImageRef image = NULL; if (nsimage != nil) - { - CGContextRef context = wxOSXCreateBitmapContextFromNSImage(nsimage); + { + image = [nsimage CGImageForProposedRect:nil context:nil hints:nil]; + CFRetain(image); if ( scaleptr ) - { - // determine content scale - CGRect userrect = CGRectMake(0, 0, 10, 10); - CGRect devicerect; - devicerect = CGContextConvertRectToDeviceSpace(context, userrect); - *scaleptr = devicerect.size.height / userrect.size.height; - } - image = CGBitmapContextCreateImage(context); - CFRelease(context); + *scaleptr = CGImageGetWidth(image)/[nsimage size].width; } return image; } From ee8132fea9dddde9861a39a1e2c1e16d638c1c97 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Thu, 20 Sep 2018 21:05:17 +0200 Subject: [PATCH 08/18] using NSImage as first-class native representation If given an NSImage this will be used, unless raw bitmap data access is needed, lazily converting to a CGBitmapContext at that moment --- src/osx/core/bitmap.cpp | 331 +++++++++++++++++++++++++++------------- 1 file changed, 221 insertions(+), 110 deletions(-) diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index 709613cf69..15293a8306 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -37,12 +37,9 @@ CGDataProviderRef wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffe // Implementation Notes // -------------------- // -// we are always working with a 32 bit deep pixel buffer -// under QuickDraw its alpha parts are going to be ignored in the GWorld, -// therefore we have a separate GWorld there for blitting the mask in - -// under Quartz then content is transformed into a CGImageRef representing the same data -// which can be transferred to the GPU by the OS for fast rendering +// A bitmap can be represented using an NSImage or a CGBitmapContextRef +// If raw bitmap data needs to be accessed, then even the NSImage has to be +// rendered into a CGBitmapContextRef class WXDLLEXPORT wxBitmapRefData: public wxGDIRefData { @@ -52,31 +49,23 @@ public: wxBitmapRefData(int width , int height , int depth, double logicalscale = 1.0); wxBitmapRefData(CGContextRef context); wxBitmapRefData(CGImageRef image, double scale); + wxBitmapRefData(WX_NSImage image); wxBitmapRefData(); wxBitmapRefData(const wxBitmapRefData &tocopy); virtual ~wxBitmapRefData(); - virtual bool IsOk() const wxOVERRIDE { return m_ok; } + bool IsOk() const; void Free(); #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 - { - CGImageAlphaInfo alpha = CGBitmapContextGetAlphaInfo(m_hBitmap); - return !(alpha == kCGImageAlphaNone || - alpha == kCGImageAlphaNoneSkipFirst || - alpha == kCGImageAlphaNoneSkipLast); - } + int GetWidth() const; + int GetHeight() const; + int GetDepth() const; + int GetBytesPerRow() const; + bool HasAlpha() const; + WX_NSImage GetNSImage() const; #else void SetWidth( int width ) { m_width = width; } void SetHeight( int height ) { m_height = height; } @@ -130,7 +119,11 @@ private : bool Create(int width , int height , int depth, double logicalscale); bool Create( CGImageRef image, double scale ); bool Create( CGContextRef bitmapcontext); + bool Create( WX_NSImage image); void Init(); + + void EnsureBitmapExists() const; + void FreeDerivedRepresentations(); #if !wxOSX_BITMAP_NATIVE_ACCESS @@ -140,9 +133,11 @@ private : int m_depth; bool m_hasAlpha; wxMemoryBuffer m_memBuf; + bool m_ok; +#else + WX_NSImage m_nsImage; #endif int m_rawAccessCount; - bool m_ok; mutable CGImageRef m_cgImageRef; bool m_isTemplate; @@ -187,8 +182,9 @@ void wxBitmapRefData::Init() m_depth = 0 ; m_bytesPerRow = 0; m_hasAlpha = false; +#else + m_nsImage = NULL; #endif - m_ok = false ; m_bitmapMask = NULL ; m_cgImageRef = NULL ; m_isTemplate = false; @@ -241,6 +237,32 @@ wxBitmapRefData::wxBitmapRefData(CGImageRef image, double scale) Init(); Create( image, scale ); } + +wxBitmapRefData::wxBitmapRefData(WX_NSImage image) +{ + Init(); + Create( image ); +} + +void wxBitmapRefData::EnsureBitmapExists() const +{ + if ( ! m_hBitmap && m_nsImage) + { + wxBitmapRefData* t = const_cast(this); + t->m_hBitmap = wxOSXCreateBitmapContextFromNSImage(m_nsImage, &t->m_isTemplate); + } +} + + +bool wxBitmapRefData::Create( WX_NSImage image ) +{ + m_nsImage = image; + + wxMacCocoaRetain(image); + + return true; +} + // code from Technical Q&A QA1509 bool wxBitmapRefData::Create(CGImageRef image, double scale) @@ -289,10 +311,7 @@ bool wxBitmapRefData::Create(CGImageRef image, double scale) } /* data != NULL */ #endif } - m_ok = ( m_hBitmap != NULL ) ; - - return m_ok ; - + return IsOk() ; } bool wxBitmapRefData::Create(CGContextRef context) @@ -336,9 +355,7 @@ bool wxBitmapRefData::Create(CGContextRef context) #endif } } - m_ok = ( m_hBitmap != NULL ) ; - - return m_ok ; + return IsOk() ; } bool wxBitmapRefData::Create( int w , int h , int d, double logicalscale ) @@ -370,13 +387,94 @@ bool wxBitmapRefData::Create( int w , int h , int d, double logicalscale ) #if !wxOSX_BITMAP_NATIVE_ACCESS } /* data != NULL */ #endif - m_ok = ( m_hBitmap != NULL ) ; - - return m_ok ; + return IsOk() ; } +bool wxBitmapRefData::IsOk() const +{ + return ( m_hBitmap.get() != NULL +#if wxOSX_BITMAP_NATIVE_ACCESS + || m_nsImage != NULL +#endif + ); +} + +#if wxOSX_BITMAP_NATIVE_ACCESS + +int wxBitmapRefData::GetWidth() const +{ + wxCHECK_MSG( IsOk() , 0 , "Invalid Bitmap"); + + if ( m_hBitmap ) + return CGBitmapContextGetWidth(m_hBitmap); + else + return (int) wxOSXGetImageSize(m_nsImage).width; +} + +int wxBitmapRefData::GetHeight() const +{ + wxCHECK_MSG( IsOk() , 0 , "Invalid Bitmap"); + + if ( m_hBitmap ) + return CGBitmapContextGetHeight(m_hBitmap); + else + return (int) wxOSXGetImageSize(m_nsImage).height; +} + +int wxBitmapRefData::GetDepth() const +{ + wxCHECK_MSG( IsOk() , 0 , "Invalid Bitmap"); + + if ( m_hBitmap ) + return CGBitmapContextGetBitsPerPixel(m_hBitmap); + else + return 32; // a bitmap converted from an nsimage would have this depth +} +int wxBitmapRefData::GetBytesPerRow() const +{ + wxCHECK_MSG( IsOk() , 0 , "Invalid Bitmap"); + + if ( m_hBitmap ) + return CGBitmapContextGetBytesPerRow(m_hBitmap); + else + return GetBestBytesPerRow( GetWidth() * 4); +} + +bool wxBitmapRefData::HasAlpha() const +{ + wxCHECK_MSG( IsOk() , 0 , "Invalid Bitmap"); + + if ( m_hBitmap ) + { + CGImageAlphaInfo alpha = CGBitmapContextGetAlphaInfo(m_hBitmap); + return !(alpha == kCGImageAlphaNone || alpha == kCGImageAlphaNoneSkipFirst || alpha == kCGImageAlphaNoneSkipLast); + } + else + { + return true; // a bitmap converted from an nsimage would have alpha + } +} + +WX_NSImage wxBitmapRefData::GetNSImage() const +{ + wxCHECK_MSG( IsOk() , 0 , "Invalid Bitmap"); + + if ( !m_nsImage ) + { + wxCFRef< CGImageRef > cgimage(CreateCGImage()); + return wxOSXGetNSImageFromCGImage( cgimage, GetScaleFactor(), IsTemplate() ); + } + + return m_nsImage; +} + + +#endif + void wxBitmapRefData::UseAlpha( bool use ) { + wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ; + if ( HasAlpha() == use ) return ; @@ -400,6 +498,9 @@ void wxBitmapRefData::UseAlpha( bool use ) const void *wxBitmapRefData::GetRawAccess() const { wxCHECK_MSG( IsOk(), NULL , wxT("invalid bitmap") ) ; + + EnsureBitmapExists(); + #if !wxOSX_BITMAP_NATIVE_ACCESS return m_memBuf.GetData() ; #else @@ -665,13 +766,19 @@ IconRef wxBitmapRefData::GetIconRef() CGImageRef wxBitmapRefData::CreateCGImage() const { - wxASSERT( m_ok ) ; + wxASSERT( IsOk() ) ; wxASSERT( m_rawAccessCount >= 0 ) ; CGImageRef image ; if ( m_rawAccessCount > 0 || m_cgImageRef == NULL ) { - if ( GetDepth() != 1 && m_bitmapMask == NULL ) + if (m_nsImage) { + image = wxOSXCreateCGImageFromNSImage(m_nsImage); + } + else + { + if (GetDepth() != 1 && m_bitmapMask == NULL) + { #if 0 // in order for this code to work properly, wxMask would have to invert black and white // in the native bitmap @@ -685,85 +792,84 @@ CGImageRef wxBitmapRefData::CreateCGImage() const } else #endif - image = CGBitmapContextCreateImage( m_hBitmap ); - } - else - { - size_t imageSize = GetHeight() * GetBytesPerRow() ; - - int w = GetWidth() ; - int h = GetHeight() ; - CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ; - wxMemoryBuffer membuf; - - if ( m_bitmapMask ) - { - alphaInfo = kCGImageAlphaFirst ; - unsigned char *destalphastart = (unsigned char*) membuf.GetWriteBuf( imageSize ) ; - memcpy( destalphastart , GetRawAccess() , imageSize ) ; - unsigned char *sourcemaskstart = (unsigned char *) m_bitmapMask->GetRawAccess() ; - int maskrowbytes = m_bitmapMask->GetBytesPerRow() ; - for ( int y = 0 ; y < h ; ++y , destalphastart += GetBytesPerRow(), sourcemaskstart += maskrowbytes) - { - unsigned char *sourcemask = sourcemaskstart ; - unsigned char *destalpha = destalphastart ; - for ( int x = 0 ; x < w ; ++x , sourcemask += kMaskBytesPerPixel , destalpha += 4 ) - { - *destalpha = 0xFF - *sourcemask ; - } - } - membuf.UngetWriteBuf( imageSize ); + image = CGBitmapContextCreateImage(m_hBitmap); } else { - if ( HasAlpha() ) + size_t imageSize = GetHeight() * GetBytesPerRow(); + + int w = GetWidth(); + int h = GetHeight(); + CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst; + wxMemoryBuffer membuf; + + if (m_bitmapMask) { + alphaInfo = kCGImageAlphaFirst; + unsigned char* destalphastart = (unsigned char*)membuf.GetWriteBuf(imageSize); + memcpy(destalphastart, GetRawAccess(), imageSize); + unsigned char* sourcemaskstart = (unsigned char*)m_bitmapMask->GetRawAccess(); + int maskrowbytes = m_bitmapMask->GetBytesPerRow(); + for (int y = 0; y < h; ++y, destalphastart += GetBytesPerRow(), sourcemaskstart += maskrowbytes) + { + unsigned char* sourcemask = sourcemaskstart; + unsigned char* destalpha = destalphastart; + for (int x = 0; x < w; ++x, sourcemask += kMaskBytesPerPixel, destalpha += 4) + { + *destalpha = 0xFF - *sourcemask; + } + } + membuf.UngetWriteBuf(imageSize); + } + else + { + if (HasAlpha()) + { #if wxOSX_USE_PREMULTIPLIED_ALPHA - alphaInfo = kCGImageAlphaPremultipliedFirst ; + alphaInfo = kCGImageAlphaPremultipliedFirst; #else - alphaInfo = kCGImageAlphaFirst ; + alphaInfo = kCGImageAlphaFirst; #endif - } -#if wxOSX_BITMAP_NATIVE_ACCESS - memcpy(membuf.GetWriteBuf( imageSize ),GetRawAccess(),imageSize); - membuf.UngetWriteBuf(imageSize); -#endif - } - - CGDataProviderRef dataProvider = NULL ; - if ( GetDepth() == 1 ) - { - // TODO CHECK ALIGNMENT - wxMemoryBuffer maskBuf; - unsigned char * maskBufData = (unsigned char*) maskBuf.GetWriteBuf( GetWidth() * GetHeight() ); - unsigned char * bufData = (unsigned char *) membuf.GetData() ; - // copy one color component - size_t i = 0; - for( int y = 0 ; y < GetHeight() ; bufData+= GetBytesPerRow(), ++y ) - { - unsigned char *bufDataIter = bufData+3; - for ( int x = 0 ; x < GetWidth() ; bufDataIter += 4, ++x, ++i ) - { - maskBufData[i] = *bufDataIter; } +#if wxOSX_BITMAP_NATIVE_ACCESS + memcpy(membuf.GetWriteBuf(imageSize), GetRawAccess(), imageSize); + membuf.UngetWriteBuf(imageSize); +#endif } - maskBuf.UngetWriteBuf( GetWidth() * GetHeight() ); - dataProvider = - wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf ); + CGDataProviderRef dataProvider = NULL; + if (GetDepth() == 1) + { + // TODO CHECK ALIGNMENT + wxMemoryBuffer maskBuf; + unsigned char* maskBufData = (unsigned char*)maskBuf.GetWriteBuf(GetWidth() * GetHeight()); + unsigned char* bufData = (unsigned char*)membuf.GetData(); + // copy one color component + size_t i = 0; + for (int y = 0; y < GetHeight(); bufData += GetBytesPerRow(), ++y) + { + unsigned char* bufDataIter = bufData + 3; + for (int x = 0; x < GetWidth(); bufDataIter += 4, ++x, ++i) + { + maskBufData[i] = *bufDataIter; + } + } + maskBuf.UngetWriteBuf(GetWidth() * GetHeight()); - image = ::CGImageMaskCreate( w, h, 8, 8, GetWidth() , dataProvider, NULL, false ); + dataProvider = wxMacCGDataProviderCreateWithMemoryBuffer(maskBuf); + + image = ::CGImageMaskCreate(w, h, 8, 8, GetWidth(), dataProvider, NULL, false); + } + else + { + CGColorSpaceRef colorSpace = wxMacGetGenericRGBColorSpace(); + dataProvider = wxMacCGDataProviderCreateWithMemoryBuffer(membuf); + image = ::CGImageCreate( + w, h, 8, 32, GetBytesPerRow(), colorSpace, alphaInfo, + dataProvider, NULL, false, kCGRenderingIntentDefault); + } + CGDataProviderRelease(dataProvider); } - else - { - CGColorSpaceRef colorSpace = wxMacGetGenericRGBColorSpace(); - dataProvider = wxMacCGDataProviderCreateWithMemoryBuffer( membuf ); - image = - ::CGImageCreate( - w, h, 8 , 32 , GetBytesPerRow() , colorSpace, alphaInfo , - dataProvider, NULL , false , kCGRenderingIntentDefault ); - } - CGDataProviderRelease( dataProvider); } } else @@ -832,6 +938,8 @@ void wxBitmapRefData::Free() FreeDerivedRepresentations(); + wxMacCocoaRelease(m_nsImage); + m_hBitmap.reset(); wxDELETE(m_bitmapMask); } @@ -849,6 +957,9 @@ wxBitmapRefData::~wxBitmapRefData() bool wxBitmap::CopyFromIcon(const wxIcon& icon) { +#if wxOSX_BITMAP_NATIVE_ACCESS + return Create( icon.GetNSImage() ); +#else int w = icon.GetWidth() ; int h = icon.GetHeight() ; @@ -866,6 +977,7 @@ bool wxBitmap::CopyFromIcon(const wxIcon& icon) } return false; +#endif } wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) @@ -1006,11 +1118,11 @@ wxBitmap::wxBitmap(WX_NSImage image) bool wxBitmap::Create(WX_NSImage image) { - bool isTemplate; - if (!Create(wxOSXCreateBitmapContextFromNSImage(image, &isTemplate))) - return false; - GetBitmapData()->SetTemplate(isTemplate); - return true; + UnRef(); + + m_refData = new wxBitmapRefData( image ); + + return GetBitmapData()->IsOk() ; } wxBitmap::wxBitmap(CGContextRef bitmapcontext) @@ -1029,8 +1141,7 @@ bool wxBitmap::Create(CGContextRef bitmapcontext) WX_NSImage wxBitmap::GetNSImage() const { - wxCFRef< CGImageRef > cgimage(CreateCGImage()); - return wxOSXGetNSImageFromCGImage( cgimage, GetScaleFactor(), GetBitmapData()->IsTemplate() ); + return GetBitmapData()->GetNSImage(); } #endif From 3370b763181bb865b041a963be93f2b719c20f4c Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Thu, 20 Sep 2018 21:53:22 +0200 Subject: [PATCH 09/18] Direct NSImage drawing --- include/wx/osx/cocoa/private.h | 4 ++++ src/osx/carbon/utilscocoa.mm | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index c460861a20..2d231e63df 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -32,6 +32,10 @@ OSStatus WXDLLIMPEXP_CORE wxMacDrawCGImage( CGContextRef inContext, const CGRect * inBounds, CGImageRef inImage) ; +void WXDLLIMPEXP_CORE wxOSXDrawNSImage( + CGContextRef inContext, + const CGRect * inBounds, + WX_NSImage inImage) ; WX_NSImage WXDLLIMPEXP_CORE wxOSXGetSystemImage(const wxString& name); WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromCGImage( CGImageRef image, double scale = 1.0, bool isTemplate = false); WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromIconRef( WXHICON iconref ); diff --git a/src/osx/carbon/utilscocoa.mm b/src/osx/carbon/utilscocoa.mm index 9b0fc6baa9..2cde919b82 100644 --- a/src/osx/carbon/utilscocoa.mm +++ b/src/osx/carbon/utilscocoa.mm @@ -280,6 +280,30 @@ CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage ns return hbitmap; } +void WXDLLIMPEXP_CORE wxOSXDrawNSImage( + CGContextRef inContext, + const CGRect * inBounds, + WX_NSImage inImage) +{ + if (inImage != nil) + { + CGContextSaveGState(inContext); + CGContextTranslateCTM(inContext, inBounds->origin.x, inBounds->origin.y + inBounds->size.height); + CGRect r = *inBounds; + r.origin.x = r.origin.y = 0; + CGContextScaleCTM(inContext, 1, -1); + + NSGraphicsContext *previousContext = [NSGraphicsContext currentContext]; + NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:inContext flipped:NO]; + [NSGraphicsContext setCurrentContext:nsGraphicsContext]; + [inImage drawInRect:NSRectFromCGRect(r) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; + [NSGraphicsContext setCurrentContext:previousContext]; + + CGContextRestoreGState(inContext); + + } +} + double wxOSXGetMainScreenContentScaleFactor() { return [[NSScreen mainScreen] backingScaleFactor]; From 0a812865cf020c65242e1ef4664020c19e1d181f Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Fri, 21 Sep 2018 16:09:54 +0200 Subject: [PATCH 10/18] switching to generic icon for macOS --- build/bakefiles/files.bkl | 5 ++--- build/cmake/files.cmake | 5 ++--- build/files | 5 ++--- build/osx/wxcocoa.xcodeproj/project.pbxproj | 2 +- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/build/bakefiles/files.bkl b/build/bakefiles/files.bkl index 5168c49ada..5dc4572ee7 100644 --- a/build/bakefiles/files.bkl +++ b/build/bakefiles/files.bkl @@ -2435,7 +2435,6 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file! src/osx/carbon/cursor.cpp src/osx/carbon/fontdlg.cpp src/osx/carbon/gdiobj.cpp - src/osx/carbon/icon.cpp src/osx/carbon/app.cpp src/osx/carbon/control.cpp src/osx/carbon/dataobj.cpp @@ -2468,6 +2467,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file! src/generic/prntdlgg.cpp src/generic/statusbr.cpp src/generic/textmeasure.cpp + src/generic/icon.cpp @@ -2511,7 +2511,6 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file! wx/osx/fontdlg.h wx/osx/frame.h wx/osx/gauge.h - wx/osx/icon.h wx/osx/imaglist.h wx/osx/listbox.h wx/osx/listctrl.h @@ -2566,6 +2565,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file! wx/generic/prntdlgg.h wx/generic/statusbr.h wx/osx/appprogress.h + wx/generic/icon.h @@ -2656,7 +2656,6 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file! $(OSX_COMMON_SRC) src/generic/regiong.cpp - src/generic/icon.cpp src/osx/iphone/anybutton.mm src/osx/iphone/button.mm diff --git a/build/cmake/files.cmake b/build/cmake/files.cmake index cd6cbeeae2..9183db4a9b 100644 --- a/build/cmake/files.cmake +++ b/build/cmake/files.cmake @@ -2312,7 +2312,6 @@ set(OSX_COMMON_SRC src/osx/carbon/cursor.cpp src/osx/carbon/fontdlg.cpp src/osx/carbon/gdiobj.cpp - src/osx/carbon/icon.cpp src/osx/carbon/app.cpp src/osx/carbon/control.cpp src/osx/carbon/dataobj.cpp @@ -2345,6 +2344,7 @@ set(OSX_COMMON_SRC src/generic/prntdlgg.cpp src/generic/statusbr.cpp src/generic/textmeasure.cpp + src/generic/icon.cpp #TODO: ) @@ -2387,7 +2387,6 @@ set(OSX_SHARED_HDR wx/osx/fontdlg.h wx/osx/frame.h wx/osx/gauge.h - wx/osx/icon.h wx/osx/imaglist.h wx/osx/listbox.h wx/osx/listctrl.h @@ -2442,6 +2441,7 @@ set(OSX_SHARED_HDR wx/generic/prntdlgg.h wx/generic/statusbr.h wx/osx/appprogress.h + wx/generic/icon.h ) set(OSX_COCOA_SRC @@ -2525,7 +2525,6 @@ set(OSX_COCOA_HDR set(OSX_IPHONE_SRC ${OSX_COMMON_SRC} src/generic/regiong.cpp - src/generic/icon.cpp src/osx/cocoa/stdpaths.mm # iphone files src/osx/iphone/anybutton.mm diff --git a/build/files b/build/files index dad7f4ccd1..5fde938a4c 100644 --- a/build/files +++ b/build/files @@ -2296,7 +2296,6 @@ OSX_COMMON_SRC = src/osx/carbon/cursor.cpp src/osx/carbon/fontdlg.cpp src/osx/carbon/gdiobj.cpp - src/osx/carbon/icon.cpp src/osx/carbon/app.cpp src/osx/carbon/control.cpp src/osx/carbon/dataobj.cpp @@ -2325,6 +2324,7 @@ OSX_COMMON_SRC = src/generic/filepickerg.cpp src/generic/fontdlgg.cpp src/generic/fontpickerg.cpp + src/generic/icon.cpp src/generic/listctrl.cpp src/generic/prntdlgg.cpp src/generic/statusbr.cpp @@ -2371,7 +2371,6 @@ OSX_SHARED_HDR = wx/osx/fontdlg.h wx/osx/frame.h wx/osx/gauge.h - wx/osx/icon.h wx/osx/imaglist.h wx/osx/listbox.h wx/osx/listctrl.h @@ -2422,6 +2421,7 @@ OSX_SHARED_HDR = wx/generic/filepickerg.h wx/generic/fontdlgg.h wx/generic/fontpickerg.h + wx/generic/icon.h wx/generic/listctrl.h wx/generic/prntdlgg.h wx/generic/statusbr.h @@ -2511,7 +2511,6 @@ OSX_COCOA_HDR = OSX_IPHONE_SRC = $(OSX_COMMON_SRC) src/generic/animateg.cpp - src/generic/icon.cpp src/generic/regiong.cpp src/osx/cocoa/stdpaths.mm src/osx/core/sound.cpp diff --git a/build/osx/wxcocoa.xcodeproj/project.pbxproj b/build/osx/wxcocoa.xcodeproj/project.pbxproj index 98a74c5db0..dc990c893a 100644 --- a/build/osx/wxcocoa.xcodeproj/project.pbxproj +++ b/build/osx/wxcocoa.xcodeproj/project.pbxproj @@ -4754,7 +4754,7 @@ F4B85051B7C835A8BF4E3EE1 /* xh_panel.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = xh_panel.cpp; path = ../../src/xrc/xh_panel.cpp; sourceTree = ""; }; F4C72C5C61A6335C8B418BA1 /* LexMetapost.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = LexMetapost.cxx; path = ../../src/stc/scintilla/lexers/LexMetapost.cxx; sourceTree = ""; }; F52DCBC0442233738B39138E /* CaseFolder.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = CaseFolder.cxx; path = ../../src/stc/scintilla/src/CaseFolder.cxx; sourceTree = ""; }; - F5DAF1F49F0F3F41A427A21D /* icon.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = icon.cpp; path = ../../src/osx/carbon/icon.cpp; sourceTree = ""; }; + F5DAF1F49F0F3F41A427A21D /* icon.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = icon.cpp; path = ../../src/generic/icon.cpp; sourceTree = ""; }; F6EA240B3DB93D398A990FAD /* tif_dirread.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; name = tif_dirread.c; path = ../../src/tiff/libtiff/tif_dirread.c; sourceTree = ""; }; F6F01A84F4DE3C9FB9849004 /* tif_jbig.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; name = tif_jbig.c; path = ../../src/tiff/libtiff/tif_jbig.c; sourceTree = ""; }; F7332A03D93D3DABB050615D /* dir.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = dir.cpp; path = ../../src/unix/dir.cpp; sourceTree = ""; }; From 34716b18a6a536be566d4271307b0588e1a5c599 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Fri, 21 Sep 2018 16:11:44 +0200 Subject: [PATCH 11/18] new ICNS Handler --- src/osx/core/bitmap.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index 15293a8306..cb0b5cc26a 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -1967,6 +1967,21 @@ public: wxIMPLEMENT_DYNAMIC_CLASS(wxJPEGResourceHandler, wxBundleResourceHandler); +class WXDLLEXPORT wxICNSResourceHandler: public wxBundleResourceHandler +{ + wxDECLARE_DYNAMIC_CLASS(wxICNSResourceHandler); + +public: + inline wxICNSResourceHandler() + { + SetName(wxT("icns resource")); + SetExtension("icns"); + SetType(wxBITMAP_TYPE_ICON_RESOURCE); + } +}; + +wxIMPLEMENT_DYNAMIC_CLASS(wxICNSResourceHandler, wxBundleResourceHandler); + bool wxBundleResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, wxBitmapType WXUNUSED(type), @@ -2030,7 +2045,8 @@ wxBitmap wxBitmapHelpers::NewFromPNGData(const void* data, size_t size) void wxBitmap::InitStandardHandlers() { #if wxOSX_USE_COCOA_OR_CARBON - AddHandler( new wxICONResourceHandler ) ; + // no icns on iOS + AddHandler( new wxICNSResourceHandler ) ; #endif AddHandler( new wxPNGResourceHandler ); AddHandler( new wxJPEGResourceHandler ); From 0bdd7e5f480a005d259d45a964cd145bf53ad1fe Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Fri, 21 Sep 2018 16:15:58 +0200 Subject: [PATCH 12/18] forgotten commit of header change for generic icon --- include/wx/icon.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/wx/icon.h b/include/wx/icon.h index 0f7c45e904..0849ab98b8 100644 --- a/include/wx/icon.h +++ b/include/wx/icon.h @@ -47,8 +47,9 @@ #elif defined(__WXMAC__) #if wxOSX_USE_COCOA_OR_CARBON #define wxICON_DEFAULT_TYPE wxBITMAP_TYPE_ICON_RESOURCE - #include "wx/osx/icon.h" + #include "wx/generic/icon.h" #else + // iOS and others #define wxICON_DEFAULT_TYPE wxBITMAP_TYPE_PNG_RESOURCE #include "wx/generic/icon.h" #endif From bca583234efe5f53a362e378ad876256f9a1650f Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Fri, 21 Sep 2018 16:17:32 +0200 Subject: [PATCH 13/18] use direct NSImage drawing commands --- src/osx/carbon/graphics.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/osx/carbon/graphics.cpp b/src/osx/carbon/graphics.cpp index 5c806893b2..98ce48469d 100644 --- a/src/osx/carbon/graphics.cpp +++ b/src/osx/carbon/graphics.cpp @@ -2159,8 +2159,15 @@ void wxMacCoreGraphicsContext::Rotate( wxDouble angle ) void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) { +#if wxOSX_USE_COCOA + { + CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h ); + wxOSXDrawNSImage( m_cgContext, &r, bmp.GetNSImage()); + } +#else wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp); DrawBitmap(bitmap, x, y, w, h); +#endif } void wxMacCoreGraphicsContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) @@ -2218,10 +2225,7 @@ void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDoubl #if wxOSX_USE_COCOA { CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h ); - const WX_NSImage nsImage = icon.GetNSImage(); - - CGImageRef cgImage = wxOSXGetCGImageFromNSImage( nsImage , &r, m_cgContext ); - wxMacDrawCGImage( m_cgContext, &r, cgImage); + wxOSXDrawNSImage( m_cgContext, &r, icon.GetNSImage()); } #endif From 266f32493e52414e925c8f463eb9646a6ea05e21 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Fri, 21 Sep 2018 20:58:06 +0200 Subject: [PATCH 14/18] Removing non-native bitmap code, using common code for iOS and macOS --- include/wx/defs.h | 3 + include/wx/osx/bitmap.h | 11 +- include/wx/osx/cocoa/private.h | 4 - include/wx/osx/core/private.h | 9 ++ src/osx/carbon/utilscocoa.mm | 111 ++++++++------ src/osx/core/bitmap.cpp | 262 ++++++++------------------------- 6 files changed, 147 insertions(+), 253 deletions(-) diff --git a/include/wx/defs.h b/include/wx/defs.h index c2336f6223..c47e26ad79 100644 --- a/include/wx/defs.h +++ b/include/wx/defs.h @@ -2764,6 +2764,7 @@ DECLARE_WXCOCOA_OBJC_CLASS(NSString); typedef WX_NSWindow WXWindow; typedef WX_NSView WXWidget; +typedef WX_NSImage WXImage; typedef WX_NSMenu WXHMENU; typedef WX_NSOpenGLPixelFormat WXGLPixelFormat; typedef WX_NSOpenGLContext WXGLContext; @@ -2771,6 +2772,7 @@ typedef WX_NSOpenGLContext WXGLContext; #elif wxOSX_USE_IPHONE DECLARE_WXCOCOA_OBJC_CLASS(UIWindow); +DECLARE_WXCOCOA_OBJC_CLASS(UImage); DECLARE_WXCOCOA_OBJC_CLASS(UIView); DECLARE_WXCOCOA_OBJC_CLASS(UIFont); DECLARE_WXCOCOA_OBJC_CLASS(UIImage); @@ -2781,6 +2783,7 @@ DECLARE_WXCOCOA_OBJC_CLASS(UIWebView); typedef WX_UIWindow WXWindow; typedef WX_UIView WXWidget; +typedef WX_UIImage WXImage; typedef WX_EAGLContext WXGLContext; typedef WX_NSString WXGLPixelFormat; typedef WX_UIWebView OSXWebViewPtr; diff --git a/include/wx/osx/bitmap.h b/include/wx/osx/bitmap.h index 621e94bcc6..364f1de6f8 100644 --- a/include/wx/osx/bitmap.h +++ b/include/wx/osx/bitmap.h @@ -118,7 +118,7 @@ public: // creates an bitmap from the native image format wxBitmap(CGImageRef image, double scale = 1.0); - wxBitmap(WX_NSImage image); + wxBitmap(WXImage image); wxBitmap(CGContextRef bitmapcontext); // Create a bitmap compatible with the given DC @@ -147,7 +147,7 @@ public: virtual bool Create(const void* data, wxBitmapType type, int width, int height, int depth = 1); bool Create( CGImageRef image, double scale = 1.0 ); - bool Create( WX_NSImage image ); + bool Create( WXImage image ); bool Create( CGContextRef bitmapcontext); // Create a bitmap compatible with the given DC, inheriting its magnification factor @@ -207,13 +207,16 @@ public: // returns a CGImageRef which must released after usage with CGImageRelease CGImageRef CreateCGImage() const ; + WXImage GetImage() const; #if wxOSX_USE_COCOA // returns an autoreleased version of the image - WX_NSImage GetNSImage() const; + WX_NSImage GetNSImage() const + { return GetImage(); } #endif #if wxOSX_USE_IPHONE // returns an autoreleased version of the image - WX_UIImage GetUIImage() const; + WX_UIImage GetUIImage() const + { return GetImage(); } #endif #if WXWIN_COMPATIBILITY_3_0 diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index 2d231e63df..599ad14eb6 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -41,10 +41,6 @@ WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromCGImage( CGImageRef image, double WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromIconRef( WXHICON iconref ); WX_NSImage WXDLLIMPEXP_CORE wxOSXGetIconForType(OSType type ); void WXDLLIMPEXP_CORE wxOSXSetImageSize(WX_NSImage image, CGFloat width, CGFloat height); -CGSize WXDLLIMPEXP_CORE wxOSXGetImageSize(WX_NSImage image); -CGImageRef WXDLLIMPEXP_CORE wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage, double *scale = NULL ); -CGImageRef WXDLLIMPEXP_CORE wxOSXGetCGImageFromNSImage( WX_NSImage nsimage, CGRect* r, CGContextRef cg); -CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage nsimage, bool *isTemplate = NULL); wxBitmap WXDLLIMPEXP_CORE wxOSXCreateSystemBitmap(const wxString& id, const wxString &client, const wxSize& size); WXWindow WXDLLIMPEXP_CORE wxOSXGetMainWindow(); diff --git a/include/wx/osx/core/private.h b/include/wx/osx/core/private.h index d5acfa3e13..3ae1783205 100644 --- a/include/wx/osx/core/private.h +++ b/include/wx/osx/core/private.h @@ -114,6 +114,15 @@ WXDLLIMPEXP_CORE CGColorSpaceRef wxMacGetGenericRGBColorSpace(void); WXDLLIMPEXP_CORE double wxOSXGetMainScreenContentScaleFactor(); +// UI + +CGSize WXDLLIMPEXP_CORE wxOSXGetImageSize(WXImage image); +CGImageRef WXDLLIMPEXP_CORE wxOSXCreateCGImageFromImage( WXImage nsimage, double *scale = NULL ); +CGImageRef WXDLLIMPEXP_CORE wxOSXGetCGImageFromImage( WXImage nsimage, CGRect* r, CGContextRef cg); +CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromImage( WXImage nsimage, bool *isTemplate = NULL); +WXImage WXDLLIMPEXP_CORE wxOSXGetImageFromCGImage( CGImageRef image, double scale = 1.0, bool isTemplate = false); + + class wxWindowMac; // to extern wxWindow* g_MacLastWindow; diff --git a/src/osx/carbon/utilscocoa.mm b/src/osx/carbon/utilscocoa.mm index 2cde919b82..ac6b06f204 100644 --- a/src/osx/carbon/utilscocoa.mm +++ b/src/osx/carbon/utilscocoa.mm @@ -165,11 +165,6 @@ WXWindow wxOSXGetKeyWindow() #if wxOSX_USE_IPHONE -WX_UIImage wxOSXGetUIImageFromCGImage( CGImageRef image ) -{ - return [UIImage imageWithCGImage:image]; -} - wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &client, const wxSize& size) { #if 1 @@ -181,29 +176,11 @@ wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &client, c #endif } -double wxOSXGetMainScreenContentScaleFactor() -{ - double scale; - -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000 - if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) - { - scale=[[UIScreen mainScreen] scale]; - } - else -#endif - { - scale=1.0; - } - - return scale; -} - #endif #if wxOSX_USE_COCOA -WX_NSImage wxOSXGetSystemImage(const wxString& name) +WXImage wxOSXGetSystemImage(const wxString& name) { wxCFStringRef cfname(name); NSImage* nsimage = [NSImage imageNamed:cfname.AsNSString()]; @@ -222,38 +199,47 @@ wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &client, c return wxNullBitmap; } -WX_NSImage wxOSXGetNSImageFromCGImage( CGImageRef image, double scaleFactor, bool isTemplate ) +#endif + +WXImage wxOSXGetImageFromCGImage( CGImageRef image, double scaleFactor, bool isTemplate ) { - NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0); - - // Get the image dimensions. - imageRect.size.height = CGImageGetHeight(image)/scaleFactor; - imageRect.size.width = CGImageGetWidth(image)/scaleFactor; - - NSImage* newImage = [[NSImage alloc] initWithCGImage:image size:imageRect.size]; +#if wxOSX_USE_COCOA + NSSize sz; + sz.height = CGImageGetHeight(image)/scaleFactor; + sz.width = CGImageGetWidth(image)/scaleFactor; + NSImage* newImage = [[NSImage alloc] initWithCGImage:image size:sz]; [newImage setTemplate:isTemplate]; [newImage autorelease]; return( newImage ); +#else + return [UIImage imageWithCGImage:image scale:scaleFactor orientation:UIImageOrientationUp]; +#endif } -WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromIconRef( WXHICON iconref ) +#if wxOSX_USE_ICONREF +WXImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromIconRef( WXHICON iconref ) { NSImage *newImage = [[NSImage alloc] initWithIconRef:iconref]; [newImage autorelease]; return( newImage ); } +#endif -CGImageRef WXDLLIMPEXP_CORE wxOSXGetCGImageFromNSImage( WX_NSImage nsimage, CGRect* r, CGContextRef cg) +CGImageRef WXDLLIMPEXP_CORE wxOSXGetCGImageFromImage( WXImage nsimage, CGRect* r, CGContextRef cg) { +#if wxOSX_USE_COCOA NSRect nsRect = NSRectFromCGRect(*r); return [nsimage CGImageForProposedRect:&nsRect context:[NSGraphicsContext graphicsContextWithGraphicsPort:cg flipped:YES] hints:nil]; +#else + return [nsimage CGImage]; +#endif } -CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage nsimage, bool *isTemplate) +CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromImage( WXImage nsimage, bool *isTemplate) { // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html @@ -261,13 +247,18 @@ CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage ns if (nsimage != nil) { double scale = wxOSXGetMainScreenContentScaleFactor(); - + +#if wxOSX_USE_COCOA NSSize imageSize = [nsimage size]; +#else + CGSize imageSize = [nsimage size]; +#endif hbitmap = CGBitmapContextCreate(NULL, imageSize.width*scale, imageSize.height*scale, 8, 0, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst); CGContextScaleCTM( hbitmap, scale, scale ); CGContextClearRect(hbitmap,CGRectMake(0, 0, imageSize.width, imageSize.height)); - + +#if wxOSX_USE_COCOA NSGraphicsContext *previousContext = [NSGraphicsContext currentContext]; NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:hbitmap flipped:NO]; [NSGraphicsContext setCurrentContext:nsGraphicsContext]; @@ -276,6 +267,10 @@ CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage ns if (isTemplate) *isTemplate = [nsimage isTemplate]; +#else + wxUnusedVar(isTemplate); + CGContextDrawImage(hbitmap,CGRectMake(0, 0, imageSize.width, imageSize.height),[nsimage CGImage]); +#endif } return hbitmap; } @@ -283,7 +278,7 @@ CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage ns void WXDLLIMPEXP_CORE wxOSXDrawNSImage( CGContextRef inContext, const CGRect * inBounds, - WX_NSImage inImage) + WXImage inImage) { if (inImage != nil) { @@ -293,12 +288,15 @@ void WXDLLIMPEXP_CORE wxOSXDrawNSImage( r.origin.x = r.origin.y = 0; CGContextScaleCTM(inContext, 1, -1); - NSGraphicsContext *previousContext = [NSGraphicsContext currentContext]; +#if wxOSX_USE_COCOA + NSGraphicsContext *previousContext = [NSGraphicsContext currentContext]; NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:inContext flipped:NO]; [NSGraphicsContext setCurrentContext:nsGraphicsContext]; [inImage drawInRect:NSRectFromCGRect(r) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; [NSGraphicsContext setCurrentContext:previousContext]; - +#else + CGContextDrawImage(inContext, *inBounds, [inImage CGImage]); +#endif CGContextRestoreGState(inContext); } @@ -306,30 +304,50 @@ void WXDLLIMPEXP_CORE wxOSXDrawNSImage( double wxOSXGetMainScreenContentScaleFactor() { +#if wxOSX_USE_COCOA return [[NSScreen mainScreen] backingScaleFactor]; +#else + return [[UIScreen mainScreen] scale]; +#endif } -WX_NSImage wxOSXGetIconForType(OSType type ) +WXImage wxOSXGetIconForType(OSType type ) { +#if wxOSX_USE_COCOA return [[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode(type)]; +#else + return NULL; +#endif } -void wxOSXSetImageSize(WX_NSImage image, CGFloat width, CGFloat height) +void wxOSXSetImageSize(WXImage image, CGFloat width, CGFloat height) { +#if wxOSX_USE_COCOA [image setSize:NSMakeSize(width, height)]; +#else + // TODO +#endif } -CGSize wxOSXGetImageSize(WX_NSImage image) +CGSize wxOSXGetImageSize(WXImage image) { +#if wxOSX_USE_COCOA return NSSizeToCGSize([image size]); +#else + return [image size]; +#endif } -CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage, double *scaleptr ) +CGImageRef wxOSXCreateCGImageFromImage( WXImage nsimage, double *scaleptr ) { CGImageRef image = NULL; if (nsimage != nil) { +#if wxOSX_USE_COCOA image = [nsimage CGImageForProposedRect:nil context:nil hints:nil]; +#else + image = [nsimage CGImage]; +#endif CFRetain(image); if ( scaleptr ) *scaleptr = CGImageGetWidth(image)/[nsimage size].width; @@ -343,6 +361,8 @@ CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage, double *scaleptr ) // copied from cursor.mm +#if wxOSX_USE_COCOA + static NSCursor* wxGetStockCursor( short sIndex ) { ClassicCursor* pCursor = &gMacCursors[sIndex]; @@ -560,6 +580,7 @@ void wxMacCocoaShowCursor() { [NSCursor unhide]; } +#endif //--------------------------------------------------------- // helper functions for NSString<->wxString conversion @@ -588,6 +609,8 @@ NSString* wxNSStringWithWxString(const wxString &wxstring) // appearance in effect // ---------------------------------------------------------------------------- +#if wxOSX_USE_COCOA + wxOSXEffectiveAppearanceSetter::wxOSXEffectiveAppearanceSetter() { #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_14 diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index cb0b5cc26a..a870521711 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -49,7 +49,7 @@ public: wxBitmapRefData(int width , int height , int depth, double logicalscale = 1.0); wxBitmapRefData(CGContextRef context); wxBitmapRefData(CGImageRef image, double scale); - wxBitmapRefData(WX_NSImage image); + wxBitmapRefData(WXImage image); wxBitmapRefData(); wxBitmapRefData(const wxBitmapRefData &tocopy); @@ -59,24 +59,12 @@ public: void Free(); -#if wxOSX_BITMAP_NATIVE_ACCESS int GetWidth() const; int GetHeight() const; int GetDepth() const; int GetBytesPerRow() const; bool HasAlpha() const; - WX_NSImage GetNSImage() const; -#else - void SetWidth( int width ) { m_width = width; } - void SetHeight( int height ) { m_height = height; } - void SetDepth( int depth ) { m_depth = depth; } - - int GetWidth() const { return m_width; } - int GetHeight() const { return m_height; } - int GetDepth() const { return m_depth; } - int GetBytesPerRow() const { return m_bytesPerRow; } - bool HasAlpha() const { return m_hasAlpha; } -#endif + WXImage GetImage() const; double GetScaleFactor() const { return m_scaleFactor; } const void *GetRawAccess() const; @@ -119,24 +107,14 @@ private : bool Create(int width , int height , int depth, double logicalscale); bool Create( CGImageRef image, double scale ); bool Create( CGContextRef bitmapcontext); - bool Create( WX_NSImage image); + bool Create( WXImage image); void Init(); void EnsureBitmapExists() const; void FreeDerivedRepresentations(); -#if !wxOSX_BITMAP_NATIVE_ACCESS - int m_width; - int m_height; - int m_bytesPerRow; - int m_depth; - bool m_hasAlpha; - wxMemoryBuffer m_memBuf; - bool m_ok; -#else - WX_NSImage m_nsImage; -#endif + WXImage m_nsImage; int m_rawAccessCount; mutable CGImageRef m_cgImageRef; bool m_isTemplate; @@ -155,7 +133,7 @@ private : static const int kBestByteAlignement = 16; static const int kMaskBytesPerPixel = 1; -static int GetBestBytesPerRow( int rawBytes ) +static size_t GetBestBytesPerRow( size_t rawBytes ) { return (((rawBytes)+kBestByteAlignement-1) & ~(kBestByteAlignement-1) ); } @@ -176,15 +154,7 @@ CGImageRef wxMacCreateCGImageFromBitmap( const wxBitmap& bitmap ) void wxBitmapRefData::Init() { -#if !wxOSX_BITMAP_NATIVE_ACCESS - m_width = 0 ; - m_height = 0 ; - m_depth = 0 ; - m_bytesPerRow = 0; - m_hasAlpha = false; -#else m_nsImage = NULL; -#endif m_bitmapMask = NULL ; m_cgImageRef = NULL ; m_isTemplate = false; @@ -238,7 +208,7 @@ wxBitmapRefData::wxBitmapRefData(CGImageRef image, double scale) Create( image, scale ); } -wxBitmapRefData::wxBitmapRefData(WX_NSImage image) +wxBitmapRefData::wxBitmapRefData(WXImage image) { Init(); Create( image ); @@ -249,12 +219,12 @@ void wxBitmapRefData::EnsureBitmapExists() const if ( ! m_hBitmap && m_nsImage) { wxBitmapRefData* t = const_cast(this); - t->m_hBitmap = wxOSXCreateBitmapContextFromNSImage(m_nsImage, &t->m_isTemplate); + t->m_hBitmap = wxOSXCreateBitmapContextFromImage(m_nsImage, &t->m_isTemplate); } } -bool wxBitmapRefData::Create( WX_NSImage image ) +bool wxBitmapRefData::Create( WXImage image ) { m_nsImage = image; @@ -267,51 +237,34 @@ bool wxBitmapRefData::Create( WX_NSImage image ) bool wxBitmapRefData::Create(CGImageRef image, double scale) { - if ( image != NULL ) + if (image != NULL) { -#if wxOSX_BITMAP_NATIVE_ACCESS - int m_width, m_height, m_depth, m_bytesPerRow; -#endif - m_width = CGImageGetWidth(image); - m_height = CGImageGetHeight(image); - m_depth = 32; + size_t m_width = CGImageGetWidth(image); + size_t m_height = CGImageGetHeight(image); + m_hBitmap = NULL; m_scaleFactor = scale; - m_bytesPerRow = GetBestBytesPerRow( m_width * 4 ) ; -#if !wxOSX_BITMAP_NATIVE_ACCESS - size_t size = m_bytesPerRow * m_height ; - void* data = m_memBuf.GetWriteBuf( size ) ; - if ( data != NULL ) - { - memset( data , 0 , size ) ; - m_memBuf.UngetWriteBuf( size ) ; -#else - void* data = NULL; -#endif - CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image); - if ( alpha == kCGImageAlphaNone || alpha == kCGImageAlphaNoneSkipFirst || alpha == kCGImageAlphaNoneSkipLast ) - { - m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst ); - } - else - { -#if !wxOSX_BITMAP_NATIVE_ACCESS - m_hasAlpha = true; -#endif - m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst ); - } - CGRect rect = CGRectMake(0,0,m_width,m_height); - CGContextDrawImage(m_hBitmap, rect, image); + size_t m_bytesPerRow = GetBestBytesPerRow(m_width * 4); + void* data = NULL; - wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ; - CGContextTranslateCTM( m_hBitmap, 0, m_height ); - CGContextScaleCTM( m_hBitmap, 1*m_scaleFactor, -1*m_scaleFactor ); -#if !wxOSX_BITMAP_NATIVE_ACCESS - } /* data != NULL */ -#endif + CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image); + if (alpha == kCGImageAlphaNone || alpha == kCGImageAlphaNoneSkipFirst || alpha == kCGImageAlphaNoneSkipLast) + { + m_hBitmap = CGBitmapContextCreate((char*)data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst); + } + else + { + m_hBitmap = CGBitmapContextCreate((char*)data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst); + } + CGRect rect = CGRectMake(0, 0, m_width, m_height); + CGContextDrawImage(m_hBitmap, rect, image); + + wxASSERT_MSG(m_hBitmap, wxT("Unable to create CGBitmapContext context")); + CGContextTranslateCTM(m_hBitmap, 0, m_height); + CGContextScaleCTM(m_hBitmap, 1 * m_scaleFactor, -1 * m_scaleFactor); } - return IsOk() ; + return IsOk(); } bool wxBitmapRefData::Create(CGContextRef context) @@ -319,94 +272,47 @@ bool wxBitmapRefData::Create(CGContextRef context) if ( context != NULL && CGBitmapContextGetData(context) ) { m_hBitmap = context; -#if !wxOSX_BITMAP_NATIVE_ACCESS - m_bytesPerRow = CGBitmapContextGetBytesPerRow(context); - m_width = CGBitmapContextGetWidth(context); - m_height = CGBitmapContextGetHeight(context); - m_depth = CGBitmapContextGetBitsPerPixel(context) ; -#endif // our own contexts conform to this, always. wxASSERT( GetDepth() == 32 ); -#if !wxOSX_BITMAP_NATIVE_ACCESS - size_t size = GetBytesPerRow() * GetHeight() ; - void* data = m_memBuf.GetWriteBuf( size ) ; - - memcpy( data , CGBitmapContextGetData(context) , size ) ; - m_memBuf.UngetWriteBuf( size ) ; -#endif - // determine content scale CGRect userrect = CGRectMake(0, 0, 10, 10); CGRect devicerect; devicerect = CGContextConvertRectToDeviceSpace(context, userrect); m_scaleFactor = devicerect.size.height / userrect.size.height; - - CGImageAlphaInfo alpha = CGBitmapContextGetAlphaInfo(context); - - if ( alpha == kCGImageAlphaNone || alpha == kCGImageAlphaNoneSkipFirst || alpha == kCGImageAlphaNoneSkipLast ) - { - // no alpha - } - else - { -#if !wxOSX_BITMAP_NATIVE_ACCESS - m_hasAlpha = true; -#endif - } } return IsOk() ; } -bool wxBitmapRefData::Create( int w , int h , int d, double logicalscale ) +bool wxBitmapRefData::Create(int w, int h, int d, double logicalscale) { -#if wxOSX_BITMAP_NATIVE_ACCESS - int m_width, m_height, m_depth, m_bytesPerRow; -#endif - m_width = wxMax(1, w); - m_height = wxMax(1, h); - m_depth = d ; - m_scaleFactor = logicalscale; - m_hBitmap = NULL ; + size_t m_width = wxMax(1, w); + size_t m_height = wxMax(1, h); - m_bytesPerRow = GetBestBytesPerRow( m_width * 4 ) ; -#if !wxOSX_BITMAP_NATIVE_ACCESS - size_t size = m_bytesPerRow * m_height ; - void* data = m_memBuf.GetWriteBuf( size ) ; - if ( data != NULL ) - { - memset( data , 0 , size ) ; - m_memBuf.UngetWriteBuf( size ) ; -#else - void* data = NULL; -#endif - m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst ); - wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ; - CGContextTranslateCTM( m_hBitmap, 0, m_height ); - CGContextScaleCTM( m_hBitmap, 1*GetScaleFactor(), -1*GetScaleFactor() ); -#if !wxOSX_BITMAP_NATIVE_ACCESS - } /* data != NULL */ -#endif - return IsOk() ; + int m_scaleFactor = logicalscale; + m_hBitmap = NULL; + + size_t m_bytesPerRow = GetBestBytesPerRow(m_width * 4); + void* data = NULL; + m_hBitmap = CGBitmapContextCreate((char*)data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst); + wxASSERT_MSG(m_hBitmap, wxT("Unable to create CGBitmapContext context")); + CGContextTranslateCTM(m_hBitmap, 0, m_height); + CGContextScaleCTM(m_hBitmap, 1 * GetScaleFactor(), -1 * GetScaleFactor()); + + return IsOk(); } bool wxBitmapRefData::IsOk() const { - return ( m_hBitmap.get() != NULL -#if wxOSX_BITMAP_NATIVE_ACCESS - || m_nsImage != NULL -#endif - ); + return (m_hBitmap.get() != NULL || m_nsImage != NULL); } -#if wxOSX_BITMAP_NATIVE_ACCESS - int wxBitmapRefData::GetWidth() const { wxCHECK_MSG( IsOk() , 0 , "Invalid Bitmap"); if ( m_hBitmap ) - return CGBitmapContextGetWidth(m_hBitmap); + return (int) CGBitmapContextGetWidth(m_hBitmap); else return (int) wxOSXGetImageSize(m_nsImage).width; } @@ -416,7 +322,7 @@ int wxBitmapRefData::GetHeight() const wxCHECK_MSG( IsOk() , 0 , "Invalid Bitmap"); if ( m_hBitmap ) - return CGBitmapContextGetHeight(m_hBitmap); + return (int) CGBitmapContextGetHeight(m_hBitmap); else return (int) wxOSXGetImageSize(m_nsImage).height; } @@ -426,7 +332,7 @@ int wxBitmapRefData::GetDepth() const wxCHECK_MSG( IsOk() , 0 , "Invalid Bitmap"); if ( m_hBitmap ) - return CGBitmapContextGetBitsPerPixel(m_hBitmap); + return (int) CGBitmapContextGetBitsPerPixel(m_hBitmap); else return 32; // a bitmap converted from an nsimage would have this depth } @@ -435,9 +341,9 @@ int wxBitmapRefData::GetBytesPerRow() const wxCHECK_MSG( IsOk() , 0 , "Invalid Bitmap"); if ( m_hBitmap ) - return CGBitmapContextGetBytesPerRow(m_hBitmap); + return (int) CGBitmapContextGetBytesPerRow(m_hBitmap); else - return GetBestBytesPerRow( GetWidth() * 4); + return (int) GetBestBytesPerRow( GetWidth() * 4); } bool wxBitmapRefData::HasAlpha() const @@ -455,22 +361,19 @@ bool wxBitmapRefData::HasAlpha() const } } -WX_NSImage wxBitmapRefData::GetNSImage() const +WXImage wxBitmapRefData::GetImage() const { wxCHECK_MSG( IsOk() , 0 , "Invalid Bitmap"); if ( !m_nsImage ) { wxCFRef< CGImageRef > cgimage(CreateCGImage()); - return wxOSXGetNSImageFromCGImage( cgimage, GetScaleFactor(), IsTemplate() ); + return wxOSXGetImageFromCGImage( cgimage, GetScaleFactor(), IsTemplate() ); } return m_nsImage; } - -#endif - void wxBitmapRefData::UseAlpha( bool use ) { wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ; @@ -478,16 +381,10 @@ void wxBitmapRefData::UseAlpha( bool use ) if ( HasAlpha() == use ) return ; -#if !wxOSX_BITMAP_NATIVE_ACCESS - m_hasAlpha = use ; - void *data = m_memBuf.GetData(); -#else - void *data = NULL; -#endif - CGContextRef hBitmap = CGBitmapContextCreate(data, GetWidth(), GetHeight(), 8, GetBytesPerRow(), wxMacGetGenericRGBColorSpace(), use ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst ); -#if wxOSX_BITMAP_NATIVE_ACCESS + CGContextRef hBitmap = CGBitmapContextCreate(NULL, GetWidth(), GetHeight(), 8, GetBytesPerRow(), wxMacGetGenericRGBColorSpace(), use ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst ); + memcpy(CGBitmapContextGetData(hBitmap),CGBitmapContextGetData(m_hBitmap),GetBytesPerRow()*GetHeight()); -#endif + wxASSERT_MSG( hBitmap , wxT("Unable to create CGBitmapContext context") ) ; CGContextTranslateCTM( hBitmap, 0, GetHeight() ); CGContextScaleCTM( hBitmap, GetScaleFactor(), -GetScaleFactor() ); @@ -501,11 +398,7 @@ const void *wxBitmapRefData::GetRawAccess() const EnsureBitmapExists(); -#if !wxOSX_BITMAP_NATIVE_ACCESS - return m_memBuf.GetData() ; -#else return CGBitmapContextGetData(m_hBitmap); -#endif } void *wxBitmapRefData::GetRawAccess() @@ -773,7 +666,7 @@ CGImageRef wxBitmapRefData::CreateCGImage() const { if (m_nsImage) { - image = wxOSXCreateCGImageFromNSImage(m_nsImage); + image = wxOSXCreateCGImageFromImage(m_nsImage); } else { @@ -957,27 +850,7 @@ wxBitmapRefData::~wxBitmapRefData() bool wxBitmap::CopyFromIcon(const wxIcon& icon) { -#if wxOSX_BITMAP_NATIVE_ACCESS - return Create( icon.GetNSImage() ); -#else - int w = icon.GetWidth() ; - int h = icon.GetHeight() ; - - if ( Create( w, h ) ) - { - // Assume 32 bpp icon has transparency values - UseAlpha(icon.GetDepth() == 32); - - wxMemoryDC dc ; - dc.SelectObject( *this ) ; - dc.DrawIcon( icon , 0 , 0 ) ; - dc.SelectObject( wxNullBitmap ) ; - - return true; - } - - return false; -#endif + return Create( icon.GetImage() ); } wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) @@ -1109,14 +982,12 @@ IconRef wxBitmap::CreateIconRef() const } #endif -#if wxOSX_USE_COCOA - -wxBitmap::wxBitmap(WX_NSImage image) +wxBitmap::wxBitmap(WXImage image) { (void)Create(image); } -bool wxBitmap::Create(WX_NSImage image) +bool wxBitmap::Create(WXImage image) { UnRef(); @@ -1139,22 +1010,11 @@ bool wxBitmap::Create(CGContextRef bitmapcontext) return GetBitmapData()->IsOk() ; } -WX_NSImage wxBitmap::GetNSImage() const +WXImage wxBitmap::GetImage() const { - return GetBitmapData()->GetNSImage(); + return GetBitmapData()->GetImage(); } -#endif - -#if wxOSX_USE_IPHONE - -WX_UIImage wxBitmap::GetUIImage() const -{ - wxCFRef< CGImageRef > cgimage(CreateCGImage()); - return wxOSXGetUIImageFromCGImage( cgimage ); -} - -#endif wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const { wxCHECK_MSG( IsOk() && From 7254515c54e252ee895d60d9e7e9805a3c27f0d1 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Fri, 21 Sep 2018 20:59:36 +0200 Subject: [PATCH 15/18] emoving non-native bitmap code, using common code for iOS and macOS second part, sources were missing --- src/osx/carbon/graphics.cpp | 4 ++-- src/osx/carbon/icon.cpp | 14 +++++++------- src/osx/carbon/utilscocoa.mm | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/osx/carbon/graphics.cpp b/src/osx/carbon/graphics.cpp index 98ce48469d..c2fc7523b6 100644 --- a/src/osx/carbon/graphics.cpp +++ b/src/osx/carbon/graphics.cpp @@ -2162,7 +2162,7 @@ void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDo #if wxOSX_USE_COCOA { CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h ); - wxOSXDrawNSImage( m_cgContext, &r, bmp.GetNSImage()); + wxOSXDrawNSImage( m_cgContext, &r, bmp.GetImage()); } #else wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp); @@ -2225,7 +2225,7 @@ void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDoubl #if wxOSX_USE_COCOA { CGRect r = CGRectMake( (CGFloat) x , (CGFloat) y , (CGFloat) w , (CGFloat) h ); - wxOSXDrawNSImage( m_cgContext, &r, icon.GetNSImage()); + wxOSXDrawNSImage( m_cgContext, &r, icon.GetImage()); } #endif diff --git a/src/osx/carbon/icon.cpp b/src/osx/carbon/icon.cpp index 2a6a3da20d..f1ad130e40 100644 --- a/src/osx/carbon/icon.cpp +++ b/src/osx/carbon/icon.cpp @@ -45,7 +45,7 @@ public: int GetWidth() const { return (int) wxOSXGetImageSize(m_nsImage).width; } int GetHeight() const { return (int) wxOSXGetImageSize(m_nsImage).height; } - WX_NSImage GetNSImage() const; + WX_NSImage GetImage() const; private: void Init(); @@ -94,7 +94,7 @@ void wxIconRefData::Free() } } -WX_NSImage wxIconRefData::GetNSImage() const +WX_NSImage wxIconRefData::GetImage() const { wxASSERT( IsOk() ); @@ -123,7 +123,7 @@ public: WXHICON GetHICON() const { return (WXHICON) m_iconRef; } - WX_NSImage GetNSImage() const; + WX_NSImage GetImage() const; private: void Init(); @@ -172,7 +172,7 @@ void wxIconRefData::Free() } } -WX_NSImage wxIconRefData::GetNSImage() const +WX_NSImage wxIconRefData::GetImage() const { wxASSERT( IsOk() ); @@ -269,11 +269,11 @@ int wxIcon::GetDepth() const return 32; } -WX_NSImage wxIcon::GetNSImage() const +WX_NSImage wxIcon::GetImage() const { wxCHECK_MSG( IsOk(), NULL, wxT("invalid icon") ); - return M_ICONDATA->GetNSImage() ; + return M_ICONDATA->GetImage() ; } #if WXWIN_COMPATIBILITY_3_0 @@ -565,7 +565,7 @@ void wxIcon::CopyFromBitmap( const wxBitmap& bmp ) UnRef() ; #if wxOSX_ICON_USE_NSIMAGE - m_refData = new wxIconRefData( bmp.GetNSImage() , bmp.GetWidth(), bmp.GetHeight() ) ; + m_refData = new wxIconRefData( bmp.GetImage() , bmp.GetWidth(), bmp.GetHeight() ) ; #else // as the bitmap owns that ref, we have to acquire it as well diff --git a/src/osx/carbon/utilscocoa.mm b/src/osx/carbon/utilscocoa.mm index ac6b06f204..73a4d4a07b 100644 --- a/src/osx/carbon/utilscocoa.mm +++ b/src/osx/carbon/utilscocoa.mm @@ -560,7 +560,7 @@ WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hot firstTime = NO; } - NSImage *nsImage = wxOSXGetNSImageFromCGImage( cgImageRef ); + NSImage *nsImage = wxOSXGetImageFromCGImage( cgImageRef ); NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )]; return cursor; From f29faa0217e827871c841a209dc228810bf590d4 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Sat, 22 Sep 2018 10:18:27 +0200 Subject: [PATCH 16/18] optimize handling of high-res native images --- include/wx/osx/cocoa/private.h | 2 +- include/wx/osx/core/private.h | 2 -- src/osx/carbon/utilscocoa.mm | 20 +++++++++++++++----- src/osx/core/bitmap.cpp | 29 ++++++++++------------------- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index 599ad14eb6..de91d02245 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -41,7 +41,7 @@ WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromCGImage( CGImageRef image, double WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromIconRef( WXHICON iconref ); WX_NSImage WXDLLIMPEXP_CORE wxOSXGetIconForType(OSType type ); void WXDLLIMPEXP_CORE wxOSXSetImageSize(WX_NSImage image, CGFloat width, CGFloat height); - +double WXDLLIMPEXP_CORE wxOSXGetImageScaleFactor(WXImage image); wxBitmap WXDLLIMPEXP_CORE wxOSXCreateSystemBitmap(const wxString& id, const wxString &client, const wxSize& size); WXWindow WXDLLIMPEXP_CORE wxOSXGetMainWindow(); WXWindow WXDLLIMPEXP_CORE wxOSXGetKeyWindow(); diff --git a/include/wx/osx/core/private.h b/include/wx/osx/core/private.h index 3ae1783205..b9cffaa443 100644 --- a/include/wx/osx/core/private.h +++ b/include/wx/osx/core/private.h @@ -104,8 +104,6 @@ protected : // Quartz -WXDLLIMPEXP_CORE CGImageRef wxMacCreateCGImageFromBitmap( const wxBitmap& bitmap ); - WXDLLIMPEXP_CORE CGDataProviderRef wxMacCGDataProviderCreateWithCFData( CFDataRef data ); WXDLLIMPEXP_CORE CGDataConsumerRef wxMacCGDataConsumerCreateWithCFData( CFMutableDataRef data ); WXDLLIMPEXP_CORE CGDataProviderRef wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer& buf ); diff --git a/src/osx/carbon/utilscocoa.mm b/src/osx/carbon/utilscocoa.mm index 73a4d4a07b..6c5ace2279 100644 --- a/src/osx/carbon/utilscocoa.mm +++ b/src/osx/carbon/utilscocoa.mm @@ -248,11 +248,7 @@ CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromImage( WXImage nsimage { double scale = wxOSXGetMainScreenContentScaleFactor(); -#if wxOSX_USE_COCOA - NSSize imageSize = [nsimage size]; -#else - CGSize imageSize = [nsimage size]; -#endif + CGSize imageSize = wxOSXGetImageSize(nsimage); hbitmap = CGBitmapContextCreate(NULL, imageSize.width*scale, imageSize.height*scale, 8, 0, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst); CGContextScaleCTM( hbitmap, scale, scale ); @@ -329,6 +325,20 @@ void wxOSXSetImageSize(WXImage image, CGFloat width, CGFloat height) #endif } +double wxOSXGetImageScaleFactor(WXImage image) +{ +#if wxOSX_USE_COCOA + NSSize imagesize = [image size]; + int width = [[image bestRepresentationForRect:NSMakeRect(0, 0, imagesize.width, imagesize.height) context:nil hints:nil] pixelsWide]; + if ( width == 0 ) // there are multi-res representations which return 0 for the pixel dimensions + return wxOSXGetMainScreenContentScaleFactor(); + + return width / [image size].width; +#else + return [image scale]; +#endif +} + CGSize wxOSXGetImageSize(WXImage image) { #if wxOSX_USE_COCOA diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index a870521711..85d90e13bd 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -138,20 +138,6 @@ static size_t GetBestBytesPerRow( size_t rawBytes ) return (((rawBytes)+kBestByteAlignement-1) & ~(kBestByteAlignement-1) ); } -#if wxUSE_GUI && !defined(__WXOSX_IPHONE__) - -// this is used for more controls than just the wxBitmap button, also for notebooks etc - -CGImageRef wxMacCreateCGImageFromBitmap( const wxBitmap& bitmap ) -{ - const wxBitmapRefData * bmap = bitmap.GetBitmapData() ; - if ( bmap == NULL ) - return NULL ; - return (CGImageRef) bmap->CreateCGImage(); -} - -#endif //wxUSE_BMPBUTTON - void wxBitmapRefData::Init() { m_nsImage = NULL; @@ -230,6 +216,8 @@ bool wxBitmapRefData::Create( WXImage image ) wxMacCocoaRetain(image); + m_scaleFactor = wxOSXGetImageScaleFactor(image); + return true; } @@ -289,7 +277,7 @@ bool wxBitmapRefData::Create(int w, int h, int d, double logicalscale) size_t m_width = wxMax(1, w); size_t m_height = wxMax(1, h); - int m_scaleFactor = logicalscale; + m_scaleFactor = logicalscale; m_hBitmap = NULL; size_t m_bytesPerRow = GetBestBytesPerRow(m_width * 4); @@ -314,7 +302,7 @@ int wxBitmapRefData::GetWidth() const if ( m_hBitmap ) return (int) CGBitmapContextGetWidth(m_hBitmap); else - return (int) wxOSXGetImageSize(m_nsImage).width; + return (int) wxOSXGetImageSize(m_nsImage).width * m_scaleFactor; } int wxBitmapRefData::GetHeight() const @@ -324,7 +312,7 @@ int wxBitmapRefData::GetHeight() const if ( m_hBitmap ) return (int) CGBitmapContextGetHeight(m_hBitmap); else - return (int) wxOSXGetImageSize(m_nsImage).height; + return (int) wxOSXGetImageSize(m_nsImage).height * m_scaleFactor; } int wxBitmapRefData::GetDepth() const @@ -1274,6 +1262,11 @@ wxImage wxBitmap::ConvertToImage() const wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid bitmap") ); + // this call may trigger a conversion from platform image to bitmap, issue it + // before any measurements are taken, multi-resolution platform images may be + // rendered incorrectly otherwise + unsigned char* sourcestart = (unsigned char*) GetBitmapData()->GetRawAccess() ; + // create an wxImage object int width = GetWidth(); int height = GetHeight(); @@ -1282,8 +1275,6 @@ wxImage wxBitmap::ConvertToImage() const unsigned char *data = image.GetData(); wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") ); - unsigned char* sourcestart = (unsigned char*) GetBitmapData()->GetRawAccess() ; - bool hasAlpha = false ; bool hasMask = false ; int maskBytesPerRow = 0 ; From 37ba39f19da4434e0a22950b8049e92ef389a17c Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Sat, 22 Sep 2018 12:55:34 +0200 Subject: [PATCH 17/18] makefile change using generic instead of osx icon file --- Makefile.in | 76 +++++++++++++++++++++-------------------------------- 1 file changed, 30 insertions(+), 46 deletions(-) diff --git a/Makefile.in b/Makefile.in index 3cddb02412..d7bc3e8d99 100644 --- a/Makefile.in +++ b/Makefile.in @@ -3305,7 +3305,6 @@ COND_TOOLKIT_OSX_COCOA_GUI_HDR = \ wx/osx/fontdlg.h \ wx/osx/frame.h \ wx/osx/gauge.h \ - wx/osx/icon.h \ wx/osx/imaglist.h \ wx/osx/listbox.h \ wx/osx/listctrl.h \ @@ -3359,6 +3358,7 @@ COND_TOOLKIT_OSX_COCOA_GUI_HDR = \ wx/generic/prntdlgg.h \ wx/generic/statusbr.h \ wx/osx/appprogress.h \ + wx/generic/icon.h \ wx/osx/cocoa/chkconf.h \ wx/osx/cocoa/evtloop.h \ wx/osx/cocoa/private.h \ @@ -3415,7 +3415,6 @@ COND_TOOLKIT_OSX_IPHONE_GUI_HDR = \ wx/osx/fontdlg.h \ wx/osx/frame.h \ wx/osx/gauge.h \ - wx/osx/icon.h \ wx/osx/imaglist.h \ wx/osx/listbox.h \ wx/osx/listctrl.h \ @@ -3469,6 +3468,7 @@ COND_TOOLKIT_OSX_IPHONE_GUI_HDR = \ wx/generic/prntdlgg.h \ wx/generic/statusbr.h \ wx/osx/appprogress.h \ + wx/generic/icon.h \ wx/osx/iphone/chkconf.h \ wx/osx/iphone/evtloop.h \ wx/osx/iphone/private.h \ @@ -5610,7 +5610,6 @@ COND_TOOLKIT_OSX_COCOA___GUI_SRC_OBJECTS = \ COND_TOOLKIT_OSX_IPHONE___GUI_SRC_OBJECTS = \ $(__OSX_COMMON_SRC_OBJECTS) \ monodll_regiong.o \ - monodll_generic_icon.o \ monodll_iphone_anybutton.o \ monodll_iphone_button.o \ monodll_iphone_checkbox.o \ @@ -7574,7 +7573,6 @@ COND_TOOLKIT_OSX_COCOA___GUI_SRC_OBJECTS_1 = \ COND_TOOLKIT_OSX_IPHONE___GUI_SRC_OBJECTS_1 = \ $(__OSX_COMMON_SRC_OBJECTS_0) \ monolib_regiong.o \ - monolib_generic_icon.o \ monolib_iphone_anybutton.o \ monolib_iphone_button.o \ monolib_iphone_checkbox.o \ @@ -9685,7 +9683,6 @@ COND_TOOLKIT_OSX_COCOA___GUI_SRC_OBJECTS_2 = \ COND_TOOLKIT_OSX_IPHONE___GUI_SRC_OBJECTS_2 = \ $(__OSX_COMMON_SRC_OBJECTS_8) \ coredll_regiong.o \ - coredll_generic_icon.o \ coredll_iphone_anybutton.o \ coredll_iphone_button.o \ coredll_iphone_checkbox.o \ @@ -11391,7 +11388,6 @@ COND_TOOLKIT_OSX_COCOA___GUI_SRC_OBJECTS_3 = \ COND_TOOLKIT_OSX_IPHONE___GUI_SRC_OBJECTS_3 = \ $(__OSX_COMMON_SRC_OBJECTS_9) \ corelib_regiong.o \ - corelib_generic_icon.o \ corelib_iphone_anybutton.o \ corelib_iphone_button.o \ corelib_iphone_checkbox.o \ @@ -13093,7 +13089,6 @@ COND_PLATFORM_MACOSX_1___OSX_COMMON_SRC_OBJECTS = \ monodll_carbon_cursor.o \ monodll_carbon_fontdlg.o \ monodll_carbon_gdiobj.o \ - monodll_carbon_icon.o \ monodll_carbon_app.o \ monodll_carbon_control.o \ monodll_carbon_dataobj.o \ @@ -13123,7 +13118,8 @@ COND_PLATFORM_MACOSX_1___OSX_COMMON_SRC_OBJECTS = \ monodll_generic_listctrl.o \ monodll_prntdlgg.o \ monodll_generic_statusbr.o \ - monodll_generic_textmeasure.o + monodll_generic_textmeasure.o \ + monodll_generic_icon.o @COND_PLATFORM_MACOSX_1@__OSX_COMMON_SRC_OBJECTS = $(COND_PLATFORM_MACOSX_1___OSX_COMMON_SRC_OBJECTS) COND_PLATFORM_MACOSX_1___GTK_PLATFORM_SRC_OBJECTS = \ monodll_generic_caret.o \ @@ -13236,7 +13232,6 @@ COND_PLATFORM_MACOSX_1___OSX_COMMON_SRC_OBJECTS_0 = \ monolib_carbon_cursor.o \ monolib_carbon_fontdlg.o \ monolib_carbon_gdiobj.o \ - monolib_carbon_icon.o \ monolib_carbon_app.o \ monolib_carbon_control.o \ monolib_carbon_dataobj.o \ @@ -13266,7 +13261,8 @@ COND_PLATFORM_MACOSX_1___OSX_COMMON_SRC_OBJECTS_0 = \ monolib_generic_listctrl.o \ monolib_prntdlgg.o \ monolib_generic_statusbr.o \ - monolib_generic_textmeasure.o + monolib_generic_textmeasure.o \ + monolib_generic_icon.o @COND_PLATFORM_MACOSX_1@__OSX_COMMON_SRC_OBJECTS_0 = $(COND_PLATFORM_MACOSX_1___OSX_COMMON_SRC_OBJECTS_0) COND_PLATFORM_MACOSX_1___GTK_PLATFORM_SRC_OBJECTS_27 = \ monolib_generic_caret.o \ @@ -13379,7 +13375,6 @@ COND_PLATFORM_MACOSX_1___OSX_COMMON_SRC_OBJECTS_8 = \ coredll_carbon_cursor.o \ coredll_carbon_fontdlg.o \ coredll_carbon_gdiobj.o \ - coredll_carbon_icon.o \ coredll_carbon_app.o \ coredll_carbon_control.o \ coredll_carbon_dataobj.o \ @@ -13409,7 +13404,8 @@ COND_PLATFORM_MACOSX_1___OSX_COMMON_SRC_OBJECTS_8 = \ coredll_generic_listctrl.o \ coredll_prntdlgg.o \ coredll_generic_statusbr.o \ - coredll_generic_textmeasure.o + coredll_generic_textmeasure.o \ + coredll_generic_icon.o @COND_PLATFORM_MACOSX_1@__OSX_COMMON_SRC_OBJECTS_8 = $(COND_PLATFORM_MACOSX_1___OSX_COMMON_SRC_OBJECTS_8) COND_PLATFORM_MACOSX_1___GTK_PLATFORM_SRC_OBJECTS_1_4 = \ coredll_generic_caret.o \ @@ -13519,7 +13515,6 @@ COND_PLATFORM_MACOSX_1___OSX_COMMON_SRC_OBJECTS_9 = \ corelib_carbon_cursor.o \ corelib_carbon_fontdlg.o \ corelib_carbon_gdiobj.o \ - corelib_carbon_icon.o \ corelib_carbon_app.o \ corelib_carbon_control.o \ corelib_carbon_dataobj.o \ @@ -13549,7 +13544,8 @@ COND_PLATFORM_MACOSX_1___OSX_COMMON_SRC_OBJECTS_9 = \ corelib_generic_listctrl.o \ corelib_prntdlgg.o \ corelib_generic_statusbr.o \ - corelib_generic_textmeasure.o + corelib_generic_textmeasure.o \ + corelib_generic_icon.o @COND_PLATFORM_MACOSX_1@__OSX_COMMON_SRC_OBJECTS_9 = $(COND_PLATFORM_MACOSX_1___OSX_COMMON_SRC_OBJECTS_9) COND_PLATFORM_MACOSX_1___GTK_PLATFORM_SRC_OBJECTS_2_2 = \ corelib_generic_caret.o \ @@ -17981,8 +17977,11 @@ monodll_sound_sdl.o: $(srcdir)/src/unix/sound_sdl.cpp $(MONODLL_ODEP) @COND_TOOLKIT_MSW_USE_GUI_1@monodll_msw_joystick.o: $(srcdir)/src/msw/joystick.cpp $(MONODLL_ODEP) @COND_TOOLKIT_MSW_USE_GUI_1@ $(CXXC) -c -o $@ $(MONODLL_CXXFLAGS) $(srcdir)/src/msw/joystick.cpp -@COND_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@monodll_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(MONODLL_ODEP) -@COND_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONODLL_CXXFLAGS) $(srcdir)/src/generic/icon.cpp +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@monodll_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(MONODLL_ODEP) +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONODLL_CXXFLAGS) $(srcdir)/src/generic/icon.cpp + +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@monodll_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(MONODLL_ODEP) +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONODLL_CXXFLAGS) $(srcdir)/src/generic/icon.cpp @COND_TOOLKIT_QT_USE_GUI_1_WXUNIV_0@monodll_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(MONODLL_ODEP) @COND_TOOLKIT_QT_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONODLL_CXXFLAGS) $(srcdir)/src/generic/icon.cpp @@ -19922,12 +19921,6 @@ monodll_sound_sdl.o: $(srcdir)/src/unix/sound_sdl.cpp $(MONODLL_ODEP) @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@monodll_carbon_gdiobj.o: $(srcdir)/src/osx/carbon/gdiobj.cpp $(MONODLL_ODEP) @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONODLL_CXXFLAGS) $(srcdir)/src/osx/carbon/gdiobj.cpp -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@monodll_carbon_icon.o: $(srcdir)/src/osx/carbon/icon.cpp $(MONODLL_ODEP) -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONODLL_CXXFLAGS) $(srcdir)/src/osx/carbon/icon.cpp - -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@monodll_carbon_icon.o: $(srcdir)/src/osx/carbon/icon.cpp $(MONODLL_ODEP) -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONODLL_CXXFLAGS) $(srcdir)/src/osx/carbon/icon.cpp - @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@monodll_carbon_app.o: $(srcdir)/src/osx/carbon/app.cpp $(MONODLL_ODEP) @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONODLL_CXXFLAGS) $(srcdir)/src/osx/carbon/app.cpp @@ -23222,8 +23215,11 @@ monolib_sound_sdl.o: $(srcdir)/src/unix/sound_sdl.cpp $(MONOLIB_ODEP) @COND_TOOLKIT_MSW_USE_GUI_1@monolib_msw_joystick.o: $(srcdir)/src/msw/joystick.cpp $(MONOLIB_ODEP) @COND_TOOLKIT_MSW_USE_GUI_1@ $(CXXC) -c -o $@ $(MONOLIB_CXXFLAGS) $(srcdir)/src/msw/joystick.cpp -@COND_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@monolib_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(MONOLIB_ODEP) -@COND_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONOLIB_CXXFLAGS) $(srcdir)/src/generic/icon.cpp +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@monolib_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(MONOLIB_ODEP) +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONOLIB_CXXFLAGS) $(srcdir)/src/generic/icon.cpp + +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@monolib_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(MONOLIB_ODEP) +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONOLIB_CXXFLAGS) $(srcdir)/src/generic/icon.cpp @COND_TOOLKIT_QT_USE_GUI_1_WXUNIV_0@monolib_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(MONOLIB_ODEP) @COND_TOOLKIT_QT_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONOLIB_CXXFLAGS) $(srcdir)/src/generic/icon.cpp @@ -25163,12 +25159,6 @@ monolib_sound_sdl.o: $(srcdir)/src/unix/sound_sdl.cpp $(MONOLIB_ODEP) @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@monolib_carbon_gdiobj.o: $(srcdir)/src/osx/carbon/gdiobj.cpp $(MONOLIB_ODEP) @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONOLIB_CXXFLAGS) $(srcdir)/src/osx/carbon/gdiobj.cpp -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@monolib_carbon_icon.o: $(srcdir)/src/osx/carbon/icon.cpp $(MONOLIB_ODEP) -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONOLIB_CXXFLAGS) $(srcdir)/src/osx/carbon/icon.cpp - -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@monolib_carbon_icon.o: $(srcdir)/src/osx/carbon/icon.cpp $(MONOLIB_ODEP) -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONOLIB_CXXFLAGS) $(srcdir)/src/osx/carbon/icon.cpp - @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@monolib_carbon_app.o: $(srcdir)/src/osx/carbon/app.cpp $(MONOLIB_ODEP) @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(MONOLIB_CXXFLAGS) $(srcdir)/src/osx/carbon/app.cpp @@ -28556,8 +28546,11 @@ coredll_sound_sdl.o: $(srcdir)/src/unix/sound_sdl.cpp $(COREDLL_ODEP) @COND_TOOLKIT_MSW_USE_GUI_1@coredll_msw_joystick.o: $(srcdir)/src/msw/joystick.cpp $(COREDLL_ODEP) @COND_TOOLKIT_MSW_USE_GUI_1@ $(CXXC) -c -o $@ $(COREDLL_CXXFLAGS) $(srcdir)/src/msw/joystick.cpp -@COND_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@coredll_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(COREDLL_ODEP) -@COND_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(COREDLL_CXXFLAGS) $(srcdir)/src/generic/icon.cpp +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@coredll_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(COREDLL_ODEP) +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(COREDLL_CXXFLAGS) $(srcdir)/src/generic/icon.cpp + +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@coredll_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(COREDLL_ODEP) +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(COREDLL_CXXFLAGS) $(srcdir)/src/generic/icon.cpp @COND_TOOLKIT_QT_USE_GUI_1_WXUNIV_0@coredll_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(COREDLL_ODEP) @COND_TOOLKIT_QT_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(COREDLL_CXXFLAGS) $(srcdir)/src/generic/icon.cpp @@ -30497,12 +30490,6 @@ coredll_sound_sdl.o: $(srcdir)/src/unix/sound_sdl.cpp $(COREDLL_ODEP) @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@coredll_carbon_gdiobj.o: $(srcdir)/src/osx/carbon/gdiobj.cpp $(COREDLL_ODEP) @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(COREDLL_CXXFLAGS) $(srcdir)/src/osx/carbon/gdiobj.cpp -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@coredll_carbon_icon.o: $(srcdir)/src/osx/carbon/icon.cpp $(COREDLL_ODEP) -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(COREDLL_CXXFLAGS) $(srcdir)/src/osx/carbon/icon.cpp - -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@coredll_carbon_icon.o: $(srcdir)/src/osx/carbon/icon.cpp $(COREDLL_ODEP) -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(COREDLL_CXXFLAGS) $(srcdir)/src/osx/carbon/icon.cpp - @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@coredll_carbon_app.o: $(srcdir)/src/osx/carbon/app.cpp $(COREDLL_ODEP) @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(COREDLL_CXXFLAGS) $(srcdir)/src/osx/carbon/app.cpp @@ -32792,8 +32779,11 @@ corelib_sound_sdl.o: $(srcdir)/src/unix/sound_sdl.cpp $(CORELIB_ODEP) @COND_TOOLKIT_MSW_USE_GUI_1@corelib_msw_joystick.o: $(srcdir)/src/msw/joystick.cpp $(CORELIB_ODEP) @COND_TOOLKIT_MSW_USE_GUI_1@ $(CXXC) -c -o $@ $(CORELIB_CXXFLAGS) $(srcdir)/src/msw/joystick.cpp -@COND_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@corelib_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(CORELIB_ODEP) -@COND_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(CORELIB_CXXFLAGS) $(srcdir)/src/generic/icon.cpp +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@corelib_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(CORELIB_ODEP) +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(CORELIB_CXXFLAGS) $(srcdir)/src/generic/icon.cpp + +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@corelib_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(CORELIB_ODEP) +@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(CORELIB_CXXFLAGS) $(srcdir)/src/generic/icon.cpp @COND_TOOLKIT_QT_USE_GUI_1_WXUNIV_0@corelib_generic_icon.o: $(srcdir)/src/generic/icon.cpp $(CORELIB_ODEP) @COND_TOOLKIT_QT_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(CORELIB_CXXFLAGS) $(srcdir)/src/generic/icon.cpp @@ -34733,12 +34723,6 @@ corelib_sound_sdl.o: $(srcdir)/src/unix/sound_sdl.cpp $(CORELIB_ODEP) @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@corelib_carbon_gdiobj.o: $(srcdir)/src/osx/carbon/gdiobj.cpp $(CORELIB_ODEP) @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(CORELIB_CXXFLAGS) $(srcdir)/src/osx/carbon/gdiobj.cpp -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@corelib_carbon_icon.o: $(srcdir)/src/osx/carbon/icon.cpp $(CORELIB_ODEP) -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(CORELIB_CXXFLAGS) $(srcdir)/src/osx/carbon/icon.cpp - -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@corelib_carbon_icon.o: $(srcdir)/src/osx/carbon/icon.cpp $(CORELIB_ODEP) -@COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_IPHONE_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(CORELIB_CXXFLAGS) $(srcdir)/src/osx/carbon/icon.cpp - @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@corelib_carbon_app.o: $(srcdir)/src/osx/carbon/app.cpp $(CORELIB_ODEP) @COND_PLATFORM_MACOSX_1_TOOLKIT_OSX_COCOA_USE_GUI_1_WXUNIV_0@ $(CXXC) -c -o $@ $(CORELIB_CXXFLAGS) $(srcdir)/src/osx/carbon/app.cpp From 9bfa4c6da33f83788358799a48d967426e2d5e09 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Sat, 22 Sep 2018 12:56:36 +0200 Subject: [PATCH 18/18] further cleanup --- include/wx/osx/bitmap.h | 6 ------ src/osx/core/bitmap.cpp | 48 ++++------------------------------------- 2 files changed, 4 insertions(+), 50 deletions(-) diff --git a/include/wx/osx/bitmap.h b/include/wx/osx/bitmap.h index 364f1de6f8..4252da9f9c 100644 --- a/include/wx/osx/bitmap.h +++ b/include/wx/osx/bitmap.h @@ -83,12 +83,6 @@ protected: virtual bool InitFromMonoBitmap(const wxBitmap& bitmap) wxOVERRIDE; private: -#if !wxOSX_BITMAP_NATIVE_ACCESS - wxMemoryBuffer m_memBuf ; - int m_bytesPerRow ; - int m_width ; - int m_height ; -#endif void DoCreateMaskBitmap(int width, int height, int bytesPerRow = -1); wxCFRef m_maskBitmap ; diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index 85d90e13bd..41162af49d 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -712,10 +712,8 @@ CGImageRef wxBitmapRefData::CreateCGImage() const alphaInfo = kCGImageAlphaFirst; #endif } -#if wxOSX_BITMAP_NATIVE_ACCESS memcpy(membuf.GetWriteBuf(imageSize), GetRawAccess(), imageSize); membuf.UngetWriteBuf(imageSize); -#endif } CGDataProviderRef dataProvider = NULL; @@ -1531,46 +1529,27 @@ wxMask::~wxMask() void wxMask::Init() { -#if !wxOSX_BITMAP_NATIVE_ACCESS - m_width = m_height = m_bytesPerRow = 0 ; -#endif } void *wxMask::GetRawAccess() const { wxCHECK_MSG( m_maskBitmap, NULL , wxT("invalid mask") ) ; -#if !wxOSX_BITMAP_NATIVE_ACCESS - return m_memBuf.GetData() ; -#else return CGBitmapContextGetData(m_maskBitmap); -#endif } int wxMask::GetBytesPerRow() const { -#if !wxOSX_BITMAP_NATIVE_ACCESS - return m_bytesPerRow ; -#else return CGBitmapContextGetBytesPerRow(m_maskBitmap); -#endif } int wxMask::GetWidth() const { -#if !wxOSX_BITMAP_NATIVE_ACCESS - return m_width ; -#else return CGBitmapContextGetWidth(m_maskBitmap); -#endif } int wxMask::GetHeight() const { -#if !wxOSX_BITMAP_NATIVE_ACCESS - return m_height ; -#else return CGBitmapContextGetHeight(m_maskBitmap); -#endif } @@ -1583,20 +1562,9 @@ void wxMask::DoCreateMaskBitmap(int width, int height, int bytesPerRow) if ( bytesPerRow < 0 ) bytesPerRow = GetBestBytesPerRow(width * kMaskBytesPerPixel); -#if !wxOSX_BITMAP_NATIVE_ACCESS - m_width = width; - m_height = height; - m_bytesPerRow = bytesPerRow; - size_t size = m_bytesPerRow * m_height; - unsigned char* data = (unsigned char*)m_memBuf.GetWriteBuf(size); -#else - unsigned char* data = NULL; -#endif - m_maskBitmap = CGBitmapContextCreate(data, width, height, kMaskBytesPerPixel * 8, bytesPerRow, colorspace, + + m_maskBitmap = CGBitmapContextCreate(NULL, width, height, kMaskBytesPerPixel * 8, bytesPerRow, colorspace, kCGImageAlphaNone); -#if !wxOSX_BITMAP_NATIVE_ACCESS - m_memBuf.UngetWriteBuf(size); -#endif wxASSERT_MSG(m_maskBitmap, wxT("Unable to create CGBitmapContext context")); } @@ -1636,16 +1604,12 @@ bool wxMask::Create(const wxMemoryBuffer& data,int width , int height , int byte bool wxMask::InitFromMonoBitmap(const wxBitmap& bitmap) { -#if wxOSX_BITMAP_NATIVE_ACCESS int m_width, m_height, m_bytesPerRow; -#endif m_width = bitmap.GetWidth() ; m_height = bitmap.GetHeight() ; DoCreateMaskBitmap(m_width, m_height); -#if wxOSX_BITMAP_NATIVE_ACCESS m_bytesPerRow = GetBytesPerRow(); -#endif // pixel access needs a non-const bitmap currently wxBitmap bmp(bitmap); @@ -1678,16 +1642,13 @@ bool wxMask::InitFromMonoBitmap(const wxBitmap& bitmap) bool wxMask::InitFromColour(const wxBitmap& bitmap, const wxColour& colour) { -#if wxOSX_BITMAP_NATIVE_ACCESS int m_width, m_height, m_bytesPerRow; -#endif + m_width = bitmap.GetWidth() ; m_height = bitmap.GetHeight() ; DoCreateMaskBitmap(m_width, m_height); -#if wxOSX_BITMAP_NATIVE_ACCESS m_bytesPerRow = GetBytesPerRow(); -#endif // pixel access needs a non-const bitmap currently wxBitmap bmp(bitmap); @@ -1722,12 +1683,11 @@ bool wxMask::InitFromColour(const wxBitmap& bitmap, const wxColour& colour) wxBitmap wxMask::GetBitmap() const { -#if wxOSX_BITMAP_NATIVE_ACCESS int m_width, m_height, m_bytesPerRow; m_width = GetWidth(); m_height = GetHeight(); m_bytesPerRow = GetBytesPerRow(); -#endif + wxBitmap bitmap(m_width, m_height, 32); wxNativePixelData data(bitmap);