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