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