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.

443 lines
9.0 KiB

  1. #include "sol.h"
  2. VSZASSERT
  3. VOID *PAlloc(INT cb)
  4. {
  5. TCHAR *p;
  6. // KLUDGE: solve overwriting memory by allocating more
  7. #define MEMORYPAD 200
  8. p = (TCHAR *)LocalAlloc(LPTR, cb+MEMORYPAD);
  9. Assert(p != NULL);
  10. return (VOID *)p;
  11. }
  12. VOID FreeP(VOID *p)
  13. {
  14. LocalFree((HANDLE) p);
  15. }
  16. VOID InvertRc(RC *prc)
  17. {
  18. Assert(xOrgCur == 0);
  19. Assert(yOrgCur == 0);
  20. AssertHdcCur();
  21. InvertRect(hdcCur, (LPRECT) prc);
  22. }
  23. VOID DrawCard(CRD *pcrd)
  24. {
  25. AssertHdcCur();
  26. cdtDrawExt( hdcCur,
  27. pcrd->pt.x-xOrgCur,
  28. pcrd->pt.y-yOrgCur,
  29. dxCrd,
  30. dyCrd,
  31. pcrd->fUp ? pcrd->cd : modeFaceDown,
  32. pcrd->fUp ? FACEUP : FACEDOWN,
  33. rgbTable);
  34. }
  35. VOID DrawOutline(PT *ppt, INT ccrd, DX dx, DY dy)
  36. {
  37. Y y;
  38. PT pt;
  39. INT rop2;
  40. if(!FGetHdc())
  41. return;
  42. pt = *ppt;
  43. rop2 = SetROP2(hdcCur, R2_NOT);
  44. MMoveTo(hdcCur, pt.x, pt.y);
  45. LineTo(hdcCur, pt.x+dxCrd, pt.y);
  46. LineTo(hdcCur, pt.x+dxCrd, y = pt.y+dyCrd+(ccrd-1) * dy);
  47. LineTo(hdcCur, pt.x, y);
  48. LineTo(hdcCur, pt.x, pt.y);
  49. y = pt.y;
  50. while(--ccrd)
  51. {
  52. y += dy;
  53. MMoveTo(hdcCur, pt.x, y);
  54. LineTo(hdcCur, pt.x+dxCrd, y);
  55. }
  56. SetROP2(hdcCur, rop2);
  57. ReleaseHdc();
  58. }
  59. VOID DrawCardPt(CRD *pcrd, PT *ppt)
  60. {
  61. DWORD dwModeExt=0; // turn on sign bit if moving fast
  62. // cdtDrawExt must support this!
  63. if( fKlondWinner )
  64. {
  65. dwModeExt= MINLONG;
  66. }
  67. AssertHdcCur();
  68. cdtDrawExt(hdcCur,
  69. ppt->x-xOrgCur,
  70. ppt->y-yOrgCur,
  71. dxCrd,
  72. dyCrd,
  73. pcrd->fUp ? pcrd->cd : modeFaceDown,
  74. (pcrd->fUp ? FACEUP : FACEDOWN ) | dwModeExt,
  75. rgbTable);
  76. }
  77. VOID DrawCardExt(PT *ppt, INT cd, INT mode)
  78. {
  79. VOID DrawBackground();
  80. AssertHdcCur();
  81. cdtDrawExt( hdcCur,
  82. ppt->x-xOrgCur,
  83. ppt->y-yOrgCur,
  84. dxCrd,
  85. dyCrd,
  86. cd,
  87. mode,
  88. rgbTable);
  89. }
  90. VOID DrawBackground(X xLeft, Y yTop, X xRight, Y yBot)
  91. {
  92. HBRUSH hbr;
  93. AssertHdcCur();
  94. MSetBrushOrg(hdcCur, xOrgCur, yOrgCur);
  95. MUnrealizeObject(hbrTable);
  96. if((hbr = SelectObject(hdcCur, hbrTable)) != NULL)
  97. {
  98. Assert(xRight >= xLeft);
  99. Assert(yBot >= yTop);
  100. PatBlt( hdcCur,
  101. xLeft-xOrgCur,
  102. yTop-yOrgCur,
  103. xRight-xLeft,
  104. yBot-yTop,
  105. PATCOPY);
  106. SelectObject(hdcCur, hbr);
  107. }
  108. }
  109. VOID EraseScreen(VOID)
  110. {
  111. RC rc;
  112. HDC HdcSet();
  113. if(!FGetHdc())
  114. return;
  115. GetClientRect(hwndApp, (LPRECT) &rc);
  116. DrawBackground(rc.xLeft, rc.yTop, rc.xRight, rc.yBot);
  117. ReleaseHdc();
  118. }
  119. BOOL FPtInCrd(CRD *pcrd, PT pt)
  120. {
  121. return(pt.x >= pcrd->pt.x && pt.x < pcrd->pt.x+dxCrd &&
  122. pt.y >= pcrd->pt.y && pt.y < pcrd->pt.y+dyCrd);
  123. }
  124. BOOL FRectIsect(RC *prc1, RC *prc2)
  125. {
  126. RC rcDummy;
  127. return(IntersectRect((LPRECT) &rcDummy, (LPRECT) prc1, (LPRECT) prc2));
  128. }
  129. VOID CrdRcFromPt(PT *ppt, RC *prc)
  130. {
  131. prc->xRight = (prc->xLeft = ppt->x) + dxCrd;
  132. prc->yBot = (prc->yTop = ppt->y) + dyCrd;
  133. }
  134. BOOL FCrdRectIsect(CRD *pcrd, RC *prc)
  135. {
  136. RC rcDummy;
  137. RC rcCrd;
  138. CrdRcFromPt(&pcrd->pt, &rcCrd);
  139. return(IntersectRect((LPRECT) &rcDummy, (LPRECT) &rcCrd, (LPRECT) prc));
  140. }
  141. /* BUG: only considers upper left and lower right corners */
  142. /* this is ok for my purposes now, but beware... */
  143. BOOL FRectAllVisible(HDC hdc, RC *prc)
  144. {
  145. return PtVisible(hdc, prc->xLeft, prc->yTop) && PtVisible(hdc, prc->xRight, prc->yBot);
  146. }
  147. VOID OffsetPt(PT *ppt, DEL *pdel, PT *pptDest)
  148. {
  149. pptDest->x = ppt->x + pdel->dx;
  150. pptDest->y = ppt->y + pdel->dy;
  151. }
  152. VOID SwapCards(CRD *pcrd1, CRD *pcrd2)
  153. {
  154. CRD crdT;
  155. crdT = *pcrd1;
  156. *pcrd1 = *pcrd2;
  157. *pcrd2 = crdT;
  158. }
  159. TCHAR *PszCopy(TCHAR *pszFrom, TCHAR *rgchTo)
  160. {
  161. while (*rgchTo++ = *pszFrom++)
  162. ;
  163. return(rgchTo-1);
  164. }
  165. INT CchDecodeInt(TCHAR *rgch, INT_PTR w)
  166. {
  167. INT fNeg;
  168. TCHAR *pch, *pchT;
  169. TCHAR rgchT[20];
  170. if (fNeg = w<0)
  171. w = -w;
  172. pchT = rgchT;
  173. do
  174. {
  175. *pchT++ = (TCHAR)(TEXT('0') + (TCHAR) (w % 10));
  176. w /= 10;
  177. }
  178. while (w);
  179. pch = rgch;
  180. if (fNeg)
  181. *pch++ = TEXT('-');
  182. do
  183. *pch++ = *--pchT;
  184. while (pchT > rgchT);
  185. *pch = TEXT('\000');
  186. return((INT)(pch - rgch));
  187. }
  188. VOID Error(TCHAR *sz)
  189. {
  190. MessageBox(hwndApp, (LPTSTR)sz, (LPTSTR)szAppName, MB_OK|MB_ICONEXCLAMATION);
  191. }
  192. /* returns fTrue if yes is clicked */
  193. BOOL FYesNoAlert( INT ids )
  194. {
  195. TCHAR sz[128];
  196. INT id;
  197. CchString(sz, ids, ARRAYSIZE(sz));
  198. id = MessageBox(hwndApp, sz, szAppName, MB_YESNO|MB_ICONEXCLAMATION);
  199. return id == IDYES || id == IDOK;
  200. }
  201. VOID ErrorIds(INT ids)
  202. {
  203. TCHAR sz[128];
  204. CchString(sz, ids, ARRAYSIZE(sz));
  205. Error(sz);
  206. }
  207. INT WMin(INT w1, INT w2)
  208. {
  209. return(w1 < w2 ? w1 : w2);
  210. }
  211. INT WMax(INT w1, INT w2)
  212. {
  213. return(w1 > w2 ? w1 : w2);
  214. }
  215. BOOL FInRange(INT w, INT wFirst, INT wLast)
  216. {
  217. Assert(wFirst <= wLast);
  218. return(w >= wFirst && w <= wLast);
  219. }
  220. INT PegRange(INT w, INT wFirst, INT wLast)
  221. {
  222. Assert(wFirst <= wLast);
  223. if(w < wFirst)
  224. return wFirst;
  225. else if(w > wLast)
  226. return wLast;
  227. else
  228. return w;
  229. }
  230. VOID OOM()
  231. {
  232. Error(szOOM);
  233. }
  234. VOID NYI()
  235. {
  236. Error(TEXT("Not Yet Implemented"));
  237. }
  238. INT CchString(TCHAR *sz, INT ids, UINT cchBuf)
  239. {
  240. return LoadString(hinstApp, (WORD)ids, (LPTSTR)sz, cchBuf);
  241. }
  242. BOOL FWriteIniString(INT idsTopic, INT idsItem, TCHAR *szValue)
  243. {
  244. TCHAR szItem[32];
  245. HKEY hKey; // key to our registry root
  246. LONG lStatus; // status from RegCreateKey
  247. INT iLen;
  248. BOOL fRet = FALSE;
  249. // create the key
  250. lStatus = RegCreateKeyEx(HKEY_CURRENT_USER, SOLKEYNAME, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL);
  251. if(lStatus != ERROR_SUCCESS)
  252. {
  253. return FALSE; // just return quietly
  254. }
  255. CchString(szItem, idsItem, ARRAYSIZE(szItem));
  256. iLen = (lstrlen(szValue)+1) * sizeof(TCHAR);
  257. // write the key and value to the registry
  258. if (RegSetValueEx(hKey, szItem, 0, REG_SZ, (BYTE*)szValue, iLen) == ERROR_SUCCESS)
  259. fRet = TRUE;
  260. else
  261. fRet = FALSE;
  262. RegCloseKey(hKey);
  263. return fRet;
  264. }
  265. BOOL FWriteIniInt(INT idsTopic, INT idsItem, DWORD w)
  266. {
  267. TCHAR szItem[32];
  268. HKEY hKey; // key to our registry root
  269. LONG lStatus; // status from RegCreateKey
  270. BOOL fRet = FALSE;
  271. // create the key
  272. lStatus = RegCreateKeyEx(HKEY_CURRENT_USER, SOLKEYNAME, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL);
  273. if(lStatus != ERROR_SUCCESS)
  274. {
  275. return FALSE; // just return quietly
  276. }
  277. CchString(szItem, idsItem, ARRAYSIZE(szItem));
  278. // write the key and value to the registry
  279. if (RegSetValueEx(hKey, szItem, 0, REG_DWORD, (BYTE*) &w, sizeof(DWORD)) == ERROR_SUCCESS)
  280. fRet = TRUE;
  281. else
  282. fRet = FALSE;
  283. RegCloseKey(hKey);
  284. return fRet;
  285. }
  286. BOOL FGetIniString(INT idsTopic, INT idsItem, TCHAR *sz, TCHAR *szDefault, INT cchMax)
  287. {
  288. TCHAR szItem[32];
  289. HKEY hKey; // key to our registry root
  290. LONG lStatus;
  291. DWORD dwType;
  292. // open the key
  293. lStatus = RegCreateKeyEx(HKEY_CURRENT_USER, SOLKEYNAME, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL);
  294. if(lStatus != ERROR_SUCCESS)
  295. {
  296. CopyMemory(sz, szDefault, min(cchMax, lstrlen(szDefault)+1));
  297. return TRUE;
  298. }
  299. CchString(szItem, idsItem, ARRAYSIZE(szItem));
  300. if(hKey)
  301. {
  302. lStatus= RegQueryValueEx(hKey, szItem, NULL, &dwType, (BYTE*) szDefault, &cchMax);
  303. RegCloseKey(hKey);
  304. if(lStatus != ERROR_SUCCESS || dwType != REG_SZ)
  305. {
  306. CopyMemory(sz, szDefault, min(cchMax, lstrlen(szDefault)+1));
  307. }
  308. }
  309. return TRUE;
  310. }
  311. DWORD GetIniInt(INT idsTopic, INT idsItem, DWORD wDefault)
  312. {
  313. TCHAR szItem[32];
  314. HKEY hKey; // key to our registry root
  315. LONG lStatus;
  316. DWORD dwResult = wDefault;
  317. DWORD dwSize = sizeof(DWORD);
  318. DWORD dwType = 0;
  319. lStatus = RegCreateKeyEx(HKEY_CURRENT_USER, SOLKEYNAME, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL);
  320. if(lStatus != ERROR_SUCCESS)
  321. {
  322. return wDefault;
  323. }
  324. CchString(szItem, idsItem, ARRAYSIZE(szItem));
  325. if(hKey)
  326. {
  327. lStatus = RegQueryValueEx(hKey, szItem, NULL, &dwType, (BYTE*) &dwResult, &dwSize);
  328. RegCloseKey(hKey);
  329. if(lStatus != ERROR_SUCCESS || dwType != REG_DWORD)
  330. {
  331. dwResult = wDefault;
  332. }
  333. }
  334. return(dwResult);
  335. }