]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dcclient.cpp
cleanup mac
[wxWidgets.git] / src / mac / carbon / dcclient.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/dcclient.cpp
3 // Purpose: wxClientDC 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/dcclient.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/log.h"
18 #include "wx/window.h"
19 #include "wx/dcmemory.h"
20 #include "wx/settings.h"
21 #include "wx/toplevel.h"
22 #include "wx/math.h"
23 #include "wx/region.h"
24 #endif
25
26 #include "wx/graphics.h"
27 #include "wx/rawbmp.h"
28 #include "wx/mac/private.h"
29
30 //-----------------------------------------------------------------------------
31 // constants
32 //-----------------------------------------------------------------------------
33
34 //-----------------------------------------------------------------------------
35 // wxPaintDC
36 //-----------------------------------------------------------------------------
37
38 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
39 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
40 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
41
42 /*
43 * wxWindowDC
44 */
45
46 #include "wx/mac/uma.h"
47 #include "wx/notebook.h"
48 #include "wx/tabctrl.h"
49
50
51 static wxBrush MacGetBackgroundBrush( wxWindow* window )
52 {
53 wxBrush bkdBrush = window->MacGetBackgroundBrush() ;
54 return bkdBrush ;
55 }
56
57 wxWindowDC::wxWindowDC()
58 {
59 m_window = NULL ;
60 m_release = false;
61 }
62
63 wxWindowDC::wxWindowDC(wxWindow *window)
64 {
65 m_window = window ;
66 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
67 if (!rootwindow)
68 return;
69
70 m_ok = true ;
71
72 m_window->GetSize( &m_width , &m_height);
73 CGContextRef cg = (CGContextRef) window->MacGetCGContextRef();
74 m_release = false;
75 if ( cg == NULL )
76 {
77 SetGraphicsContext( wxGraphicsContext::Create( window ) ) ;
78 }
79 else
80 {
81 CGContextSaveGState( cg );
82 m_release = true ;
83 // make sure the context is having its origin at the wx-window coordinates of the
84 // view (read at the top of window.cpp about the differences)
85 if ( window->MacGetLeftBorderSize() != 0 || window->MacGetTopBorderSize() != 0 )
86 CGContextTranslateCTM( cg , -window->MacGetLeftBorderSize() , -window->MacGetTopBorderSize() );
87
88 SetGraphicsContext( wxGraphicsContext::CreateFromNative( cg ) );
89 }
90 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
91
92 SetBackground(MacGetBackgroundBrush(window));
93
94 SetFont( window->GetFont() ) ;
95 }
96
97 wxWindowDC::~wxWindowDC()
98 {
99 if ( m_release )
100 {
101 // this must not necessarily be the current context, we must restore the state of the
102 // cg we started with above (before the CGContextTranslateCTM call)
103 CGContextRef cg = (CGContextRef) m_window->MacGetCGContextRef();
104 CGContextRestoreGState(cg);
105 }
106 }
107
108 void wxWindowDC::DoGetSize( int* width, int* height ) const
109 {
110 if ( width )
111 *width = m_width;
112 if ( height )
113 *height = m_height;
114 }
115
116 wxBitmap wxWindowDC::DoGetAsBitmap(const wxRect *subrect) const
117 {
118 // wxScreenDC is derived from wxWindowDC, so a screen dc will
119 // call this method when a Blit is performed with it as a source.
120 if (!m_window)
121 return wxNullBitmap;
122
123 ControlRef handle = (ControlRef) m_window->GetHandle();
124 if ( !handle )
125 return wxNullBitmap;
126
127 HIRect rect;
128 CGImageRef image;
129 CGContextRef context;
130 void* data;
131
132 size_t bytesPerRow;
133
134 HIViewCreateOffscreenImage( handle, 0, &rect, &image);
135
136
137 int width = subrect != NULL ? subrect->width : (int)rect.size.width;
138 int height = subrect != NULL ? subrect->height : (int)rect.size.height ;
139
140 bytesPerRow = ( ( width * 8 * 4 + 7 ) / 8 );
141
142 data = calloc( 1, bytesPerRow * height );
143 context = CGBitmapContextCreate( data, width, height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst );
144
145 if ( subrect )
146 rect = CGRectOffset( rect, -subrect->x, -subrect->y ) ;
147 CGContextDrawImage( context, rect, image );
148
149 unsigned char* buffer = (unsigned char*) data;
150 wxBitmap bmp = wxBitmap(width, height, 32);
151 wxAlphaPixelData pixData(bmp, wxPoint(0,0), wxSize(width, height));
152
153 wxAlphaPixelData::Iterator p(pixData);
154 for (int y=0; y<height; y++) {
155 wxAlphaPixelData::Iterator rowStart = p;
156 for (int x=0; x<width; x++) {
157 unsigned char a = buffer[3];
158 p.Red() = a; buffer++;
159 p.Green() = a; buffer++;
160 p.Blue() = a; buffer++;
161 p.Alpha() = a; buffer++;
162 ++p;
163 }
164 p = rowStart;
165 p.OffsetY(pixData, 1);
166 }
167
168 return bmp;
169 }
170
171 /*
172 * wxClientDC
173 */
174
175 wxClientDC::wxClientDC()
176 {
177 m_window = NULL ;
178 }
179
180 wxClientDC::wxClientDC(wxWindow *window) :
181 wxWindowDC( window )
182 {
183 wxCHECK_RET( window, _T("invalid window in wxClientDC") );
184 wxPoint origin = window->GetClientAreaOrigin() ;
185 m_window->GetClientSize( &m_width , &m_height);
186 SetDeviceOrigin( origin.x, origin.y );
187 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
188 }
189
190 wxClientDC::~wxClientDC()
191 {
192 }
193
194 /*
195 * wxPaintDC
196 */
197
198 wxPaintDC::wxPaintDC()
199 {
200 m_window = NULL ;
201 }
202
203 wxPaintDC::wxPaintDC(wxWindow *window) :
204 wxWindowDC( window )
205 {
206 wxPoint origin = window->GetClientAreaOrigin() ;
207 m_window->GetClientSize( &m_width , &m_height);
208 SetDeviceOrigin( origin.x, origin.y );
209 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
210 }
211
212 wxPaintDC::~wxPaintDC()
213 {
214 }