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_
17 #if wxUSE_GRAPHICS_CONTEXT
19 #include "wx/geometry.h"
20 #include "wx/dynarray.h"
22 class WXDLLIMPEXP_FWD_CORE wxWindowDC
;
23 class WXDLLIMPEXP_FWD_CORE wxMemoryDC
;
24 class WXDLLIMPEXP_FWD_CORE wxGraphicsContext
;
25 class WXDLLIMPEXP_FWD_CORE wxGraphicsPath
;
26 class WXDLLIMPEXP_FWD_CORE wxGraphicsMatrix
;
27 class WXDLLIMPEXP_FWD_CORE wxGraphicsFigure
;
28 class WXDLLIMPEXP_FWD_CORE wxGraphicsRenderer
;
29 class WXDLLIMPEXP_FWD_CORE wxGraphicsPen
;
30 class WXDLLIMPEXP_FWD_CORE wxGraphicsBrush
;
31 class WXDLLIMPEXP_FWD_CORE wxGraphicsFont
;
32 class WXDLLIMPEXP_FWD_CORE wxGraphicsBitmap
;
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 // Base class of all objects used for drawing in the new graphics API, the always point back to their
42 // originating rendering engine, there is no dynamic unloading of a renderer currently allowed,
43 // these references are not counted
46 // The data used by objects like graphics pens etc is ref counted, in order to avoid unnecessary expensive
47 // duplication. Any operation on a shared instance that results in a modified state, uncouples this
48 // instance from the other instances that were shared - using copy on write semantics
51 class WXDLLIMPEXP_FWD_CORE wxGraphicsObjectRefData
;
52 class WXDLLIMPEXP_FWD_CORE wxGraphicsMatrixData
;
53 class WXDLLIMPEXP_FWD_CORE wxGraphicsPathData
;
55 class WXDLLIMPEXP_CORE wxGraphicsObject
: public wxObject
59 wxGraphicsObject( wxGraphicsRenderer
* renderer
) ;
60 virtual ~wxGraphicsObject() ;
64 // returns the renderer that was used to create this instance, or NULL if it has not been initialized yet
65 wxGraphicsRenderer
* GetRenderer() const ;
66 wxGraphicsObjectRefData
* GetGraphicsData() const ;
68 virtual wxObjectRefData
* CreateRefData() const;
69 virtual wxObjectRefData
* CloneRefData(const wxObjectRefData
* data
) const;
71 DECLARE_DYNAMIC_CLASS(wxGraphicsObject
)
74 class WXDLLIMPEXP_CORE wxGraphicsPen
: public wxGraphicsObject
78 virtual ~wxGraphicsPen() {}
80 DECLARE_DYNAMIC_CLASS(wxGraphicsPen
)
83 extern WXDLLEXPORT_DATA(wxGraphicsPen
) wxNullGraphicsPen
;
85 class WXDLLIMPEXP_CORE wxGraphicsBrush
: public wxGraphicsObject
89 virtual ~wxGraphicsBrush() {}
91 DECLARE_DYNAMIC_CLASS(wxGraphicsBrush
)
94 extern WXDLLEXPORT_DATA(wxGraphicsBrush
) wxNullGraphicsBrush
;
96 class WXDLLIMPEXP_CORE wxGraphicsFont
: public wxGraphicsObject
100 virtual ~wxGraphicsFont() {}
102 DECLARE_DYNAMIC_CLASS(wxGraphicsFont
)
105 extern WXDLLEXPORT_DATA(wxGraphicsFont
) wxNullGraphicsFont
;
107 class WXDLLIMPEXP_CORE wxGraphicsBitmap
: public wxGraphicsObject
110 wxGraphicsBitmap() {}
111 virtual ~wxGraphicsBitmap() {}
113 DECLARE_DYNAMIC_CLASS(wxGraphicsBitmap
)
116 extern WXDLLEXPORT_DATA(wxGraphicsBitmap
) wxNullGraphicsBitmap
;
118 class WXDLLIMPEXP_CORE wxGraphicsMatrix
: public wxGraphicsObject
121 wxGraphicsMatrix() {}
123 virtual ~wxGraphicsMatrix() {}
125 // concatenates the matrix
126 virtual void Concat( const wxGraphicsMatrix
*t
);
127 void Concat( const wxGraphicsMatrix
&t
) { Concat( &t
); }
129 // sets the matrix to the respective values
130 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
131 wxDouble tx
=0.0, wxDouble ty
=0.0);
133 // gets the component valuess of the matrix
134 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
135 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
137 // makes this the inverse matrix
138 virtual void Invert();
140 // returns true if the elements of the transformation matrix are equal ?
141 virtual bool IsEqual( const wxGraphicsMatrix
* t
) const;
142 bool IsEqual( const wxGraphicsMatrix
& t
) const { return IsEqual( &t
); }
144 // return true if this is the identity matrix
145 virtual bool IsIdentity() const;
151 // add the translation to this matrix
152 virtual void Translate( wxDouble dx
, wxDouble dy
);
154 // add the scale to this matrix
155 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
157 // add the rotation to this matrix (radians)
158 virtual void Rotate( wxDouble angle
);
161 // apply the transforms
164 // applies that matrix to the point
165 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
167 // applies the matrix except for translations
168 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
170 // returns the native representation
171 virtual void * GetNativeMatrix() const;
173 const wxGraphicsMatrixData
* GetMatrixData() const
174 { return (const wxGraphicsMatrixData
*) GetRefData(); }
175 wxGraphicsMatrixData
* GetMatrixData()
176 { return (wxGraphicsMatrixData
*) GetRefData(); }
179 DECLARE_DYNAMIC_CLASS(wxGraphicsMatrix
)
182 extern WXDLLEXPORT_DATA(wxGraphicsMatrix
) wxNullGraphicsMatrix
;
184 class WXDLLIMPEXP_CORE wxGraphicsPath
: public wxGraphicsObject
188 virtual ~wxGraphicsPath() {}
191 // These are the path primitives from which everything else can be constructed
194 // begins a new subpath at (x,y)
195 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
196 void MoveToPoint( const wxPoint2DDouble
& p
);
198 // adds a straight line from the current point to (x,y)
199 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
200 void AddLineToPoint( const wxPoint2DDouble
& p
);
202 // adds a cubic Bezier curve from the current point, using two control points and an end point
203 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
) ;
204 void AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
);
207 virtual void AddPath( const wxGraphicsPath
& path
);
209 // closes the current sub-path
210 virtual void CloseSubpath() ;
212 // gets the last point of the current path, (0,0) if not yet set
213 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
214 wxPoint2DDouble
GetCurrentPoint() const;
216 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
217 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
218 void AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
221 // These are convenience functions which - if not available natively will be assembled
222 // using the primitives from above
225 // adds a quadratic Bezier curve from the current point, using a control point and an end point
226 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
228 // appends a rectangle as a new closed subpath
229 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
231 // appends an ellipsis as a new closed subpath fitting the passed rectangle
232 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
234 // appends 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)
235 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
) ;
237 // appends an ellipse
238 virtual void AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
240 // appends a rounded rectangle
241 virtual void AddRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
);
243 // returns the native path
244 virtual void * GetNativePath() const;
246 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
247 virtual void UnGetNativePath(void *p
)const;
249 // transforms each point of this path by the matrix
250 virtual void Transform( const wxGraphicsMatrix
& matrix
);
252 // gets the bounding box enclosing all points (possibly including control points)
253 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
)const;
254 wxRect2DDouble
GetBox()const;
256 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxODDEVEN_RULE
)const;
257 bool Contains( const wxPoint2DDouble
& c
, int fillStyle
= wxODDEVEN_RULE
)const;
259 const wxGraphicsPathData
* GetPathData() const
260 { return (const wxGraphicsPathData
*) GetRefData(); }
261 wxGraphicsPathData
* GetPathData()
262 { return (wxGraphicsPathData
*) GetRefData(); }
265 DECLARE_DYNAMIC_CLASS(wxGraphicsPath
)
268 extern WXDLLEXPORT_DATA(wxGraphicsPath
) wxNullGraphicsPath
;
271 class WXDLLIMPEXP_CORE wxGraphicsContext
: public wxGraphicsObject
274 wxGraphicsContext(wxGraphicsRenderer
* renderer
);
276 virtual ~wxGraphicsContext();
278 static wxGraphicsContext
* Create( const wxWindowDC
& dc
) ;
279 static wxGraphicsContext
* Create( const wxMemoryDC
& dc
) ;
281 static wxGraphicsContext
* CreateFromNative( void * context
) ;
283 static wxGraphicsContext
* CreateFromNativeWindow( void * window
) ;
285 static wxGraphicsContext
* Create( wxWindow
* window
) ;
287 // create a context that can be used for measuring texts only, no drawing allowed
288 static wxGraphicsContext
* Create();
290 // begin a new document (relevant only for printing / pdf etc) if there is a progress dialog, message will be shown
291 virtual bool StartDoc( const wxString
& message
) ;
293 // done with that document (relevant only for printing / pdf etc)
294 virtual void EndDoc();
296 // opens a new page (relevant only for printing / pdf etc) with the given size in points
297 // (if both are null the default page size will be used)
298 virtual void StartPage( wxDouble width
= 0, wxDouble height
= 0 );
300 // ends the current page (relevant only for printing / pdf etc)
301 virtual void EndPage();
303 // make sure that the current content of this context is immediately visible
304 virtual void Flush();
306 wxGraphicsPath
CreatePath() const;
308 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) const;
310 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) const;
312 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
313 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
314 const wxColour
&c1
, const wxColour
&c2
) const;
316 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
317 // with radius r and color cColor
318 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
319 const wxColour
&oColor
, const wxColour
&cColor
) const;
322 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) const;
324 // create a native bitmap representation
325 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) const;
327 // create a native bitmap representation
328 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) const;
330 // create a 'native' matrix corresponding to these values
331 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
332 wxDouble tx
=0.0, wxDouble ty
=0.0) const;
334 // push the current state of the context, ie the transformation matrix on a stack
335 virtual void PushState() = 0;
337 // pops a stored state from the stack
338 virtual void PopState() = 0;
340 // clips drawings to the region intersected with the current clipping region
341 virtual void Clip( const wxRegion
®ion
) = 0;
343 // clips drawings to the rect intersected with the current clipping region
344 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
346 // resets the clipping to original extent
347 virtual void ResetClip() = 0 ;
349 // returns the native context
350 virtual void * GetNativeContext() = 0;
352 // returns the current logical function
353 virtual int GetLogicalFunction() const { return m_logicalFunction
; }
355 // sets the current logical function, returns true if it supported
356 virtual bool SetLogicalFunction(int function
) ;
358 // returns the size of the graphics context in device coordinates
359 virtual void GetSize( wxDouble
* width
, wxDouble
* height
);
361 // returns the resolution of the graphics context in device points per inch
362 virtual void GetDPI( wxDouble
* dpiX
, wxDouble
* dpiY
);
365 // sets the current alpha on this context
366 virtual void SetAlpha( wxDouble alpha
);
368 // returns the alpha on this context
369 virtual wxDouble
GetAlpha() const;
372 // transformation : changes the current transformation matrix CTM of the context
376 virtual void Translate( wxDouble dx
, wxDouble dy
) = 0;
379 virtual void Scale( wxDouble xScale
, wxDouble yScale
) = 0;
382 virtual void Rotate( wxDouble angle
) = 0;
384 // concatenates this transform with the current transform of this context
385 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
) = 0;
387 // sets the transform of this context
388 virtual void SetTransform( const wxGraphicsMatrix
& matrix
) = 0;
390 // gets the matrix of this context
391 virtual wxGraphicsMatrix
GetTransform() const = 0;
397 virtual void SetPen( const wxGraphicsPen
& pen
);
399 void SetPen( const wxPen
& pen
);
401 // sets the brush for filling
402 virtual void SetBrush( const wxGraphicsBrush
& brush
);
404 void SetBrush( const wxBrush
& brush
);
407 virtual void SetFont( const wxGraphicsFont
& font
);
409 void SetFont( const wxFont
& font
, const wxColour
& colour
);
412 // strokes along a path with the current pen
413 virtual void StrokePath( const wxGraphicsPath
& path
) = 0;
415 // fills a path with the current brush
416 virtual void FillPath( const wxGraphicsPath
& path
, int fillStyle
= wxODDEVEN_RULE
) = 0;
418 // draws a path by first filling and then stroking
419 virtual void DrawPath( const wxGraphicsPath
& path
, int fillStyle
= wxODDEVEN_RULE
);
425 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
) = 0;
427 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
429 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, const wxGraphicsBrush
& backgroundBrush
) ;
431 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
, const wxGraphicsBrush
& backgroundBrush
);
433 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
434 wxDouble
*descent
, wxDouble
*externalLeading
) const = 0;
436 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const = 0;
442 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
444 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
446 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
449 // convenience methods
452 // strokes a single line
453 virtual void StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
);
455 // stroke lines connecting each of the points
456 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
458 // stroke disconnected lines from begin to end points
459 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
);
462 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
= wxODDEVEN_RULE
);
465 virtual void DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
468 virtual void DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
470 // draws a rounded rectangle
471 virtual void DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
);
473 // wrappers using wxPoint2DDouble TODO
475 // helper to determine if a 0.5 offset should be applied for the drawing operation
476 virtual bool ShouldOffset() const { return false; }
481 wxGraphicsBrush m_brush
;
482 wxGraphicsFont m_font
;
483 int m_logicalFunction
;
486 DECLARE_NO_COPY_CLASS(wxGraphicsContext
)
487 DECLARE_ABSTRACT_CLASS(wxGraphicsContext
)
493 // A graphics figure allows to cache path, pen etc creations, also will be a basis for layering/grouping elements
496 class WXDLLIMPEXP_CORE wxGraphicsFigure
: public wxGraphicsObject
499 wxGraphicsFigure(wxGraphicsRenderer
* renderer
) ;
501 virtual ~wxGraphicsFigure() ;
503 void SetPath( wxGraphicsMatrix
* matrix
);
505 void SetMatrix( wxGraphicsPath
* path
);
507 // draws this object on the context
508 virtual void Draw( wxGraphicsContext
* cg
);
510 // returns the path of this object
511 wxGraphicsPath
* GetPath() { return m_path
; }
513 // returns the transformation matrix of this object, may be null if there is no transformation necessary
514 wxGraphicsMatrix
* GetMatrix() { return m_matrix
; }
517 wxGraphicsMatrix
* m_matrix
;
518 wxGraphicsPath
* m_path
;
520 DECLARE_DYNAMIC_CLASS(wxGraphicsFigure
)
526 // The graphics renderer is the instance corresponding to the rendering engine used, eg there is ONE core graphics renderer
527 // instance on OSX. This instance is pointed back to by all objects created by it. Therefore you can create eg additional
528 // paths at any point from a given matrix etc.
531 class WXDLLIMPEXP_CORE wxGraphicsRenderer
: public wxObject
534 wxGraphicsRenderer() {}
536 virtual ~wxGraphicsRenderer() {}
538 static wxGraphicsRenderer
* GetDefaultRenderer();
542 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
) = 0 ;
543 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
) = 0 ;
545 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
) = 0;
547 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
) = 0;
549 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
) = 0;
551 // create a context that can be used for measuring texts only, no drawing allowed
552 virtual wxGraphicsContext
* CreateMeasuringContext() = 0;
556 virtual wxGraphicsPath
CreatePath() = 0;
560 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
561 wxDouble tx
=0.0, wxDouble ty
=0.0) = 0;
565 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) = 0 ;
567 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) = 0 ;
569 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
570 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
571 const wxColour
&c1
, const wxColour
&c2
) = 0;
573 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
574 // with radius r and color cColor
575 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
576 const wxColour
&oColor
, const wxColour
&cColor
) = 0;
579 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) = 0;
581 // create a native bitmap representation
582 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) = 0;
584 // create a subimage from a native image representation
585 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
587 DECLARE_NO_COPY_CLASS(wxGraphicsRenderer
)
588 DECLARE_ABSTRACT_CLASS(wxGraphicsRenderer
)
593 #endif // _WX_GRAPHICS_H_