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.

407 lines
9.8 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1994 - 1996.
  5. //
  6. // File: util.cxx
  7. //
  8. // Contents: Utility functions
  9. //
  10. // History: 4-30-1997 DavidMun Created
  11. //
  12. //---------------------------------------------------------------------------
  13. #include "..\pch\headers.hxx"
  14. #pragma hdrstop
  15. #include "myheaders.hxx"
  16. //+--------------------------------------------------------------------------
  17. //
  18. // Function: IsDialogClass
  19. //
  20. // Synopsis: Return TRUE if [hwnd]'s window class is the dialog class.
  21. //
  22. // History: 5-19-1997 DavidMun Created
  23. //
  24. //---------------------------------------------------------------------------
  25. BOOL
  26. IsDialogClass(
  27. HWND hwnd)
  28. {
  29. TCHAR tszClassName[20]; // looking for "#32770"
  30. if (!GetClassName(hwnd, tszClassName, ARRAYLEN(tszClassName)))
  31. {
  32. DEBUG_OUT_LASTERROR;
  33. tszClassName[0] = TEXT('\0');
  34. }
  35. return 0 == _tcscmp(tszClassName, TEXT("#32770"));
  36. }
  37. //+--------------------------------------------------------------------------
  38. //
  39. // Function: LoadStr
  40. //
  41. // Synopsis: Load string with resource id [ids] into buffer [tszBuf],
  42. // which is of size [cchBuf] characters.
  43. //
  44. // Arguments: [ids] - string to load
  45. // [tszBuf] - buffer for string
  46. // [cchBuf] - size of buffer
  47. // [tszDefault] - NULL or string to use if load fails
  48. //
  49. // Returns: S_OK or error from LoadString
  50. //
  51. // Modifies: *[tszBuf]
  52. //
  53. // History: 12-11-1996 DavidMun Created
  54. //
  55. // Notes: If the load fails and no default is supplied, [tszBuf] is
  56. // set to an empty string.
  57. //
  58. //---------------------------------------------------------------------------
  59. HRESULT
  60. LoadStr(
  61. ULONG ids,
  62. LPTSTR tszBuf,
  63. ULONG cchBuf,
  64. LPCTSTR tszDefault)
  65. {
  66. HRESULT hr = S_OK;
  67. ULONG cchLoaded;
  68. cchLoaded = LoadString(g_hInstance, ids, tszBuf, cchBuf);
  69. if (!cchLoaded)
  70. {
  71. DEBUG_OUT_LASTERROR;
  72. hr = HRESULT_FROM_LASTERROR;
  73. if (tszDefault)
  74. {
  75. lstrcpyn(tszBuf, tszDefault, cchBuf);
  76. }
  77. else
  78. {
  79. *tszBuf = TEXT('\0');
  80. }
  81. }
  82. return hr;
  83. }
  84. #ifdef WIZARD97
  85. //+--------------------------------------------------------------------------
  86. //
  87. // Function: Is256ColorSupported
  88. //
  89. // Synopsis: Return TRUE if this machine supports 256 color bitmaps
  90. //
  91. // History: 5-20-1997 DavidMun Stolen from wizard97 sample code
  92. //
  93. //---------------------------------------------------------------------------
  94. BOOL
  95. Is256ColorSupported()
  96. {
  97. BOOL bRetval = FALSE;
  98. HDC hdc = GetDC(NULL);
  99. if (hdc)
  100. {
  101. if (GetDeviceCaps(hdc, BITSPIXEL) >= 8)
  102. {
  103. bRetval = TRUE;
  104. }
  105. ReleaseDC(NULL, hdc);
  106. }
  107. return bRetval;
  108. }
  109. #endif // WIZARD97
  110. //+--------------------------------------------------------------------------
  111. //
  112. // Function: FillInStartDateTime
  113. //
  114. // Synopsis: Fill [pTrigger]'s starting date and time values from the
  115. // values in the date/time picker controls.
  116. //
  117. // Arguments: [hwndDatePick] - handle to control with start date
  118. // [hwndTimePick] - handle to control with start time
  119. // [pTrigger] - trigger to init
  120. //
  121. // Modifies: *[pTrigger]
  122. //
  123. // History: 5-20-1997 DavidMun Created
  124. //
  125. //---------------------------------------------------------------------------
  126. VOID
  127. FillInStartDateTime(
  128. HWND hwndDatePick,
  129. HWND hwndTimePick,
  130. TASK_TRIGGER *pTrigger)
  131. {
  132. SYSTEMTIME st;
  133. DateTime_GetSystemtime(hwndDatePick, &st);
  134. pTrigger->wBeginYear = st.wYear;
  135. pTrigger->wBeginMonth = st.wMonth;
  136. pTrigger->wBeginDay = st.wDay;
  137. DateTime_GetSystemtime(hwndTimePick, &st);
  138. pTrigger->wStartHour = st.wHour;
  139. pTrigger->wStartMinute = st.wMinute;
  140. }
  141. #ifdef WIZARD95
  142. //+--------------------------------------------------------------------------
  143. //
  144. // Function: CreateDIBPalette
  145. //
  146. // Synopsis: Create palette based on bitmap info
  147. //
  148. // Arguments: [lpbmi] - bitmap info
  149. // [lpiNumColors] - number of colors in palette
  150. //
  151. // Returns: handle to created palette, or NULL on error
  152. //
  153. // History: 5-22-1997 DavidMun Taken directly from sdk sample
  154. //
  155. // Notes: Caller must DeleteObject returned palette.
  156. //
  157. //---------------------------------------------------------------------------
  158. HPALETTE
  159. CreateDIBPalette(
  160. LPBITMAPINFO lpbmi,
  161. LPINT lpiNumColors)
  162. {
  163. LPBITMAPINFOHEADER lpbi;
  164. LPLOGPALETTE lpPal;
  165. HANDLE hLogPal;
  166. HPALETTE hPal = NULL;
  167. int i;
  168. lpbi = (LPBITMAPINFOHEADER)lpbmi;
  169. if (lpbi->biBitCount <= 8)
  170. {
  171. *lpiNumColors = (1 << lpbi->biBitCount);
  172. }
  173. else
  174. {
  175. DEBUG_OUT((DEB_ITRACE, "no palette needed\n"));
  176. *lpiNumColors = 0; // No palette needed for 24 BPP DIB
  177. }
  178. if (*lpiNumColors)
  179. {
  180. hLogPal = GlobalAlloc(GHND,
  181. sizeof (LOGPALETTE) + sizeof (PALETTEENTRY)
  182. * (*lpiNumColors));
  183. if (!hLogPal)
  184. {
  185. DEBUG_OUT_HRESULT(E_OUTOFMEMORY);
  186. return NULL;
  187. }
  188. lpPal = (LPLOGPALETTE) GlobalLock (hLogPal);
  189. lpPal->palVersion = 0x300;
  190. lpPal->palNumEntries = (WORD)*lpiNumColors;
  191. for (i = 0; i < *lpiNumColors; i++)
  192. {
  193. lpPal->palPalEntry[i].peRed = lpbmi->bmiColors[i].rgbRed;
  194. lpPal->palPalEntry[i].peGreen = lpbmi->bmiColors[i].rgbGreen;
  195. lpPal->palPalEntry[i].peBlue = lpbmi->bmiColors[i].rgbBlue;
  196. lpPal->palPalEntry[i].peFlags = 0;
  197. }
  198. hPal = CreatePalette (lpPal);
  199. if (!hPal)
  200. {
  201. DEBUG_OUT_LASTERROR;
  202. }
  203. GlobalUnlock (hLogPal);
  204. GlobalFree (hLogPal);
  205. }
  206. return hPal;
  207. }
  208. //+--------------------------------------------------------------------------
  209. //
  210. // Function: LoadResourceBitmap
  211. //
  212. // Synopsis: Load the bitmap with resource id [idBitmap] and put its
  213. // palette in *[phPalette].
  214. //
  215. // Arguments: [idBitmap] - resource id of bitmap to load
  216. // [phPalette] - filled with bitmap's palette
  217. //
  218. // Returns: Device dependent bitmap with palette mapped to system's,
  219. // or NULL on error.
  220. //
  221. // Modifies: *[phPalette]
  222. //
  223. // History: 5-22-1997 DavidMun Created from sdk sample
  224. //
  225. // Notes: Caller must DeleteObject returned bitmap and palette. On
  226. // error, *[phPalette] is NULL.
  227. //
  228. //---------------------------------------------------------------------------
  229. HBITMAP
  230. LoadResourceBitmap(
  231. ULONG idBitmap,
  232. HPALETTE *phPalette)
  233. {
  234. TRACE_FUNCTION(LoadResourceBitmap);
  235. HRESULT hr = E_FAIL;
  236. HBITMAP hBitmapFinal = NULL;
  237. HDC hdc = NULL;
  238. //
  239. // Init out pointer for failure case
  240. //
  241. *phPalette = NULL;
  242. do
  243. {
  244. HRSRC hRsrc = FindResource(g_hInstance,
  245. MAKEINTRESOURCE(idBitmap),
  246. RT_BITMAP);
  247. if (!hRsrc)
  248. {
  249. DEBUG_OUT_LASTERROR;
  250. break;
  251. }
  252. //
  253. // Load the resource; note win32 will automatically unload it
  254. //
  255. HGLOBAL hGlobal = LoadResource(g_hInstance, hRsrc);
  256. if (!hGlobal)
  257. {
  258. DEBUG_OUT_LASTERROR;
  259. break;
  260. }
  261. //
  262. // Convert the loaded handle into a bitmap handle. Again, win32
  263. // will automatically unlock this resource.
  264. //
  265. LPBITMAPINFOHEADER pbih = (LPBITMAPINFOHEADER) LockResource(hGlobal);
  266. if (!pbih)
  267. {
  268. DEBUG_OUT_LASTERROR;
  269. break;
  270. }
  271. //
  272. // Get the screen dc to do the palette mapping with
  273. //
  274. hdc = GetDC(NULL);
  275. if (!hdc)
  276. {
  277. DEBUG_OUT_LASTERROR;
  278. break;
  279. }
  280. //
  281. // Create a palette that can be used on this computer's display and
  282. // which will represent the DIB's colors as accurately as possible.
  283. //
  284. int iNumColors;
  285. *phPalette = CreateDIBPalette((LPBITMAPINFO)pbih, &iNumColors);
  286. if (!*phPalette)
  287. {
  288. DEBUG_OUT_LASTERROR;
  289. break;
  290. }
  291. VERIFY(SelectPalette(hdc, *phPalette, FALSE));
  292. UINT uiMapped = RealizePalette(hdc);
  293. DEBUG_OUT((DEB_ITRACE,
  294. "Mapped %u logical palette entries to system palette entries\n",
  295. uiMapped));
  296. if (uiMapped == GDI_ERROR)
  297. {
  298. DEBUG_OUT_LASTERROR;
  299. break;
  300. }
  301. hBitmapFinal = CreateDIBitmap(hdc,
  302. pbih,
  303. CBM_INIT,
  304. (PBYTE) pbih + pbih->biSize + iNumColors *
  305. sizeof(RGBQUAD),
  306. (LPBITMAPINFO) pbih,
  307. DIB_RGB_COLORS);
  308. if (!hBitmapFinal)
  309. {
  310. DEBUG_OUT_LASTERROR;
  311. break;
  312. }
  313. //
  314. // If we got here, everything succeeded
  315. //
  316. hr = S_OK;
  317. } while (0);
  318. if (hdc)
  319. {
  320. ReleaseDC(NULL, hdc);
  321. }
  322. if (FAILED(hr) && *phPalette)
  323. {
  324. DeleteObject(*phPalette);
  325. *phPalette = NULL;
  326. }
  327. return hBitmapFinal;
  328. }
  329. #endif // WIZARD95