]> git.saurik.com Git - wxWidgets.git/blame - src/common/dcgraph.cpp
fix wxMBConv_iconv to respect the base class conventions: when the input is NUL-termi...
[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"
888dde65 22#include "wx/dcgraph.h"
1b89a5cd
SC
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
b02d4340
VZ
31#include "wx/dcclient.h"
32
71be467a
SC
33#ifdef __WXOSX__
34 #include "ApplicationServices/ApplicationServices.h"
fcaea2fa 35#endif
b02d4340 36
1b89a5cd
SC
37//-----------------------------------------------------------------------------
38// constants
39//-----------------------------------------------------------------------------
40
41static const double RAD2DEG = 180.0 / M_PI;
42
43//-----------------------------------------------------------------------------
44// Local functions
45//-----------------------------------------------------------------------------
46
47static inline double DegToRad(double deg)
48{
49 return (deg * M_PI) / 180.0;
50}
51
52//-----------------------------------------------------------------------------
53// wxDC bridge class
54//-----------------------------------------------------------------------------
55
1b89a5cd 56IMPLEMENT_DYNAMIC_CLASS(wxGCDC, wxDC)
888dde65 57
7b1a66bb
RR
58wxGCDC::wxGCDC(const wxWindowDC& dc) :
59 wxDC( new wxGCDCImpl( this, dc ) )
888dde65 60{
888dde65
RR
61}
62
7b1a66bb
RR
63wxGCDC::wxGCDC( const wxMemoryDC& dc) :
64 wxDC( new wxGCDCImpl( this, dc ) )
888dde65 65{
888dde65 66}
1b89a5cd 67
b25c34aa
RR
68wxGCDC::wxGCDC( const wxPrinterDC& dc) :
69 wxDC( new wxGCDCImpl( this, dc ) )
70{
71}
72
7b1a66bb
RR
73wxGCDC::wxGCDC() :
74 wxDC( new wxGCDCImpl( this ) )
1b89a5cd 75{
888dde65
RR
76}
77
9412ee9a
SC
78wxGCDC::~wxGCDC()
79{
80}
81
888dde65
RR
82wxGraphicsContext* wxGCDC::GetGraphicsContext()
83{
84 if (!m_pimpl) return NULL;
85 wxGCDCImpl *gc_impl = (wxGCDCImpl*) m_pimpl;
86 return gc_impl->GetGraphicsContext();
1b89a5cd
SC
87}
88
89void wxGCDC::SetGraphicsContext( wxGraphicsContext* ctx )
888dde65
RR
90{
91 if (!m_pimpl) return;
92 wxGCDCImpl *gc_impl = (wxGCDCImpl*) m_pimpl;
93 gc_impl->SetGraphicsContext( ctx );
94}
95
96IMPLEMENT_ABSTRACT_CLASS(wxGCDCImpl, wxDCImpl)
97
98wxGCDCImpl::wxGCDCImpl( wxDC *owner ) :
99 wxDCImpl( owner )
100{
101 Init();
102}
103
104void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext* ctx )
6c0aace2 105{
1b89a5cd
SC
106 delete m_graphicContext;
107 m_graphicContext = ctx;
914fd3f1
SC
108 if ( m_graphicContext )
109 {
110 m_matrixOriginal = m_graphicContext->GetTransform();
111 m_ok = true;
fd791571
SC
112 // apply the stored transformations to the passed in context
113 ComputeScaleAndOrigin();
ad667945
SC
114 m_graphicContext->SetFont( m_font , m_textForegroundColour );
115 m_graphicContext->SetPen( m_pen );
116 m_graphicContext->SetBrush( m_brush);
914fd3f1 117 }
1b89a5cd
SC
118}
119
888dde65
RR
120wxGCDCImpl::wxGCDCImpl( wxDC *owner, const wxWindowDC& dc ) :
121 wxDCImpl( owner )
1b89a5cd
SC
122{
123 Init();
914fd3f1 124 SetGraphicsContext( wxGraphicsContext::Create(dc) );
7b98cf02 125 m_window = dc.GetWindow();
1b89a5cd
SC
126}
127
888dde65
RR
128wxGCDCImpl::wxGCDCImpl( wxDC *owner, const wxMemoryDC& dc ) :
129 wxDCImpl( owner )
773ccc31
SC
130{
131 Init();
132 SetGraphicsContext( wxGraphicsContext::Create(dc) );
133}
773ccc31 134
b25c34aa
RR
135wxGCDCImpl::wxGCDCImpl( wxDC *owner, const wxPrinterDC& dc ) :
136 wxDCImpl( owner )
137{
138 Init();
139 SetGraphicsContext( wxGraphicsContext::Create(dc) );
140}
141
888dde65 142void wxGCDCImpl::Init()
1b89a5cd
SC
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;
4280b879 154 m_logicalFunctionSupported = true;
1b89a5cd
SC
155}
156
157
888dde65 158wxGCDCImpl::~wxGCDCImpl()
1b89a5cd
SC
159{
160 delete m_graphicContext;
161}
162
888dde65 163void wxGCDCImpl::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool WXUNUSED(useMask) )
1b89a5cd 164{
888dde65
RR
165 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
166 wxCHECK_RET( bmp.IsOk(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
1b89a5cd 167
f889bdb3
SC
168 if ( bmp.GetDepth() == 1 )
169 {
170 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
171 m_graphicContext->SetBrush( wxBrush( m_textBackgroundColour , wxSOLID ) );
b02d4340 172 m_graphicContext->DrawRectangle( x , y , bmp.GetWidth() , bmp.GetHeight() );
f889bdb3
SC
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() );
1b89a5cd
SC
180}
181
888dde65 182void wxGCDCImpl::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
1b89a5cd 183{
888dde65
RR
184 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
185 wxCHECK_RET( icon.IsOk(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
1b89a5cd 186
1b89a5cd
SC
187 wxCoord w = icon.GetWidth();
188 wxCoord h = icon.GetHeight();
1b89a5cd 189
0ebd9515 190 m_graphicContext->DrawIcon( icon , x, y, w, h );
1b89a5cd
SC
191}
192
b02d4340 193bool wxGCDCImpl::StartDoc( const wxString& WXUNUSED(message) )
fcaea2fa 194{
26dfebbd 195 return true;
fcaea2fa
SC
196}
197
b02d4340 198void wxGCDCImpl::EndDoc()
fcaea2fa
SC
199{
200}
201
888dde65 202void wxGCDCImpl::StartPage()
fcaea2fa
SC
203{
204}
205
b02d4340 206void wxGCDCImpl::EndPage()
fcaea2fa
SC
207{
208}
b02d4340 209
888dde65 210void wxGCDCImpl::Flush()
fcaea2fa 211{
71be467a 212#ifdef __WXOSX__
fcaea2fa
SC
213 CGContextFlush( (CGContextRef) m_graphicContext->GetNativeContext() );
214#endif
215}
216
888dde65 217void wxGCDCImpl::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord w, wxCoord h )
1b89a5cd 218{
888dde65 219 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
1b89a5cd 220
0ebd9515 221 m_graphicContext->Clip( x, y, w, h );
1b89a5cd
SC
222 if ( m_clipping )
223 {
0ebd9515
SC
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) );
1b89a5cd
SC
228 }
229 else
230 {
231 m_clipping = true;
232
0ebd9515
SC
233 m_clipX1 = x;
234 m_clipY1 = y;
235 m_clipX2 = x + w;
236 m_clipY2 = y + h;
1b89a5cd
SC
237 }
238}
239
888dde65 240void wxGCDCImpl::DoSetClippingRegionAsRegion( const wxRegion &region )
1b89a5cd 241{
4bae004c 242 // region is in device coordinates
888dde65 243 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
1b89a5cd
SC
244
245 if (region.Empty())
246 {
fcaea2fa 247 //DestroyClippingRegion();
1b89a5cd
SC
248 return;
249 }
250
4bae004c 251 wxRegion logRegion( region );
1b89a5cd 252 wxCoord x, y, w, h;
0ebd9515 253
4bae004c
SC
254 logRegion.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
255 logRegion.GetBox( x, y, w, h );
256
257 m_graphicContext->Clip( logRegion );
0ebd9515 258 if ( m_clipping )
1b89a5cd 259 {
0ebd9515
SC
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) );
1b89a5cd
SC
264 }
265 else
266 {
0ebd9515 267 m_clipping = true;
1b89a5cd 268
0ebd9515
SC
269 m_clipX1 = x;
270 m_clipY1 = y;
271 m_clipX2 = x + w;
272 m_clipY2 = y + h;
1b89a5cd
SC
273 }
274}
275
888dde65 276void wxGCDCImpl::DestroyClippingRegion()
1b89a5cd
SC
277{
278 m_graphicContext->ResetClip();
cb6e26b8
SC
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 ;
888dde65 282 GetOwner()->GetSize( &width , &height ) ;
fcaea2fa 283 m_graphicContext->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width), DeviceToLogicalYRel(height) );
b02d4340 284
1b89a5cd
SC
285 m_graphicContext->SetPen( m_pen );
286 m_graphicContext->SetBrush( m_brush );
287
288 m_clipping = false;
289}
290
888dde65 291void wxGCDCImpl::DoGetSizeMM( int* width, int* height ) const
1b89a5cd
SC
292{
293 int w = 0, h = 0;
294
888dde65 295 GetOwner()->GetSize( &w, &h );
1b89a5cd
SC
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
888dde65 302void wxGCDCImpl::SetTextForeground( const wxColour &col )
1b89a5cd 303{
888dde65 304 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
1b89a5cd
SC
305
306 if ( col != m_textForegroundColour )
307 {
308 m_textForegroundColour = col;
309 m_graphicContext->SetFont( m_font, m_textForegroundColour );
310 }
311}
312
888dde65 313void wxGCDCImpl::SetTextBackground( const wxColour &col )
1b89a5cd 314{
888dde65 315 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
1b89a5cd
SC
316
317 m_textBackgroundColour = col;
318}
319
888dde65 320void wxGCDCImpl::SetMapMode( int mode )
1b89a5cd
SC
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
888dde65 349wxSize wxGCDCImpl::GetPPI() const
1b89a5cd
SC
350{
351 return wxSize(72, 72);
352}
353
888dde65 354int wxGCDCImpl::GetDepth() const
1b89a5cd
SC
355{
356 return 32;
357}
358
888dde65 359void wxGCDCImpl::ComputeScaleAndOrigin()
b02d4340 360{
2dedef25 361 wxDCImpl::ComputeScaleAndOrigin();
1b89a5cd 362
fd791571
SC
363 if ( m_graphicContext )
364 {
365 m_matrixCurrent = m_graphicContext->CreateMatrix();
d225267e 366
fd791571 367 // the logical origin sets the origin to have new coordinates
d225267e
RR
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
fd791571
SC
373 m_graphicContext->SetTransform( m_matrixOriginal );
374 m_graphicContext->ConcatTransform( m_matrixCurrent );
375 }
1b89a5cd
SC
376}
377
888dde65 378void wxGCDCImpl::SetPalette( const wxPalette& WXUNUSED(palette) )
1b89a5cd
SC
379{
380
381}
382
888dde65 383void wxGCDCImpl::SetBackgroundMode( int mode )
1b89a5cd
SC
384{
385 m_backgroundMode = mode;
386}
387
888dde65 388void wxGCDCImpl::SetFont( const wxFont &font )
1b89a5cd
SC
389{
390 m_font = font;
391 if ( m_graphicContext )
392 {
393 wxFont f = font;
888dde65 394 if ( f.IsOk() )
0ebd9515 395 f.SetPointSize( /*LogicalToDeviceYRel*/(font.GetPointSize()));
1b89a5cd
SC
396 m_graphicContext->SetFont( f, m_textForegroundColour );
397 }
398}
399
888dde65 400void wxGCDCImpl::SetPen( const wxPen &pen )
1b89a5cd
SC
401{
402 if ( m_pen == pen )
403 return;
404
405 m_pen = pen;
406 if ( m_graphicContext )
407 {
0ebd9515 408 m_graphicContext->SetPen( m_pen );
1b89a5cd
SC
409 }
410}
411
888dde65 412void wxGCDCImpl::SetBrush( const wxBrush &brush )
1b89a5cd
SC
413{
414 if (m_brush == brush)
415 return;
416
417 m_brush = brush;
418 if ( m_graphicContext )
419 {
0ebd9515 420 m_graphicContext->SetBrush( m_brush );
1b89a5cd
SC
421 }
422}
b02d4340 423
888dde65 424void wxGCDCImpl::SetBackground( const wxBrush &brush )
1b89a5cd
SC
425{
426 if (m_backgroundBrush == brush)
427 return;
428
429 m_backgroundBrush = brush;
888dde65 430 if (!m_backgroundBrush.IsOk())
1b89a5cd
SC
431 return;
432}
433
888dde65 434void wxGCDCImpl::SetLogicalFunction( int function )
1b89a5cd
SC
435{
436 if (m_logicalFunction == function)
437 return;
438
439 m_logicalFunction = function;
4280b879
SC
440 if ( m_graphicContext->SetLogicalFunction( function ) )
441 m_logicalFunctionSupported=true;
1b89a5cd 442 else
4280b879 443 m_logicalFunctionSupported=false;
1b89a5cd
SC
444}
445
888dde65 446bool wxGCDCImpl::DoFloodFill(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
1b89a5cd
SC
447 const wxColour& WXUNUSED(col), int WXUNUSED(style))
448{
449 return false;
450}
451
888dde65 452bool wxGCDCImpl::DoGetPixel( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour *WXUNUSED(col) ) const
1b89a5cd
SC
453{
454 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
455 return false;
456}
457
888dde65 458void wxGCDCImpl::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
1b89a5cd 459{
888dde65 460 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
1b89a5cd 461
4280b879 462 if ( !m_logicalFunctionSupported )
1b89a5cd 463 return;
1b89a5cd 464
0ebd9515 465 m_graphicContext->StrokeLine(x1,y1,x2,y2);
1b89a5cd
SC
466
467 CalcBoundingBox(x1, y1);
468 CalcBoundingBox(x2, y2);
469}
470
888dde65 471void wxGCDCImpl::DoCrossHair( wxCoord x, wxCoord y )
1b89a5cd 472{
888dde65 473 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
1b89a5cd 474
4280b879 475 if ( !m_logicalFunctionSupported )
1b89a5cd
SC
476 return;
477
478 int w = 0, h = 0;
479
888dde65 480 GetOwner()->GetSize( &w, &h );
1b89a5cd 481
0ebd9515
SC
482 m_graphicContext->StrokeLine(0,y,w,y);
483 m_graphicContext->StrokeLine(x,0,x,h);
1b89a5cd 484
0ebd9515
SC
485 CalcBoundingBox(0, 0);
486 CalcBoundingBox(0+w, 0+h);
1b89a5cd
SC
487}
488
888dde65 489void wxGCDCImpl::DoDrawArc( wxCoord x1, wxCoord y1,
1b89a5cd
SC
490 wxCoord x2, wxCoord y2,
491 wxCoord xc, wxCoord yc )
492{
888dde65 493 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
1b89a5cd 494
4280b879 495 if ( !m_logicalFunctionSupported )
1b89a5cd
SC
496 return;
497
0ebd9515
SC
498 double dx = x1 - xc;
499 double dy = y1 - yc;
1b89a5cd
SC
500 double radius = sqrt((double)(dx * dx + dy * dy));
501 wxCoord rad = (wxCoord)radius;
502 double sa, ea;
0ebd9515 503 if (x1 == x2 && y1 == y2)
1b89a5cd
SC
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 {
0ebd9515
SC
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;
1b89a5cd
SC
520 }
521
522 bool fill = m_brush.GetStyle() != wxTRANSPARENT;
523
a4e73390 524 wxGraphicsPath path = m_graphicContext->CreatePath();
1b89a5cd 525 if ( fill && ((x1!=x2)||(y1!=y2)) )
0ebd9515 526 path.MoveToPoint( xc, yc );
4bae004c
SC
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 );
1b89a5cd 530 if ( fill && ((x1!=x2)||(y1!=y2)) )
0ebd9515 531 path.AddLineToPoint( xc, yc );
1b89a5cd 532 m_graphicContext->DrawPath(path);
1b89a5cd
SC
533}
534
888dde65 535void wxGCDCImpl::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
1b89a5cd
SC
536 double sa, double ea )
537{
888dde65 538 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
1b89a5cd 539
4280b879 540 if ( !m_logicalFunctionSupported )
1b89a5cd
SC
541 return;
542
1b89a5cd 543 m_graphicContext->PushState();
d9485f89 544 m_graphicContext->Translate(x+w/2.0,y+h/2.0);
0ebd9515 545 wxDouble factor = ((wxDouble) w) / h;
1b89a5cd 546 m_graphicContext->Scale( factor , 1.0);
d9485f89 547
1b89a5cd
SC
548 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
549 // get clockwise angles
fcaea2fa
SC
550 if ( m_brush.GetStyle() != wxTRANSPARENT )
551 {
552 wxGraphicsPath path = m_graphicContext->CreatePath();
553 path.MoveToPoint( 0, 0 );
47c1bb95 554 path.AddArc( 0, 0, h/2.0 , DegToRad(-sa) , DegToRad(-ea), sa > ea );
fcaea2fa
SC
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 );
fcaea2fa
SC
560 m_graphicContext->StrokePath( path );
561 }
562 else
563 {
564 wxGraphicsPath path = m_graphicContext->CreatePath();
47c1bb95
SC
565 path.AddArc( 0, 0, h/2.0 , DegToRad(-sa) , DegToRad(-ea), sa > ea );
566 m_graphicContext->DrawPath( path );
fcaea2fa
SC
567 }
568
1b89a5cd 569 m_graphicContext->PopState();
1b89a5cd
SC
570}
571
888dde65 572void wxGCDCImpl::DoDrawPoint( wxCoord x, wxCoord y )
1b89a5cd 573{
888dde65 574 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
1b89a5cd
SC
575
576 DoDrawLine( x , y , x + 1 , y + 1 );
577}
578
888dde65 579void wxGCDCImpl::DoDrawLines(int n, wxPoint points[],
1b89a5cd
SC
580 wxCoord xoffset, wxCoord yoffset)
581{
888dde65 582 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
1b89a5cd 583
4280b879 584 if ( !m_logicalFunctionSupported )
1b89a5cd 585 return;
1b89a5cd
SC
586
587 wxPoint2DDouble* pointsD = new wxPoint2DDouble[n];
588 for( int i = 0; i < n; ++i)
589 {
0ebd9515
SC
590 pointsD[i].m_x = points[i].x + xoffset;
591 pointsD[i].m_y = points[i].y + yoffset;
1b89a5cd
SC
592 }
593
594 m_graphicContext->StrokeLines( n , pointsD);
595 delete[] pointsD;
596}
597
598#if wxUSE_SPLINES
888dde65 599void wxGCDCImpl::DoDrawSpline(const wxPointList *points)
1b89a5cd 600{
888dde65 601 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
1b89a5cd 602
4280b879 603 if ( !m_logicalFunctionSupported )
1b89a5cd
SC
604 return;
605
a4e73390 606 wxGraphicsPath path = m_graphicContext->CreatePath();
1b89a5cd 607
b0d7707b
RR
608 wxPointList::compatibility_iterator node = points->GetFirst();
609 if (node == wxPointList::compatibility_iterator())
1b89a5cd
SC
610 // empty list
611 return;
612
b0d7707b 613 wxPoint *p = node->GetData();
1b89a5cd
SC
614
615 wxCoord x1 = p->x;
616 wxCoord y1 = p->y;
617
618 node = node->GetNext();
b0d7707b 619 p = node->GetData();
1b89a5cd
SC
620
621 wxCoord x2 = p->x;
622 wxCoord y2 = p->y;
623 wxCoord cx1 = ( x1 + x2 ) / 2;
624 wxCoord cy1 = ( y1 + y2 ) / 2;
625
0ebd9515
SC
626 path.MoveToPoint( x1 , y1 );
627 path.AddLineToPoint( cx1 , cy1 );
1b89a5cd
SC
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 {
b0d7707b 637 p = node->GetData();
1b89a5cd
SC
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
0ebd9515 645 path.AddQuadCurveToPoint(x1 , y1 ,cx4 , cy4 );
1b89a5cd
SC
646
647 cx1 = cx4;
648 cy1 = cy4;
649 }
650
0ebd9515 651 path.AddLineToPoint( x2 , y2 );
1b89a5cd
SC
652
653 m_graphicContext->StrokePath( path );
1b89a5cd
SC
654}
655#endif // wxUSE_SPLINES
656
888dde65 657void wxGCDCImpl::DoDrawPolygon( int n, wxPoint points[],
1b89a5cd
SC
658 wxCoord xoffset, wxCoord yoffset,
659 int fillStyle )
660{
888dde65 661 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
1b89a5cd
SC
662
663 if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) )
664 return;
4280b879 665 if ( !m_logicalFunctionSupported )
1b89a5cd
SC
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 {
0ebd9515
SC
675 pointsD[i].m_x = points[i].x + xoffset;
676 pointsD[i].m_y = points[i].y + yoffset;
1b89a5cd
SC
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
888dde65 685void wxGCDCImpl::DoDrawPolyPolygon(int n,
1b89a5cd
SC
686 int count[],
687 wxPoint points[],
688 wxCoord xoffset,
689 wxCoord yoffset,
690 int fillStyle)
691{
692 wxASSERT(n > 1);
a4e73390 693 wxGraphicsPath path = m_graphicContext->CreatePath();
1b89a5cd
SC
694
695 int i = 0;
696 for ( int j = 0; j < n; ++j)
697 {
698 wxPoint start = points[i];
0ebd9515 699 path.MoveToPoint( start.x+ xoffset, start.y+ yoffset);
1b89a5cd
SC
700 ++i;
701 int l = count[j];
702 for ( int k = 1; k < l; ++k)
703 {
0ebd9515 704 path.AddLineToPoint( points[i].x+ xoffset, points[i].y+ yoffset);
1b89a5cd
SC
705 ++i;
706 }
707 // close the polygon
708 if ( start != points[i-1])
0ebd9515 709 path.AddLineToPoint( start.x+ xoffset, start.y+ yoffset);
1b89a5cd
SC
710 }
711 m_graphicContext->DrawPath( path , fillStyle);
1b89a5cd
SC
712}
713
888dde65 714void wxGCDCImpl::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
1b89a5cd 715{
888dde65 716 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
1b89a5cd 717
4280b879 718 if ( !m_logicalFunctionSupported )
1b89a5cd
SC
719 return;
720
1b89a5cd 721 // CMB: draw nothing if transformed w or h is 0
0ebd9515 722 if (w == 0 || h == 0)
1b89a5cd
SC
723 return;
724
5ebfdf41
SC
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
0ebd9515
SC
729 w -= 1;
730 h -= 1;
5ebfdf41 731 }
0ebd9515 732 m_graphicContext->DrawRectangle(x,y,w,h);
1b89a5cd
SC
733}
734
888dde65 735void wxGCDCImpl::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
0ebd9515 736 wxCoord w, wxCoord h,
1b89a5cd
SC
737 double radius)
738{
888dde65 739 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
1b89a5cd 740
4280b879 741 if ( !m_logicalFunctionSupported )
1b89a5cd
SC
742 return;
743
744 if (radius < 0.0)
0ebd9515 745 radius = - radius * ((w < h) ? w : h);
1b89a5cd
SC
746
747 // CMB: draw nothing if transformed w or h is 0
0ebd9515 748 if (w == 0 || h == 0)
1b89a5cd
SC
749 return;
750
d9485f89
RD
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 }
0ebd9515 758 m_graphicContext->DrawRoundedRectangle( x,y,w,h,radius);
1b89a5cd
SC
759}
760
888dde65 761void wxGCDCImpl::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
1b89a5cd 762{
888dde65 763 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
1b89a5cd 764
4280b879 765 if ( !m_logicalFunctionSupported )
1b89a5cd
SC
766 return;
767
d9485f89
RD
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 }
0ebd9515 775 m_graphicContext->DrawEllipse(x,y,w,h);
1b89a5cd
SC
776}
777
888dde65 778bool wxGCDCImpl::CanDrawBitmap() const
1b89a5cd
SC
779{
780 return true;
781}
782
888dde65 783bool wxGCDCImpl::DoBlit(
1b89a5cd 784 wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
e3b81044 785 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask,
1b89a5cd
SC
786 wxCoord xsrcMask, wxCoord ysrcMask )
787{
e3b81044
VZ
788 return DoStretchBlit( xdest, ydest, width, height,
789 source, xsrc, ysrc, width, height, logical_func, useMask,
790 xsrcMask,ysrcMask );
791}
792
888dde65 793bool wxGCDCImpl::DoStretchBlit(
e3b81044
VZ
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{
888dde65
RR
799 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
800 wxCHECK_MSG( source->IsOk(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
6c0aace2 801
1b89a5cd
SC
802 if ( logical_func == wxNO_OP )
803 return true;
fcaea2fa 804 else if ( !m_graphicContext->SetLogicalFunction( logical_func ) )
b02d4340 805
c64c9cd3
KO
806 {
807 wxFAIL_MSG( wxT("Blitting is only supported with wxCOPY logical operation.") );
808 return false;
809 }
1b89a5cd
SC
810
811 if (xsrcMask == -1 && ysrcMask == -1)
812 {
813 xsrcMask = xsrc;
814 ysrcMask = ysrc;
815 }
816
cce4a2ce
RD
817 wxRect subrect(source->LogicalToDeviceX(xsrc),
818 source->LogicalToDeviceY(ysrc),
e3b81044
VZ
819 source->LogicalToDeviceXRel(srcWidth),
820 source->LogicalToDeviceYRel(srcHeight));
cce4a2ce
RD
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;
6c0aace2 831
64c8307c 832 wxBitmap blit = source->GetAsBitmap( &subrect );
6c0aace2 833
888dde65 834 if ( blit.IsOk() )
c64c9cd3 835 {
cce4a2ce 836 m_graphicContext->DrawBitmap( blit, xdest, ydest,
e3b81044 837 dstWidth, dstHeight);
c64c9cd3 838 }
1b89a5cd
SC
839 else
840 {
c64c9cd3 841 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
1b89a5cd
SC
842 return false;
843 }
844
fcaea2fa
SC
845 // reset logical function
846 m_graphicContext->SetLogicalFunction( m_logicalFunction );
847
1b89a5cd
SC
848 return true;
849}
850
888dde65 851void wxGCDCImpl::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
1b89a5cd
SC
852 double angle)
853{
888dde65 854 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1b89a5cd
SC
855
856 if ( str.length() == 0 )
857 return;
4280b879 858 if ( !m_logicalFunctionSupported )
1b89a5cd
SC
859 return;
860
068eb463
SC
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) ) );
1b89a5cd
SC
865}
866
888dde65 867void wxGCDCImpl::DoDrawText(const wxString& str, wxCoord x, wxCoord y)
1b89a5cd 868{
888dde65 869 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
1b89a5cd
SC
870
871 if ( str.length() == 0 )
872 return;
4280b879
SC
873
874 if ( !m_logicalFunctionSupported )
1b89a5cd
SC
875 return;
876
068eb463
SC
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) ) );
1b89a5cd
SC
881}
882
888dde65 883bool wxGCDCImpl::CanGetTextExtent() const
1b89a5cd 884{
888dde65 885 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
1b89a5cd
SC
886
887 return true;
888}
889
888dde65 890void wxGCDCImpl::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height,
1b89a5cd 891 wxCoord *descent, wxCoord *externalLeading ,
c94f845b 892 const wxFont *theFont ) const
1b89a5cd 893{
888dde65 894 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
1b89a5cd
SC
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 )
fcaea2fa 906 *height = (wxCoord)(h+0.5);
1b89a5cd 907 if ( descent )
fcaea2fa 908 *descent = (wxCoord)(d+0.5);
1b89a5cd 909 if ( externalLeading )
fcaea2fa 910 *externalLeading = (wxCoord)(e+0.5);
1b89a5cd 911 if ( width )
fcaea2fa 912 *width = (wxCoord)(w+0.5);
1b89a5cd
SC
913
914 if ( theFont )
915 {
916 m_graphicContext->SetFont( m_font, m_textForegroundColour );
917 }
918}
919
888dde65 920bool wxGCDCImpl::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
1b89a5cd 921{
888dde65 922 wxCHECK_MSG( IsOk(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1b89a5cd
SC
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 )
0ebd9515 932 widths[i] = (wxCoord)(widthsD[i] + 0.5);
1b89a5cd
SC
933
934 return true;
935}
936
888dde65 937wxCoord wxGCDCImpl::GetCharWidth(void) const
1b89a5cd
SC
938{
939 wxCoord width;
940 DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL );
941
942 return width;
943}
944
888dde65 945wxCoord wxGCDCImpl::GetCharHeight(void) const
1b89a5cd
SC
946{
947 wxCoord height;
948 DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL );
949
950 return height;
951}
952
888dde65 953void wxGCDCImpl::Clear(void)
1b89a5cd 954{
888dde65 955 wxCHECK_RET( IsOk(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1b89a5cd
SC
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 );
6c0aace2 961 m_graphicContext->SetPen( m_pen );
1b89a5cd
SC
962 m_graphicContext->SetBrush( m_brush );
963}
964
888dde65 965void wxGCDCImpl::DoGetSize(int *width, int *height) const
1b89a5cd 966{
1cc7d13a
SC
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);
1b89a5cd
SC
974}
975
888dde65 976void wxGCDCImpl::DoGradientFillLinear(const wxRect& rect,
1b89a5cd
SC
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
0ebd9515 1009 if (rect.width == 0 || rect.height == 0)
1b89a5cd
SC
1010 return;
1011
0ebd9515
SC
1012 m_graphicContext->SetBrush( m_graphicContext->CreateLinearGradientBrush(
1013 start.x,start.y,end.x,end.y, initialColour, destColour));
1b89a5cd 1014 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
0ebd9515 1015 m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height);
1b89a5cd
SC
1016 m_graphicContext->SetPen(m_pen);
1017}
1018
888dde65 1019void wxGCDCImpl::DoGradientFillConcentric(const wxRect& rect,
1b89a5cd
SC
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
0ebd9515 1033 // make sure the background is filled (todo move into specific platform implementation ?)
1b89a5cd
SC
1034 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
1035 m_graphicContext->SetBrush( wxBrush( destColour) );
0ebd9515 1036 m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height);
1b89a5cd
SC
1037
1038 m_graphicContext->SetBrush( m_graphicContext->CreateRadialGradientBrush(
0ebd9515
SC
1039 rect.x+circleCenter.x,rect.y+circleCenter.y,
1040 rect.x+circleCenter.x,rect.y+circleCenter.y,
1041 nRadius,initialColour,destColour));
1b89a5cd 1042
0ebd9515 1043 m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height);
1b89a5cd
SC
1044 m_graphicContext->SetPen(m_pen);
1045}
1046
888dde65 1047void wxGCDCImpl::DoDrawCheckMark(wxCoord x, wxCoord y,
1b89a5cd
SC
1048 wxCoord width, wxCoord height)
1049{
888dde65 1050 wxDCImpl::DoDrawCheckMark(x,y,width,height);
1b89a5cd
SC
1051}
1052
1053#endif // wxUSE_GRAPHICS_CONTEXT