]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcmemory.cpp
Fixup Blit so it can be used with a source that is a wxBufferedDC,
[wxWidgets.git] / src / mac / carbon / dcmemory.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/dcmemory.cpp
3 // Purpose: wxMemoryDC class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/dcmemory.h"
15
16 #include "wx/mac/private.h"
17
18 //-----------------------------------------------------------------------------
19 // wxMemoryDC
20 //-----------------------------------------------------------------------------
21
22 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC,wxPaintDC)
23
24 wxMemoryDC::wxMemoryDC()
25 : m_selected()
26 {
27 m_ok = true;
28 SetBackground(*wxWHITE_BRUSH);
29 SetBrush(*wxWHITE_BRUSH);
30 SetPen(*wxBLACK_PEN);
31 SetFont(*wxNORMAL_FONT);
32 m_ok = false;
33 }
34
35 wxMemoryDC::wxMemoryDC( wxDC *WXUNUSED(dc) )
36 : m_selected()
37 {
38 m_ok = true;
39 SetBackground(*wxWHITE_BRUSH);
40 SetBrush(*wxWHITE_BRUSH);
41 SetPen(*wxBLACK_PEN);
42 SetFont(*wxNORMAL_FONT);
43 m_ok = false;
44 }
45
46 wxMemoryDC::~wxMemoryDC()
47 {
48 if ( m_selected.Ok() )
49 {
50 #if wxMAC_USE_CORE_GRAPHICS
51 m_selected.EndRawAccess() ;
52 CGContextRef bmCtx = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
53 delete m_graphicContext ;
54 m_graphicContext = NULL ;
55 CGContextRelease( bmCtx ) ;
56 #else
57 // TODO: UnlockPixels( GetGWorldPixMap(MAC_WXHBITMAP(m_selected.GetHBITMAP())) );
58 #endif
59 }
60 }
61
62 void wxMemoryDC::SelectObject( const wxBitmap& bitmap )
63 {
64 if ( m_selected.Ok() )
65 {
66 #if wxMAC_USE_CORE_GRAPHICS
67 m_selected.EndRawAccess() ;
68 CGContextRef bmCtx = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
69 delete m_graphicContext ;
70 m_graphicContext = NULL ;
71 CGContextRelease( bmCtx ) ;
72 #else
73 // TODO: UnlockPixels( GetGWorldPixMap(MAC_WXHBITMAP(m_selected.GetHBITMAP())) );
74 #endif
75 }
76
77 m_selected = bitmap;
78 if (m_selected.Ok())
79 {
80 #if wxMAC_USE_CORE_GRAPHICS
81 if ( m_selected.GetDepth() != 1 )
82 m_selected.UseAlpha() ;
83 void * data = m_selected.BeginRawAccess() ;
84
85 int bitsPerComp = 8 ;
86 int bytesPerPixel = 4 ;
87 int w = bitmap.GetWidth() ;
88 int h = bitmap.GetHeight() ;
89
90 // TODO: should this be kCGImageAlphaPremultiplied[First,Last] ?
91 CGImageAlphaInfo a = kCGImageAlphaNoneSkipFirst ;
92
93 CGColorSpaceRef genericColorSpace = wxMacGetGenericRGBColorSpace();
94 CGContextRef bmCtx = CGBitmapContextCreate( data , w, h, bitsPerComp , bytesPerPixel * w , genericColorSpace, a );
95 wxASSERT_MSG( bmCtx , wxT("Unable to create bitmap context") ) ;
96
97 if ( bmCtx )
98 {
99 CGContextSetFillColorSpace( bmCtx, genericColorSpace );
100 CGContextSetStrokeColorSpace( bmCtx, genericColorSpace );
101
102 CGContextTranslateCTM( bmCtx , 0 , m_selected.GetHeight() ) ;
103 CGContextScaleCTM( bmCtx , 1 , -1 ) ;
104
105 m_graphicContext = new wxMacCGContext( bmCtx ) ;
106 m_graphicContext->SetPen( m_pen ) ;
107 m_graphicContext->SetBrush( m_brush ) ;
108 }
109 m_ok = (m_graphicContext != NULL) ;
110
111 #else
112 m_macPort = m_selected.GetHBITMAP( &m_macMask ) ;
113 m_ok = (m_macPort != NULL) ;
114 if (m_ok)
115 {
116 LockPixels( GetGWorldPixMap( (CGrafPtr) m_macPort ) ) ;
117 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , 0 , 0 , m_selected.GetWidth() , m_selected.GetHeight() ) ;
118 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
119 }
120 #endif
121 }
122 else
123 {
124 m_ok = false;
125 }
126 }
127
128 void wxMemoryDC::DoGetSize( int *width, int *height ) const
129 {
130 if (m_selected.Ok())
131 {
132 if (width)
133 (*width) = m_selected.GetWidth();
134 if (height)
135 (*height) = m_selected.GetHeight();
136 }
137 else
138 {
139 if (width)
140 (*width) = 0;
141 if (height)
142 (*height) = 0;
143 }
144 }