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.

257 lines
6.7 KiB

  1. /**********/
  2. /* util.c */
  3. /**********/
  4. #define _WINDOWS
  5. #include <windows.h>
  6. #include <htmlhelp.h> // for HtmlHelp()
  7. #include "shellapi.h" // for ShellAbout
  8. #include <port1632.h>
  9. #include "main.h"
  10. #include "res.h"
  11. #include "pref.h"
  12. #include "util.h"
  13. #include "sound.h"
  14. #include "rtns.h"
  15. #include "string.h"
  16. #include "stdio.h"
  17. #include "stdlib.h"
  18. #include "dos.h"
  19. extern INT dypBorder;
  20. extern INT dxpBorder;
  21. extern INT dypCaption;
  22. extern INT dypMenu;
  23. extern TCHAR szClass[cchNameMax];
  24. extern TCHAR szTime[cchNameMax];
  25. extern TCHAR szDefaultName[cchNameMax];
  26. extern HANDLE hInst;
  27. extern HWND hwndMain;
  28. extern HMENU hMenu;
  29. extern PREF Preferences;
  30. extern HKEY g_hReg;
  31. extern TCHAR * rgszPref[iszPrefMax];
  32. TCHAR szIniFile[] = TEXT("entpack.ini");
  33. /****** R N D ******/
  34. /* Return a random number between 0 and rndMax */
  35. INT Rnd(INT rndMax)
  36. {
  37. return (rand() % rndMax);
  38. }
  39. /****** R E P O R T E R R ******/
  40. /* Report and error and exit */
  41. VOID ReportErr(WORD idErr)
  42. {
  43. TCHAR szMsg[cchMsgMax];
  44. TCHAR szMsgTitle[cchMsgMax];
  45. if (idErr < ID_ERR_MAX)
  46. LoadString(hInst, idErr, szMsg, cchMsgMax);
  47. else
  48. {
  49. LoadString(hInst, ID_ERR_UNKNOWN, szMsgTitle, cchMsgMax);
  50. wsprintf(szMsg, szMsgTitle, idErr);
  51. }
  52. LoadString(hInst, ID_ERR_TITLE, szMsgTitle, cchMsgMax);
  53. MessageBox(NULL, szMsg, szMsgTitle, MB_OK | MB_ICONHAND);
  54. }
  55. /****** L O A D S Z ******/
  56. VOID LoadSz(WORD id, TCHAR * sz, DWORD cch)
  57. {
  58. if (LoadString(hInst, id, sz, cch) == 0)
  59. ReportErr(1001);
  60. }
  61. // Routines to read the ini file.
  62. INT ReadIniInt(INT iszPref, INT valDefault, INT valMin, INT valMax)
  63. {
  64. return max(valMin, min(valMax,
  65. (INT) GetPrivateProfileInt(szClass, rgszPref[iszPref], valDefault, szIniFile) ) );
  66. }
  67. #define ReadIniBool(iszPref, valDefault) ReadIniInt(iszPref, valDefault, 0, 1)
  68. VOID ReadIniSz(INT iszPref, TCHAR FAR * szRet)
  69. {
  70. GetPrivateProfileString(szClass, rgszPref[iszPref], szDefaultName, szRet, cchNameMax, szIniFile);
  71. }
  72. /****** I N I T C O N S T ******/
  73. VOID InitConst(VOID)
  74. {
  75. INT iAlreadyPlayed = 0; // have we already updated the registry ?
  76. DWORD dwDisposition;
  77. srand(LOWORD(GetCurrentTime()));
  78. LoadSz(ID_GAMENAME, szClass, ARRAYSIZE(szClass));
  79. LoadSz(ID_MSG_SEC, szTime, ARRAYSIZE(szTime));
  80. LoadSz(ID_NAME_DEFAULT, szDefaultName, ARRAYSIZE(szDefaultName));
  81. dypCaption = GetSystemMetrics(SM_CYCAPTION) + 1;
  82. dypMenu = GetSystemMetrics(SM_CYMENU) + 1;
  83. dypBorder = GetSystemMetrics(SM_CYBORDER) + 1;
  84. dxpBorder = GetSystemMetrics(SM_CXBORDER) + 1;
  85. // Open the registry key;
  86. if (RegCreateKeyEx(HKEY_CURRENT_USER, SZWINMINEREG, 0, NULL, 0, KEY_READ, NULL,
  87. &g_hReg, &dwDisposition) == ERROR_SUCCESS)
  88. {
  89. iAlreadyPlayed = ReadInt(iszPrefAlreadyPlayed, 0, 0, 1);
  90. RegCloseKey(g_hReg);
  91. }
  92. // Read it from the .ini file and write it to registry.
  93. if (!iAlreadyPlayed)
  94. {
  95. Preferences.Height= ReadIniInt(iszPrefHeight,MINHEIGHT,DEFHEIGHT,25);
  96. Preferences.Width= ReadIniInt(iszPrefWidth,MINWIDTH,DEFWIDTH,30);
  97. Preferences.wGameType = (WORD)ReadIniInt(iszPrefGame,wGameBegin, wGameBegin, wGameExpert+1);
  98. Preferences.Mines = ReadIniInt(iszPrefMines, 10, 10, 999);
  99. Preferences.xWindow = ReadIniInt(iszPrefxWindow, 80, 0, 1024);
  100. Preferences.yWindow = ReadIniInt(iszPrefyWindow, 80, 0, 1024);
  101. Preferences.fSound = ReadIniInt(iszPrefSound, 0, 0, fsoundOn);
  102. Preferences.fMark = ReadIniBool(iszPrefMark, fTrue);
  103. Preferences.fTick = ReadIniBool(iszPrefTick, fFalse);
  104. Preferences.fMenu = ReadIniInt(iszPrefMenu, fmenuAlwaysOn, fmenuAlwaysOn, fmenuOn);
  105. Preferences.rgTime[wGameBegin] = ReadIniInt(iszPrefBeginTime, 999, 0, 999);
  106. Preferences.rgTime[wGameInter] = ReadIniInt(iszPrefInterTime, 999, 0, 999);
  107. Preferences.rgTime[wGameExpert] = ReadIniInt(iszPrefExpertTime, 999, 0, 999);
  108. ReadIniSz(iszPrefBeginName, Preferences.szBegin);
  109. ReadIniSz(iszPrefInterName, Preferences.szInter);
  110. ReadIniSz(iszPrefExpertName, Preferences.szExpert);
  111. // set the color preference so we will use the right bitmaps
  112. // numcolors may return -1 on true color devices
  113. {
  114. HDC hDC = GetDC(GetDesktopWindow());
  115. Preferences.fColor = ReadIniBool(iszPrefColor, (GetDeviceCaps(hDC, NUMCOLORS) != 2));
  116. ReleaseDC(GetDesktopWindow(),hDC);
  117. }
  118. if (FSoundOn())
  119. Preferences.fSound = FInitTunes();
  120. // Write it to registry.
  121. WritePreferences();
  122. }
  123. }
  124. /* * * * * * M E N U S * * * * * */
  125. /****** C H E C K E M ******/
  126. VOID CheckEm(WORD idm, BOOL fCheck)
  127. {
  128. CheckMenuItem(hMenu, idm, fCheck ? MF_CHECKED : MF_UNCHECKED);
  129. }
  130. /****** S E T M E N U B A R ******/
  131. VOID SetMenuBar(INT fActive)
  132. {
  133. Preferences.fMenu = fActive;
  134. FixMenus();
  135. SetMenu(hwndMain, FMenuOn() ? hMenu : NULL);
  136. AdjustWindow(fResize);
  137. }
  138. /****** D O A B O U T ******/
  139. VOID DoAbout(VOID)
  140. {
  141. TCHAR szVersion[cchMsgMax];
  142. TCHAR szCredit[cchMsgMax];
  143. LoadSz(ID_MSG_VERSION, szVersion, ARRAYSIZE(szVersion));
  144. LoadSz(ID_MSG_CREDIT, szCredit, ARRAYSIZE(szCredit));
  145. ShellAbout(hwndMain,
  146. szVersion, szCredit, LoadIcon(hInst, MAKEINTRESOURCE(ID_ICON_MAIN)));
  147. }
  148. /****** D O H E L P ******/
  149. VOID DoHelp(WORD wCommand, UINT lParam)
  150. {
  151. CHAR szHelpFile[cchMaxPathname];
  152. CHAR * pch;
  153. // If we are showing help_onhelp, use the
  154. // nthelp.chm file else use winmine.chm
  155. if (wCommand != HELP_HELPONHELP)
  156. {
  157. /*
  158. * Replace the .exe extension on the complete path with
  159. * the .hlp extension instead.
  160. */
  161. pch = szHelpFile +
  162. GetModuleFileNameA(hInst, szHelpFile, cchMaxPathname) - 1;
  163. if ( (pch-szHelpFile > 4) &&
  164. (*(pch-3) == '.') ) {
  165. pch -= 3;
  166. }
  167. strcpy(pch, ".chm");
  168. }
  169. else strcpy(szHelpFile, "NTHelp.chm");
  170. HtmlHelpA(GetDesktopWindow(), szHelpFile, lParam, 0);
  171. }
  172. /****** G E T D L G I N T ******/
  173. INT GetDlgInt(HWND hDlg, INT dlgID, INT numLo, INT numHi)
  174. {
  175. INT num;
  176. BOOL fFlag;
  177. num = GetDlgItemInt(hDlg, dlgID, &fFlag, fFalse);
  178. if (num < numLo)
  179. num = numLo;
  180. else if (num > numHi)
  181. num = numHi;
  182. return num;
  183. }