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>
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC,wxDC)
30 wxMemoryDC::wxMemoryDC(void)
32 m_cocoaNSImage = NULL;
36 wxMemoryDC::wxMemoryDC( wxDC *WXUNUSED(dc) )
38 m_cocoaNSImage = NULL;
42 wxMemoryDC::~wxMemoryDC(void)
44 CocoaUnwindStackAndLoseFocus();
45 [m_cocoaNSImage release];
48 bool wxMemoryDC::CocoaLockFocus()
52 [m_cocoaNSImage lockFocus];
53 sm_cocoaDCStack.Insert(this);
54 m_cocoaFlipped = [m_cocoaNSImage isFlipped];
55 m_cocoaHeight = [m_cocoaNSImage size].height;
56 CocoaApplyTransformations();
62 bool wxMemoryDC::CocoaUnlockFocus()
64 [m_cocoaNSImage unlockFocus];
68 // NOTE: The AppKit is unable to draw onto an NSBitmapImageRep so we must
69 // instead copy the data to an offscreen window, then copy it back
70 void wxMemoryDC::SelectObject( const wxBitmap& bitmap )
72 wxAutoNSAutoreleasePool pool;
73 if(m_selectedBitmap.Ok())
76 wxASSERT(m_cocoaNSImage);
77 m_selectedBitmap.SetNSBitmapImageRep(
78 [[NSBitmapImageRep alloc]
79 initWithFocusedViewRect:NSMakeRect(0.0,0.0,
80 m_selectedBitmap.GetWidth(),
81 m_selectedBitmap.GetHeight())]);
83 CocoaUnwindStackAndLoseFocus();
84 [m_cocoaNSImage release];
86 m_selectedBitmap = bitmap;
87 if(m_selectedBitmap.Ok())
89 // Create an offscreen window of the same size
90 m_cocoaNSImage = [[NSImage alloc]
91 initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(),
92 m_selectedBitmap.GetHeight())];
95 NSImage *nsimage = [m_selectedBitmap.GetNSImage(false) retain];
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 wxLogTrace(wxTRACE_COCOA,wxT("[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];