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