]> git.saurik.com Git - wxWidgets.git/blob - src/common/graphcmn.cpp
missing common create functions
[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 #endif
29
30 #if !defined(wxMAC_USE_CORE_GRAPHICS_BLEND_MODES)
31 #define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 0
32 #endif
33
34 //-----------------------------------------------------------------------------
35 // constants
36 //-----------------------------------------------------------------------------
37
38 static const double RAD2DEG = 180.0 / M_PI;
39
40 //-----------------------------------------------------------------------------
41 // Local functions
42 //-----------------------------------------------------------------------------
43
44 static inline double DegToRad(double deg)
45 {
46 return (deg * M_PI) / 180.0;
47 }
48
49 //-----------------------------------------------------------------------------
50
51 IMPLEMENT_DYNAMIC_CLASS(wxGraphicsObject, wxObject)
52
53 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsRenderer, wxObject)
54
55 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsMatrix, wxGraphicsObject)
56
57 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsPath, wxGraphicsObject)
58
59 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsPen, wxGraphicsObject)
60
61 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsBrush, wxGraphicsObject)
62
63 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsFont, wxGraphicsObject)
64
65 wxPoint2DDouble wxGraphicsPath::GetCurrentPoint()
66 {
67 wxDouble x,y;
68 GetCurrentPoint(x,y);
69 return wxPoint2DDouble(x,y);
70 }
71
72 void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble& p)
73 {
74 MoveToPoint( p.m_x , p.m_y);
75 }
76
77 void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble& p)
78 {
79 AddLineToPoint( p.m_x , p.m_y);
80 }
81
82 void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble& c1, const wxPoint2DDouble& c2, const wxPoint2DDouble& e)
83 {
84 AddCurveToPoint(c1.m_x, c1.m_y, c2.m_x, c2.m_y, e.m_x, e.m_y);
85 }
86
87 void wxGraphicsPath::AddArc( const wxPoint2DDouble& c, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise)
88 {
89 AddArc(c.m_x, c.m_y, r, startAngle, endAngle, clockwise);
90 }
91
92 wxRect2DDouble wxGraphicsPath::GetBox()
93 {
94 wxDouble x,y,w,h;
95 GetBox(&x,&y,&w,&h);
96 return wxRect2DDouble( x,y,w,h );
97 }
98
99 //
100 // Emulations
101 //
102
103 void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y )
104 {
105 // calculate using degree elevation to a cubic bezier
106 wxPoint2DDouble c1;
107 wxPoint2DDouble c2;
108
109 wxPoint2DDouble start = GetCurrentPoint();
110 wxPoint2DDouble end(x,y);
111 wxPoint2DDouble c(cx,cy);
112 c1 = wxDouble(1/3.0) * start + wxDouble(2/3.0) * c;
113 c2 = wxDouble(2/3.0) * c + wxDouble(1/3.0) * end;
114 AddCurveToPoint(c1.m_x,c1.m_y,c2.m_x,c2.m_y,x,y);
115 }
116
117 void wxGraphicsPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
118 {
119 MoveToPoint(x,y);
120 AddLineToPoint(x,y+h);
121 AddLineToPoint(x+w,y+h);
122 AddLineToPoint(x+w,y);
123 CloseSubpath();
124 }
125
126 void wxGraphicsPath::AddCircle( wxDouble x, wxDouble y, wxDouble r )
127 {
128 MoveToPoint(x+r,y);
129 AddArc( x,y,r,0,2*M_PI,false);
130 CloseSubpath();
131 }
132
133 // 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)
134 void wxGraphicsPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
135 {
136 wxPoint2DDouble current = GetCurrentPoint();
137 wxPoint2DDouble p1(x1,y1);
138 wxPoint2DDouble p2(x2,y2);
139
140 wxPoint2DDouble v1 = current - p1;
141 v1.Normalize();
142 wxPoint2DDouble v2 = p2 - p1;
143 v2.Normalize();
144
145 wxDouble alpha = v1.GetVectorAngle() - v2.GetVectorAngle();
146
147 if ( alpha < 0 )
148 alpha = 360 + alpha;
149 // TODO obtuse angles
150
151 alpha = DegToRad(alpha);
152
153 wxDouble dist = r / sin(alpha/2) * cos(alpha/2);
154 // calculate tangential points
155 wxPoint2DDouble t1 = dist*v1 + p1;
156 wxPoint2DDouble t2 = dist*v2 + p1;
157
158 wxPoint2DDouble nv1 = v1;
159 nv1.SetVectorAngle(v1.GetVectorAngle()-90);
160 wxPoint2DDouble c = t1 + r*nv1;
161
162 wxDouble a1 = v1.GetVectorAngle()+90;
163 wxDouble a2 = v2.GetVectorAngle()-90;
164
165 AddLineToPoint(t1);
166 AddArc(c.m_x,c.m_y,r,DegToRad(a1),DegToRad(a2),true);
167 AddLineToPoint(p2);
168 }
169
170 //-----------------------------------------------------------------------------
171 // wxGraphicsContext Convenience Methods
172 //-----------------------------------------------------------------------------
173
174 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsContext, wxObject)
175
176
177 wxGraphicsContext::wxGraphicsContext(wxGraphicsRenderer* renderer) : wxGraphicsObject(renderer)
178 {
179 m_pen = NULL;
180 m_releasePen = false;
181 m_brush = NULL;
182 m_releaseBrush = false;
183 m_font = NULL;
184 m_releaseFont = false;
185 }
186
187 wxGraphicsContext::~wxGraphicsContext()
188 {
189 wxASSERT_MSG( m_pen == NULL , wxT("No pen should be selected during destruction") );
190 wxASSERT_MSG( m_brush == NULL , wxT("No pen should be selected during destruction") );
191 }
192
193 // sets the pen
194 void wxGraphicsContext::SetPen( wxGraphicsPen* pen , bool release )
195 {
196 if ( m_releasePen )
197 delete m_pen;
198 m_pen = pen;
199 m_releasePen = release;
200 }
201
202 void wxGraphicsContext::SetPen( const wxPen& pen )
203 {
204 if ( pen.GetStyle() == wxTRANSPARENT )
205 SetPen( NULL );
206 else
207 SetPen( CreatePen( pen ) );
208 }
209
210 // sets the brush for filling
211 void wxGraphicsContext::SetBrush( wxGraphicsBrush* brush , bool release )
212 {
213 if ( m_releaseBrush )
214 delete m_brush;
215 m_brush = brush;
216 m_releaseBrush = release;
217 }
218
219 void wxGraphicsContext::SetBrush( const wxBrush& brush )
220 {
221 if ( brush.GetStyle() == wxTRANSPARENT )
222 SetBrush( NULL );
223 else
224 SetBrush( CreateBrush( brush ) );
225 }
226
227 // sets the brush for filling
228 void wxGraphicsContext::SetFont( wxGraphicsFont* font , bool release )
229 {
230 if ( m_releaseFont )
231 delete m_font;
232 m_font = font;
233 m_releaseFont = release;
234 }
235
236 void wxGraphicsContext::SetFont( const wxFont& font, const wxColour& colour )
237 {
238 SetFont( CreateFont( font, colour ) );
239 }
240
241 void wxGraphicsContext::DrawPath( const wxGraphicsPath *path, int fillStyle )
242 {
243 FillPath( path , fillStyle );
244 StrokePath( path );
245 }
246
247 void wxGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle )
248 {
249 Translate(x,y);
250 Rotate( -angle );
251 DrawText( str , 0, 0 );
252 Rotate( angle );
253 Translate(-x,-y);
254 }
255
256 void wxGraphicsContext::StrokeLine( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2)
257 {
258 wxGraphicsPath* path = CreatePath();
259 path->MoveToPoint(x1, y1);
260 path->AddLineToPoint( x2, y2 );
261 StrokePath( path );
262 delete path;
263 }
264
265 void wxGraphicsContext::DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
266 {
267 wxGraphicsPath* path = CreatePath();
268 path->AddRectangle( x , y , w , h );
269 DrawPath( path );
270 delete path;
271 }
272
273 void wxGraphicsContext::DrawEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
274 {
275 wxGraphicsPath* path = CreatePath();
276 if ( w == h )
277 {
278 path->AddCircle( x+w/2,y+w/2,w/2);
279 DrawPath(path);
280 }
281 else
282 {
283 PushState();
284 Translate(x+w/2,y+h/2);
285 wxDouble factor = ((wxDouble) w) / h;
286 Scale( factor , 1.0);
287 path->AddCircle(0,0,h/2);
288 DrawPath(path);
289 PopState();
290 }
291 delete path;
292 }
293
294 void wxGraphicsContext::DrawRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius)
295 {
296 wxGraphicsPath* path = CreatePath();
297 if ( radius == 0)
298 {
299 path->AddRectangle( x , y , w , h );
300 DrawPath( path );
301 }
302 else
303 {
304 PushState();
305 Translate( x , y );
306
307 path->MoveToPoint(w, h / 2);
308 path->AddArcToPoint(w, h, w / 2, h, radius);
309 path->AddArcToPoint(0, h, 0, h / 2, radius);
310 path->AddArcToPoint(0, 0, w / 2, 0, radius);
311 path->AddArcToPoint(w, 0, w, h / 2, radius);
312 path->CloseSubpath();
313 DrawPath( path );
314 PopState();
315 }
316 delete path;
317 }
318
319 void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *points)
320 {
321 wxASSERT(n > 1);
322 wxGraphicsPath* path = CreatePath();
323 path->MoveToPoint(points[0].m_x, points[0].m_y);
324 for ( size_t i = 1; i < n; ++i)
325 path->AddLineToPoint( points[i].m_x, points[i].m_y );
326 StrokePath( path );
327 delete path;
328 }
329
330 void wxGraphicsContext::DrawLines( size_t n, const wxPoint2DDouble *points, int fillStyle)
331 {
332 wxASSERT(n > 1);
333 wxGraphicsPath* path = CreatePath();
334 path->MoveToPoint(points[0].m_x, points[0].m_y);
335 for ( size_t i = 1; i < n; ++i)
336 path->AddLineToPoint( points[i].m_x, points[i].m_y );
337 DrawPath( path , fillStyle);
338 delete path;
339 }
340
341 void wxGraphicsContext::StrokeLines( size_t n, const wxPoint2DDouble *beginPoints, const wxPoint2DDouble *endPoints)
342 {
343 wxASSERT(n > 0);
344 wxGraphicsPath* path = CreatePath();
345 for ( size_t i = 0; i < n; ++i)
346 {
347 path->MoveToPoint(beginPoints[i].m_x, beginPoints[i].m_y);
348 path->AddLineToPoint( endPoints[i].m_x, endPoints[i].m_y );
349 }
350 StrokePath( path );
351 delete path;
352 }
353
354 // create a 'native' matrix corresponding to these values
355 wxGraphicsMatrix* wxGraphicsContext::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
356 wxDouble tx, wxDouble ty)
357 {
358 return GetRenderer()->CreateMatrix(a,b,c,d,tx,ty);
359 }
360
361 wxGraphicsPath * wxGraphicsContext::CreatePath()
362 {
363 return GetRenderer()->CreatePath();
364 }
365
366 wxGraphicsPen* wxGraphicsContext::CreatePen(const wxPen& pen)
367 {
368 return GetRenderer()->CreatePen(pen);
369 }
370
371 wxGraphicsBrush* wxGraphicsContext::CreateBrush(const wxBrush& brush )
372 {
373 return GetRenderer()->CreateBrush(brush);
374 }
375
376 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
377 wxGraphicsBrush* wxGraphicsContext::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
378 const wxColour&c1, const wxColour&c2)
379 {
380 return GetRenderer()->CreateLinearGradientBrush(x1,y1,x2,y2,c1,c2);
381 }
382
383 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
384 // with radius r and color cColor
385 wxGraphicsBrush* wxGraphicsContext::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
386 const wxColour &oColor, const wxColour &cColor)
387 {
388 return GetRenderer()->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
389 }
390
391 // sets the font
392 wxGraphicsFont* wxGraphicsContext::CreateFont( const wxFont &font , const wxColour &col )
393 {
394 return GetRenderer()->CreateFont(font,col);
395 }
396
397 wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC& dc)
398 {
399 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc);
400 }
401
402 wxGraphicsContext* wxGraphicsContext::CreateFromNative( void * context )
403 {
404 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeContext(context);
405 }
406
407 wxGraphicsContext* wxGraphicsContext::CreateFromNativeWindow( void * window )
408 {
409 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeWindow(window);
410 }
411
412 wxGraphicsContext* wxGraphicsContext::Create( wxWindow* window )
413 {
414 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(window);
415 }
416
417 #endif // wxUSE_GRAPHICS_CONTEXT