]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcmemory.cpp
Added wxMemoryDC::SelectObjectAsSource() and make SelectObject() unshare
[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 m_graphicContext->SetPen( m_pen ) ;
104 m_graphicContext->SetBrush( m_brush ) ;
105 m_graphicContext->SetFont( m_font , m_textForegroundColour) ;
106 }
107 m_ok = (m_graphicContext != NULL) ;
108
109 #else
110 m_macPort = m_selected.GetHBITMAP( &m_macMask ) ;
111 m_ok = (m_macPort != NULL) ;
112 if (m_ok)
113 {
114 LockPixels( GetGWorldPixMap( (CGrafPtr) m_macPort ) ) ;
115 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , 0 , 0 , m_selected.GetWidth() , m_selected.GetHeight() ) ;
116 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
117 }
118 #endif
119 }
120 else
121 {
122 m_ok = false;
123 }
124 }
125
126 void wxMemoryDC::DoGetSize( int *width, int *height ) const
127 {
128 if (m_selected.Ok())
129 {
130 if (width)
131 (*width) = m_selected.GetWidth();
132 if (height)
133 (*height) = m_selected.GetHeight();
134 }
135 else
136 {
137 if (width)
138 (*width) = 0;
139 if (height)
140 (*height) = 0;
141 }
142 }