]>
Commit | Line | Data |
---|---|---|
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 | |
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" | |
1f0c8f31 | 28 | #include "wx/osx/private.h" |
b2680ced | 29 | #include "wx/osx/dcclient.h" |
489468fe SC |
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 | ||
b2680ced | 43 | |
489468fe SC |
44 | wxWindowDCImpl::wxWindowDCImpl( wxDC *owner, wxWindow *window ) |
45 | : wxGCDCImpl( owner ) | |
46 | { | |
47 | m_window = window; | |
03647350 | 48 | |
489468fe SC |
49 | m_ok = true ; |
50 | ||
51 | m_window->GetSize( &m_width , &m_height); | |
52 | if ( !m_window->IsShownOnScreen() ) | |
53 | m_width = m_height = 0; | |
b2680ced | 54 | |
489468fe | 55 | CGContextRef cg = (CGContextRef) window->MacGetCGContextRef(); |
b2680ced | 56 | |
489468fe SC |
57 | m_release = false; |
58 | if ( cg == NULL ) | |
59 | { | |
60 | SetGraphicsContext( wxGraphicsContext::Create( window ) ) ; | |
a6808851 | 61 | m_contentScaleFactor = window->GetContentScaleFactor(); |
0ffa23cc | 62 | SetDeviceOrigin(-window->MacGetLeftBorderSize() , -window->MacGetTopBorderSize()); |
489468fe SC |
63 | } |
64 | else | |
65 | { | |
a6808851 SC |
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 | ||
489468fe SC |
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 | ||
058e3f1b SC |
79 | wxGraphicsContext* context = wxGraphicsContext::CreateFromNative( cg ); |
80 | context->EnableOffset(true); | |
81 | SetGraphicsContext( context ); | |
489468fe SC |
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 | ||
928e7a7e | 109 | #if wxOSX_USE_CARBON |
489468fe SC |
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 | ||
489468fe SC |
117 | ControlRef handle = (ControlRef) m_window->GetHandle(); |
118 | if ( !handle ) | |
119 | return wxNullBitmap; | |
03647350 | 120 | |
489468fe SC |
121 | HIRect rect; |
122 | CGImageRef image; | |
123 | CGContextRef context; | |
489468fe SC |
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); | |
03647350 | 132 | |
489468fe | 133 | context = (CGContextRef)bmp.GetHBITMAP(); |
03647350 | 134 | |
489468fe | 135 | CGContextSaveGState(context); |
03647350 | 136 | |
489468fe SC |
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); | |
489468fe | 145 | return bmp; |
524c47aa | 146 | } |
928e7a7e | 147 | #endif |
489468fe SC |
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 | { | |
9a83f860 | 163 | wxCHECK_RET( window, wxT("invalid window in wxClientDCImpl") ); |
489468fe SC |
164 | wxPoint origin = window->GetClientAreaOrigin() ; |
165 | m_window->GetClientSize( &m_width , &m_height); | |
166 | if ( !m_window->IsShownOnScreen() ) | |
167 | m_width = m_height = 0; | |
0ffa23cc SC |
168 | |
169 | int x0,y0; | |
170 | DoGetDeviceOrigin(&x0,&y0); | |
171 | SetDeviceOrigin( origin.x + x0, origin.y + y0 ); | |
172 | ||
489468fe SC |
173 | DoSetClippingRegion( 0 , 0 , m_width , m_height ) ; |
174 | } | |
175 | ||
176 | wxClientDCImpl::~wxClientDCImpl() | |
177 | { | |
72a0d0fc SC |
178 | if( GetGraphicsContext() && GetGraphicsContext()->GetNativeContext() ) |
179 | Flush(); | |
489468fe SC |
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 | { | |
9a83f860 | 196 | wxASSERT_MSG( window->MacGetCGContextRef() != NULL, wxT("using wxPaintDC without being in a native paint event") ); |
489468fe SC |
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 | } |