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