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