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