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