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