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