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