dcmirror.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/dcmirror.h
  3. // Purpose: wxMirrorDC class
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 21.07.2003
  7. // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_DCMIRROR_H_
  11. #define _WX_DCMIRROR_H_
  12. #include "wx/dc.h"
  13. // ----------------------------------------------------------------------------
  14. // wxMirrorDC allows to write the same code for horz/vertical layout
  15. // ----------------------------------------------------------------------------
  16. class WXDLLIMPEXP_CORE wxMirrorDCImpl : public wxDCImpl
  17. {
  18. public:
  19. // constructs a mirror DC associated with the given real DC
  20. //
  21. // if mirror parameter is true, all vertical and horizontal coordinates are
  22. // exchanged, otherwise this class behaves in exactly the same way as a
  23. // plain DC
  24. wxMirrorDCImpl(wxDC *owner, wxDCImpl& dc, bool mirror)
  25. : wxDCImpl(owner),
  26. m_dc(dc)
  27. {
  28. m_mirror = mirror;
  29. }
  30. // wxDCBase operations
  31. virtual void Clear() { m_dc.Clear(); }
  32. virtual void SetFont(const wxFont& font) { m_dc.SetFont(font); }
  33. virtual void SetPen(const wxPen& pen) { m_dc.SetPen(pen); }
  34. virtual void SetBrush(const wxBrush& brush) { m_dc.SetBrush(brush); }
  35. virtual void SetBackground(const wxBrush& brush)
  36. { m_dc.SetBackground(brush); }
  37. virtual void SetBackgroundMode(int mode) { m_dc.SetBackgroundMode(mode); }
  38. #if wxUSE_PALETTE
  39. virtual void SetPalette(const wxPalette& palette)
  40. { m_dc.SetPalette(palette); }
  41. #endif // wxUSE_PALETTE
  42. virtual void DestroyClippingRegion() { m_dc.DestroyClippingRegion(); }
  43. virtual wxCoord GetCharHeight() const { return m_dc.GetCharHeight(); }
  44. virtual wxCoord GetCharWidth() const { return m_dc.GetCharWidth(); }
  45. virtual bool CanDrawBitmap() const { return m_dc.CanDrawBitmap(); }
  46. virtual bool CanGetTextExtent() const { return m_dc.CanGetTextExtent(); }
  47. virtual int GetDepth() const { return m_dc.GetDepth(); }
  48. virtual wxSize GetPPI() const { return m_dc.GetPPI(); }
  49. virtual bool IsOk() const { return m_dc.IsOk(); }
  50. virtual void SetMapMode(wxMappingMode mode) { m_dc.SetMapMode(mode); }
  51. virtual void SetUserScale(double x, double y)
  52. { m_dc.SetUserScale(GetX(x, y), GetY(x, y)); }
  53. virtual void SetLogicalOrigin(wxCoord x, wxCoord y)
  54. { m_dc.SetLogicalOrigin(GetX(x, y), GetY(x, y)); }
  55. virtual void SetDeviceOrigin(wxCoord x, wxCoord y)
  56. { m_dc.SetDeviceOrigin(GetX(x, y), GetY(x, y)); }
  57. virtual void SetAxisOrientation(bool xLeftRight, bool yBottomUp)
  58. { m_dc.SetAxisOrientation(GetX(xLeftRight, yBottomUp),
  59. GetY(xLeftRight, yBottomUp)); }
  60. virtual void SetLogicalFunction(wxRasterOperationMode function)
  61. { m_dc.SetLogicalFunction(function); }
  62. virtual void* GetHandle() const
  63. { return m_dc.GetHandle(); }
  64. protected:
  65. // returns x and y if not mirroring or y and x if mirroring
  66. wxCoord GetX(wxCoord x, wxCoord y) const { return m_mirror ? y : x; }
  67. wxCoord GetY(wxCoord x, wxCoord y) const { return m_mirror ? x : y; }
  68. double GetX(double x, double y) const { return m_mirror ? y : x; }
  69. double GetY(double x, double y) const { return m_mirror ? x : y; }
  70. bool GetX(bool x, bool y) const { return m_mirror ? y : x; }
  71. bool GetY(bool x, bool y) const { return m_mirror ? x : y; }
  72. // same thing but for pointers
  73. wxCoord *GetX(wxCoord *x, wxCoord *y) const { return m_mirror ? y : x; }
  74. wxCoord *GetY(wxCoord *x, wxCoord *y) const { return m_mirror ? x : y; }
  75. // exchange x and y components of all points in the array if necessary
  76. wxPoint* Mirror(int n, const wxPoint*& points) const
  77. {
  78. wxPoint* points_alloc = NULL;
  79. if ( m_mirror )
  80. {
  81. points_alloc = new wxPoint[n];
  82. for ( int i = 0; i < n; i++ )
  83. {
  84. points_alloc[i].x = points[i].y;
  85. points_alloc[i].y = points[i].x;
  86. }
  87. points = points_alloc;
  88. }
  89. return points_alloc;
  90. }
  91. // wxDCBase functions
  92. virtual bool DoFloodFill(wxCoord x, wxCoord y, const wxColour& col,
  93. wxFloodFillStyle style = wxFLOOD_SURFACE)
  94. {
  95. return m_dc.DoFloodFill(GetX(x, y), GetY(x, y), col, style);
  96. }
  97. virtual bool DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
  98. {
  99. return m_dc.DoGetPixel(GetX(x, y), GetY(x, y), col);
  100. }
  101. virtual void DoDrawPoint(wxCoord x, wxCoord y)
  102. {
  103. m_dc.DoDrawPoint(GetX(x, y), GetY(x, y));
  104. }
  105. virtual void DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
  106. {
  107. m_dc.DoDrawLine(GetX(x1, y1), GetY(x1, y1), GetX(x2, y2), GetY(x2, y2));
  108. }
  109. virtual void DoDrawArc(wxCoord x1, wxCoord y1,
  110. wxCoord x2, wxCoord y2,
  111. wxCoord xc, wxCoord yc)
  112. {
  113. wxFAIL_MSG( wxT("this is probably wrong") );
  114. m_dc.DoDrawArc(GetX(x1, y1), GetY(x1, y1),
  115. GetX(x2, y2), GetY(x2, y2),
  116. xc, yc);
  117. }
  118. virtual void DoDrawCheckMark(wxCoord x, wxCoord y,
  119. wxCoord w, wxCoord h)
  120. {
  121. m_dc.DoDrawCheckMark(GetX(x, y), GetY(x, y),
  122. GetX(w, h), GetY(w, h));
  123. }
  124. virtual void DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h,
  125. double sa, double ea)
  126. {
  127. wxFAIL_MSG( wxT("this is probably wrong") );
  128. m_dc.DoDrawEllipticArc(GetX(x, y), GetY(x, y),
  129. GetX(w, h), GetY(w, h),
  130. sa, ea);
  131. }
  132. virtual void DoDrawRectangle(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
  133. {
  134. m_dc.DoDrawRectangle(GetX(x, y), GetY(x, y), GetX(w, h), GetY(w, h));
  135. }
  136. virtual void DoDrawRoundedRectangle(wxCoord x, wxCoord y,
  137. wxCoord w, wxCoord h,
  138. double radius)
  139. {
  140. m_dc.DoDrawRoundedRectangle(GetX(x, y), GetY(x, y),
  141. GetX(w, h), GetY(w, h),
  142. radius);
  143. }
  144. virtual void DoDrawEllipse(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
  145. {
  146. m_dc.DoDrawEllipse(GetX(x, y), GetY(x, y), GetX(w, h), GetY(w, h));
  147. }
  148. virtual void DoCrossHair(wxCoord x, wxCoord y)
  149. {
  150. m_dc.DoCrossHair(GetX(x, y), GetY(x, y));
  151. }
  152. virtual void DoDrawIcon(const wxIcon& icon, wxCoord x, wxCoord y)
  153. {
  154. m_dc.DoDrawIcon(icon, GetX(x, y), GetY(x, y));
  155. }
  156. virtual void DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y,
  157. bool useMask = false)
  158. {
  159. m_dc.DoDrawBitmap(bmp, GetX(x, y), GetY(x, y), useMask);
  160. }
  161. virtual void DoDrawText(const wxString& text, wxCoord x, wxCoord y)
  162. {
  163. // this is never mirrored
  164. m_dc.DoDrawText(text, x, y);
  165. }
  166. virtual void DoDrawRotatedText(const wxString& text,
  167. wxCoord x, wxCoord y, double angle)
  168. {
  169. // this is never mirrored
  170. m_dc.DoDrawRotatedText(text, x, y, angle);
  171. }
  172. virtual bool DoBlit(wxCoord xdest, wxCoord ydest,
  173. wxCoord w, wxCoord h,
  174. wxDC *source, wxCoord xsrc, wxCoord ysrc,
  175. wxRasterOperationMode rop = wxCOPY,
  176. bool useMask = false,
  177. wxCoord xsrcMask = wxDefaultCoord, wxCoord ysrcMask = wxDefaultCoord)
  178. {
  179. return m_dc.DoBlit(GetX(xdest, ydest), GetY(xdest, ydest),
  180. GetX(w, h), GetY(w, h),
  181. source, GetX(xsrc, ysrc), GetY(xsrc, ysrc),
  182. rop, useMask,
  183. GetX(xsrcMask, ysrcMask), GetX(xsrcMask, ysrcMask));
  184. }
  185. virtual void DoGetSize(int *w, int *h) const
  186. {
  187. m_dc.DoGetSize(GetX(w, h), GetY(w, h));
  188. }
  189. virtual void DoGetSizeMM(int *w, int *h) const
  190. {
  191. m_dc.DoGetSizeMM(GetX(w, h), GetY(w, h));
  192. }
  193. virtual void DoDrawLines(int n, const wxPoint points[],
  194. wxCoord xoffset, wxCoord yoffset)
  195. {
  196. wxPoint* points_alloc = Mirror(n, points);
  197. m_dc.DoDrawLines(n, points,
  198. GetX(xoffset, yoffset), GetY(xoffset, yoffset));
  199. delete[] points_alloc;
  200. }
  201. virtual void DoDrawPolygon(int n, const wxPoint points[],
  202. wxCoord xoffset, wxCoord yoffset,
  203. wxPolygonFillMode fillStyle = wxODDEVEN_RULE)
  204. {
  205. wxPoint* points_alloc = Mirror(n, points);
  206. m_dc.DoDrawPolygon(n, points,
  207. GetX(xoffset, yoffset), GetY(xoffset, yoffset),
  208. fillStyle);
  209. delete[] points_alloc;
  210. }
  211. virtual void DoSetDeviceClippingRegion(const wxRegion& WXUNUSED(region))
  212. {
  213. wxFAIL_MSG( wxT("not implemented") );
  214. }
  215. virtual void DoSetClippingRegion(wxCoord x, wxCoord y,
  216. wxCoord w, wxCoord h)
  217. {
  218. m_dc.DoSetClippingRegion(GetX(x, y), GetY(x, y), GetX(w, h), GetY(w, h));
  219. }
  220. virtual void DoGetTextExtent(const wxString& string,
  221. wxCoord *x, wxCoord *y,
  222. wxCoord *descent = NULL,
  223. wxCoord *externalLeading = NULL,
  224. const wxFont *theFont = NULL) const
  225. {
  226. // never mirrored
  227. m_dc.DoGetTextExtent(string, x, y, descent, externalLeading, theFont);
  228. }
  229. private:
  230. wxDCImpl& m_dc;
  231. bool m_mirror;
  232. wxDECLARE_NO_COPY_CLASS(wxMirrorDCImpl);
  233. };
  234. class WXDLLIMPEXP_CORE wxMirrorDC : public wxDC
  235. {
  236. public:
  237. wxMirrorDC(wxDC& dc, bool mirror)
  238. : wxDC(new wxMirrorDCImpl(this, *dc.GetImpl(), mirror))
  239. {
  240. m_mirror = mirror;
  241. }
  242. // helper functions which may be useful for the users of this class
  243. wxSize Reflect(const wxSize& sizeOrig)
  244. {
  245. return m_mirror ? wxSize(sizeOrig.y, sizeOrig.x) : sizeOrig;
  246. }
  247. private:
  248. bool m_mirror;
  249. wxDECLARE_NO_COPY_CLASS(wxMirrorDC);
  250. };
  251. #endif // _WX_DCMIRROR_H_