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