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