dc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msw/dc.h
  3. // Purpose: wxDC class
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 01/02/97
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_MSW_DC_H_
  11. #define _WX_MSW_DC_H_
  12. #include "wx/defs.h"
  13. #include "wx/dc.h"
  14. // ---------------------------------------------------------------------------
  15. // macros
  16. // ---------------------------------------------------------------------------
  17. #if wxUSE_DC_CACHEING
  18. /*
  19. * Cached blitting, maintaining a cache
  20. * of bitmaps required for transparent blitting
  21. * instead of constant creation/deletion
  22. */
  23. class wxDCCacheEntry: public wxObject
  24. {
  25. public:
  26. wxDCCacheEntry(WXHBITMAP hBitmap, int w, int h, int depth);
  27. wxDCCacheEntry(WXHDC hDC, int depth);
  28. virtual ~wxDCCacheEntry();
  29. WXHBITMAP m_bitmap;
  30. WXHDC m_dc;
  31. int m_width;
  32. int m_height;
  33. int m_depth;
  34. };
  35. #endif
  36. // this is an ABC: use one of the derived classes to create a DC associated
  37. // with a window, screen, printer and so on
  38. class WXDLLIMPEXP_CORE wxMSWDCImpl: public wxDCImpl
  39. {
  40. public:
  41. wxMSWDCImpl(wxDC *owner, WXHDC hDC);
  42. virtual ~wxMSWDCImpl();
  43. // implement base class pure virtuals
  44. // ----------------------------------
  45. virtual void Clear();
  46. virtual bool StartDoc(const wxString& message);
  47. virtual void EndDoc();
  48. virtual void StartPage();
  49. virtual void EndPage();
  50. virtual void SetFont(const wxFont& font);
  51. virtual void SetPen(const wxPen& pen);
  52. virtual void SetBrush(const wxBrush& brush);
  53. virtual void SetBackground(const wxBrush& brush);
  54. virtual void SetBackgroundMode(int mode);
  55. #if wxUSE_PALETTE
  56. virtual void SetPalette(const wxPalette& palette);
  57. #endif // wxUSE_PALETTE
  58. virtual void DestroyClippingRegion();
  59. virtual wxCoord GetCharHeight() const;
  60. virtual wxCoord GetCharWidth() const;
  61. virtual bool CanDrawBitmap() const;
  62. virtual bool CanGetTextExtent() const;
  63. virtual int GetDepth() const;
  64. virtual wxSize GetPPI() const;
  65. virtual void SetMapMode(wxMappingMode mode);
  66. virtual void SetUserScale(double x, double y);
  67. virtual void SetLogicalScale(double x, double y);
  68. virtual void SetLogicalOrigin(wxCoord x, wxCoord y);
  69. virtual void SetDeviceOrigin(wxCoord x, wxCoord y);
  70. virtual void SetAxisOrientation(bool xLeftRight, bool yBottomUp);
  71. #if wxUSE_DC_TRANSFORM_MATRIX
  72. virtual bool CanUseTransformMatrix() const;
  73. virtual bool SetTransformMatrix(const wxAffineMatrix2D& matrix);
  74. virtual wxAffineMatrix2D GetTransformMatrix() const;
  75. virtual void ResetTransformMatrix();
  76. #endif // wxUSE_DC_TRANSFORM_MATRIX
  77. virtual void SetLogicalFunction(wxRasterOperationMode function);
  78. // implementation from now on
  79. // --------------------------
  80. virtual void SetRop(WXHDC cdc);
  81. virtual void SelectOldObjects(WXHDC dc);
  82. void SetWindow(wxWindow *win)
  83. {
  84. m_window = win;
  85. #if wxUSE_PALETTE
  86. // if we have palettes use the correct one for this window
  87. InitializePalette();
  88. #endif // wxUSE_PALETTE
  89. }
  90. WXHDC GetHDC() const { return m_hDC; }
  91. void SetHDC(WXHDC dc, bool bOwnsDC = false)
  92. {
  93. m_hDC = dc;
  94. m_bOwnsDC = bOwnsDC;
  95. // we might have a pre existing clipping region, make sure that we
  96. // return it if asked -- but avoid calling ::GetClipBox() right now as
  97. // it could be unnecessary wasteful
  98. m_clipping = true;
  99. m_clipX1 =
  100. m_clipX2 = 0;
  101. }
  102. void* GetHandle() const { return (void*)GetHDC(); }
  103. const wxBitmap& GetSelectedBitmap() const { return m_selectedBitmap; }
  104. wxBitmap& GetSelectedBitmap() { return m_selectedBitmap; }
  105. // update the internal clip box variables
  106. void UpdateClipBox();
  107. #if wxUSE_DC_CACHEING
  108. static wxDCCacheEntry* FindBitmapInCache(WXHDC hDC, int w, int h);
  109. static wxDCCacheEntry* FindDCInCache(wxDCCacheEntry* notThis, WXHDC hDC);
  110. static void AddToBitmapCache(wxDCCacheEntry* entry);
  111. static void AddToDCCache(wxDCCacheEntry* entry);
  112. static void ClearCache();
  113. #endif
  114. // RTL related functions
  115. // ---------------------
  116. // get or change the layout direction (LTR or RTL) for this dc,
  117. // wxLayout_Default is returned if layout direction is not supported
  118. virtual wxLayoutDirection GetLayoutDirection() const;
  119. virtual void SetLayoutDirection(wxLayoutDirection dir);
  120. protected:
  121. void Init()
  122. {
  123. m_bOwnsDC = false;
  124. m_hDC = NULL;
  125. m_oldBitmap = NULL;
  126. m_oldPen = NULL;
  127. m_oldBrush = NULL;
  128. m_oldFont = NULL;
  129. #if wxUSE_PALETTE
  130. m_oldPalette = NULL;
  131. #endif // wxUSE_PALETTE
  132. }
  133. // create an uninitialized DC: this should be only used by the derived
  134. // classes
  135. wxMSWDCImpl( wxDC *owner ) : wxDCImpl( owner ) { Init(); }
  136. void RealizeScaleAndOrigin();
  137. public:
  138. virtual void DoGetFontMetrics(int *height,
  139. int *ascent,
  140. int *descent,
  141. int *internalLeading,
  142. int *externalLeading,
  143. int *averageWidth) const;
  144. virtual void DoGetTextExtent(const wxString& string,
  145. wxCoord *x, wxCoord *y,
  146. wxCoord *descent = NULL,
  147. wxCoord *externalLeading = NULL,
  148. const wxFont *theFont = NULL) const;
  149. virtual bool DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const;
  150. virtual bool DoFloodFill(wxCoord x, wxCoord y, const wxColour& col,
  151. wxFloodFillStyle style = wxFLOOD_SURFACE);
  152. virtual void DoGradientFillLinear(const wxRect& rect,
  153. const wxColour& initialColour,
  154. const wxColour& destColour,
  155. wxDirection nDirection = wxEAST);
  156. virtual bool DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const;
  157. virtual void DoDrawPoint(wxCoord x, wxCoord y);
  158. virtual void DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2);
  159. virtual void DoDrawArc(wxCoord x1, wxCoord y1,
  160. wxCoord x2, wxCoord y2,
  161. wxCoord xc, wxCoord yc);
  162. virtual void DoDrawCheckMark(wxCoord x, wxCoord y,
  163. wxCoord width, wxCoord height);
  164. virtual void DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h,
  165. double sa, double ea);
  166. virtual void DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height);
  167. virtual void DoDrawRoundedRectangle(wxCoord x, wxCoord y,
  168. wxCoord width, wxCoord height,
  169. double radius);
  170. virtual void DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height);
  171. #if wxUSE_SPLINES && !defined(__WXWINCE__)
  172. virtual void DoDrawSpline(const wxPointList *points);
  173. #endif
  174. virtual void DoCrossHair(wxCoord x, wxCoord y);
  175. virtual void DoDrawIcon(const wxIcon& icon, wxCoord x, wxCoord y);
  176. virtual void DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y,
  177. bool useMask = false);
  178. virtual void DoDrawText(const wxString& text, wxCoord x, wxCoord y);
  179. virtual void DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y,
  180. double angle);
  181. virtual bool DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
  182. wxDC *source, wxCoord xsrc, wxCoord ysrc,
  183. wxRasterOperationMode rop = wxCOPY, bool useMask = false,
  184. wxCoord xsrcMask = wxDefaultCoord, wxCoord ysrcMask = wxDefaultCoord);
  185. virtual bool DoStretchBlit(wxCoord xdest, wxCoord ydest,
  186. wxCoord dstWidth, wxCoord dstHeight,
  187. wxDC *source,
  188. wxCoord xsrc, wxCoord ysrc,
  189. wxCoord srcWidth, wxCoord srcHeight,
  190. wxRasterOperationMode rop = wxCOPY, bool useMask = false,
  191. wxCoord xsrcMask = wxDefaultCoord, wxCoord ysrcMask = wxDefaultCoord);
  192. virtual void DoSetClippingRegion(wxCoord x, wxCoord y,
  193. wxCoord width, wxCoord height);
  194. virtual void DoSetDeviceClippingRegion(const wxRegion& region);
  195. virtual void DoGetClippingBox(wxCoord *x, wxCoord *y,
  196. wxCoord *w, wxCoord *h) const;
  197. virtual void DoGetSizeMM(int* width, int* height) const;
  198. virtual void DoDrawLines(int n, const wxPoint points[],
  199. wxCoord xoffset, wxCoord yoffset);
  200. virtual void DoDrawPolygon(int n, const wxPoint points[],
  201. wxCoord xoffset, wxCoord yoffset,
  202. wxPolygonFillMode fillStyle = wxODDEVEN_RULE);
  203. virtual void DoDrawPolyPolygon(int n, const int count[], const wxPoint points[],
  204. wxCoord xoffset, wxCoord yoffset,
  205. wxPolygonFillMode fillStyle = wxODDEVEN_RULE);
  206. virtual wxBitmap DoGetAsBitmap(const wxRect *subrect) const
  207. {
  208. return subrect == NULL ? GetSelectedBitmap()
  209. : GetSelectedBitmap().GetSubBitmap(*subrect);
  210. }
  211. #if wxUSE_PALETTE
  212. // MSW specific, select a logical palette into the HDC
  213. // (tell windows to translate pixel from other palettes to our custom one
  214. // and vice versa)
  215. // Realize tells it to also reset the system palette to this one.
  216. void DoSelectPalette(bool realize = false);
  217. // Find out what palette our parent window has, then select it into the dc
  218. void InitializePalette();
  219. #endif // wxUSE_PALETTE
  220. protected:
  221. // common part of DoDrawText() and DoDrawRotatedText()
  222. void DrawAnyText(const wxString& text, wxCoord x, wxCoord y);
  223. // common part of DoSetClippingRegion() and DoSetDeviceClippingRegion()
  224. void SetClippingHrgn(WXHRGN hrgn);
  225. // implementation of DoGetSize() for wxScreen/PrinterDC: this simply
  226. // returns the size of the entire device this DC is associated with
  227. //
  228. // notice that we intentionally put it in a separate function instead of
  229. // DoGetSize() itself because we want it to remain pure virtual both
  230. // because each derived class should take care to define it as needed (this
  231. // implementation is not at all always appropriate) and because we want
  232. // wxDC to be an ABC to prevent it from being created directly
  233. void GetDeviceSize(int *width, int *height) const;
  234. // MSW-specific member variables
  235. // -----------------------------
  236. // the window associated with this DC (may be NULL)
  237. wxWindow *m_canvas;
  238. wxBitmap m_selectedBitmap;
  239. // TRUE => DeleteDC() in dtor, FALSE => only ReleaseDC() it
  240. bool m_bOwnsDC:1;
  241. // our HDC
  242. WXHDC m_hDC;
  243. // Store all old GDI objects when do a SelectObject, so we can select them
  244. // back in (this unselecting user's objects) so we can safely delete the
  245. // DC.
  246. WXHBITMAP m_oldBitmap;
  247. WXHPEN m_oldPen;
  248. WXHBRUSH m_oldBrush;
  249. WXHFONT m_oldFont;
  250. #if wxUSE_PALETTE
  251. WXHPALETTE m_oldPalette;
  252. #endif // wxUSE_PALETTE
  253. #if wxUSE_DC_CACHEING
  254. static wxObjectList sm_bitmapCache;
  255. static wxObjectList sm_dcCache;
  256. #endif
  257. DECLARE_CLASS(wxMSWDCImpl)
  258. wxDECLARE_NO_COPY_CLASS(wxMSWDCImpl);
  259. };
  260. // ----------------------------------------------------------------------------
  261. // wxDCTemp: a wxDC which doesn't free the given HDC (used by wxWidgets
  262. // only/mainly)
  263. // ----------------------------------------------------------------------------
  264. class WXDLLIMPEXP_CORE wxDCTempImpl : public wxMSWDCImpl
  265. {
  266. public:
  267. // construct a temporary DC with the specified HDC and size (it should be
  268. // specified whenever we know it for this HDC)
  269. wxDCTempImpl(wxDC *owner, WXHDC hdc, const wxSize& size )
  270. : wxMSWDCImpl( owner, hdc ),
  271. m_size(size)
  272. {
  273. }
  274. virtual ~wxDCTempImpl()
  275. {
  276. // prevent base class dtor from freeing it
  277. SetHDC((WXHDC)NULL);
  278. }
  279. virtual void DoGetSize(int *w, int *h) const
  280. {
  281. wxASSERT_MSG( m_size.IsFullySpecified(),
  282. wxT("size of this DC hadn't been set and is unknown") );
  283. if ( w )
  284. *w = m_size.x;
  285. if ( h )
  286. *h = m_size.y;
  287. }
  288. private:
  289. // size of this DC must be explicitly set by SetSize() as we have no way to
  290. // find it ourselves
  291. const wxSize m_size;
  292. wxDECLARE_NO_COPY_CLASS(wxDCTempImpl);
  293. };
  294. class WXDLLIMPEXP_CORE wxDCTemp : public wxDC
  295. {
  296. public:
  297. wxDCTemp(WXHDC hdc, const wxSize& size = wxDefaultSize)
  298. : wxDC(new wxDCTempImpl(this, hdc, size))
  299. {
  300. }
  301. };
  302. #endif // _WX_MSW_DC_H_