1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dcmemory.mm
3 // Purpose: wxMemoryDC class
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/dcmemory.h"
19 #include "wx/cocoa/autorelease.h"
21 #import <AppKit/NSImage.h>
22 #import <AppKit/NSAffineTransform.h>
23 #import <AppKit/NSGraphicsContext.h>
24 #import <AppKit/NSColor.h>
25 #import <AppKit/NSBezierPath.h>
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC,wxDC)
33 wxMemoryDC::wxMemoryDC( const wxBitmap& bitmap )
35 m_cocoaNSImage = NULL;
42 wxMemoryDC::wxMemoryDC( wxDC *WXUNUSED(dc) )
44 m_cocoaNSImage = NULL;
48 wxMemoryDC::~wxMemoryDC(void)
50 CocoaUnwindStackAndLoseFocus();
51 [m_cocoaNSImage release];
54 bool wxMemoryDC::CocoaLockFocus()
58 [m_cocoaNSImage lockFocus];
59 sm_cocoaDCStack.Insert(this);
60 NSAffineTransform *newTransform = CocoaGetWxToBoundsTransform([m_cocoaNSImage isFlipped], [m_cocoaNSImage size].height);
61 [newTransform retain];
62 [m_cocoaWxToBoundsTransform release];
63 m_cocoaWxToBoundsTransform = newTransform;
64 CocoaApplyTransformations();
70 bool wxMemoryDC::CocoaUnlockFocus()
72 [m_cocoaNSImage unlockFocus];
76 // NOTE: The AppKit is unable to draw onto an NSBitmapImageRep so we must
77 // instead copy the data to an offscreen window, then copy it back
78 void wxMemoryDC::SelectObject( const wxBitmap& bitmap )
80 wxAutoNSAutoreleasePool pool;
81 if(m_selectedBitmap.Ok())
84 wxASSERT(m_cocoaNSImage);
85 m_selectedBitmap.SetNSBitmapImageRep(
86 [[NSBitmapImageRep alloc]
87 initWithFocusedViewRect:NSMakeRect(0.0,0.0,
88 m_selectedBitmap.GetWidth(),
89 m_selectedBitmap.GetHeight())]);
91 CocoaUnwindStackAndLoseFocus();
92 [m_cocoaNSImage release];
94 m_selectedBitmap = bitmap;
95 if(m_selectedBitmap.Ok())
97 // Create an offscreen window of the same size
98 m_cocoaNSImage = [[NSImage alloc]
99 initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(),
100 m_selectedBitmap.GetHeight())];
103 NSImage *nsimage = [m_selectedBitmap.GetNSImage(false) retain];
104 [m_cocoaNSImage lockFocus];
105 [nsimage drawAtPoint: NSMakePoint(0,0)
106 fromRect: NSMakeRect(0.0,0.0,m_selectedBitmap.GetWidth(),m_selectedBitmap.GetHeight())
107 operation: NSCompositeCopy
109 [m_cocoaNSImage unlockFocus];
115 void wxMemoryDC::DoGetSize( int *width, int *height ) const
118 *width = m_selectedBitmap.GetWidth();
120 *height = m_selectedBitmap.GetHeight();
123 bool wxMemoryDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
124 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
125 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
127 if(!m_selectedBitmap.Ok())
130 NSAffineTransform *transform = [NSAffineTransform transform];
131 [transform translateXBy:xdest yBy:ydest];
133 NSAffineTransform *flipTransform = [NSAffineTransform transform];
135 y' = 0x + -1y + window's height
137 NSAffineTransformStruct matrix = {
142 [flipTransform setTransformStruct: matrix];
144 NSGraphicsContext *context = [NSGraphicsContext currentContext];
145 [context saveGraphicsState];
147 [flipTransform concat];
149 wxLogTrace(wxTRACE_COCOA,wxT("[m_cocoaNSImage isFlipped]=%d"), [m_cocoaNSImage isFlipped]);
150 [m_cocoaNSImage drawAtPoint: NSMakePoint(0,0)
151 fromRect: NSMakeRect(xsrc,
152 m_selectedBitmap.GetHeight()-height-ysrc,
154 operation: NSCompositeCopy // FIXME: raster ops
157 [context restoreGraphicsState];
161 bool wxMemoryDC::CocoaGetBounds(void *rectData)
167 NSRect *pRect = (NSRect*)rectData;
168 pRect->origin.x = 0.0;
169 pRect->origin.y = 0.0;
170 pRect->size = [m_cocoaNSImage size];