From 2476c5246e53128f8894c7842646f5da70fd7fd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Tue, 14 Oct 2014 08:53:33 +0000 Subject: [PATCH] Add support for template NSImages to wxBitmap wxBitmap uses CGImage instead of NSImage internally and the conversion looses NSImage metadata. In particular, it looses the "template" attribute, which is set for files ending with "Template" and loaded trough wxArtProvider. This change makes it easy to use template images with native controls such as the toolbar. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_3_0_BRANCH@78017 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/osx/cocoa/private.h | 4 ++-- src/osx/carbon/utilscocoa.mm | 9 +++++++-- src/osx/core/bitmap.cpp | 13 +++++++++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index ed731f7e72..24e7c86e5e 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -32,9 +32,9 @@ OSStatus WXDLLIMPEXP_CORE wxMacDrawCGImage( CGContextRef inContext, const CGRect * inBounds, CGImageRef inImage) ; -WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromCGImage( CGImageRef image, double scale = 1.0 ); +WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromCGImage( CGImageRef image, double scale = 1.0, bool isTemplate = false); CGImageRef WXDLLIMPEXP_CORE wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage, double *scale = NULL ); -CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage nsimage); +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/src/osx/carbon/utilscocoa.mm b/src/osx/carbon/utilscocoa.mm index 3dec92f22d..9f63eca74c 100644 --- a/src/osx/carbon/utilscocoa.mm +++ b/src/osx/carbon/utilscocoa.mm @@ -418,7 +418,7 @@ wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &WXUNUSED( } // From "Cocoa Drawing Guide:Working with Images" -WX_NSImage wxOSXGetNSImageFromCGImage( CGImageRef image, double scaleFactor ) +WX_NSImage wxOSXGetNSImageFromCGImage( CGImageRef image, double scaleFactor, bool isTemplate ) { NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0); @@ -435,6 +435,8 @@ WX_NSImage wxOSXGetNSImageFromCGImage( CGImageRef image, double scaleFactor ) CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image ); [newImage unlockFocus]; + [newImage setTemplate:isTemplate]; + /* // Create a bitmap rep from the image... NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage]; @@ -447,7 +449,7 @@ WX_NSImage wxOSXGetNSImageFromCGImage( CGImageRef image, double scaleFactor ) return( newImage ); } -CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage nsimage) +CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage nsimage, bool *isTemplate) { // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html @@ -469,6 +471,9 @@ CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage ns NSRectFill(NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height)); [nsimage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; [NSGraphicsContext setCurrentContext:previousContext]; + + if (isTemplate) + *isTemplate = [nsimage isTemplate]; } return hbitmap; } diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index 3c61c173e7..eff226cd14 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -86,6 +86,9 @@ public: bool HasAlpha() const { return m_hasAlpha; } void UseAlpha( bool useAlpha ); + bool IsTemplate() const { return m_isTemplate; } + void SetTemplate(bool is) { m_isTemplate = is; } + public: #if wxUSE_PALETTE wxPalette m_bitmapPalette; @@ -126,6 +129,7 @@ public: int m_rawAccessCount; bool m_ok; mutable CGImageRef m_cgImageRef; + bool m_isTemplate; #ifndef __WXOSX_IPHONE__ IconRef m_iconRef; @@ -240,6 +244,7 @@ void wxBitmapRefData::Init() m_ok = false ; m_bitmapMask = NULL ; m_cgImageRef = NULL ; + m_isTemplate = false; #ifndef __WXOSX_IPHONE__ m_iconRef = NULL ; @@ -1078,7 +1083,11 @@ wxBitmap::wxBitmap(WX_NSImage image) bool wxBitmap::Create(WX_NSImage image) { - return Create(wxOSXCreateBitmapContextFromNSImage(image)); + bool isTemplate; + if (!Create(wxOSXCreateBitmapContextFromNSImage(image, &isTemplate))) + return false; + M_BITMAPDATA->SetTemplate(isTemplate); + return true; } wxBitmap::wxBitmap(CGContextRef bitmapcontext) @@ -1098,7 +1107,7 @@ bool wxBitmap::Create(CGContextRef bitmapcontext) WX_NSImage wxBitmap::GetNSImage() const { wxCFRef< CGImageRef > cgimage(CreateCGImage()); - return wxOSXGetNSImageFromCGImage( cgimage, GetScaleFactor() ); + return wxOSXGetNSImageFromCGImage( cgimage, GetScaleFactor(), M_BITMAPDATA->IsTemplate() ); } #endif