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