using native tranforms for everything
[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 m_deviceOriginX = /* m_deviceOriginX + */ m_logicalOriginX;
295 m_deviceOriginY = /* m_deviceOriginY + */ m_logicalOriginY;
296
297 m_matrixCurrent = m_graphicContext->CreateMatrix();
298 m_matrixCurrent.Translate( m_deviceOriginX , m_deviceOriginY );
299 m_matrixCurrent.Scale( m_scaleX, m_scaleY );
300
301 m_graphicContext->SetTransform( m_matrixOriginal );
302 m_graphicContext->ConcatTransform( m_matrixCurrent );
303 }
304
305 void wxGCDC::SetPalette( const wxPalette& WXUNUSED(palette) )
306 {
307
308 }
309
310 void wxGCDC::SetBackgroundMode( int mode )
311 {
312 m_backgroundMode = mode;
313 }
314
315 void wxGCDC::SetFont( const wxFont &font )
316 {
317 m_font = font;
318 if ( m_graphicContext )
319 {
320 wxFont f = font;
321 if ( f.Ok() )
322 f.SetPointSize( /*LogicalToDeviceYRel*/(font.GetPointSize()));
323 m_graphicContext->SetFont( f, m_textForegroundColour );
324 }
325 }
326
327 void wxGCDC::SetPen( const wxPen &pen )
328 {
329 if ( m_pen == pen )
330 return;
331
332 m_pen = pen;
333 if ( m_graphicContext )
334 {
335 m_graphicContext->SetPen( m_pen );
336 }
337 }
338
339 void wxGCDC::SetBrush( const wxBrush &brush )
340 {
341 if (m_brush == brush)
342 return;
343
344 m_brush = brush;
345 if ( m_graphicContext )
346 {
347 m_graphicContext->SetBrush( m_brush );
348 }
349 }
350
351 void wxGCDC::SetBackground( const wxBrush &brush )
352 {
353 if (m_backgroundBrush == brush)
354 return;
355
356 m_backgroundBrush = brush;
357 if (!m_backgroundBrush.Ok())
358 return;
359 }
360
361 void wxGCDC::SetLogicalFunction( int function )
362 {
363 if (m_logicalFunction == function)
364 return;
365
366 m_logicalFunction = function;
367 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
368
369 CGContextRef cgContext = ((wxCairoContext*)(m_graphicContext))->GetNativeContext();
370 if ( m_logicalFunction == wxCOPY )
371 CGContextSetBlendMode( cgContext, kCGBlendModeNormal );
372 else if ( m_logicalFunction == wxINVERT )
373 CGContextSetBlendMode( cgContext, kCGBlendModeExclusion );
374 else
375 CGContextSetBlendMode( cgContext, kCGBlendModeNormal );
376 #endif
377
378 }
379
380 bool wxGCDC::DoFloodFill(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
381 const wxColour& WXUNUSED(col), int WXUNUSED(style))
382 {
383 return false;
384 }
385
386 bool wxGCDC::DoGetPixel( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour *WXUNUSED(col) ) const
387 {
388 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
389 return false;
390 }
391
392 void wxGCDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
393 {
394 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
395
396 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
397
398 if ( m_logicalFunction != wxCOPY )
399 return;
400 #endif
401
402 m_graphicContext->StrokeLine(x1,y1,x2,y2);
403
404 CalcBoundingBox(x1, y1);
405 CalcBoundingBox(x2, y2);
406 }
407
408 void wxGCDC::DoCrossHair( wxCoord x, wxCoord y )
409 {
410 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
411
412 if ( m_logicalFunction != wxCOPY )
413 return;
414
415 int w = 0, h = 0;
416
417 GetSize( &w, &h );
418
419 m_graphicContext->StrokeLine(0,y,w,y);
420 m_graphicContext->StrokeLine(x,0,x,h);
421
422 CalcBoundingBox(0, 0);
423 CalcBoundingBox(0+w, 0+h);
424 }
425
426 void wxGCDC::DoDrawArc( wxCoord x1, wxCoord y1,
427 wxCoord x2, wxCoord y2,
428 wxCoord xc, wxCoord yc )
429 {
430 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
431
432 if ( m_logicalFunction != wxCOPY )
433 return;
434
435 double dx = x1 - xc;
436 double dy = y1 - yc;
437 double radius = sqrt((double)(dx * dx + dy * dy));
438 wxCoord rad = (wxCoord)radius;
439 double sa, ea;
440 if (x1 == x2 && y1 == y2)
441 {
442 sa = 0.0;
443 ea = 360.0;
444 }
445 else if (radius == 0.0)
446 {
447 sa = ea = 0.0;
448 }
449 else
450 {
451 sa = (x1 - xc == 0) ?
452 (y1 - yc < 0) ? 90.0 : -90.0 :
453 -atan2(double(y1 - yc), double(x1 - xc)) * RAD2DEG;
454 ea = (x2 - xc == 0) ?
455 (y2 - yc < 0) ? 90.0 : -90.0 :
456 -atan2(double(y2 - yc), double(x2 - xc)) * RAD2DEG;
457 }
458
459 bool fill = m_brush.GetStyle() != wxTRANSPARENT;
460
461 wxGraphicsPath path = m_graphicContext->CreatePath();
462 if ( fill && ((x1!=x2)||(y1!=y2)) )
463 path.MoveToPoint( xc, yc );
464 path.AddArc( xc, yc , rad , DegToRad(sa) , DegToRad(ea), false );
465 if ( fill && ((x1!=x2)||(y1!=y2)) )
466 path.AddLineToPoint( xc, yc );
467 m_graphicContext->DrawPath(path);
468 }
469
470 void wxGCDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
471 double sa, double ea )
472 {
473 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
474
475 if ( m_logicalFunction != wxCOPY )
476 return;
477
478 bool fill = m_brush.GetStyle() != wxTRANSPARENT;
479
480 wxGraphicsPath path = m_graphicContext->CreatePath();
481 m_graphicContext->PushState();
482 m_graphicContext->Translate(x+w/2,y+h/2);
483 wxDouble factor = ((wxDouble) w) / h;
484 m_graphicContext->Scale( factor , 1.0);
485 if ( fill && (sa!=ea) )
486 path.MoveToPoint(0,0);
487 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
488 // get clockwise angles
489 path.AddArc( 0, 0, h/2 , DegToRad(-sa) , DegToRad(-ea), sa > ea );
490 if ( fill && (sa!=ea) )
491 path.AddLineToPoint(0,0);
492 m_graphicContext->DrawPath( path );
493 m_graphicContext->PopState();
494 }
495
496 void wxGCDC::DoDrawPoint( wxCoord x, wxCoord y )
497 {
498 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
499
500 DoDrawLine( x , y , x + 1 , y + 1 );
501 }
502
503 void wxGCDC::DoDrawLines(int n, wxPoint points[],
504 wxCoord xoffset, wxCoord yoffset)
505 {
506 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
507
508 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
509
510 if ( m_logicalFunction != wxCOPY )
511 return;
512 #endif
513
514 wxPoint2DDouble* pointsD = new wxPoint2DDouble[n];
515 for( int i = 0; i < n; ++i)
516 {
517 pointsD[i].m_x = points[i].x + xoffset;
518 pointsD[i].m_y = points[i].y + yoffset;
519 }
520
521 m_graphicContext->StrokeLines( n , pointsD);
522 delete[] pointsD;
523 }
524
525 #if wxUSE_SPLINES
526 void wxGCDC::DoDrawSpline(wxList *points)
527 {
528 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
529
530 if ( m_logicalFunction != wxCOPY )
531 return;
532
533 wxGraphicsPath path = m_graphicContext->CreatePath();
534
535 wxList::compatibility_iterator node = points->GetFirst();
536 if (node == wxList::compatibility_iterator())
537 // empty list
538 return;
539
540 wxPoint *p = (wxPoint *)node->GetData();
541
542 wxCoord x1 = p->x;
543 wxCoord y1 = p->y;
544
545 node = node->GetNext();
546 p = (wxPoint *)node->GetData();
547
548 wxCoord x2 = p->x;
549 wxCoord y2 = p->y;
550 wxCoord cx1 = ( x1 + x2 ) / 2;
551 wxCoord cy1 = ( y1 + y2 ) / 2;
552
553 path.MoveToPoint( x1 , y1 );
554 path.AddLineToPoint( cx1 , cy1 );
555 #if !wxUSE_STL
556
557 while ((node = node->GetNext()) != NULL)
558 #else
559
560 while ((node = node->GetNext()))
561 #endif // !wxUSE_STL
562
563 {
564 p = (wxPoint *)node->GetData();
565 x1 = x2;
566 y1 = y2;
567 x2 = p->x;
568 y2 = p->y;
569 wxCoord cx4 = (x1 + x2) / 2;
570 wxCoord cy4 = (y1 + y2) / 2;
571
572 path.AddQuadCurveToPoint(x1 , y1 ,cx4 , cy4 );
573
574 cx1 = cx4;
575 cy1 = cy4;
576 }
577
578 path.AddLineToPoint( x2 , y2 );
579
580 m_graphicContext->StrokePath( path );
581 }
582 #endif // wxUSE_SPLINES
583
584 void wxGCDC::DoDrawPolygon( int n, wxPoint points[],
585 wxCoord xoffset, wxCoord yoffset,
586 int fillStyle )
587 {
588 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
589
590 if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) )
591 return;
592 if ( m_logicalFunction != wxCOPY )
593 return;
594
595 bool closeIt = false;
596 if (points[n-1] != points[0])
597 closeIt = true;
598
599 wxPoint2DDouble* pointsD = new wxPoint2DDouble[n+(closeIt?1:0)];
600 for( int i = 0; i < n; ++i)
601 {
602 pointsD[i].m_x = points[i].x + xoffset;
603 pointsD[i].m_y = points[i].y + yoffset;
604 }
605 if ( closeIt )
606 pointsD[n] = pointsD[0];
607
608 m_graphicContext->DrawLines( n+(closeIt?1:0) , pointsD, fillStyle);
609 delete[] pointsD;
610 }
611
612 void wxGCDC::DoDrawPolyPolygon(int n,
613 int count[],
614 wxPoint points[],
615 wxCoord xoffset,
616 wxCoord yoffset,
617 int fillStyle)
618 {
619 wxASSERT(n > 1);
620 wxGraphicsPath path = m_graphicContext->CreatePath();
621
622 int i = 0;
623 for ( int j = 0; j < n; ++j)
624 {
625 wxPoint start = points[i];
626 path.MoveToPoint( start.x+ xoffset, start.y+ yoffset);
627 ++i;
628 int l = count[j];
629 for ( int k = 1; k < l; ++k)
630 {
631 path.AddLineToPoint( points[i].x+ xoffset, points[i].y+ yoffset);
632 ++i;
633 }
634 // close the polygon
635 if ( start != points[i-1])
636 path.AddLineToPoint( start.x+ xoffset, start.y+ yoffset);
637 }
638 m_graphicContext->DrawPath( path , fillStyle);
639 }
640
641 void wxGCDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
642 {
643 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
644
645 if ( m_logicalFunction != wxCOPY )
646 return;
647
648 // CMB: draw nothing if transformed w or h is 0
649 if (w == 0 || h == 0)
650 return;
651
652 if ( m_graphicContext->ShouldOffset() )
653 {
654 // if we are offsetting the entire rectangle is moved 0.5, so the
655 // border line gets off by 1
656 w -= 1;
657 h -= 1;
658 }
659 m_graphicContext->DrawRectangle(x,y,w,h);
660 }
661
662 void wxGCDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
663 wxCoord w, wxCoord h,
664 double radius)
665 {
666 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
667
668 if ( m_logicalFunction != wxCOPY )
669 return;
670
671 if (radius < 0.0)
672 radius = - radius * ((w < h) ? w : h);
673
674 // CMB: draw nothing if transformed w or h is 0
675 if (w == 0 || h == 0)
676 return;
677
678 m_graphicContext->DrawRoundedRectangle( x,y,w,h,radius);
679 }
680
681 void wxGCDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
682 {
683 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
684
685 if ( m_logicalFunction != wxCOPY )
686 return;
687
688 m_graphicContext->DrawEllipse(x,y,w,h);
689 }
690
691 bool wxGCDC::CanDrawBitmap() const
692 {
693 return true;
694 }
695
696 bool wxGCDC::DoBlit(
697 wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
698 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool WXUNUSED(useMask),
699 wxCoord xsrcMask, wxCoord ysrcMask )
700 {
701 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
702 wxCHECK_MSG( source->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
703
704 if ( logical_func == wxNO_OP )
705 return true;
706 else if ( logical_func != wxCOPY )
707 {
708 wxFAIL_MSG( wxT("Blitting is only supported with wxCOPY logical operation.") );
709 return false;
710 }
711
712 if (xsrcMask == -1 && ysrcMask == -1)
713 {
714 xsrcMask = xsrc;
715 ysrcMask = ysrc;
716 }
717
718 wxCoord yysrc = source-> LogicalToDeviceY(ysrc);
719 wxCoord xxsrc = source-> LogicalToDeviceX(xsrc);
720 wxCoord wwsrc = source-> LogicalToDeviceXRel(width);
721 wxCoord hhsrc = source-> LogicalToDeviceYRel(height);
722
723 wxBitmap blit;
724 wxMemoryDC* memdc = wxDynamicCast(source,wxMemoryDC);
725 if ( memdc )
726 {
727 blit = memdc->GetSelectedBitmap();
728
729 wxASSERT_MSG( blit.Ok() , wxT("Invalid bitmap for blitting") );
730
731 wxCoord bmpwidth = blit.GetWidth();
732 wxCoord bmpheight = blit.GetHeight();
733
734 if ( xxsrc != 0 || yysrc != 0 || bmpwidth != wwsrc || bmpheight != hhsrc )
735 {
736 wwsrc = wxMin( wwsrc , bmpwidth - xxsrc );
737 hhsrc = wxMin( hhsrc , bmpheight - yysrc );
738 if ( wwsrc > 0 && hhsrc > 0 )
739 {
740 if ( xxsrc >= 0 && yysrc >= 0 )
741 {
742 wxRect subrect( xxsrc, yysrc, wwsrc , hhsrc );
743 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
744 blit = blit.GetSubBitmap( subrect );
745 }
746 else
747 {
748 // in this case we'd probably have to adjust the different coordinates, but
749 // we have to find out proper contract first
750 blit = wxNullBitmap;
751 }
752 }
753 else
754 {
755 blit = wxNullBitmap;
756 }
757 }
758 }
759 else
760 {
761 wxWindowDC* windc = wxDynamicCast(source,wxWindowDC);
762 if (windc)
763 {
764 wxBitmap bmp;
765 bmp = windc->GetAsBitmap();
766 if (bmp.IsOk())
767 blit = bmp.GetSubBitmap( wxRect(xsrc, ysrc, width, height ) );
768 }
769 }
770
771 if ( blit.Ok() )
772 {
773 m_graphicContext->DrawBitmap( blit, xdest , ydest , width , height );
774 }
775 else
776 {
777 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
778 return false;
779 }
780
781 return true;
782 }
783
784 void wxGCDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
785 double angle)
786 {
787 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
788
789 if ( str.length() == 0 )
790 return;
791 if ( m_logicalFunction != wxCOPY )
792 return;
793
794 m_graphicContext->DrawText( str, x ,y , DegToRad(angle ));
795 }
796
797 void wxGCDC::DoDrawText(const wxString& str, wxCoord x, wxCoord y)
798 {
799 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
800
801 if ( str.length() == 0 )
802 return;
803 if ( m_logicalFunction != wxCOPY )
804 return;
805
806 m_graphicContext->DrawText( str, x ,y);
807 }
808
809 bool wxGCDC::CanGetTextExtent() const
810 {
811 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
812
813 return true;
814 }
815
816 void wxGCDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height,
817 wxCoord *descent, wxCoord *externalLeading ,
818 wxFont *theFont ) const
819 {
820 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
821
822 if ( theFont )
823 {
824 m_graphicContext->SetFont( *theFont, m_textForegroundColour );
825 }
826
827 wxDouble h , d , e , w;
828
829 m_graphicContext->GetTextExtent( str, &w, &h, &d, &e );
830
831 if ( height )
832 *height = h;
833 if ( descent )
834 *descent = d;
835 if ( externalLeading )
836 *externalLeading =e;
837 if ( width )
838 *width = w;
839
840 if ( theFont )
841 {
842 m_graphicContext->SetFont( m_font, m_textForegroundColour );
843 }
844 }
845
846 bool wxGCDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
847 {
848 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
849 widths.Clear();
850 widths.Add(0,text.Length());
851 if ( text.IsEmpty() )
852 return true;
853
854 wxArrayDouble widthsD;
855
856 m_graphicContext->GetPartialTextExtents( text, widthsD );
857 for ( size_t i = 0; i < widths.GetCount(); ++i )
858 widths[i] = (wxCoord)(widthsD[i] + 0.5);
859
860 return true;
861 }
862
863 wxCoord wxGCDC::GetCharWidth(void) const
864 {
865 wxCoord width;
866 DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL );
867
868 return width;
869 }
870
871 wxCoord wxGCDC::GetCharHeight(void) const
872 {
873 wxCoord height;
874 DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL );
875
876 return height;
877 }
878
879 void wxGCDC::Clear(void)
880 {
881 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
882 // TODO better implementation / incorporate size info into wxGCDC or context
883 m_graphicContext->SetBrush( m_backgroundBrush );
884 wxPen p = *wxTRANSPARENT_PEN;
885 m_graphicContext->SetPen( p );
886 DoDrawRectangle( 0, 0, 32000 , 32000 );
887 m_graphicContext->SetPen( m_pen );
888 m_graphicContext->SetBrush( m_brush );
889 }
890
891 void wxGCDC::DoGetSize(int *width, int *height) const
892 {
893 *width = 1000;
894 *height = 1000;
895 }
896
897 void wxGCDC::DoGradientFillLinear(const wxRect& rect,
898 const wxColour& initialColour,
899 const wxColour& destColour,
900 wxDirection nDirection )
901 {
902 wxPoint start;
903 wxPoint end;
904 switch( nDirection)
905 {
906 case wxWEST :
907 start = rect.GetRightBottom();
908 start.x++;
909 end = rect.GetLeftBottom();
910 break;
911 case wxEAST :
912 start = rect.GetLeftBottom();
913 end = rect.GetRightBottom();
914 end.x++;
915 break;
916 case wxNORTH :
917 start = rect.GetLeftBottom();
918 start.y++;
919 end = rect.GetLeftTop();
920 break;
921 case wxSOUTH :
922 start = rect.GetLeftTop();
923 end = rect.GetLeftBottom();
924 end.y++;
925 break;
926 default :
927 break;
928 }
929
930 if (rect.width == 0 || rect.height == 0)
931 return;
932
933 m_graphicContext->SetBrush( m_graphicContext->CreateLinearGradientBrush(
934 start.x,start.y,end.x,end.y, initialColour, destColour));
935 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
936 m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height);
937 m_graphicContext->SetPen(m_pen);
938 }
939
940 void wxGCDC::DoGradientFillConcentric(const wxRect& rect,
941 const wxColour& initialColour,
942 const wxColour& destColour,
943 const wxPoint& circleCenter)
944 {
945 //Radius
946 wxInt32 cx = rect.GetWidth() / 2;
947 wxInt32 cy = rect.GetHeight() / 2;
948 wxInt32 nRadius;
949 if (cx < cy)
950 nRadius = cx;
951 else
952 nRadius = cy;
953
954 // make sure the background is filled (todo move into specific platform implementation ?)
955 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
956 m_graphicContext->SetBrush( wxBrush( destColour) );
957 m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height);
958
959 m_graphicContext->SetBrush( m_graphicContext->CreateRadialGradientBrush(
960 rect.x+circleCenter.x,rect.y+circleCenter.y,
961 rect.x+circleCenter.x,rect.y+circleCenter.y,
962 nRadius,initialColour,destColour));
963
964 m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height);
965 m_graphicContext->SetPen(m_pen);
966 }
967
968 void wxGCDC::DoDrawCheckMark(wxCoord x, wxCoord y,
969 wxCoord width, wxCoord height)
970 {
971 wxDCBase::DoDrawCheckMark(x,y,width,height);
972 }
973
974 #endif // wxUSE_GRAPHICS_CONTEXT