]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dcclient.cpp
fix also wxPen to use wxPenStyle,wxPenJoin,wxPenCap enums instead of plain int; remov...
[wxWidgets.git] / src / mac / carbon / dcclient.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/mac/carbon/dcclient.cpp
888dde65 3// Purpose: wxClientDCImpl 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//-----------------------------------------------------------------------------
888dde65 31// wxWindowDCImpl
e9576ca5
SC
32//-----------------------------------------------------------------------------
33
888dde65 34IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl, wxGCDCImpl)
e9576ca5 35
888dde65
RR
36wxWindowDCImpl::wxWindowDCImpl( wxDC *owner )
37 : wxGCDCImpl( owner )
e9576ca5 38{
1f2b627a 39 m_release = false;
519cb848 40}
e9576ca5 41
888dde65
RR
42wxWindowDCImpl::wxWindowDCImpl( wxDC *owner, wxWindow *window )
43 : wxGCDCImpl( owner )
519cb848 44{
888dde65
RR
45 m_window = window;
46
508b1081 47 WindowRef rootwindow = (WindowRef) window->MacGetTopLevelWindowRef() ;
9f391ae1
SC
48 if (!rootwindow)
49 return;
9b2eab52 50
eb1a7cf9
DS
51 m_ok = true ;
52
4130e414 53 m_window->GetSize( &m_width , &m_height);
a2d6d210
VZ
54 CGContextRef cg = (CGContextRef) window->MacGetCGContextRef();
55 m_release = false;
4130e414 56 if ( cg == NULL )
a2d6d210
VZ
57 {
58 SetGraphicsContext( wxGraphicsContext::Create( window ) ) ;
a2d6d210
VZ
59 }
60 else
61 {
62 CGContextSaveGState( cg );
63 m_release = true ;
8a438f46 64 // make sure the context is having its origin at the wx-window coordinates of the
a2d6d210
VZ
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 }
888dde65 71 DoSetClippingRegion( 0 , 0 , m_width , m_height ) ;
e1673e52 72
a01d9a25 73 SetBackground(wxBrush(window->GetBackgroundColour(),wxSOLID));
9b2eab52 74
20b69855 75 SetFont( window->GetFont() ) ;
519cb848 76}
e9576ca5 77
888dde65 78wxWindowDCImpl::~wxWindowDCImpl()
e9576ca5 79{
35ee7124 80 if ( m_release )
a2d6d210 81 {
35ee7124
SC
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)
a2d6d210
VZ
84 CGContextRef cg = (CGContextRef) m_window->MacGetCGContextRef();
85 CGContextRestoreGState(cg);
a2d6d210 86 }
519cb848 87}
e9576ca5 88
888dde65 89void wxWindowDCImpl::DoGetSize( int* width, int* height ) const
1be0560e 90{
a2d6d210
VZ
91 if ( width )
92 *width = m_width;
93 if ( height )
94 *height = m_height;
1be0560e
SC
95}
96
888dde65 97wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
c64c9cd3 98{
90b0f5fc
KO
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;
31c1cbc7 103
1bd568fa
SC
104#ifdef __LP64__
105 return wxNullBitmap;
106#else
8a438f46 107 ControlRef handle = (ControlRef) m_window->GetHandle();
c64c9cd3
KO
108 if ( !handle )
109 return wxNullBitmap;
1bd568fa 110
8a438f46 111 HIRect rect;
c64c9cd3
KO
112 CGImageRef image;
113 CGContextRef context;
114 void* data;
8a438f46 115
c64c9cd3 116 size_t bytesPerRow;
8a438f46 117
c64c9cd3 118 HIViewCreateOffscreenImage( handle, 0, &rect, &image);
8a438f46
VZ
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
c64c9cd3
KO
124 bytesPerRow = ( ( width * 8 * 4 + 7 ) / 8 );
125
126 data = calloc( 1, bytesPerRow * height );
127 context = CGBitmapContextCreate( data, width, height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst );
8a438f46 128
b2d123f1
SC
129 if ( subrect )
130 rect = CGRectOffset( rect, -subrect->x, -subrect->y ) ;
c64c9cd3
KO
131 CGContextDrawImage( context, rect, image );
132
8a438f46 133 unsigned char* buffer = (unsigned char*) data;
c64c9cd3
KO
134 wxBitmap bmp = wxBitmap(width, height, 32);
135 wxAlphaPixelData pixData(bmp, wxPoint(0,0), wxSize(width, height));
8a438f46 136
c64c9cd3
KO
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++;
8a438f46 146 ++p;
c64c9cd3
KO
147 }
148 p = rowStart;
149 p.OffsetY(pixData, 1);
150 }
8a438f46 151
c64c9cd3 152 return bmp;
1bd568fa 153#endif
c64c9cd3
KO
154}
155
519cb848 156/*
888dde65 157 * wxClientDCImpl
519cb848 158 */
e9576ca5 159
888dde65
RR
160IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl)
161
162wxClientDCImpl::wxClientDCImpl( wxDC *owner )
163 : wxWindowDCImpl( owner )
e9576ca5 164{
e9576ca5
SC
165}
166
888dde65
RR
167wxClientDCImpl::wxClientDCImpl( wxDC *owner, wxWindow *window ) :
168 wxWindowDCImpl( owner, window )
4130e414 169{
888dde65 170 wxCHECK_RET( window, _T("invalid window in wxClientDCImpl") );
4130e414 171 wxPoint origin = window->GetClientAreaOrigin() ;
4130e414 172 m_window->GetClientSize( &m_width , &m_height);
a2d6d210 173 SetDeviceOrigin( origin.x, origin.y );
888dde65 174 DoSetClippingRegion( 0 , 0 , m_width , m_height ) ;
4130e414 175}
e9576ca5 176
888dde65 177wxClientDCImpl::~wxClientDCImpl()
e9576ca5 178{
e9576ca5
SC
179}
180
519cb848 181/*
888dde65 182 * wxPaintDCImpl
519cb848 183 */
e9576ca5 184
888dde65
RR
185IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxWindowDCImpl)
186
187wxPaintDCImpl::wxPaintDCImpl( wxDC *owner )
188 : wxWindowDCImpl( owner )
e9576ca5 189{
e9576ca5
SC
190}
191
888dde65
RR
192wxPaintDCImpl::wxPaintDCImpl( wxDC *owner, wxWindow *window ) :
193 wxWindowDCImpl( owner, window )
4130e414
SC
194{
195 wxPoint origin = window->GetClientAreaOrigin() ;
4130e414 196 m_window->GetClientSize( &m_width , &m_height);
a2d6d210 197 SetDeviceOrigin( origin.x, origin.y );
888dde65 198 DoSetClippingRegion( 0 , 0 , m_width , m_height ) ;
4130e414 199}
e9576ca5 200
888dde65 201wxPaintDCImpl::~wxPaintDCImpl()
e9576ca5 202{
519cb848 203}