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