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