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 // 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"
27 #include "wx/osx/private.h"
28 #include "wx/osx/dcclient.h"
29 
30 //-----------------------------------------------------------------------------
31 // wxWindowDCImpl
32 //-----------------------------------------------------------------------------
33 
IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl,wxGCDCImpl)34 IMPLEMENT_ABSTRACT_CLASS(wxWindowDCImpl, wxGCDCImpl)
35 
36 wxWindowDCImpl::wxWindowDCImpl( wxDC *owner )
37    : wxGCDCImpl( owner )
38 {
39     m_release = false;
40 }
41 
42 
wxWindowDCImpl(wxDC * owner,wxWindow * window)43 wxWindowDCImpl::wxWindowDCImpl( wxDC *owner, wxWindow *window )
44    : wxGCDCImpl( owner )
45 {
46     m_window = window;
47 
48     m_ok = true ;
49 
50     m_window->GetSize( &m_width , &m_height);
51     if ( !m_window->IsShownOnScreen() )
52         m_width = m_height = 0;
53 
54     CGContextRef cg = (CGContextRef) window->MacGetCGContextRef();
55 
56     m_release = false;
57     if ( cg == NULL )
58     {
59         SetGraphicsContext( wxGraphicsContext::Create( window ) ) ;
60         m_contentScaleFactor = window->GetContentScaleFactor();
61         SetDeviceOrigin(-window->MacGetLeftBorderSize() , -window->MacGetTopBorderSize());
62     }
63     else
64     {
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 
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 
78         wxGraphicsContext* context = wxGraphicsContext::CreateFromNative( cg );
79         context->EnableOffset(true);
80         SetGraphicsContext( context );
81     }
82     DoSetClippingRegion( 0 , 0 , m_width , m_height ) ;
83 
84     SetBackground(wxBrush(window->GetBackgroundColour(),wxSOLID));
85 
86     SetFont( window->GetFont() ) ;
87 }
88 
~wxWindowDCImpl()89 wxWindowDCImpl::~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 
DoGetSize(int * width,int * height) const100 void wxWindowDCImpl::DoGetSize( int* width, int* height ) const
101 {
102     if ( width )
103         *width = m_width;
104     if ( height )
105         *height = m_height;
106 }
107 
108 #if wxOSX_USE_CARBON
DoGetAsBitmap(const wxRect * subrect) const109 wxBitmap 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 
116     ControlRef handle = (ControlRef) m_window->GetHandle();
117     if ( !handle )
118         return wxNullBitmap;
119 
120     HIRect rect;
121     CGImageRef image;
122     CGContextRef context;
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);
131 
132     context = (CGContextRef)bmp.GetHBITMAP();
133 
134     CGContextSaveGState(context);
135 
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);
144     return bmp;
145 }
146 #endif
147 
148 /*
149  * wxClientDCImpl
150  */
151 
IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl,wxWindowDCImpl)152 IMPLEMENT_ABSTRACT_CLASS(wxClientDCImpl, wxWindowDCImpl)
153 
154 wxClientDCImpl::wxClientDCImpl( wxDC *owner )
155  : wxWindowDCImpl( owner )
156 {
157 }
158 
wxClientDCImpl(wxDC * owner,wxWindow * window)159 wxClientDCImpl::wxClientDCImpl( wxDC *owner, wxWindow *window ) :
160     wxWindowDCImpl( owner, window )
161 {
162     wxCHECK_RET( window, wxT("invalid window in wxClientDCImpl") );
163     wxPoint origin = window->GetClientAreaOrigin() ;
164     m_window->GetClientSize( &m_width , &m_height);
165     if ( !m_window->IsShownOnScreen() )
166         m_width = m_height = 0;
167 
168     int x0,y0;
169     DoGetDeviceOrigin(&x0,&y0);
170     SetDeviceOrigin( origin.x + x0, origin.y + y0 );
171 
172     DoSetClippingRegion( 0 , 0 , m_width , m_height ) ;
173 }
174 
~wxClientDCImpl()175 wxClientDCImpl::~wxClientDCImpl()
176 {
177     if( GetGraphicsContext() && GetGraphicsContext()->GetNativeContext() && !m_release )
178         Flush();
179 }
180 
181 /*
182  * wxPaintDCImpl
183  */
184 
IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl,wxWindowDCImpl)185 IMPLEMENT_ABSTRACT_CLASS(wxPaintDCImpl, wxWindowDCImpl)
186 
187 wxPaintDCImpl::wxPaintDCImpl( wxDC *owner )
188  : wxWindowDCImpl( owner )
189 {
190 }
191 
192 #if wxDEBUG_LEVEL
IsGLCanvas(wxWindow * window)193 static bool IsGLCanvas( wxWindow * window )
194 {
195     // If the wx gl library isn't loaded then ciGLCanvas will be NULL.
196     static const wxClassInfo* const ciGLCanvas = wxClassInfo::FindClass("wxGLCanvas");
197     return ciGLCanvas && window->IsKindOf(ciGLCanvas);
198 }
199 #endif
200 
wxPaintDCImpl(wxDC * owner,wxWindow * window)201 wxPaintDCImpl::wxPaintDCImpl( wxDC *owner, wxWindow *window ) :
202     wxWindowDCImpl( owner, window )
203 {
204     // With macOS 10.14, wxGLCanvas windows have a NULL CGContextRef.
205     wxASSERT_MSG( window->MacGetCGContextRef() != NULL || IsGLCanvas(window), wxT("using wxPaintDC without being in a native paint event") );
206     wxPoint origin = window->GetClientAreaOrigin() ;
207     m_window->GetClientSize( &m_width , &m_height);
208     SetDeviceOrigin( origin.x, origin.y );
209     DoSetClippingRegion( 0 , 0 , m_width , m_height ) ;
210 }
211 
~wxPaintDCImpl()212 wxPaintDCImpl::~wxPaintDCImpl()
213 {
214 }
215