start using wxGraphicsContext for OS X instead of old core graphics implementation
[wxWidgets.git] / include / wx / graphics.h
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
15 #include "wx/geometry.h"
16 #include "wx/dynarray.h"
17
18 class WXDLLEXPORT wxWindowDC;
19
20 #if wxUSE_GRAPHICS_CONTEXT
21
22 /*
23 * notes about the graphics context apis
24 *
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.
27 */
28
29 class WXDLLEXPORT wxGraphicsPath
30 {
31 public :
32 wxGraphicsPath() {}
33 virtual ~wxGraphicsPath() {}
34
35 //
36 // These are the path primitives from which everything else can be constructed
37 //
38
39 // begins a new subpath at (x,y)
40 virtual void MoveToPoint( wxDouble x, wxDouble y ) = 0;
41
42 // adds a straight line from the current point to (x,y)
43 virtual void AddLineToPoint( wxDouble x, wxDouble y ) = 0;
44
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;
47
48 // closes the current sub-path
49 virtual void CloseSubpath() = 0;
50
51 // gets the last point of the current path, (0,0) if not yet set
52 virtual void GetCurrentPoint( wxDouble& x, wxDouble&y) = 0;
53
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;
56
57 //
58 // These are convenience functions which - if not available natively will be assembled
59 // using the primitives from above
60 //
61
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 );
64
65 // appends a rectangle as a new closed subpath
66 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
67
68 // appends an ellipsis as a new closed subpath fitting the passed rectangle
69 virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r );
70
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 ) ;
73
74 // wrappers using wxPoint2DDoubles
75
76 wxPoint2DDouble GetCurrentPoint();
77
78 void MoveToPoint( const wxPoint2DDouble& p);
79
80 void AddLineToPoint( const wxPoint2DDouble& p);
81
82 void AddCurveToPoint( const wxPoint2DDouble& c1, const wxPoint2DDouble& c2, const wxPoint2DDouble& e);
83
84 void AddArc( const wxPoint2DDouble& c, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise);
85
86 DECLARE_NO_COPY_CLASS(wxGraphicsPath)
87 };
88
89 /*
90 class WXDLLEXPORT wxGraphicsMatrix
91 {
92 public :
93 wxGraphicsMatrix() {}
94
95 virtual ~wxGraphicsMatrix() {}
96
97 wxGraphicsMatrix* Concat( const wxGraphicsMatrix *t ) const;
98
99 // returns the inverse matrix
100 wxGraphicsMatrix* Invert() const;
101
102 // returns true if the elements of the transformation matrix are equal ?
103 bool operator==(const wxGraphicsMatrix& t) const;
104
105 // return true if this is the identity matrix
106 bool IsIdentity();
107
108 //
109 // transformation
110 //
111
112 // translate
113 virtual void Translate( wxDouble dx , wxDouble dy ) = 0;
114
115 // scale
116 virtual void Scale( wxDouble xScale , wxDouble yScale ) = 0;
117
118 // rotate (radians)
119 virtual void Rotate( wxDouble angle ) = 0;
120 } ;
121 */
122
123 class WXDLLEXPORT wxGraphicsContext
124 {
125 public:
126 wxGraphicsContext() {}
127
128 virtual ~wxGraphicsContext() {}
129
130 static wxGraphicsContext* Create( const wxWindowDC& dc) ;
131
132 static wxGraphicsContext* CreateFromNative( void * native ) ;
133
134 static wxGraphicsContext* Create( wxWindow* window ) ;
135
136 // creates a path instance that corresponds to the type of graphics context, ie GDIPlus, cairo, CoreGraphics ...
137 virtual wxGraphicsPath * CreatePath() = 0;
138
139 /*
140 // create a 'native' matrix corresponding to these values
141 virtual wxGraphicsMatrix* CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
142 wxDouble tx=0.0, wxDouble ty=0.0) = 0;
143 */
144
145 // push the current state of the context, ie the transformation matrix on a stack
146 virtual void PushState() = 0;
147
148 // pops a stored state from the stack
149 virtual void PopState() = 0;
150
151 // clips drawings to the region, combined to current clipping region
152 virtual void Clip( const wxRegion &region ) = 0;
153
154 // clips drawings to the rect
155 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
156
157 // resets the clipping to original extent
158 virtual void ResetClip() = 0 ;
159
160 // returns the native context
161 virtual void * GetNativeContext() = 0;
162
163 //
164 // transformation : changes the current transformation matrix CTM of the context
165 //
166
167 // translate
168 virtual void Translate( wxDouble dx , wxDouble dy ) = 0;
169
170 // scale
171 virtual void Scale( wxDouble xScale , wxDouble yScale ) = 0;
172
173 // rotate (radians)
174 virtual void Rotate( wxDouble angle ) = 0;
175
176 //
177 // setting the paint
178 //
179
180 // sets the pan
181 virtual void SetPen( const wxPen &pen ) = 0;
182
183 // sets the brush for filling
184 virtual void SetBrush( const wxBrush &brush ) = 0;
185
186 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
187 virtual void SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
188 const wxColour&c1, const wxColour&c2) = 0;
189
190 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
191 // with radius r and color cColor
192 virtual void SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
193 const wxColour &oColor, const wxColour &cColor) = 0;
194
195 // sets the font
196 virtual void SetFont( const wxFont &font ) = 0;
197
198 // sets the text color
199 virtual void SetTextColor( const wxColour &col ) = 0;
200
201 // strokes along a path with the current pen
202 virtual void StrokePath( const wxGraphicsPath *path ) = 0;
203
204 // fills a path with the current brush
205 virtual void FillPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE ) = 0;
206
207 // draws a path by first filling and then stroking
208 virtual void DrawPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE );
209
210 //
211 // text
212 //
213
214 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y ) = 0;
215
216 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle );
217
218 virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
219 wxDouble *descent, wxDouble *externalLeading ) const = 0;
220
221 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const = 0;
222
223 //
224 // image support
225 //
226
227 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
228
229 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
230
231 //
232 // convenience methods
233 //
234
235 // strokes a single line
236 virtual void StrokeLine( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2);
237
238 // stroke lines connecting each of the points
239 virtual void StrokeLines( size_t n, const wxPoint2DDouble *points);
240
241 // stroke disconnected lines from begin to end points
242 virtual void StrokeLines( size_t n, const wxPoint2DDouble *beginPoints, const wxPoint2DDouble *endPoints);
243
244 // draws a polygon
245 virtual void DrawLines( size_t n, const wxPoint2DDouble *points, int fillStyle = wxWINDING_RULE );
246
247 // draws a polygon
248 virtual void DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
249
250 // draws an ellipse
251 virtual void DrawEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
252
253 // draws a rounded rectangle
254 virtual void DrawRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius);
255
256 // wrappers using wxPoint2DDouble TODO
257
258 DECLARE_NO_COPY_CLASS(wxGraphicsContext)
259 };
260
261 #ifdef __WXMAC__
262 #define wxGCDC wxDC
263 #endif
264
265 class WXDLLEXPORT wxGCDC:
266 #idef __WXMAC__
267 public wxDCBase
268 #else
269 public wxDC
270 #endif
271 {
272 DECLARE_DYNAMIC_CLASS(wxGCDC)
273 DECLARE_NO_COPY_CLASS(wxGCDC)
274
275 public:
276 wxGCDC(const wxWindowDC& dc);
277 wxGCDC();
278 virtual ~wxGCDC();
279
280 void Init();
281
282
283 // implement base class pure virtuals
284 // ----------------------------------
285
286 virtual void Clear();
287
288 virtual bool StartDoc( const wxString& WXUNUSED(message) ) { return true; }
289 virtual void EndDoc(void) {}
290
291 virtual void StartPage(void) {}
292 virtual void EndPage(void) {}
293
294 virtual void SetFont(const wxFont& font);
295 virtual void SetPen(const wxPen& pen);
296 virtual void SetBrush(const wxBrush& brush);
297 virtual void SetBackground(const wxBrush& brush);
298 virtual void SetBackgroundMode(int mode);
299 virtual void SetPalette(const wxPalette& palette);
300
301 virtual void DestroyClippingRegion();
302
303 virtual wxCoord GetCharHeight() const;
304 virtual wxCoord GetCharWidth() const;
305
306 virtual bool CanDrawBitmap() const;
307 virtual bool CanGetTextExtent() const;
308 virtual int GetDepth() const;
309 virtual wxSize GetPPI() const;
310
311 virtual void SetMapMode(int mode);
312 virtual void SetUserScale(double x, double y);
313
314 virtual void SetLogicalScale(double x, double y);
315 virtual void SetLogicalOrigin(wxCoord x, wxCoord y);
316 virtual void SetDeviceOrigin(wxCoord x, wxCoord y);
317 virtual void SetAxisOrientation(bool xLeftRight, bool yBottomUp);
318 virtual void SetLogicalFunction(int function);
319
320 virtual void SetTextForeground(const wxColour& colour);
321 virtual void SetTextBackground(const wxColour& colour);
322
323 virtual void ComputeScaleAndOrigin();
324
325 wxGraphicsContext* GetGraphicContext() { return m_graphicContext; }
326 virtual void SetGraphicsContext( wxGraphicsContext* ctx )
327 { delete m_graphicContext; m_graphicContext = ctx; }
328 protected:
329 // the true implementations
330 virtual bool DoFloodFill(wxCoord x, wxCoord y, const wxColour& col,
331 int style = wxFLOOD_SURFACE);
332
333 virtual void DoGradientFillLinear(const wxRect& rect,
334 const wxColour& initialColour,
335 const wxColour& destColour,
336 wxDirection nDirection = wxEAST);
337
338 virtual void DoGradientFillConcentric(const wxRect& rect,
339 const wxColour& initialColour,
340 const wxColour& destColour,
341 const wxPoint& circleCenter);
342
343 virtual bool DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const;
344
345 virtual void DoDrawPoint(wxCoord x, wxCoord y);
346
347 #if wxUSE_SPLINES
348 virtual void DoDrawSpline(wxList *points);
349 #endif
350
351 virtual void DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2);
352
353 virtual void DoDrawArc(wxCoord x1, wxCoord y1,
354 wxCoord x2, wxCoord y2,
355 wxCoord xc, wxCoord yc);
356
357 virtual void DoDrawCheckMark(wxCoord x, wxCoord y,
358 wxCoord width, wxCoord height);
359
360 virtual void DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h,
361 double sa, double ea);
362
363 virtual void DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height);
364 virtual void DoDrawRoundedRectangle(wxCoord x, wxCoord y,
365 wxCoord width, wxCoord height,
366 double radius);
367 virtual void DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height);
368
369 virtual void DoCrossHair(wxCoord x, wxCoord y);
370
371 virtual void DoDrawIcon(const wxIcon& icon, wxCoord x, wxCoord y);
372 virtual void DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y,
373 bool useMask = false);
374
375 virtual void DoDrawText(const wxString& text, wxCoord x, wxCoord y);
376 virtual void DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y,
377 double angle);
378
379 virtual bool DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
380 wxDC *source, wxCoord xsrc, wxCoord ysrc,
381 int rop = wxCOPY, bool useMask = false, wxCoord xsrcMask = -1, wxCoord ysrcMask = -1);
382
383 virtual void DoGetSize(int *,int *) const;
384 virtual void DoGetSizeMM(int* width, int* height) const;
385
386 virtual void DoDrawLines(int n, wxPoint points[],
387 wxCoord xoffset, wxCoord yoffset);
388 virtual void DoDrawPolygon(int n, wxPoint points[],
389 wxCoord xoffset, wxCoord yoffset,
390 int fillStyle = wxODDEVEN_RULE);
391 virtual void DoDrawPolyPolygon(int n, int count[], wxPoint points[],
392 wxCoord xoffset, wxCoord yoffset,
393 int fillStyle);
394
395 virtual void DoSetClippingRegionAsRegion(const wxRegion& region);
396 virtual void DoSetClippingRegion(wxCoord x, wxCoord y,
397 wxCoord width, wxCoord height);
398
399 virtual void DoGetTextExtent(const wxString& string,
400 wxCoord *x, wxCoord *y,
401 wxCoord *descent = NULL,
402 wxCoord *externalLeading = NULL,
403 wxFont *theFont = NULL) const;
404
405 virtual bool DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const;
406
407 protected:
408 // scaling variables
409 double m_mm_to_pix_x, m_mm_to_pix_y;
410
411 double m_formerScaleX, m_formerScaleY;
412
413 wxGraphicsContext* m_graphicContext;
414 };
415
416 #endif
417
418 #endif
419 // _WX_GRID_H_BASE_