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.

429 lines
13 KiB

  1. /************************************************************/
  2. /* Windows Write, Copyright 1985-1992 Microsoft Corporation */
  3. /************************************************************/
  4. #include <windows.h>
  5. #include "mw.h"
  6. #include "menudefs.h"
  7. #include "str.h"
  8. #include <commdlg.h>
  9. #include "filedefs.h"
  10. #include <dlgs.h>
  11. #include "doslib.h"
  12. #include "obj.h"
  13. extern HANDLE hMmwModInstance;
  14. extern CHAR szAppName[];
  15. extern HWND vhWndMsgBoxParent,hParentWw,vhWnd;
  16. static OPENFILENAME OFN;
  17. static bSave;
  18. FARPROC lpfnOFNHook=NULL;
  19. BOOL FAR PASCAL fnOFNHook(HWND hDlg, unsigned msg, WORD wParam, LONG lParam) ;
  20. #define hINSTANCE hMmwModInstance
  21. #define hDOCWINDOW vhWnd
  22. #define hMAINWINDOW hParentWw
  23. #define hPARENTWINDOW ((vhWndMsgBoxParent == NULL) ? \
  24. hParentWw : vhWndMsgBoxParent)
  25. #define CBPATHMAX cchMaxFile
  26. #define CFILTERMAX 6 /* Max # filters */
  27. #define CBFILTERSIZE 40
  28. #define CBFILTERMAX (CBFILTERSIZE * CFILTERMAX) /* Max # chars/filter */
  29. #define CBMESSAGEMAX 80
  30. static char fDefFileType;
  31. #define SA_WORDTEXT 0
  32. #define SA_TEXTONLY 1
  33. #define SA_WORD 2
  34. #define SA_OLDWRITE 3
  35. #define SA_WRITE 4
  36. static char *szDefExtensions[6];
  37. static int nTextOnly,nWordText,nWord,nOldWrite; // position in filterspec list box
  38. static char szNull[1] = "";
  39. static char szWild[3] = "*.";
  40. static char szOpenFile[CBMESSAGEMAX];
  41. static char szSaveFile[CBMESSAGEMAX];
  42. static char szFileName[CBPATHMAX];
  43. static char szLastDir[CBPATHMAX];
  44. static char szDefWriExtension[CBMESSAGEMAX];
  45. static char szDefDocExtension[CBMESSAGEMAX];
  46. static char szDefTxtExtension[CBMESSAGEMAX];
  47. static char szWriDescr[CBMESSAGEMAX];
  48. static char szDocDescr[CBMESSAGEMAX];
  49. static char szTxtDescr[CBMESSAGEMAX];
  50. static char szAllFilesDescr[CBMESSAGEMAX];
  51. static char szDocTxtDescr[CBMESSAGEMAX];
  52. static char szOldWriteDescr[CBMESSAGEMAX];
  53. static char szFilterSpec[CBFILTERMAX];
  54. static char szCustFilterSpec[CBFILTERSIZE];
  55. static MakeFilterString(int iWhichOper);
  56. int InitCommDlg(int iWhichOper);
  57. int InitCommDlg(int iWhichOper)
  58. {
  59. OFN.lpstrDefExt = NULL;
  60. OFN.lpstrFile = szFileName;
  61. OFN.lpstrFilter = szFilterSpec;
  62. OFN.lpstrCustomFilter = szCustFilterSpec;
  63. switch(iWhichOper)
  64. {
  65. case 0: // beginning of Write session
  66. OFN.lStructSize = sizeof(OPENFILENAME);
  67. OFN.hInstance = hINSTANCE;
  68. OFN.lCustData = NULL;
  69. OFN.lpTemplateName = NULL;
  70. OFN.lpstrFileTitle = NULL;
  71. OFN.nMaxFileTitle = 0;
  72. OFN.nMaxFile = CBPATHMAX;
  73. OFN.lpstrInitialDir = NULL;
  74. OFN.nMaxCustFilter = CBFILTERSIZE;
  75. LoadString(hINSTANCE, IDSTROpenfile, szOpenFile, sizeof(szOpenFile));
  76. LoadString(hINSTANCE, IDSTRSavefile, szSaveFile, sizeof(szSaveFile));
  77. LoadString(hINSTANCE, IDSTRDefWriExtension, szDefWriExtension, sizeof(szDefWriExtension));
  78. LoadString(hINSTANCE, IDSTRDefDocExtension, szDefDocExtension, sizeof(szDefDocExtension));
  79. LoadString(hINSTANCE, IDSTRDefTxtExtension, szDefTxtExtension, sizeof(szDefTxtExtension));
  80. LoadString(hINSTANCE, IDSTRWriDescr, szWriDescr, sizeof(szWriDescr));
  81. LoadString(hINSTANCE, IDSTRDocDescr, szDocDescr, sizeof(szDocDescr));
  82. LoadString(hINSTANCE, IDSTRTxtDescr, szTxtDescr, sizeof(szTxtDescr));
  83. LoadString(hINSTANCE, IDSTRDocTextDescr, szDocTxtDescr, sizeof(szDocTxtDescr));
  84. LoadString(hINSTANCE, IDSTRAllFilesDescr, szAllFilesDescr, sizeof(szAllFilesDescr));
  85. LoadString(hINSTANCE, IDSTROldWriteDescr, szOldWriteDescr, sizeof(szOldWriteDescr));
  86. return FALSE;
  87. case imiOpen:
  88. if ((lpfnOFNHook = MakeProcInstance(fnOFNHook, hINSTANCE)) == NULL)
  89. return TRUE;
  90. OFN.hwndOwner = hPARENTWINDOW;
  91. OFN.Flags = OFN_ENABLEHOOK|OFN_HIDEREADONLY|OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST;
  92. OFN.lpfnHook = lpfnOFNHook;
  93. lstrcpy(szFileName,szWild);
  94. lstrcat(szFileName,szDefWriExtension);
  95. OFN.lpstrTitle = szOpenFile;
  96. szCustFilterSpec[0] = '\0';
  97. fDefFileType = SA_WRITE; /* see MakeFilterSpec */
  98. MakeFilterString(iWhichOper);
  99. return FALSE;
  100. case imiSaveAs:
  101. /* read only will become the backup check box */
  102. if ((lpfnOFNHook = MakeProcInstance(fnOFNHook, hINSTANCE)) == NULL)
  103. return TRUE;
  104. OFN.hwndOwner = hPARENTWINDOW;
  105. OFN.Flags = OFN_ENABLEHOOK|OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT;
  106. OFN.lpfnHook = lpfnOFNHook;
  107. OFN.lpstrTitle = szSaveFile;
  108. szCustFilterSpec[0] = '\0';
  109. MakeFilterString(iWhichOper);
  110. return FALSE;
  111. }
  112. }
  113. int DoCommDlg(int iWhichOper)
  114. /* returns whether file was retrieved */
  115. /* iWhichOper is the imi* code from the menu */
  116. {
  117. int iRetval;
  118. bSave = iWhichOper == imiSaveAs;
  119. iRetval = !InitCommDlg(iWhichOper);
  120. if (!iRetval)
  121. goto end;
  122. LockData(0);
  123. switch(iWhichOper)
  124. {
  125. case imiOpen:
  126. iRetval = GetOpenFileName((LPOPENFILENAME)&OFN);
  127. break;
  128. case imiSaveAs:
  129. iRetval = GetSaveFileName((LPOPENFILENAME)&OFN);
  130. break;
  131. }
  132. UnlockData(0);
  133. if (CommDlgExtendedError())
  134. {
  135. iRetval = FALSE;
  136. Error(IDPMTNoMemory);
  137. }
  138. end:
  139. if (iRetval)
  140. {
  141. lstrcpy(szLastDir,szFileName);
  142. szLastDir[OFN.nFileOffset] = 0;
  143. OFN.lpstrInitialDir = szLastDir;
  144. }
  145. switch(iWhichOper)
  146. {
  147. case imiOpen:
  148. case imiSaveAs:
  149. if (lpfnOFNHook)
  150. FreeProcInstance(lpfnOFNHook);
  151. lpfnOFNHook = NULL;
  152. break;
  153. }
  154. return iRetval;
  155. }
  156. #include "docdefs.h"
  157. BOOL FAR PASCAL fnOFNHook(HWND hDlg, unsigned msg, WORD wParam, LONG lParam)
  158. {
  159. static unsigned wmListBoxChange;
  160. static unsigned wmCheckShare;
  161. extern int docCur;
  162. extern struct DOD (**hpdocdod)[];
  163. switch (msg)
  164. {
  165. case WM_INITDIALOG:
  166. if (bSave)
  167. {
  168. char szTitle[CBMESSAGEMAX];
  169. LoadString(hINSTANCE, IDSTRBackup, szTitle, sizeof(szTitle));
  170. SetDlgItemText(hDlg,chx1,szTitle);
  171. CheckDlgButton(hDlg,chx1,OFN.Flags&OFN_READONLY);
  172. if (szFileName[0] == 0)
  173. SetDlgItemText(hDlg,edt1,"");
  174. }
  175. else // open
  176. wmCheckShare = RegisterWindowMessage(SHAREVISTRING);
  177. wmListBoxChange = RegisterWindowMessage(LBSELCHSTRING);
  178. break;
  179. case WM_COMMAND:
  180. if (bSave)
  181. switch (wParam)
  182. {
  183. case chx1:
  184. /* handle checking the readonly button (I forget what does this do??)
  185. (we've changed readonly to be a "Backup" button) */
  186. return TRUE;
  187. break;
  188. }
  189. break;
  190. default:
  191. if ((msg == wmListBoxChange) && (wParam == cmb1))
  192. /* file type selected, set the default extension */
  193. OFN.lpstrDefExt = szDefExtensions[LOWORD(lParam)];
  194. else if ((msg == wmCheckShare) && !bSave)
  195. /* we want to be able to reopen current document */
  196. {
  197. if (!lstrcmpi((LPSTR)(**((**hpdocdod)[docCur].hszFile)),(LPSTR)lParam))
  198. return OFN_SHAREFALLTHROUGH;
  199. else
  200. return OFN_SHAREWARN;
  201. }
  202. break;
  203. }
  204. return FALSE;
  205. }
  206. static MakeFilterString(int iWhichOper)
  207. /* Construct the filter string for the Open, Save dialogs */
  208. /* assume fDefFileType is set */
  209. {
  210. LPSTR lpStr = szFilterSpec;
  211. char **ppstr = szDefExtensions;
  212. int nCount=1;
  213. /* WRI */
  214. lstrcpy(lpStr, szWriDescr);
  215. lpStr += lstrlen(lpStr)+1;
  216. lstrcpy(lpStr, szWild);
  217. lstrcat(lpStr, szDefWriExtension);
  218. lpStr += lstrlen(lpStr)+1;
  219. *ppstr++ = szDefWriExtension;
  220. ++nCount;
  221. if (iWhichOper == imiSaveAs)
  222. {
  223. /* Old WRI (without objects) */
  224. vcObjects = ObjEnumInDoc(docCur,NULL);
  225. if (vcObjects > 0)
  226. {
  227. lstrcpy(lpStr, szOldWriteDescr);
  228. lpStr += lstrlen(lpStr)+1;
  229. lstrcpy(lpStr, szWild);
  230. lstrcat(lpStr, szDefWriExtension);
  231. lpStr += lstrlen(lpStr)+1;
  232. *ppstr++ = szDefWriExtension;
  233. nOldWrite = nCount;
  234. ++nCount;
  235. }
  236. else if (fDefFileType == SA_OLDWRITE)
  237. fDefFileType = SA_WRITE;
  238. }
  239. #ifndef JAPAN // added 09 Jun. 1992 by Hiraisi
  240. /*
  241. * The reason is as follows.
  242. * We don't show the MS-WORD document at list files of type in OPEN
  243. * DIALOG and SAVE (AS) DIALOG in Japan because the file format of
  244. * MS-WORD(JPN) differs from that of WRITE.
  245. * But I modified only this part, because it is easy to restore when
  246. * we show the MS-WORD document at file type lists in those dialogs.
  247. */
  248. /* DOC */
  249. lstrcpy(lpStr, szDocDescr);
  250. lpStr += lstrlen(lpStr)+1;
  251. lstrcpy(lpStr, szWild);
  252. lstrcat(lpStr, szDefDocExtension);
  253. lpStr += lstrlen(lpStr)+1;
  254. *ppstr++ = szDefDocExtension;
  255. nWord = nCount;
  256. ++nCount;
  257. /* DOC, Text only */
  258. if (iWhichOper == imiSaveAs)
  259. {
  260. lstrcpy(lpStr, szDocTxtDescr);
  261. lpStr += lstrlen(lpStr)+1;
  262. lstrcpy(lpStr, szWild);
  263. lstrcat(lpStr, szDefDocExtension);
  264. lpStr += lstrlen(lpStr)+1;
  265. *ppstr++ = szDefDocExtension;
  266. nWordText = nCount;
  267. ++nCount;
  268. }
  269. #endif // !JAPAN
  270. /* Text only */
  271. lstrcpy(lpStr, szTxtDescr);
  272. lpStr += lstrlen(lpStr)+1;
  273. lstrcpy(lpStr, szWild);
  274. lstrcat(lpStr, szDefTxtExtension);
  275. lpStr += lstrlen(lpStr)+1;
  276. *ppstr++ = szDefTxtExtension;
  277. nTextOnly = nCount;
  278. ++nCount;
  279. /* All files */
  280. lstrcpy(lpStr, szAllFilesDescr);
  281. lpStr += lstrlen(lpStr)+1;
  282. lstrcpy(lpStr, szWild);
  283. lstrcat(lpStr, "*");
  284. lpStr += lstrlen(lpStr)+1;
  285. *ppstr++ = NULL;
  286. ++nCount;
  287. *lpStr = 0;
  288. switch(fDefFileType)
  289. {
  290. case SA_WORDTEXT:
  291. OFN.nFilterIndex = nWordText;
  292. break;
  293. case SA_TEXTONLY:
  294. OFN.nFilterIndex = nTextOnly;
  295. break;
  296. case SA_WORD :
  297. OFN.nFilterIndex = nWord;
  298. break;
  299. case SA_OLDWRITE:
  300. OFN.nFilterIndex = nOldWrite;
  301. break;
  302. case SA_WRITE :
  303. OFN.nFilterIndex = 1;
  304. break;
  305. }
  306. OFN.lpstrDefExt = szDefExtensions[OFN.nFilterIndex - 1];
  307. }
  308. DoOpenFilenameGet(LPSTR lpstrFilenameBuf)
  309. /* returns whether filename retrieved */
  310. {
  311. int nRetval;
  312. if (nRetval = DoCommDlg(imiOpen))
  313. lstrcpy(lpstrFilenameBuf,(LPSTR)szFileName);
  314. return nRetval;
  315. }
  316. DoSaveAsFilenameGet(LPSTR lpstrDefault,LPSTR lpstrFilenameBuf,int *fBackup,int *fTextOnly,int *fWordFmt,int *fOldWriteFmt)
  317. /* Returns whether filename retrieved. Returns filename. If readonly is checked,
  318. returns if readonly is checked in fBackup. Returns file type selected in fTextOonly and
  319. fWordFmt. */
  320. {
  321. int nRetval;
  322. lstrcpy(szFileName,lpstrDefault);
  323. /* see MakeFilterSpec */
  324. if (*fTextOnly && *fWordFmt)
  325. fDefFileType = SA_WORDTEXT;
  326. else if (*fTextOnly)
  327. fDefFileType = SA_TEXTONLY;
  328. else if (*fWordFmt)
  329. fDefFileType = SA_WORD;
  330. else if (*fOldWriteFmt)
  331. fDefFileType = SA_OLDWRITE;
  332. else
  333. fDefFileType = SA_WRITE;
  334. /* check or uncheck backup prompt */
  335. OFN.Flags |= (*fBackup) ? OFN_READONLY : 0;
  336. if (nRetval = DoCommDlg(imiSaveAs))
  337. {
  338. lstrcpy(lpstrFilenameBuf,(LPSTR)szFileName);
  339. if (OFN.nFilterIndex == 1)
  340. {
  341. *fTextOnly = *fWordFmt = FALSE;
  342. *fOldWriteFmt = FALSE;
  343. }
  344. else if (OFN.nFilterIndex == nOldWrite)
  345. {
  346. *fTextOnly = *fWordFmt = FALSE;
  347. *fOldWriteFmt = TRUE;
  348. }
  349. else if (OFN.nFilterIndex == nWord)
  350. {
  351. *fTextOnly = *fOldWriteFmt = FALSE;
  352. *fWordFmt = TRUE;
  353. }
  354. else if (OFN.nFilterIndex == nWordText)
  355. {
  356. *fTextOnly = *fWordFmt = TRUE;
  357. *fOldWriteFmt = FALSE;
  358. }
  359. else if (OFN.nFilterIndex == nTextOnly)
  360. {
  361. *fTextOnly = TRUE;
  362. *fWordFmt = *fOldWriteFmt = FALSE;
  363. }
  364. *fBackup = OFN.Flags & OFN_READONLY;
  365. }
  366. return nRetval;
  367. }
  368.