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