]>
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 | ||
432efcb0 | 33 | wxMemoryDC::wxMemoryDC( const wxBitmap& bitmap ) |
28f3fe51 DE |
34 | { |
35 | m_cocoaNSImage = NULL; | |
36 | m_ok = false; | |
432efcb0 RD |
37 | |
38 | if ( bitmap.IsOk() ) | |
39 | SelectObject(bitmap); | |
28f3fe51 DE |
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 | { | |
e3d7f770 | 58 | [m_cocoaNSImage lockFocus]; |
28f3fe51 | 59 | sm_cocoaDCStack.Insert(this); |
1a1e9ff1 DE |
60 | NSAffineTransform *newTransform = CocoaGetWxToBoundsTransform([m_cocoaNSImage isFlipped], [m_cocoaNSImage size].height); |
61 | [newTransform retain]; | |
62 | [m_cocoaWxToBoundsTransform release]; | |
63 | m_cocoaWxToBoundsTransform = newTransform; | |
e3d7f770 | 64 | CocoaApplyTransformations(); |
28f3fe51 DE |
65 | return true; |
66 | } | |
67 | return false; | |
68 | } | |
69 | ||
70 | bool wxMemoryDC::CocoaUnlockFocus() | |
71 | { | |
72 | [m_cocoaNSImage unlockFocus]; | |
73 | return true; | |
74 | } | |
75 | ||
e3d7f770 DE |
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 | |
28f3fe51 DE |
78 | void wxMemoryDC::SelectObject( const wxBitmap& bitmap ) |
79 | { | |
d1adc257 | 80 | wxAutoNSAutoreleasePool pool; |
e3d7f770 DE |
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 | } | |
28f3fe51 DE |
91 | CocoaUnwindStackAndLoseFocus(); |
92 | [m_cocoaNSImage release]; | |
93 | m_cocoaNSImage = nil; | |
94 | m_selectedBitmap = bitmap; | |
95 | if(m_selectedBitmap.Ok()) | |
96 | { | |
e3d7f770 | 97 | // Create an offscreen window of the same size |
28f3fe51 DE |
98 | m_cocoaNSImage = [[NSImage alloc] |
99 | initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(), | |
100 | m_selectedBitmap.GetHeight())]; | |
e3d7f770 DE |
101 | |
102 | // Now copy the data | |
d23193b2 | 103 | NSImage *nsimage = [m_selectedBitmap.GetNSImage(false) retain]; |
5a5797ca | 104 | [m_cocoaNSImage lockFocus]; |
e3d7f770 DE |
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]; | |
5a5797ca | 109 | [m_cocoaNSImage unlockFocus]; |
f38924e8 | 110 | |
e3d7f770 | 111 | [nsimage release]; |
28f3fe51 DE |
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 | ||
2c23fe91 DE |
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 | ||
48580976 | 149 | wxLogTrace(wxTRACE_COCOA,wxT("[m_cocoaNSImage isFlipped]=%d"), [m_cocoaNSImage isFlipped]); |
2c23fe91 DE |
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]; | |
f38924e8 | 156 | |
2c23fe91 DE |
157 | [context restoreGraphicsState]; |
158 | return false; | |
159 | } | |
160 | ||
3e21fc05 | 161 | bool wxMemoryDC::CocoaGetBounds(void *rectData) |
bf428459 | 162 | { |
3e21fc05 DE |
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; | |
bf428459 DE |
172 | } |
173 |