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/dcmemory.h"
15 #import <AppKit/NSImage.h>
16 #import <AppKit/NSAffineTransform.h>
17 #import <AppKit/NSGraphicsContext.h>
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC,wxDC)
25 wxMemoryDC::wxMemoryDC(void)
27 m_cocoaNSImage = NULL;
31 wxMemoryDC::wxMemoryDC( wxDC *WXUNUSED(dc) )
33 m_cocoaNSImage = NULL;
37 wxMemoryDC::~wxMemoryDC(void)
39 CocoaUnwindStackAndLoseFocus();
40 [m_cocoaNSImage release];
43 bool wxMemoryDC::CocoaLockFocus()
47 [m_cocoaNSImage lockFocus];
48 sm_cocoaDCStack.Insert(this);
49 m_cocoaFlipped = [m_cocoaNSImage isFlipped];
50 m_cocoaHeight = [m_cocoaNSImage size].height;
51 CocoaApplyTransformations();
57 bool wxMemoryDC::CocoaUnlockFocus()
59 [m_cocoaNSImage unlockFocus];
63 // NOTE: The AppKit is unable to draw onto an NSBitmapImageRep so we must
64 // instead copy the data to an offscreen window, then copy it back
65 void wxMemoryDC::SelectObject( const wxBitmap& bitmap )
67 if(m_selectedBitmap.Ok())
70 wxASSERT(m_cocoaNSImage);
71 m_selectedBitmap.SetNSBitmapImageRep(
72 [[NSBitmapImageRep alloc]
73 initWithFocusedViewRect:NSMakeRect(0.0,0.0,
74 m_selectedBitmap.GetWidth(),
75 m_selectedBitmap.GetHeight())]);
77 CocoaUnwindStackAndLoseFocus();
78 [m_cocoaNSImage release];
80 m_selectedBitmap = bitmap;
81 if(m_selectedBitmap.Ok())
83 // Create an offscreen window of the same size
84 m_cocoaNSImage = [[NSImage alloc]
85 initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(),
86 m_selectedBitmap.GetHeight())];
89 NSImage *nsimage = [[NSImage alloc]
90 initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(),
91 m_selectedBitmap.GetHeight())];
92 [nsimage addRepresentation: const_cast<wxBitmap&>(m_selectedBitmap).GetNSBitmapImageRep()];
93 [m_cocoaNSImage lockFocus];
94 [nsimage drawAtPoint: NSMakePoint(0,0)
95 fromRect: NSMakeRect(0.0,0.0,m_selectedBitmap.GetWidth(),m_selectedBitmap.GetHeight())
96 operation: NSCompositeCopy
98 [m_cocoaNSImage unlockFocus];
104 void wxMemoryDC::DoGetSize( int *width, int *height ) const
107 *width = m_selectedBitmap.GetWidth();
109 *height = m_selectedBitmap.GetHeight();
112 bool wxMemoryDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
113 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
114 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
116 if(!m_selectedBitmap.Ok())
119 NSAffineTransform *transform = [NSAffineTransform transform];
120 [transform translateXBy:xdest yBy:ydest];
122 NSAffineTransform *flipTransform = [NSAffineTransform transform];
124 y' = 0x + -1y + window's height
126 NSAffineTransformStruct matrix = {
131 [flipTransform setTransformStruct: matrix];
133 NSGraphicsContext *context = [NSGraphicsContext currentContext];
134 [context saveGraphicsState];
136 [flipTransform concat];
138 wxLogDebug("[m_cocoaNSImage isFlipped]=%d", [m_cocoaNSImage isFlipped]);
139 [m_cocoaNSImage drawAtPoint: NSMakePoint(0,0)
140 fromRect: NSMakeRect(xsrc,
141 m_selectedBitmap.GetHeight()-height-ysrc,
143 operation: NSCompositeCopy // FIXME: raster ops
146 [context restoreGraphicsState];