]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/dcclient.cpp
avoid setting initial position if it was not specified, broken in r70734
[wxWidgets.git] / src / osx / carbon / dcclient.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/dcclient.cpp
489468fe
SC
3// Purpose: wxClientDCImpl class
4// Author: Stefan Csomor
5// Modified by:
6// Created: 01/02/97
489468fe
SC
7// Copyright: (c) Stefan Csomor
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#include "wx/wxprec.h"
12
13#include "wx/dcclient.h"
14
15#ifndef WX_PRECOMP
16 #include "wx/log.h"
17 #include "wx/window.h"
18 #include "wx/dcmemory.h"
19 #include "wx/settings.h"
20 #include "wx/toplevel.h"
21 #include "wx/math.h"
22 #include "wx/region.h"
23#endif
24
25#include "wx/graphics.h"
26#include "wx/rawbmp.h"
1f0c8f31 27#include "wx/osx/private.h"
b2680ced 28#include "wx/osx/dcclient.h"
489468fe
SC
29
30//-----------------------------------------------------------------------------
31// wxWindowDCImpl
32//-----------------------------------------------------------------------------
33
34IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl, wxGCDCImpl)
35
36wxWindowDCImpl::wxWindowDCImpl( wxDC *owner )
37 : wxGCDCImpl( owner )
38{
39 m_release = false;
40}
41
b2680ced 42
489468fe
SC
43wxWindowDCImpl::wxWindowDCImpl( wxDC *owner, wxWindow *window )
44 : wxGCDCImpl( owner )
45{
46 m_window = window;
03647350 47
489468fe
SC
48 m_ok = true ;
49
50 m_window->GetSize( &m_width , &m_height);
51 if ( !m_window->IsShownOnScreen() )
52 m_width = m_height = 0;
b2680ced 53
489468fe 54 CGContextRef cg = (CGContextRef) window->MacGetCGContextRef();
b2680ced 55
489468fe
SC
56 m_release = false;
57 if ( cg == NULL )
58 {
59 SetGraphicsContext( wxGraphicsContext::Create( window ) ) ;
a6808851 60 m_contentScaleFactor = window->GetContentScaleFactor();
0ffa23cc 61 SetDeviceOrigin(-window->MacGetLeftBorderSize() , -window->MacGetTopBorderSize());
489468fe
SC
62 }
63 else
64 {
a6808851
SC
65 // determine content scale
66 CGRect userrect = CGRectMake(0, 0, 10, 10);
67 CGRect devicerect;
68 devicerect = CGContextConvertRectToDeviceSpace(cg, userrect);
69 m_contentScaleFactor = devicerect.size.height / userrect.size.height;
70
489468fe
SC
71 CGContextSaveGState( cg );
72 m_release = true ;
73 // make sure the context is having its origin at the wx-window coordinates of the
74 // view (read at the top of window.cpp about the differences)
75 if ( window->MacGetLeftBorderSize() != 0 || window->MacGetTopBorderSize() != 0 )
76 CGContextTranslateCTM( cg , -window->MacGetLeftBorderSize() , -window->MacGetTopBorderSize() );
77
058e3f1b
SC
78 wxGraphicsContext* context = wxGraphicsContext::CreateFromNative( cg );
79 context->EnableOffset(true);
80 SetGraphicsContext( context );
489468fe
SC
81 }
82 DoSetClippingRegion( 0 , 0 , m_width , m_height ) ;
83
84 SetBackground(wxBrush(window->GetBackgroundColour(),wxSOLID));
85
86 SetFont( window->GetFont() ) ;
87}
88
89wxWindowDCImpl::~wxWindowDCImpl()
90{
91 if ( m_release )
92 {
93 // this must not necessarily be the current context, we must restore the state of the
94 // cg we started with above (before the CGContextTranslateCTM call)
95 CGContextRef cg = (CGContextRef) m_window->MacGetCGContextRef();
96 CGContextRestoreGState(cg);
97 }
98}
99
100void wxWindowDCImpl::DoGetSize( int* width, int* height ) const
101{
102 if ( width )
103 *width = m_width;
104 if ( height )
105 *height = m_height;
106}
107
928e7a7e 108#if wxOSX_USE_CARBON
489468fe
SC
109wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
110{
111 // wxScreenDC is derived from wxWindowDC, so a screen dc will
112 // call this method when a Blit is performed with it as a source.
113 if (!m_window)
114 return wxNullBitmap;
115
489468fe
SC
116 ControlRef handle = (ControlRef) m_window->GetHandle();
117 if ( !handle )
118 return wxNullBitmap;
03647350 119
489468fe
SC
120 HIRect rect;
121 CGImageRef image;
122 CGContextRef context;
489468fe
SC
123
124 HIViewCreateOffscreenImage( handle, 0, &rect, &image);
125
126
127 int width = subrect != NULL ? subrect->width : (int)rect.size.width;
128 int height = subrect != NULL ? subrect->height : (int)rect.size.height ;
129
130 wxBitmap bmp = wxBitmap(width, height, 32);
03647350 131
489468fe 132 context = (CGContextRef)bmp.GetHBITMAP();
03647350 133
489468fe 134 CGContextSaveGState(context);
03647350 135
489468fe
SC
136 CGContextTranslateCTM( context, 0, height );
137 CGContextScaleCTM( context, 1, -1 );
138
139 if ( subrect )
140 rect = CGRectOffset( rect, -subrect->x, -subrect->y ) ;
141 CGContextDrawImage( context, rect, image );
142
143 CGContextRestoreGState(context);
489468fe 144 return bmp;
524c47aa 145}
928e7a7e 146#endif
489468fe
SC
147
148/*
149 * wxClientDCImpl
150 */
151
152IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl)
153
154wxClientDCImpl::wxClientDCImpl( wxDC *owner )
155 : wxWindowDCImpl( owner )
156{
157}
158
159wxClientDCImpl::wxClientDCImpl( wxDC *owner, wxWindow *window ) :
160 wxWindowDCImpl( owner, window )
161{
9a83f860 162 wxCHECK_RET( window, wxT("invalid window in wxClientDCImpl") );
489468fe
SC
163 wxPoint origin = window->GetClientAreaOrigin() ;
164 m_window->GetClientSize( &m_width , &m_height);
165 if ( !m_window->IsShownOnScreen() )
166 m_width = m_height = 0;
0ffa23cc
SC
167
168 int x0,y0;
169 DoGetDeviceOrigin(&x0,&y0);
170 SetDeviceOrigin( origin.x + x0, origin.y + y0 );
171
489468fe
SC
172 DoSetClippingRegion( 0 , 0 , m_width , m_height ) ;
173}
174
175wxClientDCImpl::~wxClientDCImpl()
176{
72a0d0fc
SC
177 if( GetGraphicsContext() && GetGraphicsContext()->GetNativeContext() )
178 Flush();
489468fe
SC
179}
180
181/*
182 * wxPaintDCImpl
183 */
184
185IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxWindowDCImpl)
186
187wxPaintDCImpl::wxPaintDCImpl( wxDC *owner )
188 : wxWindowDCImpl( owner )
189{
190}
191
192wxPaintDCImpl::wxPaintDCImpl( wxDC *owner, wxWindow *window ) :
193 wxWindowDCImpl( owner, window )
194{
9a83f860 195 wxASSERT_MSG( window->MacGetCGContextRef() != NULL, wxT("using wxPaintDC without being in a native paint event") );
489468fe
SC
196 wxPoint origin = window->GetClientAreaOrigin() ;
197 m_window->GetClientSize( &m_width , &m_height);
198 SetDeviceOrigin( origin.x, origin.y );
199 DoSetClippingRegion( 0 , 0 , m_width , m_height ) ;
200}
201
202wxPaintDCImpl::~wxPaintDCImpl()
203{
204}