graphics.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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. #ifndef _WX_GRAPHICS_H_
  11. #define _WX_GRAPHICS_H_
  12. #include "wx/defs.h"
  13. #if wxUSE_GRAPHICS_CONTEXT
  14. #include "wx/geometry.h"
  15. #include "wx/dynarray.h"
  16. #include "wx/dc.h"
  17. #include "wx/image.h"
  18. #include "wx/vector.h"
  19. enum wxAntialiasMode
  20. {
  21. wxANTIALIAS_NONE, // should be 0
  22. wxANTIALIAS_DEFAULT
  23. };
  24. enum wxInterpolationQuality
  25. {
  26. // default interpolation
  27. wxINTERPOLATION_DEFAULT,
  28. // no interpolation
  29. wxINTERPOLATION_NONE,
  30. // fast interpolation, suited for interactivity
  31. wxINTERPOLATION_FAST,
  32. // better quality
  33. wxINTERPOLATION_GOOD,
  34. // best quality, not suited for interactivity
  35. wxINTERPOLATION_BEST
  36. };
  37. enum wxCompositionMode
  38. {
  39. // R = Result, S = Source, D = Destination, premultiplied with alpha
  40. // Ra, Sa, Da their alpha components
  41. // classic Porter-Duff compositions
  42. // http://keithp.com/~keithp/porterduff/p253-porter.pdf
  43. wxCOMPOSITION_INVALID = -1, /* indicates invalid/unsupported mode */
  44. wxCOMPOSITION_CLEAR, /* R = 0 */
  45. wxCOMPOSITION_SOURCE, /* R = S */
  46. wxCOMPOSITION_OVER, /* R = S + D*(1 - Sa) */
  47. wxCOMPOSITION_IN, /* R = S*Da */
  48. wxCOMPOSITION_OUT, /* R = S*(1 - Da) */
  49. wxCOMPOSITION_ATOP, /* R = S*Da + D*(1 - Sa) */
  50. wxCOMPOSITION_DEST, /* R = D, essentially a noop */
  51. wxCOMPOSITION_DEST_OVER, /* R = S*(1 - Da) + D */
  52. wxCOMPOSITION_DEST_IN, /* R = D*Sa */
  53. wxCOMPOSITION_DEST_OUT, /* R = D*(1 - Sa) */
  54. wxCOMPOSITION_DEST_ATOP, /* R = S*(1 - Da) + D*Sa */
  55. wxCOMPOSITION_XOR, /* R = S*(1 - Da) + D*(1 - Sa) */
  56. // mathematical compositions
  57. wxCOMPOSITION_ADD /* R = S + D */
  58. };
  59. class WXDLLIMPEXP_FWD_CORE wxWindowDC;
  60. class WXDLLIMPEXP_FWD_CORE wxMemoryDC;
  61. #if wxUSE_PRINTING_ARCHITECTURE
  62. class WXDLLIMPEXP_FWD_CORE wxPrinterDC;
  63. #endif
  64. #ifdef __WXMSW__
  65. #if wxUSE_ENH_METAFILE
  66. class WXDLLIMPEXP_FWD_CORE wxEnhMetaFileDC;
  67. #endif
  68. #endif
  69. class WXDLLIMPEXP_FWD_CORE wxGraphicsContext;
  70. class WXDLLIMPEXP_FWD_CORE wxGraphicsPath;
  71. class WXDLLIMPEXP_FWD_CORE wxGraphicsMatrix;
  72. class WXDLLIMPEXP_FWD_CORE wxGraphicsFigure;
  73. class WXDLLIMPEXP_FWD_CORE wxGraphicsRenderer;
  74. class WXDLLIMPEXP_FWD_CORE wxGraphicsPen;
  75. class WXDLLIMPEXP_FWD_CORE wxGraphicsBrush;
  76. class WXDLLIMPEXP_FWD_CORE wxGraphicsFont;
  77. class WXDLLIMPEXP_FWD_CORE wxGraphicsBitmap;
  78. /*
  79. * notes about the graphics context apis
  80. *
  81. * angles : are measured in radians, 0.0 being in direction of positiv x axis, PI/2 being
  82. * in direction of positive y axis.
  83. */
  84. // Base class of all objects used for drawing in the new graphics API, the always point back to their
  85. // originating rendering engine, there is no dynamic unloading of a renderer currently allowed,
  86. // these references are not counted
  87. //
  88. // The data used by objects like graphics pens etc is ref counted, in order to avoid unnecessary expensive
  89. // duplication. Any operation on a shared instance that results in a modified state, uncouples this
  90. // instance from the other instances that were shared - using copy on write semantics
  91. //
  92. class WXDLLIMPEXP_FWD_CORE wxGraphicsObjectRefData;
  93. class WXDLLIMPEXP_FWD_CORE wxGraphicsBitmapData;
  94. class WXDLLIMPEXP_FWD_CORE wxGraphicsMatrixData;
  95. class WXDLLIMPEXP_FWD_CORE wxGraphicsPathData;
  96. class WXDLLIMPEXP_CORE wxGraphicsObject : public wxObject
  97. {
  98. public:
  99. wxGraphicsObject();
  100. wxGraphicsObject( wxGraphicsRenderer* renderer );
  101. virtual ~wxGraphicsObject();
  102. bool IsNull() const;
  103. // returns the renderer that was used to create this instance, or NULL if it has not been initialized yet
  104. wxGraphicsRenderer* GetRenderer() const;
  105. wxGraphicsObjectRefData* GetGraphicsData() const;
  106. protected:
  107. virtual wxObjectRefData* CreateRefData() const;
  108. virtual wxObjectRefData* CloneRefData(const wxObjectRefData* data) const;
  109. DECLARE_DYNAMIC_CLASS(wxGraphicsObject)
  110. };
  111. class WXDLLIMPEXP_CORE wxGraphicsPen : public wxGraphicsObject
  112. {
  113. public:
  114. wxGraphicsPen() {}
  115. virtual ~wxGraphicsPen() {}
  116. private:
  117. DECLARE_DYNAMIC_CLASS(wxGraphicsPen)
  118. };
  119. extern WXDLLIMPEXP_DATA_CORE(wxGraphicsPen) wxNullGraphicsPen;
  120. class WXDLLIMPEXP_CORE wxGraphicsBrush : public wxGraphicsObject
  121. {
  122. public:
  123. wxGraphicsBrush() {}
  124. virtual ~wxGraphicsBrush() {}
  125. private:
  126. DECLARE_DYNAMIC_CLASS(wxGraphicsBrush)
  127. };
  128. extern WXDLLIMPEXP_DATA_CORE(wxGraphicsBrush) wxNullGraphicsBrush;
  129. class WXDLLIMPEXP_CORE wxGraphicsFont : public wxGraphicsObject
  130. {
  131. public:
  132. wxGraphicsFont() {}
  133. virtual ~wxGraphicsFont() {}
  134. private:
  135. DECLARE_DYNAMIC_CLASS(wxGraphicsFont)
  136. };
  137. extern WXDLLIMPEXP_DATA_CORE(wxGraphicsFont) wxNullGraphicsFont;
  138. class WXDLLIMPEXP_CORE wxGraphicsBitmap : public wxGraphicsObject
  139. {
  140. public:
  141. wxGraphicsBitmap() {}
  142. virtual ~wxGraphicsBitmap() {}
  143. // Convert bitmap to wxImage: this is more efficient than converting to
  144. // wxBitmap first and then to wxImage and also works without X server
  145. // connection under Unix that wxBitmap requires.
  146. #if wxUSE_IMAGE
  147. wxImage ConvertToImage() const;
  148. #endif // wxUSE_IMAGE
  149. void* GetNativeBitmap() const;
  150. const wxGraphicsBitmapData* GetBitmapData() const
  151. { return (const wxGraphicsBitmapData*) GetRefData(); }
  152. wxGraphicsBitmapData* GetBitmapData()
  153. { return (wxGraphicsBitmapData*) GetRefData(); }
  154. private:
  155. DECLARE_DYNAMIC_CLASS(wxGraphicsBitmap)
  156. };
  157. extern WXDLLIMPEXP_DATA_CORE(wxGraphicsBitmap) wxNullGraphicsBitmap;
  158. class WXDLLIMPEXP_CORE wxGraphicsMatrix : public wxGraphicsObject
  159. {
  160. public:
  161. wxGraphicsMatrix() {}
  162. virtual ~wxGraphicsMatrix() {}
  163. // concatenates the matrix
  164. virtual void Concat( const wxGraphicsMatrix *t );
  165. void Concat( const wxGraphicsMatrix &t ) { Concat( &t ); }
  166. // sets the matrix to the respective values
  167. virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
  168. wxDouble tx=0.0, wxDouble ty=0.0);
  169. // gets the component valuess of the matrix
  170. virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
  171. wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
  172. // makes this the inverse matrix
  173. virtual void Invert();
  174. // returns true if the elements of the transformation matrix are equal ?
  175. virtual bool IsEqual( const wxGraphicsMatrix* t) const;
  176. bool IsEqual( const wxGraphicsMatrix& t) const { return IsEqual( &t ); }
  177. // return true if this is the identity matrix
  178. virtual bool IsIdentity() const;
  179. //
  180. // transformation
  181. //
  182. // add the translation to this matrix
  183. virtual void Translate( wxDouble dx , wxDouble dy );
  184. // add the scale to this matrix
  185. virtual void Scale( wxDouble xScale , wxDouble yScale );
  186. // add the rotation to this matrix (radians)
  187. virtual void Rotate( wxDouble angle );
  188. //
  189. // apply the transforms
  190. //
  191. // applies that matrix to the point
  192. virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
  193. // applies the matrix except for translations
  194. virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
  195. // returns the native representation
  196. virtual void * GetNativeMatrix() const;
  197. const wxGraphicsMatrixData* GetMatrixData() const
  198. { return (const wxGraphicsMatrixData*) GetRefData(); }
  199. wxGraphicsMatrixData* GetMatrixData()
  200. { return (wxGraphicsMatrixData*) GetRefData(); }
  201. private:
  202. DECLARE_DYNAMIC_CLASS(wxGraphicsMatrix)
  203. };
  204. extern WXDLLIMPEXP_DATA_CORE(wxGraphicsMatrix) wxNullGraphicsMatrix;
  205. class WXDLLIMPEXP_CORE wxGraphicsPath : public wxGraphicsObject
  206. {
  207. public:
  208. wxGraphicsPath() {}
  209. virtual ~wxGraphicsPath() {}
  210. //
  211. // These are the path primitives from which everything else can be constructed
  212. //
  213. // begins a new subpath at (x,y)
  214. virtual void MoveToPoint( wxDouble x, wxDouble y );
  215. void MoveToPoint( const wxPoint2DDouble& p);
  216. // adds a straight line from the current point to (x,y)
  217. virtual void AddLineToPoint( wxDouble x, wxDouble y );
  218. void AddLineToPoint( const wxPoint2DDouble& p);
  219. // adds a cubic Bezier curve from the current point, using two control points and an end point
  220. virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
  221. void AddCurveToPoint( const wxPoint2DDouble& c1, const wxPoint2DDouble& c2, const wxPoint2DDouble& e);
  222. // adds another path
  223. virtual void AddPath( const wxGraphicsPath& path );
  224. // closes the current sub-path
  225. virtual void CloseSubpath();
  226. // gets the last point of the current path, (0,0) if not yet set
  227. virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
  228. wxPoint2DDouble GetCurrentPoint() const;
  229. // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
  230. virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise );
  231. void AddArc( const wxPoint2DDouble& c, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise);
  232. //
  233. // These are convenience functions which - if not available natively will be assembled
  234. // using the primitives from above
  235. //
  236. // adds a quadratic Bezier curve from the current point, using a control point and an end point
  237. virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y );
  238. // appends a rectangle as a new closed subpath
  239. virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
  240. // appends an ellipsis as a new closed subpath fitting the passed rectangle
  241. virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r );
  242. // 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)
  243. virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r );
  244. // appends an ellipse
  245. virtual void AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
  246. // appends a rounded rectangle
  247. virtual void AddRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius);
  248. // returns the native path
  249. virtual void * GetNativePath() const;
  250. // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
  251. virtual void UnGetNativePath(void *p)const;
  252. // transforms each point of this path by the matrix
  253. virtual void Transform( const wxGraphicsMatrix& matrix );
  254. // gets the bounding box enclosing all points (possibly including control points)
  255. virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h)const;
  256. wxRect2DDouble GetBox()const;
  257. virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxODDEVEN_RULE)const;
  258. bool Contains( const wxPoint2DDouble& c, wxPolygonFillMode fillStyle = wxODDEVEN_RULE)const;
  259. const wxGraphicsPathData* GetPathData() const
  260. { return (const wxGraphicsPathData*) GetRefData(); }
  261. wxGraphicsPathData* GetPathData()
  262. { return (wxGraphicsPathData*) GetRefData(); }
  263. private:
  264. DECLARE_DYNAMIC_CLASS(wxGraphicsPath)
  265. };
  266. extern WXDLLIMPEXP_DATA_CORE(wxGraphicsPath) wxNullGraphicsPath;
  267. // Describes a single gradient stop.
  268. class wxGraphicsGradientStop
  269. {
  270. public:
  271. wxGraphicsGradientStop(wxColour col = wxTransparentColour,
  272. float pos = 0.)
  273. : m_col(col),
  274. m_pos(pos)
  275. {
  276. }
  277. // default copy ctor, assignment operator and dtor are ok
  278. const wxColour& GetColour() const { return m_col; }
  279. void SetColour(const wxColour& col) { m_col = col; }
  280. float GetPosition() const { return m_pos; }
  281. void SetPosition(float pos)
  282. {
  283. wxASSERT_MSG( pos >= 0 && pos <= 1, "invalid gradient stop position" );
  284. m_pos = pos;
  285. }
  286. private:
  287. // The colour of this gradient band.
  288. wxColour m_col;
  289. // Its starting position: 0 is the beginning and 1 is the end.
  290. float m_pos;
  291. };
  292. // A collection of gradient stops ordered by their positions (from lowest to
  293. // highest). The first stop (index 0, position 0.0) is always the starting
  294. // colour and the last one (index GetCount() - 1, position 1.0) is the end
  295. // colour.
  296. class WXDLLIMPEXP_CORE wxGraphicsGradientStops
  297. {
  298. public:
  299. wxGraphicsGradientStops(wxColour startCol = wxTransparentColour,
  300. wxColour endCol = wxTransparentColour)
  301. {
  302. // we can't use Add() here as it relies on having start/end stops as
  303. // first/last array elements so do it manually
  304. m_stops.push_back(wxGraphicsGradientStop(startCol, 0.f));
  305. m_stops.push_back(wxGraphicsGradientStop(endCol, 1.f));
  306. }
  307. // default copy ctor, assignment operator and dtor are ok for this class
  308. // Add a stop in correct order.
  309. void Add(const wxGraphicsGradientStop& stop);
  310. void Add(wxColour col, float pos) { Add(wxGraphicsGradientStop(col, pos)); }
  311. // Get the number of stops.
  312. size_t GetCount() const { return m_stops.size(); }
  313. // Return the stop at the given index (which must be valid).
  314. wxGraphicsGradientStop Item(unsigned n) const { return m_stops.at(n); }
  315. // Get/set start and end colours.
  316. void SetStartColour(wxColour col)
  317. { m_stops[0].SetColour(col); }
  318. wxColour GetStartColour() const
  319. { return m_stops[0].GetColour(); }
  320. void SetEndColour(wxColour col)
  321. { m_stops[m_stops.size() - 1].SetColour(col); }
  322. wxColour GetEndColour() const
  323. { return m_stops[m_stops.size() - 1].GetColour(); }
  324. private:
  325. // All the stops stored in ascending order of positions.
  326. wxVector<wxGraphicsGradientStop> m_stops;
  327. };
  328. class WXDLLIMPEXP_CORE wxGraphicsContext : public wxGraphicsObject
  329. {
  330. public:
  331. wxGraphicsContext(wxGraphicsRenderer* renderer);
  332. virtual ~wxGraphicsContext();
  333. static wxGraphicsContext* Create( const wxWindowDC& dc);
  334. static wxGraphicsContext * Create( const wxMemoryDC& dc);
  335. #if wxUSE_PRINTING_ARCHITECTURE
  336. static wxGraphicsContext * Create( const wxPrinterDC& dc);
  337. #endif
  338. #ifdef __WXMSW__
  339. #if wxUSE_ENH_METAFILE
  340. static wxGraphicsContext * Create( const wxEnhMetaFileDC& dc);
  341. #endif
  342. #endif
  343. static wxGraphicsContext* CreateFromNative( void * context );
  344. static wxGraphicsContext* CreateFromNativeWindow( void * window );
  345. static wxGraphicsContext* Create( wxWindow* window );
  346. #if wxUSE_IMAGE
  347. // Create a context for drawing onto a wxImage. The image life time must be
  348. // greater than that of the context itself as when the context is destroyed
  349. // it will copy its contents to the specified image.
  350. static wxGraphicsContext* Create(wxImage& image);
  351. #endif // wxUSE_IMAGE
  352. // create a context that can be used for measuring texts only, no drawing allowed
  353. static wxGraphicsContext * Create();
  354. // begin a new document (relevant only for printing / pdf etc) if there is a progress dialog, message will be shown
  355. virtual bool StartDoc( const wxString& message );
  356. // done with that document (relevant only for printing / pdf etc)
  357. virtual void EndDoc();
  358. // opens a new page (relevant only for printing / pdf etc) with the given size in points
  359. // (if both are null the default page size will be used)
  360. virtual void StartPage( wxDouble width = 0, wxDouble height = 0 );
  361. // ends the current page (relevant only for printing / pdf etc)
  362. virtual void EndPage();
  363. // make sure that the current content of this context is immediately visible
  364. virtual void Flush();
  365. wxGraphicsPath CreatePath() const;
  366. virtual wxGraphicsPen CreatePen(const wxPen& pen) const;
  367. virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) const;
  368. // sets the brush to a linear gradient, starting at (x1,y1) and ending at
  369. // (x2,y2) with the given boundary colours or the specified stops
  370. wxGraphicsBrush
  371. CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
  372. wxDouble x2, wxDouble y2,
  373. const wxColour& c1, const wxColour& c2) const;
  374. wxGraphicsBrush
  375. CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
  376. wxDouble x2, wxDouble y2,
  377. const wxGraphicsGradientStops& stops) const;
  378. // sets the brush to a radial gradient originating at (xo,yc) and ending
  379. // on a circle around (xc,yc) with the given radius; the colours may be
  380. // specified by just the two extremes or the full array of gradient stops
  381. wxGraphicsBrush
  382. CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
  383. wxDouble xc, wxDouble yc, wxDouble radius,
  384. const wxColour& oColor, const wxColour& cColor) const;
  385. wxGraphicsBrush
  386. CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
  387. wxDouble xc, wxDouble yc, wxDouble radius,
  388. const wxGraphicsGradientStops& stops) const;
  389. // creates a font
  390. virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) const;
  391. virtual wxGraphicsFont CreateFont(double sizeInPixels,
  392. const wxString& facename,
  393. int flags = wxFONTFLAG_DEFAULT,
  394. const wxColour& col = *wxBLACK) const;
  395. // create a native bitmap representation
  396. virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ) const;
  397. #if wxUSE_IMAGE
  398. wxGraphicsBitmap CreateBitmapFromImage(const wxImage& image) const;
  399. #endif // wxUSE_IMAGE
  400. // create a native bitmap representation
  401. virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) const;
  402. // create a 'native' matrix corresponding to these values
  403. virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
  404. wxDouble tx=0.0, wxDouble ty=0.0) const;
  405. wxGraphicsMatrix CreateMatrix( const wxAffineMatrix2DBase& mat ) const
  406. {
  407. wxMatrix2D mat2D;
  408. wxPoint2DDouble tr;
  409. mat.Get(&mat2D, &tr);
  410. return CreateMatrix(mat2D.m_11, mat2D.m_12, mat2D.m_21, mat2D.m_22,
  411. tr.m_x, tr.m_y);
  412. }
  413. // push the current state of the context, ie the transformation matrix on a stack
  414. virtual void PushState() = 0;
  415. // pops a stored state from the stack
  416. virtual void PopState() = 0;
  417. // clips drawings to the region intersected with the current clipping region
  418. virtual void Clip( const wxRegion &region ) = 0;
  419. // clips drawings to the rect intersected with the current clipping region
  420. virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
  421. // resets the clipping to original extent
  422. virtual void ResetClip() = 0;
  423. // returns the native context
  424. virtual void * GetNativeContext() = 0;
  425. // returns the current shape antialiasing mode
  426. virtual wxAntialiasMode GetAntialiasMode() const { return m_antialias; }
  427. // sets the antialiasing mode, returns true if it supported
  428. virtual bool SetAntialiasMode(wxAntialiasMode antialias) = 0;
  429. // returns the current interpolation quality
  430. virtual wxInterpolationQuality GetInterpolationQuality() const { return m_interpolation; }
  431. // sets the interpolation quality, returns true if it supported
  432. virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation) = 0;
  433. // returns the current compositing operator
  434. virtual wxCompositionMode GetCompositionMode() const { return m_composition; }
  435. // sets the compositing operator, returns true if it supported
  436. virtual bool SetCompositionMode(wxCompositionMode op) = 0;
  437. // returns the size of the graphics context in device coordinates
  438. void GetSize(wxDouble* width, wxDouble* height) const
  439. {
  440. if ( width )
  441. *width = m_width;
  442. if ( height )
  443. *height = m_height;
  444. }
  445. // returns the resolution of the graphics context in device points per inch
  446. virtual void GetDPI( wxDouble* dpiX, wxDouble* dpiY);
  447. #if 0
  448. // sets the current alpha on this context
  449. virtual void SetAlpha( wxDouble alpha );
  450. // returns the alpha on this context
  451. virtual wxDouble GetAlpha() const;
  452. #endif
  453. // all rendering is done into a fully transparent temporary context
  454. virtual void BeginLayer(wxDouble opacity) = 0;
  455. // composites back the drawings into the context with the opacity given at
  456. // the BeginLayer call
  457. virtual void EndLayer() = 0;
  458. //
  459. // transformation : changes the current transformation matrix CTM of the context
  460. //
  461. // translate
  462. virtual void Translate( wxDouble dx , wxDouble dy ) = 0;
  463. // scale
  464. virtual void Scale( wxDouble xScale , wxDouble yScale ) = 0;
  465. // rotate (radians)
  466. virtual void Rotate( wxDouble angle ) = 0;
  467. // concatenates this transform with the current transform of this context
  468. virtual void ConcatTransform( const wxGraphicsMatrix& matrix ) = 0;
  469. // sets the transform of this context
  470. virtual void SetTransform( const wxGraphicsMatrix& matrix ) = 0;
  471. // gets the matrix of this context
  472. virtual wxGraphicsMatrix GetTransform() const = 0;
  473. //
  474. // setting the paint
  475. //
  476. // sets the pen
  477. virtual void SetPen( const wxGraphicsPen& pen );
  478. void SetPen( const wxPen& pen );
  479. // sets the brush for filling
  480. virtual void SetBrush( const wxGraphicsBrush& brush );
  481. void SetBrush( const wxBrush& brush );
  482. // sets the font
  483. virtual void SetFont( const wxGraphicsFont& font );
  484. void SetFont( const wxFont& font, const wxColour& colour );
  485. // strokes along a path with the current pen
  486. virtual void StrokePath( const wxGraphicsPath& path ) = 0;
  487. // fills a path with the current brush
  488. virtual void FillPath( const wxGraphicsPath& path, wxPolygonFillMode fillStyle = wxODDEVEN_RULE ) = 0;
  489. // draws a path by first filling and then stroking
  490. virtual void DrawPath( const wxGraphicsPath& path, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
  491. //
  492. // text
  493. //
  494. void DrawText( const wxString &str, wxDouble x, wxDouble y )
  495. { DoDrawText(str, x, y); }
  496. void DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle )
  497. { DoDrawRotatedText(str, x, y, angle); }
  498. void DrawText( const wxString &str, wxDouble x, wxDouble y,
  499. const wxGraphicsBrush& backgroundBrush )
  500. { DoDrawFilledText(str, x, y, backgroundBrush); }
  501. void DrawText( const wxString &str, wxDouble x, wxDouble y,
  502. wxDouble angle, const wxGraphicsBrush& backgroundBrush )
  503. { DoDrawRotatedFilledText(str, x, y, angle, backgroundBrush); }
  504. virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
  505. wxDouble *descent = NULL, wxDouble *externalLeading = NULL ) const = 0;
  506. virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const = 0;
  507. //
  508. // image support
  509. //
  510. virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
  511. virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
  512. virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
  513. //
  514. // convenience methods
  515. //
  516. // strokes a single line
  517. virtual void StrokeLine( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2);
  518. // stroke lines connecting each of the points
  519. virtual void StrokeLines( size_t n, const wxPoint2DDouble *points);
  520. // stroke disconnected lines from begin to end points
  521. virtual void StrokeLines( size_t n, const wxPoint2DDouble *beginPoints, const wxPoint2DDouble *endPoints);
  522. // draws a polygon
  523. virtual void DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
  524. // draws a rectangle
  525. virtual void DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
  526. // draws an ellipse
  527. virtual void DrawEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h);
  528. // draws a rounded rectangle
  529. virtual void DrawRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius);
  530. // wrappers using wxPoint2DDouble TODO
  531. // helper to determine if a 0.5 offset should be applied for the drawing operation
  532. virtual bool ShouldOffset() const { return false; }
  533. // indicates whether the context should try to offset for pixel boundaries, this only makes sense on
  534. // bitmap devices like screen, by default this is turned off
  535. virtual void EnableOffset(bool enable = true);
  536. void DisableOffset() { EnableOffset(false); }
  537. bool OffsetEnabled() { return m_enableOffset; }
  538. protected:
  539. // These fields must be initialized in the derived class ctors.
  540. wxDouble m_width,
  541. m_height;
  542. wxGraphicsPen m_pen;
  543. wxGraphicsBrush m_brush;
  544. wxGraphicsFont m_font;
  545. wxAntialiasMode m_antialias;
  546. wxCompositionMode m_composition;
  547. wxInterpolationQuality m_interpolation;
  548. bool m_enableOffset;
  549. protected:
  550. // implementations of overloaded public functions: we use different names
  551. // for them to avoid the virtual function hiding problems in the derived
  552. // classes
  553. virtual void DoDrawText(const wxString& str, wxDouble x, wxDouble y) = 0;
  554. virtual void DoDrawRotatedText(const wxString& str, wxDouble x, wxDouble y,
  555. wxDouble angle);
  556. virtual void DoDrawFilledText(const wxString& str, wxDouble x, wxDouble y,
  557. const wxGraphicsBrush& backgroundBrush);
  558. virtual void DoDrawRotatedFilledText(const wxString& str,
  559. wxDouble x, wxDouble y,
  560. wxDouble angle,
  561. const wxGraphicsBrush& backgroundBrush);
  562. wxDECLARE_NO_COPY_CLASS(wxGraphicsContext);
  563. DECLARE_ABSTRACT_CLASS(wxGraphicsContext)
  564. };
  565. #if 0
  566. //
  567. // A graphics figure allows to cache path, pen etc creations, also will be a basis for layering/grouping elements
  568. //
  569. class WXDLLIMPEXP_CORE wxGraphicsFigure : public wxGraphicsObject
  570. {
  571. public:
  572. wxGraphicsFigure(wxGraphicsRenderer* renderer);
  573. virtual ~wxGraphicsFigure();
  574. void SetPath( wxGraphicsMatrix* matrix );
  575. void SetMatrix( wxGraphicsPath* path);
  576. // draws this object on the context
  577. virtual void Draw( wxGraphicsContext* cg );
  578. // returns the path of this object
  579. wxGraphicsPath* GetPath() { return m_path; }
  580. // returns the transformation matrix of this object, may be null if there is no transformation necessary
  581. wxGraphicsMatrix* GetMatrix() { return m_matrix; }
  582. private:
  583. wxGraphicsMatrix* m_matrix;
  584. wxGraphicsPath* m_path;
  585. DECLARE_DYNAMIC_CLASS(wxGraphicsFigure)
  586. };
  587. #endif
  588. //
  589. // The graphics renderer is the instance corresponding to the rendering engine used, eg there is ONE core graphics renderer
  590. // instance on OSX. This instance is pointed back to by all objects created by it. Therefore you can create eg additional
  591. // paths at any point from a given matrix etc.
  592. //
  593. class WXDLLIMPEXP_CORE wxGraphicsRenderer : public wxObject
  594. {
  595. public:
  596. wxGraphicsRenderer() {}
  597. virtual ~wxGraphicsRenderer() {}
  598. static wxGraphicsRenderer* GetDefaultRenderer();
  599. static wxGraphicsRenderer* GetCairoRenderer();
  600. // Context
  601. virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc) = 0;
  602. virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc) = 0;
  603. #if wxUSE_PRINTING_ARCHITECTURE
  604. virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc) = 0;
  605. #endif
  606. #ifdef __WXMSW__
  607. #if wxUSE_ENH_METAFILE
  608. virtual wxGraphicsContext * CreateContext( const wxEnhMetaFileDC& dc) = 0;
  609. #endif
  610. #endif
  611. virtual wxGraphicsContext * CreateContextFromNativeContext( void * context ) = 0;
  612. virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window ) = 0;
  613. virtual wxGraphicsContext * CreateContext( wxWindow* window ) = 0;
  614. #if wxUSE_IMAGE
  615. virtual wxGraphicsContext * CreateContextFromImage(wxImage& image) = 0;
  616. #endif // wxUSE_IMAGE
  617. // create a context that can be used for measuring texts only, no drawing allowed
  618. virtual wxGraphicsContext * CreateMeasuringContext() = 0;
  619. // Path
  620. virtual wxGraphicsPath CreatePath() = 0;
  621. // Matrix
  622. virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
  623. wxDouble tx=0.0, wxDouble ty=0.0) = 0;
  624. // Paints
  625. virtual wxGraphicsPen CreatePen(const wxPen& pen) = 0;
  626. virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) = 0;
  627. // Gradient brush creation functions may not honour all the stops specified
  628. // stops and use just its boundary colours (this is currently the case
  629. // under OS X)
  630. virtual wxGraphicsBrush
  631. CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
  632. wxDouble x2, wxDouble y2,
  633. const wxGraphicsGradientStops& stops) = 0;
  634. virtual wxGraphicsBrush
  635. CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
  636. wxDouble xc, wxDouble yc,
  637. wxDouble radius,
  638. const wxGraphicsGradientStops& stops) = 0;
  639. // sets the font
  640. virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) = 0;
  641. virtual wxGraphicsFont CreateFont(double sizeInPixels,
  642. const wxString& facename,
  643. int flags = wxFONTFLAG_DEFAULT,
  644. const wxColour& col = *wxBLACK) = 0;
  645. // create a native bitmap representation
  646. virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ) = 0;
  647. #if wxUSE_IMAGE
  648. virtual wxGraphicsBitmap CreateBitmapFromImage(const wxImage& image) = 0;
  649. virtual wxImage CreateImageFromBitmap(const wxGraphicsBitmap& bmp) = 0;
  650. #endif // wxUSE_IMAGE
  651. // create a graphics bitmap from a native bitmap
  652. virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap ) = 0;
  653. // create a subimage from a native image representation
  654. virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) = 0;
  655. private:
  656. wxDECLARE_NO_COPY_CLASS(wxGraphicsRenderer);
  657. DECLARE_ABSTRACT_CLASS(wxGraphicsRenderer)
  658. };
  659. #if wxUSE_IMAGE
  660. inline
  661. wxImage wxGraphicsBitmap::ConvertToImage() const
  662. {
  663. wxGraphicsRenderer* renderer = GetRenderer();
  664. return renderer ? renderer->CreateImageFromBitmap(*this) : wxNullImage;
  665. }
  666. #endif // wxUSE_IMAGE
  667. #endif // wxUSE_GRAPHICS_CONTEXT
  668. #endif // _WX_GRAPHICS_H_