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