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