wxMemoryDC constructor now optionally accepts a wxBitmap parameter,
[wxWidgets.git] / src / cocoa / dcmemory.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/dcmemory.mm
3 // Purpose:     wxMemoryDC class
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/03/16
7 // RCS-ID:      $Id$
8 // Copyright:   (c) 2002 David Elliott
9 // Licence:     wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifndef WX_PRECOMP
15     #include "wx/log.h"
16     #include "wx/dcmemory.h"
17 #endif //WX_PRECOMP
18
19 #include "wx/cocoa/autorelease.h"
20
21 #import <AppKit/NSImage.h>
22 #import <AppKit/NSAffineTransform.h>
23 #import <AppKit/NSGraphicsContext.h>
24 #import <AppKit/NSColor.h>
25 #import <AppKit/NSBezierPath.h>
26
27 //-----------------------------------------------------------------------------
28 // wxMemoryDC
29 //-----------------------------------------------------------------------------
30
31 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC,wxDC)
32
33 wxMemoryDC::wxMemoryDC( const wxBitmap& bitmap )
34 {
35     m_cocoaNSImage = NULL;
36     m_ok = false;
37
38     if ( bitmap.IsOk() )
39         SelectObject(bitmap);
40 }
41
42 wxMemoryDC::wxMemoryDC( wxDC *WXUNUSED(dc) )
43 {
44     m_cocoaNSImage = NULL;
45     m_ok = false;
46 }
47
48 wxMemoryDC::~wxMemoryDC(void)
49 {
50     CocoaUnwindStackAndLoseFocus();
51     [m_cocoaNSImage release];
52 }
53
54 bool wxMemoryDC::CocoaLockFocus()
55 {
56     if(m_cocoaNSImage)
57     {
58         [m_cocoaNSImage lockFocus];
59         sm_cocoaDCStack.Insert(this);
60         NSAffineTransform *newTransform = CocoaGetWxToBoundsTransform([m_cocoaNSImage isFlipped], [m_cocoaNSImage size].height);
61         [newTransform retain];
62         [m_cocoaWxToBoundsTransform release];
63         m_cocoaWxToBoundsTransform = newTransform;
64         CocoaApplyTransformations();
65         return true;
66     }
67     return false;
68 }
69
70 bool wxMemoryDC::CocoaUnlockFocus()
71 {
72     [m_cocoaNSImage unlockFocus];
73     return true;
74 }
75
76 // NOTE: The AppKit is unable to draw onto an NSBitmapImageRep so we must
77 // instead copy the data to an offscreen window, then copy it back
78 void wxMemoryDC::SelectObject( const wxBitmap& bitmap )
79 {
80     wxAutoNSAutoreleasePool pool;
81     if(m_selectedBitmap.Ok())
82     {
83         CocoaTakeFocus();
84         wxASSERT(m_cocoaNSImage);
85         m_selectedBitmap.SetNSBitmapImageRep(
86             [[NSBitmapImageRep alloc]
87                 initWithFocusedViewRect:NSMakeRect(0.0,0.0,
88                     m_selectedBitmap.GetWidth(),
89                     m_selectedBitmap.GetHeight())]);
90     }
91     CocoaUnwindStackAndLoseFocus();
92     [m_cocoaNSImage release];
93     m_cocoaNSImage = nil;
94     m_selectedBitmap = bitmap;
95     if(m_selectedBitmap.Ok())
96     {
97         // Create an offscreen window of the same size
98         m_cocoaNSImage = [[NSImage alloc]
99                 initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(),
100                     m_selectedBitmap.GetHeight())];
101
102         // Now copy the data
103         NSImage *nsimage = [m_selectedBitmap.GetNSImage(false) retain];
104         [m_cocoaNSImage lockFocus];
105         [nsimage drawAtPoint: NSMakePoint(0,0)
106             fromRect: NSMakeRect(0.0,0.0,m_selectedBitmap.GetWidth(),m_selectedBitmap.GetHeight())
107             operation: NSCompositeCopy
108             fraction: 1.0];
109         [m_cocoaNSImage unlockFocus];
110
111         [nsimage release];
112     }
113 }
114
115 void wxMemoryDC::DoGetSize( int *width, int *height ) const
116 {
117     if(width)
118         *width = m_selectedBitmap.GetWidth();
119     if(height)
120         *height = m_selectedBitmap.GetHeight();
121 }
122
123 bool wxMemoryDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
124     wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
125     int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
126 {
127     if(!m_selectedBitmap.Ok())
128         return false;
129
130     NSAffineTransform *transform = [NSAffineTransform transform];
131     [transform translateXBy:xdest yBy:ydest];
132
133     NSAffineTransform *flipTransform = [NSAffineTransform transform];
134     /*  x' = 1x + 0y + 0
135         y' = 0x + -1y + window's height
136     */
137     NSAffineTransformStruct matrix = {
138         1,  0
139     ,   0, -1
140     ,   0, height
141     };
142     [flipTransform setTransformStruct: matrix];
143
144     NSGraphicsContext *context = [NSGraphicsContext currentContext];
145     [context saveGraphicsState];
146     [transform concat];
147     [flipTransform concat];
148
149     wxLogTrace(wxTRACE_COCOA,wxT("[m_cocoaNSImage isFlipped]=%d"), [m_cocoaNSImage isFlipped]);
150     [m_cocoaNSImage drawAtPoint: NSMakePoint(0,0)
151         fromRect: NSMakeRect(xsrc,
152             m_selectedBitmap.GetHeight()-height-ysrc,
153             width, height)
154         operation: NSCompositeCopy // FIXME: raster ops
155         fraction: 1.0];
156
157     [context restoreGraphicsState];
158     return false;
159 }
160
161 bool wxMemoryDC::CocoaGetBounds(void *rectData)
162 {
163     if(!rectData)
164         return false;
165     if(!m_cocoaNSImage)
166         return false;
167     NSRect *pRect = (NSRect*)rectData;
168     pRect->origin.x = 0.0;
169     pRect->origin.y = 0.0;
170     pRect->size = [m_cocoaNSImage size];
171     return true;
172 }
173