]>
git.saurik.com Git - wxWidgets.git/blob - src/common/graphcmn.cpp
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 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 IMPLEMENT_DYNAMIC_CLASS(wxGraphicsObject
, wxObject
)
57 wxGraphicsObjectRefData::wxGraphicsObjectRefData( wxGraphicsRenderer
* renderer
)
59 m_renderer
= renderer
;
61 wxGraphicsObjectRefData::wxGraphicsObjectRefData( const wxGraphicsObjectRefData
* data
)
63 m_renderer
= data
->m_renderer
;
65 wxGraphicsRenderer
* wxGraphicsObjectRefData::GetRenderer() const
70 wxGraphicsObjectRefData
* wxGraphicsObjectRefData::Clone() const
72 return new wxGraphicsObjectRefData(this);
75 wxGraphicsObject::wxGraphicsObject()
79 wxGraphicsObject::wxGraphicsObject( wxGraphicsRenderer
* renderer
)
81 SetRefData( new wxGraphicsObjectRefData(renderer
));
84 wxGraphicsObject::~wxGraphicsObject()
88 bool wxGraphicsObject::IsNull() const
90 return m_refData
== NULL
;
93 wxGraphicsRenderer
* wxGraphicsObject::GetRenderer() const
95 return ( IsNull() ? NULL
: GetGraphicsData()->GetRenderer() );
98 wxGraphicsObjectRefData
* wxGraphicsObject::GetGraphicsData() const
100 return (wxGraphicsObjectRefData
*) m_refData
;
103 wxObjectRefData
* wxGraphicsObject::CreateRefData() const
105 wxLogDebug(wxT("A Null Object cannot be changed"));
109 wxObjectRefData
* wxGraphicsObject::CloneRefData(const wxObjectRefData
* data
) const
111 const wxGraphicsObjectRefData
* ptr
= (const wxGraphicsObjectRefData
*) data
;
115 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
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
;
126 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsRenderer
, wxObject
)
127 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsMatrix
, wxGraphicsObject
)
128 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsPath
, wxGraphicsObject
)
130 wxPoint2DDouble
wxGraphicsPath::GetCurrentPoint()
133 GetCurrentPoint(x
,y
);
134 return wxPoint2DDouble(x
,y
);
137 void wxGraphicsPath::MoveToPoint( const wxPoint2DDouble
& p
)
139 MoveToPoint( p
.m_x
, p
.m_y
);
142 void wxGraphicsPath::AddLineToPoint( const wxPoint2DDouble
& p
)
144 AddLineToPoint( p
.m_x
, p
.m_y
);
147 void wxGraphicsPath::AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
)
149 AddCurveToPoint(c1
.m_x
, c1
.m_y
, c2
.m_x
, c2
.m_y
, e
.m_x
, e
.m_y
);
152 void wxGraphicsPath::AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
)
154 AddArc(c
.m_x
, c
.m_y
, r
, startAngle
, endAngle
, clockwise
);
157 wxRect2DDouble
wxGraphicsPath::GetBox()
161 return wxRect2DDouble( x
,y
,w
,h
);
168 void wxGraphicsPath::AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
)
170 // calculate using degree elevation to a cubic bezier
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
);
182 void wxGraphicsPath::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
185 AddLineToPoint(x
,y
+h
);
186 AddLineToPoint(x
+w
,y
+h
);
187 AddLineToPoint(x
+w
,y
);
191 void wxGraphicsPath::AddCircle( wxDouble x
, wxDouble y
, wxDouble r
)
194 AddArc( x
,y
,r
,0,2*M_PI
,false);
198 void wxGraphicsPath::AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
202 wxDouble xc
= x
+ rw
;
203 wxDouble yc
= y
+ rh
;
204 wxGraphicsMatrix
* m
= GetRenderer()->CreateMatrix();
207 wxGraphicsPath
* p
= GetRenderer()->CreatePath();
208 p
->AddCircle(0,0,rh
);
215 void wxGraphicsPath::AddRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
)
218 AddRectangle(x
,y
,w
,h
);
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
);
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
)
233 wxPoint2DDouble current
= GetCurrentPoint();
234 wxPoint2DDouble
p1(x1
,y1
);
235 wxPoint2DDouble
p2(x2
,y2
);
237 wxPoint2DDouble v1
= current
- p1
;
239 wxPoint2DDouble v2
= p2
- p1
;
242 wxDouble alpha
= v1
.GetVectorAngle() - v2
.GetVectorAngle();
246 // TODO obtuse angles
248 alpha
= DegToRad(alpha
);
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
;
255 wxPoint2DDouble nv1
= v1
;
256 nv1
.SetVectorAngle(v1
.GetVectorAngle()-90);
257 wxPoint2DDouble c
= t1
+ r
*nv1
;
259 wxDouble a1
= v1
.GetVectorAngle()+90;
260 wxDouble a2
= v2
.GetVectorAngle()-90;
263 AddArc(c
.m_x
,c
.m_y
,r
,DegToRad(a1
),DegToRad(a2
),true);
267 //-----------------------------------------------------------------------------
268 // wxGraphicsContext Convenience Methods
269 //-----------------------------------------------------------------------------
271 IMPLEMENT_ABSTRACT_CLASS(wxGraphicsContext
, wxObject
)
274 wxGraphicsContext::wxGraphicsContext(wxGraphicsRenderer
* renderer
) : wxGraphicsObject(renderer
)
278 wxGraphicsContext::~wxGraphicsContext()
283 void wxGraphicsContext::SetPen( const wxGraphicsPen
& pen
)
288 void wxGraphicsContext::SetPen( const wxPen
& pen
)
290 if ( pen
.GetStyle() == wxTRANSPARENT
)
291 SetPen( wxNullGraphicsPen
);
293 SetPen( CreatePen( pen
) );
296 // sets the brush for filling
297 void wxGraphicsContext::SetBrush( const wxGraphicsBrush
& brush
)
302 void wxGraphicsContext::SetBrush( const wxBrush
& brush
)
304 if ( brush
.GetStyle() == wxTRANSPARENT
)
305 SetBrush( wxNullGraphicsBrush
);
307 SetBrush( CreateBrush( brush
) );
310 // sets the brush for filling
311 void wxGraphicsContext::SetFont( const wxGraphicsFont
& font
)
316 void wxGraphicsContext::SetFont( const wxFont
& font
, const wxColour
& colour
)
319 SetFont( CreateFont( font
, colour
) );
321 SetFont( wxNullGraphicsFont
);
324 void wxGraphicsContext::DrawPath( const wxGraphicsPath
*path
, int fillStyle
)
326 FillPath( path
, fillStyle
);
330 void wxGraphicsContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
)
334 DrawText( str
, 0, 0 );
339 void wxGraphicsContext::StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
)
341 wxGraphicsPath
* path
= CreatePath();
342 path
->MoveToPoint(x1
, y1
);
343 path
->AddLineToPoint( x2
, y2
);
348 void wxGraphicsContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
350 wxGraphicsPath
* path
= CreatePath();
351 path
->AddRectangle( x
, y
, w
, h
);
356 void wxGraphicsContext::DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
358 wxGraphicsPath
* path
= CreatePath();
359 path
->AddEllipse(x
,y
,w
,h
);
364 void wxGraphicsContext::DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
)
366 wxGraphicsPath
* path
= CreatePath();
367 path
->AddRoundedRectangle(x
,y
,w
,h
,radius
);
372 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
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
);
383 void wxGraphicsContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
)
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
);
394 void wxGraphicsContext::StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
)
397 wxGraphicsPath
* path
= CreatePath();
398 for ( size_t i
= 0; i
< n
; ++i
)
400 path
->MoveToPoint(beginPoints
[i
].m_x
, beginPoints
[i
].m_y
);
401 path
->AddLineToPoint( endPoints
[i
].m_x
, endPoints
[i
].m_y
);
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
)
411 return GetRenderer()->CreateMatrix(a
,b
,c
,d
,tx
,ty
);
414 wxGraphicsPath
* wxGraphicsContext::CreatePath()
416 return GetRenderer()->CreatePath();
419 wxGraphicsPen
wxGraphicsContext::CreatePen(const wxPen
& pen
)
421 return GetRenderer()->CreatePen(pen
);
424 wxGraphicsBrush
wxGraphicsContext::CreateBrush(const wxBrush
& brush
)
426 return GetRenderer()->CreateBrush(brush
);
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
)
433 return GetRenderer()->CreateLinearGradientBrush(x1
,y1
,x2
,y2
,c1
,c2
);
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
)
441 return GetRenderer()->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
445 wxGraphicsFont
wxGraphicsContext::CreateFont( const wxFont
&font
, const wxColour
&col
)
447 return GetRenderer()->CreateFont(font
,col
);
450 wxGraphicsContext
* wxGraphicsContext::Create( const wxWindowDC
& dc
)
452 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(dc
);
455 wxGraphicsContext
* wxGraphicsContext::CreateFromNative( void * context
)
457 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeContext(context
);
460 wxGraphicsContext
* wxGraphicsContext::CreateFromNativeWindow( void * window
)
462 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContextFromNativeWindow(window
);
465 wxGraphicsContext
* wxGraphicsContext::Create( wxWindow
* window
)
467 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext(window
);
470 #endif // wxUSE_GRAPHICS_CONTEXT