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