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