1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: graphics context header
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_GRAPHICS_H_
13 #define _WX_GRAPHICS_H_
16 #if wxUSE_GRAPHICS_CONTEXT
18 // ---------------------------------------------------------------------------
20 // ---------------------------------------------------------------------------
22 #include "wx/geometry.h"
23 #include "wx/dcclient.h"
24 #include "wx/dynarray.h"
27 * notes about the graphics context apis
29 * angles : are measured in radians, 0.0 being in direction of positiv x axis, PI/2 being
30 * in direction of positive y axis.
33 class WXDLLEXPORT wxGraphicsPath
37 virtual ~wxGraphicsPath() {}
40 // These are the path primitives from which everything else can be constructed
43 // begins a new subpath at (x,y)
44 virtual void MoveToPoint( wxDouble x
, wxDouble y
) = 0;
46 // adds a straight line from the current point to (x,y)
47 virtual void AddLineToPoint( wxDouble x
, wxDouble y
) = 0;
49 // adds a cubic Bezier curve from the current point, using two control points and an end point
50 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
) = 0;
52 // closes the current sub-path
53 virtual void CloseSubpath() = 0;
55 // gets the last point of the current path, (0,0) if not yet set
56 virtual void GetCurrentPoint( wxDouble
& x
, wxDouble
&y
) = 0;
58 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
59 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) = 0;
62 // These are convenience functions which - if not available natively will be assembled
63 // using the primitives from above
66 // adds a quadratic Bezier curve from the current point, using a control point and an end point
67 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
69 // appends a rectangle as a new closed subpath
70 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
72 // appends an ellipsis as a new closed subpath fitting the passed rectangle
73 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
75 // 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)
76 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
) ;
78 // wrappers using wxPoint2DDoubles
80 wxPoint2DDouble
GetCurrentPoint();
82 void MoveToPoint( const wxPoint2DDouble
& p
);
84 void AddLineToPoint( const wxPoint2DDouble
& p
);
86 void AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
);
88 void AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
90 DECLARE_NO_COPY_CLASS(wxGraphicsPath
)
93 class WXDLLEXPORT wxGraphicsContext
96 wxGraphicsContext() {}
97 virtual ~wxGraphicsContext() {}
99 static wxGraphicsContext
* Create( const wxWindowDC
& dc
) ;
101 // creates a path instance that corresponds to the type of graphics context, ie GDIPlus, cairo, CoreGraphics ...
102 virtual wxGraphicsPath
* CreatePath() = 0;
104 // push the current state of the context, ie the transformation matrix on a stack
105 virtual void PushState() = 0;
107 // pops a stored state from the stack
108 virtual void PopState() = 0;
110 // clips drawings to the region
111 virtual void Clip( const wxRegion
®ion
) = 0;
118 virtual void Translate( wxDouble dx
, wxDouble dy
) = 0;
121 virtual void Scale( wxDouble xScale
, wxDouble yScale
) = 0;
124 virtual void Rotate( wxDouble angle
) = 0;
131 virtual void SetPen( const wxPen
&pen
) = 0;
133 // sets the brush for filling
134 virtual void SetBrush( const wxBrush
&brush
) = 0;
136 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
137 virtual void SetLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
138 const wxColour
&c1
, const wxColour
&c2
) = 0;
140 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
141 // with radius r and color cColor
142 virtual void SetRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
143 const wxColour
&oColor
, const wxColour
&cColor
) = 0;
146 virtual void SetFont( const wxFont
&font
) = 0;
148 // sets the text color
149 virtual void SetTextColor( const wxColour
&col
) = 0;
151 // strokes along a path with the current pen
152 virtual void StrokePath( const wxGraphicsPath
*path
) = 0;
154 // fills a path with the current brush
155 virtual void FillPath( const wxGraphicsPath
*path
, int fillStyle
= wxWINDING_RULE
) = 0;
157 // draws a path by first filling and then stroking
158 virtual void DrawPath( const wxGraphicsPath
*path
, int fillStyle
= wxWINDING_RULE
);
164 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
) = 0;
166 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
168 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
169 wxDouble
*descent
, wxDouble
*externalLeading
) const = 0;
171 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const = 0;
177 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
179 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
182 // convenience methods
185 // strokes a single line
186 virtual void StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
);
188 // stroke lines connecting each of the points
189 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
191 // stroke disconnected lines from begin to end points
192 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
);
195 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
= wxWINDING_RULE
);
198 virtual void DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
201 virtual void DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
203 // draws a rounded rectangle
204 virtual void DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
);
206 // wrappers using wxPoint2DDouble TODO
208 DECLARE_NO_COPY_CLASS(wxGraphicsContext
)
211 class WXDLLEXPORT wxGCDC
: public wxDC
213 DECLARE_DYNAMIC_CLASS(wxGCDC
)
214 DECLARE_NO_COPY_CLASS(wxGCDC
)
217 wxGCDC(const wxWindowDC
& dc
);
224 // implement base class pure virtuals
225 // ----------------------------------
227 virtual void Clear();
229 virtual bool StartDoc( const wxString
& WXUNUSED(message
) ) { return true; }
230 virtual void EndDoc(void) {}
232 virtual void StartPage(void) {}
233 virtual void EndPage(void) {}
235 virtual void SetFont(const wxFont
& font
);
236 virtual void SetPen(const wxPen
& pen
);
237 virtual void SetBrush(const wxBrush
& brush
);
238 virtual void SetBackground(const wxBrush
& brush
);
239 virtual void SetBackgroundMode(int mode
);
240 virtual void SetPalette(const wxPalette
& palette
);
242 virtual void DestroyClippingRegion();
244 virtual wxCoord
GetCharHeight() const;
245 virtual wxCoord
GetCharWidth() const;
247 virtual bool CanDrawBitmap() const;
248 virtual bool CanGetTextExtent() const;
249 virtual int GetDepth() const;
250 virtual wxSize
GetPPI() const;
252 virtual void SetMapMode(int mode
);
253 virtual void SetUserScale(double x
, double y
);
255 virtual void SetLogicalScale(double x
, double y
);
256 virtual void SetLogicalOrigin(wxCoord x
, wxCoord y
);
257 virtual void SetDeviceOrigin(wxCoord x
, wxCoord y
);
258 virtual void SetAxisOrientation(bool xLeftRight
, bool yBottomUp
);
259 virtual void SetLogicalFunction(int function
);
261 virtual void SetTextForeground(const wxColour
& colour
);
262 virtual void SetTextBackground(const wxColour
& colour
);
264 virtual void ComputeScaleAndOrigin();
266 wxGraphicsContext
* GetGraphicContext() { return m_graphicContext
; }
269 // the true implementations
270 virtual bool DoFloodFill(wxCoord x
, wxCoord y
, const wxColour
& col
,
271 int style
= wxFLOOD_SURFACE
);
273 virtual void DoGradientFillLinear(const wxRect
& rect
,
274 const wxColour
& initialColour
,
275 const wxColour
& destColour
,
276 wxDirection nDirection
= wxEAST
);
278 virtual void DoGradientFillConcentric(const wxRect
& rect
,
279 const wxColour
& initialColour
,
280 const wxColour
& destColour
,
281 const wxPoint
& circleCenter
);
283 virtual bool DoGetPixel(wxCoord x
, wxCoord y
, wxColour
*col
) const;
285 virtual void DoDrawPoint(wxCoord x
, wxCoord y
);
288 virtual void DoDrawSpline(wxList
*points
);
291 virtual void DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
);
293 virtual void DoDrawArc(wxCoord x1
, wxCoord y1
,
294 wxCoord x2
, wxCoord y2
,
295 wxCoord xc
, wxCoord yc
);
297 virtual void DoDrawCheckMark(wxCoord x
, wxCoord y
,
298 wxCoord width
, wxCoord height
);
300 virtual void DoDrawEllipticArc(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
301 double sa
, double ea
);
303 virtual void DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
);
304 virtual void DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
305 wxCoord width
, wxCoord height
,
307 virtual void DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
);
309 virtual void DoCrossHair(wxCoord x
, wxCoord y
);
311 virtual void DoDrawIcon(const wxIcon
& icon
, wxCoord x
, wxCoord y
);
312 virtual void DoDrawBitmap(const wxBitmap
&bmp
, wxCoord x
, wxCoord y
,
313 bool useMask
= false);
315 virtual void DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
);
316 virtual void DoDrawRotatedText(const wxString
& text
, wxCoord x
, wxCoord y
,
319 virtual bool DoBlit(wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
320 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
321 int rop
= wxCOPY
, bool useMask
= false, wxCoord xsrcMask
= -1, wxCoord ysrcMask
= -1);
323 virtual void DoGetSize(int *,int *) const;
324 virtual void DoGetSizeMM(int* width
, int* height
) const;
326 virtual void DoDrawLines(int n
, wxPoint points
[],
327 wxCoord xoffset
, wxCoord yoffset
);
328 virtual void DoDrawPolygon(int n
, wxPoint points
[],
329 wxCoord xoffset
, wxCoord yoffset
,
330 int fillStyle
= wxODDEVEN_RULE
);
331 virtual void DoDrawPolyPolygon(int n
, int count
[], wxPoint points
[],
332 wxCoord xoffset
, wxCoord yoffset
,
335 virtual void DoSetClippingRegionAsRegion(const wxRegion
& region
);
336 virtual void DoSetClippingRegion(wxCoord x
, wxCoord y
,
337 wxCoord width
, wxCoord height
);
339 virtual void DoGetTextExtent(const wxString
& string
,
340 wxCoord
*x
, wxCoord
*y
,
341 wxCoord
*descent
= NULL
,
342 wxCoord
*externalLeading
= NULL
,
343 wxFont
*theFont
= NULL
) const;
345 virtual bool DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const;
349 double m_mm_to_pix_x
, m_mm_to_pix_y
;
351 wxGraphicsContext
* m_graphicContext
;