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.

403 lines
12 KiB

  1. /*-----------------------------------------------------------------------------+
  2. | OPEN.C |
  3. | |
  4. | This file contains the code that controls the 'Open Device or File' dialog. |
  5. | |
  6. | (C) Copyright Microsoft Corporation 1991. All rights reserved. |
  7. | |
  8. | Revision History |
  9. | Oct-1992 MikeTri Ported to WIN32 / WIN16 common code |
  10. | |
  11. +-----------------------------------------------------------------------------*/
  12. /* include files */
  13. #include <windows.h>
  14. #include <windowsx.h>
  15. #include <commdlg.h>
  16. #include <mmsystem.h>
  17. #include "mpole.h"
  18. #include "mplayer.h"
  19. #include "registry.h"
  20. #include "regstr.h"
  21. extern HMENU ghMenu; /* handle to main menu */
  22. extern HMENU ghDeviceMenu; /* handle to the Device menu */
  23. extern UINT gwNumDevices; /* number of available devices */
  24. extern UINT gwCurDevice;
  25. extern PTSTR gpchFilter;
  26. LPTSTR gpInitialDir = NULL;
  27. extern SZCODE aszOptionsSection[];
  28. static SZCODE aszNULL[] = TEXT("");
  29. static SZCODE aszDirName[] = TEXT("%s Directory");
  30. BOOL GetDefaultMediaDirectory(LPTSTR pDirectory, DWORD cbDirectory);
  31. /*
  32. * fOK = DoOpen()
  33. *
  34. * Invoke the standard "File Open" dialog
  35. *
  36. * Return TRUE if and only if a new file is successfully opened.
  37. *
  38. */
  39. INT_PTR FAR PASCAL DoOpen(UINT wCurDevice, LPTSTR szFileName)
  40. {
  41. OPENFILENAME ofn;
  42. TCHAR achFile[MAX_PATH + 1]; /* file or device name buffer */
  43. TCHAR achTitle[80]; /* string holding the title bar name */
  44. INT_PTR f;
  45. TCHAR DirectoryValue[80];
  46. ZeroMemory(&ofn, sizeof ofn);
  47. if (!LOADSTRING(IDS_OPENTITLE, achTitle)) {
  48. Error(ghwndApp, IDS_OUTOFMEMORY);
  49. return FALSE;
  50. }
  51. if (wCurDevice != 0)
  52. {
  53. /* Saving and restoring the current directory for the device:
  54. *
  55. * We remember the directory that the user just selected.
  56. * It is saved as the "<device> Directory" value under
  57. * \Software\Microsoft\Media Player\Options for the current user.
  58. * The next time the user goes to open another file via the same
  59. * Device menu, we present the same initial directory.
  60. * This directory is also presented in the case where the user
  61. * selects File.Open.
  62. */
  63. wsprintf(DirectoryValue, aszDirName, garMciDevices[wCurDevice].szDevice);
  64. } else {
  65. //
  66. // If you choose Open from File menu instead of Device menu,
  67. // we should use "<No Name>" value instead of "<device> Directory".
  68. //
  69. DirectoryValue[0] = '\0';
  70. }
  71. if (ReadRegistryData(aszOptionsSection, DirectoryValue, NULL, (LPBYTE)achFile,
  72. BYTE_COUNT(achFile)) == NO_ERROR)
  73. {
  74. DWORD FileAttrs = GetFileAttributes(achFile);
  75. if ((FileAttrs != (DWORD)-1) && (FileAttrs & FILE_ATTRIBUTE_DIRECTORY))
  76. {
  77. if (gpInitialDir)
  78. FreeStr(gpInitialDir);
  79. gpInitialDir = AllocStr(achFile);
  80. }
  81. }
  82. /* Win95 and NT have a Media directory. Use that by default.
  83. */
  84. if (!gpInitialDir)
  85. {
  86. if (GetDefaultMediaDirectory(achFile, BYTE_COUNT(achFile)))
  87. {
  88. if (gpInitialDir)
  89. FreeStr(gpInitialDir);
  90. gpInitialDir = AllocStr(achFile);
  91. }
  92. }
  93. *achFile = TEXT('\0');
  94. /* Display the dialog box */
  95. ofn.lStructSize = sizeof(OPENFILENAME);
  96. ofn.hwndOwner = ghwndApp;
  97. ofn.hInstance = ghInst;
  98. ofn.lpstrFilter = gpchFilter; // in init.c
  99. if (wCurDevice == 0)
  100. ofn.nFilterIndex = gwNumDevices+1; // select "All Files"
  101. else
  102. ofn.nFilterIndex = wCurDevice;
  103. ofn.lpstrFile = achFile;
  104. ofn.nMaxFile = sizeof(achFile);
  105. ofn.lpstrInitialDir = gpInitialDir;
  106. ofn.lpstrTitle = achTitle;
  107. ofn.Flags = OFN_HIDEREADONLY |
  108. OFN_FILEMUSTEXIST |
  109. OFN_SHAREAWARE |
  110. OFN_PATHMUSTEXIST;
  111. f = GetOpenFileName(&ofn);
  112. lstrcpy(szFileName, achFile);
  113. if (f) {
  114. LPTSTR pLastBackslash;
  115. //
  116. // get the device selected in the dialog...
  117. //
  118. if (ofn.nFilterIndex == gwNumDevices+1)
  119. wCurDevice = 0; // all files
  120. else
  121. wCurDevice = (UINT)ofn.nFilterIndex;
  122. f = OpenMciDevice(achFile, garMciDevices[wCurDevice].szDevice);
  123. /* Save the directory that the user selected the file in.
  124. * achFile contains the full path of the file, which must include
  125. * at least one backslash.
  126. */
  127. pLastBackslash = STRRCHR(achFile, TEXT('\\'));
  128. if (pLastBackslash)
  129. {
  130. *(pLastBackslash) = TEXT('\0'); /* Make character following last
  131. backslash null terminator */
  132. if (gpInitialDir)
  133. FreeStr(gpInitialDir);
  134. gpInitialDir = AllocStr(achFile);
  135. if (wCurDevice != 0 && gpInitialDir)
  136. {
  137. /* Save the initial directory for this device:
  138. */
  139. WriteRegistryData(aszOptionsSection, DirectoryValue, REG_SZ,
  140. (LPBYTE)gpInitialDir, STRING_BYTE_COUNT(gpInitialDir));
  141. }
  142. }
  143. }
  144. return f;
  145. }
  146. /* GetDefaultMediaDirectory
  147. *
  148. * Returns C:\WINNT\Media, or whatever it's called.
  149. *
  150. */
  151. BOOL GetDefaultMediaDirectory(LPTSTR pDirectory, DWORD cbDirectory)
  152. {
  153. static SZCODE szSetup[] = REGSTR_PATH_SETUP;
  154. static SZCODE szMedia[] = REGSTR_VAL_MEDIA;
  155. HKEY hkeySetup;
  156. LONG Result;
  157. Result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szSetup,
  158. REG_OPTION_RESERVED,
  159. KEY_QUERY_VALUE, &hkeySetup);
  160. if (Result == ERROR_SUCCESS)
  161. {
  162. Result = RegQueryValueEx(hkeySetup, szMedia, NULL, REG_NONE,
  163. (LPBYTE)pDirectory, &cbDirectory);
  164. RegCloseKey(hkeySetup);
  165. }
  166. return (Result == ERROR_SUCCESS);
  167. }
  168. BOOL FAR PASCAL OpenMciDevice(LPCTSTR szFile, LPCTSTR szDevice)
  169. {
  170. HCURSOR hcurPrev; /* handle to the pre-hourglass cursor */
  171. BOOL f;
  172. BOOL fWeWereActive;
  173. UINT wDevice;
  174. if (szDevice == NULL && ((wDevice = IsMCIDevice(szFile)) != 0))
  175. return DoChooseDevice(wDevice);
  176. hcurPrev = SetCursor(LoadCursor(NULL, IDC_WAIT));
  177. /* Avoid the appearance of a half-painted window - update it now */
  178. UpdateWindow(ghwndApp);
  179. fWeWereActive = gfAppActive;
  180. if (gwCurDevice)
  181. WriteOutOptions(); // save current options as default for the old device
  182. // that is being closed before we open the new one.
  183. gwCurDevice = IsMCIDevice(szDevice);
  184. //
  185. // open the device/file
  186. //
  187. f = OpenMCI(szFile, szDevice);
  188. /* Give us activation back so UpdateDisplay can set focus to toolbar */
  189. if (f && fWeWereActive)
  190. SetActiveWindow(ghwndApp);
  191. //
  192. // only get the new options if:
  193. //
  194. // we actually opened the device and we did not get the options
  195. // from a OLE SetData!
  196. //
  197. if (f && (!gfRunWithEmbeddingFlag || gwOptions == 0))
  198. ReadOptions(); // Get the default options for this new device
  199. UpdateDisplay();
  200. SetCursor(hcurPrev);
  201. return f;
  202. }
  203. BOOL FAR PASCAL DoChooseDevice(UINT wID)
  204. {
  205. BOOL f;
  206. TCHAR szFile[256];
  207. UINT wOldDevice;
  208. UINT wOldScale;
  209. //
  210. // is this a valid device id?
  211. //
  212. if (wID < 1 || wID > gwNumDevices)
  213. return FALSE;
  214. wOldDevice = gwCurDevice;
  215. wOldScale = gwCurScale;
  216. //
  217. // if this device does files, bring up the open dialog else just open it!
  218. //
  219. if (garMciDevices[wID].wDeviceType & DTMCI_FILEDEV)
  220. f = OpenDoc(wID, szFile);
  221. else
  222. f = OpenMciDevice(aszNULL, garMciDevices[wID].szDevice);
  223. /* NOTE: This needs to be above the UpdateDisplay() so that if no */
  224. /* device was properly opened everything will be reset properly. */
  225. /* If nothing was opened, reset the current device back to what it was */
  226. /* and uncheck everything in the scale menu. */
  227. /* Yes, but this surely won't work unless we reopen the old device!! */
  228. /* Let's not bother with the previous device. */
  229. if (!f) {
  230. // gwCurDevice = wOldDevice;
  231. // gwCurScale = wOldScale;
  232. InvalidateRect(ghwndMap, NULL, TRUE); // wipe out track area??
  233. }
  234. return f;
  235. }
  236. //
  237. // find the device, given a MCI device name.
  238. //
  239. UINT FAR PASCAL IsMCIDevice(LPCTSTR szDevice)
  240. {
  241. UINT w;
  242. if (szDevice == NULL || *szDevice == 0)
  243. return 0;
  244. for (w=1; w<=gwNumDevices; w++)
  245. {
  246. if (lstrcmpi(szDevice, garMciDevices[w].szDevice) == 0 ||
  247. lstrcmpi(szDevice, garMciDevices[w].szDeviceName) == 0)
  248. return w;
  249. }
  250. return 0;
  251. }
  252. INT_PTR FAR PASCAL FixLinkDialog(LPTSTR szFile, LPTSTR szDevice, int iLen)
  253. {
  254. UINT wDevice;
  255. TCHAR achFile[_MAX_PATH + 1]; /* file or device name buffer */
  256. TCHAR achTitle[80]; /* string holding the title bar name */
  257. HWND hwndFocus;
  258. OPENFILENAME ofn;
  259. INT_PTR f;
  260. static SZCODE aszDialog[] = TEXT("MciOpenDialog"); // in open.c too.
  261. //
  262. // I GIVE UP!!! Put up an open dlg box and let them find it themselves!
  263. //
  264. /* Ensure the device menu's built:
  265. */
  266. InitDeviceMenu();
  267. WaitForDeviceMenu();
  268. // find out the device number for the specifed device
  269. wDevice = IsMCIDevice(szDevice);
  270. LOADSTRING(IDS_FINDFILE, achFile);
  271. wsprintf(achTitle, achFile, gachClassRoot, FileName(szFile)); // title bar for locate dlg
  272. /* Start with the bogus file name */
  273. if (szFile)
  274. {
  275. LPTSTR szFileName = FileName(szFile);
  276. if (szFileName)
  277. lstrcpy(achFile, szFileName);
  278. }
  279. /* Set up the ofn struct */
  280. ofn.lStructSize = sizeof(OPENFILENAME);
  281. /* MUST use ActiveWindow to make user deal with us NOW in case of multiple*/
  282. /* broken links */
  283. ofn.hwndOwner = GetActiveWindow();
  284. ofn.hInstance = ghInst;
  285. ofn.lpstrFilter = gpchFilter;
  286. ofn.lpstrCustomFilter = NULL;
  287. ofn.nMaxCustFilter = 0;
  288. if (wDevice == 0)
  289. ofn.nFilterIndex = gwNumDevices+1; // select "All Files"
  290. else
  291. ofn.nFilterIndex = wDevice;
  292. ofn.lpstrFile = achFile;
  293. ofn.nMaxFile = CHAR_COUNT(achFile);
  294. ofn.lpstrFileTitle = NULL;
  295. ofn.nMaxFileTitle = 0;
  296. ofn.lpstrInitialDir = NULL;
  297. ofn.lpstrTitle = achTitle;
  298. ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST |
  299. OFN_SHAREAWARE | OFN_PATHMUSTEXIST;
  300. ofn.nFileOffset = 0;
  301. ofn.nFileExtension = 0;
  302. ofn.lpstrDefExt = NULL;
  303. ofn.lCustData = 0;
  304. ofn.lpfnHook = NULL;
  305. ofn.lpTemplateName = NULL;
  306. // Show the cursor in case PowerPig is hiding it
  307. ShowCursor(TRUE);
  308. hwndFocus = GetFocus();
  309. /* Let the user pick a filename */
  310. f = GetOpenFileName(&ofn);
  311. if (f) {
  312. lstrcpyn(szFile, achFile, iLen);
  313. gfDirty = TRUE; // make sure the object is dirty now
  314. }
  315. SetFocus(hwndFocus);
  316. // Put cursor back how it used to be
  317. ShowCursor(FALSE);
  318. return f;
  319. }