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_
15 #if wxMAC_USE_CORE_GRAPHICS
16 #undef wxUSE_GRAPHICS_CONTEXT
17 #define wxUSE_GRAPHICS_CONTEXT 1
20 #ifndef wxUSE_GRAPHICS_CONTEXT
21 #define wxUSE_GRAPHICS_CONTEXT 0
24 #if wxUSE_GRAPHICS_CONTEXT
26 // ---------------------------------------------------------------------------
28 // ---------------------------------------------------------------------------
30 #include "wx/geometry.h"
31 #include "wx/dcclient.h"
32 #include "wx/dynarray.h"
35 * notes about the graphics context apis
37 * angles : are measured in radians, 0.0 being in direction of positiv x axis, PI/2 being
38 * in direction of positive y axis.
41 class WXDLLEXPORT wxGraphicsPath
45 virtual ~wxGraphicsPath() {}
48 // These are the path primitives from which everything else can be constructed
51 // begins a new subpath at (x,y)
52 virtual void MoveToPoint( wxDouble x
, wxDouble y
) = 0;
54 // adds a straight line from the current point to (x,y)
55 virtual void AddLineToPoint( wxDouble x
, wxDouble y
) = 0;
57 // adds a cubic Bezier curve from the current point, using two control points and an end point
58 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
) = 0;
60 // closes the current sub-path
61 virtual void CloseSubpath() = 0;
63 // gets the last point of the current path, (0,0) if not yet set
64 virtual void GetCurrentPoint( wxDouble
& x
, wxDouble
&y
) = 0;
66 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
67 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) = 0;
70 // These are convenience functions which - if not available natively will be assembled
71 // using the primitives from above
74 // adds a quadratic Bezier curve from the current point, using a control point and an end point
75 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
77 // appends a rectangle as a new closed subpath
78 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
80 // appends an ellipsis as a new closed subpath fitting the passed rectangle
81 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
83 // 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)
84 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
) ;
86 // wrappers using wxPoint2DDoubles
88 wxPoint2DDouble
GetCurrentPoint();
90 void MoveToPoint( const wxPoint2DDouble
& p
);
92 void AddLineToPoint( const wxPoint2DDouble
& p
);
94 void AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
);
96 void AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
98 DECLARE_NO_COPY_CLASS(wxGraphicsPath
)
101 class WXDLLEXPORT wxGraphicsContext
104 wxGraphicsContext() {}
105 virtual ~wxGraphicsContext() {}
107 static wxGraphicsContext
* Create( const wxWindowDC
& dc
) ;
109 // creates a path instance that corresponds to the type of graphics context, ie GDIPlus, cairo, CoreGraphics ...
110 virtual wxGraphicsPath
* CreatePath() = 0;
112 // push the current state of the context, ie the transformation matrix on a stack
113 virtual void PushState() = 0;
115 // pops a stored state from the stack
116 virtual void PopState() = 0;
118 // clips drawings to the region
119 virtual void Clip( const wxRegion
®ion
) = 0;
126 virtual void Translate( wxDouble dx
, wxDouble dy
) = 0;
129 virtual void Scale( wxDouble xScale
, wxDouble yScale
) = 0;
132 virtual void Rotate( wxDouble angle
) = 0;
139 virtual void SetPen( const wxPen
&pen
) = 0;
141 // sets the brush for filling
142 virtual void SetBrush( const wxBrush
&brush
) = 0;
144 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
145 virtual void SetLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
146 const wxColour
&c1
, const wxColour
&c2
) = 0;
148 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
149 // with radius r and color cColor
150 virtual void SetRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
151 const wxColour
&oColor
, const wxColour
&cColor
) = 0;
154 virtual void SetFont( const wxFont
&font
) = 0;
156 // sets the text color
157 virtual void SetTextColor( const wxColour
&col
) = 0;
159 // strokes along a path with the current pen
160 virtual void StrokePath( const wxGraphicsPath
*path
) = 0;
162 // fills a path with the current brush
163 virtual void FillPath( const wxGraphicsPath
*path
, int fillStyle
= wxWINDING_RULE
) = 0;
165 // draws a path by first filling and then stroking
166 virtual void DrawPath( const wxGraphicsPath
*path
, int fillStyle
= wxWINDING_RULE
);
172 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
) = 0;
174 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
176 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
177 wxDouble
*descent
, wxDouble
*externalLeading
) const = 0;
179 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const = 0;
185 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
187 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
190 // convenience methods
193 // strokes a single line
194 virtual void StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
);
196 // stroke lines connecting each of the points
197 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
199 // stroke disconnected lines from begin to end points
200 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
);
203 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
= wxWINDING_RULE
);
206 virtual void DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
209 virtual void DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
211 // draws a rounded rectangle
212 virtual void DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
);
214 // wrappers using wxPoint2DDouble TODO
216 DECLARE_NO_COPY_CLASS(wxGraphicsContext
)
219 class WXDLLEXPORT wxGCDC
: public wxDC
221 DECLARE_DYNAMIC_CLASS(wxGCDC
)
222 DECLARE_NO_COPY_CLASS(wxGCDC
)
225 wxGCDC(const wxWindowDC
& dc
);
232 // implement base class pure virtuals
233 // ----------------------------------
235 virtual void Clear();
237 virtual bool StartDoc( const wxString
& WXUNUSED(message
) ) { return true; }
238 virtual void EndDoc(void) {}
240 virtual void StartPage(void) {}
241 virtual void EndPage(void) {}
243 virtual void SetFont(const wxFont
& font
);
244 virtual void SetPen(const wxPen
& pen
);
245 virtual void SetBrush(const wxBrush
& brush
);
246 virtual void SetBackground(const wxBrush
& brush
);
247 virtual void SetBackgroundMode(int mode
);
248 virtual void SetPalette(const wxPalette
& palette
);
250 virtual void DestroyClippingRegion();
252 virtual wxCoord
GetCharHeight() const;
253 virtual wxCoord
GetCharWidth() const;
255 virtual bool CanDrawBitmap() const;
256 virtual bool CanGetTextExtent() const;
257 virtual int GetDepth() const;
258 virtual wxSize
GetPPI() const;
260 virtual void SetMapMode(int mode
);
261 virtual void SetUserScale(double x
, double y
);
263 virtual void SetLogicalScale(double x
, double y
);
264 virtual void SetLogicalOrigin(wxCoord x
, wxCoord y
);
265 virtual void SetDeviceOrigin(wxCoord x
, wxCoord y
);
266 virtual void SetAxisOrientation(bool xLeftRight
, bool yBottomUp
);
267 virtual void SetLogicalFunction(int function
);
269 virtual void SetTextForeground(const wxColour
& colour
);
270 virtual void SetTextBackground(const wxColour
& colour
);
272 virtual void ComputeScaleAndOrigin();
274 wxGraphicsContext
* GetGraphicContext() { return m_graphicContext
; }
277 // the true implementations
278 virtual bool DoFloodFill(wxCoord x
, wxCoord y
, const wxColour
& col
,
279 int style
= wxFLOOD_SURFACE
);
281 virtual void DoGradientFillLinear(const wxRect
& rect
,
282 const wxColour
& initialColour
,
283 const wxColour
& destColour
,
284 wxDirection nDirection
= wxEAST
);
286 virtual void DoGradientFillConcentric(const wxRect
& rect
,
287 const wxColour
& initialColour
,
288 const wxColour
& destColour
,
289 const wxPoint
& circleCenter
);
291 virtual bool DoGetPixel(wxCoord x
, wxCoord y
, wxColour
*col
) const;
293 virtual void DoDrawPoint(wxCoord x
, wxCoord y
);
296 virtual void DoDrawSpline(wxList
*points
);
299 virtual void DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
);
301 virtual void DoDrawArc(wxCoord x1
, wxCoord y1
,
302 wxCoord x2
, wxCoord y2
,
303 wxCoord xc
, wxCoord yc
);
305 virtual void DoDrawCheckMark(wxCoord x
, wxCoord y
,
306 wxCoord width
, wxCoord height
);
308 virtual void DoDrawEllipticArc(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
309 double sa
, double ea
);
311 virtual void DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
);
312 virtual void DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
313 wxCoord width
, wxCoord height
,
315 virtual void DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
);
317 virtual void DoCrossHair(wxCoord x
, wxCoord y
);
319 virtual void DoDrawIcon(const wxIcon
& icon
, wxCoord x
, wxCoord y
);
320 virtual void DoDrawBitmap(const wxBitmap
&bmp
, wxCoord x
, wxCoord y
,
321 bool useMask
= false);
323 virtual void DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
);
324 virtual void DoDrawRotatedText(const wxString
& text
, wxCoord x
, wxCoord y
,
327 virtual bool DoBlit(wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
328 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
329 int rop
= wxCOPY
, bool useMask
= false, wxCoord xsrcMask
= -1, wxCoord ysrcMask
= -1);
331 virtual void DoGetSize(int *,int *) const;
332 virtual void DoGetSizeMM(int* width
, int* height
) const;
334 virtual void DoDrawLines(int n
, wxPoint points
[],
335 wxCoord xoffset
, wxCoord yoffset
);
336 virtual void DoDrawPolygon(int n
, wxPoint points
[],
337 wxCoord xoffset
, wxCoord yoffset
,
338 int fillStyle
= wxODDEVEN_RULE
);
339 virtual void DoDrawPolyPolygon(int n
, int count
[], wxPoint points
[],
340 wxCoord xoffset
, wxCoord yoffset
,
343 virtual void DoSetClippingRegionAsRegion(const wxRegion
& region
);
344 virtual void DoSetClippingRegion(wxCoord x
, wxCoord y
,
345 wxCoord width
, wxCoord height
);
347 virtual void DoGetTextExtent(const wxString
& string
,
348 wxCoord
*x
, wxCoord
*y
,
349 wxCoord
*descent
= NULL
,
350 wxCoord
*externalLeading
= NULL
,
351 wxFont
*theFont
= NULL
) const;
353 virtual bool DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const;
357 double m_mm_to_pix_x
, m_mm_to_pix_y
;
359 wxGraphicsContext
* m_graphicContext
;