Leaked source code of windows server 2003
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.

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