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 lockFocusOnRepresentation: m_selectedBitmap.GetNSBitmapImageRep()];
48 sm_cocoaDCStack.Insert(this);
54 bool wxMemoryDC::CocoaUnlockFocus()
56 [m_cocoaNSImage unlockFocus];
60 void wxMemoryDC::SelectObject( const wxBitmap& bitmap )
62 CocoaUnwindStackAndLoseFocus();
63 [m_cocoaNSImage release];
65 m_selectedBitmap = bitmap;
66 if(m_selectedBitmap.Ok())
68 m_cocoaNSImage = [[NSImage alloc]
69 initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(),
70 m_selectedBitmap.GetHeight())];
71 [m_cocoaNSImage addRepresentation: m_selectedBitmap.GetNSBitmapImageRep()];
75 void wxMemoryDC::DoGetSize( int *width, int *height ) const
78 *width = m_selectedBitmap.GetWidth();
80 *height = m_selectedBitmap.GetHeight();
83 bool wxMemoryDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
84 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
85 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
87 if(!m_selectedBitmap.Ok())
90 NSAffineTransform *transform = [NSAffineTransform transform];
91 [transform translateXBy:xdest yBy:ydest];
93 NSAffineTransform *flipTransform = [NSAffineTransform transform];
95 y' = 0x + -1y + window's height
97 NSAffineTransformStruct matrix = {
102 [flipTransform setTransformStruct: matrix];
104 NSGraphicsContext *context = [NSGraphicsContext currentContext];
105 [context saveGraphicsState];
107 [flipTransform concat];
109 wxLogDebug("[m_cocoaNSImage isFlipped]=%d", [m_cocoaNSImage isFlipped]);
110 [m_cocoaNSImage drawAtPoint: NSMakePoint(0,0)
111 fromRect: NSMakeRect(xsrc,
112 m_selectedBitmap.GetHeight()-height-ysrc,
114 operation: NSCompositeCopy // FIXME: raster ops
117 [context restoreGraphicsState];