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