]>
git.saurik.com Git - wxWidgets.git/blob - src/common/graphcmn.cpp
4193b2b9cce9e6f5c5d9a02972e026867cc7818f
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/graphcmn.cpp
3 // Purpose: graphics context methods common to all platforms
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #if defined(__BORLANDC__)
19 #if wxUSE_GRAPHICS_CONTEXT
21 #include "wx/graphics.h"
25 #include "wx/bitmap.h"
26 #include "wx/dcmemory.h"
27 #include "wx/region.h"
31 #if !defined(wxMAC_USE_CORE_GRAPHICS_BLEND_MODES)
32 #define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 0
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 static const double RAD2DEG
= 180.0 / M_PI
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static inline double DegToRad(double deg
)
47 return (deg
* M_PI
) / 180.0;
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS(wxGraphicsObject
, wxObject
)
58 wxGraphicsObjectRefData::wxGraphicsObjectRefData( wxGraphicsRenderer
* renderer
)
60 m_renderer
= renderer
;
62 wxGraphicsObjectRefData::wxGraphicsObjectRefData( const wxGraphicsObjectRefData
* data
)
64 m_renderer
= data
->m_renderer
;
66 wxGraphicsRenderer
* wxGraphicsObjectRefData::GetRenderer() const
71 wxGraphicsObjectRefData
* wxGraphicsObjectRefData::Clone() const
73 return new wxGraphicsObjectRefData(this);
76 wxGraphicsObject::wxGraphicsObject()
80 wxGraphicsObject::wxGraphicsObject( wxGraphicsRenderer
* renderer
)
82 SetRefData( new wxGraphicsObjectRefData(renderer
));
85 wxGraphicsObject::~wxGraphicsObject()
89 bool wxGraphicsObject::IsNull() const
91 return m_refData
== NULL
;
94 wxGraphicsRenderer
* wxGraphicsObject::GetRenderer() const
96 return ( IsNull() ? NULL
: GetGraphicsData()->GetRenderer() );
99 wxGraphicsObjectRefData
* wxGraphicsObject::GetGraphicsData() const
101 return (wxGraphicsObjectRefData
*) m_refData
;
104 wxObjectRefData
* wxGraphicsObject::CreateRefData() const
106 wxLogDebug(wxT("A Null Object cannot be changed"));
110 wxObjectRefData
* wxGraphicsObject::CloneRefData(const wxObjectRefData
* data
) const
112 const wxGraphicsObjectRefData
* ptr
= (const wxGraphicsObjectRefData
*) data
;
116 //-----------------------------------------------------------------------------
118 //-----------------------------------------------------------------------------
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
;
127 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsRenderer
, wxObject
)
128 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsMatrix
, wxGraphicsObject
)
129 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsPath
, wxGraphicsObject
)
131 wxPoint2DDouble
wxGraphicsPath::GetCurrentPoint()
134 GetCurrentPoint(x
,y
);
135 return wxPoint2DDouble(x
,y
);
138 void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble
& p
)
140 MoveToPoint( p
.m_x
, p
.m_y
);
143 void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble
& p
)
145 AddLineToPoint( p
.m_x
, p
.m_y
);
148 void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
)
150 AddCurveToPoint(c1
.m_x
, c1
.m_y
, c2
.m_x
, c2
.m_y
, e
.m_x
, e
.m_y
);
153 void wxGraphicsPath::AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
155 AddArc(c
.m_x
, c
.m_y
, r
, startAngle
, endAngle
, clockwise
);
158 wxRect2DDouble
wxGraphicsPath::GetBox()
162 return wxRect2DDouble( x
,y
,w
,h
);
169 void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
)
171 // calculate using degree elevation to a cubic bezier
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
);
183 void wxGraphicsPath::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
186 AddLineToPoint(x
,y
+h
);
187 AddLineToPoint(x
+w
,y
+h
);
188 AddLineToPoint(x
+w
,y
);
192 void wxGraphicsPath::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
195 AddArc( x
,y
,r
,0,2*M_PI
,false);
199 void wxGraphicsPath::AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
203 wxDouble xc
= x
+ rw
;
204 wxDouble yc
= y
+ rh
;
205 wxGraphicsMatrix
* m
= GetRenderer()->CreateMatrix();
208 wxGraphicsPath
* p
= GetRenderer()->CreatePath();
209 p
->AddCircle(0,0,rh
);
216 void wxGraphicsPath::AddRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
)
219 AddRectangle(x
,y
,w
,h
);
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
);
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
)
234 wxPoint2DDouble current
= GetCurrentPoint();
235 wxPoint2DDouble
p1(x1
,y1
);
236 wxPoint2DDouble
p2(x2
,y2
);
238 wxPoint2DDouble v1
= current
- p1
;
240 wxPoint2DDouble v2
= p2
- p1
;
243 wxDouble alpha
= v1
.GetVectorAngle() - v2
.GetVectorAngle();
247 // TODO obtuse angles
249 alpha
= DegToRad(alpha
);
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
;
256 wxPoint2DDouble nv1
= v1
;
257 nv1
.SetVectorAngle(v1
.GetVectorAngle()-90);
258 wxPoint2DDouble c
= t1
+ r
*nv1
;
260 wxDouble a1
= v1
.GetVectorAngle()+90;
261 wxDouble a2
= v2
.GetVectorAngle()-90;
264 AddArc(c
.m_x
,c
.m_y
,r
,DegToRad(a1
),DegToRad(a2
),true);
268 //-----------------------------------------------------------------------------
269 // wxGraphicsContext Convenience Methods
270 //-----------------------------------------------------------------------------
272 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsContext
, wxObject
)
275 wxGraphicsContext::wxGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsObject(renderer
)
279 wxGraphicsContext::~wxGraphicsContext()
284 void wxGraphicsContext::SetPen( const wxGraphicsPen
& pen
)
289 void wxGraphicsContext::SetPen( const wxPen
& pen
)
291 if ( pen
.GetStyle() == wxTRANSPARENT
)
292 SetPen( wxNullGraphicsPen
);
294 SetPen( CreatePen( pen
) );
297 // sets the brush for filling
298 void wxGraphicsContext::SetBrush( const wxGraphicsBrush
& brush
)
303 void wxGraphicsContext::SetBrush( const wxBrush
& brush
)
305 if ( brush
.GetStyle() == wxTRANSPARENT
)
306 SetBrush( wxNullGraphicsBrush
);
308 SetBrush( CreateBrush( brush
) );
311 // sets the brush for filling
312 void wxGraphicsContext::SetFont( const wxGraphicsFont
& font
)
317 void wxGraphicsContext::SetFont( const wxFont
& font
, const wxColour
& colour
)
320 SetFont( CreateFont( font
, colour
) );
322 SetFont( wxNullGraphicsFont
);
325 void wxGraphicsContext::DrawPath( const wxGraphicsPath
*path
, int fillStyle
)
327 FillPath( path
, fillStyle
);
331 void wxGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
335 DrawText( str
, 0, 0 );
340 void wxGraphicsContext::StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
)
342 wxGraphicsPath
* path
= CreatePath();
343 path
->MoveToPoint(x1
, y1
);
344 path
->AddLineToPoint( x2
, y2
);
349 void wxGraphicsContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
351 wxGraphicsPath
* path
= CreatePath();
352 path
->AddRectangle( x
, y
, w
, h
);
357 void wxGraphicsContext::DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
359 wxGraphicsPath
* path
= CreatePath();
360 path
->AddEllipse(x
,y
,w
,h
);
365 void wxGraphicsContext::DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
)
367 wxGraphicsPath
* path
= CreatePath();
368 path
->AddRoundedRectangle(x
,y
,w
,h
,radius
);
373 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
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
);
384 void wxGraphicsContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
)
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
);
395 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
)
398 wxGraphicsPath
* path
= CreatePath();
399 for ( size_t i
= 0; i
< n
; ++i
)
401 path
->MoveToPoint(beginPoints
[i
].m_x
, beginPoints
[i
].m_y
);
402 path
->AddLineToPoint( endPoints
[i
].m_x
, endPoints
[i
].m_y
);
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
)
412 return GetRenderer()->CreateMatrix(a
,b
,c
,d
,tx
,ty
);
415 wxGraphicsPath
* wxGraphicsContext::CreatePath()
417 return GetRenderer()->CreatePath();
420 wxGraphicsPen
wxGraphicsContext::CreatePen(const wxPen
& pen
)
422 return GetRenderer()->CreatePen(pen
);
425 wxGraphicsBrush
wxGraphicsContext::CreateBrush(const wxBrush
& brush
)
427 return GetRenderer()->CreateBrush(brush
);
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
)
434 return GetRenderer()->CreateLinearGradientBrush(x1
,y1
,x2
,y2
,c1
,c2
);
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
)
442 return GetRenderer()->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
446 wxGraphicsFont
wxGraphicsContext::CreateFont( const wxFont
&font
, const wxColour
&col
)
448 return GetRenderer()->CreateFont(font
,col
);
451 wxGraphicsContext
* wxGraphicsContext::Create( const wxWindowDC
& dc
)
453 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc
);
456 wxGraphicsContext
* wxGraphicsContext::CreateFromNative( void * context
)
458 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeContext(context
);
461 wxGraphicsContext
* wxGraphicsContext::CreateFromNativeWindow( void * window
)
463 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeWindow(window
);
466 wxGraphicsContext
* wxGraphicsContext::Create( wxWindow
* window
)
468 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(window
);
471 #endif // wxUSE_GRAPHICS_CONTEXT