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