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 m_cocoaFlipped = [m_cocoaNSImage isFlipped];
57 m_cocoaHeight = [m_cocoaNSImage size].height;
58 CocoaApplyTransformations();
64 bool wxMemoryDC::CocoaUnlockFocus()
66 [m_cocoaNSImage unlockFocus];
70 // NOTE: The AppKit is unable to draw onto an NSBitmapImageRep so we must
71 // instead copy the data to an offscreen window, then copy it back
72 void wxMemoryDC::SelectObject( const wxBitmap& bitmap )
74 wxAutoNSAutoreleasePool pool;
75 if(m_selectedBitmap.Ok())
78 wxASSERT(m_cocoaNSImage);
79 m_selectedBitmap.SetNSBitmapImageRep(
80 [[NSBitmapImageRep alloc]
81 initWithFocusedViewRect:NSMakeRect(0.0,0.0,
82 m_selectedBitmap.GetWidth(),
83 m_selectedBitmap.GetHeight())]);
85 CocoaUnwindStackAndLoseFocus();
86 [m_cocoaNSImage release];
88 m_selectedBitmap = bitmap;
89 if(m_selectedBitmap.Ok())
91 // Create an offscreen window of the same size
92 m_cocoaNSImage = [[NSImage alloc]
93 initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(),
94 m_selectedBitmap.GetHeight())];
97 NSImage *nsimage = [m_selectedBitmap.GetNSImage(false) retain];
98 [m_cocoaNSImage lockFocus];
99 [nsimage drawAtPoint: NSMakePoint(0,0)
100 fromRect: NSMakeRect(0.0,0.0,m_selectedBitmap.GetWidth(),m_selectedBitmap.GetHeight())
101 operation: NSCompositeCopy
103 [m_cocoaNSImage unlockFocus];
109 void wxMemoryDC::DoGetSize( int *width, int *height ) const
112 *width = m_selectedBitmap.GetWidth();
114 *height = m_selectedBitmap.GetHeight();
117 bool wxMemoryDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
118 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
119 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
121 if(!m_selectedBitmap.Ok())
124 NSAffineTransform *transform = [NSAffineTransform transform];
125 [transform translateXBy:xdest yBy:ydest];
127 NSAffineTransform *flipTransform = [NSAffineTransform transform];
129 y' = 0x + -1y + window's height
131 NSAffineTransformStruct matrix = {
136 [flipTransform setTransformStruct: matrix];
138 NSGraphicsContext *context = [NSGraphicsContext currentContext];
139 [context saveGraphicsState];
141 [flipTransform concat];
143 wxLogTrace(wxTRACE_COCOA,wxT("[m_cocoaNSImage isFlipped]=%d"), [m_cocoaNSImage isFlipped]);
144 [m_cocoaNSImage drawAtPoint: NSMakePoint(0,0)
145 fromRect: NSMakeRect(xsrc,
146 m_selectedBitmap.GetHeight()-height-ysrc,
148 operation: NSCompositeCopy // FIXME: raster ops
151 [context restoreGraphicsState];
155 void wxMemoryDC::Clear()
157 if(!CocoaTakeFocus()) return;
159 NSGraphicsContext *context = [NSGraphicsContext currentContext];
160 [context saveGraphicsState];
162 [m_backgroundBrush.GetNSColor() set];
166 rect.size = [m_cocoaNSImage size];
167 [NSBezierPath fillRect:rect];
169 [context restoreGraphicsState];