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