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