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