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