]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC,wxPaintDC) | |
23 | ||
432efcb0 | 24 | wxMemoryDC::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 | |
38 | wxMemoryDC::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 | 49 | wxMemoryDC::~wxMemoryDC() |
e9576ca5 | 50 | { |
e40298d5 JS |
51 | if ( m_selected.Ok() ) |
52 | { | |
20b69855 SC |
53 | #if wxMAC_USE_CORE_GRAPHICS |
54 | m_selected.EndRawAccess() ; | |
f7862d3e | 55 | CGContextRef bmCtx = (CGContextRef) 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 | |
65 | void 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() ; | |
f7862d3e | 71 | CGContextRef bmCtx = (CGContextRef) 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() ; | |
f7862d3e SC |
92 | m_width = w; |
93 | m_height = h; | |
cefe5886 DS |
94 | |
95 | // TODO: should this be kCGImageAlphaPremultiplied[First,Last] ? | |
f38924e8 | 96 | CGImageAlphaInfo a = kCGImageAlphaNoneSkipFirst ; |
cefe5886 | 97 | |
20b69855 | 98 | CGColorSpaceRef genericColorSpace = wxMacGetGenericRGBColorSpace(); |
cefe5886 | 99 | CGContextRef bmCtx = CGBitmapContextCreate( data , w, h, bitsPerComp , bytesPerPixel * w , genericColorSpace, a ); |
20b69855 SC |
100 | wxASSERT_MSG( bmCtx , wxT("Unable to create bitmap context") ) ; |
101 | ||
cefe5886 | 102 | if ( bmCtx ) |
20b69855 | 103 | { |
f38924e8 WS |
104 | CGContextSetFillColorSpace( bmCtx, genericColorSpace ); |
105 | CGContextSetStrokeColorSpace( bmCtx, genericColorSpace ); | |
cefe5886 | 106 | |
20b69855 SC |
107 | CGContextTranslateCTM( bmCtx , 0 , m_selected.GetHeight() ) ; |
108 | CGContextScaleCTM( bmCtx , 1 , -1 ) ; | |
cefe5886 | 109 | |
f7862d3e | 110 | SetGraphicsContext( wxGraphicsContext::CreateFromNative( bmCtx ) ); |
20b69855 | 111 | m_graphicContext->SetPen( m_pen ) ; |
f38924e8 | 112 | m_graphicContext->SetBrush( m_brush ) ; |
62fa228a | 113 | m_graphicContext->SetFont( m_font ) ; |
20b69855 | 114 | } |
cefe5886 DS |
115 | m_ok = (m_graphicContext != NULL) ; |
116 | ||
20b69855 | 117 | #else |
cefe5886 DS |
118 | m_macPort = m_selected.GetHBITMAP( &m_macMask ) ; |
119 | m_ok = (m_macPort != NULL) ; | |
120 | if (m_ok) | |
e40298d5 | 121 | { |
cefe5886 | 122 | LockPixels( GetGWorldPixMap( (CGrafPtr) m_macPort ) ) ; |
e40298d5 | 123 | SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , 0 , 0 , m_selected.GetWidth() , m_selected.GetHeight() ) ; |
cefe5886 | 124 | CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ; |
e40298d5 | 125 | } |
20b69855 | 126 | #endif |
e40298d5 JS |
127 | } |
128 | else | |
129 | { | |
cefe5886 | 130 | m_ok = false; |
e40298d5 | 131 | } |
3dec57ad | 132 | } |
e9576ca5 | 133 | |
0a67a93b | 134 | void wxMemoryDC::DoGetSize( int *width, int *height ) const |
e9576ca5 | 135 | { |
e40298d5 JS |
136 | if (m_selected.Ok()) |
137 | { | |
cefe5886 DS |
138 | if (width) |
139 | (*width) = m_selected.GetWidth(); | |
140 | if (height) | |
141 | (*height) = m_selected.GetHeight(); | |
e40298d5 JS |
142 | } |
143 | else | |
144 | { | |
cefe5886 DS |
145 | if (width) |
146 | (*width) = 0; | |
147 | if (height) | |
148 | (*height) = 0; | |
e40298d5 | 149 | } |
3dec57ad | 150 | } |