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