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.

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