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