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"
19 class WXDLLEXPORT wxWindowDC
;
21 #if wxUSE_GRAPHICS_CONTEXT
24 * notes about the graphics context apis
26 * angles : are measured in radians, 0.0 being in direction of positiv x axis, PI/2 being
27 * in direction of positive y axis.
30 class WXDLLEXPORT wxGraphicsPath
34 virtual ~wxGraphicsPath() {}
37 // These are the path primitives from which everything else can be constructed
40 // begins a new subpath at (x,y)
41 virtual void MoveToPoint( wxDouble x
, wxDouble y
) = 0;
43 // adds a straight line from the current point to (x,y)
44 virtual void AddLineToPoint( wxDouble x
, wxDouble y
) = 0;
46 // adds a cubic Bezier curve from the current point, using two control points and an end point
47 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
) = 0;
49 // closes the current sub-path
50 virtual void CloseSubpath() = 0;
52 // gets the last point of the current path, (0,0) if not yet set
53 virtual void GetCurrentPoint( wxDouble
& x
, wxDouble
&y
) = 0;
55 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
56 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) = 0;
59 // These are convenience functions which - if not available natively will be assembled
60 // using the primitives from above
63 // adds a quadratic Bezier curve from the current point, using a control point and an end point
64 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
66 // appends a rectangle as a new closed subpath
67 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
69 // appends an ellipsis as a new closed subpath fitting the passed rectangle
70 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
72 // 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)
73 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
) ;
75 // wrappers using wxPoint2DDoubles
77 wxPoint2DDouble
GetCurrentPoint();
79 void MoveToPoint( const wxPoint2DDouble
& p
);
81 void AddLineToPoint( const wxPoint2DDouble
& p
);
83 void AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
);
85 void AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
87 DECLARE_NO_COPY_CLASS(wxGraphicsPath
)
91 class WXDLLEXPORT wxGraphicsMatrix
96 virtual ~wxGraphicsMatrix() {}
98 wxGraphicsMatrix* Concat( const wxGraphicsMatrix *t ) const;
100 // returns the inverse matrix
101 wxGraphicsMatrix* Invert() const;
103 // returns true if the elements of the transformation matrix are equal ?
104 bool operator==(const wxGraphicsMatrix& t) const;
106 // return true if this is the identity matrix
114 virtual void Translate( wxDouble dx , wxDouble dy ) = 0;
117 virtual void Scale( wxDouble xScale , wxDouble yScale ) = 0;
120 virtual void Rotate( wxDouble angle ) = 0;
124 class WXDLLEXPORT wxGraphicsContext
127 wxGraphicsContext() {}
129 virtual ~wxGraphicsContext() {}
131 static wxGraphicsContext
* Create( const wxWindowDC
& dc
) ;
133 static wxGraphicsContext
* CreateFromNative( void * context
) ;
136 static wxGraphicsContext
* CreateFromNativeWindow( void * window
) ;
138 static wxGraphicsContext
* Create( wxWindow
* window
) ;
140 // creates a path instance that corresponds to the type of graphics context, ie GDIPlus, cairo, CoreGraphics ...
141 virtual wxGraphicsPath
* CreatePath() = 0;
144 // create a 'native' matrix corresponding to these values
145 virtual wxGraphicsMatrix* CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
146 wxDouble tx=0.0, wxDouble ty=0.0) = 0;
149 // push the current state of the context, ie the transformation matrix on a stack
150 virtual void PushState() = 0;
152 // pops a stored state from the stack
153 virtual void PopState() = 0;
155 // clips drawings to the region, combined to current clipping region
156 virtual void Clip( const wxRegion
®ion
) = 0;
158 // clips drawings to the rect
159 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
161 // resets the clipping to original extent
162 virtual void ResetClip() = 0 ;
164 // returns the native context
165 virtual void * GetNativeContext() = 0;
168 // transformation : changes the current transformation matrix CTM of the context
172 virtual void Translate( wxDouble dx
, wxDouble dy
) = 0;
175 virtual void Scale( wxDouble xScale
, wxDouble yScale
) = 0;
178 virtual void Rotate( wxDouble angle
) = 0;
185 virtual void SetPen( const wxPen
&pen
) = 0;
187 // sets the brush for filling
188 virtual void SetBrush( const wxBrush
&brush
) = 0;
190 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
191 virtual void SetLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
192 const wxColour
&c1
, const wxColour
&c2
) = 0;
194 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
195 // with radius r and color cColor
196 virtual void SetRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
197 const wxColour
&oColor
, const wxColour
&cColor
) = 0;
200 virtual void SetFont( const wxFont
&font
) = 0;
202 // sets the text color
203 virtual void SetTextColor( const wxColour
&col
) = 0;
205 // strokes along a path with the current pen
206 virtual void StrokePath( const wxGraphicsPath
*path
) = 0;
208 // fills a path with the current brush
209 virtual void FillPath( const wxGraphicsPath
*path
, int fillStyle
= wxWINDING_RULE
) = 0;
211 // draws a path by first filling and then stroking
212 virtual void DrawPath( const wxGraphicsPath
*path
, int fillStyle
= wxWINDING_RULE
);
218 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
) = 0;
220 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
222 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
223 wxDouble
*descent
, wxDouble
*externalLeading
) const = 0;
225 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const = 0;
231 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
233 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
236 // convenience methods
239 // strokes a single line
240 virtual void StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
);
242 // stroke lines connecting each of the points
243 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
245 // stroke disconnected lines from begin to end points
246 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
);
249 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
= wxWINDING_RULE
);
252 virtual void DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
255 virtual void DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
257 // draws a rounded rectangle
258 virtual void DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
);
260 // wrappers using wxPoint2DDouble TODO
262 DECLARE_NO_COPY_CLASS(wxGraphicsContext
)
269 class WXDLLEXPORT wxGCDC
:
276 DECLARE_DYNAMIC_CLASS(wxGCDC
)
277 DECLARE_NO_COPY_CLASS(wxGCDC
)
280 wxGCDC(const wxWindowDC
& dc
);
287 // implement base class pure virtuals
288 // ----------------------------------
290 virtual void Clear();
292 virtual bool StartDoc( const wxString
& WXUNUSED(message
) ) { return true; }
293 virtual void EndDoc(void) {}
295 virtual void StartPage(void) {}
296 virtual void EndPage(void) {}
298 virtual void SetFont(const wxFont
& font
);
299 virtual void SetPen(const wxPen
& pen
);
300 virtual void SetBrush(const wxBrush
& brush
);
301 virtual void SetBackground(const wxBrush
& brush
);
302 virtual void SetBackgroundMode(int mode
);
303 virtual void SetPalette(const wxPalette
& palette
);
305 virtual void DestroyClippingRegion();
307 virtual wxCoord
GetCharHeight() const;
308 virtual wxCoord
GetCharWidth() const;
310 virtual bool CanDrawBitmap() const;
311 virtual bool CanGetTextExtent() const;
312 virtual int GetDepth() const;
313 virtual wxSize
GetPPI() const;
315 virtual void SetMapMode(int mode
);
316 virtual void SetUserScale(double x
, double y
);
318 virtual void SetLogicalScale(double x
, double y
);
319 virtual void SetLogicalOrigin(wxCoord x
, wxCoord y
);
320 virtual void SetDeviceOrigin(wxCoord x
, wxCoord y
);
321 virtual void SetAxisOrientation(bool xLeftRight
, bool yBottomUp
);
322 virtual void SetLogicalFunction(int function
);
324 virtual void SetTextForeground(const wxColour
& colour
);
325 virtual void SetTextBackground(const wxColour
& colour
);
327 virtual void ComputeScaleAndOrigin();
329 wxGraphicsContext
* GetGraphicsContext() { return m_graphicContext
; }
330 virtual void SetGraphicsContext( wxGraphicsContext
* ctx
)
331 { delete m_graphicContext
; m_graphicContext
= ctx
; }
333 // the true implementations
334 virtual bool DoFloodFill(wxCoord x
, wxCoord y
, const wxColour
& col
,
335 int style
= wxFLOOD_SURFACE
);
337 virtual void DoGradientFillLinear(const wxRect
& rect
,
338 const wxColour
& initialColour
,
339 const wxColour
& destColour
,
340 wxDirection nDirection
= wxEAST
);
342 virtual void DoGradientFillConcentric(const wxRect
& rect
,
343 const wxColour
& initialColour
,
344 const wxColour
& destColour
,
345 const wxPoint
& circleCenter
);
347 virtual bool DoGetPixel(wxCoord x
, wxCoord y
, wxColour
*col
) const;
349 virtual void DoDrawPoint(wxCoord x
, wxCoord y
);
352 virtual void DoDrawSpline(wxList
*points
);
355 virtual void DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
);
357 virtual void DoDrawArc(wxCoord x1
, wxCoord y1
,
358 wxCoord x2
, wxCoord y2
,
359 wxCoord xc
, wxCoord yc
);
361 virtual void DoDrawCheckMark(wxCoord x
, wxCoord y
,
362 wxCoord width
, wxCoord height
);
364 virtual void DoDrawEllipticArc(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
365 double sa
, double ea
);
367 virtual void DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
);
368 virtual void DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
369 wxCoord width
, wxCoord height
,
371 virtual void DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
);
373 virtual void DoCrossHair(wxCoord x
, wxCoord y
);
375 virtual void DoDrawIcon(const wxIcon
& icon
, wxCoord x
, wxCoord y
);
376 virtual void DoDrawBitmap(const wxBitmap
&bmp
, wxCoord x
, wxCoord y
,
377 bool useMask
= false);
379 virtual void DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
);
380 virtual void DoDrawRotatedText(const wxString
& text
, wxCoord x
, wxCoord y
,
383 virtual bool DoBlit(wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
384 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
385 int rop
= wxCOPY
, bool useMask
= false, wxCoord xsrcMask
= -1, wxCoord ysrcMask
= -1);
387 virtual void DoGetSize(int *,int *) const;
388 virtual void DoGetSizeMM(int* width
, int* height
) const;
390 virtual void DoDrawLines(int n
, wxPoint points
[],
391 wxCoord xoffset
, wxCoord yoffset
);
392 virtual void DoDrawPolygon(int n
, wxPoint points
[],
393 wxCoord xoffset
, wxCoord yoffset
,
394 int fillStyle
= wxODDEVEN_RULE
);
395 virtual void DoDrawPolyPolygon(int n
, int count
[], wxPoint points
[],
396 wxCoord xoffset
, wxCoord yoffset
,
399 virtual void DoSetClippingRegionAsRegion(const wxRegion
& region
);
400 virtual void DoSetClippingRegion(wxCoord x
, wxCoord y
,
401 wxCoord width
, wxCoord height
);
403 virtual void DoGetTextExtent(const wxString
& string
,
404 wxCoord
*x
, wxCoord
*y
,
405 wxCoord
*descent
= NULL
,
406 wxCoord
*externalLeading
= NULL
,
407 wxFont
*theFont
= NULL
) const;
409 virtual bool DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const;
413 double m_mm_to_pix_x
, m_mm_to_pix_y
;
415 double m_formerScaleX
, m_formerScaleY
;
417 wxGraphicsContext
* m_graphicContext
;