Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

362 lines
6.9 KiB

  1. //-------------------------------------------------------------------------
  2. // Autos.h - resource holder classes that automatically return their resource
  3. // resources in the destructors. These can be declared on the stack
  4. // to free resources whenever the enclosing block is exited or
  5. // as class member variables that free the associated resources
  6. // when the containing object is destroyed.
  7. //-------------------------------------------------------------------------
  8. #ifndef _AUTOS_H_
  9. #define _AUTOS_H_
  10. //-------------------------------------------------------------------------
  11. #include "errors.h"
  12. //-------------------------------------------------------------------------
  13. class COptionalDC
  14. {
  15. public:
  16. COptionalDC(HDC hdcOpt)
  17. {
  18. _hdc = hdcOpt;
  19. _fReleaseDC = FALSE;
  20. if (! _hdc)
  21. {
  22. _hdc = GetWindowDC(NULL);
  23. if (_hdc)
  24. {
  25. _fReleaseDC = TRUE;
  26. }
  27. }
  28. }
  29. operator HDC()
  30. {
  31. return _hdc;
  32. }
  33. ~COptionalDC()
  34. {
  35. if (_fReleaseDC)
  36. {
  37. ReleaseDC(NULL, _hdc);
  38. }
  39. }
  40. private:
  41. HDC _hdc;
  42. BOOL _fReleaseDC;
  43. };
  44. //-------------------------------------------------------------------------
  45. template <class T>
  46. class CAutoGDI
  47. {
  48. public:
  49. CAutoGDI(T Value=NULL)
  50. {
  51. _Handle = Value;
  52. }
  53. ~CAutoGDI()
  54. {
  55. if (_Handle)
  56. DeleteObject(_Handle);
  57. }
  58. T & operator = (T Value)
  59. {
  60. if (_Handle)
  61. DeleteObject(_Handle);
  62. _Handle = Value;
  63. return _Handle;
  64. }
  65. operator T() const
  66. {
  67. return _Handle;
  68. }
  69. protected:
  70. T _Handle;
  71. };
  72. //------------------------------------------------------------------------------------
  73. class CAutoDC
  74. {
  75. public:
  76. CAutoDC(HDC Value=NULL)
  77. {
  78. _hdc = Value;
  79. _fOldPen = FALSE;
  80. _fOldBrush = FALSE;
  81. _fOldBitmap = FALSE;
  82. _fOldFont = FALSE;
  83. _fOldRegion = FALSE;
  84. }
  85. ~CAutoDC()
  86. {
  87. RestoreObjects();
  88. }
  89. HDC & operator = (HDC &Value)
  90. {
  91. if (_hdc)
  92. RestoreObjects();
  93. _hdc = Value;
  94. return _hdc;
  95. }
  96. operator HDC() const
  97. {
  98. return _hdc;
  99. }
  100. void RestoreObjects()
  101. {
  102. if (_fOldBitmap)
  103. {
  104. SelectObject(_hdc, _hOldBitmap);
  105. _fOldBitmap = FALSE;
  106. }
  107. if (_fOldFont)
  108. {
  109. SelectObject(_hdc, _hOldFont);
  110. _fOldFont = FALSE;
  111. }
  112. if (_fOldPen)
  113. {
  114. SelectObject(_hdc, _hOldPen);
  115. _fOldPen = FALSE;
  116. }
  117. if (_fOldBrush)
  118. {
  119. SelectObject(_hdc, _hOldBrush);
  120. _fOldBrush = FALSE;
  121. }
  122. if (_fOldRegion)
  123. {
  124. SelectObject(_hdc, _hOldRegion);
  125. _fOldRegion = FALSE;
  126. }
  127. }
  128. inline HBITMAP SelectBitmap(HBITMAP hValue)
  129. {
  130. if (! _fOldBitmap)
  131. {
  132. _fOldBitmap = TRUE;
  133. _hOldBitmap = (HBITMAP)SelectObject(_hdc, hValue);
  134. return _hOldBitmap;
  135. }
  136. return (HBITMAP)SelectObject(_hdc, hValue);
  137. }
  138. inline HFONT SelectFont(HFONT hValue)
  139. {
  140. if (! _fOldFont)
  141. {
  142. _fOldFont = TRUE;
  143. _hOldFont = (HFONT)SelectObject(_hdc, hValue);
  144. return _hOldFont;
  145. }
  146. return (HFONT)SelectObject(_hdc, hValue);
  147. }
  148. inline HBRUSH SelectBrush(HBRUSH hValue)
  149. {
  150. if (! _fOldBrush)
  151. {
  152. _fOldBrush = TRUE;
  153. _hOldBrush = (HBRUSH)SelectObject(_hdc, hValue);
  154. return _hOldBrush;
  155. }
  156. return (HBRUSH) SelectObject(_hdc, hValue);
  157. }
  158. inline HPEN SelectPen(HPEN hValue)
  159. {
  160. if (! _fOldPen)
  161. {
  162. _fOldPen = TRUE;
  163. _hOldPen = (HPEN)SelectObject(_hdc, hValue);
  164. return _hOldPen;
  165. }
  166. return (HPEN)SelectObject(_hdc, hValue);
  167. }
  168. inline HRGN SelectRegion(HRGN hValue)
  169. {
  170. if (! _fOldRegion)
  171. {
  172. _fOldRegion = TRUE;
  173. _hOldRegion = (HRGN)SelectObject(_hdc, hValue);
  174. return _hOldRegion;
  175. }
  176. return (HRGN)SelectObject(_hdc, hValue);
  177. }
  178. protected:
  179. HDC _hdc;
  180. BOOL _fOldBitmap;
  181. BOOL _fOldFont;
  182. BOOL _fOldBrush;
  183. BOOL _fOldPen;
  184. BOOL _fOldRegion;
  185. HBITMAP _hOldBitmap;
  186. HFONT _hOldFont;
  187. HBRUSH _hOldBrush;
  188. HPEN _hOldPen;
  189. HRGN _hOldRegion;
  190. };
  191. //------------------------------------------------------------------------------------
  192. class CAutoCS
  193. {
  194. public:
  195. CAutoCS(CRITICAL_SECTION *pcs)
  196. {
  197. _pcs = pcs;
  198. EnterCriticalSection(_pcs);
  199. }
  200. ~CAutoCS()
  201. {
  202. LeaveCriticalSection(_pcs);
  203. }
  204. protected:
  205. CRITICAL_SECTION *_pcs;
  206. };
  207. //------------------------------------------------------------------------------------
  208. class CSaveClipRegion
  209. {
  210. public:
  211. CSaveClipRegion()
  212. {
  213. _hRegion = NULL;
  214. _fSaved = FALSE;
  215. }
  216. HRESULT Save(HDC hdc)
  217. {
  218. HRESULT hr;
  219. int iRetVal;
  220. if (! _hRegion)
  221. {
  222. _hRegion = CreateRectRgn(0, 0, 1, 1);
  223. if (! _hRegion)
  224. {
  225. hr = MakeErrorLast();
  226. goto exit;
  227. }
  228. }
  229. iRetVal = GetClipRgn(hdc, _hRegion);
  230. if (iRetVal == -1)
  231. {
  232. hr = MakeErrorLast();
  233. goto exit;
  234. }
  235. if (iRetVal == 0) // no previous region
  236. {
  237. DeleteObject(_hRegion);
  238. _hRegion = NULL;
  239. }
  240. _fSaved = TRUE;
  241. hr = S_OK;
  242. exit:
  243. return hr;
  244. }
  245. HRESULT Restore(HDC hdc)
  246. {
  247. if (_fSaved)
  248. {
  249. //---- works for both NULL and valid _hRegion ----
  250. SelectClipRgn(hdc, _hRegion);
  251. }
  252. return S_OK;
  253. }
  254. ~CSaveClipRegion()
  255. {
  256. if (_hRegion)
  257. {
  258. DeleteObject(_hRegion);
  259. _hRegion = NULL;
  260. }
  261. }
  262. protected:
  263. HRGN _hRegion;
  264. BOOL _fSaved;
  265. };
  266. //------------------------------------------------------------------------------------
  267. template <class T>
  268. class CAutoArrayPtr
  269. {
  270. public:
  271. CAutoArrayPtr(T *pValue=NULL)
  272. {
  273. _pMem = pValue;
  274. }
  275. ~CAutoArrayPtr()
  276. {
  277. if (_pMem)
  278. delete [] _pMem;
  279. }
  280. T * operator = (T *pValue)
  281. {
  282. if (_pMem)
  283. delete [] _pMem;
  284. _pMem = pValue;
  285. return _pMem;
  286. }
  287. T & operator [] (int iIndex) const
  288. {
  289. return _pMem[iIndex];
  290. }
  291. operator T*() const
  292. {
  293. return _pMem;
  294. }
  295. bool operator!() const
  296. {
  297. return (_pMem == NULL);
  298. }
  299. T** operator&()
  300. {
  301. return &_pMem;
  302. }
  303. protected:
  304. T *_pMem;
  305. };
  306. //------------------------------------------------------------------------------------
  307. #endif // _AUTOS_H_
  308. //------------------------------------------------------------------------------------