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 #if wxUSE_PRINTING_ARCHITECTURE
25 class WXDLLIMPEXP_FWD_CORE wxPrinterDC
;
27 class WXDLLIMPEXP_FWD_CORE wxGraphicsContext
;
28 class WXDLLIMPEXP_FWD_CORE wxGraphicsPath
;
29 class WXDLLIMPEXP_FWD_CORE wxGraphicsMatrix
;
30 class WXDLLIMPEXP_FWD_CORE wxGraphicsFigure
;
31 class WXDLLIMPEXP_FWD_CORE wxGraphicsRenderer
;
32 class WXDLLIMPEXP_FWD_CORE wxGraphicsPen
;
33 class WXDLLIMPEXP_FWD_CORE wxGraphicsBrush
;
34 class WXDLLIMPEXP_FWD_CORE wxGraphicsFont
;
35 class WXDLLIMPEXP_FWD_CORE wxGraphicsBitmap
;
38 * notes about the graphics context apis
40 * angles : are measured in radians, 0.0 being in direction of positiv x axis, PI/2 being
41 * in direction of positive y axis.
44 // Base class of all objects used for drawing in the new graphics API, the always point back to their
45 // originating rendering engine, there is no dynamic unloading of a renderer currently allowed,
46 // these references are not counted
49 // The data used by objects like graphics pens etc is ref counted, in order to avoid unnecessary expensive
50 // duplication. Any operation on a shared instance that results in a modified state, uncouples this
51 // instance from the other instances that were shared - using copy on write semantics
54 class WXDLLIMPEXP_FWD_CORE wxGraphicsObjectRefData
;
55 class WXDLLIMPEXP_FWD_CORE wxGraphicsMatrixData
;
56 class WXDLLIMPEXP_FWD_CORE wxGraphicsPathData
;
58 class WXDLLIMPEXP_CORE wxGraphicsObject
: public wxObject
62 wxGraphicsObject( wxGraphicsRenderer
* renderer
) ;
63 virtual ~wxGraphicsObject() ;
67 // returns the renderer that was used to create this instance, or NULL if it has not been initialized yet
68 wxGraphicsRenderer
* GetRenderer() const ;
69 wxGraphicsObjectRefData
* GetGraphicsData() const ;
71 virtual wxObjectRefData
* CreateRefData() const;
72 virtual wxObjectRefData
* CloneRefData(const wxObjectRefData
* data
) const;
74 DECLARE_DYNAMIC_CLASS(wxGraphicsObject
)
77 class WXDLLIMPEXP_CORE wxGraphicsPen
: public wxGraphicsObject
81 virtual ~wxGraphicsPen() {}
83 DECLARE_DYNAMIC_CLASS(wxGraphicsPen
)
86 extern WXDLLIMPEXP_DATA_CORE(wxGraphicsPen
) wxNullGraphicsPen
;
88 class WXDLLIMPEXP_CORE wxGraphicsBrush
: public wxGraphicsObject
92 virtual ~wxGraphicsBrush() {}
94 DECLARE_DYNAMIC_CLASS(wxGraphicsBrush
)
97 extern WXDLLIMPEXP_DATA_CORE(wxGraphicsBrush
) wxNullGraphicsBrush
;
99 class WXDLLIMPEXP_CORE wxGraphicsFont
: public wxGraphicsObject
103 virtual ~wxGraphicsFont() {}
105 DECLARE_DYNAMIC_CLASS(wxGraphicsFont
)
108 extern WXDLLIMPEXP_DATA_CORE(wxGraphicsFont
) wxNullGraphicsFont
;
110 class WXDLLIMPEXP_CORE wxGraphicsBitmap
: public wxGraphicsObject
113 wxGraphicsBitmap() {}
114 virtual ~wxGraphicsBitmap() {}
116 DECLARE_DYNAMIC_CLASS(wxGraphicsBitmap
)
119 extern WXDLLIMPEXP_DATA_CORE(wxGraphicsBitmap
) wxNullGraphicsBitmap
;
121 class WXDLLIMPEXP_CORE wxGraphicsMatrix
: public wxGraphicsObject
124 wxGraphicsMatrix() {}
126 virtual ~wxGraphicsMatrix() {}
128 // concatenates the matrix
129 virtual void Concat( const wxGraphicsMatrix
*t
);
130 void Concat( const wxGraphicsMatrix
&t
) { Concat( &t
); }
132 // sets the matrix to the respective values
133 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
134 wxDouble tx
=0.0, wxDouble ty
=0.0);
136 // gets the component valuess of the matrix
137 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
138 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
140 // makes this the inverse matrix
141 virtual void Invert();
143 // returns true if the elements of the transformation matrix are equal ?
144 virtual bool IsEqual( const wxGraphicsMatrix
* t
) const;
145 bool IsEqual( const wxGraphicsMatrix
& t
) const { return IsEqual( &t
); }
147 // return true if this is the identity matrix
148 virtual bool IsIdentity() const;
154 // add the translation to this matrix
155 virtual void Translate( wxDouble dx
, wxDouble dy
);
157 // add the scale to this matrix
158 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
160 // add the rotation to this matrix (radians)
161 virtual void Rotate( wxDouble angle
);
164 // apply the transforms
167 // applies that matrix to the point
168 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
170 // applies the matrix except for translations
171 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
173 // returns the native representation
174 virtual void * GetNativeMatrix() const;
176 const wxGraphicsMatrixData
* GetMatrixData() const
177 { return (const wxGraphicsMatrixData
*) GetRefData(); }
178 wxGraphicsMatrixData
* GetMatrixData()
179 { return (wxGraphicsMatrixData
*) GetRefData(); }
182 DECLARE_DYNAMIC_CLASS(wxGraphicsMatrix
)
185 extern WXDLLIMPEXP_DATA_CORE(wxGraphicsMatrix
) wxNullGraphicsMatrix
;
187 class WXDLLIMPEXP_CORE wxGraphicsPath
: public wxGraphicsObject
191 virtual ~wxGraphicsPath() {}
194 // These are the path primitives from which everything else can be constructed
197 // begins a new subpath at (x,y)
198 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
199 void MoveToPoint( const wxPoint2DDouble
& p
);
201 // adds a straight line from the current point to (x,y)
202 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
203 void AddLineToPoint( const wxPoint2DDouble
& p
);
205 // adds a cubic Bezier curve from the current point, using two control points and an end point
206 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
) ;
207 void AddCurveToPoint( const wxPoint2DDouble
& c1
, const wxPoint2DDouble
& c2
, const wxPoint2DDouble
& e
);
210 virtual void AddPath( const wxGraphicsPath
& path
);
212 // closes the current sub-path
213 virtual void CloseSubpath() ;
215 // gets the last point of the current path, (0,0) if not yet set
216 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
217 wxPoint2DDouble
GetCurrentPoint() const;
219 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
220 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
221 void AddArc( const wxPoint2DDouble
& c
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
);
224 // These are convenience functions which - if not available natively will be assembled
225 // using the primitives from above
228 // adds a quadratic Bezier curve from the current point, using a control point and an end point
229 virtual void AddQuadCurveToPoint( wxDouble cx
, wxDouble cy
, wxDouble x
, wxDouble y
);
231 // appends a rectangle as a new closed subpath
232 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
234 // appends an ellipsis as a new closed subpath fitting the passed rectangle
235 virtual void AddCircle( wxDouble x
, wxDouble y
, wxDouble r
);
237 // 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)
238 virtual void AddArcToPoint( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, wxDouble r
) ;
240 // appends an ellipse
241 virtual void AddEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
243 // appends a rounded rectangle
244 virtual void AddRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
);
246 // returns the native path
247 virtual void * GetNativePath() const;
249 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
250 virtual void UnGetNativePath(void *p
)const;
252 // transforms each point of this path by the matrix
253 virtual void Transform( const wxGraphicsMatrix
& matrix
);
255 // gets the bounding box enclosing all points (possibly including control points)
256 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
)const;
257 wxRect2DDouble
GetBox()const;
259 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxODDEVEN_RULE
)const;
260 bool Contains( const wxPoint2DDouble
& c
, int fillStyle
= wxODDEVEN_RULE
)const;
262 const wxGraphicsPathData
* GetPathData() const
263 { return (const wxGraphicsPathData
*) GetRefData(); }
264 wxGraphicsPathData
* GetPathData()
265 { return (wxGraphicsPathData
*) GetRefData(); }
268 DECLARE_DYNAMIC_CLASS(wxGraphicsPath
)
271 extern WXDLLIMPEXP_DATA_CORE(wxGraphicsPath
) wxNullGraphicsPath
;
274 class WXDLLIMPEXP_CORE wxGraphicsContext
: public wxGraphicsObject
277 wxGraphicsContext(wxGraphicsRenderer
* renderer
);
279 virtual ~wxGraphicsContext();
281 static wxGraphicsContext
* Create( const wxWindowDC
& dc
) ;
282 static wxGraphicsContext
* Create( const wxMemoryDC
& dc
) ;
283 #if wxUSE_PRINTING_ARCHITECTURE
284 static wxGraphicsContext
* Create( const wxPrinterDC
& dc
) ;
287 static wxGraphicsContext
* CreateFromNative( void * context
) ;
289 static wxGraphicsContext
* CreateFromNativeWindow( void * window
) ;
291 static wxGraphicsContext
* Create( wxWindow
* window
) ;
293 // create a context that can be used for measuring texts only, no drawing allowed
294 static wxGraphicsContext
* Create();
296 // begin a new document (relevant only for printing / pdf etc) if there is a progress dialog, message will be shown
297 virtual bool StartDoc( const wxString
& message
) ;
299 // done with that document (relevant only for printing / pdf etc)
300 virtual void EndDoc();
302 // opens a new page (relevant only for printing / pdf etc) with the given size in points
303 // (if both are null the default page size will be used)
304 virtual void StartPage( wxDouble width
= 0, wxDouble height
= 0 );
306 // ends the current page (relevant only for printing / pdf etc)
307 virtual void EndPage();
309 // make sure that the current content of this context is immediately visible
310 virtual void Flush();
312 wxGraphicsPath
CreatePath() const;
314 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) const;
316 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) const;
318 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
319 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
320 const wxColour
&c1
, const wxColour
&c2
) const;
322 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
323 // with radius r and color cColor
324 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
325 const wxColour
&oColor
, const wxColour
&cColor
) const;
328 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) const;
330 // create a native bitmap representation
331 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) const;
333 // create a native bitmap representation
334 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) const;
336 // create a 'native' matrix corresponding to these values
337 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
338 wxDouble tx
=0.0, wxDouble ty
=0.0) const;
340 // push the current state of the context, ie the transformation matrix on a stack
341 virtual void PushState() = 0;
343 // pops a stored state from the stack
344 virtual void PopState() = 0;
346 // clips drawings to the region intersected with the current clipping region
347 virtual void Clip( const wxRegion
®ion
) = 0;
349 // clips drawings to the rect intersected with the current clipping region
350 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
352 // resets the clipping to original extent
353 virtual void ResetClip() = 0 ;
355 // returns the native context
356 virtual void * GetNativeContext() = 0;
358 // returns the current logical function
359 virtual int GetLogicalFunction() const { return m_logicalFunction
; }
361 // sets the current logical function, returns true if it supported
362 virtual bool SetLogicalFunction(int function
) ;
364 // returns the size of the graphics context in device coordinates
365 virtual void GetSize( wxDouble
* width
, wxDouble
* height
);
367 // returns the resolution of the graphics context in device points per inch
368 virtual void GetDPI( wxDouble
* dpiX
, wxDouble
* dpiY
);
371 // sets the current alpha on this context
372 virtual void SetAlpha( wxDouble alpha
);
374 // returns the alpha on this context
375 virtual wxDouble
GetAlpha() const;
378 // transformation : changes the current transformation matrix CTM of the context
382 virtual void Translate( wxDouble dx
, wxDouble dy
) = 0;
385 virtual void Scale( wxDouble xScale
, wxDouble yScale
) = 0;
388 virtual void Rotate( wxDouble angle
) = 0;
390 // concatenates this transform with the current transform of this context
391 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
) = 0;
393 // sets the transform of this context
394 virtual void SetTransform( const wxGraphicsMatrix
& matrix
) = 0;
396 // gets the matrix of this context
397 virtual wxGraphicsMatrix
GetTransform() const = 0;
403 virtual void SetPen( const wxGraphicsPen
& pen
);
405 void SetPen( const wxPen
& pen
);
407 // sets the brush for filling
408 virtual void SetBrush( const wxGraphicsBrush
& brush
);
410 void SetBrush( const wxBrush
& brush
);
413 virtual void SetFont( const wxGraphicsFont
& font
);
415 void SetFont( const wxFont
& font
, const wxColour
& colour
);
418 // strokes along a path with the current pen
419 virtual void StrokePath( const wxGraphicsPath
& path
) = 0;
421 // fills a path with the current brush
422 virtual void FillPath( const wxGraphicsPath
& path
, int fillStyle
= wxODDEVEN_RULE
) = 0;
424 // draws a path by first filling and then stroking
425 virtual void DrawPath( const wxGraphicsPath
& path
, int fillStyle
= wxODDEVEN_RULE
);
431 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
) = 0;
433 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
);
435 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, const wxGraphicsBrush
& backgroundBrush
) ;
437 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
, wxDouble angle
, const wxGraphicsBrush
& backgroundBrush
);
439 virtual void GetTextExtent( const wxString
&text
, wxDouble
*width
, wxDouble
*height
,
440 wxDouble
*descent
, wxDouble
*externalLeading
) const = 0;
442 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const = 0;
449 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
452 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
454 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
457 // convenience methods
460 // strokes a single line
461 virtual void StrokeLine( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
);
463 // stroke lines connecting each of the points
464 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
466 // stroke disconnected lines from begin to end points
467 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*beginPoints
, const wxPoint2DDouble
*endPoints
);
470 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
= wxODDEVEN_RULE
);
473 virtual void DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
476 virtual void DrawEllipse( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
478 // draws a rounded rectangle
479 virtual void DrawRoundedRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
, wxDouble radius
);
481 // wrappers using wxPoint2DDouble TODO
483 // helper to determine if a 0.5 offset should be applied for the drawing operation
484 virtual bool ShouldOffset() const { return false; }
489 wxGraphicsBrush m_brush
;
490 wxGraphicsFont m_font
;
491 int m_logicalFunction
;
494 DECLARE_NO_COPY_CLASS(wxGraphicsContext
)
495 DECLARE_ABSTRACT_CLASS(wxGraphicsContext
)
501 // A graphics figure allows to cache path, pen etc creations, also will be a basis for layering/grouping elements
504 class WXDLLIMPEXP_CORE wxGraphicsFigure
: public wxGraphicsObject
507 wxGraphicsFigure(wxGraphicsRenderer
* renderer
) ;
509 virtual ~wxGraphicsFigure() ;
511 void SetPath( wxGraphicsMatrix
* matrix
);
513 void SetMatrix( wxGraphicsPath
* path
);
515 // draws this object on the context
516 virtual void Draw( wxGraphicsContext
* cg
);
518 // returns the path of this object
519 wxGraphicsPath
* GetPath() { return m_path
; }
521 // returns the transformation matrix of this object, may be null if there is no transformation necessary
522 wxGraphicsMatrix
* GetMatrix() { return m_matrix
; }
525 wxGraphicsMatrix
* m_matrix
;
526 wxGraphicsPath
* m_path
;
528 DECLARE_DYNAMIC_CLASS(wxGraphicsFigure
)
534 // The graphics renderer is the instance corresponding to the rendering engine used, eg there is ONE core graphics renderer
535 // instance on OSX. This instance is pointed back to by all objects created by it. Therefore you can create eg additional
536 // paths at any point from a given matrix etc.
539 class WXDLLIMPEXP_CORE wxGraphicsRenderer
: public wxObject
542 wxGraphicsRenderer() {}
544 virtual ~wxGraphicsRenderer() {}
546 static wxGraphicsRenderer
* GetDefaultRenderer();
550 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
) = 0 ;
551 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
) = 0 ;
552 #if wxUSE_PRINTING_ARCHITECTURE
553 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
) = 0 ;
556 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
) = 0;
558 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
) = 0;
560 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
) = 0;
562 // create a context that can be used for measuring texts only, no drawing allowed
563 virtual wxGraphicsContext
* CreateMeasuringContext() = 0;
567 virtual wxGraphicsPath
CreatePath() = 0;
571 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
572 wxDouble tx
=0.0, wxDouble ty
=0.0) = 0;
576 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) = 0 ;
578 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) = 0 ;
580 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
581 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
582 const wxColour
&c1
, const wxColour
&c2
) = 0;
584 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
585 // with radius r and color cColor
586 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
587 const wxColour
&oColor
, const wxColour
&cColor
) = 0;
590 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) = 0;
593 // create a native bitmap representation
594 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
) = 0;
596 // create a subimage from a native image representation
597 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) = 0;
602 DECLARE_NO_COPY_CLASS(wxGraphicsRenderer
)
603 DECLARE_ABSTRACT_CLASS(wxGraphicsRenderer
)
608 #endif // _WX_GRAPHICS_H_