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"
15 #include "wx/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_DYNAMIC_CLASS(wxMemoryDC,wxDC)
32 wxMemoryDC::wxMemoryDC(void)
34 m_cocoaNSImage = NULL;
38 wxMemoryDC::wxMemoryDC( wxDC *WXUNUSED(dc) )
40 m_cocoaNSImage = NULL;
44 wxMemoryDC::~wxMemoryDC(void)
46 CocoaUnwindStackAndLoseFocus();
47 [m_cocoaNSImage release];
50 bool wxMemoryDC::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 wxMemoryDC::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 wxMemoryDC::SelectObject( const wxBitmap& bitmap )
76 wxAutoNSAutoreleasePool pool;
77 if(m_selectedBitmap.Ok())
80 wxASSERT(m_cocoaNSImage);
81 m_selectedBitmap.SetNSBitmapImageRep(
82 [[NSBitmapImageRep alloc]
83 initWithFocusedViewRect:NSMakeRect(0.0,0.0,
84 m_selectedBitmap.GetWidth(),
85 m_selectedBitmap.GetHeight())]);
87 CocoaUnwindStackAndLoseFocus();
88 [m_cocoaNSImage release];
90 m_selectedBitmap = bitmap;
91 if(m_selectedBitmap.Ok())
93 // Create an offscreen window of the same size
94 m_cocoaNSImage = [[NSImage alloc]
95 initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(),
96 m_selectedBitmap.GetHeight())];
99 NSImage *nsimage = [m_selectedBitmap.GetNSImage(false) retain];
100 [m_cocoaNSImage lockFocus];
101 [nsimage drawAtPoint: NSMakePoint(0,0)
102 fromRect: NSMakeRect(0.0,0.0,m_selectedBitmap.GetWidth(),m_selectedBitmap.GetHeight())
103 operation: NSCompositeCopy
105 [m_cocoaNSImage unlockFocus];
111 void wxMemoryDC::DoGetSize( int *width, int *height ) const
114 *width = m_selectedBitmap.GetWidth();
116 *height = m_selectedBitmap.GetHeight();
119 bool wxMemoryDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
120 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
121 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
123 if(!m_selectedBitmap.Ok())
126 NSAffineTransform *transform = [NSAffineTransform transform];
127 [transform translateXBy:xdest yBy:ydest];
129 NSAffineTransform *flipTransform = [NSAffineTransform transform];
131 y' = 0x + -1y + window's height
133 NSAffineTransformStruct matrix = {
138 [flipTransform setTransformStruct: matrix];
140 NSGraphicsContext *context = [NSGraphicsContext currentContext];
141 [context saveGraphicsState];
143 [flipTransform concat];
145 wxLogTrace(wxTRACE_COCOA,wxT("[m_cocoaNSImage isFlipped]=%d"), [m_cocoaNSImage isFlipped]);
146 [m_cocoaNSImage drawAtPoint: NSMakePoint(0,0)
147 fromRect: NSMakeRect(xsrc,
148 m_selectedBitmap.GetHeight()-height-ysrc,
150 operation: NSCompositeCopy // FIXME: raster ops
153 [context restoreGraphicsState];
157 bool wxMemoryDC::CocoaGetBounds(void *rectData)
163 NSRect *pRect = (NSRect*)rectData;
164 pRect->origin.x = 0.0;
165 pRect->origin.y = 0.0;
166 pRect->size = [m_cocoaNSImage size];