adding missing part when using the native paint CGContextRef on osx, see #11853
[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 }
62 else
63 {
64 CGContextSaveGState( cg );
65 m_release = true ;
66 // make sure the context is having its origin at the wx-window coordinates of the
67 // view (read at the top of window.cpp about the differences)
68 if ( window->MacGetLeftBorderSize() != 0 || window->MacGetTopBorderSize() != 0 )
69 CGContextTranslateCTM( cg , -window->MacGetLeftBorderSize() , -window->MacGetTopBorderSize() );
70
71 wxGraphicsContext* context = wxGraphicsContext::CreateFromNative( cg );
72 context->EnableOffset(true);
73 SetGraphicsContext( context );
74 }
75 DoSetClippingRegion( 0 , 0 , m_width , m_height ) ;
76
77 SetBackground(wxBrush(window->GetBackgroundColour(),wxSOLID));
78
79 SetFont( window->GetFont() ) ;
80 }
81
82 wxWindowDCImpl::~wxWindowDCImpl()
83 {
84 if ( m_release )
85 {
86 // this must not necessarily be the current context, we must restore the state of the
87 // cg we started with above (before the CGContextTranslateCTM call)
88 CGContextRef cg = (CGContextRef) m_window->MacGetCGContextRef();
89 CGContextRestoreGState(cg);
90 }
91 }
92
93 void wxWindowDCImpl::DoGetSize( int* width, int* height ) const
94 {
95 if ( width )
96 *width = m_width;
97 if ( height )
98 *height = m_height;
99 }
100
101 #if wxOSX_USE_CARBON
102 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
103 {
104 // wxScreenDC is derived from wxWindowDC, so a screen dc will
105 // call this method when a Blit is performed with it as a source.
106 if (!m_window)
107 return wxNullBitmap;
108
109 ControlRef handle = (ControlRef) m_window->GetHandle();
110 if ( !handle )
111 return wxNullBitmap;
112
113 HIRect rect;
114 CGImageRef image;
115 CGContextRef context;
116
117 HIViewCreateOffscreenImage( handle, 0, &rect, &image);
118
119
120 int width = subrect != NULL ? subrect->width : (int)rect.size.width;
121 int height = subrect != NULL ? subrect->height : (int)rect.size.height ;
122
123 wxBitmap bmp = wxBitmap(width, height, 32);
124
125 context = (CGContextRef)bmp.GetHBITMAP();
126
127 CGContextSaveGState(context);
128
129 CGContextTranslateCTM( context, 0, height );
130 CGContextScaleCTM( context, 1, -1 );
131
132 if ( subrect )
133 rect = CGRectOffset( rect, -subrect->x, -subrect->y ) ;
134 CGContextDrawImage( context, rect, image );
135
136 CGContextRestoreGState(context);
137 return bmp;
138 }
139 #endif
140
141 /*
142 * wxClientDCImpl
143 */
144
145 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl)
146
147 wxClientDCImpl::wxClientDCImpl( wxDC *owner )
148 : wxWindowDCImpl( owner )
149 {
150 }
151
152 wxClientDCImpl::wxClientDCImpl( wxDC *owner, wxWindow *window ) :
153 wxWindowDCImpl( owner, window )
154 {
155 wxCHECK_RET( window, wxT("invalid window in wxClientDCImpl") );
156 wxPoint origin = window->GetClientAreaOrigin() ;
157 m_window->GetClientSize( &m_width , &m_height);
158 if ( !m_window->IsShownOnScreen() )
159 m_width = m_height = 0;
160 SetDeviceOrigin( origin.x, origin.y );
161 DoSetClippingRegion( 0 , 0 , m_width , m_height ) ;
162 }
163
164 wxClientDCImpl::~wxClientDCImpl()
165 {
166 if( GetGraphicsContext() && GetGraphicsContext()->GetNativeContext() )
167 Flush();
168 }
169
170 /*
171 * wxPaintDCImpl
172 */
173
174 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxWindowDCImpl)
175
176 wxPaintDCImpl::wxPaintDCImpl( wxDC *owner )
177 : wxWindowDCImpl( owner )
178 {
179 }
180
181 wxPaintDCImpl::wxPaintDCImpl( wxDC *owner, wxWindow *window ) :
182 wxWindowDCImpl( owner, window )
183 {
184 wxASSERT_MSG( window->MacGetCGContextRef() != NULL, wxT("using wxPaintDC without being in a native paint event") );
185 wxPoint origin = window->GetClientAreaOrigin() ;
186 m_window->GetClientSize( &m_width , &m_height);
187 SetDeviceOrigin( origin.x, origin.y );
188 DoSetClippingRegion( 0 , 0 , m_width , m_height ) ;
189 }
190
191 wxPaintDCImpl::~wxPaintDCImpl()
192 {
193 }