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.

368 lines
5.8 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. bkutils.c
  5. Abstract:
  6. Miscellaneous utility functions for online books program.
  7. Author:
  8. Ted Miller (tedm) 5-Jan-1995
  9. Revision History:
  10. --*/
  11. #include "books.h"
  12. UINT
  13. MyGetDriveType(
  14. IN WCHAR Drive
  15. )
  16. /*++
  17. Routine Description:
  18. Determine the type of a drive (removeable, fixed, net, cd, etc).
  19. Arguments:
  20. Drive - supplies drive letter of drive whose type is needed.
  21. Return Value:
  22. Same set of values as returned by GetDriveType() API.
  23. --*/
  24. {
  25. WCHAR DriveName[3];
  26. DriveName[0] = Drive;
  27. DriveName[1] = L':';
  28. DriveName[2] = 0;
  29. return GetDriveType(DriveName);
  30. }
  31. WCHAR
  32. LocateCdRomDrive(
  33. VOID
  34. )
  35. /*++
  36. Routine Description:
  37. Determine if a CD-ROM drive is attached to the computer and
  38. return its drive letter. If there's more than one cd-rom drive
  39. the one with the alphabetically lower drive letter is returned.
  40. Arguments:
  41. None.
  42. Return Value:
  43. Drive letter of CD-ROM drive, or 0 if none could be located.
  44. --*/
  45. {
  46. WCHAR Drive;
  47. UINT OldMode;
  48. OldMode = SetErrorMode(SEM_FAILCRITICALERRORS);
  49. for(Drive=L'C'; Drive<=L'Z'; Drive++) {
  50. if(MyGetDriveType(Drive) == DRIVE_CDROM) {
  51. SetErrorMode(OldMode);
  52. return Drive;
  53. }
  54. }
  55. SetErrorMode(OldMode);
  56. return 0;
  57. }
  58. BOOL
  59. IsCdRomInDrive(
  60. IN WCHAR Drive,
  61. IN PWSTR TagFile
  62. )
  63. /*++
  64. Routine Description:
  65. Determine if a particular CD-ROM is in a drive,
  66. based on the presence of a given tagfile.
  67. Arguments:
  68. Drive - supplies drive letter of drive to be checked
  69. for presence of the tagfile.
  70. TagFile - supplies drive-relative path (from root)
  71. of the file whose presence validates the presence
  72. of a volume.
  73. Return Value:
  74. Boolean value indicating whether the tagfile could be
  75. accessed.
  76. --*/
  77. {
  78. WCHAR Path[MAX_PATH];
  79. if(*TagFile == L'\\') {
  80. TagFile++;
  81. }
  82. wsprintf(Path,L"%c:\\%s",Drive,TagFile);
  83. return DoesFileExist(Path);
  84. }
  85. BOOL
  86. DoesFileExist(
  87. IN PWSTR File
  88. )
  89. /*++
  90. Routine Description:
  91. Determine if a file exists and is accessible.
  92. Arguments:
  93. File - supplies full path of file whose accessibility
  94. is in question.
  95. Return Value:
  96. Boolean value indicating whether file is accessible.
  97. --*/
  98. {
  99. UINT OldMode;
  100. HANDLE h;
  101. WIN32_FIND_DATA FindData;
  102. //
  103. // Avoid system popups.
  104. //
  105. OldMode = SetErrorMode(SEM_FAILCRITICALERRORS);
  106. h = FindFirstFile(File,&FindData);
  107. SetErrorMode(OldMode);
  108. if(h == INVALID_HANDLE_VALUE) {
  109. return(FALSE);
  110. }
  111. FindClose(h);
  112. return(TRUE);
  113. }
  114. PWSTR
  115. DupString(
  116. IN PWSTR String
  117. )
  118. /*++
  119. Routine Description:
  120. Duplicate a string and return a pointer to the copy.
  121. This routine always succeeds.
  122. Arguments:
  123. String - supplies pointer to the string to be duplicated.
  124. Return Value:
  125. Pointer to copy of string. Caller can free this buffer with
  126. MyFree when the copy is no longer needed.
  127. --*/
  128. {
  129. PWSTR p = MyMalloc((lstrlen(String)+1)*sizeof(WCHAR));
  130. lstrcpy(p,String);
  131. return p;
  132. }
  133. VOID
  134. CenterDialogOnScreen(
  135. IN HWND hdlg
  136. )
  137. /*++
  138. Routine Description:
  139. Center a window on the screen.
  140. Arguments:
  141. hdlg - supplies handle of window to be centered on the screen.
  142. Return Value:
  143. None.
  144. --*/
  145. {
  146. RECT rcWindow;
  147. LONG x,y,w,h;
  148. POINT point;
  149. LONG sx = GetSystemMetrics(SM_CXSCREEN),
  150. sy = GetSystemMetrics(SM_CYSCREEN);
  151. GetWindowRect (hdlg,&rcWindow);
  152. w = rcWindow.right - rcWindow.left + 1;
  153. h = rcWindow.bottom - rcWindow.top + 1;
  154. x = (sx - w) / 2;
  155. y = (sy - h) / 2;
  156. MoveWindow(hdlg,x,y,w,h,FALSE);
  157. }
  158. VOID
  159. CenterDialogInWindow(
  160. IN HWND hdlg,
  161. IN HWND hwnd
  162. )
  163. /*++
  164. Routine Description:
  165. Center a dialog relative to a window.
  166. Arguments:
  167. hdlg - supplies handle of window to be centered relative to a window
  168. hwnd - supplies handle of window relative to which hdlg is to be centered.
  169. Return Value:
  170. None.
  171. --*/
  172. {
  173. RECT rcFrame,
  174. rcWindow;
  175. LONG x,y,w,h;
  176. POINT point;
  177. LONG sx = GetSystemMetrics(SM_CXSCREEN),
  178. sy = GetSystemMetrics(SM_CYSCREEN);
  179. point.x = point.y = 0;
  180. ClientToScreen(hwnd,&point);
  181. GetWindowRect (hdlg,&rcWindow);
  182. GetClientRect (hwnd,&rcFrame );
  183. w = rcWindow.right - rcWindow.left + 1;
  184. h = rcWindow.bottom - rcWindow.top + 1;
  185. x = point.x + ((rcFrame.right - rcFrame.left + 1 - w) / 2);
  186. y = point.y + ((rcFrame.bottom - rcFrame.top + 1 - h) / 2);
  187. if (x + w > sx) {
  188. x = sx - w;
  189. } else if (x < 0) {
  190. x = 0;
  191. }
  192. if (y + h > sy) {
  193. y = sy - h;
  194. } else if (y < 0) {
  195. y = 0;
  196. }
  197. MoveWindow(hdlg,x,y,w,h,FALSE);
  198. }
  199. VOID
  200. MyError(
  201. IN HWND Owner,
  202. IN UINT StringId,
  203. IN BOOL Fatal
  204. )
  205. /*++
  206. Routine Description:
  207. Display message box whose text is taken from the application's
  208. string resources. The caption will be "Error"; the icon will be
  209. ICONSTOP for fatal errors and ICONINFORMATION for nonfatal ones.
  210. Arguments:
  211. Owner - supplies the window handle of the window that is to own
  212. the message box.
  213. StringId - supplies the string Id of the message to be displayed.
  214. Fatal - if TRUE, this is a fatal error and this routine does not
  215. return to the caller but exits via ExitProcess().
  216. Return Value:
  217. Returns only if the error is non-fatal.
  218. --*/
  219. {
  220. PWSTR p;
  221. //
  222. // Load error string
  223. //
  224. p = MyLoadString(StringId);
  225. //
  226. // Put up message box indicating the error.
  227. //
  228. MessageBox(
  229. Owner,
  230. p,
  231. NULL,
  232. MB_OK | MB_SETFOREGROUND | MB_TASKMODAL | (Fatal ? MB_ICONSTOP : MB_ICONINFORMATION)
  233. );
  234. if(Fatal) {
  235. ExitProcess(1);
  236. }
  237. }