moving path and matrix to refcounting as well, switching filling rule default to...
[wxWidgets.git] / src / common / dcgraph.cpp
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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if defined(__BORLANDC__)
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_GRAPHICS_CONTEXT
20
21 #include "wx/graphics.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/icon.h"
25 #include "wx/bitmap.h"
26 #include "wx/dcmemory.h"
27 #include "wx/region.h"
28 #endif
29
30 //-----------------------------------------------------------------------------
31 // constants
32 //-----------------------------------------------------------------------------
33
34 static const double RAD2DEG = 180.0 / M_PI;
35
36 //-----------------------------------------------------------------------------
37 // Local functions
38 //-----------------------------------------------------------------------------
39
40 static inline double DegToRad(double deg)
41 {
42 return (deg * M_PI) / 180.0;
43 }
44
45 //-----------------------------------------------------------------------------
46 // wxDC bridge class
47 //-----------------------------------------------------------------------------
48
49 #ifdef __WXMAC__
50 IMPLEMENT_DYNAMIC_CLASS(wxGCDC, wxDCBase)
51 #else
52 IMPLEMENT_DYNAMIC_CLASS(wxGCDC, wxDC)
53 #endif
54
55 wxGCDC::wxGCDC()
56 {
57 Init();
58 }
59
60 void wxGCDC::SetGraphicsContext( wxGraphicsContext* ctx )
61 {
62 delete m_graphicContext;
63 m_graphicContext = ctx;
64 }
65
66 wxGCDC::wxGCDC(const wxWindowDC& dc)
67 {
68 Init();
69 m_graphicContext = wxGraphicsContext::Create(dc);
70 m_ok = true;
71 if ( dc.GetFont().Ok())
72 m_graphicContext->SetFont( m_graphicContext->CreateFont(dc.GetFont(),dc.GetTextForeground()));
73 if ( dc.GetPen().Ok())
74 m_graphicContext->SetPen( m_graphicContext->CreatePen(dc.GetPen()));
75 if ( dc.GetBrush().Ok())
76 m_graphicContext->SetBrush( m_graphicContext->CreateBrush(dc.GetBrush()));
77 }
78
79 void wxGCDC::Init()
80 {
81 m_ok = false;
82 m_colour = true;
83 m_mm_to_pix_x = mm2pt;
84 m_mm_to_pix_y = mm2pt;
85
86 m_pen = *wxBLACK_PEN;
87 m_font = *wxNORMAL_FONT;
88 m_brush = *wxWHITE_BRUSH;
89
90 m_graphicContext = NULL;
91 }
92
93
94 wxGCDC::~wxGCDC()
95 {
96 delete m_graphicContext;
97 }
98
99 void wxGCDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool WXUNUSED(useMask) )
100 {
101 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
102 wxCHECK_RET( bmp.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
103
104 wxCoord xx = LogicalToDeviceX(x);
105 wxCoord yy = LogicalToDeviceY(y);
106 wxCoord w = bmp.GetWidth();
107 wxCoord h = bmp.GetHeight();
108 wxCoord ww = LogicalToDeviceXRel(w);
109 wxCoord hh = LogicalToDeviceYRel(h);
110
111 m_graphicContext->DrawBitmap( bmp, xx , yy , ww , hh );
112 }
113
114 void wxGCDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
115 {
116 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
117 wxCHECK_RET( icon.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
118
119 wxCoord xx = LogicalToDeviceX(x);
120 wxCoord yy = LogicalToDeviceY(y);
121 wxCoord w = icon.GetWidth();
122 wxCoord h = icon.GetHeight();
123 wxCoord ww = LogicalToDeviceXRel(w);
124 wxCoord hh = LogicalToDeviceYRel(h);
125
126 m_graphicContext->DrawIcon( icon , xx, yy, ww, hh );
127 }
128
129 void wxGCDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
130 {
131 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
132
133 wxCoord xx, yy, ww, hh;
134 xx = LogicalToDeviceX(x);
135 yy = LogicalToDeviceY(y);
136 ww = LogicalToDeviceXRel(width);
137 hh = LogicalToDeviceYRel(height);
138
139 m_graphicContext->Clip( xx, yy, ww, hh );
140 if ( m_clipping )
141 {
142 m_clipX1 = wxMax( m_clipX1, xx );
143 m_clipY1 = wxMax( m_clipY1, yy );
144 m_clipX2 = wxMin( m_clipX2, (xx + ww) );
145 m_clipY2 = wxMin( m_clipY2, (yy + hh) );
146 }
147 else
148 {
149 m_clipping = true;
150
151 m_clipX1 = xx;
152 m_clipY1 = yy;
153 m_clipX2 = xx + ww;
154 m_clipY2 = yy + hh;
155 }
156 }
157
158 void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion &region )
159 {
160 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
161
162 if (region.Empty())
163 {
164 DestroyClippingRegion();
165 return;
166 }
167
168 wxCoord x, y, w, h;
169 region.GetBox( x, y, w, h );
170 wxCoord xx, yy, ww, hh;
171 xx = LogicalToDeviceX(x);
172 yy = LogicalToDeviceY(y);
173 ww = LogicalToDeviceXRel(w);
174 hh = LogicalToDeviceYRel(h);
175
176 // if we have a scaling that we cannot map onto native regions
177 // we must use the box
178 if ( ww != w || hh != h )
179 {
180 wxGCDC::DoSetClippingRegion( x, y, w, h );
181 }
182 else
183 {
184 m_graphicContext->Clip( region );
185 if ( m_clipping )
186 {
187 m_clipX1 = wxMax( m_clipX1, xx );
188 m_clipY1 = wxMax( m_clipY1, yy );
189 m_clipX2 = wxMin( m_clipX2, (xx + ww) );
190 m_clipY2 = wxMin( m_clipY2, (yy + hh) );
191 }
192 else
193 {
194 m_clipping = true;
195
196 m_clipX1 = xx;
197 m_clipY1 = yy;
198 m_clipX2 = xx + ww;
199 m_clipY2 = yy + hh;
200 }
201 }
202 }
203
204 void wxGCDC::DestroyClippingRegion()
205 {
206 m_graphicContext->ResetClip();
207 m_graphicContext->SetPen( m_pen );
208 m_graphicContext->SetBrush( m_brush );
209
210 m_clipping = false;
211 }
212
213 void wxGCDC::DoGetSizeMM( int* width, int* height ) const
214 {
215 int w = 0, h = 0;
216
217 GetSize( &w, &h );
218 if (width)
219 *width = long( double(w) / (m_scaleX * m_mm_to_pix_x) );
220 if (height)
221 *height = long( double(h) / (m_scaleY * m_mm_to_pix_y) );
222 }
223
224 void wxGCDC::SetTextForeground( const wxColour &col )
225 {
226 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
227
228 if ( col != m_textForegroundColour )
229 {
230 m_textForegroundColour = col;
231 m_graphicContext->SetFont( m_font, m_textForegroundColour );
232 }
233 }
234
235 void wxGCDC::SetTextBackground( const wxColour &col )
236 {
237 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
238
239 m_textBackgroundColour = col;
240 }
241
242 void wxGCDC::SetMapMode( int mode )
243 {
244 switch (mode)
245 {
246 case wxMM_TWIPS:
247 SetLogicalScale( twips2mm * m_mm_to_pix_x, twips2mm * m_mm_to_pix_y );
248 break;
249
250 case wxMM_POINTS:
251 SetLogicalScale( pt2mm * m_mm_to_pix_x, pt2mm * m_mm_to_pix_y );
252 break;
253
254 case wxMM_METRIC:
255 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
256 break;
257
258 case wxMM_LOMETRIC:
259 SetLogicalScale( m_mm_to_pix_x / 10.0, m_mm_to_pix_y / 10.0 );
260 break;
261
262 case wxMM_TEXT:
263 default:
264 SetLogicalScale( 1.0, 1.0 );
265 break;
266 }
267
268 ComputeScaleAndOrigin();
269 }
270
271 void wxGCDC::SetUserScale( double x, double y )
272 {
273 // allow negative ? -> no
274
275 m_userScaleX = x;
276 m_userScaleY = y;
277 ComputeScaleAndOrigin();
278 }
279
280 void wxGCDC::SetLogicalScale( double x, double y )
281 {
282 // allow negative ?
283 m_logicalScaleX = x;
284 m_logicalScaleY = y;
285 ComputeScaleAndOrigin();
286 }
287
288 void wxGCDC::SetLogicalOrigin( wxCoord x, wxCoord y )
289 {
290 m_logicalOriginX = x * m_signX; // is this still correct ?
291 m_logicalOriginY = y * m_signY;
292 ComputeScaleAndOrigin();
293 }
294
295 void wxGCDC::SetDeviceOrigin( wxCoord x, wxCoord y )
296 {
297 m_deviceOriginX = x;
298 m_deviceOriginY = y;
299 ComputeScaleAndOrigin();
300 }
301
302 void wxGCDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
303 {
304 m_signX = (xLeftRight ? 1 : -1);
305 m_signY = (yBottomUp ? -1 : 1);
306 ComputeScaleAndOrigin();
307 }
308
309 wxSize wxGCDC::GetPPI() const
310 {
311 return wxSize(72, 72);
312 }
313
314 int wxGCDC::GetDepth() const
315 {
316 return 32;
317 }
318
319 void wxGCDC::ComputeScaleAndOrigin()
320 {
321 // CMB: copy scale to see if it changes
322 double origScaleX = m_scaleX;
323 double origScaleY = m_scaleY;
324 m_scaleX = m_logicalScaleX * m_userScaleX;
325 m_scaleY = m_logicalScaleY * m_userScaleY;
326 m_deviceOriginX = m_deviceOriginX + m_logicalOriginX;
327 m_deviceOriginY = m_deviceOriginY + m_logicalOriginY;
328
329 // CMB: if scale has changed call SetPen to recalulate the line width
330 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
331 {
332 // this is a bit artificial, but we need to force wxDC to think
333 // the pen has changed
334 wxPen pen(GetPen());
335 m_pen = wxNullPen;
336 SetPen(pen);
337 SetFont(m_font);
338 }
339 }
340
341 void wxGCDC::SetPalette( const wxPalette& WXUNUSED(palette) )
342 {
343
344 }
345
346 void wxGCDC::SetBackgroundMode( int mode )
347 {
348 m_backgroundMode = mode;
349 }
350
351 void wxGCDC::SetFont( const wxFont &font )
352 {
353 m_font = font;
354 if ( m_graphicContext )
355 {
356 wxFont f = font;
357 if ( f.Ok() )
358 f.SetPointSize( LogicalToDeviceYRel(font.GetPointSize()));
359 m_graphicContext->SetFont( f, m_textForegroundColour );
360 }
361 }
362
363 void wxGCDC::SetPen( const wxPen &pen )
364 {
365 if ( m_pen == pen )
366 return;
367
368 m_pen = pen;
369 if ( m_graphicContext )
370 {
371 if ( m_pen.GetStyle() == wxSOLID || m_pen.GetStyle() == wxTRANSPARENT )
372 {
373 m_graphicContext->SetPen( m_pen );
374 }
375 else
376 {
377 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
378 // eg when using a wxScrollWindow and scrolling around
379 int origX = LogicalToDeviceX( 0 );
380 int origY = LogicalToDeviceY( 0 );
381 m_graphicContext->Translate( origX , origY );
382 m_graphicContext->SetPen( m_pen );
383 m_graphicContext->Translate( -origX , -origY );
384 }
385 }
386 }
387
388 void wxGCDC::SetBrush( const wxBrush &brush )
389 {
390 if (m_brush == brush)
391 return;
392
393 m_brush = brush;
394 if ( m_graphicContext )
395 {
396 if ( brush.GetStyle() == wxSOLID || brush.GetStyle() == wxTRANSPARENT )
397 {
398 m_graphicContext->SetBrush( m_brush );
399 }
400 else
401 {
402 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
403 // eg when using a wxScrollWindow and scrolling around
404 // TODO on MSW / GDIPlus this still occurs with hatched brushes
405 int origX = LogicalToDeviceX(0);
406 int origY = LogicalToDeviceY(0);
407 m_graphicContext->Translate( origX , origY );
408 m_graphicContext->SetBrush( m_brush );
409 m_graphicContext->Translate( -origX , -origY );
410 }
411 }
412 }
413
414 void wxGCDC::SetBackground( const wxBrush &brush )
415 {
416 if (m_backgroundBrush == brush)
417 return;
418
419 m_backgroundBrush = brush;
420 if (!m_backgroundBrush.Ok())
421 return;
422 }
423
424 void wxGCDC::SetLogicalFunction( int function )
425 {
426 if (m_logicalFunction == function)
427 return;
428
429 m_logicalFunction = function;
430 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
431
432 CGContextRef cgContext = ((wxCairoContext*)(m_graphicContext))->GetNativeContext();
433 if ( m_logicalFunction == wxCOPY )
434 CGContextSetBlendMode( cgContext, kCGBlendModeNormal );
435 else if ( m_logicalFunction == wxINVERT )
436 CGContextSetBlendMode( cgContext, kCGBlendModeExclusion );
437 else
438 CGContextSetBlendMode( cgContext, kCGBlendModeNormal );
439 #endif
440
441 }
442
443 bool wxGCDC::DoFloodFill(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
444 const wxColour& WXUNUSED(col), int WXUNUSED(style))
445 {
446 return false;
447 }
448
449 bool wxGCDC::DoGetPixel( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour *WXUNUSED(col) ) const
450 {
451 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
452 return false;
453 }
454
455 void wxGCDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
456 {
457 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
458
459 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
460
461 if ( m_logicalFunction != wxCOPY )
462 return;
463 #endif
464
465 wxCoord xx1 = LogicalToDeviceX(x1);
466 wxCoord yy1 = LogicalToDeviceY(y1);
467 wxCoord xx2 = LogicalToDeviceX(x2);
468 wxCoord yy2 = LogicalToDeviceY(y2);
469
470 m_graphicContext->StrokeLine(xx1,yy1,xx2,yy2);
471
472 CalcBoundingBox(x1, y1);
473 CalcBoundingBox(x2, y2);
474 }
475
476 void wxGCDC::DoCrossHair( wxCoord x, wxCoord y )
477 {
478 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
479
480 if ( m_logicalFunction != wxCOPY )
481 return;
482
483 int w = 0, h = 0;
484
485 GetSize( &w, &h );
486
487 wxCoord xx = LogicalToDeviceX(x);
488 wxCoord yy = LogicalToDeviceY(y);
489 wxCoord xw = LogicalToDeviceX(w);
490 wxCoord x0 = LogicalToDeviceX(0);
491 wxCoord y0 = LogicalToDeviceY(0);
492 wxCoord yh = LogicalToDeviceY(h);
493
494 m_graphicContext->StrokeLine(x0,yy,xw,yy);
495 m_graphicContext->StrokeLine(xx,y0,xx,yh);
496
497 CalcBoundingBox(x0, y0);
498 CalcBoundingBox(x0+xw, y0+yh);
499 }
500
501 void wxGCDC::DoDrawArc( wxCoord x1, wxCoord y1,
502 wxCoord x2, wxCoord y2,
503 wxCoord xc, wxCoord yc )
504 {
505 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
506
507 if ( m_logicalFunction != wxCOPY )
508 return;
509
510 wxCoord xx1 = LogicalToDeviceX(x1);
511 wxCoord yy1 = LogicalToDeviceY(y1);
512 wxCoord xx2 = LogicalToDeviceX(x2);
513 wxCoord yy2 = LogicalToDeviceY(y2);
514 wxCoord xxc = LogicalToDeviceX(xc);
515 wxCoord yyc = LogicalToDeviceY(yc);
516
517 double dx = xx1 - xxc;
518 double dy = yy1 - yyc;
519 double radius = sqrt((double)(dx * dx + dy * dy));
520 wxCoord rad = (wxCoord)radius;
521 double sa, ea;
522 if (xx1 == xx2 && yy1 == yy2)
523 {
524 sa = 0.0;
525 ea = 360.0;
526 }
527 else if (radius == 0.0)
528 {
529 sa = ea = 0.0;
530 }
531 else
532 {
533 sa = (xx1 - xxc == 0) ?
534 (yy1 - yyc < 0) ? 90.0 : -90.0 :
535 -atan2(double(yy1 - yyc), double(xx1 - xxc)) * RAD2DEG;
536 ea = (xx2 - xxc == 0) ?
537 (yy2 - yyc < 0) ? 90.0 : -90.0 :
538 -atan2(double(yy2 - yyc), double(xx2 - xxc)) * RAD2DEG;
539 }
540
541 bool fill = m_brush.GetStyle() != wxTRANSPARENT;
542
543 wxGraphicsPath path = m_graphicContext->CreatePath();
544 if ( fill && ((x1!=x2)||(y1!=y2)) )
545 path.MoveToPoint( xxc, yyc );
546 path.AddArc( xxc, yyc , rad , DegToRad(sa) , DegToRad(ea), false );
547 if ( fill && ((x1!=x2)||(y1!=y2)) )
548 path.AddLineToPoint( xxc, yyc );
549 m_graphicContext->DrawPath(path);
550 }
551
552 void wxGCDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
553 double sa, double ea )
554 {
555 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
556
557 if ( m_logicalFunction != wxCOPY )
558 return;
559
560 wxCoord xx = LogicalToDeviceX(x);
561 wxCoord yy = LogicalToDeviceY(y);
562 wxCoord ww = m_signX * LogicalToDeviceXRel(w);
563 wxCoord hh = m_signY * LogicalToDeviceYRel(h);
564
565 // handle -ve width and/or height
566 if (ww < 0)
567 {
568 ww = -ww;
569 xx = xx - ww;
570 }
571 if (hh < 0)
572 {
573 hh = -hh;
574 yy = yy - hh;
575 }
576
577 bool fill = m_brush.GetStyle() != wxTRANSPARENT;
578
579 wxGraphicsPath path = m_graphicContext->CreatePath();
580 m_graphicContext->PushState();
581 m_graphicContext->Translate(xx+ww/2,yy+hh/2);
582 wxDouble factor = ((wxDouble) ww) / hh;
583 m_graphicContext->Scale( factor , 1.0);
584 if ( fill && (sa!=ea) )
585 path.MoveToPoint(0,0);
586 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
587 // get clockwise angles
588 path.AddArc( 0, 0, hh/2 , DegToRad(-sa) , DegToRad(-ea), sa > ea );
589 if ( fill && (sa!=ea) )
590 path.AddLineToPoint(0,0);
591 m_graphicContext->DrawPath( path );
592 m_graphicContext->PopState();
593 }
594
595 void wxGCDC::DoDrawPoint( wxCoord x, wxCoord y )
596 {
597 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
598
599 DoDrawLine( x , y , x + 1 , y + 1 );
600 }
601
602 void wxGCDC::DoDrawLines(int n, wxPoint points[],
603 wxCoord xoffset, wxCoord yoffset)
604 {
605 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
606
607 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
608
609 if ( m_logicalFunction != wxCOPY )
610 return;
611 #endif
612
613 wxPoint2DDouble* pointsD = new wxPoint2DDouble[n];
614 for( int i = 0; i < n; ++i)
615 {
616 pointsD[i].m_x = LogicalToDeviceX(points[i].x + xoffset);
617 pointsD[i].m_y = LogicalToDeviceY(points[i].y + yoffset);
618 }
619
620 m_graphicContext->StrokeLines( n , pointsD);
621 delete[] pointsD;
622 }
623
624 #if wxUSE_SPLINES
625 void wxGCDC::DoDrawSpline(wxList *points)
626 {
627 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
628
629 if ( m_logicalFunction != wxCOPY )
630 return;
631
632 wxGraphicsPath path = m_graphicContext->CreatePath();
633
634 wxList::compatibility_iterator node = points->GetFirst();
635 if (node == wxList::compatibility_iterator())
636 // empty list
637 return;
638
639 wxPoint *p = (wxPoint *)node->GetData();
640
641 wxCoord x1 = p->x;
642 wxCoord y1 = p->y;
643
644 node = node->GetNext();
645 p = (wxPoint *)node->GetData();
646
647 wxCoord x2 = p->x;
648 wxCoord y2 = p->y;
649 wxCoord cx1 = ( x1 + x2 ) / 2;
650 wxCoord cy1 = ( y1 + y2 ) / 2;
651
652 path.MoveToPoint( LogicalToDeviceX( x1 ) , LogicalToDeviceY( y1 ) );
653 path.AddLineToPoint( LogicalToDeviceX( cx1 ) , LogicalToDeviceY( cy1 ) );
654 #if !wxUSE_STL
655
656 while ((node = node->GetNext()) != NULL)
657 #else
658
659 while ((node = node->GetNext()))
660 #endif // !wxUSE_STL
661
662 {
663 p = (wxPoint *)node->GetData();
664 x1 = x2;
665 y1 = y2;
666 x2 = p->x;
667 y2 = p->y;
668 wxCoord cx4 = (x1 + x2) / 2;
669 wxCoord cy4 = (y1 + y2) / 2;
670
671 path.AddQuadCurveToPoint(
672 LogicalToDeviceX( x1 ) , LogicalToDeviceY( y1 ) ,
673 LogicalToDeviceX( cx4 ) , LogicalToDeviceY( cy4 ) );
674
675 cx1 = cx4;
676 cy1 = cy4;
677 }
678
679 path.AddLineToPoint( LogicalToDeviceX( x2 ) , LogicalToDeviceY( y2 ) );
680
681 m_graphicContext->StrokePath( path );
682 }
683 #endif // wxUSE_SPLINES
684
685 void wxGCDC::DoDrawPolygon( int n, wxPoint points[],
686 wxCoord xoffset, wxCoord yoffset,
687 int fillStyle )
688 {
689 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
690
691 if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) )
692 return;
693 if ( m_logicalFunction != wxCOPY )
694 return;
695
696 bool closeIt = false;
697 if (points[n-1] != points[0])
698 closeIt = true;
699
700 wxPoint2DDouble* pointsD = new wxPoint2DDouble[n+(closeIt?1:0)];
701 for( int i = 0; i < n; ++i)
702 {
703 pointsD[i].m_x = LogicalToDeviceX(points[i].x + xoffset);
704 pointsD[i].m_y = LogicalToDeviceY(points[i].y + yoffset);
705 }
706 if ( closeIt )
707 pointsD[n] = pointsD[0];
708
709 m_graphicContext->DrawLines( n+(closeIt?1:0) , pointsD, fillStyle);
710 delete[] pointsD;
711 }
712
713 void wxGCDC::DoDrawPolyPolygon(int n,
714 int count[],
715 wxPoint points[],
716 wxCoord xoffset,
717 wxCoord yoffset,
718 int fillStyle)
719 {
720 wxASSERT(n > 1);
721 wxGraphicsPath path = m_graphicContext->CreatePath();
722
723 int i = 0;
724 for ( int j = 0; j < n; ++j)
725 {
726 wxPoint start = points[i];
727 path.MoveToPoint(LogicalToDeviceX(start.x+ xoffset), LogicalToDeviceY(start.y+ yoffset));
728 ++i;
729 int l = count[j];
730 for ( int k = 1; k < l; ++k)
731 {
732 path.AddLineToPoint( LogicalToDeviceX(points[i].x+ xoffset), LogicalToDeviceY(points[i].y+ yoffset));
733 ++i;
734 }
735 // close the polygon
736 if ( start != points[i-1])
737 path.AddLineToPoint( LogicalToDeviceX(start.x+ xoffset), LogicalToDeviceY(start.y+ yoffset));
738 }
739 m_graphicContext->DrawPath( path , fillStyle);
740 }
741
742 void wxGCDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
743 {
744 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
745
746 if ( m_logicalFunction != wxCOPY )
747 return;
748
749 wxCoord xx = LogicalToDeviceX(x);
750 wxCoord yy = LogicalToDeviceY(y);
751 wxCoord ww = m_signX * LogicalToDeviceXRel(width);
752 wxCoord hh = m_signY * LogicalToDeviceYRel(height);
753
754 // CMB: draw nothing if transformed w or h is 0
755 if (ww == 0 || hh == 0)
756 return;
757
758 // CMB: handle -ve width and/or height
759 if (ww < 0)
760 {
761 ww = -ww;
762 xx = xx - ww;
763 }
764 if (hh < 0)
765 {
766 hh = -hh;
767 yy = yy - hh;
768 }
769 if ( m_graphicContext->ShouldOffset() )
770 {
771 // if we are offsetting the entire rectangle is moved 0.5, so the
772 // border line gets off by 1
773 ww -= 1;
774 hh -= 1;
775 }
776 m_graphicContext->DrawRectangle( xx,yy,ww,hh);
777 }
778
779 void wxGCDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
780 wxCoord width, wxCoord height,
781 double radius)
782 {
783 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
784
785 if ( m_logicalFunction != wxCOPY )
786 return;
787
788 if (radius < 0.0)
789 radius = - radius * ((width < height) ? width : height);
790 wxCoord xx = LogicalToDeviceX(x);
791 wxCoord yy = LogicalToDeviceY(y);
792 wxCoord ww = m_signX * LogicalToDeviceXRel(width);
793 wxCoord hh = m_signY * LogicalToDeviceYRel(height);
794
795 // CMB: draw nothing if transformed w or h is 0
796 if (ww == 0 || hh == 0)
797 return;
798
799 // CMB: handle -ve width and/or height
800 if (ww < 0)
801 {
802 ww = -ww;
803 xx = xx - ww;
804 }
805 if (hh < 0)
806 {
807 hh = -hh;
808 yy = yy - hh;
809 }
810
811 m_graphicContext->DrawRoundedRectangle( xx,yy,ww,hh,radius);
812 }
813
814 void wxGCDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
815 {
816 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
817
818 if ( m_logicalFunction != wxCOPY )
819 return;
820
821 wxDouble xx = LogicalToDeviceX(x);
822 wxDouble yy = LogicalToDeviceY(y);
823 wxDouble ww = m_signX * LogicalToDeviceXRel(width);
824 wxDouble hh = m_signY * LogicalToDeviceYRel(height);
825
826 // CMB: draw nothing if transformed w or h is 0
827 if (ww == 0 || hh == 0)
828 return;
829
830 // CMB: handle -ve width and/or height
831 if (ww < 0)
832 {
833 ww = -ww;
834 xx = xx - ww;
835 }
836 if (hh < 0)
837 {
838 hh = -hh;
839 yy = yy - hh;
840 }
841
842 m_graphicContext->DrawEllipse(xx,yy,ww,hh);
843 }
844
845 bool wxGCDC::CanDrawBitmap() const
846 {
847 return true;
848 }
849
850 bool wxGCDC::DoBlit(
851 wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
852 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool WXUNUSED(useMask),
853 wxCoord xsrcMask, wxCoord ysrcMask )
854 {
855 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
856 wxCHECK_MSG( source->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
857
858 if ( logical_func == wxNO_OP )
859 return true;
860
861 if (xsrcMask == -1 && ysrcMask == -1)
862 {
863 xsrcMask = xsrc;
864 ysrcMask = ysrc;
865 }
866
867 wxCoord yysrc = source-> LogicalToDeviceY(ysrc);
868 wxCoord xxsrc = source-> LogicalToDeviceX(xsrc);
869 wxCoord wwsrc = source-> LogicalToDeviceXRel(width);
870 wxCoord hhsrc = source-> LogicalToDeviceYRel(height);
871
872 wxCoord yydest = LogicalToDeviceY(ydest);
873 wxCoord xxdest = LogicalToDeviceX(xdest);
874 wxCoord wwdest = LogicalToDeviceXRel(width);
875 wxCoord hhdest = LogicalToDeviceYRel(height);
876
877 wxMemoryDC* memdc = wxDynamicCast(source,wxMemoryDC);
878 if ( memdc && logical_func == wxCOPY )
879 {
880 wxBitmap blit = memdc->GetSelectedBitmap();
881
882 wxASSERT_MSG( blit.Ok() , wxT("Invalid bitmap for blitting") );
883
884 wxCoord bmpwidth = blit.GetWidth();
885 wxCoord bmpheight = blit.GetHeight();
886
887 if ( xxsrc != 0 || yysrc != 0 || bmpwidth != wwsrc || bmpheight != hhsrc )
888 {
889 wwsrc = wxMin( wwsrc , bmpwidth - xxsrc );
890 hhsrc = wxMin( hhsrc , bmpheight - yysrc );
891 if ( wwsrc > 0 && hhsrc > 0 )
892 {
893 if ( xxsrc >= 0 && yysrc >= 0 )
894 {
895 wxRect subrect( xxsrc, yysrc, wwsrc , hhsrc );
896 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
897 blit = blit.GetSubBitmap( subrect );
898 }
899 else
900 {
901 // in this case we'd probably have to adjust the different coordinates, but
902 // we have to find out proper contract first
903 blit = wxNullBitmap;
904 }
905 }
906 else
907 {
908 blit = wxNullBitmap;
909 }
910 }
911
912 if ( blit.Ok() )
913 {
914 m_graphicContext->DrawBitmap( blit, xxdest , yydest , wwdest , hhdest );
915 }
916 }
917 else
918 {
919 wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts, and only with wxCOPY logical operation.") );
920 return false;
921 }
922
923 return true;
924 }
925
926 void wxGCDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
927 double angle)
928 {
929 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
930
931 if ( str.length() == 0 )
932 return;
933 if ( m_logicalFunction != wxCOPY )
934 return;
935
936 int drawX = LogicalToDeviceX(x);
937 int drawY = LogicalToDeviceY(y);
938
939 m_graphicContext->DrawText( str, drawX ,drawY , DegToRad(angle ));
940 }
941
942 void wxGCDC::DoDrawText(const wxString& str, wxCoord x, wxCoord y)
943 {
944 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
945
946 if ( str.length() == 0 )
947 return;
948 if ( m_logicalFunction != wxCOPY )
949 return;
950
951 int drawX = LogicalToDeviceX(x);
952 int drawY = LogicalToDeviceY(y);
953
954 m_graphicContext->DrawText( str, drawX ,drawY);
955 }
956
957 bool wxGCDC::CanGetTextExtent() const
958 {
959 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
960
961 return true;
962 }
963
964 void wxGCDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height,
965 wxCoord *descent, wxCoord *externalLeading ,
966 wxFont *theFont ) const
967 {
968 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
969
970 if ( theFont )
971 {
972 m_graphicContext->SetFont( *theFont, m_textForegroundColour );
973 }
974
975 wxDouble h , d , e , w;
976
977 m_graphicContext->GetTextExtent( str, &w, &h, &d, &e );
978
979 if ( height )
980 *height = DeviceToLogicalYRel((wxCoord)h);
981 if ( descent )
982 *descent = DeviceToLogicalYRel((wxCoord)d);
983 if ( externalLeading )
984 *externalLeading = DeviceToLogicalYRel((wxCoord)e);
985 if ( width )
986 *width = DeviceToLogicalXRel((wxCoord)w);
987
988 if ( theFont )
989 {
990 m_graphicContext->SetFont( m_font, m_textForegroundColour );
991 }
992 }
993
994 bool wxGCDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
995 {
996 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
997 widths.Clear();
998 widths.Add(0,text.Length());
999 if ( text.IsEmpty() )
1000 return true;
1001
1002 wxArrayDouble widthsD;
1003
1004 m_graphicContext->GetPartialTextExtents( text, widthsD );
1005 for ( size_t i = 0; i < widths.GetCount(); ++i )
1006 widths[i] = DeviceToLogicalXRel((wxCoord)(widthsD[i] + 0.5));
1007
1008 return true;
1009 }
1010
1011 wxCoord wxGCDC::GetCharWidth(void) const
1012 {
1013 wxCoord width;
1014 DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL );
1015
1016 return width;
1017 }
1018
1019 wxCoord wxGCDC::GetCharHeight(void) const
1020 {
1021 wxCoord height;
1022 DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL );
1023
1024 return height;
1025 }
1026
1027 void wxGCDC::Clear(void)
1028 {
1029 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1030 // TODO better implementation / incorporate size info into wxGCDC or context
1031 m_graphicContext->SetBrush( m_backgroundBrush );
1032 wxPen p = *wxTRANSPARENT_PEN;
1033 m_graphicContext->SetPen( p );
1034 DoDrawRectangle( 0, 0, 32000 , 32000 );
1035 m_graphicContext->SetPen( m_pen );
1036 m_graphicContext->SetBrush( m_brush );
1037 }
1038
1039 void wxGCDC::DoGetSize(int *width, int *height) const
1040 {
1041 *width = 1000;
1042 *height = 1000;
1043 }
1044
1045 void wxGCDC::DoGradientFillLinear(const wxRect& rect,
1046 const wxColour& initialColour,
1047 const wxColour& destColour,
1048 wxDirection nDirection )
1049 {
1050 wxPoint start;
1051 wxPoint end;
1052 switch( nDirection)
1053 {
1054 case wxWEST :
1055 start = rect.GetRightBottom();
1056 start.x++;
1057 end = rect.GetLeftBottom();
1058 break;
1059 case wxEAST :
1060 start = rect.GetLeftBottom();
1061 end = rect.GetRightBottom();
1062 end.x++;
1063 break;
1064 case wxNORTH :
1065 start = rect.GetLeftBottom();
1066 start.y++;
1067 end = rect.GetLeftTop();
1068 break;
1069 case wxSOUTH :
1070 start = rect.GetLeftTop();
1071 end = rect.GetLeftBottom();
1072 end.y++;
1073 break;
1074 default :
1075 break;
1076 }
1077
1078 m_graphicContext->SetBrush( m_graphicContext->CreateLinearGradientBrush(
1079 LogicalToDeviceX(start.x),LogicalToDeviceY(start.y),
1080 LogicalToDeviceX(end.x),LogicalToDeviceY(end.y), initialColour, destColour));
1081
1082 wxDouble xx = LogicalToDeviceX(rect.x);
1083 wxDouble yy = LogicalToDeviceY(rect.y);
1084 wxDouble ww = m_signX * LogicalToDeviceXRel(rect.width);
1085 wxDouble hh = m_signY * LogicalToDeviceYRel(rect.height);
1086
1087 if (ww == 0 || hh == 0)
1088 return;
1089
1090 if (ww < 0)
1091 {
1092 ww = -ww;
1093 xx = xx - ww;
1094 }
1095 if (hh < 0)
1096 {
1097 hh = -hh;
1098 yy = yy - hh;
1099 }
1100
1101 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
1102 m_graphicContext->DrawRectangle(xx,yy,ww,hh);
1103 m_graphicContext->SetPen(m_pen);
1104 }
1105
1106 void wxGCDC::DoGradientFillConcentric(const wxRect& rect,
1107 const wxColour& initialColour,
1108 const wxColour& destColour,
1109 const wxPoint& circleCenter)
1110 {
1111 //Radius
1112 wxInt32 cx = rect.GetWidth() / 2;
1113 wxInt32 cy = rect.GetHeight() / 2;
1114 wxInt32 nRadius;
1115 if (cx < cy)
1116 nRadius = cx;
1117 else
1118 nRadius = cy;
1119
1120 wxDouble xx = LogicalToDeviceX(rect.x);
1121 wxDouble yy = LogicalToDeviceY(rect.y);
1122 wxDouble ww = m_signX * LogicalToDeviceXRel(rect.width);
1123 wxDouble hh = m_signY * LogicalToDeviceYRel(rect.height);
1124
1125 if (ww == 0 || hh == 0)
1126 return;
1127
1128 if (ww < 0)
1129 {
1130 ww = -ww;
1131 xx = xx - ww;
1132 }
1133 if (hh < 0)
1134 {
1135 hh = -hh;
1136 yy = yy - hh;
1137 }
1138
1139 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
1140 m_graphicContext->SetBrush( wxBrush( destColour) );
1141 m_graphicContext->DrawRectangle( xx,yy,ww,hh);
1142
1143 m_graphicContext->SetBrush( m_graphicContext->CreateRadialGradientBrush(
1144 xx+LogicalToDeviceX(circleCenter.x),yy+LogicalToDeviceY(circleCenter.y),
1145 xx+LogicalToDeviceX(circleCenter.x),yy+LogicalToDeviceY(circleCenter.y),
1146 LogicalToDeviceXRel(nRadius),
1147 initialColour,destColour));
1148
1149 m_graphicContext->DrawRectangle( xx,yy,ww,hh);
1150 m_graphicContext->SetPen(m_pen);
1151 }
1152
1153 void wxGCDC::DoDrawCheckMark(wxCoord x, wxCoord y,
1154 wxCoord width, wxCoord height)
1155 {
1156 wxDCBase::DoDrawCheckMark(x,y,width,height);
1157 }
1158
1159 #endif // wxUSE_GRAPHICS_CONTEXT