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