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.

280 lines
6.5 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1998
  4. *
  5. * TITLE: PSUTIL.H
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 5/28/1998
  12. *
  13. * DESCRIPTION: Various utility functions we use in more than one place
  14. *
  15. *******************************************************************************/
  16. #ifndef __PSUTIL_H_INCLUDED
  17. #define __PSUTIL_H_INCLUDED
  18. #include <windows.h>
  19. #include <simstr.h>
  20. #if !defined(ARRAYSIZE)
  21. #define ARRAYSIZE(x) (sizeof((x))/sizeof((x)[0]))
  22. #endif
  23. #if !defined(SETFormatEtc)
  24. #define SETFormatEtc(fe, cf, asp, td, med, li) \
  25. {\
  26. (fe).cfFormat=cf;\
  27. (fe).dwAspect=asp;\
  28. (fe).ptd=td;\
  29. (fe).tymed=med;\
  30. (fe).lindex=li;\
  31. };
  32. #endif
  33. #if !defined(SETDefFormatEtc)
  34. #define SETDefFormatEtc(fe, cf, med) \
  35. {\
  36. (fe).cfFormat=cf;\
  37. (fe).dwAspect=DVASPECT_CONTENT;\
  38. (fe).ptd=NULL;\
  39. (fe).tymed=med;\
  40. (fe).lindex=-1;\
  41. };
  42. #endif
  43. namespace PrintScanUtil
  44. {
  45. template <class T>
  46. T Absolute( const T &m )
  47. {
  48. return((m < 0) ? -m : m);
  49. }
  50. template <class T>
  51. T Max( const T &m, const T &n )
  52. {
  53. return((m > n) ? m : n);
  54. }
  55. template <class T>
  56. T Min( const T &m, const T &n )
  57. {
  58. return((m < n) ? m : n);
  59. }
  60. template <class T>
  61. T GetMinimum( const T& nDesired, const T& nMin, const T& nStep )
  62. {
  63. T nResult = Max<T>( nMin, nDesired );
  64. if (nStep)
  65. nResult = nResult + (nResult - nMin) % nStep;
  66. return nResult;
  67. }
  68. inline bool ScreenToClient( HWND hwnd, RECT *prc )
  69. {
  70. return (::MapWindowPoints( NULL, hwnd, reinterpret_cast<POINT*>(prc), 2 ) != 0);
  71. }
  72. inline bool ClientToScreen( HWND hwnd, RECT *prc )
  73. {
  74. return (::MapWindowPoints( hwnd, NULL, reinterpret_cast<POINT*>(prc), 2 ) != 0);
  75. }
  76. inline bool ScreenToClient( HWND hwnd, RECT &rc )
  77. {
  78. return ScreenToClient( hwnd, &rc );
  79. }
  80. inline bool ClientToScreen( HWND hwnd, RECT &rc )
  81. {
  82. return ClientToScreen( hwnd, &rc );
  83. }
  84. inline int RectWidth( const RECT &rc )
  85. {
  86. return (rc.right - rc.left);
  87. }
  88. inline int RectHeight( const RECT &rc )
  89. {
  90. return (rc.bottom - rc.top);
  91. }
  92. inline LONGLONG PowerOfTwo( int nCount )
  93. {
  94. return(LONGLONG)1 << nCount;
  95. }
  96. inline int MulDivNoRound( int nNumber, int nNumerator, int nDenominator )
  97. {
  98. return(int)(((LONGLONG)nNumber * nNumerator) / nDenominator);
  99. }
  100. inline SIZE ScalePreserveAspectRatio( int nAvailX, int nAvailY, int nItemX, int nItemY )
  101. {
  102. SIZE sizeResult = { nAvailX, nAvailY };
  103. if (nItemX && nItemY)
  104. {
  105. //
  106. // Width is greater than height. X is the constraining factor
  107. //
  108. if (nAvailY*nItemX > nAvailX*nItemY)
  109. {
  110. sizeResult.cy = MulDivNoRound(nItemY,nAvailX,nItemX);
  111. }
  112. //
  113. // Height is greater than width. Y is the constraining factor
  114. //
  115. else
  116. {
  117. sizeResult.cx = MulDivNoRound(nItemX,nAvailY,nItemY);
  118. }
  119. }
  120. return sizeResult;
  121. }
  122. inline bool GetBitmapSize( HBITMAP hBitmap, SIZE &sizeBitmap )
  123. {
  124. bool bResult = false;
  125. BITMAP Bitmap = {0};
  126. if (GetObject(hBitmap,sizeof(Bitmap),&Bitmap))
  127. {
  128. sizeBitmap.cx = Bitmap.bmWidth;
  129. sizeBitmap.cy = Bitmap.bmHeight;
  130. bResult = true;
  131. }
  132. return bResult;
  133. }
  134. //
  135. // Get the size of an icon
  136. //
  137. inline bool GetIconSize( HICON hIcon, SIZE &sizeIcon )
  138. {
  139. //
  140. // Assume failure
  141. //
  142. bool bResult = false;
  143. //
  144. // Get the icon information
  145. //
  146. ICONINFO IconInfo = {0};
  147. if (GetIconInfo( hIcon, &IconInfo ))
  148. {
  149. //
  150. // Get one of the bitmaps
  151. //
  152. BITMAP bm;
  153. if (GetObject( IconInfo.hbmColor, sizeof(bm), &bm ))
  154. {
  155. //
  156. // Save the size of the icon
  157. //
  158. sizeIcon.cx = bm.bmWidth;
  159. sizeIcon.cy = bm.bmHeight;
  160. //
  161. // Everything worked
  162. //
  163. bResult = true;
  164. }
  165. //
  166. // Free the bitmaps
  167. //
  168. if (IconInfo.hbmMask)
  169. {
  170. DeleteObject(IconInfo.hbmMask);
  171. }
  172. if (IconInfo.hbmColor)
  173. {
  174. DeleteObject(IconInfo.hbmColor);
  175. }
  176. }
  177. return bResult;
  178. }
  179. inline void Enable( HWND hWnd, bool bEnable )
  180. {
  181. if (hWnd && IsWindow(hWnd))
  182. {
  183. if (!IsWindowEnabled(hWnd) && bEnable)
  184. {
  185. ::EnableWindow( hWnd, TRUE );
  186. }
  187. else if (IsWindowEnabled(hWnd) && !bEnable)
  188. {
  189. ::EnableWindow( hWnd, FALSE );
  190. }
  191. }
  192. }
  193. inline void Enable( HWND hWnd, int nChildId, bool bEnable )
  194. {
  195. if (hWnd && IsWindow(hWnd))
  196. {
  197. Enable(GetDlgItem(hWnd,nChildId),bEnable);
  198. }
  199. }
  200. inline int CalculateImageListColorDepth(void)
  201. {
  202. //
  203. // Let's assume worst case
  204. //
  205. int nColorDepth = 4;
  206. HDC hDC = GetDC( NULL );
  207. if (hDC)
  208. {
  209. //
  210. // Calculate the color depth for the display
  211. //
  212. nColorDepth = GetDeviceCaps( hDC, BITSPIXEL ) * GetDeviceCaps( hDC, PLANES );
  213. ReleaseDC( NULL, hDC );
  214. }
  215. //
  216. // Get the correct image list color depth
  217. //
  218. int nImageListColorDepth;
  219. switch (nColorDepth)
  220. {
  221. case 4:
  222. case 8:
  223. nImageListColorDepth = ILC_COLOR4;
  224. break;
  225. case 16:
  226. nImageListColorDepth = ILC_COLOR16;
  227. break;
  228. case 24:
  229. nImageListColorDepth = ILC_COLOR24;
  230. break;
  231. case 32:
  232. nImageListColorDepth = ILC_COLOR32;
  233. break;
  234. default:
  235. nImageListColorDepth = ILC_COLOR;
  236. }
  237. return nImageListColorDepth;
  238. }
  239. }
  240. #endif // __PSUTIL_H_INCLUDED