]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/graphcmn.cpp | |
3 | // Purpose: graphics context methods common to all platforms | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #if defined(__BORLANDC__) | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #include "wx/graphics.h" | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/icon.h" | |
31 | #include "wx/bitmap.h" | |
32 | #include "wx/dcmemory.h" | |
33 | #endif | |
34 | ||
35 | #if wxUSE_GRAPHICS_CONTEXT | |
36 | ||
37 | #if !defined(wxMAC_USE_CORE_GRAPHICS_BLEND_MODES) | |
38 | #define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 0 | |
39 | #endif | |
40 | ||
41 | //----------------------------------------------------------------------------- | |
42 | // constants | |
43 | //----------------------------------------------------------------------------- | |
44 | ||
45 | const double RAD2DEG = 180.0 / M_PI; | |
46 | const short kEmulatedMode = -1; | |
47 | const short kUnsupportedMode = -2; | |
48 | ||
49 | //----------------------------------------------------------------------------- | |
50 | // Local functions | |
51 | //----------------------------------------------------------------------------- | |
52 | ||
53 | static inline double dmin(double a, double b) | |
54 | { | |
55 | return a < b ? a : b; | |
56 | } | |
57 | static inline double dmax(double a, double b) | |
58 | { | |
59 | return a > b ? a : b; | |
60 | } | |
61 | ||
62 | static inline double DegToRad(double deg) | |
63 | { | |
64 | return (deg * M_PI) / 180.0; | |
65 | } | |
66 | static inline double RadToDeg(double deg) | |
67 | { | |
68 | return (deg * 180.0) / M_PI; | |
69 | } | |
70 | ||
71 | ||
72 | wxPoint2DDouble wxGraphicsPath::GetCurrentPoint() | |
73 | { | |
74 | wxDouble x,y ; | |
75 | GetCurrentPoint(x,y); | |
76 | return wxPoint2DDouble(x,y); | |
77 | } | |
78 | ||
79 | void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble& p) | |
80 | { | |
81 | MoveToPoint( p.m_x , p.m_y); | |
82 | } | |
83 | ||
84 | void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble& p) | |
85 | { | |
86 | AddLineToPoint( p.m_x , p.m_y); | |
87 | } | |
88 | ||
89 | void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble& c1, const wxPoint2DDouble& c2, const wxPoint2DDouble& e) | |
90 | { | |
91 | AddCurveToPoint(c1.m_x, c1.m_y, c2.m_x, c2.m_y, e.m_x, e.m_y); | |
92 | } | |
93 | ||
94 | void wxGraphicsPath::AddArc( const wxPoint2DDouble& c, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise) | |
95 | { | |
96 | AddArc(c.m_x, c.m_y, r, startAngle, endAngle, clockwise); | |
97 | } | |
98 | ||
99 | // | |
100 | // Emulations | |
101 | // | |
102 | ||
103 | void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y ) | |
104 | { | |
105 | // calculate using degree elevation to a cubic bezier | |
106 | wxPoint2DDouble c1 ; | |
107 | wxPoint2DDouble c2 ; | |
108 | ||
109 | wxPoint2DDouble start = GetCurrentPoint() ; | |
110 | wxPoint2DDouble end(x,y); | |
111 | wxPoint2DDouble c(cx,cy); | |
112 | c1 = wxDouble(1/3.0) * start + wxDouble(2/3.0) * c; | |
113 | c2 = wxDouble(2/3.0) * c + wxDouble(1/3.0) * end ; | |
114 | AddCurveToPoint(c1.m_x,c1.m_y,c2.m_x,c2.m_y,x,y); | |
115 | } | |
116 | ||
117 | void wxGraphicsPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
118 | { | |
119 | MoveToPoint(x,y); | |
120 | AddLineToPoint(x,y+h); | |
121 | AddLineToPoint(x+w,y+h); | |
122 | AddLineToPoint(x+w,y); | |
123 | CloseSubpath(); | |
124 | } | |
125 | ||
126 | void wxGraphicsPath::AddCircle( wxDouble x, wxDouble y, wxDouble r ) | |
127 | { | |
128 | MoveToPoint(x+r,y); | |
129 | AddArc( x,y,r,0,2*M_PI,false); | |
130 | CloseSubpath(); | |
131 | } | |
132 | ||
133 | // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1) | |
134 | void wxGraphicsPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) | |
135 | { | |
136 | wxPoint2DDouble current = GetCurrentPoint(); | |
137 | wxPoint2DDouble p1(x1,y1); | |
138 | wxPoint2DDouble p2(x2,y2); | |
139 | ||
140 | wxPoint2DDouble v1 = current - p1 ; | |
141 | v1.Normalize(); | |
142 | wxPoint2DDouble v2 = p2 - p1 ; | |
143 | v2.Normalize(); | |
144 | ||
145 | wxDouble alpha = v1.GetVectorAngle() - v2.GetVectorAngle(); | |
146 | ||
147 | if ( alpha < 0 ) | |
148 | alpha = 360 + alpha ; | |
149 | // TODO obtuse angles | |
150 | ||
151 | alpha = DegToRad(alpha); | |
152 | ||
153 | wxDouble dist = r / sin(alpha/2) * cos(alpha/2) ; | |
154 | // calculate tangential points | |
155 | wxPoint2DDouble t1 = dist*v1 + p1 ; | |
156 | wxPoint2DDouble t2 = dist*v2 + p1 ; | |
157 | ||
158 | wxPoint2DDouble nv1 = v1 ; | |
159 | nv1.SetVectorAngle(v1.GetVectorAngle()-90); | |
160 | wxPoint2DDouble c = t1 + r*nv1; | |
161 | ||
162 | wxDouble a1 = v1.GetVectorAngle()+90 ; | |
163 | wxDouble a2 = v2.GetVectorAngle()-90 ; | |
164 | ||
165 | AddLineToPoint(t1); | |
166 | AddArc(c.m_x,c.m_y,r,DegToRad(a1),DegToRad(a2),true); | |
167 | AddLineToPoint(p2); | |
168 | } | |
169 | ||
170 | //----------------------------------------------------------------------------- | |
171 | // wxGraphicsContext Convenience Methods | |
172 | //----------------------------------------------------------------------------- | |
173 | ||
174 | void wxGraphicsContext::DrawPath( const wxGraphicsPath *path, int fillStyle ) | |
175 | { | |
176 | FillPath( path , fillStyle ); | |
177 | StrokePath( path ); | |
178 | } | |
179 | ||
180 | void wxGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle ) | |
181 | { | |
182 | Translate(x,y) ; | |
183 | Rotate( -angle ); | |
184 | DrawText( str , 0, 0 ); | |
185 | Rotate( angle ); | |
186 | Translate(-x,-y) ; | |
187 | } | |
188 | ||
189 | void wxGraphicsContext::StrokeLine( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2) | |
190 | { | |
191 | wxGraphicsPath* path = CreatePath(); | |
192 | path->MoveToPoint(x1, y1) ; | |
193 | path->AddLineToPoint( x2, y2 ); | |
194 | StrokePath( path ); | |
195 | delete path; | |
196 | } | |
197 | ||
198 | void wxGraphicsContext::DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h) | |
199 | { | |
200 | wxGraphicsPath* path = CreatePath(); | |
201 | path->AddRectangle( x , y , w , h ); | |
202 | DrawPath( path ); | |
203 | delete path; | |
204 | } | |
205 | ||
206 | void wxGraphicsContext::DrawEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h) | |
207 | { | |
208 | wxGraphicsPath* path = CreatePath(); | |
209 | if ( w == h ) | |
210 | { | |
211 | path->AddCircle( x+w/2,y+w/2,w/2); | |
212 | DrawPath(path); | |
213 | } | |
214 | else | |
215 | { | |
216 | PushState(); | |
217 | Translate(x+w/2,y+h/2); | |
218 | wxDouble factor = ((wxDouble) w) / h ; | |
219 | Scale( factor , 1.0) ; | |
220 | path->AddCircle(0,0,h/2); | |
221 | DrawPath(path); | |
222 | PopState(); | |
223 | } | |
224 | delete path; | |
225 | } | |
226 | ||
227 | void wxGraphicsContext::DrawRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius) | |
228 | { | |
229 | wxGraphicsPath* path = CreatePath(); | |
230 | if ( radius == 0) | |
231 | { | |
232 | path->AddRectangle( x , y , w , h ); | |
233 | DrawPath( path ); | |
234 | } | |
235 | else | |
236 | { | |
237 | PushState(); | |
238 | Translate( x , y ); | |
239 | ||
240 | path->MoveToPoint(w, h / 2); | |
241 | path->AddArcToPoint(w, h, w / 2, h, radius); | |
242 | path->AddArcToPoint(0, h, 0, h / 2, radius); | |
243 | path->AddArcToPoint(0, 0, w / 2, 0, radius); | |
244 | path->AddArcToPoint(w, 0, w, h / 2, radius); | |
245 | path->CloseSubpath(); | |
246 | DrawPath( path ); | |
247 | PopState(); | |
248 | } | |
249 | delete path; | |
250 | } | |
251 | ||
252 | void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *points) | |
253 | { | |
254 | wxASSERT(n > 1); | |
255 | wxGraphicsPath* path = CreatePath(); | |
256 | path->MoveToPoint(points[0].m_x, points[0].m_y) ; | |
257 | for ( size_t i = 1 ; i < n; ++i) | |
258 | path->AddLineToPoint( points[i].m_x, points[i].m_y ); | |
259 | StrokePath( path ); | |
260 | delete path; | |
261 | } | |
262 | ||
263 | void wxGraphicsContext::DrawLines( size_t n, const wxPoint2DDouble *points, int fillStyle) | |
264 | { | |
265 | wxASSERT(n > 1); | |
266 | wxGraphicsPath* path = CreatePath(); | |
267 | path->MoveToPoint(points[0].m_x, points[0].m_y) ; | |
268 | for ( size_t i = 1 ; i < n; ++i) | |
269 | path->AddLineToPoint( points[i].m_x, points[i].m_y ); | |
270 | DrawPath( path , fillStyle); | |
271 | delete path; | |
272 | } | |
273 | ||
274 | void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *beginPoints, const wxPoint2DDouble *endPoints) | |
275 | { | |
276 | wxASSERT(n > 0); | |
277 | wxGraphicsPath* path = CreatePath(); | |
278 | for ( size_t i = 0 ; i < n; ++i) | |
279 | { | |
280 | path->MoveToPoint(beginPoints[i].m_x, beginPoints[i].m_y) ; | |
281 | path->AddLineToPoint( endPoints[i].m_x, endPoints[i].m_y ); | |
282 | } | |
283 | StrokePath( path ); | |
284 | delete path; | |
285 | } | |
286 | ||
287 | IMPLEMENT_ABSTRACT_CLASS(wxGCDC, wxObject) | |
288 | ||
289 | //----------------------------------------------------------------------------- | |
290 | // wxDC bridge class | |
291 | //----------------------------------------------------------------------------- | |
292 | ||
293 | wxGCDC::wxGCDC() | |
294 | { | |
295 | Init(); | |
296 | } | |
297 | ||
298 | ||
299 | wxGCDC::wxGCDC(const wxWindowDC& dc) | |
300 | { | |
301 | Init(); | |
302 | m_graphicContext = wxGraphicsContext::Create(dc); | |
303 | m_ok = true; | |
304 | if ( dc.GetFont().Ok()) | |
305 | m_graphicContext->SetFont(dc.GetFont()); | |
306 | if ( dc.GetPen().Ok()) | |
307 | m_graphicContext->SetPen(dc.GetPen()); | |
308 | if ( dc.GetBrush().Ok()) | |
309 | m_graphicContext->SetBrush(dc.GetBrush()); | |
310 | m_graphicContext->SetTextColor(dc.GetTextForeground()); | |
311 | } | |
312 | ||
313 | void wxGCDC::Init() | |
314 | { | |
315 | m_ok = false; | |
316 | m_colour = true; | |
317 | m_mm_to_pix_x = mm2pt; | |
318 | m_mm_to_pix_y = mm2pt; | |
319 | ||
320 | m_pen = *wxBLACK_PEN; | |
321 | m_font = *wxNORMAL_FONT; | |
322 | m_brush = *wxWHITE_BRUSH; | |
323 | ||
324 | m_graphicContext = NULL; | |
325 | } | |
326 | ||
327 | ||
328 | wxGCDC::~wxGCDC() | |
329 | { | |
330 | delete m_graphicContext; | |
331 | } | |
332 | ||
333 | void wxGCDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool WXUNUSED(useMask) ) | |
334 | { | |
335 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") ); | |
336 | wxCHECK_RET( bmp.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") ); | |
337 | ||
338 | wxCoord xx = LogicalToDeviceX(x); | |
339 | wxCoord yy = LogicalToDeviceY(y); | |
340 | wxCoord w = bmp.GetWidth(); | |
341 | wxCoord h = bmp.GetHeight(); | |
342 | wxCoord ww = LogicalToDeviceXRel(w); | |
343 | wxCoord hh = LogicalToDeviceYRel(h); | |
344 | ||
345 | m_graphicContext->DrawBitmap( bmp, xx , yy , ww , hh ); | |
346 | } | |
347 | ||
348 | void wxGCDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y ) | |
349 | { | |
350 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") ); | |
351 | wxCHECK_RET( icon.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") ); | |
352 | ||
353 | wxCoord xx = LogicalToDeviceX(x); | |
354 | wxCoord yy = LogicalToDeviceY(y); | |
355 | wxCoord w = icon.GetWidth(); | |
356 | wxCoord h = icon.GetHeight(); | |
357 | wxCoord ww = LogicalToDeviceXRel(w); | |
358 | wxCoord hh = LogicalToDeviceYRel(h); | |
359 | ||
360 | m_graphicContext->DrawIcon( icon , xx, yy, ww, hh ); | |
361 | } | |
362 | ||
363 | void wxGCDC::DoSetClippingRegion( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxCoord WXUNUSED(width), wxCoord WXUNUSED(height) ) | |
364 | { | |
365 | // TODO Clipping | |
366 | #if 0 | |
367 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") ); | |
368 | ||
369 | wxCoord xx, yy, ww, hh; | |
370 | xx = LogicalToDeviceX(x); | |
371 | yy = LogicalToDeviceY(y); | |
372 | ww = LogicalToDeviceXRel(width); | |
373 | hh = LogicalToDeviceYRel(height); | |
374 | ||
375 | CGContextRef cgContext = ((wxCairoContext*)(m_graphicContext))->GetNativeContext(); | |
376 | CGRect clipRect = CGRectMake( xx , yy , ww, hh ); | |
377 | CGContextClipToRect( cgContext , clipRect ); | |
378 | ||
379 | // SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh ); | |
380 | // SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ); | |
381 | ||
382 | if ( m_clipping ) | |
383 | { | |
384 | m_clipX1 = wxMax( m_clipX1, xx ); | |
385 | m_clipY1 = wxMax( m_clipY1, yy ); | |
386 | m_clipX2 = wxMin( m_clipX2, (xx + ww) ); | |
387 | m_clipY2 = wxMin( m_clipY2, (yy + hh) ); | |
388 | } | |
389 | else | |
390 | { | |
391 | m_clipping = true; | |
392 | ||
393 | m_clipX1 = xx; | |
394 | m_clipY1 = yy; | |
395 | m_clipX2 = xx + ww; | |
396 | m_clipY2 = yy + hh; | |
397 | } | |
398 | ||
399 | // TODO: as soon as we don't reset the context for each operation anymore | |
400 | // we have to update the context as well | |
401 | #endif | |
402 | ||
403 | } | |
404 | ||
405 | void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion ®ion ) | |
406 | { | |
407 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") ); | |
408 | ||
409 | if (region.Empty()) | |
410 | { | |
411 | DestroyClippingRegion(); | |
412 | return; | |
413 | } | |
414 | ||
415 | wxCoord x, y, w, h; | |
416 | region.GetBox( x, y, w, h ); | |
417 | wxCoord xx, yy, ww, hh; | |
418 | xx = LogicalToDeviceX(x); | |
419 | yy = LogicalToDeviceY(y); | |
420 | ww = LogicalToDeviceXRel(w); | |
421 | hh = LogicalToDeviceYRel(h); | |
422 | ||
423 | // if we have a scaling that we cannot map onto native regions | |
424 | // we must use the box | |
425 | if ( ww != w || hh != h ) | |
426 | { | |
427 | wxGCDC::DoSetClippingRegion( x, y, w, h ); | |
428 | } | |
429 | else | |
430 | { | |
431 | #if 0 | |
432 | CopyRgn( (RgnHandle) region.GetWXHRGN() , (RgnHandle) m_macCurrentClipRgn ); | |
433 | if ( xx != x || yy != y ) | |
434 | OffsetRgn( (RgnHandle) m_macCurrentClipRgn , xx - x , yy - y ); | |
435 | SectRgn( (RgnHandle)m_macCurrentClipRgn , (RgnHandle)m_macBoundaryClipRgn , (RgnHandle)m_macCurrentClipRgn ); | |
436 | #endif | |
437 | ||
438 | if ( m_clipping ) | |
439 | { | |
440 | m_clipX1 = wxMax( m_clipX1, xx ); | |
441 | m_clipY1 = wxMax( m_clipY1, yy ); | |
442 | m_clipX2 = wxMin( m_clipX2, (xx + ww) ); | |
443 | m_clipY2 = wxMin( m_clipY2, (yy + hh) ); | |
444 | } | |
445 | else | |
446 | { | |
447 | m_clipping = true; | |
448 | ||
449 | m_clipX1 = xx; | |
450 | m_clipY1 = yy; | |
451 | m_clipX2 = xx + ww; | |
452 | m_clipY2 = yy + hh; | |
453 | } | |
454 | } | |
455 | } | |
456 | ||
457 | void wxGCDC::DestroyClippingRegion() | |
458 | { | |
459 | // TODO Clipping | |
460 | #if 0 | |
461 | // CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ); | |
462 | ||
463 | CGContextRef cgContext = ((wxCairoContext*)(m_graphicContext))->GetNativeContext(); | |
464 | CGContextRestoreGState( cgContext ); | |
465 | CGContextSaveGState( cgContext ); | |
466 | ||
467 | m_graphicContext->SetPen( m_pen ); | |
468 | m_graphicContext->SetBrush( m_brush ); | |
469 | ||
470 | m_clipping = false; | |
471 | #endif | |
472 | ||
473 | } | |
474 | ||
475 | void wxGCDC::DoGetSizeMM( int* width, int* height ) const | |
476 | { | |
477 | int w = 0, h = 0; | |
478 | ||
479 | GetSize( &w, &h ); | |
480 | if (width) | |
481 | *width = long( double(w) / (m_scaleX * m_mm_to_pix_x) ); | |
482 | if (height) | |
483 | *height = long( double(h) / (m_scaleY * m_mm_to_pix_y) ); | |
484 | } | |
485 | ||
486 | void wxGCDC::SetTextForeground( const wxColour &col ) | |
487 | { | |
488 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") ); | |
489 | ||
490 | if ( col != m_textForegroundColour ) | |
491 | { | |
492 | m_textForegroundColour = col; | |
493 | m_graphicContext->SetTextColor( col ); | |
494 | } | |
495 | } | |
496 | ||
497 | void wxGCDC::SetTextBackground( const wxColour &col ) | |
498 | { | |
499 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") ); | |
500 | ||
501 | m_textBackgroundColour = col; | |
502 | } | |
503 | ||
504 | void wxGCDC::SetMapMode( int mode ) | |
505 | { | |
506 | switch (mode) | |
507 | { | |
508 | case wxMM_TWIPS: | |
509 | SetLogicalScale( twips2mm * m_mm_to_pix_x, twips2mm * m_mm_to_pix_y ); | |
510 | break; | |
511 | ||
512 | case wxMM_POINTS: | |
513 | SetLogicalScale( pt2mm * m_mm_to_pix_x, pt2mm * m_mm_to_pix_y ); | |
514 | break; | |
515 | ||
516 | case wxMM_METRIC: | |
517 | SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y ); | |
518 | break; | |
519 | ||
520 | case wxMM_LOMETRIC: | |
521 | SetLogicalScale( m_mm_to_pix_x / 10.0, m_mm_to_pix_y / 10.0 ); | |
522 | break; | |
523 | ||
524 | case wxMM_TEXT: | |
525 | default: | |
526 | SetLogicalScale( 1.0, 1.0 ); | |
527 | break; | |
528 | } | |
529 | ||
530 | ComputeScaleAndOrigin(); | |
531 | } | |
532 | ||
533 | void wxGCDC::SetUserScale( double x, double y ) | |
534 | { | |
535 | // allow negative ? -> no | |
536 | m_userScaleX = x; | |
537 | m_userScaleY = y; | |
538 | ComputeScaleAndOrigin(); | |
539 | } | |
540 | ||
541 | void wxGCDC::SetLogicalScale( double x, double y ) | |
542 | { | |
543 | // allow negative ? | |
544 | m_logicalScaleX = x; | |
545 | m_logicalScaleY = y; | |
546 | ComputeScaleAndOrigin(); | |
547 | } | |
548 | ||
549 | void wxGCDC::SetLogicalOrigin( wxCoord x, wxCoord y ) | |
550 | { | |
551 | m_logicalOriginX = x * m_signX; // is this still correct ? | |
552 | m_logicalOriginY = y * m_signY; | |
553 | ComputeScaleAndOrigin(); | |
554 | } | |
555 | ||
556 | void wxGCDC::SetDeviceOrigin( wxCoord x, wxCoord y ) | |
557 | { | |
558 | m_deviceOriginX = x; | |
559 | m_deviceOriginY = y; | |
560 | ComputeScaleAndOrigin(); | |
561 | } | |
562 | ||
563 | void wxGCDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp ) | |
564 | { | |
565 | m_signX = (xLeftRight ? 1 : -1); | |
566 | m_signY = (yBottomUp ? -1 : 1); | |
567 | ComputeScaleAndOrigin(); | |
568 | } | |
569 | ||
570 | wxSize wxGCDC::GetPPI() const | |
571 | { | |
572 | return wxSize(72, 72); | |
573 | } | |
574 | ||
575 | int wxGCDC::GetDepth() const | |
576 | { | |
577 | return 32; | |
578 | } | |
579 | ||
580 | void wxGCDC::ComputeScaleAndOrigin() | |
581 | { | |
582 | // this is a bit artificial, but we need to force wxGCDC to think | |
583 | // the pen has changed | |
584 | wxPen pen( GetPen() ); | |
585 | ||
586 | m_pen = wxNullPen; | |
587 | SetPen( pen ); | |
588 | } | |
589 | ||
590 | void wxGCDC::SetPalette( const wxPalette& WXUNUSED(palette) ) | |
591 | { | |
592 | ||
593 | } | |
594 | ||
595 | void wxGCDC::SetBackgroundMode( int mode ) | |
596 | { | |
597 | m_backgroundMode = mode; | |
598 | } | |
599 | ||
600 | void wxGCDC::SetFont( const wxFont &font ) | |
601 | { | |
602 | m_font = font; | |
603 | if ( m_graphicContext ) | |
604 | m_graphicContext->SetFont( font ); | |
605 | } | |
606 | ||
607 | void wxGCDC::SetPen( const wxPen &pen ) | |
608 | { | |
609 | if ( m_pen == pen ) | |
610 | return; | |
611 | ||
612 | m_pen = pen; | |
613 | if ( m_graphicContext ) | |
614 | { | |
615 | if ( m_pen.GetStyle() == wxSOLID || m_pen.GetStyle() == wxTRANSPARENT ) | |
616 | { | |
617 | m_graphicContext->SetPen( m_pen ); | |
618 | } | |
619 | else | |
620 | { | |
621 | // we have to compensate for moved device origins etc. otherwise patterned pens are standing still | |
622 | // eg when using a wxScrollWindow and scrolling around | |
623 | int origX = LogicalToDeviceX( 0 ); | |
624 | int origY = LogicalToDeviceY( 0 ); | |
625 | m_graphicContext->Translate( origX , origY ); | |
626 | m_graphicContext->SetPen( m_pen ); | |
627 | m_graphicContext->Translate( -origX , -origY ); | |
628 | } | |
629 | } | |
630 | } | |
631 | ||
632 | void wxGCDC::SetBrush( const wxBrush &brush ) | |
633 | { | |
634 | if (m_brush == brush) | |
635 | return; | |
636 | ||
637 | m_brush = brush; | |
638 | if ( m_graphicContext ) | |
639 | { | |
640 | if ( brush.GetStyle() == wxSOLID || brush.GetStyle() == wxTRANSPARENT ) | |
641 | { | |
642 | m_graphicContext->SetBrush( m_brush ); | |
643 | } | |
644 | else | |
645 | { | |
646 | // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still | |
647 | // eg when using a wxScrollWindow and scrolling around | |
648 | // TODO on MSW / GDIPlus this still occurs with hatched brushes | |
649 | int origX = LogicalToDeviceX(0); | |
650 | int origY = LogicalToDeviceY(0); | |
651 | m_graphicContext->Translate( origX , origY ); | |
652 | m_graphicContext->SetBrush( m_brush ); | |
653 | m_graphicContext->Translate( -origX , -origY ); | |
654 | } | |
655 | } | |
656 | } | |
657 | ||
658 | void wxGCDC::SetBackground( const wxBrush &brush ) | |
659 | { | |
660 | if (m_backgroundBrush == brush) | |
661 | return; | |
662 | ||
663 | m_backgroundBrush = brush; | |
664 | if (!m_backgroundBrush.Ok()) | |
665 | return; | |
666 | } | |
667 | ||
668 | void wxGCDC::SetLogicalFunction( int function ) | |
669 | { | |
670 | if (m_logicalFunction == function) | |
671 | return; | |
672 | ||
673 | m_logicalFunction = function; | |
674 | #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES | |
675 | ||
676 | CGContextRef cgContext = ((wxCairoContext*)(m_graphicContext))->GetNativeContext(); | |
677 | if ( m_logicalFunction == wxCOPY ) | |
678 | CGContextSetBlendMode( cgContext, kCGBlendModeNormal ); | |
679 | else if ( m_logicalFunction == wxINVERT ) | |
680 | CGContextSetBlendMode( cgContext, kCGBlendModeExclusion ); | |
681 | else | |
682 | CGContextSetBlendMode( cgContext, kCGBlendModeNormal ); | |
683 | #endif | |
684 | ||
685 | } | |
686 | ||
687 | extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y, | |
688 | const wxColour & col, int style); | |
689 | ||
690 | bool wxGCDC::DoFloodFill(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), | |
691 | const wxColour& WXUNUSED(col), int WXUNUSED(style)) | |
692 | { | |
693 | return false; | |
694 | } | |
695 | ||
696 | bool wxGCDC::DoGetPixel( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour *WXUNUSED(col) ) const | |
697 | { | |
698 | // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") ); | |
699 | return false; | |
700 | } | |
701 | ||
702 | void wxGCDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 ) | |
703 | { | |
704 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") ); | |
705 | ||
706 | #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES | |
707 | ||
708 | if ( m_logicalFunction != wxCOPY ) | |
709 | return; | |
710 | #endif | |
711 | ||
712 | wxCoord xx1 = LogicalToDeviceX(x1); | |
713 | wxCoord yy1 = LogicalToDeviceY(y1); | |
714 | wxCoord xx2 = LogicalToDeviceX(x2); | |
715 | wxCoord yy2 = LogicalToDeviceY(y2); | |
716 | ||
717 | m_graphicContext->StrokeLine(xx1,yy1,xx2,yy2); | |
718 | ||
719 | CalcBoundingBox(x1, y1); | |
720 | CalcBoundingBox(x2, y2); | |
721 | } | |
722 | ||
723 | void wxGCDC::DoCrossHair( wxCoord x, wxCoord y ) | |
724 | { | |
725 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") ); | |
726 | ||
727 | if ( m_logicalFunction != wxCOPY ) | |
728 | return; | |
729 | ||
730 | int w = 0, h = 0; | |
731 | ||
732 | GetSize( &w, &h ); | |
733 | ||
734 | wxCoord xx = LogicalToDeviceX(x); | |
735 | wxCoord yy = LogicalToDeviceY(y); | |
736 | wxCoord xw = LogicalToDeviceX(w); | |
737 | wxCoord x0 = LogicalToDeviceX(0); | |
738 | wxCoord y0 = LogicalToDeviceY(0); | |
739 | wxCoord yh = LogicalToDeviceY(h); | |
740 | ||
741 | m_graphicContext->StrokeLine(x0,yy,xw,yy); | |
742 | m_graphicContext->StrokeLine(xx,y0,xx,yh); | |
743 | ||
744 | CalcBoundingBox(x0, y0); | |
745 | CalcBoundingBox(x0+xw, y0+yh); | |
746 | } | |
747 | ||
748 | void wxGCDC::DoDrawArc( wxCoord x1, wxCoord y1, | |
749 | wxCoord x2, wxCoord y2, | |
750 | wxCoord xc, wxCoord yc ) | |
751 | { | |
752 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") ); | |
753 | ||
754 | if ( m_logicalFunction != wxCOPY ) | |
755 | return; | |
756 | ||
757 | wxCoord xx1 = LogicalToDeviceX(x1); | |
758 | wxCoord yy1 = LogicalToDeviceY(y1); | |
759 | wxCoord xx2 = LogicalToDeviceX(x2); | |
760 | wxCoord yy2 = LogicalToDeviceY(y2); | |
761 | wxCoord xxc = LogicalToDeviceX(xc); | |
762 | wxCoord yyc = LogicalToDeviceY(yc); | |
763 | ||
764 | double dx = xx1 - xxc; | |
765 | double dy = yy1 - yyc; | |
766 | double radius = sqrt((double)(dx * dx + dy * dy)); | |
767 | wxCoord rad = (wxCoord)radius; | |
768 | double sa, ea; | |
769 | if (xx1 == xx2 && yy1 == yy2) | |
770 | { | |
771 | sa = 0.0; | |
772 | ea = 360.0; | |
773 | } | |
774 | else if (radius == 0.0) | |
775 | { | |
776 | sa = ea = 0.0; | |
777 | } | |
778 | else | |
779 | { | |
780 | sa = (xx1 - xxc == 0) ? | |
781 | (yy1 - yyc < 0) ? 90.0 : -90.0 : | |
782 | -atan2(double(yy1 - yyc), double(xx1 - xxc)) * RAD2DEG; | |
783 | ea = (xx2 - xxc == 0) ? | |
784 | (yy2 - yyc < 0) ? 90.0 : -90.0 : | |
785 | -atan2(double(yy2 - yyc), double(xx2 - xxc)) * RAD2DEG; | |
786 | } | |
787 | ||
788 | bool fill = m_brush.GetStyle() != wxTRANSPARENT; | |
789 | ||
790 | wxGraphicsPath* path = m_graphicContext->CreatePath(); | |
791 | if ( fill && ((x1!=x2)||(y1!=y2)) ) | |
792 | path->MoveToPoint( xxc, yyc ); | |
793 | path->AddArc( xxc, yyc , rad , DegToRad(sa) , DegToRad(ea), false ); | |
794 | if ( fill && ((x1!=x2)||(y1!=y2)) ) | |
795 | path->AddLineToPoint( xxc, yyc ); | |
796 | m_graphicContext->DrawPath(path); | |
797 | delete path; | |
798 | } | |
799 | ||
800 | void wxGCDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h, | |
801 | double sa, double ea ) | |
802 | { | |
803 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") ); | |
804 | ||
805 | if ( m_logicalFunction != wxCOPY ) | |
806 | return; | |
807 | ||
808 | wxCoord xx = LogicalToDeviceX(x); | |
809 | wxCoord yy = LogicalToDeviceY(y); | |
810 | wxCoord ww = m_signX * LogicalToDeviceXRel(w); | |
811 | wxCoord hh = m_signY * LogicalToDeviceYRel(h); | |
812 | ||
813 | // handle -ve width and/or height | |
814 | if (ww < 0) | |
815 | { | |
816 | ww = -ww; | |
817 | xx = xx - ww; | |
818 | } | |
819 | if (hh < 0) | |
820 | { | |
821 | hh = -hh; | |
822 | yy = yy - hh; | |
823 | } | |
824 | ||
825 | bool fill = m_brush.GetStyle() != wxTRANSPARENT; | |
826 | ||
827 | wxGraphicsPath* path = m_graphicContext->CreatePath(); | |
828 | m_graphicContext->PushState(); | |
829 | m_graphicContext->Translate(xx+ww/2,yy+hh/2); | |
830 | wxDouble factor = ((wxDouble) ww) / hh ; | |
831 | m_graphicContext->Scale( factor , 1.0) ; | |
832 | if ( fill && (sa!=ea) ) | |
833 | path->MoveToPoint(0,0); | |
834 | // since these angles (ea,sa) are measured counter-clockwise, we invert them to | |
835 | // get clockwise angles | |
836 | path->AddArc( 0, 0, hh/2 , DegToRad(-sa) , DegToRad(-ea), sa > ea ); | |
837 | if ( fill && (sa!=ea) ) | |
838 | path->AddLineToPoint(0,0); | |
839 | m_graphicContext->DrawPath( path ); | |
840 | m_graphicContext->PopState(); | |
841 | delete path; | |
842 | } | |
843 | ||
844 | void wxGCDC::DoDrawPoint( wxCoord x, wxCoord y ) | |
845 | { | |
846 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") ); | |
847 | ||
848 | DoDrawLine( x , y , x + 1 , y + 1 ); | |
849 | } | |
850 | ||
851 | void wxGCDC::DoDrawLines(int n, wxPoint points[], | |
852 | wxCoord xoffset, wxCoord yoffset) | |
853 | { | |
854 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") ); | |
855 | ||
856 | #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES | |
857 | ||
858 | if ( m_logicalFunction != wxCOPY ) | |
859 | return; | |
860 | #endif | |
861 | ||
862 | wxPoint2DDouble* pointsD = new wxPoint2DDouble[n] ; | |
863 | for( int i = 0 ; i < n; ++i) | |
864 | { | |
865 | pointsD[i].m_x = LogicalToDeviceX(points[i].x + xoffset); | |
866 | pointsD[i].m_y = LogicalToDeviceY(points[i].y + yoffset); | |
867 | } | |
868 | ||
869 | m_graphicContext->StrokeLines( n , pointsD) ; | |
870 | delete[] pointsD; | |
871 | } | |
872 | ||
873 | #if wxUSE_SPLINES | |
874 | void wxGCDC::DoDrawSpline(wxList *points) | |
875 | { | |
876 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") ); | |
877 | ||
878 | if ( m_logicalFunction != wxCOPY ) | |
879 | return; | |
880 | ||
881 | wxGraphicsPath* path = m_graphicContext->CreatePath(); | |
882 | ||
883 | wxList::compatibility_iterator node = points->GetFirst(); | |
884 | if (node == wxList::compatibility_iterator()) | |
885 | // empty list | |
886 | return; | |
887 | ||
888 | wxPoint *p = (wxPoint *)node->GetData(); | |
889 | ||
890 | wxCoord x1 = p->x; | |
891 | wxCoord y1 = p->y; | |
892 | ||
893 | node = node->GetNext(); | |
894 | p = (wxPoint *)node->GetData(); | |
895 | ||
896 | wxCoord x2 = p->x; | |
897 | wxCoord y2 = p->y; | |
898 | wxCoord cx1 = ( x1 + x2 ) / 2; | |
899 | wxCoord cy1 = ( y1 + y2 ) / 2; | |
900 | ||
901 | path->MoveToPoint( LogicalToDeviceX( x1 ) , LogicalToDeviceY( y1 ) ); | |
902 | path->AddLineToPoint( LogicalToDeviceX( cx1 ) , LogicalToDeviceY( cy1 ) ); | |
903 | #if !wxUSE_STL | |
904 | ||
905 | while ((node = node->GetNext()) != NULL) | |
906 | #else | |
907 | ||
908 | while ((node = node->GetNext())) | |
909 | #endif // !wxUSE_STL | |
910 | ||
911 | { | |
912 | p = (wxPoint *)node->GetData(); | |
913 | x1 = x2; | |
914 | y1 = y2; | |
915 | x2 = p->x; | |
916 | y2 = p->y; | |
917 | wxCoord cx4 = (x1 + x2) / 2; | |
918 | wxCoord cy4 = (y1 + y2) / 2; | |
919 | ||
920 | path->AddQuadCurveToPoint( | |
921 | LogicalToDeviceX( x1 ) , LogicalToDeviceY( y1 ) , | |
922 | LogicalToDeviceX( cx4 ) , LogicalToDeviceY( cy4 ) ); | |
923 | ||
924 | cx1 = cx4; | |
925 | cy1 = cy4; | |
926 | } | |
927 | ||
928 | path->AddLineToPoint( LogicalToDeviceX( x2 ) , LogicalToDeviceY( y2 ) ); | |
929 | ||
930 | m_graphicContext->StrokePath( path ); | |
931 | delete path; | |
932 | } | |
933 | #endif | |
934 | ||
935 | void wxGCDC::DoDrawPolygon( int n, wxPoint points[], | |
936 | wxCoord xoffset, wxCoord yoffset, | |
937 | int fillStyle ) | |
938 | { | |
939 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") ); | |
940 | ||
941 | if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) ) | |
942 | return; | |
943 | if ( m_logicalFunction != wxCOPY ) | |
944 | return; | |
945 | ||
946 | bool closeIt = false ; | |
947 | if (points[n-1] != points[0]) | |
948 | closeIt = true ; | |
949 | ||
950 | wxPoint2DDouble* pointsD = new wxPoint2DDouble[n+(closeIt?1:0)] ; | |
951 | for( int i = 0 ; i < n; ++i) | |
952 | { | |
953 | pointsD[i].m_x = LogicalToDeviceX(points[i].x + xoffset); | |
954 | pointsD[i].m_y = LogicalToDeviceY(points[i].y + yoffset); | |
955 | } | |
956 | if ( closeIt ) | |
957 | pointsD[n] = pointsD[0]; | |
958 | ||
959 | m_graphicContext->DrawLines( n+(closeIt?1:0) , pointsD, fillStyle) ; | |
960 | delete[] pointsD; | |
961 | } | |
962 | ||
963 | void wxGCDC::DoDrawPolyPolygon(int n, | |
964 | int count[], | |
965 | wxPoint points[], | |
966 | wxCoord xoffset, | |
967 | wxCoord yoffset, | |
968 | int fillStyle) | |
969 | { | |
970 | wxASSERT(n > 1); | |
971 | wxGraphicsPath* path = m_graphicContext->CreatePath(); | |
972 | ||
973 | int i = 0 ; | |
974 | for ( int j = 0 ; j < n ; ++j) | |
975 | { | |
976 | wxPoint start = points[i]; | |
977 | path->MoveToPoint(LogicalToDeviceX(start.x+ xoffset), LogicalToDeviceY(start.y+ yoffset)) ; | |
978 | ++i; | |
979 | int l = count[j]; | |
980 | for ( int k = 1 ; k < l ; ++k) | |
981 | { | |
982 | path->AddLineToPoint( LogicalToDeviceX(points[i].x+ xoffset), LogicalToDeviceY(points[i].y+ yoffset)); | |
983 | ++i ; | |
984 | } | |
985 | // close the polygon | |
986 | if ( start != points[i-1]) | |
987 | path->AddLineToPoint( LogicalToDeviceX(start.x+ xoffset), LogicalToDeviceY(start.y+ yoffset)); | |
988 | } | |
989 | m_graphicContext->DrawPath( path , fillStyle); | |
990 | delete path; | |
991 | } | |
992 | ||
993 | void wxGCDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height) | |
994 | { | |
995 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") ); | |
996 | ||
997 | if ( m_logicalFunction != wxCOPY ) | |
998 | return; | |
999 | ||
1000 | wxCoord xx = LogicalToDeviceX(x); | |
1001 | wxCoord yy = LogicalToDeviceY(y); | |
1002 | wxCoord ww = m_signX * LogicalToDeviceXRel(width); | |
1003 | wxCoord hh = m_signY * LogicalToDeviceYRel(height); | |
1004 | ||
1005 | // CMB: draw nothing if transformed w or h is 0 | |
1006 | if (ww == 0 || hh == 0) | |
1007 | return; | |
1008 | ||
1009 | // CMB: handle -ve width and/or height | |
1010 | if (ww < 0) | |
1011 | { | |
1012 | ww = -ww; | |
1013 | xx = xx - ww; | |
1014 | } | |
1015 | if (hh < 0) | |
1016 | { | |
1017 | hh = -hh; | |
1018 | yy = yy - hh; | |
1019 | } | |
1020 | m_graphicContext->DrawRectangle( xx,yy,ww,hh); | |
1021 | } | |
1022 | ||
1023 | void wxGCDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, | |
1024 | wxCoord width, wxCoord height, | |
1025 | double radius) | |
1026 | { | |
1027 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") ); | |
1028 | ||
1029 | if ( m_logicalFunction != wxCOPY ) | |
1030 | return; | |
1031 | ||
1032 | if (radius < 0.0) | |
1033 | radius = - radius * ((width < height) ? width : height); | |
1034 | wxCoord xx = LogicalToDeviceX(x); | |
1035 | wxCoord yy = LogicalToDeviceY(y); | |
1036 | wxCoord ww = m_signX * LogicalToDeviceXRel(width); | |
1037 | wxCoord hh = m_signY * LogicalToDeviceYRel(height); | |
1038 | ||
1039 | // CMB: draw nothing if transformed w or h is 0 | |
1040 | if (ww == 0 || hh == 0) | |
1041 | return; | |
1042 | ||
1043 | // CMB: handle -ve width and/or height | |
1044 | if (ww < 0) | |
1045 | { | |
1046 | ww = -ww; | |
1047 | xx = xx - ww; | |
1048 | } | |
1049 | if (hh < 0) | |
1050 | { | |
1051 | hh = -hh; | |
1052 | yy = yy - hh; | |
1053 | } | |
1054 | ||
1055 | m_graphicContext->DrawRoundedRectangle( xx,yy,ww,hh,radius); | |
1056 | } | |
1057 | ||
1058 | void wxGCDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height) | |
1059 | { | |
1060 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") ); | |
1061 | ||
1062 | if ( m_logicalFunction != wxCOPY ) | |
1063 | return; | |
1064 | ||
1065 | wxCoord xx = LogicalToDeviceX(x); | |
1066 | wxCoord yy = LogicalToDeviceY(y); | |
1067 | wxDouble ww = m_signX * LogicalToDeviceXRel(width); | |
1068 | wxCoord hh = m_signY * LogicalToDeviceYRel(height); | |
1069 | ||
1070 | // CMB: draw nothing if transformed w or h is 0 | |
1071 | if (ww == 0 || hh == 0) | |
1072 | return; | |
1073 | ||
1074 | // CMB: handle -ve width and/or height | |
1075 | if (ww < 0) | |
1076 | { | |
1077 | ww = -ww; | |
1078 | xx = xx - ww; | |
1079 | } | |
1080 | if (hh < 0) | |
1081 | { | |
1082 | hh = -hh; | |
1083 | yy = yy - hh; | |
1084 | } | |
1085 | ||
1086 | m_graphicContext->DrawEllipse(xx,yy,ww,hh); | |
1087 | } | |
1088 | ||
1089 | bool wxGCDC::CanDrawBitmap() const | |
1090 | { | |
1091 | return true; | |
1092 | } | |
1093 | ||
1094 | bool wxGCDC::DoBlit( | |
1095 | wxCoord WXUNUSED(xdest), wxCoord WXUNUSED(ydest), wxCoord WXUNUSED(width), wxCoord WXUNUSED(height), | |
1096 | wxDC *source, wxCoord WXUNUSED(xsrc), wxCoord WXUNUSED(ysrc), int logical_func , bool WXUNUSED(useMask), | |
1097 | wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask) ) | |
1098 | { | |
1099 | wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") ); | |
1100 | wxCHECK_MSG( source->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") ); | |
1101 | ||
1102 | if ( logical_func == wxNO_OP ) | |
1103 | return true; | |
1104 | ||
1105 | #if 0 | |
1106 | if (xsrcMask == -1 && ysrcMask == -1) | |
1107 | { | |
1108 | xsrcMask = xsrc; | |
1109 | ysrcMask = ysrc; | |
1110 | } | |
1111 | ||
1112 | wxCoord yysrc = source-> LogicalToDeviceY(ysrc); | |
1113 | wxCoord xxsrc = source-> LogicalToDeviceX(xsrc); | |
1114 | wxCoord wwsrc = source-> LogicalToDeviceXRel(width); | |
1115 | wxCoord hhsrc = source-> LogicalToDeviceYRel(height); | |
1116 | ||
1117 | wxCoord yydest = LogicalToDeviceY(ydest); | |
1118 | wxCoord xxdest = LogicalToDeviceX(xdest); | |
1119 | wxCoord wwdest = LogicalToDeviceXRel(width); | |
1120 | wxCoord hhdest = LogicalToDeviceYRel(height); | |
1121 | ||
1122 | wxMemoryDC* memdc = dynamic_cast<wxMemoryDC*>(source); | |
1123 | if ( memdc && logical_func == wxCOPY ) | |
1124 | { | |
1125 | wxBitmap blit = memdc->GetSelectedObject(); | |
1126 | ||
1127 | wxASSERT_MSG( blit.Ok() , wxT("Invalid bitmap for blitting") ); | |
1128 | ||
1129 | wxCoord bmpwidth = blit.GetWidth(); | |
1130 | wxCoord bmpheight = blit.GetHeight(); | |
1131 | ||
1132 | if ( xxsrc != 0 || yysrc != 0 || bmpwidth != wwsrc || bmpheight != hhsrc ) | |
1133 | { | |
1134 | wwsrc = wxMin( wwsrc , bmpwidth - xxsrc ); | |
1135 | hhsrc = wxMin( hhsrc , bmpheight - yysrc ); | |
1136 | if ( wwsrc > 0 && hhsrc > 0 ) | |
1137 | { | |
1138 | if ( xxsrc >= 0 && yysrc >= 0 ) | |
1139 | { | |
1140 | wxRect subrect( xxsrc, yysrc, wwsrc , hhsrc ); | |
1141 | // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons | |
1142 | blit = blit.GetSubBitmap( subrect ); | |
1143 | } | |
1144 | else | |
1145 | { | |
1146 | // in this case we'd probably have to adjust the different coordinates, but | |
1147 | // we have to find out proper contract first | |
1148 | blit = wxNullBitmap; | |
1149 | } | |
1150 | } | |
1151 | else | |
1152 | { | |
1153 | blit = wxNullBitmap; | |
1154 | } | |
1155 | } | |
1156 | ||
1157 | if ( blit.Ok() ) | |
1158 | { | |
1159 | m_graphicContext->DrawBitmap( blit, xxdest , yydest , wwdest , hhdest ); | |
1160 | } | |
1161 | ||
1162 | } | |
1163 | else | |
1164 | { | |
1165 | wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") ); | |
1166 | return false; | |
1167 | } | |
1168 | #endif | |
1169 | ||
1170 | return false; | |
1171 | } | |
1172 | ||
1173 | void wxGCDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y, | |
1174 | double angle) | |
1175 | { | |
1176 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") ); | |
1177 | ||
1178 | if ( str.length() == 0 ) | |
1179 | return; | |
1180 | if ( m_logicalFunction != wxCOPY ) | |
1181 | return; | |
1182 | ||
1183 | int drawX = LogicalToDeviceX(x); | |
1184 | int drawY = LogicalToDeviceY(y); | |
1185 | ||
1186 | m_graphicContext->DrawText( str, drawX ,drawY , DegToRad(angle )); | |
1187 | } | |
1188 | ||
1189 | void wxGCDC::DoDrawText(const wxString& str, wxCoord x, wxCoord y) | |
1190 | { | |
1191 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") ); | |
1192 | ||
1193 | if ( str.length() == 0 ) | |
1194 | return; | |
1195 | if ( m_logicalFunction != wxCOPY ) | |
1196 | return; | |
1197 | ||
1198 | int drawX = LogicalToDeviceX(x); | |
1199 | int drawY = LogicalToDeviceY(y); | |
1200 | ||
1201 | m_graphicContext->DrawText( str, drawX ,drawY); | |
1202 | } | |
1203 | ||
1204 | bool wxGCDC::CanGetTextExtent() const | |
1205 | { | |
1206 | wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") ); | |
1207 | ||
1208 | return true; | |
1209 | } | |
1210 | ||
1211 | void wxGCDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height, | |
1212 | wxCoord *descent, wxCoord *externalLeading , | |
1213 | wxFont *theFont ) const | |
1214 | { | |
1215 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") ); | |
1216 | ||
1217 | wxFont formerFont = m_font; | |
1218 | if ( theFont ) | |
1219 | { | |
1220 | m_graphicContext->SetFont( *theFont ); | |
1221 | } | |
1222 | ||
1223 | wxDouble h , d , e , w; | |
1224 | ||
1225 | m_graphicContext->GetTextExtent( str, &w, &h, &d, &e ); | |
1226 | ||
1227 | if ( height ) | |
1228 | *height = DeviceToLogicalYRel( h ); | |
1229 | if ( descent ) | |
1230 | *descent =DeviceToLogicalYRel( d); | |
1231 | if ( externalLeading ) | |
1232 | *externalLeading = DeviceToLogicalYRel( e); | |
1233 | if ( width ) | |
1234 | *width = DeviceToLogicalXRel( w ); | |
1235 | ||
1236 | if ( theFont ) | |
1237 | { | |
1238 | m_graphicContext->SetFont( m_font ); | |
1239 | } | |
1240 | } | |
1241 | ||
1242 | bool wxGCDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const | |
1243 | { | |
1244 | wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") ); | |
1245 | widths.Clear(); | |
1246 | widths.Add(0,text.Length()); | |
1247 | if ( text.IsEmpty() ) | |
1248 | return true ; | |
1249 | ||
1250 | wxArrayDouble widthsD ; | |
1251 | ||
1252 | m_graphicContext->GetPartialTextExtents( text, widthsD ); | |
1253 | for ( size_t i = 0; i < widths.GetCount(); ++i ) | |
1254 | widths[i] = DeviceToLogicalXRel( widthsD[i] + 0.5 ) ; | |
1255 | ||
1256 | return true; | |
1257 | } | |
1258 | ||
1259 | wxCoord wxGCDC::GetCharWidth(void) const | |
1260 | { | |
1261 | wxCoord width; | |
1262 | DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL ); | |
1263 | ||
1264 | return width; | |
1265 | } | |
1266 | ||
1267 | wxCoord wxGCDC::GetCharHeight(void) const | |
1268 | { | |
1269 | wxCoord height; | |
1270 | DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL ); | |
1271 | ||
1272 | return height; | |
1273 | } | |
1274 | ||
1275 | void wxGCDC::Clear(void) | |
1276 | { | |
1277 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") ); | |
1278 | // TODO | |
1279 | } | |
1280 | ||
1281 | void wxGCDC::DoGetSize(int *width, int *height) const | |
1282 | { | |
1283 | *width = 1000; | |
1284 | *height = 1000; | |
1285 | } | |
1286 | ||
1287 | void wxGCDC::DoGradientFillLinear(const wxRect& rect, | |
1288 | const wxColour& initialColour, | |
1289 | const wxColour& destColour, | |
1290 | wxDirection nDirection ) | |
1291 | { | |
1292 | wxPoint start ; | |
1293 | wxPoint end ; | |
1294 | switch( nDirection) | |
1295 | { | |
1296 | case wxWEST : | |
1297 | start = rect.GetRightBottom(); | |
1298 | start.x++; | |
1299 | end = rect.GetLeftBottom(); | |
1300 | break ; | |
1301 | case wxEAST : | |
1302 | start = rect.GetLeftBottom(); | |
1303 | end = rect.GetRightBottom(); | |
1304 | end.x++; | |
1305 | break ; | |
1306 | case wxNORTH : | |
1307 | start = rect.GetLeftBottom(); | |
1308 | start.y++; | |
1309 | end = rect.GetLeftTop(); | |
1310 | break ; | |
1311 | case wxSOUTH : | |
1312 | start = rect.GetLeftTop(); | |
1313 | end = rect.GetLeftBottom(); | |
1314 | end.y++; | |
1315 | break ; | |
1316 | } | |
1317 | ||
1318 | m_graphicContext->SetLinearGradientBrush( | |
1319 | LogicalToDeviceX(start.x),LogicalToDeviceY(start.y), | |
1320 | LogicalToDeviceX(end.x),LogicalToDeviceY(end.y), initialColour, destColour); | |
1321 | ||
1322 | wxCoord xx = LogicalToDeviceX(rect.x); | |
1323 | wxCoord yy = LogicalToDeviceY(rect.y); | |
1324 | wxDouble ww = m_signX * LogicalToDeviceXRel(rect.width); | |
1325 | wxCoord hh = m_signY * LogicalToDeviceYRel(rect.height); | |
1326 | ||
1327 | if (ww == 0 || hh == 0) | |
1328 | return; | |
1329 | ||
1330 | if (ww < 0) | |
1331 | { | |
1332 | ww = -ww; | |
1333 | xx = xx - ww; | |
1334 | } | |
1335 | if (hh < 0) | |
1336 | { | |
1337 | hh = -hh; | |
1338 | yy = yy - hh; | |
1339 | } | |
1340 | ||
1341 | m_graphicContext->SetPen(*wxTRANSPARENT_PEN); | |
1342 | m_graphicContext->DrawRectangle(xx,yy,ww,hh); | |
1343 | m_graphicContext->SetPen(m_pen); | |
1344 | } | |
1345 | ||
1346 | void wxGCDC::DoGradientFillConcentric(const wxRect& rect, | |
1347 | const wxColour& initialColour, | |
1348 | const wxColour& destColour, | |
1349 | const wxPoint& circleCenter) | |
1350 | { | |
1351 | //Radius | |
1352 | wxInt32 cx = rect.GetWidth() / 2; | |
1353 | wxInt32 cy = rect.GetHeight() / 2; | |
1354 | wxInt32 nRadius; | |
1355 | if (cx < cy) | |
1356 | nRadius = cx; | |
1357 | else | |
1358 | nRadius = cy; | |
1359 | ||
1360 | wxCoord xx = LogicalToDeviceX(rect.x); | |
1361 | wxCoord yy = LogicalToDeviceY(rect.y); | |
1362 | wxDouble ww = m_signX * LogicalToDeviceXRel(rect.width); | |
1363 | wxCoord hh = m_signY * LogicalToDeviceYRel(rect.height); | |
1364 | ||
1365 | if (ww == 0 || hh == 0) | |
1366 | return; | |
1367 | ||
1368 | if (ww < 0) | |
1369 | { | |
1370 | ww = -ww; | |
1371 | xx = xx - ww; | |
1372 | } | |
1373 | if (hh < 0) | |
1374 | { | |
1375 | hh = -hh; | |
1376 | yy = yy - hh; | |
1377 | } | |
1378 | ||
1379 | m_graphicContext->SetPen(*wxTRANSPARENT_PEN); | |
1380 | m_graphicContext->SetBrush( wxBrush( destColour) ) ; | |
1381 | m_graphicContext->DrawRectangle( xx,yy,ww,hh); | |
1382 | ||
1383 | m_graphicContext->SetRadialGradientBrush( | |
1384 | xx+LogicalToDeviceX(circleCenter.x),yy+LogicalToDeviceY(circleCenter.y), | |
1385 | xx+LogicalToDeviceX(circleCenter.x),yy+LogicalToDeviceY(circleCenter.y), | |
1386 | LogicalToDeviceXRel(nRadius), | |
1387 | initialColour,destColour); | |
1388 | ||
1389 | m_graphicContext->DrawRectangle( xx,yy,ww,hh); | |
1390 | m_graphicContext->SetPen(m_pen); | |
1391 | } | |
1392 | ||
1393 | void wxGCDC::DoDrawCheckMark(wxCoord x, wxCoord y, | |
1394 | wxCoord width, wxCoord height) | |
1395 | { | |
1396 | wxDCBase::DoDrawCheckMark(x,y,width,height); | |
1397 | } | |
1398 | ||
1399 | #endif | |
1400 |