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