]>
Commit | Line | Data |
---|---|---|
28f3fe51 DE |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/cocoa/dcmemory.mm | |
938156b2 | 3 | // Purpose: wxMemoryDCImpl class |
28f3fe51 DE |
4 | // Author: David Elliott |
5 | // Modified by: | |
6 | // Created: 2003/03/16 | |
28f3fe51 | 7 | // Copyright: (c) 2002 David Elliott |
526954c5 | 8 | // Licence: wxWindows licence |
28f3fe51 DE |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
449c5673 | 11 | #include "wx/wxprec.h" |
f38924e8 | 12 | |
449c5673 DE |
13 | #ifndef WX_PRECOMP |
14 | #include "wx/log.h" | |
449c5673 | 15 | #endif //WX_PRECOMP |
28f3fe51 | 16 | |
938156b2 | 17 | #include "wx/cocoa/dcmemory.h" |
d1adc257 DE |
18 | #include "wx/cocoa/autorelease.h" |
19 | ||
28f3fe51 | 20 | #import <AppKit/NSImage.h> |
2c23fe91 DE |
21 | #import <AppKit/NSAffineTransform.h> |
22 | #import <AppKit/NSGraphicsContext.h> | |
bf428459 DE |
23 | #import <AppKit/NSColor.h> |
24 | #import <AppKit/NSBezierPath.h> | |
28f3fe51 DE |
25 | |
26 | //----------------------------------------------------------------------------- | |
938156b2 | 27 | // wxMemoryDCImpl |
28f3fe51 DE |
28 | //----------------------------------------------------------------------------- |
29 | ||
938156b2 | 30 | IMPLEMENT_ABSTRACT_CLASS(wxMemoryDCImpl,wxCocoaDCImpl) |
28f3fe51 | 31 | |
938156b2 | 32 | void wxMemoryDCImpl::Init() |
28f3fe51 DE |
33 | { |
34 | m_cocoaNSImage = NULL; | |
35 | m_ok = false; | |
36 | } | |
37 | ||
938156b2 DE |
38 | wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC *owner, wxDC *WXUNUSED(dc)) |
39 | : wxCocoaDCImpl(owner) | |
28f3fe51 | 40 | { |
64dd1c46 | 41 | Init(); |
28f3fe51 DE |
42 | } |
43 | ||
938156b2 | 44 | wxMemoryDCImpl::~wxMemoryDCImpl(void) |
28f3fe51 DE |
45 | { |
46 | CocoaUnwindStackAndLoseFocus(); | |
47 | [m_cocoaNSImage release]; | |
48 | } | |
49 | ||
938156b2 | 50 | bool wxMemoryDCImpl::CocoaLockFocus() |
28f3fe51 DE |
51 | { |
52 | if(m_cocoaNSImage) | |
53 | { | |
e3d7f770 | 54 | [m_cocoaNSImage lockFocus]; |
28f3fe51 | 55 | sm_cocoaDCStack.Insert(this); |
1a1e9ff1 DE |
56 | NSAffineTransform *newTransform = CocoaGetWxToBoundsTransform([m_cocoaNSImage isFlipped], [m_cocoaNSImage size].height); |
57 | [newTransform retain]; | |
58 | [m_cocoaWxToBoundsTransform release]; | |
59 | m_cocoaWxToBoundsTransform = newTransform; | |
e3d7f770 | 60 | CocoaApplyTransformations(); |
28f3fe51 DE |
61 | return true; |
62 | } | |
63 | return false; | |
64 | } | |
65 | ||
938156b2 | 66 | bool wxMemoryDCImpl::CocoaUnlockFocus() |
28f3fe51 DE |
67 | { |
68 | [m_cocoaNSImage unlockFocus]; | |
69 | return true; | |
70 | } | |
71 | ||
e3d7f770 DE |
72 | // NOTE: The AppKit is unable to draw onto an NSBitmapImageRep so we must |
73 | // instead copy the data to an offscreen window, then copy it back | |
938156b2 | 74 | void wxMemoryDCImpl::DoSelect( const wxBitmap& bitmap ) |
28f3fe51 | 75 | { |
d1adc257 | 76 | wxAutoNSAutoreleasePool pool; |
a1b806b9 | 77 | if(m_selectedBitmap.IsOk()) |
e3d7f770 DE |
78 | { |
79 | CocoaTakeFocus(); | |
80 | wxASSERT(m_cocoaNSImage); | |
50918c65 DE |
81 | // Replace the bitmap's native data with a newly created one based on the |
82 | // NSImage that has been (potentially) drawn upon. Note that this may and | |
83 | // probably will in many cases change the bitmap's format. | |
84 | // There is nothing we can do about this using pure Cocoa code. Even using | |
85 | // CGBitmapContext is not an option because it only supports a limited | |
86 | // number of bitmap formats. Specifically, 24-bpp is not supported. | |
e3d7f770 DE |
87 | m_selectedBitmap.SetNSBitmapImageRep( |
88 | [[NSBitmapImageRep alloc] | |
89 | initWithFocusedViewRect:NSMakeRect(0.0,0.0, | |
90 | m_selectedBitmap.GetWidth(), | |
91 | m_selectedBitmap.GetHeight())]); | |
92 | } | |
28f3fe51 DE |
93 | CocoaUnwindStackAndLoseFocus(); |
94 | [m_cocoaNSImage release]; | |
95 | m_cocoaNSImage = nil; | |
96 | m_selectedBitmap = bitmap; | |
a1b806b9 | 97 | if(m_selectedBitmap.IsOk()) |
28f3fe51 | 98 | { |
e3d7f770 | 99 | // Create an offscreen window of the same size |
28f3fe51 DE |
100 | m_cocoaNSImage = [[NSImage alloc] |
101 | initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(), | |
102 | m_selectedBitmap.GetHeight())]; | |
e3d7f770 DE |
103 | |
104 | // Now copy the data | |
50918c65 DE |
105 | // Pass false to GetNSImage so the mask is not applied as an alpha channel. |
106 | // Cocoa uses premultiplied alpha so applying the mask would cause all | |
107 | // color information masked out to be turned black which is undesirable. | |
108 | // FIXME: Currently, the mask will not be updated if any drawing occurs. | |
109 | // My only suggestion is for wxCocoa users to eschew the mask in favor | |
110 | // of an alpha channel or to recreate the mask after drawing. | |
111 | // The only way to fix this is to draw twice, once as normal and again | |
112 | // onto the mask to update it. That would require overriding every | |
113 | // single drawing primitive (e.g. DoDrawLine, DoDrawRectangle, etc.) | |
114 | // and would be a major undertaking. | |
d23193b2 | 115 | NSImage *nsimage = [m_selectedBitmap.GetNSImage(false) retain]; |
5a5797ca | 116 | [m_cocoaNSImage lockFocus]; |
e3d7f770 DE |
117 | [nsimage drawAtPoint: NSMakePoint(0,0) |
118 | fromRect: NSMakeRect(0.0,0.0,m_selectedBitmap.GetWidth(),m_selectedBitmap.GetHeight()) | |
119 | operation: NSCompositeCopy | |
120 | fraction: 1.0]; | |
5a5797ca | 121 | [m_cocoaNSImage unlockFocus]; |
f38924e8 | 122 | |
e3d7f770 | 123 | [nsimage release]; |
28f3fe51 DE |
124 | } |
125 | } | |
126 | ||
938156b2 | 127 | void wxMemoryDCImpl::DoGetSize( int *width, int *height ) const |
28f3fe51 DE |
128 | { |
129 | if(width) | |
130 | *width = m_selectedBitmap.GetWidth(); | |
131 | if(height) | |
132 | *height = m_selectedBitmap.GetHeight(); | |
133 | } | |
134 | ||
938156b2 | 135 | bool wxMemoryDCImpl::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest, |
2c23fe91 DE |
136 | wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc, |
137 | int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask) | |
138 | { | |
a1b806b9 | 139 | if(!m_selectedBitmap.IsOk()) |
2c23fe91 DE |
140 | return false; |
141 | ||
142 | NSAffineTransform *transform = [NSAffineTransform transform]; | |
143 | [transform translateXBy:xdest yBy:ydest]; | |
144 | ||
145 | NSAffineTransform *flipTransform = [NSAffineTransform transform]; | |
146 | /* x' = 1x + 0y + 0 | |
147 | y' = 0x + -1y + window's height | |
148 | */ | |
149 | NSAffineTransformStruct matrix = { | |
150 | 1, 0 | |
151 | , 0, -1 | |
152 | , 0, height | |
153 | }; | |
154 | [flipTransform setTransformStruct: matrix]; | |
155 | ||
156 | NSGraphicsContext *context = [NSGraphicsContext currentContext]; | |
157 | [context saveGraphicsState]; | |
158 | [transform concat]; | |
159 | [flipTransform concat]; | |
160 | ||
50918c65 DE |
161 | NSImage *sourceImage; |
162 | if(useMask) | |
163 | { | |
164 | sourceImage = [m_cocoaNSImage copy]; | |
165 | // Apply the mask to the copied image | |
166 | NSBitmapImageRep *maskRep = m_selectedBitmap.GetMask()->GetNSBitmapImageRep(); | |
167 | NSImage *maskImage = [[NSImage alloc] initWithSize:[maskRep size]]; | |
168 | [maskImage addRepresentation:maskRep]; | |
169 | [sourceImage lockFocus]; | |
170 | [maskImage compositeToPoint:NSZeroPoint operation:NSCompositeDestinationIn]; | |
171 | [sourceImage unlockFocus]; | |
172 | [maskImage release]; | |
173 | } | |
174 | else | |
175 | { // retain the m_cocoaNSImage so it has the same ownership as the copy done in the other case. | |
176 | sourceImage = [m_cocoaNSImage retain]; | |
177 | } | |
178 | NSCompositingOperation drawingOp; | |
179 | switch(logicalFunc) | |
180 | { | |
181 | case wxCOPY: | |
182 | // Even if not using the mask, the image might have an alpha channel | |
183 | // so always use NSCompositeSourceOver. If the image is fully opaque | |
184 | // it works out the same as NSCompositeCopy. | |
185 | drawingOp = NSCompositeSourceOver; | |
186 | break; | |
187 | // FIXME: implement more raster ops | |
188 | default: | |
189 | wxLogDebug(wxT("wxCocoa does not support blitting with raster operation %d."), logicalFunc); | |
190 | // Just use the default operation. | |
191 | drawingOp = NSCompositeCopy; | |
192 | } | |
193 | ||
48580976 | 194 | wxLogTrace(wxTRACE_COCOA,wxT("[m_cocoaNSImage isFlipped]=%d"), [m_cocoaNSImage isFlipped]); |
50918c65 | 195 | [sourceImage drawAtPoint: NSMakePoint(0,0) |
2c23fe91 DE |
196 | fromRect: NSMakeRect(xsrc, |
197 | m_selectedBitmap.GetHeight()-height-ysrc, | |
198 | width, height) | |
50918c65 | 199 | operation: drawingOp |
2c23fe91 | 200 | fraction: 1.0]; |
f38924e8 | 201 | |
50918c65 | 202 | [sourceImage release]; // It was either retained, copied, or allocated. |
2c23fe91 DE |
203 | [context restoreGraphicsState]; |
204 | return false; | |
205 | } | |
206 | ||
938156b2 | 207 | bool wxMemoryDCImpl::CocoaGetBounds(void *rectData) |
bf428459 | 208 | { |
3e21fc05 DE |
209 | if(!rectData) |
210 | return false; | |
211 | if(!m_cocoaNSImage) | |
212 | return false; | |
213 | NSRect *pRect = (NSRect*)rectData; | |
214 | pRect->origin.x = 0.0; | |
215 | pRect->origin.y = 0.0; | |
216 | pRect->size = [m_cocoaNSImage size]; | |
217 | return true; | |
bf428459 DE |
218 | } |
219 |