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