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