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 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_GRAPHICS_H_
12 #define _WX_GRAPHICS_H_
13
14 #include "wx/defs.h"
15
16 #if wxUSE_GRAPHICS_CONTEXT
17
18 #include "wx/affinematrix2d.h"
19 #include "wx/geometry.h"
20 #include "wx/colour.h"
21 #include "wx/dynarray.h"
22 #include "wx/font.h"
23 #include "wx/image.h"
24 #include "wx/peninfobase.h"
25 #include "wx/vector.h"
26
27 enum wxAntialiasMode
28 {
29 wxANTIALIAS_NONE, // should be 0
30 wxANTIALIAS_DEFAULT
31 };
32
33 enum wxInterpolationQuality
34 {
35 // default interpolation
36 wxINTERPOLATION_DEFAULT,
37 // no interpolation
38 wxINTERPOLATION_NONE,
39 // fast interpolation, suited for interactivity
40 wxINTERPOLATION_FAST,
41 // better quality
42 wxINTERPOLATION_GOOD,
43 // best quality, not suited for interactivity
44 wxINTERPOLATION_BEST
45 };
46
47 enum wxCompositionMode
48 {
49 // R = Result, S = Source, D = Destination, premultiplied with alpha
50 // Ra, Sa, Da their alpha components
51
52 // classic Porter-Duff compositions
53 // http://keithp.com/~keithp/porterduff/p253-porter.pdf
54
55 wxCOMPOSITION_INVALID = -1, /* indicates invalid/unsupported mode */
56 wxCOMPOSITION_CLEAR, /* R = 0 */
57 wxCOMPOSITION_SOURCE, /* R = S */
58 wxCOMPOSITION_OVER, /* R = S + D*(1 - Sa) */
59 wxCOMPOSITION_IN, /* R = S*Da */
60 wxCOMPOSITION_OUT, /* R = S*(1 - Da) */
61 wxCOMPOSITION_ATOP, /* R = S*Da + D*(1 - Sa) */
62
63 wxCOMPOSITION_DEST, /* R = D, essentially a noop */
64 wxCOMPOSITION_DEST_OVER, /* R = S*(1 - Da) + D */
65 wxCOMPOSITION_DEST_IN, /* R = D*Sa */
66 wxCOMPOSITION_DEST_OUT, /* R = D*(1 - Sa) */
67 wxCOMPOSITION_DEST_ATOP, /* R = S*(1 - Da) + D*Sa */
68 wxCOMPOSITION_XOR, /* R = S*(1 - Da) + D*(1 - Sa) */
69
70 // mathematical compositions
71 wxCOMPOSITION_ADD /* R = S + D */
72 };
73
74 enum wxGradientType
75 {
76 wxGRADIENT_NONE,
77 wxGRADIENT_LINEAR,
78 wxGRADIENT_RADIAL
79 };
80
81
82 class WXDLLIMPEXP_FWD_CORE wxDC;
83 class WXDLLIMPEXP_FWD_CORE wxWindowDC;
84 class WXDLLIMPEXP_FWD_CORE wxMemoryDC;
85 #if wxUSE_PRINTING_ARCHITECTURE
86 class WXDLLIMPEXP_FWD_CORE wxPrinterDC;
87 #endif
88 #ifdef __WXMSW__
89 #if wxUSE_ENH_METAFILE
90 class WXDLLIMPEXP_FWD_CORE wxEnhMetaFileDC;
91 #endif
92 #endif
93 class WXDLLIMPEXP_FWD_CORE wxGraphicsContext;
94 class WXDLLIMPEXP_FWD_CORE wxGraphicsPath;
95 class WXDLLIMPEXP_FWD_CORE wxGraphicsMatrix;
96 class WXDLLIMPEXP_FWD_CORE wxGraphicsFigure;
97 class WXDLLIMPEXP_FWD_CORE wxGraphicsRenderer;
98 class WXDLLIMPEXP_FWD_CORE wxGraphicsPen;
99 class WXDLLIMPEXP_FWD_CORE wxGraphicsBrush;
100 class WXDLLIMPEXP_FWD_CORE wxGraphicsFont;
101 class WXDLLIMPEXP_FWD_CORE wxGraphicsBitmap;
102
103
104 /*
105 * notes about the graphics context apis
106 *
107 * angles : are measured in radians, 0.0 being in direction of positive x axis, PI/2 being
108 * in direction of positive y axis.
109 */
110
111 // Base class of all objects used for drawing in the new graphics API, the always point back to their
112 // originating rendering engine, there is no dynamic unloading of a renderer currently allowed,
113 // these references are not counted
114
115 //
116 // The data used by objects like graphics pens etc is ref counted, in order to avoid unnecessary expensive
117 // duplication. Any operation on a shared instance that results in a modified state, uncouples this
118 // instance from the other instances that were shared - using copy on write semantics
119 //
120
121 class WXDLLIMPEXP_FWD_CORE wxGraphicsObjectRefData;
122 class WXDLLIMPEXP_FWD_CORE wxGraphicsBitmapData;
123 class WXDLLIMPEXP_FWD_CORE wxGraphicsMatrixData;
124 class WXDLLIMPEXP_FWD_CORE wxGraphicsPathData;
125
126 class WXDLLIMPEXP_CORE wxGraphicsObject : public wxObject
127 {
128 public:
129 wxGraphicsObject();
130 wxGraphicsObject( wxGraphicsRenderer* renderer );
131 virtual ~wxGraphicsObject();
132
133 bool IsNull() const;
134
135 // returns the renderer that was used to create this instance, or NULL if it has not been initialized yet
136 wxGraphicsRenderer* GetRenderer() const;
137 wxGraphicsObjectRefData* GetGraphicsData() const;
138 protected:
139 virtual wxObjectRefData* CreateRefData() const wxOVERRIDE;
140 virtual wxObjectRefData* CloneRefData(const wxObjectRefData* data) const wxOVERRIDE;
141
142 wxDECLARE_DYNAMIC_CLASS(wxGraphicsObject);
143 };
144
145
146
147 class WXDLLIMPEXP_CORE wxGraphicsPen : public wxGraphicsObject
148 {
149 public:
wxGraphicsPen()150 wxGraphicsPen() {}
~wxGraphicsPen()151 virtual ~wxGraphicsPen() {}
152 private:
153 wxDECLARE_DYNAMIC_CLASS(wxGraphicsPen);
154 };
155
156 extern WXDLLIMPEXP_DATA_CORE(wxGraphicsPen) wxNullGraphicsPen;
157
158 class WXDLLIMPEXP_CORE wxGraphicsBrush : public wxGraphicsObject
159 {
160 public:
wxGraphicsBrush()161 wxGraphicsBrush() {}
~wxGraphicsBrush()162 virtual ~wxGraphicsBrush() {}
163 private:
164 wxDECLARE_DYNAMIC_CLASS(wxGraphicsBrush);
165 };
166
167 extern WXDLLIMPEXP_DATA_CORE(wxGraphicsBrush) wxNullGraphicsBrush;
168
169 class WXDLLIMPEXP_CORE wxGraphicsFont : public wxGraphicsObject
170 {
171 public:
wxGraphicsFont()172 wxGraphicsFont() {}
~wxGraphicsFont()173 virtual ~wxGraphicsFont() {}
174 private:
175 wxDECLARE_DYNAMIC_CLASS(wxGraphicsFont);
176 };
177
178 extern WXDLLIMPEXP_DATA_CORE(wxGraphicsFont) wxNullGraphicsFont;
179
180 class WXDLLIMPEXP_CORE wxGraphicsBitmap : public wxGraphicsObject
181 {
182 public:
wxGraphicsBitmap()183 wxGraphicsBitmap() {}
~wxGraphicsBitmap()184 virtual ~wxGraphicsBitmap() {}
185
186 // Convert bitmap to wxImage: this is more efficient than converting to
187 // wxBitmap first and then to wxImage and also works without X server
188 // connection under Unix that wxBitmap requires.
189 #if wxUSE_IMAGE
190 wxImage ConvertToImage() const;
191 #endif // wxUSE_IMAGE
192
193 void* GetNativeBitmap() const;
194
GetBitmapData()195 const wxGraphicsBitmapData* GetBitmapData() const
196 { return (const wxGraphicsBitmapData*) GetRefData(); }
GetBitmapData()197 wxGraphicsBitmapData* GetBitmapData()
198 { return (wxGraphicsBitmapData*) GetRefData(); }
199
200 private:
201 wxDECLARE_DYNAMIC_CLASS(wxGraphicsBitmap);
202 };
203
204 extern WXDLLIMPEXP_DATA_CORE(wxGraphicsBitmap) wxNullGraphicsBitmap;
205
206 class WXDLLIMPEXP_CORE wxGraphicsMatrix : public wxGraphicsObject
207 {
208 public:
wxGraphicsMatrix()209 wxGraphicsMatrix() {}
210
~wxGraphicsMatrix()211 virtual ~wxGraphicsMatrix() {}
212
213 // concatenates the matrix
214 virtual void Concat( const wxGraphicsMatrix *t );
Concat(const wxGraphicsMatrix & t)215 void Concat( const wxGraphicsMatrix &t ) { Concat( &t ); }
216
217 // sets the matrix to the respective values
218 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
219 wxDouble tx=0.0, wxDouble ty=0.0);
220
221 // gets the component values of the matrix
222 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
223 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
224
225 // makes this the inverse matrix
226 virtual void Invert();
227
228 // returns true if the elements of the transformation matrix are equal ?
229 virtual bool IsEqual( const wxGraphicsMatrix* t) const;
IsEqual(const wxGraphicsMatrix & t)230 bool IsEqual( const wxGraphicsMatrix& t) const { return IsEqual( &t ); }
231
232 // return true if this is the identity matrix
233 virtual bool IsIdentity() const;
234
235 //
236 // transformation
237 //
238
239 // add the translation to this matrix
240 virtual void Translate( wxDouble dx , wxDouble dy );
241
242 // add the scale to this matrix
243 virtual void Scale( wxDouble xScale , wxDouble yScale );
244
245 // add the rotation to this matrix (radians)
246 virtual void Rotate( wxDouble angle );
247
248 //
249 // apply the transforms
250 //
251
252 // applies that matrix to the point
253 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
254
255 // applies the matrix except for translations
256 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
257
258 // returns the native representation
259 virtual void * GetNativeMatrix() const;
260
GetMatrixData()261 const wxGraphicsMatrixData* GetMatrixData() const
262 { return (const wxGraphicsMatrixData*) GetRefData(); }
GetMatrixData()263 wxGraphicsMatrixData* GetMatrixData()
264 { return (wxGraphicsMatrixData*) GetRefData(); }
265
266 private:
267 wxDECLARE_DYNAMIC_CLASS(wxGraphicsMatrix);
268 };
269
270 extern WXDLLIMPEXP_DATA_CORE(wxGraphicsMatrix) wxNullGraphicsMatrix;
271
272 // ----------------------------------------------------------------------------
273 // wxGradientStop and wxGradientStops: Specify what intermediate colors are used
274 // and how they are spread out in a gradient
275 // ----------------------------------------------------------------------------
276
277 // gcc 9 gives a nonsensical warning about implicitly generated move ctor not
278 // throwing but not being noexcept, suppress it.
279 #if wxCHECK_GCC_VERSION(9, 1) && !wxCHECK_GCC_VERSION(10, 0)
wxGCC_WARNING_SUPPRESS(noexcept)280 wxGCC_WARNING_SUPPRESS(noexcept)
281 #endif
282
283 // Describes a single gradient stop.
284 class wxGraphicsGradientStop
285 {
286 public:
287 wxGraphicsGradientStop(wxColour col = wxTransparentColour,
288 float pos = 0.0f)
289 : m_col(col),
290 m_pos(pos)
291 {
292 }
293
294 // default copy ctor, assignment operator and dtor are ok
295
296 const wxColour& GetColour() const { return m_col; }
297 void SetColour(const wxColour& col) { m_col = col; }
298
299 float GetPosition() const { return m_pos; }
300 void SetPosition(float pos)
301 {
302 wxASSERT_MSG( pos >= 0 && pos <= 1, "invalid gradient stop position" );
303
304 m_pos = pos;
305 }
306
307 private:
308 // The colour of this gradient band.
309 wxColour m_col;
310
311 // Its starting position: 0 is the beginning and 1 is the end.
312 float m_pos;
313 };
314
315 #if wxCHECK_GCC_VERSION(9, 1) && !wxCHECK_GCC_VERSION(10, 0)
wxGCC_WARNING_RESTORE(noexcept)316 wxGCC_WARNING_RESTORE(noexcept)
317 #endif
318
319 // A collection of gradient stops ordered by their positions (from lowest to
320 // highest). The first stop (index 0, position 0.0) is always the starting
321 // colour and the last one (index GetCount() - 1, position 1.0) is the end
322 // colour.
323 class WXDLLIMPEXP_CORE wxGraphicsGradientStops
324 {
325 public:
326 wxGraphicsGradientStops(wxColour startCol = wxTransparentColour,
327 wxColour endCol = wxTransparentColour)
328 {
329 // we can't use Add() here as it relies on having start/end stops as
330 // first/last array elements so do it manually
331 m_stops.push_back(wxGraphicsGradientStop(startCol, 0.f));
332 m_stops.push_back(wxGraphicsGradientStop(endCol, 1.f));
333 }
334
335 // default copy ctor, assignment operator and dtor are ok for this class
336
337
338 // Add a stop in correct order.
339 void Add(const wxGraphicsGradientStop& stop);
340 void Add(wxColour col, float pos) { Add(wxGraphicsGradientStop(col, pos)); }
341
342 // Get the number of stops.
343 size_t GetCount() const { return m_stops.size(); }
344
345 // Return the stop at the given index (which must be valid).
346 wxGraphicsGradientStop Item(unsigned n) const { return m_stops.at(n); }
347
348 // Get/set start and end colours.
349 void SetStartColour(wxColour col)
350 { m_stops[0].SetColour(col); }
351 wxColour GetStartColour() const
352 { return m_stops[0].GetColour(); }
353 void SetEndColour(wxColour col)
354 { m_stops[m_stops.size() - 1].SetColour(col); }
355 wxColour GetEndColour() const
356 { return m_stops[m_stops.size() - 1].GetColour(); }
357
358 private:
359 // All the stops stored in ascending order of positions.
360 wxVector<wxGraphicsGradientStop> m_stops;
361 };
362
363 // ----------------------------------------------------------------------------
364 // wxGraphicsPenInfo describes a wxGraphicsPen
365 // ----------------------------------------------------------------------------
366
367 class wxGraphicsPenInfo : public wxPenInfoBase<wxGraphicsPenInfo>
368 {
369 public:
370 explicit wxGraphicsPenInfo(const wxColour& colour = wxColour(),
371 wxDouble width = 1.0,
372 wxPenStyle style = wxPENSTYLE_SOLID)
373 : wxPenInfoBase<wxGraphicsPenInfo>(colour, style)
374 {
375 m_width = width;
376 m_gradientType = wxGRADIENT_NONE;
377 }
378
379 // Setters
380
Width(wxDouble width)381 wxGraphicsPenInfo& Width(wxDouble width)
382 { m_width = width; return *this; }
383
384 wxGraphicsPenInfo&
385 LinearGradient(wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
386 const wxColour& c1, const wxColour& c2,
387 const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix)
388 {
389 m_gradientType = wxGRADIENT_LINEAR;
390 m_x1 = x1;
391 m_y1 = y1;
392 m_x2 = x2;
393 m_y2 = y2;
394 m_stops.SetStartColour(c1);
395 m_stops.SetEndColour(c2);
396 m_matrix = matrix;
397 return *this;
398 }
399
400 wxGraphicsPenInfo&
401 LinearGradient(wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
402 const wxGraphicsGradientStops& stops,
403 const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix)
404 {
405 m_gradientType = wxGRADIENT_LINEAR;
406 m_x1 = x1;
407 m_y1 = y1;
408 m_x2 = x2;
409 m_y2 = y2;
410 m_stops = stops;
411 m_matrix = matrix;
412 return *this;
413 }
414
415 wxGraphicsPenInfo&
416 RadialGradient(wxDouble startX, wxDouble startY,
417 wxDouble endX, wxDouble endY, wxDouble radius,
418 const wxColour& oColor, const wxColour& cColor,
419 const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix)
420 {
421 m_gradientType = wxGRADIENT_RADIAL;
422 m_x1 = startX;
423 m_y1 = startY;
424 m_x2 = endX;
425 m_y2 = endY;
426 m_radius = radius;
427 m_stops.SetStartColour(oColor);
428 m_stops.SetEndColour(cColor);
429 m_matrix = matrix;
430 return *this;
431 }
432
433 wxGraphicsPenInfo&
434 RadialGradient(wxDouble startX, wxDouble startY,
435 wxDouble endX, wxDouble endY,
436 wxDouble radius, const wxGraphicsGradientStops& stops,
437 const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix)
438 {
439 m_gradientType = wxGRADIENT_RADIAL;
440 m_x1 = startX;
441 m_y1 = startY;
442 m_x2 = endX;
443 m_y2 = endY;
444 m_radius = radius;
445 m_stops = stops;
446 m_matrix = matrix;
447 return *this;
448 }
449
450 // Accessors
451
GetWidth()452 wxDouble GetWidth() const { return m_width; }
GetGradientType()453 wxGradientType GetGradientType() const { return m_gradientType; }
GetX1()454 wxDouble GetX1() const { return m_x1; }
GetY1()455 wxDouble GetY1() const { return m_y1; }
GetX2()456 wxDouble GetX2() const { return m_x2; }
GetY2()457 wxDouble GetY2() const { return m_y2; }
GetStartX()458 wxDouble GetStartX() const { return m_x1; }
GetStartY()459 wxDouble GetStartY() const { return m_y1; }
GetEndX()460 wxDouble GetEndX() const { return m_x2; }
GetEndY()461 wxDouble GetEndY() const { return m_y2; }
GetRadius()462 wxDouble GetRadius() const { return m_radius; }
GetStops()463 const wxGraphicsGradientStops& GetStops() const { return m_stops; }
GetMatrix()464 const wxGraphicsMatrix& GetMatrix() const { return m_matrix; }
465
466 private:
467 wxDouble m_width;
468 wxGradientType m_gradientType;
469 wxDouble m_x1, m_y1, m_x2, m_y2; // also used for m_xo, m_yo, m_xc, m_yc
470 wxDouble m_radius;
471 wxGraphicsGradientStops m_stops;
472 wxGraphicsMatrix m_matrix;
473 };
474
475
476
477 class WXDLLIMPEXP_CORE wxGraphicsPath : public wxGraphicsObject
478 {
479 public:
wxGraphicsPath()480 wxGraphicsPath() {}
~wxGraphicsPath()481 virtual ~wxGraphicsPath() {}
482
483 //
484 // These are the path primitives from which everything else can be constructed
485 //
486
487 // begins a new subpath at (x,y)
488 virtual void MoveToPoint( wxDouble x, wxDouble y );
489 void MoveToPoint( const wxPoint2DDouble& p);
490
491 // adds a straight line from the current point to (x,y)
492 virtual void AddLineToPoint( wxDouble x, wxDouble y );
493 void AddLineToPoint( const wxPoint2DDouble& p);
494
495 // adds a cubic Bezier curve from the current point, using two control points and an end point
496 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
497 void AddCurveToPoint( const wxPoint2DDouble& c1, const wxPoint2DDouble& c2, const wxPoint2DDouble& e);
498
499 // adds another path
500 virtual void AddPath( const wxGraphicsPath& path );
501
502 // closes the current sub-path
503 virtual void CloseSubpath();
504
505 // gets the last point of the current path, (0,0) if not yet set
506 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
507 wxPoint2DDouble GetCurrentPoint() const;
508
509 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
510 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise );
511 void AddArc( const wxPoint2DDouble& c, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise);
512
513 //
514 // These are convenience functions which - if not available natively will be assembled
515 // using the primitives from above
516 //
517
518 // adds a quadratic Bezier curve from the current point, using a control point and an end point
519 virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y );
520
521 // appends a rectangle as a new closed subpath
522 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
523
524 // appends an ellipsis as a new closed subpath fitting the passed rectangle
525 virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r );
526
527 // 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)
528 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r );
529
530 // appends an ellipse
531 virtual void AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
532
533 // appends a rounded rectangle
534 virtual void AddRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius);
535
536 // returns the native path
537 virtual void * GetNativePath() const;
538
539 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
540 virtual void UnGetNativePath(void *p)const;
541
542 // transforms each point of this path by the matrix
543 virtual void Transform( const wxGraphicsMatrix& matrix );
544
545 // gets the bounding box enclosing all points (possibly including control points)
546 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h)const;
547 wxRect2DDouble GetBox()const;
548
549 virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxODDEVEN_RULE)const;
550 bool Contains( const wxPoint2DDouble& c, wxPolygonFillMode fillStyle = wxODDEVEN_RULE)const;
551
GetPathData()552 const wxGraphicsPathData* GetPathData() const
553 { return (const wxGraphicsPathData*) GetRefData(); }
GetPathData()554 wxGraphicsPathData* GetPathData()
555 { return (wxGraphicsPathData*) GetRefData(); }
556
557 private:
558 wxDECLARE_DYNAMIC_CLASS(wxGraphicsPath);
559 };
560
561 extern WXDLLIMPEXP_DATA_CORE(wxGraphicsPath) wxNullGraphicsPath;
562
563
564 class WXDLLIMPEXP_CORE wxGraphicsContext : public wxGraphicsObject
565 {
566 public:
567 wxGraphicsContext(wxGraphicsRenderer* renderer, wxWindow* window = NULL);
568
569 virtual ~wxGraphicsContext();
570
571 static wxGraphicsContext* Create( const wxWindowDC& dc);
572 static wxGraphicsContext * Create( const wxMemoryDC& dc);
573 #if wxUSE_PRINTING_ARCHITECTURE
574 static wxGraphicsContext * Create( const wxPrinterDC& dc);
575 #endif
576 #ifdef __WXMSW__
577 #if wxUSE_ENH_METAFILE
578 static wxGraphicsContext * Create( const wxEnhMetaFileDC& dc);
579 #endif
580 #endif
581
582 // Create a context from a DC of unknown type, if supported, returns NULL otherwise
583 static wxGraphicsContext* CreateFromUnknownDC(const wxDC& dc);
584
585 static wxGraphicsContext* CreateFromNative( void * context );
586
587 static wxGraphicsContext* CreateFromNativeWindow( void * window );
588
589 #ifdef __WXMSW__
590 static wxGraphicsContext* CreateFromNativeHDC(WXHDC dc);
591 #endif
592
593 static wxGraphicsContext* Create( wxWindow* window );
594
595 #if wxUSE_IMAGE
596 // Create a context for drawing onto a wxImage. The image life time must be
597 // greater than that of the context itself as when the context is destroyed
598 // it will copy its contents to the specified image.
599 static wxGraphicsContext* Create(wxImage& image);
600 #endif // wxUSE_IMAGE
601
602 // create a context that can be used for measuring texts only, no drawing allowed
603 static wxGraphicsContext * Create();
604
605 // Return the window this context is associated with, if any.
GetWindow()606 wxWindow* GetWindow() const { return m_window; }
607
608 // begin a new document (relevant only for printing / pdf etc) if there is a progress dialog, message will be shown
609 virtual bool StartDoc( const wxString& message );
610
611 // done with that document (relevant only for printing / pdf etc)
612 virtual void EndDoc();
613
614 // opens a new page (relevant only for printing / pdf etc) with the given size in points
615 // (if both are null the default page size will be used)
616 virtual void StartPage( wxDouble width = 0, wxDouble height = 0 );
617
618 // ends the current page (relevant only for printing / pdf etc)
619 virtual void EndPage();
620
621 // make sure that the current content of this context is immediately visible
622 virtual void Flush();
623
624 wxGraphicsPath CreatePath() const;
625
626 wxGraphicsPen CreatePen(const wxPen& pen) const;
627
CreatePen(const wxGraphicsPenInfo & info)628 wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) const
629 { return DoCreatePen(info); }
630
631 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) const;
632
633 // sets the brush to a linear gradient, starting at (x1,y1) and ending at
634 // (x2,y2) with the given boundary colours or the specified stops
635 wxGraphicsBrush
636 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
637 wxDouble x2, wxDouble y2,
638 const wxColour& c1, const wxColour& c2,
639 const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix) const;
640 wxGraphicsBrush
641 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
642 wxDouble x2, wxDouble y2,
643 const wxGraphicsGradientStops& stops,
644 const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix) const;
645
646 // sets the brush to a radial gradient originating at (xo,yc) and ending
647 // on a circle around (xc,yc) with the given radius; the colours may be
648 // specified by just the two extremes or the full array of gradient stops
649 wxGraphicsBrush
650 CreateRadialGradientBrush(wxDouble startX, wxDouble startY,
651 wxDouble endX, wxDouble endY, wxDouble radius,
652 const wxColour& oColor, const wxColour& cColor,
653 const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix) const;
654
655 wxGraphicsBrush
656 CreateRadialGradientBrush(wxDouble startX, wxDouble startY,
657 wxDouble endX, wxDouble endY, wxDouble radius,
658 const wxGraphicsGradientStops& stops,
659 const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix) const;
660
661 // creates a font
662 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) const;
663 virtual wxGraphicsFont CreateFont(double sizeInPixels,
664 const wxString& facename,
665 int flags = wxFONTFLAG_DEFAULT,
666 const wxColour& col = *wxBLACK) const;
667
668 // create a native bitmap representation
669 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ) const;
670 #if wxUSE_IMAGE
671 wxGraphicsBitmap CreateBitmapFromImage(const wxImage& image) const;
672 #endif // wxUSE_IMAGE
673
674 // create a native bitmap representation
675 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) const;
676
677 // create a 'native' matrix corresponding to these values
678 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
679 wxDouble tx=0.0, wxDouble ty=0.0) const;
680
CreateMatrix(const wxAffineMatrix2DBase & mat)681 wxGraphicsMatrix CreateMatrix( const wxAffineMatrix2DBase& mat ) const
682 {
683 wxMatrix2D mat2D;
684 wxPoint2DDouble tr;
685 mat.Get(&mat2D, &tr);
686
687 return CreateMatrix(mat2D.m_11, mat2D.m_12, mat2D.m_21, mat2D.m_22,
688 tr.m_x, tr.m_y);
689 }
690
691 // push the current state of the context, ie the transformation matrix on a stack
692 virtual void PushState() = 0;
693
694 // pops a stored state from the stack
695 virtual void PopState() = 0;
696
697 // clips drawings to the region intersected with the current clipping region
698 virtual void Clip( const wxRegion ®ion ) = 0;
699
700 // clips drawings to the rect intersected with the current clipping region
701 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
702
703 // resets the clipping to original extent
704 virtual void ResetClip() = 0;
705
706 // returns bounding box of the clipping region
707 virtual void GetClipBox(wxDouble* x, wxDouble* y, wxDouble* w, wxDouble* h) = 0;
708
709 // returns the native context
710 virtual void * GetNativeContext() = 0;
711
712 // returns the current shape antialiasing mode
GetAntialiasMode()713 virtual wxAntialiasMode GetAntialiasMode() const { return m_antialias; }
714
715 // sets the antialiasing mode, returns true if it supported
716 virtual bool SetAntialiasMode(wxAntialiasMode antialias) = 0;
717
718 // returns the current interpolation quality
GetInterpolationQuality()719 virtual wxInterpolationQuality GetInterpolationQuality() const { return m_interpolation; }
720
721 // sets the interpolation quality, returns true if it supported
722 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation) = 0;
723
724 // returns the current compositing operator
GetCompositionMode()725 virtual wxCompositionMode GetCompositionMode() const { return m_composition; }
726
727 // sets the compositing operator, returns true if it supported
728 virtual bool SetCompositionMode(wxCompositionMode op) = 0;
729
730 // returns the size of the graphics context in device coordinates
GetSize(wxDouble * width,wxDouble * height)731 void GetSize(wxDouble* width, wxDouble* height) const
732 {
733 if ( width )
734 *width = m_width;
735 if ( height )
736 *height = m_height;
737 }
738
739 // returns the resolution of the graphics context in device points per inch
740 virtual void GetDPI( wxDouble* dpiX, wxDouble* dpiY) const;
741
742 #if 0
743 // sets the current alpha on this context
744 virtual void SetAlpha( wxDouble alpha );
745
746 // returns the alpha on this context
747 virtual wxDouble GetAlpha() const;
748 #endif
749
750 // all rendering is done into a fully transparent temporary context
751 virtual void BeginLayer(wxDouble opacity) = 0;
752
753 // composites back the drawings into the context with the opacity given at
754 // the BeginLayer call
755 virtual void EndLayer() = 0;
756
757 //
758 // transformation : changes the current transformation matrix CTM of the context
759 //
760
761 // translate
762 virtual void Translate( wxDouble dx , wxDouble dy ) = 0;
763
764 // scale
765 virtual void Scale( wxDouble xScale , wxDouble yScale ) = 0;
766
767 // rotate (radians)
768 virtual void Rotate( wxDouble angle ) = 0;
769
770 // concatenates this transform with the current transform of this context
771 virtual void ConcatTransform( const wxGraphicsMatrix& matrix ) = 0;
772
773 // sets the transform of this context
774 virtual void SetTransform( const wxGraphicsMatrix& matrix ) = 0;
775
776 // gets the matrix of this context
777 virtual wxGraphicsMatrix GetTransform() const = 0;
778 //
779 // setting the paint
780 //
781
782 // sets the pen
783 virtual void SetPen( const wxGraphicsPen& pen );
784
785 void SetPen( const wxPen& pen );
786
787 // sets the brush for filling
788 virtual void SetBrush( const wxGraphicsBrush& brush );
789
790 void SetBrush( const wxBrush& brush );
791
792 // sets the font
793 virtual void SetFont( const wxGraphicsFont& font );
794
795 void SetFont( const wxFont& font, const wxColour& colour );
796
797
798 // strokes along a path with the current pen
799 virtual void StrokePath( const wxGraphicsPath& path ) = 0;
800
801 // fills a path with the current brush
802 virtual void FillPath( const wxGraphicsPath& path, wxPolygonFillMode fillStyle = wxODDEVEN_RULE ) = 0;
803
804 // draws a path by first filling and then stroking
805 virtual void DrawPath( const wxGraphicsPath& path, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
806
807 // paints a transparent rectangle (only useful for bitmaps or windows)
808 virtual void ClearRectangle(wxDouble x, wxDouble y, wxDouble w, wxDouble h);
809
810 //
811 // text
812 //
813
DrawText(const wxString & str,wxDouble x,wxDouble y)814 void DrawText( const wxString &str, wxDouble x, wxDouble y )
815 { DoDrawText(str, x, y); }
816
DrawText(const wxString & str,wxDouble x,wxDouble y,wxDouble angle)817 void DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle )
818 { DoDrawRotatedText(str, x, y, angle); }
819
DrawText(const wxString & str,wxDouble x,wxDouble y,const wxGraphicsBrush & backgroundBrush)820 void DrawText( const wxString &str, wxDouble x, wxDouble y,
821 const wxGraphicsBrush& backgroundBrush )
822 { DoDrawFilledText(str, x, y, backgroundBrush); }
823
DrawText(const wxString & str,wxDouble x,wxDouble y,wxDouble angle,const wxGraphicsBrush & backgroundBrush)824 void DrawText( const wxString &str, wxDouble x, wxDouble y,
825 wxDouble angle, const wxGraphicsBrush& backgroundBrush )
826 { DoDrawRotatedFilledText(str, x, y, angle, backgroundBrush); }
827
828
829 virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
830 wxDouble *descent = NULL, wxDouble *externalLeading = NULL ) const = 0;
831
832 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const = 0;
833
834 //
835 // image support
836 //
837
838 virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
839
840 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
841
842 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
843
844 //
845 // convenience methods
846 //
847
848 // strokes a single line
849 virtual void StrokeLine( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2);
850
851 // stroke lines connecting each of the points
852 virtual void StrokeLines( size_t n, const wxPoint2DDouble *points);
853
854 // stroke disconnected lines from begin to end points
855 virtual void StrokeLines( size_t n, const wxPoint2DDouble *beginPoints, const wxPoint2DDouble *endPoints);
856
857 // draws a polygon
858 virtual void DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
859
860 // draws a rectangle
861 virtual void DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
862
863 // draws an ellipse
864 virtual void DrawEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
865
866 // draws a rounded rectangle
867 virtual void DrawRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius);
868
869 // wrappers using wxPoint2DDouble TODO
870
871 // helper to determine if a 0.5 offset should be applied for the drawing operation
ShouldOffset()872 virtual bool ShouldOffset() const { return false; }
873
874 // indicates whether the context should try to offset for pixel boundaries, this only makes sense on
875 // bitmap devices like screen, by default this is turned off
876 virtual void EnableOffset(bool enable = true);
877
DisableOffset()878 void DisableOffset() { EnableOffset(false); }
OffsetEnabled()879 bool OffsetEnabled() const { return m_enableOffset; }
880
881 void SetContentScaleFactor(double contentScaleFactor);
GetContentScaleFactor()882 double GetContentScaleFactor() const { return m_contentScaleFactor; }
883
884 protected:
885 // These fields must be initialized in the derived class ctors.
886 wxDouble m_width,
887 m_height;
888
889 wxGraphicsPen m_pen;
890 wxGraphicsBrush m_brush;
891 wxGraphicsFont m_font;
892 wxAntialiasMode m_antialias;
893 wxCompositionMode m_composition;
894 wxInterpolationQuality m_interpolation;
895 bool m_enableOffset;
896
897 protected:
898 // implementations of overloaded public functions: we use different names
899 // for them to avoid the virtual function hiding problems in the derived
900 // classes
901 virtual wxGraphicsPen DoCreatePen(const wxGraphicsPenInfo& info) const;
902
903 virtual void DoDrawText(const wxString& str, wxDouble x, wxDouble y) = 0;
904 virtual void DoDrawRotatedText(const wxString& str, wxDouble x, wxDouble y,
905 wxDouble angle);
906 virtual void DoDrawFilledText(const wxString& str, wxDouble x, wxDouble y,
907 const wxGraphicsBrush& backgroundBrush);
908 virtual void DoDrawRotatedFilledText(const wxString& str,
909 wxDouble x, wxDouble y,
910 wxDouble angle,
911 const wxGraphicsBrush& backgroundBrush);
912
913 private:
914 // The associated window, if any, i.e. if one was passed directly to
915 // Create() or the associated window of the wxDC this context was created
916 // from.
917 wxWindow* const m_window;
918 double m_contentScaleFactor;
919
920 wxDECLARE_NO_COPY_CLASS(wxGraphicsContext);
921 wxDECLARE_ABSTRACT_CLASS(wxGraphicsContext);
922 };
923
924 #if 0
925
926 //
927 // A graphics figure allows to cache path, pen etc creations, also will be a basis for layering/grouping elements
928 //
929
930 class WXDLLIMPEXP_CORE wxGraphicsFigure : public wxGraphicsObject
931 {
932 public:
933 wxGraphicsFigure(wxGraphicsRenderer* renderer);
934
935 virtual ~wxGraphicsFigure();
936
937 void SetPath( wxGraphicsMatrix* matrix );
938
939 void SetMatrix( wxGraphicsPath* path);
940
941 // draws this object on the context
942 virtual void Draw( wxGraphicsContext* cg );
943
944 // returns the path of this object
945 wxGraphicsPath* GetPath() { return m_path; }
946
947 // returns the transformation matrix of this object, may be null if there is no transformation necessary
948 wxGraphicsMatrix* GetMatrix() { return m_matrix; }
949
950 private:
951 wxGraphicsMatrix* m_matrix;
952 wxGraphicsPath* m_path;
953
954 wxDECLARE_DYNAMIC_CLASS(wxGraphicsFigure);
955 };
956
957 #endif
958
959 //
960 // The graphics renderer is the instance corresponding to the rendering engine used, eg there is ONE core graphics renderer
961 // instance on OSX. This instance is pointed back to by all objects created by it. Therefore you can create eg additional
962 // paths at any point from a given matrix etc.
963 //
964
965 class WXDLLIMPEXP_CORE wxGraphicsRenderer : public wxObject
966 {
967 public:
wxGraphicsRenderer()968 wxGraphicsRenderer() {}
969
~wxGraphicsRenderer()970 virtual ~wxGraphicsRenderer() {}
971
972 static wxGraphicsRenderer* GetDefaultRenderer();
973
974 static wxGraphicsRenderer* GetCairoRenderer();
975
976 #ifdef __WXMSW__
977 #if wxUSE_GRAPHICS_GDIPLUS
978 static wxGraphicsRenderer* GetGDIPlusRenderer();
979 #endif
980
981 #if wxUSE_GRAPHICS_DIRECT2D
982 static wxGraphicsRenderer* GetDirect2DRenderer();
983 #endif
984 #endif
985
986 // Context
987
988 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc) = 0;
989 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc) = 0;
990 #if wxUSE_PRINTING_ARCHITECTURE
991 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc) = 0;
992 #endif
993 #ifdef __WXMSW__
994 #if wxUSE_ENH_METAFILE
995 virtual wxGraphicsContext * CreateContext( const wxEnhMetaFileDC& dc) = 0;
996 #endif
997 #endif
998
999 wxGraphicsContext* CreateContextFromUnknownDC(const wxDC& dc);
1000
1001 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context ) = 0;
1002
1003 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window ) = 0;
1004
1005 #ifdef __WXMSW__
1006 virtual wxGraphicsContext * CreateContextFromNativeHDC(WXHDC dc) = 0;
1007 #endif
1008
1009 virtual wxGraphicsContext * CreateContext( wxWindow* window ) = 0;
1010
1011 #if wxUSE_IMAGE
1012 virtual wxGraphicsContext * CreateContextFromImage(wxImage& image) = 0;
1013 #endif // wxUSE_IMAGE
1014
1015 // create a context that can be used for measuring texts only, no drawing allowed
1016 virtual wxGraphicsContext * CreateMeasuringContext() = 0;
1017
1018 // Path
1019
1020 virtual wxGraphicsPath CreatePath() = 0;
1021
1022 // Matrix
1023
1024 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1025 wxDouble tx=0.0, wxDouble ty=0.0) = 0;
1026
1027 // Paints
1028
1029 virtual wxGraphicsPen CreatePen(const wxGraphicsPenInfo& info) = 0;
1030
1031 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) = 0;
1032
1033 // Gradient brush creation functions may not honour all the stops specified
1034 // stops and use just its boundary colours (this is currently the case
1035 // under OS X)
1036 virtual wxGraphicsBrush
1037 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
1038 wxDouble x2, wxDouble y2,
1039 const wxGraphicsGradientStops& stops,
1040 const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix) = 0;
1041
1042 virtual wxGraphicsBrush
1043 CreateRadialGradientBrush(wxDouble startX, wxDouble startY,
1044 wxDouble endX, wxDouble endY,
1045 wxDouble radius,
1046 const wxGraphicsGradientStops& stops,
1047 const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix) = 0;
1048
1049 // sets the font
1050 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) = 0;
1051 virtual wxGraphicsFont CreateFont(double sizeInPixels,
1052 const wxString& facename,
1053 int flags = wxFONTFLAG_DEFAULT,
1054 const wxColour& col = *wxBLACK) = 0;
1055 virtual wxGraphicsFont CreateFontAtDPI(const wxFont& font,
1056 const wxRealPoint& dpi,
1057 const wxColour& col = *wxBLACK) = 0;
1058
1059 // create a native bitmap representation
1060 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ) = 0;
1061 #if wxUSE_IMAGE
1062 virtual wxGraphicsBitmap CreateBitmapFromImage(const wxImage& image) = 0;
1063 virtual wxImage CreateImageFromBitmap(const wxGraphicsBitmap& bmp) = 0;
1064 #endif // wxUSE_IMAGE
1065
1066 // create a graphics bitmap from a native bitmap
1067 virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap ) = 0;
1068
1069 // create a subimage from a native image representation
1070 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
1071
1072 virtual wxString GetName() const = 0;
1073 virtual void
1074 GetVersion(int* major, int* minor = NULL, int* micro = NULL) const = 0;
1075
1076 private:
1077 wxDECLARE_NO_COPY_CLASS(wxGraphicsRenderer);
1078 wxDECLARE_ABSTRACT_CLASS(wxGraphicsRenderer);
1079 };
1080
1081
1082 #if wxUSE_IMAGE
1083 inline
ConvertToImage()1084 wxImage wxGraphicsBitmap::ConvertToImage() const
1085 {
1086 wxGraphicsRenderer* renderer = GetRenderer();
1087 return renderer ? renderer->CreateImageFromBitmap(*this) : wxNullImage;
1088 }
1089 #endif // wxUSE_IMAGE
1090
1091 #endif // wxUSE_GRAPHICS_CONTEXT
1092
1093 #endif // _WX_GRAPHICS_H_
1094