wxCocoa: Added preliminary Blit support
[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:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/dcmemory.h"
13 #include "wx/log.h"
14
15 #import <AppKit/NSImage.h>
16 #import <AppKit/NSAffineTransform.h>
17 #import <AppKit/NSGraphicsContext.h>
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     {
47         [m_cocoaNSImage lockFocusOnRepresentation: m_selectedBitmap.GetNSBitmapImageRep()];
48         sm_cocoaDCStack.Insert(this);
49         return true;
50     }
51     return false;
52 }
53
54 bool wxMemoryDC::CocoaUnlockFocus()
55 {
56     [m_cocoaNSImage unlockFocus];
57     return true;
58 }
59
60 void wxMemoryDC::SelectObject( const wxBitmap& bitmap )
61 {
62     CocoaUnwindStackAndLoseFocus();
63     [m_cocoaNSImage release];
64     m_cocoaNSImage = nil;
65     m_selectedBitmap = bitmap;
66     if(m_selectedBitmap.Ok())
67     {
68         m_cocoaNSImage = [[NSImage alloc]
69                 initWithSize:NSMakeSize(m_selectedBitmap.GetWidth(),
70                     m_selectedBitmap.GetHeight())];
71         [m_cocoaNSImage addRepresentation: m_selectedBitmap.GetNSBitmapImageRep()];
72     }
73 }
74
75 void wxMemoryDC::DoGetSize( int *width, int *height ) const
76 {
77     if(width)
78         *width = m_selectedBitmap.GetWidth();
79     if(height)
80         *height = m_selectedBitmap.GetHeight();
81 }
82
83 bool wxMemoryDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
84     wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
85     int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
86 {
87     if(!m_selectedBitmap.Ok())
88         return false;
89
90     NSAffineTransform *transform = [NSAffineTransform transform];
91     [transform translateXBy:xdest yBy:ydest];
92
93     NSAffineTransform *flipTransform = [NSAffineTransform transform];
94     /*  x' = 1x + 0y + 0
95         y' = 0x + -1y + window's height
96     */
97     NSAffineTransformStruct matrix = {
98         1,  0
99     ,   0, -1
100     ,   0, height
101     };
102     [flipTransform setTransformStruct: matrix];
103
104     NSGraphicsContext *context = [NSGraphicsContext currentContext];
105     [context saveGraphicsState];
106     [transform concat];
107     [flipTransform concat];
108
109     wxLogDebug("[m_cocoaNSImage isFlipped]=%d", [m_cocoaNSImage isFlipped]);
110     [m_cocoaNSImage drawAtPoint: NSMakePoint(0,0)
111         fromRect: NSMakeRect(xsrc,
112             m_selectedBitmap.GetHeight()-height-ysrc,
113             width, height)
114         operation: NSCompositeCopy // FIXME: raster ops
115         fraction: 1.0];
116         
117     [context restoreGraphicsState];
118     return false;
119 }
120