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 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
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
18 #if wxUSE_GRAPHICS_CONTEXT
19
20 #include "wx/graphics.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/icon.h"
24 #include "wx/bitmap.h"
25 #include "wx/dcmemory.h"
26 #include "wx/region.h"
27 #include "wx/log.h"
28 #endif
29
30 #include "wx/private/graphics.h"
31
32 //-----------------------------------------------------------------------------
33 // Local functions
34 //-----------------------------------------------------------------------------
35
DegToRad(double deg)36 static inline double DegToRad(double deg)
37 {
38 return (deg * M_PI) / 180.0;
39 }
40
41 //-----------------------------------------------------------------------------
42
43 //-----------------------------------------------------------------------------
44 // wxGraphicsObject
45 //-----------------------------------------------------------------------------
46
IMPLEMENT_DYNAMIC_CLASS(wxGraphicsObject,wxObject)47 IMPLEMENT_DYNAMIC_CLASS(wxGraphicsObject, wxObject)
48
49 wxGraphicsObjectRefData::wxGraphicsObjectRefData( wxGraphicsRenderer* renderer )
50 {
51 m_renderer = renderer;
52 }
wxGraphicsObjectRefData(const wxGraphicsObjectRefData * data)53 wxGraphicsObjectRefData::wxGraphicsObjectRefData( const wxGraphicsObjectRefData* data )
54 {
55 m_renderer = data->m_renderer;
56 }
GetRenderer() const57 wxGraphicsRenderer* wxGraphicsObjectRefData::GetRenderer() const
58 {
59 return m_renderer ;
60 }
61
Clone() const62 wxGraphicsObjectRefData* wxGraphicsObjectRefData::Clone() const
63 {
64 return new wxGraphicsObjectRefData(this);
65 }
66
wxGraphicsObject()67 wxGraphicsObject::wxGraphicsObject()
68 {
69 }
70
wxGraphicsObject(wxGraphicsRenderer * renderer)71 wxGraphicsObject::wxGraphicsObject( wxGraphicsRenderer* renderer )
72 {
73 SetRefData( new wxGraphicsObjectRefData(renderer));
74 }
75
~wxGraphicsObject()76 wxGraphicsObject::~wxGraphicsObject()
77 {
78 }
79
IsNull() const80 bool wxGraphicsObject::IsNull() const
81 {
82 return m_refData == NULL;
83 }
84
GetRenderer() const85 wxGraphicsRenderer* wxGraphicsObject::GetRenderer() const
86 {
87 return ( IsNull() ? NULL : GetGraphicsData()->GetRenderer() );
88 }
89
GetGraphicsData() const90 wxGraphicsObjectRefData* wxGraphicsObject::GetGraphicsData() const
91 {
92 return (wxGraphicsObjectRefData*) m_refData;
93 }
94
CreateRefData() const95 wxObjectRefData* wxGraphicsObject::CreateRefData() const
96 {
97 wxLogDebug(wxT("A Null Object cannot be changed"));
98 return NULL;
99 }
100
CloneRefData(const wxObjectRefData * data) const101 wxObjectRefData* 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
111 IMPLEMENT_DYNAMIC_CLASS(wxGraphicsPen, wxGraphicsObject)
112 IMPLEMENT_DYNAMIC_CLASS(wxGraphicsBrush, wxGraphicsObject)
113 IMPLEMENT_DYNAMIC_CLASS(wxGraphicsFont, wxGraphicsObject)
114 IMPLEMENT_DYNAMIC_CLASS(wxGraphicsBitmap, wxGraphicsObject)
115
116 WXDLLIMPEXP_DATA_CORE(wxGraphicsPen) wxNullGraphicsPen;
117 WXDLLIMPEXP_DATA_CORE(wxGraphicsBrush) wxNullGraphicsBrush;
118 WXDLLIMPEXP_DATA_CORE(wxGraphicsFont) wxNullGraphicsFont;
119 WXDLLIMPEXP_DATA_CORE(wxGraphicsBitmap) wxNullGraphicsBitmap;
120
121 //-----------------------------------------------------------------------------
122 // matrix
123 //-----------------------------------------------------------------------------
124
125 IMPLEMENT_DYNAMIC_CLASS(wxGraphicsMatrix, wxGraphicsObject)
126 WXDLLIMPEXP_DATA_CORE(wxGraphicsMatrix) wxNullGraphicsMatrix;
127
128 // concatenates the matrix
Concat(const wxGraphicsMatrix * t)129 void wxGraphicsMatrix::Concat( const wxGraphicsMatrix *t )
130 {
131 AllocExclusive();
132 GetMatrixData()->Concat(t->GetMatrixData());
133 }
134
135 // sets the matrix to the respective values
Set(wxDouble a,wxDouble b,wxDouble c,wxDouble d,wxDouble tx,wxDouble ty)136 void wxGraphicsMatrix::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
137 wxDouble tx, wxDouble ty)
138 {
139 AllocExclusive();
140 GetMatrixData()->Set(a,b,c,d,tx,ty);
141 }
142
143 // gets the component valuess of the matrix
Get(wxDouble * a,wxDouble * b,wxDouble * c,wxDouble * d,wxDouble * tx,wxDouble * ty) const144 void 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
150 // makes this the inverse matrix
Invert()151 void wxGraphicsMatrix::Invert()
152 {
153 AllocExclusive();
154 GetMatrixData()->Invert();
155 }
156
157 // returns true if the elements of the transformation matrix are equal ?
IsEqual(const wxGraphicsMatrix * t) const158 bool wxGraphicsMatrix::IsEqual( const wxGraphicsMatrix* t) const
159 {
160 return GetMatrixData()->IsEqual(t->GetMatrixData());
161 }
162
163 // return true if this is the identity matrix
IsIdentity() const164 bool wxGraphicsMatrix::IsIdentity() const
165 {
166 return GetMatrixData()->IsIdentity();
167 }
168
169 // add the translation to this matrix
Translate(wxDouble dx,wxDouble dy)170 void wxGraphicsMatrix::Translate( wxDouble dx , wxDouble dy )
171 {
172 AllocExclusive();
173 GetMatrixData()->Translate(dx,dy);
174 }
175
176 // add the scale to this matrix
Scale(wxDouble xScale,wxDouble yScale)177 void wxGraphicsMatrix::Scale( wxDouble xScale , wxDouble yScale )
178 {
179 AllocExclusive();
180 GetMatrixData()->Scale(xScale,yScale);
181 }
182
183 // add the rotation to this matrix (radians)
Rotate(wxDouble angle)184 void wxGraphicsMatrix::Rotate( wxDouble angle )
185 {
186 AllocExclusive();
187 GetMatrixData()->Rotate(angle);
188 }
189
190 //
191 // apply the transforms
192 //
193
194 // applies that matrix to the point
TransformPoint(wxDouble * x,wxDouble * y) const195 void wxGraphicsMatrix::TransformPoint( wxDouble *x, wxDouble *y ) const
196 {
197 GetMatrixData()->TransformPoint(x,y);
198 }
199
200 // applies the matrix except for translations
TransformDistance(wxDouble * dx,wxDouble * dy) const201 void wxGraphicsMatrix::TransformDistance( wxDouble *dx, wxDouble *dy ) const
202 {
203 GetMatrixData()->TransformDistance(dx,dy);
204 }
205
206 // returns the native representation
GetNativeMatrix() const207 void * wxGraphicsMatrix::GetNativeMatrix() const
208 {
209 return GetMatrixData()->GetNativeMatrix();
210 }
211
212 //-----------------------------------------------------------------------------
213 // path
214 //-----------------------------------------------------------------------------
215
216 IMPLEMENT_DYNAMIC_CLASS(wxGraphicsPath, wxGraphicsObject)
217 WXDLLIMPEXP_DATA_CORE(wxGraphicsPath) wxNullGraphicsPath;
218
219 // convenience functions, for using wxPoint2DDouble etc
220
GetCurrentPoint() const221 wxPoint2DDouble wxGraphicsPath::GetCurrentPoint() const
222 {
223 wxDouble x,y;
224 GetCurrentPoint(&x,&y);
225 return wxPoint2DDouble(x,y);
226 }
227
MoveToPoint(const wxPoint2DDouble & p)228 void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble& p)
229 {
230 MoveToPoint( p.m_x , p.m_y);
231 }
232
AddLineToPoint(const wxPoint2DDouble & p)233 void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble& p)
234 {
235 AddLineToPoint( p.m_x , p.m_y);
236 }
237
AddCurveToPoint(const wxPoint2DDouble & c1,const wxPoint2DDouble & c2,const wxPoint2DDouble & e)238 void 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
AddArc(const wxPoint2DDouble & c,wxDouble r,wxDouble startAngle,wxDouble endAngle,bool clockwise)243 void 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
GetBox() const248 wxRect2DDouble wxGraphicsPath::GetBox() const
249 {
250 wxDouble x,y,w,h;
251 GetBox(&x,&y,&w,&h);
252 return wxRect2DDouble( x,y,w,h );
253 }
254
Contains(const wxPoint2DDouble & c,wxPolygonFillMode fillStyle) const255 bool wxGraphicsPath::Contains( const wxPoint2DDouble& c, wxPolygonFillMode fillStyle ) const
256 {
257 return Contains( c.m_x, c.m_y, fillStyle);
258 }
259
260 // true redirections
261
262 // begins a new subpath at (x,y)
MoveToPoint(wxDouble x,wxDouble y)263 void wxGraphicsPath::MoveToPoint( wxDouble x, wxDouble y )
264 {
265 AllocExclusive();
266 GetPathData()->MoveToPoint(x,y);
267 }
268
269 // adds a straight line from the current point to (x,y)
AddLineToPoint(wxDouble x,wxDouble y)270 void 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
AddCurveToPoint(wxDouble cx1,wxDouble cy1,wxDouble cx2,wxDouble cy2,wxDouble x,wxDouble y)277 void 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
AddPath(const wxGraphicsPath & path)284 void wxGraphicsPath::AddPath( const wxGraphicsPath& path )
285 {
286 AllocExclusive();
287 GetPathData()->AddPath(path.GetPathData());
288 }
289
290 // closes the current sub-path
CloseSubpath()291 void 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
GetCurrentPoint(wxDouble * x,wxDouble * y) const298 void 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
AddArc(wxDouble x,wxDouble y,wxDouble r,wxDouble startAngle,wxDouble endAngle,bool clockwise)304 void 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
310 //
311 // These are convenience functions which - if not available natively will be assembled
312 // using the primitives from above
313 //
314
315 // adds a quadratic Bezier curve from the current point, using a control point and an end point
AddQuadCurveToPoint(wxDouble cx,wxDouble cy,wxDouble x,wxDouble y)316 void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y )
317 {
318 AllocExclusive();
319 GetPathData()->AddQuadCurveToPoint(cx,cy,x,y);
320 }
321
322 // appends a rectangle as a new closed subpath
AddRectangle(wxDouble x,wxDouble y,wxDouble w,wxDouble h)323 void 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
AddCircle(wxDouble x,wxDouble y,wxDouble r)330 void 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)
AddArcToPoint(wxDouble x1,wxDouble y1,wxDouble x2,wxDouble y2,wxDouble r)337 void wxGraphicsPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
338 {
339 GetPathData()->AddArcToPoint(x1,y1,x2,y2,r);
340 }
341
342 // appends an ellipse
AddEllipse(wxDouble x,wxDouble y,wxDouble w,wxDouble h)343 void 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
AddRoundedRectangle(wxDouble x,wxDouble y,wxDouble w,wxDouble h,wxDouble radius)350 void 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
GetNativePath() const357 void * 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)
UnGetNativePath(void * p) const363 void wxGraphicsPath::UnGetNativePath(void *p)const
364 {
365 GetPathData()->UnGetNativePath(p);
366 }
367
368 // transforms each point of this path by the matrix
Transform(const wxGraphicsMatrix & matrix)369 void 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)
GetBox(wxDouble * x,wxDouble * y,wxDouble * w,wxDouble * h) const376 void wxGraphicsPath::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
377 {
378 GetPathData()->GetBox(x,y,w,h);
379 }
380
Contains(wxDouble x,wxDouble y,wxPolygonFillMode fillStyle) const381 bool wxGraphicsPath::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const
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
AddQuadCurveToPoint(wxDouble cx,wxDouble cy,wxDouble x,wxDouble y)390 void wxGraphicsPathData::AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y )
391 {
392 // calculate using degree elevation to a cubic bezier
393 wxPoint2DDouble c1;
394 wxPoint2DDouble c2;
395
396 wxPoint2DDouble start;
397 GetCurrentPoint(&start.m_x,&start.m_y);
398 wxPoint2DDouble end(x,y);
399 wxPoint2DDouble c(cx,cy);
400 c1 = wxDouble(1/3.0) * start + wxDouble(2/3.0) * c;
401 c2 = wxDouble(2/3.0) * c + wxDouble(1/3.0) * end;
402 AddCurveToPoint(c1.m_x,c1.m_y,c2.m_x,c2.m_y,x,y);
403 }
404
AddRectangle(wxDouble x,wxDouble y,wxDouble w,wxDouble h)405 void wxGraphicsPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
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
AddCircle(wxDouble x,wxDouble y,wxDouble r)414 void wxGraphicsPathData::AddCircle( wxDouble x, wxDouble y, wxDouble r )
415 {
416 MoveToPoint(x+r,y);
417 AddArc( x,y,r,0,2*M_PI,false);
418 CloseSubpath();
419 }
420
AddEllipse(wxDouble x,wxDouble y,wxDouble w,wxDouble h)421 void wxGraphicsPathData::AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
422 {
423 if (w <= 0. || h <= 0.)
424 return;
425
426 wxDouble rw = w/2;
427 wxDouble rh = h/2;
428 wxDouble xc = x + rw;
429 wxDouble yc = y + rh;
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());
437 }
438
AddRoundedRectangle(wxDouble x,wxDouble y,wxDouble w,wxDouble h,wxDouble radius)439 void wxGraphicsPathData::AddRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius)
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
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)
AddArcToPoint(wxDouble x1,wxDouble y1,wxDouble x2,wxDouble y2,wxDouble r)455 void wxGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
456 {
457 wxPoint2DDouble current;
458 GetCurrentPoint(¤t.m_x,¤t.m_y);
459 wxPoint2DDouble p1(x1,y1);
460 wxPoint2DDouble p2(x2,y2);
461
462 wxPoint2DDouble v1 = current - p1;
463 v1.Normalize();
464 wxPoint2DDouble v2 = p2 - p1;
465 v2.Normalize();
466
467 wxDouble alpha = v1.GetVectorAngle() - v2.GetVectorAngle();
468
469 if ( alpha < 0 )
470 alpha = 360 + alpha;
471 // TODO obtuse angles
472
473 alpha = DegToRad(alpha);
474
475 wxDouble dist = r / sin(alpha/2) * cos(alpha/2);
476 // calculate tangential points
477 wxPoint2DDouble t1 = dist*v1 + p1;
478
479 wxPoint2DDouble nv1 = v1;
480 nv1.SetVectorAngle(v1.GetVectorAngle()-90);
481 wxPoint2DDouble c = t1 + r*nv1;
482
483 wxDouble a1 = v1.GetVectorAngle()+90;
484 wxDouble a2 = v2.GetVectorAngle()-90;
485
486 AddLineToPoint(t1.m_x,t1.m_y);
487 AddArc(c.m_x,c.m_y,r,DegToRad(a1),DegToRad(a2),true);
488 AddLineToPoint(p2.m_x,p2.m_y);
489 }
490
491 //-----------------------------------------------------------------------------
492 // wxGraphicsGradientStops
493 //-----------------------------------------------------------------------------
494
Add(const wxGraphicsGradientStop & stop)495 void 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 {
522 wxFAIL_MSG( "invalid gradient stop position > 1" );
523 }
524 }
525
GetNativeBitmap() const526 void * wxGraphicsBitmap::GetNativeBitmap() const
527 {
528 return GetBitmapData()->GetNativeBitmap();
529 }
530
531 //-----------------------------------------------------------------------------
532 // wxGraphicsContext Convenience Methods
533 //-----------------------------------------------------------------------------
534
IMPLEMENT_ABSTRACT_CLASS(wxGraphicsContext,wxObject)535 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsContext, wxObject)
536
537
538 wxGraphicsContext::wxGraphicsContext(wxGraphicsRenderer* renderer) :
539 wxGraphicsObject(renderer),
540 m_antialias(wxANTIALIAS_DEFAULT),
541 m_composition(wxCOMPOSITION_OVER),
542 m_interpolation(wxINTERPOLATION_DEFAULT),
543 m_enableOffset(false)
544 {
545 }
546
~wxGraphicsContext()547 wxGraphicsContext::~wxGraphicsContext()
548 {
549 }
550
StartDoc(const wxString & WXUNUSED (message))551 bool wxGraphicsContext::StartDoc(const wxString& WXUNUSED(message))
552 {
553 return true;
554 }
555
EndDoc()556 void wxGraphicsContext::EndDoc()
557 {
558 }
559
StartPage(wxDouble WXUNUSED (width),wxDouble WXUNUSED (height))560 void wxGraphicsContext::StartPage(wxDouble WXUNUSED(width),
561 wxDouble WXUNUSED(height))
562 {
563 }
564
EndPage()565 void wxGraphicsContext::EndPage()
566 {
567 }
568
Flush()569 void wxGraphicsContext::Flush()
570 {
571 }
572
EnableOffset(bool enable)573 void wxGraphicsContext::EnableOffset(bool enable)
574 {
575 m_enableOffset = enable;
576 }
577
578 #if 0
579 void wxGraphicsContext::SetAlpha( wxDouble WXUNUSED(alpha) )
580 {
581 }
582
583 wxDouble wxGraphicsContext::GetAlpha() const
584 {
585 return 1.0;
586 }
587 #endif
588
GetDPI(wxDouble * dpiX,wxDouble * dpiY)589 void wxGraphicsContext::GetDPI( wxDouble* dpiX, wxDouble* dpiY)
590 {
591 *dpiX = 72.0;
592 *dpiY = 72.0;
593 }
594
595 // sets the pen
SetPen(const wxGraphicsPen & pen)596 void wxGraphicsContext::SetPen( const wxGraphicsPen& pen )
597 {
598 m_pen = pen;
599 }
600
SetPen(const wxPen & pen)601 void wxGraphicsContext::SetPen( const wxPen& pen )
602 {
603 if ( !pen.IsOk() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT )
604 SetPen( wxNullGraphicsPen );
605 else
606 SetPen( CreatePen( pen ) );
607 }
608
609 // sets the brush for filling
SetBrush(const wxGraphicsBrush & brush)610 void wxGraphicsContext::SetBrush( const wxGraphicsBrush& brush )
611 {
612 m_brush = brush;
613 }
614
SetBrush(const wxBrush & brush)615 void wxGraphicsContext::SetBrush( const wxBrush& brush )
616 {
617 if ( !brush.IsOk() || brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT )
618 SetBrush( wxNullGraphicsBrush );
619 else
620 SetBrush( CreateBrush( brush ) );
621 }
622
623 // sets the brush for filling
SetFont(const wxGraphicsFont & font)624 void wxGraphicsContext::SetFont( const wxGraphicsFont& font )
625 {
626 m_font = font;
627 }
628
SetFont(const wxFont & font,const wxColour & colour)629 void wxGraphicsContext::SetFont( const wxFont& font, const wxColour& colour )
630 {
631 if ( font.IsOk() )
632 SetFont( CreateFont( font, colour ) );
633 else
634 SetFont( wxNullGraphicsFont );
635 }
636
DrawPath(const wxGraphicsPath & path,wxPolygonFillMode fillStyle)637 void wxGraphicsContext::DrawPath( const wxGraphicsPath& path, wxPolygonFillMode fillStyle )
638 {
639 FillPath( path , fillStyle );
640 StrokePath( path );
641 }
642
643 void
DoDrawRotatedText(const wxString & str,wxDouble x,wxDouble y,wxDouble angle)644 wxGraphicsContext::DoDrawRotatedText(const wxString &str,
645 wxDouble x,
646 wxDouble y,
647 wxDouble angle)
648 {
649 Translate(x,y);
650 Rotate( -angle );
651 DrawText( str , 0, 0 );
652 Rotate( angle );
653 Translate(-x,-y);
654 }
655
656 void
DoDrawFilledText(const wxString & str,wxDouble x,wxDouble y,const wxGraphicsBrush & backgroundBrush)657 wxGraphicsContext::DoDrawFilledText(const wxString &str,
658 wxDouble x,
659 wxDouble y,
660 const wxGraphicsBrush& backgroundBrush)
661 {
662 wxGraphicsBrush formerBrush = m_brush;
663 wxGraphicsPen formerPen = m_pen;
664 wxDouble width;
665 wxDouble height;
666 wxDouble descent;
667 wxDouble externalLeading;
668 GetTextExtent( str , &width, &height, &descent, &externalLeading );
669 SetBrush( backgroundBrush );
670 // to make sure our 'OffsetToPixelBoundaries' doesn't move the fill shape
671 SetPen( wxNullGraphicsPen );
672
673 DrawRectangle(x , y, width, height);
674
675 DrawText( str, x ,y);
676 SetBrush( formerBrush );
677 SetPen( formerPen );
678 }
679
680 void
DoDrawRotatedFilledText(const wxString & str,wxDouble x,wxDouble y,wxDouble angle,const wxGraphicsBrush & backgroundBrush)681 wxGraphicsContext::DoDrawRotatedFilledText(const wxString &str,
682 wxDouble x, wxDouble y,
683 wxDouble angle,
684 const wxGraphicsBrush& backgroundBrush)
685 {
686 wxGraphicsBrush formerBrush = m_brush;
687 wxGraphicsPen formerPen = m_pen;
688
689 wxDouble width;
690 wxDouble height;
691 wxDouble descent;
692 wxDouble externalLeading;
693 GetTextExtent( str , &width, &height, &descent, &externalLeading );
694 SetBrush( backgroundBrush );
695 // to make sure our 'OffsetToPixelBoundaries' doesn't move the fill shape
696 SetPen( wxNullGraphicsPen );
697
698 wxGraphicsPath path = CreatePath();
699 path.MoveToPoint( x , y );
700 path.AddLineToPoint( (int) (x + sin(angle) * height) , (int) (y + cos(angle) * height) );
701 path.AddLineToPoint(
702 (int) (x + sin(angle) * height + cos(angle) * width) ,
703 (int) (y + cos(angle) * height - sin(angle) * width));
704 path.AddLineToPoint((int) (x + cos(angle) * width) , (int) (y - sin(angle) * width) );
705 FillPath( path );
706 DrawText( str, x ,y, angle);
707 SetBrush( formerBrush );
708 SetPen( formerPen );
709 }
710
StrokeLine(wxDouble x1,wxDouble y1,wxDouble x2,wxDouble y2)711 void wxGraphicsContext::StrokeLine( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2)
712 {
713 wxGraphicsPath path = CreatePath();
714 path.MoveToPoint(x1, y1);
715 path.AddLineToPoint( x2, y2 );
716 StrokePath( path );
717 }
718
DrawRectangle(wxDouble x,wxDouble y,wxDouble w,wxDouble h)719 void wxGraphicsContext::DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
720 {
721 wxGraphicsPath path = CreatePath();
722 path.AddRectangle( x , y , w , h );
723 DrawPath( path );
724 }
725
DrawEllipse(wxDouble x,wxDouble y,wxDouble w,wxDouble h)726 void wxGraphicsContext::DrawEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
727 {
728 wxGraphicsPath path = CreatePath();
729 path.AddEllipse(x,y,w,h);
730 DrawPath(path);
731 }
732
DrawRoundedRectangle(wxDouble x,wxDouble y,wxDouble w,wxDouble h,wxDouble radius)733 void wxGraphicsContext::DrawRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius)
734 {
735 wxGraphicsPath path = CreatePath();
736 path.AddRoundedRectangle(x,y,w,h,radius);
737 DrawPath(path);
738 }
739
StrokeLines(size_t n,const wxPoint2DDouble * points)740 void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *points)
741 {
742 wxASSERT(n > 1);
743 wxGraphicsPath path = CreatePath();
744 path.MoveToPoint(points[0].m_x, points[0].m_y);
745 for ( size_t i = 1; i < n; ++i)
746 path.AddLineToPoint( points[i].m_x, points[i].m_y );
747 StrokePath( path );
748 }
749
DrawLines(size_t n,const wxPoint2DDouble * points,wxPolygonFillMode fillStyle)750 void wxGraphicsContext::DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode fillStyle)
751 {
752 wxASSERT(n > 1);
753 wxGraphicsPath path = CreatePath();
754 path.MoveToPoint(points[0].m_x, points[0].m_y);
755 for ( size_t i = 1; i < n; ++i)
756 path.AddLineToPoint( points[i].m_x, points[i].m_y );
757 DrawPath( path , fillStyle);
758 }
759
StrokeLines(size_t n,const wxPoint2DDouble * beginPoints,const wxPoint2DDouble * endPoints)760 void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *beginPoints, const wxPoint2DDouble *endPoints)
761 {
762 wxASSERT(n > 0);
763 wxGraphicsPath path = CreatePath();
764 for ( size_t i = 0; i < n; ++i)
765 {
766 path.MoveToPoint(beginPoints[i].m_x, beginPoints[i].m_y);
767 path.AddLineToPoint( endPoints[i].m_x, endPoints[i].m_y );
768 }
769 StrokePath( path );
770 }
771
772 // create a 'native' matrix corresponding to these values
CreateMatrix(wxDouble a,wxDouble b,wxDouble c,wxDouble d,wxDouble tx,wxDouble ty) const773 wxGraphicsMatrix wxGraphicsContext::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
774 wxDouble tx, wxDouble ty) const
775 {
776 return GetRenderer()->CreateMatrix(a,b,c,d,tx,ty);
777 }
778
CreatePath() const779 wxGraphicsPath wxGraphicsContext::CreatePath() const
780 {
781 return GetRenderer()->CreatePath();
782 }
783
CreatePen(const wxPen & pen) const784 wxGraphicsPen wxGraphicsContext::CreatePen(const wxPen& pen) const
785 {
786 return GetRenderer()->CreatePen(pen);
787 }
788
CreateBrush(const wxBrush & brush) const789 wxGraphicsBrush wxGraphicsContext::CreateBrush(const wxBrush& brush ) const
790 {
791 return GetRenderer()->CreateBrush(brush);
792 }
793
794 wxGraphicsBrush
CreateLinearGradientBrush(wxDouble x1,wxDouble y1,wxDouble x2,wxDouble y2,const wxColour & c1,const wxColour & c2) const795 wxGraphicsContext::CreateLinearGradientBrush(
796 wxDouble x1, wxDouble y1,
797 wxDouble x2, wxDouble y2,
798 const wxColour& c1, const wxColour& c2) const
799 {
800 return GetRenderer()->CreateLinearGradientBrush
801 (
802 x1, y1,
803 x2, y2,
804 wxGraphicsGradientStops(c1,c2)
805 );
806 }
807
808 wxGraphicsBrush
CreateLinearGradientBrush(wxDouble x1,wxDouble y1,wxDouble x2,wxDouble y2,const wxGraphicsGradientStops & gradientStops) const809 wxGraphicsContext::CreateLinearGradientBrush(
810 wxDouble x1, wxDouble y1,
811 wxDouble x2, wxDouble y2,
812 const wxGraphicsGradientStops& gradientStops) const
813 {
814 return GetRenderer()->CreateLinearGradientBrush(x1,y1,x2,y2, gradientStops);
815 }
816
817 wxGraphicsBrush
CreateRadialGradientBrush(wxDouble xo,wxDouble yo,wxDouble xc,wxDouble yc,wxDouble radius,const wxColour & oColor,const wxColour & cColor) const818 wxGraphicsContext::CreateRadialGradientBrush(
819 wxDouble xo, wxDouble yo,
820 wxDouble xc, wxDouble yc, wxDouble radius,
821 const wxColour &oColor, const wxColour &cColor) const
822 {
823 return GetRenderer()->CreateRadialGradientBrush
824 (
825 xo, yo,
826 xc, yc, radius,
827 wxGraphicsGradientStops(oColor, cColor)
828 );
829 }
830
831 wxGraphicsBrush
CreateRadialGradientBrush(wxDouble xo,wxDouble yo,wxDouble xc,wxDouble yc,wxDouble radius,const wxGraphicsGradientStops & gradientStops) const832 wxGraphicsContext::CreateRadialGradientBrush(
833 wxDouble xo, wxDouble yo,
834 wxDouble xc, wxDouble yc, wxDouble radius,
835 const wxGraphicsGradientStops& gradientStops) const
836 {
837 return GetRenderer()->CreateRadialGradientBrush
838 (
839 xo, yo,
840 xc, yc, radius,
841 gradientStops
842 );
843 }
844
CreateFont(const wxFont & font,const wxColour & col) const845 wxGraphicsFont wxGraphicsContext::CreateFont( const wxFont &font , const wxColour &col ) const
846 {
847 return GetRenderer()->CreateFont(font,col);
848 }
849
850 wxGraphicsFont
CreateFont(double size,const wxString & facename,int flags,const wxColour & col) const851 wxGraphicsContext::CreateFont(double size,
852 const wxString& facename,
853 int flags,
854 const wxColour& col) const
855 {
856 return GetRenderer()->CreateFont(size, facename, flags, col);
857 }
858
CreateBitmap(const wxBitmap & bmp) const859 wxGraphicsBitmap wxGraphicsContext::CreateBitmap( const wxBitmap& bmp ) const
860 {
861 return GetRenderer()->CreateBitmap(bmp);
862 }
863
864 #if wxUSE_IMAGE
CreateBitmapFromImage(const wxImage & image) const865 wxGraphicsBitmap wxGraphicsContext::CreateBitmapFromImage(const wxImage& image) const
866 {
867 return GetRenderer()->CreateBitmapFromImage(image);
868 }
869 #endif // wxUSE_IMAGE
870
CreateSubBitmap(const wxGraphicsBitmap & bmp,wxDouble x,wxDouble y,wxDouble w,wxDouble h) const871 wxGraphicsBitmap wxGraphicsContext::CreateSubBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) const
872 {
873 return GetRenderer()->CreateSubBitmap(bmp,x,y,w,h);
874 }
875
Create(const wxWindowDC & dc)876 /* static */ wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC& dc)
877 {
878 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc);
879 }
880
Create(const wxMemoryDC & dc)881 /* static */ wxGraphicsContext* wxGraphicsContext::Create( const wxMemoryDC& dc)
882 {
883 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc);
884 }
885
886 #if wxUSE_PRINTING_ARCHITECTURE
Create(const wxPrinterDC & dc)887 /* static */ wxGraphicsContext* wxGraphicsContext::Create( const wxPrinterDC& dc)
888 {
889 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc);
890 }
891 #endif
892
893 #ifdef __WXMSW__
894 #if wxUSE_ENH_METAFILE
Create(const wxEnhMetaFileDC & dc)895 /* static */ wxGraphicsContext* wxGraphicsContext::Create( const wxEnhMetaFileDC& dc)
896 {
897 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc);
898 }
899 #endif
900 #endif
901
CreateFromNative(void * context)902 wxGraphicsContext* wxGraphicsContext::CreateFromNative( void * context )
903 {
904 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeContext(context);
905 }
906
CreateFromNativeWindow(void * window)907 wxGraphicsContext* wxGraphicsContext::CreateFromNativeWindow( void * window )
908 {
909 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeWindow(window);
910 }
911
Create(wxWindow * window)912 wxGraphicsContext* wxGraphicsContext::Create( wxWindow* window )
913 {
914 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(window);
915 }
916
917 #if wxUSE_IMAGE
Create(wxImage & image)918 /* static */ wxGraphicsContext* wxGraphicsContext::Create(wxImage& image)
919 {
920 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromImage(image);
921 }
922 #endif // wxUSE_IMAGE
923
Create()924 wxGraphicsContext* wxGraphicsContext::Create()
925 {
926 return wxGraphicsRenderer::GetDefaultRenderer()->CreateMeasuringContext();
927 }
928
929 //-----------------------------------------------------------------------------
930 // wxGraphicsRenderer
931 //-----------------------------------------------------------------------------
932
933 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsRenderer, wxObject)
934
935 #endif // wxUSE_GRAPHICS_CONTEXT
936