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