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(void)
35 m_cocoaNSImage = NULL;
39 wxMemoryDC::wxMemoryDC( wxDC *WXUNUSED(dc) )
41 m_cocoaNSImage = NULL;
45 wxMemoryDC::~wxMemoryDC(void)
47 CocoaUnwindStackAndLoseFocus();
48 [m_cocoaNSImage release];
51 bool wxMemoryDC::CocoaLockFocus()
55 [m_cocoaNSImage lockFocus];
56 sm_cocoaDCStack.Insert(this);
57 NSAffineTransform *newTransform = CocoaGetWxToBoundsTransform([m_cocoaNSImage isFlipped], [m_cocoaNSImage size].height);
58 [newTransform retain];
59 [m_cocoaWxToBoundsTransform release];
60 m_cocoaWxToBoundsTransform = newTransform;
61 CocoaApplyTransformations();
67 bool wxMemoryDC::CocoaUnlockFocus()
69 [m_cocoaNSImage unlockFocus];
73 // NOTE: The AppKit is unable to draw onto an NSBitmapImageRep so we must
74 // instead copy the data to an offscreen window, then copy it back
75 void wxMemoryDC::SelectObject( const wxBitmap& bitmap )
77 wxAutoNSAutoreleasePool pool;
78 if(m_selectedBitmap.Ok())
81 wxASSERT(m_cocoaNSImage);
82 m_selectedBitmap.SetNSBitmapImageRep(
83 [[NSBitmapImageRep alloc]
84 initWithFocusedViewRect:NSMakeRect(0.0,0.0,
85 m_selectedBitmap.GetWidth(),
86 m_selectedBitmap.GetHeight())]);
88 CocoaUnwindStackAndLoseFocus();
89 [m_cocoaNSImage release];
91 m_selectedBitmap = bitmap;
92 if(m_selectedBitmap.Ok())
94 // Create an offscreen window of the same size
95 m_cocoaNSImage = [[NSImage alloc]
96 initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(),
97 m_selectedBitmap.GetHeight())];
100 NSImage *nsimage = [m_selectedBitmap.GetNSImage(false) retain];
101 [m_cocoaNSImage lockFocus];
102 [nsimage drawAtPoint: NSMakePoint(0,0)
103 fromRect: NSMakeRect(0.0,0.0,m_selectedBitmap.GetWidth(),m_selectedBitmap.GetHeight())
104 operation: NSCompositeCopy
106 [m_cocoaNSImage unlockFocus];
112 void wxMemoryDC::DoGetSize( int *width, int *height ) const
115 *width = m_selectedBitmap.GetWidth();
117 *height = m_selectedBitmap.GetHeight();
120 bool wxMemoryDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
121 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
122 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
124 if(!m_selectedBitmap.Ok())
127 NSAffineTransform *transform = [NSAffineTransform transform];
128 [transform translateXBy:xdest yBy:ydest];
130 NSAffineTransform *flipTransform = [NSAffineTransform transform];
132 y' = 0x + -1y + window's height
134 NSAffineTransformStruct matrix = {
139 [flipTransform setTransformStruct: matrix];
141 NSGraphicsContext *context = [NSGraphicsContext currentContext];
142 [context saveGraphicsState];
144 [flipTransform concat];
146 wxLogTrace(wxTRACE_COCOA,wxT("[m_cocoaNSImage isFlipped]=%d"), [m_cocoaNSImage isFlipped]);
147 [m_cocoaNSImage drawAtPoint: NSMakePoint(0,0)
148 fromRect: NSMakeRect(xsrc,
149 m_selectedBitmap.GetHeight()-height-ysrc,
151 operation: NSCompositeCopy // FIXME: raster ops
154 [context restoreGraphicsState];
158 bool wxMemoryDC::CocoaGetBounds(void *rectData)
164 NSRect *pRect = (NSRect*)rectData;
165 pRect->origin.x = 0.0;
166 pRect->origin.y = 0.0;
167 pRect->size = [m_cocoaNSImage size];