]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcmemory.cpp
wxMemoryDC constructor now optionally accepts a wxBitmap parameter,
[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( const wxBitmap& bitmap )
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 if ( bitmap.IsOk() )
35 SelectObject(bitmap);
36 }
37
38 wxMemoryDC::wxMemoryDC( wxDC *WXUNUSED(dc) )
39 : m_selected()
40 {
41 m_ok = true;
42 SetBackground(*wxWHITE_BRUSH);
43 SetBrush(*wxWHITE_BRUSH);
44 SetPen(*wxBLACK_PEN);
45 SetFont(*wxNORMAL_FONT);
46 m_ok = false;
47 }
48
49 wxMemoryDC::~wxMemoryDC()
50 {
51 if ( m_selected.Ok() )
52 {
53 #if wxMAC_USE_CORE_GRAPHICS
54 m_selected.EndRawAccess() ;
55 CGContextRef bmCtx = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
56 delete m_graphicContext ;
57 m_graphicContext = NULL ;
58 CGContextRelease( bmCtx ) ;
59 #else
60 // TODO: UnlockPixels( GetGWorldPixMap(MAC_WXHBITMAP(m_selected.GetHBITMAP())) );
61 #endif
62 }
63 }
64
65 void wxMemoryDC::SelectObject( const wxBitmap& bitmap )
66 {
67 if ( m_selected.Ok() )
68 {
69 #if wxMAC_USE_CORE_GRAPHICS
70 m_selected.EndRawAccess() ;
71 CGContextRef bmCtx = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
72 delete m_graphicContext ;
73 m_graphicContext = NULL ;
74 CGContextRelease( bmCtx ) ;
75 #else
76 // TODO: UnlockPixels( GetGWorldPixMap(MAC_WXHBITMAP(m_selected.GetHBITMAP())) );
77 #endif
78 }
79
80 m_selected = bitmap;
81 if (m_selected.Ok())
82 {
83 #if wxMAC_USE_CORE_GRAPHICS
84 if ( m_selected.GetDepth() != 1 )
85 m_selected.UseAlpha() ;
86 void * data = m_selected.BeginRawAccess() ;
87
88 int bitsPerComp = 8 ;
89 int bytesPerPixel = 4 ;
90 int w = bitmap.GetWidth() ;
91 int h = bitmap.GetHeight() ;
92
93 // TODO: should this be kCGImageAlphaPremultiplied[First,Last] ?
94 CGImageAlphaInfo a = kCGImageAlphaNoneSkipFirst ;
95
96 CGColorSpaceRef genericColorSpace = wxMacGetGenericRGBColorSpace();
97 CGContextRef bmCtx = CGBitmapContextCreate( data , w, h, bitsPerComp , bytesPerPixel * w , genericColorSpace, a );
98 wxASSERT_MSG( bmCtx , wxT("Unable to create bitmap context") ) ;
99
100 if ( bmCtx )
101 {
102 CGContextSetFillColorSpace( bmCtx, genericColorSpace );
103 CGContextSetStrokeColorSpace( bmCtx, genericColorSpace );
104
105 CGContextTranslateCTM( bmCtx , 0 , m_selected.GetHeight() ) ;
106 CGContextScaleCTM( bmCtx , 1 , -1 ) ;
107
108 m_graphicContext = new wxMacCGContext( bmCtx ) ;
109 m_graphicContext->SetPen( m_pen ) ;
110 m_graphicContext->SetBrush( m_brush ) ;
111 m_graphicContext->SetFont( m_font ) ;
112 }
113 m_ok = (m_graphicContext != NULL) ;
114
115 #else
116 m_macPort = m_selected.GetHBITMAP( &m_macMask ) ;
117 m_ok = (m_macPort != NULL) ;
118 if (m_ok)
119 {
120 LockPixels( GetGWorldPixMap( (CGrafPtr) m_macPort ) ) ;
121 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , 0 , 0 , m_selected.GetWidth() , m_selected.GetHeight() ) ;
122 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
123 }
124 #endif
125 }
126 else
127 {
128 m_ok = false;
129 }
130 }
131
132 void wxMemoryDC::DoGetSize( int *width, int *height ) const
133 {
134 if (m_selected.Ok())
135 {
136 if (width)
137 (*width) = m_selected.GetWidth();
138 if (height)
139 (*height) = m_selected.GetHeight();
140 }
141 else
142 {
143 if (width)
144 (*width) = 0;
145 if (height)
146 (*height) = 0;
147 }
148 }