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