]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dcclient.cpp
added wxWindow::GetPrev/NextSibling()
[wxWidgets.git] / src / mac / carbon / dcclient.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/mac/carbon/dcclient.cpp
e9576ca5 3// Purpose: wxClientDC 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
e4db172a 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3d1a4878
SC
12#include "wx/wxprec.h"
13
e9576ca5 14#include "wx/dcclient.h"
e4db172a
WS
15
16#ifndef WX_PRECOMP
17 #include "wx/log.h"
cdccdfab 18 #include "wx/window.h"
f38924e8 19 #include "wx/dcmemory.h"
9eddec69 20 #include "wx/settings.h"
1832043f 21 #include "wx/toplevel.h"
18680f86 22 #include "wx/math.h"
b3a44e05 23 #include "wx/region.h"
e4db172a
WS
24#endif
25
8acd14d1 26#include "wx/graphics.h"
c64c9cd3 27#include "wx/rawbmp.h"
76a5e5d2 28#include "wx/mac/private.h"
e9576ca5
SC
29
30//-----------------------------------------------------------------------------
31// constants
32//-----------------------------------------------------------------------------
33
e9576ca5
SC
34//-----------------------------------------------------------------------------
35// wxPaintDC
36//-----------------------------------------------------------------------------
37
e9576ca5
SC
38IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
39IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
40IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
e9576ca5
SC
41
42/*
43 * wxWindowDC
44 */
45
d497dca4 46#include "wx/mac/uma.h"
be346c26
SC
47#include "wx/notebook.h"
48#include "wx/tabctrl.h"
e4db172a 49
be346c26
SC
50
51static wxBrush MacGetBackgroundBrush( wxWindow* window )
52{
53 wxBrush bkdBrush = window->MacGetBackgroundBrush() ;
be346c26 54 return bkdBrush ;
e4db172a 55}
be346c26 56
e4db172a 57wxWindowDC::wxWindowDC()
e9576ca5 58{
1be0560e 59 m_window = NULL ;
1f2b627a 60 m_release = false;
519cb848 61}
e9576ca5 62
e4db172a 63wxWindowDC::wxWindowDC(wxWindow *window)
519cb848 64{
1be0560e 65 m_window = window ;
9f391ae1
SC
66 wxTopLevelWindowMac* rootwindow = window->MacGetTopLevelWindow() ;
67 if (!rootwindow)
68 return;
9b2eab52 69
eb1a7cf9
DS
70 m_ok = true ;
71
4130e414 72 m_window->GetSize( &m_width , &m_height);
a2d6d210
VZ
73 CGContextRef cg = (CGContextRef) window->MacGetCGContextRef();
74 m_release = false;
4130e414 75 if ( cg == NULL )
a2d6d210
VZ
76 {
77 SetGraphicsContext( wxGraphicsContext::Create( window ) ) ;
a2d6d210
VZ
78 }
79 else
80 {
81 CGContextSaveGState( cg );
82 m_release = true ;
8a438f46 83 // make sure the context is having its origin at the wx-window coordinates of the
a2d6d210
VZ
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 }
a2d6d210 90 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
e1673e52 91
489d2dd3 92 SetBackground(MacGetBackgroundBrush(window));
9b2eab52 93
20b69855 94 SetFont( window->GetFont() ) ;
519cb848 95}
e9576ca5 96
2f1ae414 97wxWindowDC::~wxWindowDC()
e9576ca5 98{
35ee7124 99 if ( m_release )
a2d6d210 100 {
35ee7124
SC
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)
a2d6d210
VZ
103 CGContextRef cg = (CGContextRef) m_window->MacGetCGContextRef();
104 CGContextRestoreGState(cg);
a2d6d210 105 }
519cb848 106}
e9576ca5 107
1be0560e
SC
108void wxWindowDC::DoGetSize( int* width, int* height ) const
109{
a2d6d210
VZ
110 if ( width )
111 *width = m_width;
112 if ( height )
113 *height = m_height;
1be0560e
SC
114}
115
b2d123f1 116wxBitmap wxWindowDC::DoGetAsBitmap(const wxRect *subrect) const
c64c9cd3 117{
90b0f5fc
KO
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;
31c1cbc7 122
8a438f46 123 ControlRef handle = (ControlRef) m_window->GetHandle();
c64c9cd3
KO
124 if ( !handle )
125 return wxNullBitmap;
126
8a438f46 127 HIRect rect;
c64c9cd3
KO
128 CGImageRef image;
129 CGContextRef context;
130 void* data;
8a438f46 131
c64c9cd3 132 size_t bytesPerRow;
8a438f46 133
c64c9cd3 134 HIViewCreateOffscreenImage( handle, 0, &rect, &image);
8a438f46
VZ
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
c64c9cd3
KO
140 bytesPerRow = ( ( width * 8 * 4 + 7 ) / 8 );
141
142 data = calloc( 1, bytesPerRow * height );
143 context = CGBitmapContextCreate( data, width, height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst );
8a438f46 144
b2d123f1
SC
145 if ( subrect )
146 rect = CGRectOffset( rect, -subrect->x, -subrect->y ) ;
c64c9cd3
KO
147 CGContextDrawImage( context, rect, image );
148
8a438f46 149 unsigned char* buffer = (unsigned char*) data;
c64c9cd3
KO
150 wxBitmap bmp = wxBitmap(width, height, 32);
151 wxAlphaPixelData pixData(bmp, wxPoint(0,0), wxSize(width, height));
8a438f46 152
c64c9cd3
KO
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++;
8a438f46 162 ++p;
c64c9cd3
KO
163 }
164 p = rowStart;
165 p.OffsetY(pixData, 1);
166 }
8a438f46 167
c64c9cd3
KO
168 return bmp;
169}
170
519cb848
SC
171/*
172 * wxClientDC
173 */
e9576ca5 174
2f1ae414 175wxClientDC::wxClientDC()
e9576ca5 176{
1be0560e 177 m_window = NULL ;
e9576ca5
SC
178}
179
4130e414 180wxClientDC::wxClientDC(wxWindow *window) :
a2d6d210 181 wxWindowDC( window )
4130e414 182{
6239ee05 183 wxCHECK_RET( window, _T("invalid window in wxClientDC") );
4130e414 184 wxPoint origin = window->GetClientAreaOrigin() ;
4130e414 185 m_window->GetClientSize( &m_width , &m_height);
a2d6d210
VZ
186 SetDeviceOrigin( origin.x, origin.y );
187 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
4130e414 188}
e9576ca5 189
2f1ae414 190wxClientDC::~wxClientDC()
e9576ca5 191{
e9576ca5
SC
192}
193
519cb848
SC
194/*
195 * wxPaintDC
196 */
e9576ca5 197
2f1ae414 198wxPaintDC::wxPaintDC()
e9576ca5 199{
1be0560e 200 m_window = NULL ;
e9576ca5
SC
201}
202
4130e414 203wxPaintDC::wxPaintDC(wxWindow *window) :
a2d6d210 204 wxWindowDC( window )
4130e414
SC
205{
206 wxPoint origin = window->GetClientAreaOrigin() ;
4130e414 207 m_window->GetClientSize( &m_width , &m_height);
a2d6d210
VZ
208 SetDeviceOrigin( origin.x, origin.y );
209 SetClippingRegion( 0 , 0 , m_width , m_height ) ;
4130e414 210}
e9576ca5 211
519cb848 212wxPaintDC::~wxPaintDC()
e9576ca5 213{
519cb848 214}