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