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