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