1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dcmemory.mm
3 // Purpose: wxMemoryDCImpl class
4 // Author: David Elliott
7 // Copyright: (c) 2002 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
17 #include "wx/cocoa/dcmemory.h"
18 #include "wx/cocoa/autorelease.h"
20 #import <AppKit/NSImage.h>
21 #import <AppKit/NSAffineTransform.h>
22 #import <AppKit/NSGraphicsContext.h>
23 #import <AppKit/NSColor.h>
24 #import <AppKit/NSBezierPath.h>
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 IMPLEMENT_ABSTRACT_CLASS(wxMemoryDCImpl,wxCocoaDCImpl)
32 void wxMemoryDCImpl::Init()
34 m_cocoaNSImage = NULL;
38 wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC *owner, wxDC *WXUNUSED(dc))
39 : wxCocoaDCImpl(owner)
44 wxMemoryDCImpl::~wxMemoryDCImpl(void)
46 CocoaUnwindStackAndLoseFocus();
47 [m_cocoaNSImage release];
50 bool wxMemoryDCImpl::CocoaLockFocus()
54 [m_cocoaNSImage lockFocus];
55 sm_cocoaDCStack.Insert(this);
56 NSAffineTransform *newTransform = CocoaGetWxToBoundsTransform([m_cocoaNSImage isFlipped], [m_cocoaNSImage size].height);
57 [newTransform retain];
58 [m_cocoaWxToBoundsTransform release];
59 m_cocoaWxToBoundsTransform = newTransform;
60 CocoaApplyTransformations();
66 bool wxMemoryDCImpl::CocoaUnlockFocus()
68 [m_cocoaNSImage unlockFocus];
72 // NOTE: The AppKit is unable to draw onto an NSBitmapImageRep so we must
73 // instead copy the data to an offscreen window, then copy it back
74 void wxMemoryDCImpl::DoSelect( const wxBitmap& bitmap )
76 wxAutoNSAutoreleasePool pool;
77 if(m_selectedBitmap.IsOk())
80 wxASSERT(m_cocoaNSImage);
81 // Replace the bitmap's native data with a newly created one based on the
82 // NSImage that has been (potentially) drawn upon. Note that this may and
83 // probably will in many cases change the bitmap's format.
84 // There is nothing we can do about this using pure Cocoa code. Even using
85 // CGBitmapContext is not an option because it only supports a limited
86 // number of bitmap formats. Specifically, 24-bpp is not supported.
87 m_selectedBitmap.SetNSBitmapImageRep(
88 [[NSBitmapImageRep alloc]
89 initWithFocusedViewRect:NSMakeRect(0.0,0.0,
90 m_selectedBitmap.GetWidth(),
91 m_selectedBitmap.GetHeight())]);
93 CocoaUnwindStackAndLoseFocus();
94 [m_cocoaNSImage release];
96 m_selectedBitmap = bitmap;
97 if(m_selectedBitmap.IsOk())
99 // Create an offscreen window of the same size
100 m_cocoaNSImage = [[NSImage alloc]
101 initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(),
102 m_selectedBitmap.GetHeight())];
105 // Pass false to GetNSImage so the mask is not applied as an alpha channel.
106 // Cocoa uses premultiplied alpha so applying the mask would cause all
107 // color information masked out to be turned black which is undesirable.
108 // FIXME: Currently, the mask will not be updated if any drawing occurs.
109 // My only suggestion is for wxCocoa users to eschew the mask in favor
110 // of an alpha channel or to recreate the mask after drawing.
111 // The only way to fix this is to draw twice, once as normal and again
112 // onto the mask to update it. That would require overriding every
113 // single drawing primitive (e.g. DoDrawLine, DoDrawRectangle, etc.)
114 // and would be a major undertaking.
115 NSImage *nsimage = [m_selectedBitmap.GetNSImage(false) retain];
116 [m_cocoaNSImage lockFocus];
117 [nsimage drawAtPoint: NSMakePoint(0,0)
118 fromRect: NSMakeRect(0.0,0.0,m_selectedBitmap.GetWidth(),m_selectedBitmap.GetHeight())
119 operation: NSCompositeCopy
121 [m_cocoaNSImage unlockFocus];
127 void wxMemoryDCImpl::DoGetSize( int *width, int *height ) const
130 *width = m_selectedBitmap.GetWidth();
132 *height = m_selectedBitmap.GetHeight();
135 bool wxMemoryDCImpl::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
136 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
137 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
139 if(!m_selectedBitmap.IsOk())
142 NSAffineTransform *transform = [NSAffineTransform transform];
143 [transform translateXBy:xdest yBy:ydest];
145 NSAffineTransform *flipTransform = [NSAffineTransform transform];
147 y' = 0x + -1y + window's height
149 NSAffineTransformStruct matrix = {
154 [flipTransform setTransformStruct: matrix];
156 NSGraphicsContext *context = [NSGraphicsContext currentContext];
157 [context saveGraphicsState];
159 [flipTransform concat];
161 NSImage *sourceImage;
164 sourceImage = [m_cocoaNSImage copy];
165 // Apply the mask to the copied image
166 NSBitmapImageRep *maskRep = m_selectedBitmap.GetMask()->GetNSBitmapImageRep();
167 NSImage *maskImage = [[NSImage alloc] initWithSize:[maskRep size]];
168 [maskImage addRepresentation:maskRep];
169 [sourceImage lockFocus];
170 [maskImage compositeToPoint:NSZeroPoint operation:NSCompositeDestinationIn];
171 [sourceImage unlockFocus];
175 { // retain the m_cocoaNSImage so it has the same ownership as the copy done in the other case.
176 sourceImage = [m_cocoaNSImage retain];
178 NSCompositingOperation drawingOp;
182 // Even if not using the mask, the image might have an alpha channel
183 // so always use NSCompositeSourceOver. If the image is fully opaque
184 // it works out the same as NSCompositeCopy.
185 drawingOp = NSCompositeSourceOver;
187 // FIXME: implement more raster ops
189 wxLogDebug(wxT("wxCocoa does not support blitting with raster operation %d."), logicalFunc);
190 // Just use the default operation.
191 drawingOp = NSCompositeCopy;
194 wxLogTrace(wxTRACE_COCOA,wxT("[m_cocoaNSImage isFlipped]=%d"), [m_cocoaNSImage isFlipped]);
195 [sourceImage drawAtPoint: NSMakePoint(0,0)
196 fromRect: NSMakeRect(xsrc,
197 m_selectedBitmap.GetHeight()-height-ysrc,
202 [sourceImage release]; // It was either retained, copied, or allocated.
203 [context restoreGraphicsState];
207 bool wxMemoryDCImpl::CocoaGetBounds(void *rectData)
213 NSRect *pRect = (NSRect*)rectData;
214 pRect->origin.x = 0.0;
215 pRect->origin.y = 0.0;
216 pRect->size = [m_cocoaNSImage size];