]>
Commit | Line | Data |
---|---|---|
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 | ||
31 | IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC,wxDC) | |
32 | ||
64dd1c46 | 33 | void wxMemoryDC::Init() |
28f3fe51 DE |
34 | { |
35 | m_cocoaNSImage = NULL; | |
36 | m_ok = false; | |
37 | } | |
38 | ||
39 | wxMemoryDC::wxMemoryDC( wxDC *WXUNUSED(dc) ) | |
40 | { | |
64dd1c46 | 41 | Init(); |
28f3fe51 DE |
42 | } |
43 | ||
44 | wxMemoryDC::~wxMemoryDC(void) | |
45 | { | |
46 | CocoaUnwindStackAndLoseFocus(); | |
47 | [m_cocoaNSImage release]; | |
48 | } | |
49 | ||
50 | bool 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 | ||
66 | bool 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 | 74 | void 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); | |
81 | m_selectedBitmap.SetNSBitmapImageRep( | |
82 | [[NSBitmapImageRep alloc] | |
83 | initWithFocusedViewRect:NSMakeRect(0.0,0.0, | |
84 | m_selectedBitmap.GetWidth(), | |
85 | m_selectedBitmap.GetHeight())]); | |
86 | } | |
28f3fe51 DE |
87 | CocoaUnwindStackAndLoseFocus(); |
88 | [m_cocoaNSImage release]; | |
89 | m_cocoaNSImage = nil; | |
90 | m_selectedBitmap = bitmap; | |
91 | if(m_selectedBitmap.Ok()) | |
92 | { | |
e3d7f770 | 93 | // Create an offscreen window of the same size |
28f3fe51 DE |
94 | m_cocoaNSImage = [[NSImage alloc] |
95 | initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(), | |
96 | m_selectedBitmap.GetHeight())]; | |
e3d7f770 DE |
97 | |
98 | // Now copy the data | |
d23193b2 | 99 | NSImage *nsimage = [m_selectedBitmap.GetNSImage(false) retain]; |
5a5797ca | 100 | [m_cocoaNSImage lockFocus]; |
e3d7f770 DE |
101 | [nsimage drawAtPoint: NSMakePoint(0,0) |
102 | fromRect: NSMakeRect(0.0,0.0,m_selectedBitmap.GetWidth(),m_selectedBitmap.GetHeight()) | |
103 | operation: NSCompositeCopy | |
104 | fraction: 1.0]; | |
5a5797ca | 105 | [m_cocoaNSImage unlockFocus]; |
f38924e8 | 106 | |
e3d7f770 | 107 | [nsimage release]; |
28f3fe51 DE |
108 | } |
109 | } | |
110 | ||
111 | void wxMemoryDC::DoGetSize( int *width, int *height ) const | |
112 | { | |
113 | if(width) | |
114 | *width = m_selectedBitmap.GetWidth(); | |
115 | if(height) | |
116 | *height = m_selectedBitmap.GetHeight(); | |
117 | } | |
118 | ||
2c23fe91 DE |
119 | bool wxMemoryDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest, |
120 | wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc, | |
121 | int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask) | |
122 | { | |
123 | if(!m_selectedBitmap.Ok()) | |
124 | return false; | |
125 | ||
126 | NSAffineTransform *transform = [NSAffineTransform transform]; | |
127 | [transform translateXBy:xdest yBy:ydest]; | |
128 | ||
129 | NSAffineTransform *flipTransform = [NSAffineTransform transform]; | |
130 | /* x' = 1x + 0y + 0 | |
131 | y' = 0x + -1y + window's height | |
132 | */ | |
133 | NSAffineTransformStruct matrix = { | |
134 | 1, 0 | |
135 | , 0, -1 | |
136 | , 0, height | |
137 | }; | |
138 | [flipTransform setTransformStruct: matrix]; | |
139 | ||
140 | NSGraphicsContext *context = [NSGraphicsContext currentContext]; | |
141 | [context saveGraphicsState]; | |
142 | [transform concat]; | |
143 | [flipTransform concat]; | |
144 | ||
48580976 | 145 | wxLogTrace(wxTRACE_COCOA,wxT("[m_cocoaNSImage isFlipped]=%d"), [m_cocoaNSImage isFlipped]); |
2c23fe91 DE |
146 | [m_cocoaNSImage drawAtPoint: NSMakePoint(0,0) |
147 | fromRect: NSMakeRect(xsrc, | |
148 | m_selectedBitmap.GetHeight()-height-ysrc, | |
149 | width, height) | |
150 | operation: NSCompositeCopy // FIXME: raster ops | |
151 | fraction: 1.0]; | |
f38924e8 | 152 | |
2c23fe91 DE |
153 | [context restoreGraphicsState]; |
154 | return false; | |
155 | } | |
156 | ||
3e21fc05 | 157 | bool wxMemoryDC::CocoaGetBounds(void *rectData) |
bf428459 | 158 | { |
3e21fc05 DE |
159 | if(!rectData) |
160 | return false; | |
161 | if(!m_cocoaNSImage) | |
162 | return false; | |
163 | NSRect *pRect = (NSRect*)rectData; | |
164 | pRect->origin.x = 0.0; | |
165 | pRect->origin.y = 0.0; | |
166 | pRect->size = [m_cocoaNSImage size]; | |
167 | return true; | |
bf428459 DE |
168 | } |
169 |