]>
git.saurik.com Git - wxWidgets.git/blob - src/common/graphcmn.cpp
36a3d228dbe5af4b7fddfd4cf70c95da8d509dad
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"
30 #if !defined(wxMAC_USE_CORE_GRAPHICS_BLEND_MODES)
31 #define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 0
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 static const double RAD2DEG
= 180.0 / M_PI
;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 static inline double DegToRad(double deg
)
46 return (deg
* M_PI
) / 180.0;
49 //-----------------------------------------------------------------------------
51 IMPLEMENT_DYNAMIC_CLASS(wxGraphicsObject
, wxObject
)
53 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsRenderer
, wxObject
)
55 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsMatrix
, wxGraphicsObject
)
57 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsPath
, wxGraphicsObject
)
59 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsPen
, wxGraphicsObject
)
61 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsBrush
, wxGraphicsObject
)
63 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsFont
, wxGraphicsObject
)
65 wxPoint2DDouble
wxGraphicsPath::GetCurrentPoint()
69 return wxPoint2DDouble(x
,y
);
72 void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble
& p
)
74 MoveToPoint( p
.m_x
, p
.m_y
);
77 void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble
& p
)
79 AddLineToPoint( p
.m_x
, p
.m_y
);
82 void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
)
84 AddCurveToPoint(c1
.m_x
, c1
.m_y
, c2
.m_x
, c2
.m_y
, e
.m_x
, e
.m_y
);
87 void wxGraphicsPath::AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
89 AddArc(c
.m_x
, c
.m_y
, r
, startAngle
, endAngle
, clockwise
);
92 wxRect2DDouble
wxGraphicsPath::GetBox()
96 return wxRect2DDouble( x
,y
,w
,h
);
103 void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
)
105 // calculate using degree elevation to a cubic bezier
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
);
117 void wxGraphicsPath::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
120 AddLineToPoint(x
,y
+h
);
121 AddLineToPoint(x
+w
,y
+h
);
122 AddLineToPoint(x
+w
,y
);
126 void wxGraphicsPath::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
129 AddArc( x
,y
,r
,0,2*M_PI
,false);
133 void wxGraphicsPath::AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
137 wxDouble xc
= x
+ rw
;
138 wxDouble yc
= y
+ rh
;
139 wxGraphicsMatrix
* m
= GetRenderer()->CreateMatrix();
142 wxGraphicsPath
* p
= GetRenderer()->CreatePath();
143 p
->AddCircle(0,0,rh
);
150 void wxGraphicsPath::AddRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
)
153 AddRectangle(x
,y
,w
,h
);
156 MoveToPoint( x
+ w
, y
+ h
/ 2);
157 AddArcToPoint(x
+ w
, y
+ h
, x
+ w
/ 2, y
+ h
, radius
);
158 AddArcToPoint(x
, y
+ h
, x
, y
+ h
/ 2, radius
);
159 AddArcToPoint(x
, y
, x
+ w
/ 2, y
, radius
);
160 AddArcToPoint(x
+ w
, y
, x
+ w
, y
+ h
/ 2, radius
);
165 // 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)
166 void wxGraphicsPath::AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
)
168 wxPoint2DDouble current
= GetCurrentPoint();
169 wxPoint2DDouble
p1(x1
,y1
);
170 wxPoint2DDouble
p2(x2
,y2
);
172 wxPoint2DDouble v1
= current
- p1
;
174 wxPoint2DDouble v2
= p2
- p1
;
177 wxDouble alpha
= v1
.GetVectorAngle() - v2
.GetVectorAngle();
181 // TODO obtuse angles
183 alpha
= DegToRad(alpha
);
185 wxDouble dist
= r
/ sin(alpha
/2) * cos(alpha
/2);
186 // calculate tangential points
187 wxPoint2DDouble t1
= dist
*v1
+ p1
;
188 wxPoint2DDouble t2
= dist
*v2
+ p1
;
190 wxPoint2DDouble nv1
= v1
;
191 nv1
.SetVectorAngle(v1
.GetVectorAngle()-90);
192 wxPoint2DDouble c
= t1
+ r
*nv1
;
194 wxDouble a1
= v1
.GetVectorAngle()+90;
195 wxDouble a2
= v2
.GetVectorAngle()-90;
198 AddArc(c
.m_x
,c
.m_y
,r
,DegToRad(a1
),DegToRad(a2
),true);
202 //-----------------------------------------------------------------------------
203 // wxGraphicsContext Convenience Methods
204 //-----------------------------------------------------------------------------
206 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsContext
, wxObject
)
209 wxGraphicsContext::wxGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsObject(renderer
)
212 m_releasePen
= false;
214 m_releaseBrush
= false;
216 m_releaseFont
= false;
219 wxGraphicsContext::~wxGraphicsContext()
221 wxASSERT_MSG( m_pen
== NULL
, wxT("No pen should be selected during destruction") );
222 wxASSERT_MSG( m_brush
== NULL
, wxT("No pen should be selected during destruction") );
226 void wxGraphicsContext::SetPen( wxGraphicsPen
* pen
, bool release
)
231 m_releasePen
= release
;
234 void wxGraphicsContext::SetPen( const wxPen
& pen
)
236 if ( pen
.GetStyle() == wxTRANSPARENT
)
239 SetPen( CreatePen( pen
) );
242 // sets the brush for filling
243 void wxGraphicsContext::SetBrush( wxGraphicsBrush
* brush
, bool release
)
245 if ( m_releaseBrush
)
248 m_releaseBrush
= release
;
251 void wxGraphicsContext::SetBrush( const wxBrush
& brush
)
253 if ( brush
.GetStyle() == wxTRANSPARENT
)
256 SetBrush( CreateBrush( brush
) );
259 // sets the brush for filling
260 void wxGraphicsContext::SetFont( wxGraphicsFont
* font
, bool release
)
265 m_releaseFont
= release
;
268 void wxGraphicsContext::SetFont( const wxFont
& font
, const wxColour
& colour
)
270 SetFont( CreateFont( font
, colour
) );
273 void wxGraphicsContext::DrawPath( const wxGraphicsPath
*path
, int fillStyle
)
275 FillPath( path
, fillStyle
);
279 void wxGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
283 DrawText( str
, 0, 0 );
288 void wxGraphicsContext::StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
)
290 wxGraphicsPath
* path
= CreatePath();
291 path
->MoveToPoint(x1
, y1
);
292 path
->AddLineToPoint( x2
, y2
);
297 void wxGraphicsContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
299 wxGraphicsPath
* path
= CreatePath();
300 path
->AddRectangle( x
, y
, w
, h
);
305 void wxGraphicsContext::DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
307 wxGraphicsPath
* path
= CreatePath();
308 path
->AddEllipse(x
,y
,w
,h
);
313 void wxGraphicsContext::DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
)
315 wxGraphicsPath
* path
= CreatePath();
316 path
->AddRoundedRectangle(x
,y
,w
,h
,radius
);
321 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
324 wxGraphicsPath
* path
= CreatePath();
325 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
);
326 for ( size_t i
= 1; i
< n
; ++i
)
327 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
332 void wxGraphicsContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
)
335 wxGraphicsPath
* path
= CreatePath();
336 path
->MoveToPoint(points
[0].m_x
, points
[0].m_y
);
337 for ( size_t i
= 1; i
< n
; ++i
)
338 path
->AddLineToPoint( points
[i
].m_x
, points
[i
].m_y
);
339 DrawPath( path
, fillStyle
);
343 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
)
346 wxGraphicsPath
* path
= CreatePath();
347 for ( size_t i
= 0; i
< n
; ++i
)
349 path
->MoveToPoint(beginPoints
[i
].m_x
, beginPoints
[i
].m_y
);
350 path
->AddLineToPoint( endPoints
[i
].m_x
, endPoints
[i
].m_y
);
356 // create a 'native' matrix corresponding to these values
357 wxGraphicsMatrix
* wxGraphicsContext::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
358 wxDouble tx
, wxDouble ty
)
360 return GetRenderer()->CreateMatrix(a
,b
,c
,d
,tx
,ty
);
363 wxGraphicsPath
* wxGraphicsContext::CreatePath()
365 return GetRenderer()->CreatePath();
368 wxGraphicsPen
* wxGraphicsContext::CreatePen(const wxPen
& pen
)
370 return GetRenderer()->CreatePen(pen
);
373 wxGraphicsBrush
* wxGraphicsContext::CreateBrush(const wxBrush
& brush
)
375 return GetRenderer()->CreateBrush(brush
);
378 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
379 wxGraphicsBrush
* wxGraphicsContext::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
380 const wxColour
&c1
, const wxColour
&c2
)
382 return GetRenderer()->CreateLinearGradientBrush(x1
,y1
,x2
,y2
,c1
,c2
);
385 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
386 // with radius r and color cColor
387 wxGraphicsBrush
* wxGraphicsContext::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
388 const wxColour
&oColor
, const wxColour
&cColor
)
390 return GetRenderer()->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
394 wxGraphicsFont
* wxGraphicsContext::CreateFont( const wxFont
&font
, const wxColour
&col
)
396 return GetRenderer()->CreateFont(font
,col
);
399 wxGraphicsContext
* wxGraphicsContext::Create( const wxWindowDC
& dc
)
401 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc
);
404 wxGraphicsContext
* wxGraphicsContext::CreateFromNative( void * context
)
406 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeContext(context
);
409 wxGraphicsContext
* wxGraphicsContext::CreateFromNativeWindow( void * window
)
411 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeWindow(window
);
414 wxGraphicsContext
* wxGraphicsContext::Create( wxWindow
* window
)
416 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(window
);
419 #endif // wxUSE_GRAPHICS_CONTEXT