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