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