]> git.saurik.com Git - wxWidgets.git/blame - src/common/graphcmn.cpp
Upgrade bundled zlib to 1.2.8.
[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:
caa42c43 6// Created:
50581042
SC
7// Copyright: (c) Stefan Csomor
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
50581042
SC
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#if defined(__BORLANDC__)
15 #pragma hdrstop
16#endif
17
9fd707a5
PC
18#if wxUSE_GRAPHICS_CONTEXT
19
00491f99 20#include "wx/graphics.h"
50581042 21
539872ae
SC
22#ifndef WX_PRECOMP
23 #include "wx/icon.h"
24 #include "wx/bitmap.h"
25 #include "wx/dcmemory.h"
9fd707a5 26 #include "wx/region.h"
4f40354a 27 #include "wx/log.h"
539872ae
SC
28#endif
29
00491f99 30#include "wx/private/graphics.h"
d94228a2
SC
31
32//-----------------------------------------------------------------------------
33// Local functions
34//-----------------------------------------------------------------------------
35
d94228a2
SC
36static inline double DegToRad(double deg)
37{
38 return (deg * M_PI) / 180.0;
39}
d94228a2 40
9fd707a5 41//-----------------------------------------------------------------------------
d94228a2 42
2c820406
SC
43//-----------------------------------------------------------------------------
44// wxGraphicsObject
45//-----------------------------------------------------------------------------
46
facbe363
SC
47IMPLEMENT_DYNAMIC_CLASS(wxGraphicsObject, wxObject)
48
2c820406
SC
49wxGraphicsObjectRefData::wxGraphicsObjectRefData( wxGraphicsRenderer* renderer )
50{
51 m_renderer = renderer;
52}
53wxGraphicsObjectRefData::wxGraphicsObjectRefData( const wxGraphicsObjectRefData* data )
54{
55 m_renderer = data->m_renderer;
56}
caa42c43
VZ
57wxGraphicsRenderer* wxGraphicsObjectRefData::GetRenderer() const
58{
59 return m_renderer ;
2c820406 60}
facbe363 61
caa42c43 62wxGraphicsObjectRefData* wxGraphicsObjectRefData::Clone() const
2c820406
SC
63{
64 return new wxGraphicsObjectRefData(this);
65}
facbe363 66
caa42c43 67wxGraphicsObject::wxGraphicsObject()
2c820406
SC
68{
69}
facbe363 70
caa42c43 71wxGraphicsObject::wxGraphicsObject( wxGraphicsRenderer* renderer )
2c820406
SC
72{
73 SetRefData( new wxGraphicsObjectRefData(renderer));
74}
facbe363 75
caa42c43 76wxGraphicsObject::~wxGraphicsObject()
2c820406
SC
77{
78}
facbe363 79
caa42c43
VZ
80bool wxGraphicsObject::IsNull() const
81{
82 return m_refData == NULL;
2c820406
SC
83}
84
caa42c43
VZ
85wxGraphicsRenderer* wxGraphicsObject::GetRenderer() const
86{
87 return ( IsNull() ? NULL : GetGraphicsData()->GetRenderer() );
2c820406
SC
88}
89
caa42c43
VZ
90wxGraphicsObjectRefData* wxGraphicsObject::GetGraphicsData() const
91{
92 return (wxGraphicsObjectRefData*) m_refData;
2c820406
SC
93}
94
95wxObjectRefData* wxGraphicsObject::CreateRefData() const
96{
97 wxLogDebug(wxT("A Null Object cannot be changed"));
98 return NULL;
99}
100
101wxObjectRefData* wxGraphicsObject::CloneRefData(const wxObjectRefData* data) const
102{
103 const wxGraphicsObjectRefData* ptr = (const wxGraphicsObjectRefData*) data;
104 return ptr->Clone();
105}
106
107//-----------------------------------------------------------------------------
108// pens etc.
109//-----------------------------------------------------------------------------
110
111IMPLEMENT_DYNAMIC_CLASS(wxGraphicsPen, wxGraphicsObject)
112IMPLEMENT_DYNAMIC_CLASS(wxGraphicsBrush, wxGraphicsObject)
113IMPLEMENT_DYNAMIC_CLASS(wxGraphicsFont, wxGraphicsObject)
9786ef01 114IMPLEMENT_DYNAMIC_CLASS(wxGraphicsBitmap, wxGraphicsObject)
a4e73390 115
2c820406
SC
116WXDLLIMPEXP_DATA_CORE(wxGraphicsPen) wxNullGraphicsPen;
117WXDLLIMPEXP_DATA_CORE(wxGraphicsBrush) wxNullGraphicsBrush;
118WXDLLIMPEXP_DATA_CORE(wxGraphicsFont) wxNullGraphicsFont;
9786ef01 119WXDLLIMPEXP_DATA_CORE(wxGraphicsBitmap) wxNullGraphicsBitmap;
2c820406 120
a4e73390
SC
121//-----------------------------------------------------------------------------
122// matrix
123//-----------------------------------------------------------------------------
124
125IMPLEMENT_DYNAMIC_CLASS(wxGraphicsMatrix, wxGraphicsObject)
126WXDLLIMPEXP_DATA_CORE(wxGraphicsMatrix) wxNullGraphicsMatrix;
127
128// concatenates the matrix
129void wxGraphicsMatrix::Concat( const wxGraphicsMatrix *t )
130{
131 AllocExclusive();
132 GetMatrixData()->Concat(t->GetMatrixData());
133}
134
135// sets the matrix to the respective values
caa42c43 136void wxGraphicsMatrix::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
a4e73390
SC
137 wxDouble tx, wxDouble ty)
138{
139 AllocExclusive();
140 GetMatrixData()->Set(a,b,c,d,tx,ty);
141}
142
248802d0
RD
143// gets the component valuess of the matrix
144void wxGraphicsMatrix::Get(wxDouble* a, wxDouble* b, wxDouble* c,
145 wxDouble* d, wxDouble* tx, wxDouble* ty) const
146{
147 GetMatrixData()->Get(a, b, c, d, tx, ty);
148}
149
a4e73390
SC
150// makes this the inverse matrix
151void wxGraphicsMatrix::Invert()
152{
153 AllocExclusive();
154 GetMatrixData()->Invert();
155}
156
157// returns true if the elements of the transformation matrix are equal ?
158bool wxGraphicsMatrix::IsEqual( const wxGraphicsMatrix* t) const
159{
160 return GetMatrixData()->IsEqual(t->GetMatrixData());
161}
162
163// return true if this is the identity matrix
164bool wxGraphicsMatrix::IsIdentity() const
165{
166 return GetMatrixData()->IsIdentity();
167}
168
169// add the translation to this matrix
170void wxGraphicsMatrix::Translate( wxDouble dx , wxDouble dy )
171{
172 AllocExclusive();
173 GetMatrixData()->Translate(dx,dy);
174}
175
176// add the scale to this matrix
177void wxGraphicsMatrix::Scale( wxDouble xScale , wxDouble yScale )
178{
179 AllocExclusive();
180 GetMatrixData()->Scale(xScale,yScale);
181}
182
183// add the rotation to this matrix (radians)
184void wxGraphicsMatrix::Rotate( wxDouble angle )
185{
186 AllocExclusive();
187 GetMatrixData()->Rotate(angle);
caa42c43 188}
a4e73390
SC
189
190//
191// apply the transforms
192//
193
194// applies that matrix to the point
195void wxGraphicsMatrix::TransformPoint( wxDouble *x, wxDouble *y ) const
196{
197 GetMatrixData()->TransformPoint(x,y);
198}
199
200// applies the matrix except for translations
201void wxGraphicsMatrix::TransformDistance( wxDouble *dx, wxDouble *dy ) const
202{
203 GetMatrixData()->TransformDistance(dx,dy);
204}
8ddf8e82 205
a4e73390
SC
206// returns the native representation
207void * wxGraphicsMatrix::GetNativeMatrix() const
208{
209 return GetMatrixData()->GetNativeMatrix();
210}
211
212//-----------------------------------------------------------------------------
213// path
214//-----------------------------------------------------------------------------
215
216IMPLEMENT_DYNAMIC_CLASS(wxGraphicsPath, wxGraphicsObject)
1ea0eb4e 217WXDLLIMPEXP_DATA_CORE(wxGraphicsPath) wxNullGraphicsPath;
a4e73390
SC
218
219// convenience functions, for using wxPoint2DDouble etc
220
221wxPoint2DDouble wxGraphicsPath::GetCurrentPoint() const
50581042 222{
a2d6d210 223 wxDouble x,y;
a4e73390 224 GetCurrentPoint(&x,&y);
50581042
SC
225 return wxPoint2DDouble(x,y);
226}
227
228void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble& p)
229{
230 MoveToPoint( p.m_x , p.m_y);
231}
232
233void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble& p)
234{
235 AddLineToPoint( p.m_x , p.m_y);
236}
237
238void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble& c1, const wxPoint2DDouble& c2, const wxPoint2DDouble& e)
239{
240 AddCurveToPoint(c1.m_x, c1.m_y, c2.m_x, c2.m_y, e.m_x, e.m_y);
241}
242
243void wxGraphicsPath::AddArc( const wxPoint2DDouble& c, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise)
244{
245 AddArc(c.m_x, c.m_y, r, startAngle, endAngle, clockwise);
246}
247
a4e73390 248wxRect2DDouble wxGraphicsPath::GetBox() const
facbe363 249{
caa42c43
VZ
250 wxDouble x,y,w,h;
251 GetBox(&x,&y,&w,&h);
252 return wxRect2DDouble( x,y,w,h );
facbe363
SC
253}
254
94a007ec 255bool wxGraphicsPath::Contains( const wxPoint2DDouble& c, wxPolygonFillMode fillStyle ) const
c907796b
RD
256{
257 return Contains( c.m_x, c.m_y, fillStyle);
258}
259
a4e73390
SC
260// true redirections
261
262// begins a new subpath at (x,y)
263void wxGraphicsPath::MoveToPoint( wxDouble x, wxDouble y )
264{
265 AllocExclusive();
266 GetPathData()->MoveToPoint(x,y);
267}
268
caa42c43 269// adds a straight line from the current point to (x,y)
a4e73390
SC
270void wxGraphicsPath::AddLineToPoint( wxDouble x, wxDouble y )
271{
272 AllocExclusive();
273 GetPathData()->AddLineToPoint(x,y);
274}
275
276// adds a cubic Bezier curve from the current point, using two control points and an end point
277void wxGraphicsPath::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
278{
279 AllocExclusive();
280 GetPathData()->AddCurveToPoint(cx1,cy1,cx2,cy2,x,y);
281}
282
283// adds another path
284void wxGraphicsPath::AddPath( const wxGraphicsPath& path )
285{
286 AllocExclusive();
287 GetPathData()->AddPath(path.GetPathData());
288}
289
290// closes the current sub-path
291void wxGraphicsPath::CloseSubpath()
292{
293 AllocExclusive();
294 GetPathData()->CloseSubpath();
295}
296
297// gets the last point of the current path, (0,0) if not yet set
298void wxGraphicsPath::GetCurrentPoint( wxDouble* x, wxDouble* y) const
299{
300 GetPathData()->GetCurrentPoint(x,y);
301}
302
303// adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
304void wxGraphicsPath::AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise )
305{
306 AllocExclusive();
307 GetPathData()->AddArc(x,y,r,startAngle,endAngle,clockwise);
308}
309
50581042 310//
caa42c43 311// These are convenience functions which - if not available natively will be assembled
a4e73390 312// using the primitives from above
50581042
SC
313//
314
a4e73390 315// adds a quadratic Bezier curve from the current point, using a control point and an end point
50581042 316void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y )
a4e73390
SC
317{
318 AllocExclusive();
319 GetPathData()->AddQuadCurveToPoint(cx,cy,x,y);
320}
321
caa42c43 322// appends a rectangle as a new closed subpath
a4e73390
SC
323void wxGraphicsPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
324{
325 AllocExclusive();
326 GetPathData()->AddRectangle(x,y,w,h);
327}
328
329// appends an ellipsis as a new closed subpath fitting the passed rectangle
330void wxGraphicsPath::AddCircle( wxDouble x, wxDouble y, wxDouble r )
331{
332 AllocExclusive();
333 GetPathData()->AddCircle(x,y,r);
334}
335
336// 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)
caa42c43 337void wxGraphicsPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
a4e73390
SC
338{
339 GetPathData()->AddArcToPoint(x1,y1,x2,y2,r);
340}
341
342// appends an ellipse
343void wxGraphicsPath::AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
344{
345 AllocExclusive();
346 GetPathData()->AddEllipse(x,y,w,h);
347}
348
349// appends a rounded rectangle
350void wxGraphicsPath::AddRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius)
351{
352 AllocExclusive();
353 GetPathData()->AddRoundedRectangle(x,y,w,h,radius);
354}
355
356// returns the native path
357void * wxGraphicsPath::GetNativePath() const
358{
359 return GetPathData()->GetNativePath();
360}
361
362// give the native path returned by GetNativePath() back (there might be some deallocations necessary)
363void wxGraphicsPath::UnGetNativePath(void *p)const
364{
365 GetPathData()->UnGetNativePath(p);
366}
367
368// transforms each point of this path by the matrix
369void wxGraphicsPath::Transform( const wxGraphicsMatrix& matrix )
370{
371 AllocExclusive();
372 GetPathData()->Transform(matrix.GetMatrixData());
373}
374
375// gets the bounding box enclosing all points (possibly including control points)
376void wxGraphicsPath::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
377{
378 GetPathData()->GetBox(x,y,w,h);
379}
380
94a007ec 381bool wxGraphicsPath::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const
a4e73390
SC
382{
383 return GetPathData()->Contains(x,y,fillStyle);
384}
385
386//
387// Emulations, these mus be implemented in the ...Data classes in order to allow for proper overrides
388//
389
390void wxGraphicsPathData::AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y )
50581042
SC
391{
392 // calculate using degree elevation to a cubic bezier
a2d6d210
VZ
393 wxPoint2DDouble c1;
394 wxPoint2DDouble c2;
50581042 395
a4e73390
SC
396 wxPoint2DDouble start;
397 GetCurrentPoint(&start.m_x,&start.m_y);
50581042
SC
398 wxPoint2DDouble end(x,y);
399 wxPoint2DDouble c(cx,cy);
5ce3abfb 400 c1 = wxDouble(1/3.0) * start + wxDouble(2/3.0) * c;
a2d6d210 401 c2 = wxDouble(2/3.0) * c + wxDouble(1/3.0) * end;
50581042
SC
402 AddCurveToPoint(c1.m_x,c1.m_y,c2.m_x,c2.m_y,x,y);
403}
404
a4e73390 405void wxGraphicsPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
50581042
SC
406{
407 MoveToPoint(x,y);
408 AddLineToPoint(x,y+h);
409 AddLineToPoint(x+w,y+h);
410 AddLineToPoint(x+w,y);
411 CloseSubpath();
412}
413
a4e73390 414void wxGraphicsPathData::AddCircle( wxDouble x, wxDouble y, wxDouble r )
50581042
SC
415{
416 MoveToPoint(x+r,y);
417 AddArc( x,y,r,0,2*M_PI,false);
418 CloseSubpath();
419}
420
a4e73390 421void wxGraphicsPathData::AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
59720690 422{
d9468a20
RR
423 if (w <= 0. || h <= 0.)
424 return;
caa42c43 425
59720690
SC
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 )
caa42c43 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 477 wxPoint2DDouble t1 = dist*v1 + p1;
d94228a2 478
a2d6d210 479 wxPoint2DDouble nv1 = v1;
d94228a2
SC
480 nv1.SetVectorAngle(v1.GetVectorAngle()-90);
481 wxPoint2DDouble c = t1 + r*nv1;
482
a2d6d210
VZ
483 wxDouble a1 = v1.GetVectorAngle()+90;
484 wxDouble a2 = v2.GetVectorAngle()-90;
d94228a2 485
a4e73390 486 AddLineToPoint(t1.m_x,t1.m_y);
d94228a2 487 AddArc(c.m_x,c.m_y,r,DegToRad(a1),DegToRad(a2),true);
a4e73390 488 AddLineToPoint(p2.m_x,p2.m_y);
d94228a2
SC
489}
490
4ee4c7b9
VZ
491//-----------------------------------------------------------------------------
492// wxGraphicsGradientStops
493//-----------------------------------------------------------------------------
494
495void wxGraphicsGradientStops::Add(const wxGraphicsGradientStop& stop)
496{
497 for ( wxVector<wxGraphicsGradientStop>::iterator it = m_stops.begin();
498 it != m_stops.end();
499 ++it )
500 {
501 if ( stop.GetPosition() < it->GetPosition() )
502 {
503 if ( it != m_stops.begin() )
504 {
505 m_stops.insert(it, stop);
506 }
507 else // we shouldn't be inserting it at the beginning
508 {
509 wxFAIL_MSG( "invalid gradient stop position < 0" );
510 }
511
512 return;
513 }
514 }
515
516 if ( stop.GetPosition() == 1. )
517 {
518 m_stops.insert(m_stops.end() - 1, stop);
519 }
520 else
521 {
3784bf30 522 wxFAIL_MSG( "invalid gradient stop position > 1" );
4ee4c7b9
VZ
523 }
524}
525
8b180bde
RD
526void * wxGraphicsBitmap::GetNativeBitmap() const
527{
528 return GetBitmapData()->GetNativeBitmap();
529}
530
50581042
SC
531//-----------------------------------------------------------------------------
532// wxGraphicsContext Convenience Methods
533//-----------------------------------------------------------------------------
534
8ddf8e82
SC
535IMPLEMENT_ABSTRACT_CLASS(wxGraphicsContext, wxObject)
536
facbe363 537
03647350 538wxGraphicsContext::wxGraphicsContext(wxGraphicsRenderer* renderer) :
bf02a7f9
SC
539 wxGraphicsObject(renderer),
540 m_antialias(wxANTIALIAS_DEFAULT),
0217cfa5
SC
541 m_composition(wxCOMPOSITION_OVER),
542 m_enableOffset(false)
facbe363 543{
facbe363
SC
544}
545
caa42c43 546wxGraphicsContext::~wxGraphicsContext()
facbe363 547{
facbe363
SC
548}
549
caa42c43 550bool wxGraphicsContext::StartDoc(const wxString& WXUNUSED(message))
fcaea2fa
SC
551{
552 return true;
553}
caa42c43 554
fcaea2fa
SC
555void wxGraphicsContext::EndDoc()
556{
557}
558
c29bdd5f
VZ
559void wxGraphicsContext::StartPage(wxDouble WXUNUSED(width),
560 wxDouble WXUNUSED(height))
fcaea2fa
SC
561{
562}
caa42c43 563
fcaea2fa
SC
564void wxGraphicsContext::EndPage()
565{
566}
567
568void wxGraphicsContext::Flush()
569{
570}
571
0217cfa5
SC
572void wxGraphicsContext::EnableOffset(bool enable)
573{
574 m_enableOffset = enable;
575}
576
fcaea2fa
SC
577#if 0
578void wxGraphicsContext::SetAlpha( wxDouble WXUNUSED(alpha) )
579{
580}
caa42c43 581
fcaea2fa
SC
582wxDouble wxGraphicsContext::GetAlpha() const
583{
584 return 1.0;
585}
586#endif
587
fcaea2fa
SC
588void wxGraphicsContext::GetDPI( wxDouble* dpiX, wxDouble* dpiY)
589{
590 *dpiX = 72.0;
591 *dpiY = 72.0;
592}
593
facbe363 594// sets the pen
caa42c43 595void wxGraphicsContext::SetPen( const wxGraphicsPen& pen )
facbe363 596{
facbe363 597 m_pen = pen;
facbe363
SC
598}
599
600void wxGraphicsContext::SetPen( const wxPen& pen )
601{
a1b806b9 602 if ( !pen.IsOk() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT )
2c820406 603 SetPen( wxNullGraphicsPen );
facbe363
SC
604 else
605 SetPen( CreatePen( pen ) );
606}
caa42c43 607
facbe363 608// sets the brush for filling
caa42c43 609void wxGraphicsContext::SetBrush( const wxGraphicsBrush& brush )
facbe363 610{
facbe363 611 m_brush = brush;
facbe363
SC
612}
613
614void wxGraphicsContext::SetBrush( const wxBrush& brush )
615{
a1b806b9 616 if ( !brush.IsOk() || brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT )
2c820406 617 SetBrush( wxNullGraphicsBrush );
facbe363
SC
618 else
619 SetBrush( CreateBrush( brush ) );
620}
621
622// sets the brush for filling
caa42c43 623void wxGraphicsContext::SetFont( const wxGraphicsFont& font )
facbe363 624{
facbe363 625 m_font = font;
facbe363
SC
626}
627
628void wxGraphicsContext::SetFont( const wxFont& font, const wxColour& colour )
629{
a1b806b9 630 if ( font.IsOk() )
2c820406
SC
631 SetFont( CreateFont( font, colour ) );
632 else
633 SetFont( wxNullGraphicsFont );
facbe363
SC
634}
635
94a007ec 636void wxGraphicsContext::DrawPath( const wxGraphicsPath& path, wxPolygonFillMode fillStyle )
50581042
SC
637{
638 FillPath( path , fillStyle );
639 StrokePath( path );
640}
641
0b7dce54
VZ
642void
643wxGraphicsContext::DoDrawRotatedText(const wxString &str,
644 wxDouble x,
645 wxDouble y,
646 wxDouble angle)
50581042 647{
a2d6d210 648 Translate(x,y);
50581042
SC
649 Rotate( -angle );
650 DrawText( str , 0, 0 );
651 Rotate( angle );
a2d6d210 652 Translate(-x,-y);
50581042
SC
653}
654
0b7dce54
VZ
655void
656wxGraphicsContext::DoDrawFilledText(const wxString &str,
657 wxDouble x,
658 wxDouble y,
659 const wxGraphicsBrush& backgroundBrush)
068eb463
SC
660{
661 wxGraphicsBrush formerBrush = m_brush;
caa42c43 662 wxGraphicsPen formerPen = m_pen;
068eb463
SC
663 wxDouble width;
664 wxDouble height;
665 wxDouble descent;
666 wxDouble externalLeading;
667 GetTextExtent( str , &width, &height, &descent, &externalLeading );
668 SetBrush( backgroundBrush );
caa42c43
VZ
669 // to make sure our 'OffsetToPixelBoundaries' doesn't move the fill shape
670 SetPen( wxNullGraphicsPen );
068eb463 671
ab332136 672 DrawRectangle(x , y, width, height);
068eb463
SC
673
674 DrawText( str, x ,y);
675 SetBrush( formerBrush );
caa42c43 676 SetPen( formerPen );
068eb463
SC
677}
678
0b7dce54
VZ
679void
680wxGraphicsContext::DoDrawRotatedFilledText(const wxString &str,
681 wxDouble x, wxDouble y,
682 wxDouble angle,
683 const wxGraphicsBrush& backgroundBrush)
068eb463
SC
684{
685 wxGraphicsBrush formerBrush = m_brush;
caa42c43 686 wxGraphicsPen formerPen = m_pen;
068eb463
SC
687
688 wxDouble width;
689 wxDouble height;
690 wxDouble descent;
691 wxDouble externalLeading;
692 GetTextExtent( str , &width, &height, &descent, &externalLeading );
693 SetBrush( backgroundBrush );
caa42c43
VZ
694 // to make sure our 'OffsetToPixelBoundaries' doesn't move the fill shape
695 SetPen( wxNullGraphicsPen );
068eb463
SC
696
697 wxGraphicsPath path = CreatePath();
698 path.MoveToPoint( x , y );
699 path.AddLineToPoint( (int) (x + sin(angle) * height) , (int) (y + cos(angle) * height) );
700 path.AddLineToPoint(
701 (int) (x + sin(angle) * height + cos(angle) * width) ,
702 (int) (y + cos(angle) * height - sin(angle) * width));
703 path.AddLineToPoint((int) (x + cos(angle) * width) , (int) (y - sin(angle) * width) );
704 FillPath( path );
705 DrawText( str, x ,y, angle);
706 SetBrush( formerBrush );
caa42c43 707 SetPen( formerPen );
068eb463
SC
708}
709
50581042
SC
710void wxGraphicsContext::StrokeLine( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2)
711{
a4e73390
SC
712 wxGraphicsPath path = CreatePath();
713 path.MoveToPoint(x1, y1);
714 path.AddLineToPoint( x2, y2 );
50581042 715 StrokePath( path );
50581042
SC
716}
717
718void wxGraphicsContext::DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
719{
a4e73390
SC
720 wxGraphicsPath path = CreatePath();
721 path.AddRectangle( x , y , w , h );
50581042 722 DrawPath( path );
50581042
SC
723}
724
725void wxGraphicsContext::DrawEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
726{
a4e73390
SC
727 wxGraphicsPath path = CreatePath();
728 path.AddEllipse(x,y,w,h);
59720690 729 DrawPath(path);
50581042
SC
730}
731
732void wxGraphicsContext::DrawRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius)
733{
a4e73390
SC
734 wxGraphicsPath path = CreatePath();
735 path.AddRoundedRectangle(x,y,w,h,radius);
59720690 736 DrawPath(path);
50581042
SC
737}
738
739void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *points)
740{
741 wxASSERT(n > 1);
a4e73390
SC
742 wxGraphicsPath path = CreatePath();
743 path.MoveToPoint(points[0].m_x, points[0].m_y);
a2d6d210 744 for ( size_t i = 1; i < n; ++i)
a4e73390 745 path.AddLineToPoint( points[i].m_x, points[i].m_y );
50581042 746 StrokePath( path );
50581042
SC
747}
748
94a007ec 749void wxGraphicsContext::DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode fillStyle)
50581042
SC
750{
751 wxASSERT(n > 1);
a4e73390
SC
752 wxGraphicsPath path = CreatePath();
753 path.MoveToPoint(points[0].m_x, points[0].m_y);
a2d6d210 754 for ( size_t i = 1; i < n; ++i)
a4e73390 755 path.AddLineToPoint( points[i].m_x, points[i].m_y );
50581042 756 DrawPath( path , fillStyle);
50581042
SC
757}
758
759void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *beginPoints, const wxPoint2DDouble *endPoints)
760{
761 wxASSERT(n > 0);
a4e73390 762 wxGraphicsPath path = CreatePath();
a2d6d210 763 for ( size_t i = 0; i < n; ++i)
50581042 764 {
a4e73390
SC
765 path.MoveToPoint(beginPoints[i].m_x, beginPoints[i].m_y);
766 path.AddLineToPoint( endPoints[i].m_x, endPoints[i].m_y );
50581042
SC
767 }
768 StrokePath( path );
50581042
SC
769}
770
facbe363 771// create a 'native' matrix corresponding to these values
caa42c43 772wxGraphicsMatrix wxGraphicsContext::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
a4e73390 773 wxDouble tx, wxDouble ty) const
50581042 774{
facbe363 775 return GetRenderer()->CreateMatrix(a,b,c,d,tx,ty);
50581042
SC
776}
777
a4e73390 778wxGraphicsPath wxGraphicsContext::CreatePath() const
06b9edb7
SC
779{
780 return GetRenderer()->CreatePath();
781}
782
a4e73390 783wxGraphicsPen wxGraphicsContext::CreatePen(const wxPen& pen) const
06b9edb7
SC
784{
785 return GetRenderer()->CreatePen(pen);
786}
787
a4e73390 788wxGraphicsBrush wxGraphicsContext::CreateBrush(const wxBrush& brush ) const
06b9edb7
SC
789{
790 return GetRenderer()->CreateBrush(brush);
791}
792
4ee4c7b9
VZ
793wxGraphicsBrush
794wxGraphicsContext::CreateLinearGradientBrush(
795 wxDouble x1, wxDouble y1,
796 wxDouble x2, wxDouble y2,
797 const wxColour& c1, const wxColour& c2) const
798{
799 return GetRenderer()->CreateLinearGradientBrush
800 (
801 x1, y1,
802 x2, y2,
803 wxGraphicsGradientStops(c1,c2)
804 );
805}
806
807wxGraphicsBrush
808wxGraphicsContext::CreateLinearGradientBrush(
809 wxDouble x1, wxDouble y1,
810 wxDouble x2, wxDouble y2,
811 const wxGraphicsGradientStops& gradientStops) const
812{
813 return GetRenderer()->CreateLinearGradientBrush(x1,y1,x2,y2, gradientStops);
814}
815
816wxGraphicsBrush
817wxGraphicsContext::CreateRadialGradientBrush(
818 wxDouble xo, wxDouble yo,
819 wxDouble xc, wxDouble yc, wxDouble radius,
820 const wxColour &oColor, const wxColour &cColor) const
06b9edb7 821{
4ee4c7b9
VZ
822 return GetRenderer()->CreateRadialGradientBrush
823 (
824 xo, yo,
825 xc, yc, radius,
826 wxGraphicsGradientStops(oColor, cColor)
827 );
06b9edb7
SC
828}
829
4ee4c7b9
VZ
830wxGraphicsBrush
831wxGraphicsContext::CreateRadialGradientBrush(
832 wxDouble xo, wxDouble yo,
833 wxDouble xc, wxDouble yc, wxDouble radius,
834 const wxGraphicsGradientStops& gradientStops) const
06b9edb7 835{
4ee4c7b9
VZ
836 return GetRenderer()->CreateRadialGradientBrush
837 (
838 xo, yo,
839 xc, yc, radius,
840 gradientStops
841 );
06b9edb7
SC
842}
843
a4e73390 844wxGraphicsFont wxGraphicsContext::CreateFont( const wxFont &font , const wxColour &col ) const
06b9edb7
SC
845{
846 return GetRenderer()->CreateFont(font,col);
847}
848
fa378d36
VZ
849wxGraphicsFont
850wxGraphicsContext::CreateFont(double size,
851 const wxString& facename,
852 int flags,
853 const wxColour& col) const
854{
855 return GetRenderer()->CreateFont(size, facename, flags, col);
856}
857
9786ef01
SC
858wxGraphicsBitmap wxGraphicsContext::CreateBitmap( const wxBitmap& bmp ) const
859{
860 return GetRenderer()->CreateBitmap(bmp);
861}
862
0a470e5e
VZ
863#if wxUSE_IMAGE
864wxGraphicsBitmap wxGraphicsContext::CreateBitmapFromImage(const wxImage& image) const
865{
866 return GetRenderer()->CreateBitmapFromImage(image);
867}
868#endif // wxUSE_IMAGE
869
9786ef01
SC
870wxGraphicsBitmap wxGraphicsContext::CreateSubBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) const
871{
872 return GetRenderer()->CreateSubBitmap(bmp,x,y,w,h);
873}
874
caa42c43 875/* static */ wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC& dc)
06b9edb7
SC
876{
877 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc);
878}
888dde65 879
caa42c43 880/* static */ wxGraphicsContext* wxGraphicsContext::Create( const wxMemoryDC& dc)
0b822969
RR
881{
882 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc);
883}
884
6d0d8455 885#if wxUSE_PRINTING_ARCHITECTURE
caa42c43 886/* static */ wxGraphicsContext* wxGraphicsContext::Create( const wxPrinterDC& dc)
773ccc31
SC
887{
888 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc);
889}
c929ad91 890#endif
8371a353 891
29188693 892#ifdef __WXMSW__
c929ad91 893#if wxUSE_ENH_METAFILE
8371a353
JS
894/* static */ wxGraphicsContext* wxGraphicsContext::Create( const wxEnhMetaFileDC& dc)
895{
896 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc);
897}
6d0d8455 898#endif
29188693 899#endif
06b9edb7
SC
900
901wxGraphicsContext* wxGraphicsContext::CreateFromNative( void * context )
902{
903 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeContext(context);
904}
905
906wxGraphicsContext* wxGraphicsContext::CreateFromNativeWindow( void * window )
907{
908 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeWindow(window);
909}
910
911wxGraphicsContext* wxGraphicsContext::Create( wxWindow* window )
912{
913 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(window);
914}
915
0a470e5e
VZ
916#if wxUSE_IMAGE
917/* static */ wxGraphicsContext* wxGraphicsContext::Create(wxImage& image)
918{
919 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromImage(image);
920}
921#endif // wxUSE_IMAGE
922
ad667945
SC
923wxGraphicsContext* wxGraphicsContext::Create()
924{
925 return wxGraphicsRenderer::GetDefaultRenderer()->CreateMeasuringContext();
926}
927
a4e73390
SC
928//-----------------------------------------------------------------------------
929// wxGraphicsRenderer
930//-----------------------------------------------------------------------------
931
932IMPLEMENT_ABSTRACT_CLASS(wxGraphicsRenderer, wxObject)
933
9fd707a5 934#endif // wxUSE_GRAPHICS_CONTEXT