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