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