From de0c7f725d23552d27de54238046274f7625edb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Tue, 14 Oct 2014 08:53:11 +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/trunk@78016 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 7d36a1e6c6..0470d3aa50 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -32,11 +32,11 @@ 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); WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromIconRef( WXHICON iconref ); CGImageRef WXDLLIMPEXP_CORE wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage, double *scale = NULL ); CGImageRef WXDLLIMPEXP_CORE wxOSXGetCGImageFromNSImage( const WX_NSImage nsimage, CGRect* r, CGContextRef cg); -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 b51e6f9283..60c03445e0 100644 --- a/src/osx/carbon/utilscocoa.mm +++ b/src/osx/carbon/utilscocoa.mm @@ -417,7 +417,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); @@ -434,6 +434,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]; @@ -461,7 +463,7 @@ CGImageRef WXDLLIMPEXP_CORE wxOSXGetCGImageFromNSImage( WX_NSImage nsimage, CGRe hints:nil]; } -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 @@ -483,6 +485,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 9870b4485a..351973219f 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -82,6 +82,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; @@ -122,6 +125,7 @@ public: int m_rawAccessCount; bool m_ok; mutable CGImageRef m_cgImageRef; + bool m_isTemplate; #ifndef __WXOSX_IPHONE__ IconRef m_iconRef; @@ -236,6 +240,7 @@ void wxBitmapRefData::Init() m_ok = false ; m_bitmapMask = NULL ; m_cgImageRef = NULL ; + m_isTemplate = false; #ifndef __WXOSX_IPHONE__ m_iconRef = NULL ; @@ -1074,7 +1079,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) @@ -1094,7 +1103,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