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