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 #include "wx/geometry.h"
16 #include "wx/dynarray.h"
18 class WXDLLEXPORT wxWindowDC
;
20 #if wxUSE_GRAPHICS_CONTEXT
23 * notes about the graphics context apis
25 * angles : are measured in radians, 0.0 being in direction of positiv x axis, PI/2 being
26 * in direction of positive y axis.
29 class WXDLLEXPORT wxGraphicsPath
33 virtual ~wxGraphicsPath() {}
36 // These are the path primitives from which everything else can be constructed
39 // begins a new subpath at (x,y)
40 virtual void MoveToPoint( wxDouble x
, wxDouble y
) = 0;
42 // adds a straight line from the current point to (x,y)
43 virtual void AddLineToPoint( wxDouble x
, wxDouble y
) = 0;
45 // adds a cubic Bezier curve from the current point, using two control points and an end point
46 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
) = 0;
48 // closes the current sub-path
49 virtual void CloseSubpath() = 0;
51 // gets the last point of the current path, (0,0) if not yet set
52 virtual void GetCurrentPoint( wxDouble
& x
, wxDouble
&y
) = 0;
54 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
55 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) = 0;
58 // These are convenience functions which - if not available natively will be assembled
59 // using the primitives from above
62 // adds a quadratic Bezier curve from the current point, using a control point and an end point
63 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
65 // appends a rectangle as a new closed subpath
66 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
68 // appends an ellipsis as a new closed subpath fitting the passed rectangle
69 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
71 // 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)
72 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
) ;
74 // wrappers using wxPoint2DDoubles
76 wxPoint2DDouble
GetCurrentPoint();
78 void MoveToPoint( const wxPoint2DDouble
& p
);
80 void AddLineToPoint( const wxPoint2DDouble
& p
);
82 void AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
);
84 void AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
86 DECLARE_NO_COPY_CLASS(wxGraphicsPath
)
90 class WXDLLEXPORT wxGraphicsMatrix
95 virtual ~wxGraphicsMatrix() {}
97 wxGraphicsMatrix* Concat( const wxGraphicsMatrix *t ) const;
99 // returns the inverse matrix
100 wxGraphicsMatrix* Invert() const;
102 // returns true if the elements of the transformation matrix are equal ?
103 bool operator==(const wxGraphicsMatrix& t) const;
105 // return true if this is the identity matrix
113 virtual void Translate( wxDouble dx , wxDouble dy ) = 0;
116 virtual void Scale( wxDouble xScale , wxDouble yScale ) = 0;
119 virtual void Rotate( wxDouble angle ) = 0;
123 class WXDLLEXPORT wxGraphicsContext
126 wxGraphicsContext() {}
128 virtual ~wxGraphicsContext() {}
130 static wxGraphicsContext
* Create( const wxWindowDC
& dc
) ;
132 static wxGraphicsContext
* CreateFromNative( void * context
) ;
135 static wxGraphicsContext
* CreateFromNativeWindow( void * window
) ;
137 static wxGraphicsContext
* Create( wxWindow
* window
) ;
139 // creates a path instance that corresponds to the type of graphics context, ie GDIPlus, cairo, CoreGraphics ...
140 virtual wxGraphicsPath
* CreatePath() = 0;
143 // create a 'native' matrix corresponding to these values
144 virtual wxGraphicsMatrix* CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
145 wxDouble tx=0.0, wxDouble ty=0.0) = 0;
148 // push the current state of the context, ie the transformation matrix on a stack
149 virtual void PushState() = 0;
151 // pops a stored state from the stack
152 virtual void PopState() = 0;
154 // clips drawings to the region, combined to current clipping region
155 virtual void Clip( const wxRegion
®ion
) = 0;
157 // clips drawings to the rect
158 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
160 // resets the clipping to original extent
161 virtual void ResetClip() = 0 ;
163 // returns the native context
164 virtual void * GetNativeContext() = 0;
167 // transformation : changes the current transformation matrix CTM of the context
171 virtual void Translate( wxDouble dx
, wxDouble dy
) = 0;
174 virtual void Scale( wxDouble xScale
, wxDouble yScale
) = 0;
177 virtual void Rotate( wxDouble angle
) = 0;
184 virtual void SetPen( const wxPen
&pen
) = 0;
186 // sets the brush for filling
187 virtual void SetBrush( const wxBrush
&brush
) = 0;
189 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
190 virtual void SetLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
191 const wxColour
&c1
, const wxColour
&c2
) = 0;
193 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
194 // with radius r and color cColor
195 virtual void SetRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
196 const wxColour
&oColor
, const wxColour
&cColor
) = 0;
199 virtual void SetFont( const wxFont
&font
) = 0;
201 // sets the text color
202 virtual void SetTextColor( const wxColour
&col
) = 0;
204 // strokes along a path with the current pen
205 virtual void StrokePath( const wxGraphicsPath
*path
) = 0;
207 // fills a path with the current brush
208 virtual void FillPath( const wxGraphicsPath
*path
, int fillStyle
= wxWINDING_RULE
) = 0;
210 // draws a path by first filling and then stroking
211 virtual void DrawPath( const wxGraphicsPath
*path
, int fillStyle
= wxWINDING_RULE
);
217 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
) = 0;
219 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
221 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
222 wxDouble
*descent
, wxDouble
*externalLeading
) const = 0;
224 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const = 0;
230 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
232 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
235 // convenience methods
238 // strokes a single line
239 virtual void StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
);
241 // stroke lines connecting each of the points
242 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
244 // stroke disconnected lines from begin to end points
245 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
);
248 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
= wxWINDING_RULE
);
251 virtual void DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
254 virtual void DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
256 // draws a rounded rectangle
257 virtual void DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
);
259 // wrappers using wxPoint2DDouble TODO
261 DECLARE_NO_COPY_CLASS(wxGraphicsContext
)
268 class WXDLLEXPORT wxGCDC
:
275 DECLARE_DYNAMIC_CLASS(wxGCDC
)
276 DECLARE_NO_COPY_CLASS(wxGCDC
)
279 wxGCDC(const wxWindowDC
& dc
);
286 // implement base class pure virtuals
287 // ----------------------------------
289 virtual void Clear();
291 virtual bool StartDoc( const wxString
& WXUNUSED(message
) ) { return true; }
292 virtual void EndDoc(void) {}
294 virtual void StartPage(void) {}
295 virtual void EndPage(void) {}
297 virtual void SetFont(const wxFont
& font
);
298 virtual void SetPen(const wxPen
& pen
);
299 virtual void SetBrush(const wxBrush
& brush
);
300 virtual void SetBackground(const wxBrush
& brush
);
301 virtual void SetBackgroundMode(int mode
);
302 virtual void SetPalette(const wxPalette
& palette
);
304 virtual void DestroyClippingRegion();
306 virtual wxCoord
GetCharHeight() const;
307 virtual wxCoord
GetCharWidth() const;
309 virtual bool CanDrawBitmap() const;
310 virtual bool CanGetTextExtent() const;
311 virtual int GetDepth() const;
312 virtual wxSize
GetPPI() const;
314 virtual void SetMapMode(int mode
);
315 virtual void SetUserScale(double x
, double y
);
317 virtual void SetLogicalScale(double x
, double y
);
318 virtual void SetLogicalOrigin(wxCoord x
, wxCoord y
);
319 virtual void SetDeviceOrigin(wxCoord x
, wxCoord y
);
320 virtual void SetAxisOrientation(bool xLeftRight
, bool yBottomUp
);
321 virtual void SetLogicalFunction(int function
);
323 virtual void SetTextForeground(const wxColour
& colour
);
324 virtual void SetTextBackground(const wxColour
& colour
);
326 virtual void ComputeScaleAndOrigin();
328 wxGraphicsContext
* GetGraphicsContext() { return m_graphicContext
; }
329 virtual void SetGraphicsContext( wxGraphicsContext
* ctx
)
330 { delete m_graphicContext
; m_graphicContext
= ctx
; }
332 // the true implementations
333 virtual bool DoFloodFill(wxCoord x
, wxCoord y
, const wxColour
& col
,
334 int style
= wxFLOOD_SURFACE
);
336 virtual void DoGradientFillLinear(const wxRect
& rect
,
337 const wxColour
& initialColour
,
338 const wxColour
& destColour
,
339 wxDirection nDirection
= wxEAST
);
341 virtual void DoGradientFillConcentric(const wxRect
& rect
,
342 const wxColour
& initialColour
,
343 const wxColour
& destColour
,
344 const wxPoint
& circleCenter
);
346 virtual bool DoGetPixel(wxCoord x
, wxCoord y
, wxColour
*col
) const;
348 virtual void DoDrawPoint(wxCoord x
, wxCoord y
);
351 virtual void DoDrawSpline(wxList
*points
);
354 virtual void DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
);
356 virtual void DoDrawArc(wxCoord x1
, wxCoord y1
,
357 wxCoord x2
, wxCoord y2
,
358 wxCoord xc
, wxCoord yc
);
360 virtual void DoDrawCheckMark(wxCoord x
, wxCoord y
,
361 wxCoord width
, wxCoord height
);
363 virtual void DoDrawEllipticArc(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
364 double sa
, double ea
);
366 virtual void DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
);
367 virtual void DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
368 wxCoord width
, wxCoord height
,
370 virtual void DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
);
372 virtual void DoCrossHair(wxCoord x
, wxCoord y
);
374 virtual void DoDrawIcon(const wxIcon
& icon
, wxCoord x
, wxCoord y
);
375 virtual void DoDrawBitmap(const wxBitmap
&bmp
, wxCoord x
, wxCoord y
,
376 bool useMask
= false);
378 virtual void DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
);
379 virtual void DoDrawRotatedText(const wxString
& text
, wxCoord x
, wxCoord y
,
382 virtual bool DoBlit(wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
383 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
384 int rop
= wxCOPY
, bool useMask
= false, wxCoord xsrcMask
= -1, wxCoord ysrcMask
= -1);
386 virtual void DoGetSize(int *,int *) const;
387 virtual void DoGetSizeMM(int* width
, int* height
) const;
389 virtual void DoDrawLines(int n
, wxPoint points
[],
390 wxCoord xoffset
, wxCoord yoffset
);
391 virtual void DoDrawPolygon(int n
, wxPoint points
[],
392 wxCoord xoffset
, wxCoord yoffset
,
393 int fillStyle
= wxODDEVEN_RULE
);
394 virtual void DoDrawPolyPolygon(int n
, int count
[], wxPoint points
[],
395 wxCoord xoffset
, wxCoord yoffset
,
398 virtual void DoSetClippingRegionAsRegion(const wxRegion
& region
);
399 virtual void DoSetClippingRegion(wxCoord x
, wxCoord y
,
400 wxCoord width
, wxCoord height
);
402 virtual void DoGetTextExtent(const wxString
& string
,
403 wxCoord
*x
, wxCoord
*y
,
404 wxCoord
*descent
= NULL
,
405 wxCoord
*externalLeading
= NULL
,
406 wxFont
*theFont
= NULL
) const;
408 virtual bool DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const;
412 double m_mm_to_pix_x
, m_mm_to_pix_y
;
414 double m_formerScaleX
, m_formerScaleY
;
416 wxGraphicsContext
* m_graphicContext
;