1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dcmemory.mm
3 // Purpose: wxMemoryDC class
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
15 #include "wx/dcmemory.h"
18 #import <AppKit/NSImage.h>
19 #import <AppKit/NSAffineTransform.h>
20 #import <AppKit/NSGraphicsContext.h>
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC,wxDC)
28 wxMemoryDC::wxMemoryDC(void)
30 m_cocoaNSImage = NULL;
34 wxMemoryDC::wxMemoryDC( wxDC *WXUNUSED(dc) )
36 m_cocoaNSImage = NULL;
40 wxMemoryDC::~wxMemoryDC(void)
42 CocoaUnwindStackAndLoseFocus();
43 [m_cocoaNSImage release];
46 bool wxMemoryDC::CocoaLockFocus()
50 [m_cocoaNSImage lockFocus];
51 sm_cocoaDCStack.Insert(this);
52 m_cocoaFlipped = [m_cocoaNSImage isFlipped];
53 m_cocoaHeight = [m_cocoaNSImage size].height;
54 CocoaApplyTransformations();
60 bool wxMemoryDC::CocoaUnlockFocus()
62 [m_cocoaNSImage unlockFocus];
66 // NOTE: The AppKit is unable to draw onto an NSBitmapImageRep so we must
67 // instead copy the data to an offscreen window, then copy it back
68 void wxMemoryDC::SelectObject( const wxBitmap& bitmap )
70 if(m_selectedBitmap.Ok())
73 wxASSERT(m_cocoaNSImage);
74 m_selectedBitmap.SetNSBitmapImageRep(
75 [[NSBitmapImageRep alloc]
76 initWithFocusedViewRect:NSMakeRect(0.0,0.0,
77 m_selectedBitmap.GetWidth(),
78 m_selectedBitmap.GetHeight())]);
80 CocoaUnwindStackAndLoseFocus();
81 [m_cocoaNSImage release];
83 m_selectedBitmap = bitmap;
84 if(m_selectedBitmap.Ok())
86 // Create an offscreen window of the same size
87 m_cocoaNSImage = [[NSImage alloc]
88 initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(),
89 m_selectedBitmap.GetHeight())];
92 NSImage *nsimage = [[NSImage alloc]
93 initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(),
94 m_selectedBitmap.GetHeight())];
95 [nsimage addRepresentation: const_cast<wxBitmap&>(m_selectedBitmap).GetNSBitmapImageRep()];
96 [m_cocoaNSImage lockFocus];
97 [nsimage drawAtPoint: NSMakePoint(0,0)
98 fromRect: NSMakeRect(0.0,0.0,m_selectedBitmap.GetWidth(),m_selectedBitmap.GetHeight())
99 operation: NSCompositeCopy
101 [m_cocoaNSImage unlockFocus];
107 void wxMemoryDC::DoGetSize( int *width, int *height ) const
110 *width = m_selectedBitmap.GetWidth();
112 *height = m_selectedBitmap.GetHeight();
115 bool wxMemoryDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
116 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
117 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
119 if(!m_selectedBitmap.Ok())
122 NSAffineTransform *transform = [NSAffineTransform transform];
123 [transform translateXBy:xdest yBy:ydest];
125 NSAffineTransform *flipTransform = [NSAffineTransform transform];
127 y' = 0x + -1y + window's height
129 NSAffineTransformStruct matrix = {
134 [flipTransform setTransformStruct: matrix];
136 NSGraphicsContext *context = [NSGraphicsContext currentContext];
137 [context saveGraphicsState];
139 [flipTransform concat];
141 wxLogDebug("[m_cocoaNSImage isFlipped]=%d", [m_cocoaNSImage isFlipped]);
142 [m_cocoaNSImage drawAtPoint: NSMakePoint(0,0)
143 fromRect: NSMakeRect(xsrc,
144 m_selectedBitmap.GetHeight()-height-ysrc,
146 operation: NSCompositeCopy // FIXME: raster ops
149 [context restoreGraphicsState];