fixing warning about missing virt destructor
[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 #include "wx/dcgraph.h"
23
24 #ifndef WX_PRECOMP
25 #include "wx/icon.h"
26 #include "wx/bitmap.h"
27 #include "wx/dcmemory.h"
28 #include "wx/region.h"
29 #endif
30
31 #ifdef __WXMAC__
32 #include "wx/mac/private.h"
33 #endif
34 //-----------------------------------------------------------------------------
35 // constants
36 //-----------------------------------------------------------------------------
37
38 static const double RAD2DEG = 180.0 / M_PI;
39
40 //-----------------------------------------------------------------------------
41 // Local functions
42 //-----------------------------------------------------------------------------
43
44 static inline double DegToRad(double deg)
45 {
46 return (deg * M_PI) / 180.0;
47 }
48
49 //-----------------------------------------------------------------------------
50 // wxDC bridge class
51 //-----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS(wxGCDC, wxDC)
54
55 wxGCDC::wxGCDC(const wxWindowDC& dc) :
56 wxDC( new wxGCDCImpl( this, dc ) )
57 {
58 }
59
60 wxGCDC::wxGCDC( const wxMemoryDC& dc) :
61 wxDC( new wxGCDCImpl( this, dc ) )
62 {
63 }
64
65 wxGCDC::wxGCDC() :
66 wxDC( new wxGCDCImpl( this ) )
67 {
68 }
69
70 wxGCDC::~wxGCDC()
71 {
72 }
73
74 wxGraphicsContext* wxGCDC::GetGraphicsContext()
75 {
76 if (!m_pimpl) return NULL;
77 wxGCDCImpl *gc_impl = (wxGCDCImpl*) m_pimpl;
78 return gc_impl->GetGraphicsContext();
79 }
80
81 void wxGCDC::SetGraphicsContext( wxGraphicsContext* ctx )
82 {
83 if (!m_pimpl) return;
84 wxGCDCImpl *gc_impl = (wxGCDCImpl*) m_pimpl;
85 gc_impl->SetGraphicsContext( ctx );
86 }
87
88 IMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl, wxDCImpl)
89
90 wxGCDCImpl::wxGCDCImpl( wxDC *owner ) :
91 wxDCImpl( owner )
92 {
93 Init();
94 }
95
96 void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext* ctx )
97 {
98 delete m_graphicContext;
99 m_graphicContext = ctx;
100 if ( m_graphicContext )
101 {
102 m_matrixOriginal = m_graphicContext->GetTransform();
103 m_ok = true;
104 // apply the stored transformations to the passed in context
105 ComputeScaleAndOrigin();
106 m_graphicContext->SetFont( m_font , m_textForegroundColour );
107 m_graphicContext->SetPen( m_pen );
108 m_graphicContext->SetBrush( m_brush);
109 }
110 }
111
112 wxGCDCImpl::wxGCDCImpl( wxDC *owner, const wxWindowDC& dc ) :
113 wxDCImpl( owner )
114 {
115 Init();
116 SetGraphicsContext( wxGraphicsContext::Create(dc) );
117 }
118
119 wxGCDCImpl::wxGCDCImpl( wxDC *owner, const wxMemoryDC& dc ) :
120 wxDCImpl( owner )
121 {
122 Init();
123 SetGraphicsContext( wxGraphicsContext::Create(dc) );
124 }
125
126 void wxGCDCImpl::Init()
127 {
128 m_ok = false;
129 m_colour = true;
130 m_mm_to_pix_x = mm2pt;
131 m_mm_to_pix_y = mm2pt;
132
133 m_pen = *wxBLACK_PEN;
134 m_font = *wxNORMAL_FONT;
135 m_brush = *wxWHITE_BRUSH;
136
137 m_graphicContext = NULL;
138 m_logicalFunctionSupported = true;
139 }
140
141
142 wxGCDCImpl::~wxGCDCImpl()
143 {
144 delete m_graphicContext;
145 }
146
147 void wxGCDCImpl::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool WXUNUSED(useMask) )
148 {
149 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
150 wxCHECK_RET( bmp.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
151
152 if ( bmp.GetDepth() == 1 )
153 {
154 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
155 m_graphicContext->SetBrush( wxBrush( m_textBackgroundColour , wxSOLID ) );
156 m_graphicContext->DrawRectangle( x , y , bmp.GetWidth() , bmp.GetHeight() );
157 m_graphicContext->SetBrush( wxBrush( m_textForegroundColour , wxSOLID ) );
158 m_graphicContext->DrawBitmap( bmp, x , y , bmp.GetWidth() , bmp.GetHeight() );
159 m_graphicContext->SetBrush( m_graphicContext->CreateBrush(m_brush));
160 m_graphicContext->SetPen( m_graphicContext->CreatePen(m_pen));
161 }
162 else
163 m_graphicContext->DrawBitmap( bmp, x , y , bmp.GetWidth() , bmp.GetHeight() );
164 }
165
166 void wxGCDCImpl::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
167 {
168 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
169 wxCHECK_RET( icon.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
170
171 wxCoord w = icon.GetWidth();
172 wxCoord h = icon.GetHeight();
173
174 m_graphicContext->DrawIcon( icon , x, y, w, h );
175 }
176
177 bool wxGCDCImpl::StartDoc( const wxString& WXUNUSED(message) )
178 {
179 return false;
180 }
181
182 void wxGCDCImpl::EndDoc()
183 {
184 }
185
186 void wxGCDCImpl::StartPage()
187 {
188 }
189
190 void wxGCDCImpl::EndPage()
191 {
192 }
193
194 void wxGCDCImpl::Flush()
195 {
196 #ifdef __WXMAC__
197 CGContextFlush( (CGContextRef) m_graphicContext->GetNativeContext() );
198 #endif
199 }
200
201 void wxGCDCImpl::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord w, wxCoord h )
202 {
203 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
204
205 m_graphicContext->Clip( x, y, w, h );
206 if ( m_clipping )
207 {
208 m_clipX1 = wxMax( m_clipX1, x );
209 m_clipY1 = wxMax( m_clipY1, y );
210 m_clipX2 = wxMin( m_clipX2, (x + w) );
211 m_clipY2 = wxMin( m_clipY2, (y + h) );
212 }
213 else
214 {
215 m_clipping = true;
216
217 m_clipX1 = x;
218 m_clipY1 = y;
219 m_clipX2 = x + w;
220 m_clipY2 = y + h;
221 }
222 }
223
224 void wxGCDCImpl::DoSetClippingRegionAsRegion( const wxRegion &region )
225 {
226 // region is in device coordinates
227 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
228
229 if (region.Empty())
230 {
231 //DestroyClippingRegion();
232 return;
233 }
234
235 wxRegion logRegion( region );
236 wxCoord x, y, w, h;
237
238 logRegion.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
239 logRegion.GetBox( x, y, w, h );
240
241 m_graphicContext->Clip( logRegion );
242 if ( m_clipping )
243 {
244 m_clipX1 = wxMax( m_clipX1, x );
245 m_clipY1 = wxMax( m_clipY1, y );
246 m_clipX2 = wxMin( m_clipX2, (x + w) );
247 m_clipY2 = wxMin( m_clipY2, (y + h) );
248 }
249 else
250 {
251 m_clipping = true;
252
253 m_clipX1 = x;
254 m_clipY1 = y;
255 m_clipX2 = x + w;
256 m_clipY2 = y + h;
257 }
258 }
259
260 void wxGCDCImpl::DestroyClippingRegion()
261 {
262 m_graphicContext->ResetClip();
263 // currently the clip eg of a window extends to the area between the scrollbars
264 // so we must explicitely make sure it only covers the area we want it to draw
265 int width, height ;
266 GetOwner()->GetSize( &width , &height ) ;
267 m_graphicContext->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width), DeviceToLogicalYRel(height) );
268
269 m_graphicContext->SetPen( m_pen );
270 m_graphicContext->SetBrush( m_brush );
271
272 m_clipping = false;
273 }
274
275 void wxGCDCImpl::DoGetSizeMM( int* width, int* height ) const
276 {
277 int w = 0, h = 0;
278
279 GetOwner()->GetSize( &w, &h );
280 if (width)
281 *width = long( double(w) / (m_scaleX * m_mm_to_pix_x) );
282 if (height)
283 *height = long( double(h) / (m_scaleY * m_mm_to_pix_y) );
284 }
285
286 void wxGCDCImpl::SetTextForeground( const wxColour &col )
287 {
288 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
289
290 if ( col != m_textForegroundColour )
291 {
292 m_textForegroundColour = col;
293 m_graphicContext->SetFont( m_font, m_textForegroundColour );
294 }
295 }
296
297 void wxGCDCImpl::SetTextBackground( const wxColour &col )
298 {
299 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
300
301 m_textBackgroundColour = col;
302 }
303
304 void wxGCDCImpl::SetMapMode( int mode )
305 {
306 switch (mode)
307 {
308 case wxMM_TWIPS:
309 SetLogicalScale( twips2mm * m_mm_to_pix_x, twips2mm * m_mm_to_pix_y );
310 break;
311
312 case wxMM_POINTS:
313 SetLogicalScale( pt2mm * m_mm_to_pix_x, pt2mm * m_mm_to_pix_y );
314 break;
315
316 case wxMM_METRIC:
317 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
318 break;
319
320 case wxMM_LOMETRIC:
321 SetLogicalScale( m_mm_to_pix_x / 10.0, m_mm_to_pix_y / 10.0 );
322 break;
323
324 case wxMM_TEXT:
325 default:
326 SetLogicalScale( 1.0, 1.0 );
327 break;
328 }
329
330 ComputeScaleAndOrigin();
331 }
332
333 wxSize wxGCDCImpl::GetPPI() const
334 {
335 return wxSize(72, 72);
336 }
337
338 int wxGCDCImpl::GetDepth() const
339 {
340 return 32;
341 }
342
343 void wxGCDCImpl::ComputeScaleAndOrigin()
344 {
345 wxDCImpl::ComputeScaleAndOrigin();
346
347 if ( m_graphicContext )
348 {
349 m_matrixCurrent = m_graphicContext->CreateMatrix();
350 m_matrixCurrent.Translate( m_deviceOriginX, m_deviceOriginY );
351 m_matrixCurrent.Scale( m_scaleX, m_scaleY );
352 // the logical origin sets the origin to have new coordinates
353 m_matrixCurrent.Translate( -m_logicalOriginX, -m_logicalOriginY );
354
355 m_graphicContext->SetTransform( m_matrixOriginal );
356 m_graphicContext->ConcatTransform( m_matrixCurrent );
357 }
358 }
359
360 void wxGCDCImpl::SetPalette( const wxPalette& WXUNUSED(palette) )
361 {
362
363 }
364
365 void wxGCDCImpl::SetBackgroundMode( int mode )
366 {
367 m_backgroundMode = mode;
368 }
369
370 void wxGCDCImpl::SetFont( const wxFont &font )
371 {
372 m_font = font;
373 if ( m_graphicContext )
374 {
375 wxFont f = font;
376 if ( f.IsOk() )
377 f.SetPointSize( /*LogicalToDeviceYRel*/(font.GetPointSize()));
378 m_graphicContext->SetFont( f, m_textForegroundColour );
379 }
380 }
381
382 void wxGCDCImpl::SetPen( const wxPen &pen )
383 {
384 if ( m_pen == pen )
385 return;
386
387 m_pen = pen;
388 if ( m_graphicContext )
389 {
390 m_graphicContext->SetPen( m_pen );
391 }
392 }
393
394 void wxGCDCImpl::SetBrush( const wxBrush &brush )
395 {
396 if (m_brush == brush)
397 return;
398
399 m_brush = brush;
400 if ( m_graphicContext )
401 {
402 m_graphicContext->SetBrush( m_brush );
403 }
404 }
405
406 void wxGCDCImpl::SetBackground( const wxBrush &brush )
407 {
408 if (m_backgroundBrush == brush)
409 return;
410
411 m_backgroundBrush = brush;
412 if (!m_backgroundBrush.IsOk())
413 return;
414 }
415
416 void wxGCDCImpl::SetLogicalFunction( int function )
417 {
418 if (m_logicalFunction == function)
419 return;
420
421 m_logicalFunction = function;
422 if ( m_graphicContext->SetLogicalFunction( function ) )
423 m_logicalFunctionSupported=true;
424 else
425 m_logicalFunctionSupported=false;
426 }
427
428 bool wxGCDCImpl::DoFloodFill(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
429 const wxColour& WXUNUSED(col), int WXUNUSED(style))
430 {
431 return false;
432 }
433
434 bool wxGCDCImpl::DoGetPixel( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour *WXUNUSED(col) ) const
435 {
436 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
437 return false;
438 }
439
440 void wxGCDCImpl::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
441 {
442 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
443
444 if ( !m_logicalFunctionSupported )
445 return;
446
447 m_graphicContext->StrokeLine(x1,y1,x2,y2);
448
449 CalcBoundingBox(x1, y1);
450 CalcBoundingBox(x2, y2);
451 }
452
453 void wxGCDCImpl::DoCrossHair( wxCoord x, wxCoord y )
454 {
455 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
456
457 if ( !m_logicalFunctionSupported )
458 return;
459
460 int w = 0, h = 0;
461
462 GetOwner()->GetSize( &w, &h );
463
464 m_graphicContext->StrokeLine(0,y,w,y);
465 m_graphicContext->StrokeLine(x,0,x,h);
466
467 CalcBoundingBox(0, 0);
468 CalcBoundingBox(0+w, 0+h);
469 }
470
471 void wxGCDCImpl::DoDrawArc( wxCoord x1, wxCoord y1,
472 wxCoord x2, wxCoord y2,
473 wxCoord xc, wxCoord yc )
474 {
475 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
476
477 if ( !m_logicalFunctionSupported )
478 return;
479
480 double dx = x1 - xc;
481 double dy = y1 - yc;
482 double radius = sqrt((double)(dx * dx + dy * dy));
483 wxCoord rad = (wxCoord)radius;
484 double sa, ea;
485 if (x1 == x2 && y1 == y2)
486 {
487 sa = 0.0;
488 ea = 360.0;
489 }
490 else if (radius == 0.0)
491 {
492 sa = ea = 0.0;
493 }
494 else
495 {
496 sa = (x1 - xc == 0) ?
497 (y1 - yc < 0) ? 90.0 : -90.0 :
498 -atan2(double(y1 - yc), double(x1 - xc)) * RAD2DEG;
499 ea = (x2 - xc == 0) ?
500 (y2 - yc < 0) ? 90.0 : -90.0 :
501 -atan2(double(y2 - yc), double(x2 - xc)) * RAD2DEG;
502 }
503
504 bool fill = m_brush.GetStyle() != wxTRANSPARENT;
505
506 wxGraphicsPath path = m_graphicContext->CreatePath();
507 if ( fill && ((x1!=x2)||(y1!=y2)) )
508 path.MoveToPoint( xc, yc );
509 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
510 // get clockwise angles
511 path.AddArc( xc, yc , rad , DegToRad(-sa) , DegToRad(-ea), false );
512 if ( fill && ((x1!=x2)||(y1!=y2)) )
513 path.AddLineToPoint( xc, yc );
514 m_graphicContext->DrawPath(path);
515 }
516
517 void wxGCDCImpl::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
518 double sa, double ea )
519 {
520 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
521
522 if ( !m_logicalFunctionSupported )
523 return;
524
525 m_graphicContext->PushState();
526 m_graphicContext->Translate(x+w/2.0,y+h/2.0);
527 wxDouble factor = ((wxDouble) w) / h;
528 m_graphicContext->Scale( factor , 1.0);
529
530 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
531 // get clockwise angles
532 if ( m_brush.GetStyle() != wxTRANSPARENT )
533 {
534 wxGraphicsPath path = m_graphicContext->CreatePath();
535 path.MoveToPoint( 0, 0 );
536 path.AddLineToPoint( h / 2.0 * cos(DegToRad(sa)) , h / 2.0 * sin(DegToRad(-sa)) );
537 path.AddLineToPoint( h / 2.0 * cos(DegToRad(ea)) , h / 2.0 * sin(DegToRad(-ea)) );
538 path.AddLineToPoint( 0, 0 );
539 m_graphicContext->FillPath( path );
540
541 path = m_graphicContext->CreatePath();
542 path.AddArc( 0, 0, h/2.0 , DegToRad(-sa) , DegToRad(-ea), sa > ea );
543 m_graphicContext->FillPath( path );
544 m_graphicContext->StrokePath( path );
545 }
546 else
547 {
548 wxGraphicsPath path = m_graphicContext->CreatePath();
549 path.AddArc( 0, 0, h/2.0 , DegToRad(-sa) , DegToRad(-ea), sa > ea );
550 m_graphicContext->DrawPath( path );
551 }
552
553 m_graphicContext->PopState();
554 }
555
556 void wxGCDCImpl::DoDrawPoint( wxCoord x, wxCoord y )
557 {
558 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
559
560 DoDrawLine( x , y , x + 1 , y + 1 );
561 }
562
563 void wxGCDCImpl::DoDrawLines(int n, wxPoint points[],
564 wxCoord xoffset, wxCoord yoffset)
565 {
566 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
567
568 if ( !m_logicalFunctionSupported )
569 return;
570
571 wxPoint2DDouble* pointsD = new wxPoint2DDouble[n];
572 for( int i = 0; i < n; ++i)
573 {
574 pointsD[i].m_x = points[i].x + xoffset;
575 pointsD[i].m_y = points[i].y + yoffset;
576 }
577
578 m_graphicContext->StrokeLines( n , pointsD);
579 delete[] pointsD;
580 }
581
582 #if wxUSE_SPLINES
583 void wxGCDCImpl::DoDrawSpline(const wxPointList *points)
584 {
585 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
586
587 if ( !m_logicalFunctionSupported )
588 return;
589
590 wxGraphicsPath path = m_graphicContext->CreatePath();
591
592 wxPointList::compatibility_iterator node = points->GetFirst();
593 if (node == wxPointList::compatibility_iterator())
594 // empty list
595 return;
596
597 wxPoint *p = node->GetData();
598
599 wxCoord x1 = p->x;
600 wxCoord y1 = p->y;
601
602 node = node->GetNext();
603 p = node->GetData();
604
605 wxCoord x2 = p->x;
606 wxCoord y2 = p->y;
607 wxCoord cx1 = ( x1 + x2 ) / 2;
608 wxCoord cy1 = ( y1 + y2 ) / 2;
609
610 path.MoveToPoint( x1 , y1 );
611 path.AddLineToPoint( cx1 , cy1 );
612 #if !wxUSE_STL
613
614 while ((node = node->GetNext()) != NULL)
615 #else
616
617 while ((node = node->GetNext()))
618 #endif // !wxUSE_STL
619
620 {
621 p = node->GetData();
622 x1 = x2;
623 y1 = y2;
624 x2 = p->x;
625 y2 = p->y;
626 wxCoord cx4 = (x1 + x2) / 2;
627 wxCoord cy4 = (y1 + y2) / 2;
628
629 path.AddQuadCurveToPoint(x1 , y1 ,cx4 , cy4 );
630
631 cx1 = cx4;
632 cy1 = cy4;
633 }
634
635 path.AddLineToPoint( x2 , y2 );
636
637 m_graphicContext->StrokePath( path );
638 }
639 #endif // wxUSE_SPLINES
640
641 void wxGCDCImpl::DoDrawPolygon( int n, wxPoint points[],
642 wxCoord xoffset, wxCoord yoffset,
643 int fillStyle )
644 {
645 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
646
647 if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) )
648 return;
649 if ( !m_logicalFunctionSupported )
650 return;
651
652 bool closeIt = false;
653 if (points[n-1] != points[0])
654 closeIt = true;
655
656 wxPoint2DDouble* pointsD = new wxPoint2DDouble[n+(closeIt?1:0)];
657 for( int i = 0; i < n; ++i)
658 {
659 pointsD[i].m_x = points[i].x + xoffset;
660 pointsD[i].m_y = points[i].y + yoffset;
661 }
662 if ( closeIt )
663 pointsD[n] = pointsD[0];
664
665 m_graphicContext->DrawLines( n+(closeIt?1:0) , pointsD, fillStyle);
666 delete[] pointsD;
667 }
668
669 void wxGCDCImpl::DoDrawPolyPolygon(int n,
670 int count[],
671 wxPoint points[],
672 wxCoord xoffset,
673 wxCoord yoffset,
674 int fillStyle)
675 {
676 wxASSERT(n > 1);
677 wxGraphicsPath path = m_graphicContext->CreatePath();
678
679 int i = 0;
680 for ( int j = 0; j < n; ++j)
681 {
682 wxPoint start = points[i];
683 path.MoveToPoint( start.x+ xoffset, start.y+ yoffset);
684 ++i;
685 int l = count[j];
686 for ( int k = 1; k < l; ++k)
687 {
688 path.AddLineToPoint( points[i].x+ xoffset, points[i].y+ yoffset);
689 ++i;
690 }
691 // close the polygon
692 if ( start != points[i-1])
693 path.AddLineToPoint( start.x+ xoffset, start.y+ yoffset);
694 }
695 m_graphicContext->DrawPath( path , fillStyle);
696 }
697
698 void wxGCDCImpl::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
699 {
700 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
701
702 if ( !m_logicalFunctionSupported )
703 return;
704
705 // CMB: draw nothing if transformed w or h is 0
706 if (w == 0 || h == 0)
707 return;
708
709 if ( m_graphicContext->ShouldOffset() )
710 {
711 // if we are offsetting the entire rectangle is moved 0.5, so the
712 // border line gets off by 1
713 w -= 1;
714 h -= 1;
715 }
716 m_graphicContext->DrawRectangle(x,y,w,h);
717 }
718
719 void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
720 wxCoord w, wxCoord h,
721 double radius)
722 {
723 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
724
725 if ( !m_logicalFunctionSupported )
726 return;
727
728 if (radius < 0.0)
729 radius = - radius * ((w < h) ? w : h);
730
731 // CMB: draw nothing if transformed w or h is 0
732 if (w == 0 || h == 0)
733 return;
734
735 if ( m_graphicContext->ShouldOffset() )
736 {
737 // if we are offsetting the entire rectangle is moved 0.5, so the
738 // border line gets off by 1
739 w -= 1;
740 h -= 1;
741 }
742 m_graphicContext->DrawRoundedRectangle( x,y,w,h,radius);
743 }
744
745 void wxGCDCImpl::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
746 {
747 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
748
749 if ( !m_logicalFunctionSupported )
750 return;
751
752 if ( m_graphicContext->ShouldOffset() )
753 {
754 // if we are offsetting the entire rectangle is moved 0.5, so the
755 // border line gets off by 1
756 w -= 1;
757 h -= 1;
758 }
759 m_graphicContext->DrawEllipse(x,y,w,h);
760 }
761
762 bool wxGCDCImpl::CanDrawBitmap() const
763 {
764 return true;
765 }
766
767 bool wxGCDCImpl::DoBlit(
768 wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
769 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask,
770 wxCoord xsrcMask, wxCoord ysrcMask )
771 {
772 return DoStretchBlit( xdest, ydest, width, height,
773 source, xsrc, ysrc, width, height, logical_func, useMask,
774 xsrcMask,ysrcMask );
775 }
776
777 bool wxGCDCImpl::DoStretchBlit(
778 wxCoord xdest, wxCoord ydest, wxCoord dstWidth, wxCoord dstHeight,
779 wxDC *source, wxCoord xsrc, wxCoord ysrc, wxCoord srcWidth, wxCoord srcHeight,
780 int logical_func , bool WXUNUSED(useMask),
781 wxCoord xsrcMask, wxCoord ysrcMask )
782 {
783 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
784 wxCHECK_MSG( source->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
785
786 if ( logical_func == wxNO_OP )
787 return true;
788 else if ( !m_graphicContext->SetLogicalFunction( logical_func ) )
789
790 {
791 wxFAIL_MSG( wxT("Blitting is only supported with wxCOPY logical operation.") );
792 return false;
793 }
794
795 if (xsrcMask == -1 && ysrcMask == -1)
796 {
797 xsrcMask = xsrc;
798 ysrcMask = ysrc;
799 }
800
801 wxRect subrect(source->LogicalToDeviceX(xsrc),
802 source->LogicalToDeviceY(ysrc),
803 source->LogicalToDeviceXRel(srcWidth),
804 source->LogicalToDeviceYRel(srcHeight));
805
806 // if needed clip the subrect down to the size of the source DC
807 wxCoord sw, sh;
808 source->GetSize(&sw, &sh);
809 sw = source->LogicalToDeviceXRel(sw);
810 sh = source->LogicalToDeviceYRel(sh);
811 if (subrect.x + subrect.width > sw)
812 subrect.width = sw - subrect.x;
813 if (subrect.y + subrect.height > sh)
814 subrect.height = sh - subrect.y;
815
816 wxBitmap blit = source->GetAsBitmap( &subrect );
817
818 if ( blit.IsOk() )
819 {
820 m_graphicContext->DrawBitmap( blit, xdest, ydest,
821 dstWidth, dstHeight);
822 }
823 else
824 {
825 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
826 return false;
827 }
828
829 // reset logical function
830 m_graphicContext->SetLogicalFunction( m_logicalFunction );
831
832 return true;
833 }
834
835 void wxGCDCImpl::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
836 double angle)
837 {
838 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
839
840 if ( str.length() == 0 )
841 return;
842 if ( !m_logicalFunctionSupported )
843 return;
844
845 if ( m_backgroundMode == wxTRANSPARENT )
846 m_graphicContext->DrawText( str, x ,y , DegToRad(angle ));
847 else
848 m_graphicContext->DrawText( str, x ,y , DegToRad(angle ), m_graphicContext->CreateBrush( wxBrush(m_textBackgroundColour,wxSOLID) ) );
849 }
850
851 void wxGCDCImpl::DoDrawText(const wxString& str, wxCoord x, wxCoord y)
852 {
853 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
854
855 if ( str.length() == 0 )
856 return;
857
858 if ( !m_logicalFunctionSupported )
859 return;
860
861 if ( m_backgroundMode == wxTRANSPARENT )
862 m_graphicContext->DrawText( str, x ,y);
863 else
864 m_graphicContext->DrawText( str, x ,y , m_graphicContext->CreateBrush( wxBrush(m_textBackgroundColour,wxSOLID) ) );
865 }
866
867 bool wxGCDCImpl::CanGetTextExtent() const
868 {
869 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
870
871 return true;
872 }
873
874 void wxGCDCImpl::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height,
875 wxCoord *descent, wxCoord *externalLeading ,
876 const wxFont *theFont ) const
877 {
878 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
879
880 if ( theFont )
881 {
882 m_graphicContext->SetFont( *theFont, m_textForegroundColour );
883 }
884
885 wxDouble h , d , e , w;
886
887 m_graphicContext->GetTextExtent( str, &w, &h, &d, &e );
888
889 if ( height )
890 *height = (wxCoord)(h+0.5);
891 if ( descent )
892 *descent = (wxCoord)(d+0.5);
893 if ( externalLeading )
894 *externalLeading = (wxCoord)(e+0.5);
895 if ( width )
896 *width = (wxCoord)(w+0.5);
897
898 if ( theFont )
899 {
900 m_graphicContext->SetFont( m_font, m_textForegroundColour );
901 }
902 }
903
904 bool wxGCDCImpl::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
905 {
906 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
907 widths.Clear();
908 widths.Add(0,text.Length());
909 if ( text.IsEmpty() )
910 return true;
911
912 wxArrayDouble widthsD;
913
914 m_graphicContext->GetPartialTextExtents( text, widthsD );
915 for ( size_t i = 0; i < widths.GetCount(); ++i )
916 widths[i] = (wxCoord)(widthsD[i] + 0.5);
917
918 return true;
919 }
920
921 wxCoord wxGCDCImpl::GetCharWidth(void) const
922 {
923 wxCoord width;
924 DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL );
925
926 return width;
927 }
928
929 wxCoord wxGCDCImpl::GetCharHeight(void) const
930 {
931 wxCoord height;
932 DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL );
933
934 return height;
935 }
936
937 void wxGCDCImpl::Clear(void)
938 {
939 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
940 // TODO better implementation / incorporate size info into wxGCDC or context
941 m_graphicContext->SetBrush( m_backgroundBrush );
942 wxPen p = *wxTRANSPARENT_PEN;
943 m_graphicContext->SetPen( p );
944 DoDrawRectangle( 0, 0, 32000 , 32000 );
945 m_graphicContext->SetPen( m_pen );
946 m_graphicContext->SetBrush( m_brush );
947 }
948
949 void wxGCDCImpl::DoGetSize(int *width, int *height) const
950 {
951 *width = 10000;
952 *height = 10000;
953 }
954
955 void wxGCDCImpl::DoGradientFillLinear(const wxRect& rect,
956 const wxColour& initialColour,
957 const wxColour& destColour,
958 wxDirection nDirection )
959 {
960 wxPoint start;
961 wxPoint end;
962 switch( nDirection)
963 {
964 case wxWEST :
965 start = rect.GetRightBottom();
966 start.x++;
967 end = rect.GetLeftBottom();
968 break;
969 case wxEAST :
970 start = rect.GetLeftBottom();
971 end = rect.GetRightBottom();
972 end.x++;
973 break;
974 case wxNORTH :
975 start = rect.GetLeftBottom();
976 start.y++;
977 end = rect.GetLeftTop();
978 break;
979 case wxSOUTH :
980 start = rect.GetLeftTop();
981 end = rect.GetLeftBottom();
982 end.y++;
983 break;
984 default :
985 break;
986 }
987
988 if (rect.width == 0 || rect.height == 0)
989 return;
990
991 m_graphicContext->SetBrush( m_graphicContext->CreateLinearGradientBrush(
992 start.x,start.y,end.x,end.y, initialColour, destColour));
993 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
994 m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height);
995 m_graphicContext->SetPen(m_pen);
996 }
997
998 void wxGCDCImpl::DoGradientFillConcentric(const wxRect& rect,
999 const wxColour& initialColour,
1000 const wxColour& destColour,
1001 const wxPoint& circleCenter)
1002 {
1003 //Radius
1004 wxInt32 cx = rect.GetWidth() / 2;
1005 wxInt32 cy = rect.GetHeight() / 2;
1006 wxInt32 nRadius;
1007 if (cx < cy)
1008 nRadius = cx;
1009 else
1010 nRadius = cy;
1011
1012 // make sure the background is filled (todo move into specific platform implementation ?)
1013 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
1014 m_graphicContext->SetBrush( wxBrush( destColour) );
1015 m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height);
1016
1017 m_graphicContext->SetBrush( m_graphicContext->CreateRadialGradientBrush(
1018 rect.x+circleCenter.x,rect.y+circleCenter.y,
1019 rect.x+circleCenter.x,rect.y+circleCenter.y,
1020 nRadius,initialColour,destColour));
1021
1022 m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height);
1023 m_graphicContext->SetPen(m_pen);
1024 }
1025
1026 void wxGCDCImpl::DoDrawCheckMark(wxCoord x, wxCoord y,
1027 wxCoord width, wxCoord height)
1028 {
1029 wxDCImpl::DoDrawCheckMark(x,y,width,height);
1030 }
1031
1032 #endif // wxUSE_GRAPHICS_CONTEXT