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