]> git.saurik.com Git - wxWidgets.git/blame - include/wx/graphics.h
Add tar streams.
[wxWidgets.git] / include / wx / graphics.h
CommitLineData
50581042
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/graphics.h
3// Purpose: graphics context header
4// Author: Stefan Csomor
5// Modified by:
6// Created:
7// Copyright: (c) Stefan Csomor
8// RCS-ID: $Id$
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_GRAPHICS_H_
13#define _WX_GRAPHICS_H_
14
8acd14d1
SC
15#if wxUSE_GRAPHICS_CONTEXT
16
50581042 17#include "wx/geometry.h"
50581042
SC
18#include "wx/dynarray.h"
19
9e605538
SC
20class WXDLLIMPEXP_CORE wxWindowDC;
21class WXDLLIMPEXP_CORE wxGraphicsContext;
22class WXDLLIMPEXP_CORE wxGraphicsPath;
23class WXDLLIMPEXP_CORE wxGraphicsMatrix;
24class WXDLLIMPEXP_CORE wxGraphicsFigure;
25class WXDLLIMPEXP_CORE wxGraphicsRenderer;
26class WXDLLIMPEXP_CORE wxGraphicsPen;
27class WXDLLIMPEXP_CORE wxGraphicsBrush;
28class WXDLLIMPEXP_CORE wxGraphicsFont;
774f4d12 29
50581042
SC
30/*
31 * notes about the graphics context apis
32 *
33 * angles : are measured in radians, 0.0 being in direction of positiv x axis, PI/2 being
34 * in direction of positive y axis.
35 */
36
9e605538
SC
37// Base class of all objects used for drawing in the new graphics API, the always point back to their
38// originating rendering engine, there is no dynamic unloading of a renderer currently allowed,
39// these references are not counted
40
41class WXDLLIMPEXP_CORE wxGraphicsObject : public wxObject
42{
43public :
0d3675a6
RD
44 wxGraphicsObject( wxGraphicsRenderer* renderer = NULL ) : m_renderer(renderer) {}
45
46 wxGraphicsObject( const wxGraphicsObject& obj ) : m_renderer(obj.GetRenderer()) {}
47
48 virtual ~wxGraphicsObject() {}
49
50 wxGraphicsRenderer* GetRenderer() const { return m_renderer ; }
9e605538 51protected :
0d3675a6
RD
52 wxGraphicsRenderer* m_renderer;
53 DECLARE_DYNAMIC_CLASS(wxGraphicsObject);
9e605538
SC
54} ;
55
56class WXDLLIMPEXP_CORE wxGraphicsPen : public wxGraphicsObject
57{
58public :
59 wxGraphicsPen(wxGraphicsRenderer* renderer) : wxGraphicsObject(renderer) {}
60 virtual ~wxGraphicsPen() {}
61 virtual void Apply( wxGraphicsContext* context) = 0;
62 virtual wxDouble GetWidth() = 0;
63private :
64 DECLARE_NO_COPY_CLASS(wxGraphicsPen)
65 DECLARE_ABSTRACT_CLASS(wxGraphicsPen)
66} ;
67
68class WXDLLIMPEXP_CORE wxGraphicsBrush : public wxGraphicsObject
69{
70public :
71 wxGraphicsBrush(wxGraphicsRenderer* renderer) : wxGraphicsObject(renderer) {}
72 virtual ~wxGraphicsBrush() {}
73 virtual void Apply( wxGraphicsContext* context) = 0;
74private :
75 DECLARE_NO_COPY_CLASS(wxGraphicsBrush)
76 DECLARE_ABSTRACT_CLASS(wxGraphicsBrush)
77} ;
78
79class WXDLLIMPEXP_CORE wxGraphicsFont : public wxGraphicsObject
50581042
SC
80{
81public :
9e605538
SC
82 wxGraphicsFont(wxGraphicsRenderer* renderer) : wxGraphicsObject(renderer) {}
83 virtual ~wxGraphicsFont() {}
84 virtual void Apply( wxGraphicsContext* context) = 0;
85private :
86 DECLARE_NO_COPY_CLASS(wxGraphicsFont)
87 DECLARE_ABSTRACT_CLASS(wxGraphicsFont)
88} ;
89
90class WXDLLIMPEXP_CORE wxGraphicsPath : public wxGraphicsObject
91{
92public :
93 wxGraphicsPath(wxGraphicsRenderer* renderer) : wxGraphicsObject(renderer) {}
50581042
SC
94 virtual ~wxGraphicsPath() {}
95
9e605538
SC
96 virtual wxGraphicsPath *Clone() const = 0;
97
50581042
SC
98 //
99 // These are the path primitives from which everything else can be constructed
100 //
101
102 // begins a new subpath at (x,y)
103 virtual void MoveToPoint( wxDouble x, wxDouble y ) = 0;
9e605538 104 void MoveToPoint( const wxPoint2DDouble& p);
50581042
SC
105
106 // adds a straight line from the current point to (x,y)
107 virtual void AddLineToPoint( wxDouble x, wxDouble y ) = 0;
9e605538 108 void AddLineToPoint( const wxPoint2DDouble& p);
50581042
SC
109
110 // adds a cubic Bezier curve from the current point, using two control points and an end point
111 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y ) = 0;
9e605538 112 void AddCurveToPoint( const wxPoint2DDouble& c1, const wxPoint2DDouble& c2, const wxPoint2DDouble& e);
0d3675a6
RD
113
114 // adds another path
115 virtual void AddPath( const wxGraphicsPath* path ) =0;
50581042
SC
116
117 // closes the current sub-path
118 virtual void CloseSubpath() = 0;
119
120 // gets the last point of the current path, (0,0) if not yet set
121 virtual void GetCurrentPoint( wxDouble& x, wxDouble&y) = 0;
9e605538 122 wxPoint2DDouble GetCurrentPoint();
50581042
SC
123
124 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
125 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) = 0;
0d3675a6 126 void AddArc( const wxPoint2DDouble& c, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise);
50581042
SC
127
128 //
129 // These are convenience functions which - if not available natively will be assembled
130 // using the primitives from above
131 //
132
133 // adds a quadratic Bezier curve from the current point, using a control point and an end point
134 virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y );
135
136 // appends a rectangle as a new closed subpath
137 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
138
139 // appends an ellipsis as a new closed subpath fitting the passed rectangle
140 virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r );
141
59720690 142 // 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)
50581042 143 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
0d3675a6 144
59720690
SC
145 // appends an ellipse
146 virtual void AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
147
148 // appends a rounded rectangle
149 virtual void AddRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius);
150
0d3675a6
RD
151 // returns the native path
152 virtual void * GetNativePath() const = 0;
153
154 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
155 virtual void UnGetNativePath(void *p) = 0;
156
157 // transforms each point of this path by the matrix
158 virtual void Transform( wxGraphicsMatrix* matrix ) =0;
159
160 // gets the bounding box enclosing all points (possibly including control points)
161 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) =0;
162 wxRect2DDouble GetBox();
163
164 virtual bool Contains( wxDouble x, wxDouble y, int fillStyle = wxWINDING_RULE) =0;
165 bool Contains( const wxPoint2DDouble& c, int fillStyle = wxWINDING_RULE);
166
50581042 167 DECLARE_NO_COPY_CLASS(wxGraphicsPath)
8ddf8e82 168 DECLARE_ABSTRACT_CLASS(wxGraphicsPath)
50581042
SC
169};
170
9e605538 171class WXDLLIMPEXP_CORE wxGraphicsMatrix : public wxGraphicsObject
774f4d12
SC
172{
173public :
0d3675a6
RD
174 wxGraphicsMatrix(wxGraphicsRenderer* renderer) : wxGraphicsObject(renderer) {}
175
176 virtual ~wxGraphicsMatrix() {}
177
9e605538
SC
178 virtual wxGraphicsMatrix *Clone() const = 0;
179
0d3675a6
RD
180 // concatenates the matrix
181 virtual void Concat( const wxGraphicsMatrix *t ) = 0;
182 void Concat( const wxGraphicsMatrix &t ) { Concat( &t ); }
183
184 // copies the passed in matrix
185 virtual void Copy( const wxGraphicsMatrix *t ) = 0;
186 void Copy( const wxGraphicsMatrix &t ) { Copy( &t ); }
187
188 // sets the matrix to the respective values
189 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
190 wxDouble tx=0.0, wxDouble ty=0.0) = 0;
191
192 // makes this the inverse matrix
193 virtual void Invert() = 0;
194
195 // returns true if the elements of the transformation matrix are equal ?
9e605538 196 virtual bool IsEqual( const wxGraphicsMatrix* t) const = 0;
0d3675a6
RD
197 bool IsEqual( const wxGraphicsMatrix& t) const { return IsEqual( &t ); }
198
199 // return true if this is the identity matrix
200 virtual bool IsIdentity() = 0;
201
774f4d12
SC
202 //
203 // transformation
204 //
205
9e605538 206 // add the translation to this matrix
774f4d12
SC
207 virtual void Translate( wxDouble dx , wxDouble dy ) = 0;
208
9e605538 209 // add the scale to this matrix
774f4d12
SC
210 virtual void Scale( wxDouble xScale , wxDouble yScale ) = 0;
211
9e605538 212 // add the rotation to this matrix (radians)
0d3675a6
RD
213 virtual void Rotate( wxDouble angle ) = 0;
214
9e605538
SC
215 //
216 // apply the transforms
217 //
0d3675a6
RD
218
219 // applies that matrix to the point
220 virtual void TransformPoint( wxDouble *x, wxDouble *y ) = 0;
221
222 // applies the matrix except for translations
223 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) =0;
224
225 // returns the native representation
226 virtual void * GetNativeMatrix() const = 0;
227
9e605538
SC
228 DECLARE_NO_COPY_CLASS(wxGraphicsMatrix)
229 DECLARE_ABSTRACT_CLASS(wxGraphicsMatrix)
774f4d12 230} ;
774f4d12 231
9e605538 232class WXDLLIMPEXP_CORE wxGraphicsContext : public wxGraphicsObject
50581042
SC
233{
234public:
9e605538 235 wxGraphicsContext(wxGraphicsRenderer* renderer);
774f4d12 236
9e605538 237 virtual ~wxGraphicsContext();
50581042
SC
238
239 static wxGraphicsContext* Create( const wxWindowDC& dc) ;
0d3675a6 240
bf2185eb 241 static wxGraphicsContext* CreateFromNative( void * context ) ;
774f4d12 242
bf2185eb 243 static wxGraphicsContext* CreateFromNativeWindow( void * window ) ;
9a02779a 244
774f4d12 245 static wxGraphicsContext* Create( wxWindow* window ) ;
50581042 246
9e605538 247 wxGraphicsPath * CreatePath();
0d3675a6 248
9e605538
SC
249 virtual wxGraphicsPen* CreatePen(const wxPen& pen);
250
251 virtual wxGraphicsBrush* CreateBrush(const wxBrush& brush );
252
253 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
254 virtual wxGraphicsBrush* CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
255 const wxColour&c1, const wxColour&c2);
256
257 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
258 // with radius r and color cColor
259 virtual wxGraphicsBrush* CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
260 const wxColour &oColor, const wxColour &cColor);
261
262 // sets the font
263 virtual wxGraphicsFont* CreateFont( const wxFont &font , const wxColour &col = *wxBLACK );
264
0d3675a6
RD
265 // create a 'native' matrix corresponding to these values
266 virtual wxGraphicsMatrix* CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
267 wxDouble tx=0.0, wxDouble ty=0.0);
268
50581042
SC
269 // push the current state of the context, ie the transformation matrix on a stack
270 virtual void PushState() = 0;
271
272 // pops a stored state from the stack
273 virtual void PopState() = 0;
274
774f4d12 275 // clips drawings to the region, combined to current clipping region
50581042
SC
276 virtual void Clip( const wxRegion &region ) = 0;
277
774f4d12
SC
278 // clips drawings to the rect
279 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
0d3675a6
RD
280
281 // resets the clipping to original extent
282 virtual void ResetClip() = 0 ;
774f4d12 283
0d3675a6
RD
284 // returns the native context
285 virtual void * GetNativeContext() = 0;
774f4d12 286
50581042 287 //
774f4d12 288 // transformation : changes the current transformation matrix CTM of the context
50581042
SC
289 //
290
291 // translate
292 virtual void Translate( wxDouble dx , wxDouble dy ) = 0;
293
294 // scale
295 virtual void Scale( wxDouble xScale , wxDouble yScale ) = 0;
296
297 // rotate (radians)
298 virtual void Rotate( wxDouble angle ) = 0;
0d3675a6
RD
299
300 // concatenates this transform with the current transform of this context
301 virtual void ConcatTransform( const wxGraphicsMatrix* matrix ) = 0;
9e605538 302
0d3675a6
RD
303 // sets the transform of this context
304 virtual void SetTransform( const wxGraphicsMatrix* matrix ) = 0;
50581042 305
0d3675a6
RD
306 // gets the matrix of this context
307 virtual void GetTransform( wxGraphicsMatrix* matrix ) = 0;
50581042
SC
308 //
309 // setting the paint
310 //
311
9e605538
SC
312 // sets the pen
313 virtual void SetPen( wxGraphicsPen* pen , bool release = true );
314
315 void SetPen( const wxPen& pen );
50581042
SC
316
317 // sets the brush for filling
9e605538
SC
318 virtual void SetBrush( wxGraphicsBrush* brush , bool release = true );
319
320 void SetBrush( const wxBrush& brush );
50581042
SC
321
322 // sets the font
9e605538
SC
323 virtual void SetFont( wxGraphicsFont* font, bool release = true );
324
325 void SetFont( const wxFont& font, const wxColour& colour );
0d3675a6 326
50581042 327
50581042
SC
328 // strokes along a path with the current pen
329 virtual void StrokePath( const wxGraphicsPath *path ) = 0;
330
331 // fills a path with the current brush
332 virtual void FillPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE ) = 0;
333
334 // draws a path by first filling and then stroking
335 virtual void DrawPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE );
0d3675a6 336
50581042
SC
337 //
338 // text
339 //
340
341 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y ) = 0;
342
343 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle );
344
345 virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
346 wxDouble *descent, wxDouble *externalLeading ) const = 0;
347
348 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const = 0;
349
350 //
351 // image support
352 //
353
354 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
355
356 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
357
358 //
359 // convenience methods
360 //
361
362 // strokes a single line
363 virtual void StrokeLine( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2);
364
365 // stroke lines connecting each of the points
366 virtual void StrokeLines( size_t n, const wxPoint2DDouble *points);
367
368 // stroke disconnected lines from begin to end points
369 virtual void StrokeLines( size_t n, const wxPoint2DDouble *beginPoints, const wxPoint2DDouble *endPoints);
370
371 // draws a polygon
372 virtual void DrawLines( size_t n, const wxPoint2DDouble *points, int fillStyle = wxWINDING_RULE );
373
374 // draws a polygon
375 virtual void DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
376
377 // draws an ellipse
378 virtual void DrawEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
379
380 // draws a rounded rectangle
381 virtual void DrawRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius);
382
9e605538 383 // wrappers using wxPoint2DDouble TODO
50581042 384
de3cb39f
RD
385 // helper to determine if a 0.5 offset should be applied for the drawing operation
386 virtual bool ShouldOffset() const { return false; }
9e605538 387
9da34e21
JS
388protected :
389
9e605538
SC
390 wxGraphicsPen* m_pen;
391 bool m_releasePen;
392 wxGraphicsBrush* m_brush;
393 bool m_releaseBrush;
394 wxGraphicsFont* m_font;
395 bool m_releaseFont;
396
397private :
50581042 398 DECLARE_NO_COPY_CLASS(wxGraphicsContext)
8ddf8e82 399 DECLARE_ABSTRACT_CLASS(wxGraphicsContext)
50581042
SC
400};
401
9e605538
SC
402#if 0
403
404//
405// A graphics figure allows to cache path, pen etc creations, also will be a basis for layering/grouping elements
406//
407
408class WXDLLIMPEXP_CORE wxGraphicsFigure : public wxGraphicsObject
409{
410public :
0d3675a6
RD
411 wxGraphicsFigure(wxGraphicsRenderer* renderer) ;
412
413 virtual ~wxGraphicsFigure() ;
414
415 void SetPath( wxGraphicsMatrix* matrix );
416
417 void SetMatrix( wxGraphicsPath* path);
418
419 // draws this object on the context
420 virtual void Draw( wxGraphicsContext* cg );
421
422 // returns the path of this object
423 wxGraphicsPath* GetPath() { return m_path; }
424
425 // returns the transformation matrix of this object, may be null if there is no transformation necessary
426 wxGraphicsMatrix* GetMatrix() { return m_matrix; }
427
9e605538 428private :
0d3675a6
RD
429 wxGraphicsMatrix* m_matrix;
430 wxGraphicsPath* m_path;
9e605538
SC
431
432 DECLARE_DYNAMIC_CLASS(wxGraphicsFigure)
433} ;
434
435#endif
436
437//
438// The graphics renderer is the instance corresponding to the rendering engine used, eg there is ONE core graphics renderer
439// instance on OSX. This instance is pointed back to by all objects created by it. Therefore you can create eg additional
440// paths at any point from a given matrix etc.
441//
442
443class WXDLLIMPEXP_CORE wxGraphicsRenderer : public wxObject
444{
445public :
446 wxGraphicsRenderer() {}
447
448 virtual ~wxGraphicsRenderer() {}
449
0d3675a6 450 static wxGraphicsRenderer* GetDefaultRenderer();
9e605538 451
0d3675a6 452 // Context
9e605538 453
0d3675a6
RD
454 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc) = 0 ;
455
456 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context ) = 0;
9e605538 457
0d3675a6 458 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window ) = 0;
9e605538 459
0d3675a6 460 virtual wxGraphicsContext * CreateContext( wxWindow* window ) = 0;
9e605538 461
0d3675a6
RD
462 // Path
463
9e605538
SC
464 virtual wxGraphicsPath * CreatePath() = 0;
465
0d3675a6
RD
466 // Matrix
467
468 virtual wxGraphicsMatrix * CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
469 wxDouble tx=0.0, wxDouble ty=0.0) = 0;
9e605538
SC
470
471 // Paints
472
473 virtual wxGraphicsPen* CreatePen(const wxPen& pen) = 0 ;
474
475 virtual wxGraphicsBrush* CreateBrush(const wxBrush& brush ) = 0 ;
476
477 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
478 virtual wxGraphicsBrush* CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
479 const wxColour&c1, const wxColour&c2) = 0;
480
481 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
482 // with radius r and color cColor
483 virtual wxGraphicsBrush* CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
484 const wxColour &oColor, const wxColour &cColor) = 0;
485
486 // sets the font
487 virtual wxGraphicsFont* CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) = 0;
488
489private :
0d3675a6 490 DECLARE_NO_COPY_CLASS(wxGraphicsRenderer)
9e605538
SC
491 DECLARE_ABSTRACT_CLASS(wxGraphicsRenderer)
492} ;
493
774f4d12 494#endif
50581042 495
8acd14d1 496#endif // _WX_GRAPHICS_H_