]>
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 | |
065e208e | 9 | // Licence: wxWidgets licence |
28f3fe51 DE |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
449c5673 DE |
12 | #include "wx/wxprec.h" |
13 | #ifndef WX_PRECOMP | |
14 | #include "wx/log.h" | |
15 | #include "wx/dcmemory.h" | |
16 | #endif //WX_PRECOMP | |
28f3fe51 | 17 | |
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> | |
28f3fe51 DE |
23 | |
24 | //----------------------------------------------------------------------------- | |
25 | // wxMemoryDC | |
26 | //----------------------------------------------------------------------------- | |
27 | ||
28 | IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC,wxDC) | |
29 | ||
30 | wxMemoryDC::wxMemoryDC(void) | |
31 | { | |
32 | m_cocoaNSImage = NULL; | |
33 | m_ok = false; | |
34 | } | |
35 | ||
36 | wxMemoryDC::wxMemoryDC( wxDC *WXUNUSED(dc) ) | |
37 | { | |
38 | m_cocoaNSImage = NULL; | |
39 | m_ok = false; | |
40 | } | |
41 | ||
42 | wxMemoryDC::~wxMemoryDC(void) | |
43 | { | |
44 | CocoaUnwindStackAndLoseFocus(); | |
45 | [m_cocoaNSImage release]; | |
46 | } | |
47 | ||
48 | bool wxMemoryDC::CocoaLockFocus() | |
49 | { | |
50 | if(m_cocoaNSImage) | |
51 | { | |
e3d7f770 | 52 | [m_cocoaNSImage lockFocus]; |
28f3fe51 | 53 | sm_cocoaDCStack.Insert(this); |
e3d7f770 DE |
54 | m_cocoaFlipped = [m_cocoaNSImage isFlipped]; |
55 | m_cocoaHeight = [m_cocoaNSImage size].height; | |
56 | CocoaApplyTransformations(); | |
28f3fe51 DE |
57 | return true; |
58 | } | |
59 | return false; | |
60 | } | |
61 | ||
62 | bool wxMemoryDC::CocoaUnlockFocus() | |
63 | { | |
64 | [m_cocoaNSImage unlockFocus]; | |
65 | return true; | |
66 | } | |
67 | ||
e3d7f770 DE |
68 | // NOTE: The AppKit is unable to draw onto an NSBitmapImageRep so we must |
69 | // instead copy the data to an offscreen window, then copy it back | |
28f3fe51 DE |
70 | void wxMemoryDC::SelectObject( const wxBitmap& bitmap ) |
71 | { | |
d1adc257 | 72 | wxAutoNSAutoreleasePool pool; |
e3d7f770 DE |
73 | if(m_selectedBitmap.Ok()) |
74 | { | |
75 | CocoaTakeFocus(); | |
76 | wxASSERT(m_cocoaNSImage); | |
77 | m_selectedBitmap.SetNSBitmapImageRep( | |
78 | [[NSBitmapImageRep alloc] | |
79 | initWithFocusedViewRect:NSMakeRect(0.0,0.0, | |
80 | m_selectedBitmap.GetWidth(), | |
81 | m_selectedBitmap.GetHeight())]); | |
82 | } | |
28f3fe51 DE |
83 | CocoaUnwindStackAndLoseFocus(); |
84 | [m_cocoaNSImage release]; | |
85 | m_cocoaNSImage = nil; | |
86 | m_selectedBitmap = bitmap; | |
87 | if(m_selectedBitmap.Ok()) | |
88 | { | |
e3d7f770 | 89 | // Create an offscreen window of the same size |
28f3fe51 DE |
90 | m_cocoaNSImage = [[NSImage alloc] |
91 | initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(), | |
92 | m_selectedBitmap.GetHeight())]; | |
e3d7f770 DE |
93 | |
94 | // Now copy the data | |
d23193b2 | 95 | NSImage *nsimage = [m_selectedBitmap.GetNSImage(false) retain]; |
5a5797ca | 96 | [m_cocoaNSImage lockFocus]; |
e3d7f770 DE |
97 | [nsimage drawAtPoint: NSMakePoint(0,0) |
98 | fromRect: NSMakeRect(0.0,0.0,m_selectedBitmap.GetWidth(),m_selectedBitmap.GetHeight()) | |
99 | operation: NSCompositeCopy | |
100 | fraction: 1.0]; | |
5a5797ca | 101 | [m_cocoaNSImage unlockFocus]; |
e3d7f770 DE |
102 | |
103 | [nsimage release]; | |
28f3fe51 DE |
104 | } |
105 | } | |
106 | ||
107 | void wxMemoryDC::DoGetSize( int *width, int *height ) const | |
108 | { | |
109 | if(width) | |
110 | *width = m_selectedBitmap.GetWidth(); | |
111 | if(height) | |
112 | *height = m_selectedBitmap.GetHeight(); | |
113 | } | |
114 | ||
2c23fe91 DE |
115 | bool wxMemoryDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest, |
116 | wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc, | |
117 | int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask) | |
118 | { | |
119 | if(!m_selectedBitmap.Ok()) | |
120 | return false; | |
121 | ||
122 | NSAffineTransform *transform = [NSAffineTransform transform]; | |
123 | [transform translateXBy:xdest yBy:ydest]; | |
124 | ||
125 | NSAffineTransform *flipTransform = [NSAffineTransform transform]; | |
126 | /* x' = 1x + 0y + 0 | |
127 | y' = 0x + -1y + window's height | |
128 | */ | |
129 | NSAffineTransformStruct matrix = { | |
130 | 1, 0 | |
131 | , 0, -1 | |
132 | , 0, height | |
133 | }; | |
134 | [flipTransform setTransformStruct: matrix]; | |
135 | ||
136 | NSGraphicsContext *context = [NSGraphicsContext currentContext]; | |
137 | [context saveGraphicsState]; | |
138 | [transform concat]; | |
139 | [flipTransform concat]; | |
140 | ||
48580976 | 141 | wxLogTrace(wxTRACE_COCOA,wxT("[m_cocoaNSImage isFlipped]=%d"), [m_cocoaNSImage isFlipped]); |
2c23fe91 DE |
142 | [m_cocoaNSImage drawAtPoint: NSMakePoint(0,0) |
143 | fromRect: NSMakeRect(xsrc, | |
144 | m_selectedBitmap.GetHeight()-height-ysrc, | |
145 | width, height) | |
146 | operation: NSCompositeCopy // FIXME: raster ops | |
147 | fraction: 1.0]; | |
148 | ||
149 | [context restoreGraphicsState]; | |
150 | return false; | |
151 | } | |
152 |