]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/dcmemory.mm
added functions for setting the columns order in wxListCtrl (modified patch 1828074)
[wxWidgets.git] / src / cocoa / dcmemory.mm
CommitLineData
28f3fe51
DE
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
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"
16 #include "wx/dcmemory.h"
17#endif //WX_PRECOMP
28f3fe51 18
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//-----------------------------------------------------------------------------
28// wxMemoryDC
29//-----------------------------------------------------------------------------
30
31IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC,wxDC)
32
64dd1c46 33void wxMemoryDC::Init()
28f3fe51
DE
34{
35 m_cocoaNSImage = NULL;
36 m_ok = false;
37}
38
39wxMemoryDC::wxMemoryDC( wxDC *WXUNUSED(dc) )
40{
64dd1c46 41 Init();
28f3fe51
DE
42}
43
44wxMemoryDC::~wxMemoryDC(void)
45{
46 CocoaUnwindStackAndLoseFocus();
47 [m_cocoaNSImage release];
48}
49
50bool wxMemoryDC::CocoaLockFocus()
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
66bool wxMemoryDC::CocoaUnlockFocus()
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
64dd1c46 74void wxMemoryDC::DoSelect( const wxBitmap& bitmap )
28f3fe51 75{
d1adc257 76 wxAutoNSAutoreleasePool pool;
e3d7f770
DE
77 if(m_selectedBitmap.Ok())
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;
97 if(m_selectedBitmap.Ok())
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
127void wxMemoryDC::DoGetSize( int *width, int *height ) const
128{
129 if(width)
130 *width = m_selectedBitmap.GetWidth();
131 if(height)
132 *height = m_selectedBitmap.GetHeight();
133}
134
2c23fe91
DE
135bool wxMemoryDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
136 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
137 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
138{
139 if(!m_selectedBitmap.Ok())
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
3e21fc05 207bool wxMemoryDC::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