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