]> git.saurik.com Git - wxWidgets.git/blame - src/common/graphcmn.cpp
avoid undefined behavior from token paste resulting in more than one token
[wxWidgets.git] / src / common / graphcmn.cpp
CommitLineData
50581042
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/common/graphcmn.cpp
3// Purpose: graphics context methods common to all platforms
4// Author: Stefan Csomor
5// Modified by:
6// Created:
7// RCS-ID: $Id$
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
50581042
SC
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
9fd707a5
PC
19#if wxUSE_GRAPHICS_CONTEXT
20
66bc98cc 21#include "wx/private/graphics.h"
50581042 22
539872ae
SC
23#ifndef WX_PRECOMP
24 #include "wx/icon.h"
25 #include "wx/bitmap.h"
26 #include "wx/dcmemory.h"
9fd707a5 27 #include "wx/region.h"
4f40354a 28 #include "wx/log.h"
539872ae
SC
29#endif
30
d94228a2
SC
31//-----------------------------------------------------------------------------
32// constants
33//-----------------------------------------------------------------------------
34
9fd707a5 35static const double RAD2DEG = 180.0 / M_PI;
d94228a2
SC
36
37//-----------------------------------------------------------------------------
38// Local functions
39//-----------------------------------------------------------------------------
40
d94228a2
SC
41static inline double DegToRad(double deg)
42{
43 return (deg * M_PI) / 180.0;
44}
d94228a2 45
9fd707a5 46//-----------------------------------------------------------------------------
d94228a2 47
2c820406
SC
48//-----------------------------------------------------------------------------
49// wxGraphicsObject
50//-----------------------------------------------------------------------------
51
facbe363
SC
52IMPLEMENT_DYNAMIC_CLASS(wxGraphicsObject, wxObject)
53
2c820406
SC
54wxGraphicsObjectRefData::wxGraphicsObjectRefData( wxGraphicsRenderer* renderer )
55{
56 m_renderer = renderer;
57}
58wxGraphicsObjectRefData::wxGraphicsObjectRefData( const wxGraphicsObjectRefData* data )
59{
60 m_renderer = data->m_renderer;
61}
62wxGraphicsRenderer* wxGraphicsObjectRefData::GetRenderer() const
63{
64 return m_renderer ;
65}
facbe363 66
2c820406
SC
67wxGraphicsObjectRefData* wxGraphicsObjectRefData::Clone() const
68{
69 return new wxGraphicsObjectRefData(this);
70}
facbe363 71
2c820406
SC
72wxGraphicsObject::wxGraphicsObject()
73{
74}
facbe363 75
2c820406
SC
76wxGraphicsObject::wxGraphicsObject( wxGraphicsRenderer* renderer )
77{
78 SetRefData( new wxGraphicsObjectRefData(renderer));
79}
facbe363 80
2c820406
SC
81wxGraphicsObject::~wxGraphicsObject()
82{
83}
facbe363 84
2c820406
SC
85bool wxGraphicsObject::IsNull() const
86{
87 return m_refData == NULL;
88}
89
90wxGraphicsRenderer* wxGraphicsObject::GetRenderer() const
91{
92 return ( IsNull() ? NULL : GetGraphicsData()->GetRenderer() );
93}
94
95wxGraphicsObjectRefData* wxGraphicsObject::GetGraphicsData() const
96{
97 return (wxGraphicsObjectRefData*) m_refData;
98}
99
100wxObjectRefData* wxGraphicsObject::CreateRefData() const
101{
102 wxLogDebug(wxT("A Null Object cannot be changed"));
103 return NULL;
104}
105
106wxObjectRefData* wxGraphicsObject::CloneRefData(const wxObjectRefData* data) const
107{
108 const wxGraphicsObjectRefData* ptr = (const wxGraphicsObjectRefData*) data;
109 return ptr->Clone();
110}
111
112//-----------------------------------------------------------------------------
113// pens etc.
114//-----------------------------------------------------------------------------
115
116IMPLEMENT_DYNAMIC_CLASS(wxGraphicsPen, wxGraphicsObject)
117IMPLEMENT_DYNAMIC_CLASS(wxGraphicsBrush, wxGraphicsObject)
118IMPLEMENT_DYNAMIC_CLASS(wxGraphicsFont, wxGraphicsObject)
a4e73390 119
2c820406
SC
120WXDLLIMPEXP_DATA_CORE(wxGraphicsPen) wxNullGraphicsPen;
121WXDLLIMPEXP_DATA_CORE(wxGraphicsBrush) wxNullGraphicsBrush;
122WXDLLIMPEXP_DATA_CORE(wxGraphicsFont) wxNullGraphicsFont;
123
a4e73390
SC
124//-----------------------------------------------------------------------------
125// matrix
126//-----------------------------------------------------------------------------
127
128IMPLEMENT_DYNAMIC_CLASS(wxGraphicsMatrix, wxGraphicsObject)
129WXDLLIMPEXP_DATA_CORE(wxGraphicsMatrix) wxNullGraphicsMatrix;
130
131// concatenates the matrix
132void wxGraphicsMatrix::Concat( const wxGraphicsMatrix *t )
133{
134 AllocExclusive();
135 GetMatrixData()->Concat(t->GetMatrixData());
136}
137
138// sets the matrix to the respective values
139void wxGraphicsMatrix::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
140 wxDouble tx, wxDouble ty)
141{
142 AllocExclusive();
143 GetMatrixData()->Set(a,b,c,d,tx,ty);
144}
145
248802d0
RD
146// gets the component valuess of the matrix
147void wxGraphicsMatrix::Get(wxDouble* a, wxDouble* b, wxDouble* c,
148 wxDouble* d, wxDouble* tx, wxDouble* ty) const
149{
150 GetMatrixData()->Get(a, b, c, d, tx, ty);
151}
152
a4e73390
SC
153// makes this the inverse matrix
154void wxGraphicsMatrix::Invert()
155{
156 AllocExclusive();
157 GetMatrixData()->Invert();
158}
159
160// returns true if the elements of the transformation matrix are equal ?
161bool wxGraphicsMatrix::IsEqual( const wxGraphicsMatrix* t) const
162{
163 return GetMatrixData()->IsEqual(t->GetMatrixData());
164}
165
166// return true if this is the identity matrix
167bool wxGraphicsMatrix::IsIdentity() const
168{
169 return GetMatrixData()->IsIdentity();
170}
171
172// add the translation to this matrix
173void wxGraphicsMatrix::Translate( wxDouble dx , wxDouble dy )
174{
175 AllocExclusive();
176 GetMatrixData()->Translate(dx,dy);
177}
178
179// add the scale to this matrix
180void wxGraphicsMatrix::Scale( wxDouble xScale , wxDouble yScale )
181{
182 AllocExclusive();
183 GetMatrixData()->Scale(xScale,yScale);
184}
185
186// add the rotation to this matrix (radians)
187void wxGraphicsMatrix::Rotate( wxDouble angle )
188{
189 AllocExclusive();
190 GetMatrixData()->Rotate(angle);
191}
192
193//
194// apply the transforms
195//
196
197// applies that matrix to the point
198void wxGraphicsMatrix::TransformPoint( wxDouble *x, wxDouble *y ) const
199{
200 GetMatrixData()->TransformPoint(x,y);
201}
202
203// applies the matrix except for translations
204void wxGraphicsMatrix::TransformDistance( wxDouble *dx, wxDouble *dy ) const
205{
206 GetMatrixData()->TransformDistance(dx,dy);
207}
8ddf8e82 208
a4e73390
SC
209// returns the native representation
210void * wxGraphicsMatrix::GetNativeMatrix() const
211{
212 return GetMatrixData()->GetNativeMatrix();
213}
214
215//-----------------------------------------------------------------------------
216// path
217//-----------------------------------------------------------------------------
218
219IMPLEMENT_DYNAMIC_CLASS(wxGraphicsPath, wxGraphicsObject)
1ea0eb4e 220WXDLLIMPEXP_DATA_CORE(wxGraphicsPath) wxNullGraphicsPath;
a4e73390
SC
221
222// convenience functions, for using wxPoint2DDouble etc
223
224wxPoint2DDouble wxGraphicsPath::GetCurrentPoint() const
50581042 225{
a2d6d210 226 wxDouble x,y;
a4e73390 227 GetCurrentPoint(&x,&y);
50581042
SC
228 return wxPoint2DDouble(x,y);
229}
230
231void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble& p)
232{
233 MoveToPoint( p.m_x , p.m_y);
234}
235
236void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble& p)
237{
238 AddLineToPoint( p.m_x , p.m_y);
239}
240
241void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble& c1, const wxPoint2DDouble& c2, const wxPoint2DDouble& e)
242{
243 AddCurveToPoint(c1.m_x, c1.m_y, c2.m_x, c2.m_y, e.m_x, e.m_y);
244}
245
246void wxGraphicsPath::AddArc( const wxPoint2DDouble& c, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise)
247{
248 AddArc(c.m_x, c.m_y, r, startAngle, endAngle, clockwise);
249}
250
a4e73390 251wxRect2DDouble wxGraphicsPath::GetBox() const
facbe363
SC
252{
253 wxDouble x,y,w,h;
254 GetBox(&x,&y,&w,&h);
255 return wxRect2DDouble( x,y,w,h );
256}
257
a4e73390 258bool wxGraphicsPath::Contains( const wxPoint2DDouble& c, int fillStyle ) const
c907796b
RD
259{
260 return Contains( c.m_x, c.m_y, fillStyle);
261}
262
a4e73390
SC
263// true redirections
264
265// begins a new subpath at (x,y)
266void wxGraphicsPath::MoveToPoint( wxDouble x, wxDouble y )
267{
268 AllocExclusive();
269 GetPathData()->MoveToPoint(x,y);
270}
271
272// adds a straight line from the current point to (x,y)
273void wxGraphicsPath::AddLineToPoint( wxDouble x, wxDouble y )
274{
275 AllocExclusive();
276 GetPathData()->AddLineToPoint(x,y);
277}
278
279// adds a cubic Bezier curve from the current point, using two control points and an end point
280void wxGraphicsPath::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
281{
282 AllocExclusive();
283 GetPathData()->AddCurveToPoint(cx1,cy1,cx2,cy2,x,y);
284}
285
286// adds another path
287void wxGraphicsPath::AddPath( const wxGraphicsPath& path )
288{
289 AllocExclusive();
290 GetPathData()->AddPath(path.GetPathData());
291}
292
293// closes the current sub-path
294void wxGraphicsPath::CloseSubpath()
295{
296 AllocExclusive();
297 GetPathData()->CloseSubpath();
298}
299
300// gets the last point of the current path, (0,0) if not yet set
301void wxGraphicsPath::GetCurrentPoint( wxDouble* x, wxDouble* y) const
302{
303 GetPathData()->GetCurrentPoint(x,y);
304}
305
306// adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
307void wxGraphicsPath::AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise )
308{
309 AllocExclusive();
310 GetPathData()->AddArc(x,y,r,startAngle,endAngle,clockwise);
311}
312
50581042 313//
a4e73390
SC
314// These are convenience functions which - if not available natively will be assembled
315// using the primitives from above
50581042
SC
316//
317
a4e73390 318// adds a quadratic Bezier curve from the current point, using a control point and an end point
50581042 319void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y )
a4e73390
SC
320{
321 AllocExclusive();
322 GetPathData()->AddQuadCurveToPoint(cx,cy,x,y);
323}
324
325// appends a rectangle as a new closed subpath
326void wxGraphicsPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
327{
328 AllocExclusive();
329 GetPathData()->AddRectangle(x,y,w,h);
330}
331
332// appends an ellipsis as a new closed subpath fitting the passed rectangle
333void wxGraphicsPath::AddCircle( wxDouble x, wxDouble y, wxDouble r )
334{
335 AllocExclusive();
336 GetPathData()->AddCircle(x,y,r);
337}
338
339// appends a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1)
340void wxGraphicsPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
341{
342 GetPathData()->AddArcToPoint(x1,y1,x2,y2,r);
343}
344
345// appends an ellipse
346void wxGraphicsPath::AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
347{
348 AllocExclusive();
349 GetPathData()->AddEllipse(x,y,w,h);
350}
351
352// appends a rounded rectangle
353void wxGraphicsPath::AddRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius)
354{
355 AllocExclusive();
356 GetPathData()->AddRoundedRectangle(x,y,w,h,radius);
357}
358
359// returns the native path
360void * wxGraphicsPath::GetNativePath() const
361{
362 return GetPathData()->GetNativePath();
363}
364
365// give the native path returned by GetNativePath() back (there might be some deallocations necessary)
366void wxGraphicsPath::UnGetNativePath(void *p)const
367{
368 GetPathData()->UnGetNativePath(p);
369}
370
371// transforms each point of this path by the matrix
372void wxGraphicsPath::Transform( const wxGraphicsMatrix& matrix )
373{
374 AllocExclusive();
375 GetPathData()->Transform(matrix.GetMatrixData());
376}
377
378// gets the bounding box enclosing all points (possibly including control points)
379void wxGraphicsPath::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
380{
381 GetPathData()->GetBox(x,y,w,h);
382}
383
384bool wxGraphicsPath::Contains( wxDouble x, wxDouble y, int fillStyle ) const
385{
386 return GetPathData()->Contains(x,y,fillStyle);
387}
388
389//
390// Emulations, these mus be implemented in the ...Data classes in order to allow for proper overrides
391//
392
393void wxGraphicsPathData::AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y )
50581042
SC
394{
395 // calculate using degree elevation to a cubic bezier
a2d6d210
VZ
396 wxPoint2DDouble c1;
397 wxPoint2DDouble c2;
50581042 398
a4e73390
SC
399 wxPoint2DDouble start;
400 GetCurrentPoint(&start.m_x,&start.m_y);
50581042
SC
401 wxPoint2DDouble end(x,y);
402 wxPoint2DDouble c(cx,cy);
5ce3abfb 403 c1 = wxDouble(1/3.0) * start + wxDouble(2/3.0) * c;
a2d6d210 404 c2 = wxDouble(2/3.0) * c + wxDouble(1/3.0) * end;
50581042
SC
405 AddCurveToPoint(c1.m_x,c1.m_y,c2.m_x,c2.m_y,x,y);
406}
407
a4e73390 408void wxGraphicsPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
50581042
SC
409{
410 MoveToPoint(x,y);
411 AddLineToPoint(x,y+h);
412 AddLineToPoint(x+w,y+h);
413 AddLineToPoint(x+w,y);
414 CloseSubpath();
415}
416
a4e73390 417void wxGraphicsPathData::AddCircle( wxDouble x, wxDouble y, wxDouble r )
50581042
SC
418{
419 MoveToPoint(x+r,y);
420 AddArc( x,y,r,0,2*M_PI,false);
421 CloseSubpath();
422}
423
a4e73390 424void wxGraphicsPathData::AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
59720690
SC
425{
426 wxDouble rw = w/2;
427 wxDouble rh = h/2;
428 wxDouble xc = x + rw;
429 wxDouble yc = y + rh;
a4e73390
SC
430 wxGraphicsMatrix m = GetRenderer()->CreateMatrix();
431 m.Translate(xc,yc);
432 m.Scale(rw/rh,1.0);
433 wxGraphicsPath p = GetRenderer()->CreatePath();
434 p.AddCircle(0,0,rh);
435 p.Transform(m);
436 AddPath(p.GetPathData());
59720690
SC
437}
438
a4e73390 439void wxGraphicsPathData::AddRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius)
59720690
SC
440{
441 if ( radius == 0 )
442 AddRectangle(x,y,w,h);
443 else
444 {
445 MoveToPoint( x + w, y + h / 2);
446 AddArcToPoint(x + w, y + h, x + w / 2, y + h, radius);
447 AddArcToPoint(x, y + h, x, y + h / 2, radius);
448 AddArcToPoint(x, y , x + w / 2, y, radius);
449 AddArcToPoint(x + w, y, x + w, y + h / 2, radius);
450 CloseSubpath();
451 }
452}
453
d94228a2 454// draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1)
a4e73390 455void wxGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
d94228a2 456{
a4e73390
SC
457 wxPoint2DDouble current;
458 GetCurrentPoint(&current.m_x,&current.m_y);
d94228a2
SC
459 wxPoint2DDouble p1(x1,y1);
460 wxPoint2DDouble p2(x2,y2);
461
a2d6d210 462 wxPoint2DDouble v1 = current - p1;
d94228a2 463 v1.Normalize();
a2d6d210 464 wxPoint2DDouble v2 = p2 - p1;
d94228a2
SC
465 v2.Normalize();
466
467 wxDouble alpha = v1.GetVectorAngle() - v2.GetVectorAngle();
468
469 if ( alpha < 0 )
a2d6d210 470 alpha = 360 + alpha;
d94228a2
SC
471 // TODO obtuse angles
472
473 alpha = DegToRad(alpha);
474
a2d6d210 475 wxDouble dist = r / sin(alpha/2) * cos(alpha/2);
d94228a2 476 // calculate tangential points
a2d6d210
VZ
477 wxPoint2DDouble t1 = dist*v1 + p1;
478 wxPoint2DDouble t2 = dist*v2 + p1;
d94228a2 479
a2d6d210 480 wxPoint2DDouble nv1 = v1;
d94228a2
SC
481 nv1.SetVectorAngle(v1.GetVectorAngle()-90);
482 wxPoint2DDouble c = t1 + r*nv1;
483
a2d6d210
VZ
484 wxDouble a1 = v1.GetVectorAngle()+90;
485 wxDouble a2 = v2.GetVectorAngle()-90;
d94228a2 486
a4e73390 487 AddLineToPoint(t1.m_x,t1.m_y);
d94228a2 488 AddArc(c.m_x,c.m_y,r,DegToRad(a1),DegToRad(a2),true);
a4e73390 489 AddLineToPoint(p2.m_x,p2.m_y);
d94228a2
SC
490}
491
50581042
SC
492//-----------------------------------------------------------------------------
493// wxGraphicsContext Convenience Methods
494//-----------------------------------------------------------------------------
495
8ddf8e82
SC
496IMPLEMENT_ABSTRACT_CLASS(wxGraphicsContext, wxObject)
497
facbe363
SC
498
499wxGraphicsContext::wxGraphicsContext(wxGraphicsRenderer* renderer) : wxGraphicsObject(renderer)
500{
4280b879 501 m_logicalFunction = wxCOPY;
facbe363
SC
502}
503
504wxGraphicsContext::~wxGraphicsContext()
505{
facbe363
SC
506}
507
c29bdd5f 508bool wxGraphicsContext::StartDoc(const wxString& WXUNUSED(message))
fcaea2fa
SC
509{
510 return true;
511}
512
513void wxGraphicsContext::EndDoc()
514{
515}
516
c29bdd5f
VZ
517void wxGraphicsContext::StartPage(wxDouble WXUNUSED(width),
518 wxDouble WXUNUSED(height))
fcaea2fa
SC
519{
520}
521
522void wxGraphicsContext::EndPage()
523{
524}
525
526void wxGraphicsContext::Flush()
527{
528}
529
530#if 0
531void wxGraphicsContext::SetAlpha( wxDouble WXUNUSED(alpha) )
532{
533}
534
535wxDouble wxGraphicsContext::GetAlpha() const
536{
537 return 1.0;
538}
539#endif
540
541void wxGraphicsContext::GetSize( wxDouble* width, wxDouble* height)
542{
543 *width = 10000.0;
544 *height = 10000.0;
545}
546
547void wxGraphicsContext::GetDPI( wxDouble* dpiX, wxDouble* dpiY)
548{
549 *dpiX = 72.0;
550 *dpiY = 72.0;
551}
552
facbe363 553// sets the pen
2c820406 554void wxGraphicsContext::SetPen( const wxGraphicsPen& pen )
facbe363 555{
facbe363 556 m_pen = pen;
facbe363
SC
557}
558
559void wxGraphicsContext::SetPen( const wxPen& pen )
560{
ad667945 561 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
2c820406 562 SetPen( wxNullGraphicsPen );
facbe363
SC
563 else
564 SetPen( CreatePen( pen ) );
565}
566
567// sets the brush for filling
2c820406 568void wxGraphicsContext::SetBrush( const wxGraphicsBrush& brush )
facbe363 569{
facbe363 570 m_brush = brush;
facbe363
SC
571}
572
573void wxGraphicsContext::SetBrush( const wxBrush& brush )
574{
ad667945 575 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
2c820406 576 SetBrush( wxNullGraphicsBrush );
facbe363
SC
577 else
578 SetBrush( CreateBrush( brush ) );
579}
580
581// sets the brush for filling
2c820406 582void wxGraphicsContext::SetFont( const wxGraphicsFont& font )
facbe363 583{
facbe363 584 m_font = font;
facbe363
SC
585}
586
4280b879
SC
587bool wxGraphicsContext::SetLogicalFunction( int function )
588{
589 if ( function == wxCOPY )
590 {
591 m_logicalFunction = function;
592 return true;
593 }
594 return false;
595}
596
facbe363
SC
597void wxGraphicsContext::SetFont( const wxFont& font, const wxColour& colour )
598{
2c820406
SC
599 if ( font.Ok() )
600 SetFont( CreateFont( font, colour ) );
601 else
602 SetFont( wxNullGraphicsFont );
facbe363
SC
603}
604
a4e73390 605void wxGraphicsContext::DrawPath( const wxGraphicsPath& path, int fillStyle )
50581042
SC
606{
607 FillPath( path , fillStyle );
608 StrokePath( path );
609}
610
611void wxGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle )
612{
a2d6d210 613 Translate(x,y);
50581042
SC
614 Rotate( -angle );
615 DrawText( str , 0, 0 );
616 Rotate( angle );
a2d6d210 617 Translate(-x,-y);
50581042
SC
618}
619
068eb463
SC
620void wxGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, const wxGraphicsBrush& backgroundBrush )
621{
622 wxGraphicsBrush formerBrush = m_brush;
fcaea2fa 623 wxGraphicsPen formerPen = m_pen;
068eb463
SC
624 wxDouble width;
625 wxDouble height;
626 wxDouble descent;
627 wxDouble externalLeading;
628 GetTextExtent( str , &width, &height, &descent, &externalLeading );
629 SetBrush( backgroundBrush );
fcaea2fa
SC
630 // to make sure our 'OffsetToPixelBoundaries' doesn't move the fill shape
631 SetPen( wxNullGraphicsPen );
068eb463
SC
632
633 wxGraphicsPath path = CreatePath();
634 path.AddRectangle( x , y, width, height );
635 FillPath( path );
636
637 DrawText( str, x ,y);
638 SetBrush( formerBrush );
fcaea2fa 639 SetPen( formerPen );
068eb463
SC
640}
641
642void wxGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle, const wxGraphicsBrush& backgroundBrush )
643{
644 wxGraphicsBrush formerBrush = m_brush;
fcaea2fa 645 wxGraphicsPen formerPen = m_pen;
068eb463
SC
646
647 wxDouble width;
648 wxDouble height;
649 wxDouble descent;
650 wxDouble externalLeading;
651 GetTextExtent( str , &width, &height, &descent, &externalLeading );
652 SetBrush( backgroundBrush );
fcaea2fa
SC
653 // to make sure our 'OffsetToPixelBoundaries' doesn't move the fill shape
654 SetPen( wxNullGraphicsPen );
068eb463
SC
655
656 wxGraphicsPath path = CreatePath();
657 path.MoveToPoint( x , y );
658 path.AddLineToPoint( (int) (x + sin(angle) * height) , (int) (y + cos(angle) * height) );
659 path.AddLineToPoint(
660 (int) (x + sin(angle) * height + cos(angle) * width) ,
661 (int) (y + cos(angle) * height - sin(angle) * width));
662 path.AddLineToPoint((int) (x + cos(angle) * width) , (int) (y - sin(angle) * width) );
663 FillPath( path );
664 DrawText( str, x ,y, angle);
665 SetBrush( formerBrush );
fcaea2fa 666 SetPen( formerPen );
068eb463
SC
667}
668
50581042
SC
669void wxGraphicsContext::StrokeLine( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2)
670{
a4e73390
SC
671 wxGraphicsPath path = CreatePath();
672 path.MoveToPoint(x1, y1);
673 path.AddLineToPoint( x2, y2 );
50581042 674 StrokePath( path );
50581042
SC
675}
676
677void wxGraphicsContext::DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
678{
a4e73390
SC
679 wxGraphicsPath path = CreatePath();
680 path.AddRectangle( x , y , w , h );
50581042 681 DrawPath( path );
50581042
SC
682}
683
684void wxGraphicsContext::DrawEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
685{
a4e73390
SC
686 wxGraphicsPath path = CreatePath();
687 path.AddEllipse(x,y,w,h);
59720690 688 DrawPath(path);
50581042
SC
689}
690
691void wxGraphicsContext::DrawRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius)
692{
a4e73390
SC
693 wxGraphicsPath path = CreatePath();
694 path.AddRoundedRectangle(x,y,w,h,radius);
59720690 695 DrawPath(path);
50581042
SC
696}
697
698void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *points)
699{
700 wxASSERT(n > 1);
a4e73390
SC
701 wxGraphicsPath path = CreatePath();
702 path.MoveToPoint(points[0].m_x, points[0].m_y);
a2d6d210 703 for ( size_t i = 1; i < n; ++i)
a4e73390 704 path.AddLineToPoint( points[i].m_x, points[i].m_y );
50581042 705 StrokePath( path );
50581042
SC
706}
707
708void wxGraphicsContext::DrawLines( size_t n, const wxPoint2DDouble *points, int fillStyle)
709{
710 wxASSERT(n > 1);
a4e73390
SC
711 wxGraphicsPath path = CreatePath();
712 path.MoveToPoint(points[0].m_x, points[0].m_y);
a2d6d210 713 for ( size_t i = 1; i < n; ++i)
a4e73390 714 path.AddLineToPoint( points[i].m_x, points[i].m_y );
50581042 715 DrawPath( path , fillStyle);
50581042
SC
716}
717
718void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *beginPoints, const wxPoint2DDouble *endPoints)
719{
720 wxASSERT(n > 0);
a4e73390 721 wxGraphicsPath path = CreatePath();
a2d6d210 722 for ( size_t i = 0; i < n; ++i)
50581042 723 {
a4e73390
SC
724 path.MoveToPoint(beginPoints[i].m_x, beginPoints[i].m_y);
725 path.AddLineToPoint( endPoints[i].m_x, endPoints[i].m_y );
50581042
SC
726 }
727 StrokePath( path );
50581042
SC
728}
729
facbe363 730// create a 'native' matrix corresponding to these values
a4e73390
SC
731wxGraphicsMatrix wxGraphicsContext::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
732 wxDouble tx, wxDouble ty) const
50581042 733{
facbe363 734 return GetRenderer()->CreateMatrix(a,b,c,d,tx,ty);
50581042
SC
735}
736
a4e73390 737wxGraphicsPath wxGraphicsContext::CreatePath() const
06b9edb7
SC
738{
739 return GetRenderer()->CreatePath();
740}
741
a4e73390 742wxGraphicsPen wxGraphicsContext::CreatePen(const wxPen& pen) const
06b9edb7
SC
743{
744 return GetRenderer()->CreatePen(pen);
745}
746
a4e73390 747wxGraphicsBrush wxGraphicsContext::CreateBrush(const wxBrush& brush ) const
06b9edb7
SC
748{
749 return GetRenderer()->CreateBrush(brush);
750}
751
752// sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
2c820406 753wxGraphicsBrush wxGraphicsContext::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
a4e73390 754 const wxColour&c1, const wxColour&c2) const
06b9edb7
SC
755{
756 return GetRenderer()->CreateLinearGradientBrush(x1,y1,x2,y2,c1,c2);
757}
758
759// sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
760// with radius r and color cColor
2c820406 761wxGraphicsBrush wxGraphicsContext::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
a4e73390 762 const wxColour &oColor, const wxColour &cColor) const
06b9edb7
SC
763{
764 return GetRenderer()->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
765}
766
767// sets the font
a4e73390 768wxGraphicsFont wxGraphicsContext::CreateFont( const wxFont &font , const wxColour &col ) const
06b9edb7
SC
769{
770 return GetRenderer()->CreateFont(font,col);
771}
772
773wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC& dc)
774{
775 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc);
776}
888dde65 777
773ccc31
SC
778wxGraphicsContext* wxGraphicsContext::Create( const wxMemoryDC& dc)
779{
780 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc);
781}
06b9edb7
SC
782
783wxGraphicsContext* wxGraphicsContext::CreateFromNative( void * context )
784{
785 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeContext(context);
786}
787
788wxGraphicsContext* wxGraphicsContext::CreateFromNativeWindow( void * window )
789{
790 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeWindow(window);
791}
792
793wxGraphicsContext* wxGraphicsContext::Create( wxWindow* window )
794{
795 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(window);
796}
797
ad667945
SC
798wxGraphicsContext* wxGraphicsContext::Create()
799{
800 return wxGraphicsRenderer::GetDefaultRenderer()->CreateMeasuringContext();
801}
802
a4e73390
SC
803//-----------------------------------------------------------------------------
804// wxGraphicsRenderer
805//-----------------------------------------------------------------------------
806
807IMPLEMENT_ABSTRACT_CLASS(wxGraphicsRenderer, wxObject)
808
9fd707a5 809#endif // wxUSE_GRAPHICS_CONTEXT