]> git.saurik.com Git - wxWidgets.git/blob - src/common/graphcmn.cpp
4193b2b9cce9e6f5c5d9a02972e026867cc7818f
[wxWidgets.git] / src / common / graphcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/graphcmn.cpp
3 // Purpose: graphics context methods common to all platforms
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created:
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
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 WXDLLIMPEXP_DATA_CORE(wxGraphicsPen) wxNullGraphicsPen;
124 WXDLLIMPEXP_DATA_CORE(wxGraphicsBrush) wxNullGraphicsBrush;
125 WXDLLIMPEXP_DATA_CORE(wxGraphicsFont) wxNullGraphicsFont;
126
127 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsRenderer, wxObject)
128 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsMatrix, wxGraphicsObject)
129 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsPath, wxGraphicsObject)
130
131 wxPoint2DDouble wxGraphicsPath::GetCurrentPoint()
132 {
133 wxDouble x,y;
134 GetCurrentPoint(x,y);
135 return wxPoint2DDouble(x,y);
136 }
137
138 void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble& p)
139 {
140 MoveToPoint( p.m_x , p.m_y);
141 }
142
143 void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble& p)
144 {
145 AddLineToPoint( p.m_x , p.m_y);
146 }
147
148 void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble& c1, const wxPoint2DDouble& c2, const wxPoint2DDouble& e)
149 {
150 AddCurveToPoint(c1.m_x, c1.m_y, c2.m_x, c2.m_y, e.m_x, e.m_y);
151 }
152
153 void wxGraphicsPath::AddArc( const wxPoint2DDouble& c, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise)
154 {
155 AddArc(c.m_x, c.m_y, r, startAngle, endAngle, clockwise);
156 }
157
158 wxRect2DDouble wxGraphicsPath::GetBox()
159 {
160 wxDouble x,y,w,h;
161 GetBox(&x,&y,&w,&h);
162 return wxRect2DDouble( x,y,w,h );
163 }
164
165 //
166 // Emulations
167 //
168
169 void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y )
170 {
171 // calculate using degree elevation to a cubic bezier
172 wxPoint2DDouble c1;
173 wxPoint2DDouble c2;
174
175 wxPoint2DDouble start = GetCurrentPoint();
176 wxPoint2DDouble end(x,y);
177 wxPoint2DDouble c(cx,cy);
178 c1 = wxDouble(1/3.0) * start + wxDouble(2/3.0) * c;
179 c2 = wxDouble(2/3.0) * c + wxDouble(1/3.0) * end;
180 AddCurveToPoint(c1.m_x,c1.m_y,c2.m_x,c2.m_y,x,y);
181 }
182
183 void wxGraphicsPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
184 {
185 MoveToPoint(x,y);
186 AddLineToPoint(x,y+h);
187 AddLineToPoint(x+w,y+h);
188 AddLineToPoint(x+w,y);
189 CloseSubpath();
190 }
191
192 void wxGraphicsPath::AddCircle( wxDouble x, wxDouble y, wxDouble r )
193 {
194 MoveToPoint(x+r,y);
195 AddArc( x,y,r,0,2*M_PI,false);
196 CloseSubpath();
197 }
198
199 void wxGraphicsPath::AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
200 {
201 wxDouble rw = w/2;
202 wxDouble rh = h/2;
203 wxDouble xc = x + rw;
204 wxDouble yc = y + rh;
205 wxGraphicsMatrix* m = GetRenderer()->CreateMatrix();
206 m->Translate(xc,yc);
207 m->Scale(rw/rh,1.0);
208 wxGraphicsPath* p = GetRenderer()->CreatePath();
209 p->AddCircle(0,0,rh);
210 p->Transform(m);
211 AddPath(p);
212 delete p;
213 delete m;
214 }
215
216 void wxGraphicsPath::AddRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius)
217 {
218 if ( radius == 0 )
219 AddRectangle(x,y,w,h);
220 else
221 {
222 MoveToPoint( x + w, y + h / 2);
223 AddArcToPoint(x + w, y + h, x + w / 2, y + h, radius);
224 AddArcToPoint(x, y + h, x, y + h / 2, radius);
225 AddArcToPoint(x, y , x + w / 2, y, radius);
226 AddArcToPoint(x + w, y, x + w, y + h / 2, radius);
227 CloseSubpath();
228 }
229 }
230
231 // 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)
232 void wxGraphicsPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
233 {
234 wxPoint2DDouble current = GetCurrentPoint();
235 wxPoint2DDouble p1(x1,y1);
236 wxPoint2DDouble p2(x2,y2);
237
238 wxPoint2DDouble v1 = current - p1;
239 v1.Normalize();
240 wxPoint2DDouble v2 = p2 - p1;
241 v2.Normalize();
242
243 wxDouble alpha = v1.GetVectorAngle() - v2.GetVectorAngle();
244
245 if ( alpha < 0 )
246 alpha = 360 + alpha;
247 // TODO obtuse angles
248
249 alpha = DegToRad(alpha);
250
251 wxDouble dist = r / sin(alpha/2) * cos(alpha/2);
252 // calculate tangential points
253 wxPoint2DDouble t1 = dist*v1 + p1;
254 wxPoint2DDouble t2 = dist*v2 + p1;
255
256 wxPoint2DDouble nv1 = v1;
257 nv1.SetVectorAngle(v1.GetVectorAngle()-90);
258 wxPoint2DDouble c = t1 + r*nv1;
259
260 wxDouble a1 = v1.GetVectorAngle()+90;
261 wxDouble a2 = v2.GetVectorAngle()-90;
262
263 AddLineToPoint(t1);
264 AddArc(c.m_x,c.m_y,r,DegToRad(a1),DegToRad(a2),true);
265 AddLineToPoint(p2);
266 }
267
268 //-----------------------------------------------------------------------------
269 // wxGraphicsContext Convenience Methods
270 //-----------------------------------------------------------------------------
271
272 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsContext, wxObject)
273
274
275 wxGraphicsContext::wxGraphicsContext(wxGraphicsRenderer* renderer) : wxGraphicsObject(renderer)
276 {
277 }
278
279 wxGraphicsContext::~wxGraphicsContext()
280 {
281 }
282
283 // sets the pen
284 void wxGraphicsContext::SetPen( const wxGraphicsPen& pen )
285 {
286 m_pen = pen;
287 }
288
289 void wxGraphicsContext::SetPen( const wxPen& pen )
290 {
291 if ( pen.GetStyle() == wxTRANSPARENT )
292 SetPen( wxNullGraphicsPen );
293 else
294 SetPen( CreatePen( pen ) );
295 }
296
297 // sets the brush for filling
298 void wxGraphicsContext::SetBrush( const wxGraphicsBrush& brush )
299 {
300 m_brush = brush;
301 }
302
303 void wxGraphicsContext::SetBrush( const wxBrush& brush )
304 {
305 if ( brush.GetStyle() == wxTRANSPARENT )
306 SetBrush( wxNullGraphicsBrush );
307 else
308 SetBrush( CreateBrush( brush ) );
309 }
310
311 // sets the brush for filling
312 void wxGraphicsContext::SetFont( const wxGraphicsFont& font )
313 {
314 m_font = font;
315 }
316
317 void wxGraphicsContext::SetFont( const wxFont& font, const wxColour& colour )
318 {
319 if ( font.Ok() )
320 SetFont( CreateFont( font, colour ) );
321 else
322 SetFont( wxNullGraphicsFont );
323 }
324
325 void wxGraphicsContext::DrawPath( const wxGraphicsPath *path, int fillStyle )
326 {
327 FillPath( path , fillStyle );
328 StrokePath( path );
329 }
330
331 void wxGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle )
332 {
333 Translate(x,y);
334 Rotate( -angle );
335 DrawText( str , 0, 0 );
336 Rotate( angle );
337 Translate(-x,-y);
338 }
339
340 void wxGraphicsContext::StrokeLine( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2)
341 {
342 wxGraphicsPath* path = CreatePath();
343 path->MoveToPoint(x1, y1);
344 path->AddLineToPoint( x2, y2 );
345 StrokePath( path );
346 delete path;
347 }
348
349 void wxGraphicsContext::DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
350 {
351 wxGraphicsPath* path = CreatePath();
352 path->AddRectangle( x , y , w , h );
353 DrawPath( path );
354 delete path;
355 }
356
357 void wxGraphicsContext::DrawEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
358 {
359 wxGraphicsPath* path = CreatePath();
360 path->AddEllipse(x,y,w,h);
361 DrawPath(path);
362 delete path;
363 }
364
365 void wxGraphicsContext::DrawRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius)
366 {
367 wxGraphicsPath* path = CreatePath();
368 path->AddRoundedRectangle(x,y,w,h,radius);
369 DrawPath(path);
370 delete path;
371 }
372
373 void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *points)
374 {
375 wxASSERT(n > 1);
376 wxGraphicsPath* path = CreatePath();
377 path->MoveToPoint(points[0].m_x, points[0].m_y);
378 for ( size_t i = 1; i < n; ++i)
379 path->AddLineToPoint( points[i].m_x, points[i].m_y );
380 StrokePath( path );
381 delete path;
382 }
383
384 void wxGraphicsContext::DrawLines( size_t n, const wxPoint2DDouble *points, int fillStyle)
385 {
386 wxASSERT(n > 1);
387 wxGraphicsPath* path = CreatePath();
388 path->MoveToPoint(points[0].m_x, points[0].m_y);
389 for ( size_t i = 1; i < n; ++i)
390 path->AddLineToPoint( points[i].m_x, points[i].m_y );
391 DrawPath( path , fillStyle);
392 delete path;
393 }
394
395 void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *beginPoints, const wxPoint2DDouble *endPoints)
396 {
397 wxASSERT(n > 0);
398 wxGraphicsPath* path = CreatePath();
399 for ( size_t i = 0; i < n; ++i)
400 {
401 path->MoveToPoint(beginPoints[i].m_x, beginPoints[i].m_y);
402 path->AddLineToPoint( endPoints[i].m_x, endPoints[i].m_y );
403 }
404 StrokePath( path );
405 delete path;
406 }
407
408 // create a 'native' matrix corresponding to these values
409 wxGraphicsMatrix* wxGraphicsContext::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
410 wxDouble tx, wxDouble ty)
411 {
412 return GetRenderer()->CreateMatrix(a,b,c,d,tx,ty);
413 }
414
415 wxGraphicsPath * wxGraphicsContext::CreatePath()
416 {
417 return GetRenderer()->CreatePath();
418 }
419
420 wxGraphicsPen wxGraphicsContext::CreatePen(const wxPen& pen)
421 {
422 return GetRenderer()->CreatePen(pen);
423 }
424
425 wxGraphicsBrush wxGraphicsContext::CreateBrush(const wxBrush& brush )
426 {
427 return GetRenderer()->CreateBrush(brush);
428 }
429
430 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
431 wxGraphicsBrush wxGraphicsContext::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
432 const wxColour&c1, const wxColour&c2)
433 {
434 return GetRenderer()->CreateLinearGradientBrush(x1,y1,x2,y2,c1,c2);
435 }
436
437 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
438 // with radius r and color cColor
439 wxGraphicsBrush wxGraphicsContext::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
440 const wxColour &oColor, const wxColour &cColor)
441 {
442 return GetRenderer()->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
443 }
444
445 // sets the font
446 wxGraphicsFont wxGraphicsContext::CreateFont( const wxFont &font , const wxColour &col )
447 {
448 return GetRenderer()->CreateFont(font,col);
449 }
450
451 wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC& dc)
452 {
453 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc);
454 }
455
456 wxGraphicsContext* wxGraphicsContext::CreateFromNative( void * context )
457 {
458 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeContext(context);
459 }
460
461 wxGraphicsContext* wxGraphicsContext::CreateFromNativeWindow( void * window )
462 {
463 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeWindow(window);
464 }
465
466 wxGraphicsContext* wxGraphicsContext::Create( wxWindow* window )
467 {
468 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(window);
469 }
470
471 #endif // wxUSE_GRAPHICS_CONTEXT