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.

1175 lines
24 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. coverpg.c
  5. Abstract:
  6. Functions for working with cover pages
  7. Environment:
  8. Windows NT fax driver user interface
  9. Revision History:
  10. 02/05/96 -davidx-
  11. Created it.
  12. mm/dd/yy -author-
  13. description
  14. --*/
  15. #include "faxcpl.h"
  16. #include <shlobj.h>
  17. #include <shellapi.h>
  18. #include <commdlg.h>
  19. #define FAXUTIL_DEBUG
  20. #define FAXUTIL_MEM
  21. #define FAXUTIL_REG
  22. #define FAXUTIL_ADAPTIVE
  23. #include "faxutil.h"
  24. //
  25. // Find the filename portion given a filename:
  26. // return a pointer to the '.' character if successful
  27. // NULL if there is no extension
  28. //
  29. #define FindFilenameExtension(pFilename) _tcsrchr(pFilename, TEXT(FILENAME_EXT))
  30. VOID
  31. AddCoverPagesToList(
  32. PCPDATA pCPInfo,
  33. HWND hwndList,
  34. INT dirIndex
  35. )
  36. /*++
  37. Routine Description:
  38. Add the cover page files in the specified directory to a list
  39. Arguments:
  40. pCPInfo - Points to cover page information
  41. hwndList - Handle to a list window
  42. dirIndex - Cover page directory index
  43. Return Value:
  44. NONE
  45. --*/
  46. {
  47. WIN32_FIND_DATA findData;
  48. TCHAR filename[MAX_PATH];
  49. HANDLE hFindFile;
  50. LPTSTR pDirPath, pFilename, pExtension;
  51. INT listIndex, dirLen, fileLen, flags;
  52. //
  53. // Copy the directory path to a local buffer
  54. //
  55. flags = dirIndex;
  56. pDirPath = pCPInfo->pDirPath[dirIndex];
  57. if (IsEmptyString(pDirPath))
  58. return;
  59. if ((dirLen = _tcslen(pDirPath)) >= MAX_PATH - MAX_FILENAME_EXT - 1) {
  60. Error(("Directory name too long: %ws\n", pDirPath));
  61. return;
  62. }
  63. _tcscpy(filename, pDirPath);
  64. //
  65. // Go through the following loop twice:
  66. // Once to add the files with .ncp extension
  67. // Again to add the files with .lnk extension
  68. //
  69. // Don't chase links for server based cover pages
  70. //
  71. do {
  72. //
  73. // Generate a specification for the files we're interested in
  74. //
  75. pFilename = &filename[dirLen];
  76. *pFilename = TEXT('*');
  77. _tcscpy(pFilename+1, (flags & CPFLAG_LINK) ? LNK_FILENAME_EXT : CP_FILENAME_EXT);
  78. //
  79. // Call FindFirstFile/FindNextFile to enumerate the files
  80. // matching our specification
  81. //
  82. hFindFile = FindFirstFile(filename, &findData);
  83. if (hFindFile != INVALID_HANDLE_VALUE) {
  84. do {
  85. //
  86. // Exclude directories and hidden files
  87. //
  88. if (findData.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_DIRECTORY))
  89. continue;
  90. //
  91. // Make sure we have enough room to store the full pathname
  92. //
  93. if ((fileLen = _tcslen(findData.cFileName)) <= MAX_FILENAME_EXT)
  94. continue;
  95. if (fileLen + dirLen >= MAX_PATH) {
  96. Error(("Filename too long: %ws%ws\n", pDirPath, findData.cFileName));
  97. continue;
  98. }
  99. //
  100. // If we're chasing links, make sure the link refers to
  101. // a cover page file.
  102. //
  103. if (flags & CPFLAG_LINK) {
  104. _tcscpy(pFilename, findData.cFileName);
  105. if (! IsCoverPageShortcut(filename))
  106. continue;
  107. }
  108. //
  109. // Don't display the filename extension
  110. //
  111. if (pExtension = FindFilenameExtension(findData.cFileName))
  112. *pExtension = NUL;
  113. //
  114. // Add the cover page name to the list window
  115. //
  116. listIndex = SendMessage(hwndList,
  117. LB_ADDSTRING,
  118. 0,
  119. (LPARAM) findData.cFileName);
  120. if (listIndex != LB_ERR)
  121. SendMessage(hwndList, LB_SETITEMDATA, listIndex, flags);
  122. } while (FindNextFile(hFindFile, &findData));
  123. FindClose(hFindFile);
  124. }
  125. flags ^= CPFLAG_LINK;
  126. } while ((flags & CPFLAG_LINK) && ! pCPInfo->serverCP);
  127. }
  128. VOID
  129. InitCoverPageList(
  130. PCPDATA pCPInfo,
  131. HWND hDlg
  132. )
  133. /*++
  134. Routine Description:
  135. Generate a list of available cover pages
  136. Arguments:
  137. pCPInfo - Points to cover page information
  138. hDlg - Handle to the dialog window containing cover page list
  139. Return Value:
  140. NONE
  141. --*/
  142. {
  143. HWND hwndList;
  144. INT index, lastSel;
  145. if ((hwndList = GetDlgItem(hDlg, IDC_COVERPG_LIST)) && pCPInfo) {
  146. //
  147. // Disable redraw on the list and reset its content
  148. //
  149. if ((lastSel = SendMessage(hwndList, LB_GETCURSEL, 0, 0)) == LB_ERR)
  150. lastSel = 0;
  151. SendMessage(hwndList, WM_SETREDRAW, FALSE, 0);
  152. SendMessage(hwndList, LB_RESETCONTENT, 0, 0);
  153. //
  154. // Add cover pages to the list
  155. //
  156. for (index=0; index < pCPInfo->nDirs; index++)
  157. AddCoverPagesToList(pCPInfo, hwndList, index);
  158. //
  159. // Highlight the first cover page in the list
  160. //
  161. if ((index = SendMessage(hwndList, LB_GETCOUNT, 0, 0)) > 0 && lastSel >= index)
  162. lastSel = index - 1;
  163. SendMessage(hwndList, LB_SETCURSEL, lastSel, 0);
  164. //
  165. // Enable redraw on the list window
  166. //
  167. SendMessage(hwndList, WM_SETREDRAW, TRUE, 0);
  168. } else if (hwndList) {
  169. SendMessage(hwndList, LB_RESETCONTENT, 0, 0);
  170. }
  171. UpdateCoverPageControls(hDlg);
  172. }
  173. INT
  174. GetSelectedCoverPage(
  175. PCPDATA pCPInfo,
  176. HWND hwndList,
  177. LPTSTR pBuffer
  178. )
  179. /*++
  180. Routine Description:
  181. Retrieve the currently selected cover page filename
  182. Arguments:
  183. pCPInfo - Points to cover page information
  184. hwndList - Handle to the list window
  185. pBuffer - Points to a buffer for storing the selected cover page filename
  186. The size of the buffer is assumed to be MAX_PATH characters.
  187. if pBuffer is NULL, we assume the called is interested in the item flags
  188. Return Value:
  189. Flags associated with the currently selected item
  190. Negative if there is an error
  191. --*/
  192. {
  193. LPTSTR pDirPath;
  194. INT selIndex, itemFlags;
  195. //
  196. // Default to empty string in case of an error
  197. //
  198. if (pBuffer)
  199. pBuffer[0] = NUL;
  200. if (pCPInfo == NULL || hwndList == NULL)
  201. return LB_ERR;
  202. //
  203. // Get currently selected item index
  204. //
  205. if ((selIndex = SendMessage(hwndList, LB_GETCURSEL, 0, 0)) == LB_ERR)
  206. return selIndex;
  207. //
  208. // Get the flags associated with the currently selected item
  209. //
  210. itemFlags = SendMessage(hwndList, LB_GETITEMDATA, selIndex, 0);
  211. if (itemFlags == LB_ERR || !pBuffer)
  212. return itemFlags;
  213. Assert((itemFlags & CPFLAG_DIRINDEX) < pCPInfo->nDirs);
  214. pDirPath = pCPInfo->pDirPath[itemFlags & CPFLAG_DIRINDEX];
  215. //
  216. // Assemble the full pathname for the cover page file
  217. // directory prefix
  218. // display name
  219. // filename extension
  220. //
  221. while (*pBuffer++ = *pDirPath++)
  222. NULL;
  223. pBuffer--;
  224. SendMessage(hwndList, LB_GETTEXT, selIndex, (LPARAM) pBuffer);
  225. _tcscat(pBuffer, (itemFlags & CPFLAG_LINK) ? LNK_FILENAME_EXT : CP_FILENAME_EXT);
  226. return itemFlags;
  227. }
  228. BOOL
  229. GetServerCoverPageDirs(
  230. PCPDATA pCPInfo
  231. )
  232. /*++
  233. Routine Description:
  234. Find the directories in which the server cover pages are stored
  235. Arguments:
  236. hPrinter - Handle to a printer object
  237. pCPInfo - Points to cover page information
  238. Return Value:
  239. TRUE if successful, FALSE otherwise
  240. --*/
  241. {
  242. BOOL status = FALSE;
  243. DWORD cbNeeded;
  244. LPTSTR pServerDir;
  245. //
  246. // Find the driver directory on the printer server
  247. //
  248. cbNeeded = (_tcslen(gConfigData->pServerName) + _tcslen(SERVER_CP_DIRECTORY) + 16) * sizeof(TCHAR);
  249. if (pServerDir = MemAllocZ(cbNeeded)) {
  250. _tcscpy( pServerDir, gConfigData->pServerName );
  251. _tcscat( pServerDir, SERVER_CP_DIRECTORY );
  252. Assert(pCPInfo->nDirs == 0);
  253. pCPInfo->nDirs = 1;
  254. pCPInfo->pDirPath[0] = pServerDir;
  255. status = TRUE;
  256. }
  257. //
  258. // Clean up before returning to caller
  259. //
  260. if (! status) {
  261. MemFree(pServerDir);
  262. }
  263. return status;
  264. }
  265. VOID
  266. AppendPathSeparator(
  267. LPTSTR pDirPath
  268. )
  269. /*++
  270. Routine Description:
  271. Append a path separator (if necessary) at the end of a directory name
  272. Arguments:
  273. pDirPath - Points to a directory name
  274. Return Value:
  275. NONE
  276. --*/
  277. {
  278. INT length;
  279. //
  280. // Calculate the length of directory string
  281. //
  282. length = _tcslen(pDirPath);
  283. if (length >= MAX_PATH-1 || length < 1)
  284. return;
  285. //
  286. // If the last character is not a path separator,
  287. // append a path separator at the end
  288. //
  289. if (pDirPath[length-1] != TEXT(PATH_SEPARATOR)) {
  290. pDirPath[length] = TEXT(PATH_SEPARATOR);
  291. pDirPath[length+1] = NUL;
  292. }
  293. }
  294. PCPDATA
  295. AllocCoverPageInfo(
  296. BOOL serverCP
  297. )
  298. /*++
  299. Routine Description:
  300. Allocate memory to hold cover page information
  301. Arguments:
  302. serverCP - Is the caller interested in server or user cover pages
  303. hPrinter - Handle to a printer object if serverCP is TRUE
  304. Return Value:
  305. Pointer to a CPDATA structure, NULL if there is an error
  306. NOTE:
  307. Put this inside a critical section is the caller is concerned about
  308. being thread safe.
  309. --*/
  310. {
  311. PCPDATA pCPInfo;
  312. INT nDirs;
  313. LPTSTR pDirPath, pUserCPDir, pSavedPtr;
  314. if (pCPInfo = MemAllocZ(sizeof(CPDATA))) {
  315. if (pCPInfo->serverCP = serverCP) {
  316. //
  317. // Find the directory in which the server cover pages are stored
  318. //
  319. if (! GetServerCoverPageDirs(pCPInfo))
  320. Error(("Couldn't get server cover page directories\n"));
  321. } else if (pUserCPDir = pSavedPtr = GetUserCoverPageDir()) {
  322. //
  323. // Find the directory in which the user cover pages are stored
  324. //
  325. while (*pUserCPDir && pCPInfo->nDirs < MAX_COVERPAGE_DIRS) {
  326. LPTSTR pNextDir = pUserCPDir;
  327. //
  328. // Find the next semicolon character
  329. //
  330. while (*pNextDir && *pNextDir != TEXT(';'))
  331. pNextDir++;
  332. if (*pNextDir != NUL)
  333. *pNextDir++ = NUL;
  334. //
  335. // Make sure the directory name is not too long
  336. //
  337. if (_tcslen(pUserCPDir) < MAX_PATH) {
  338. if (! (pDirPath = AllocStringZ(MAX_PATH)))
  339. break;
  340. pCPInfo->pDirPath[pCPInfo->nDirs++] = pDirPath;
  341. _tcscpy(pDirPath, pUserCPDir);
  342. }
  343. pUserCPDir = pNextDir;
  344. }
  345. MemFree(pSavedPtr);
  346. }
  347. //
  348. // Append path separators at the end if necessary
  349. //
  350. for (nDirs=0; nDirs < pCPInfo->nDirs; nDirs++) {
  351. AppendPathSeparator(pCPInfo->pDirPath[nDirs]);
  352. Verbose(("Cover page directory: %ws\n", pCPInfo->pDirPath[nDirs]));
  353. }
  354. }
  355. return pCPInfo;
  356. }
  357. VOID
  358. FreeCoverPageInfo(
  359. PCPDATA pCPInfo
  360. )
  361. /*++
  362. Routine Description:
  363. Free up memory used for cover page information
  364. Arguments:
  365. pCPInfo - Points to cover page information to be freed
  366. Return Value:
  367. NONE
  368. --*/
  369. {
  370. if (pCPInfo) {
  371. INT index;
  372. for (index=0; index < pCPInfo->nDirs; index++)
  373. MemFree(pCPInfo->pDirPath[index]);
  374. MemFree(pCPInfo);
  375. }
  376. }
  377. LPTSTR
  378. MakeQuotedParameterString(
  379. LPTSTR pInputStr
  380. )
  381. /*++
  382. Routine Description:
  383. Make a copy of the input string and make sure it's in the same form
  384. as expected by SHELLEXECUTEINFO.lpParameters.
  385. Arguments:
  386. pInputStr - Specifies the input string
  387. Return Value:
  388. Pointer to the processed parameter string
  389. --*/
  390. #define QUOTE TEXT('"')
  391. {
  392. LPTSTR pStr, pDestStr;
  393. INT length;
  394. //
  395. // Special case: if the input string is NULL, simply return NULL
  396. //
  397. if (pInputStr == NULL)
  398. return NULL;
  399. //
  400. // Figure out how long the resulting string is.
  401. // Initial value is 3 = two extra quotes plus NUL terminator.
  402. //
  403. for (pStr=pInputStr, length=3; *pStr; pStr++)
  404. length += (*pStr == QUOTE) ? 3 : 1;
  405. //
  406. // Copy the input string and replace quote characters
  407. //
  408. if (pStr = pDestStr = MemAlloc(length * sizeof(TCHAR))) {
  409. *pStr++ = QUOTE;
  410. while (*pInputStr) {
  411. if ((*pStr++ = *pInputStr++) == QUOTE) {
  412. *pStr++ = QUOTE;
  413. *pStr++ = QUOTE;
  414. }
  415. }
  416. *pStr++ = QUOTE;
  417. *pStr = NUL;
  418. }
  419. return pDestStr;
  420. }
  421. VOID
  422. HandleOpenCoverPage(
  423. HWND hDlg,
  424. PCPDATA pCPInfo,
  425. HWND hwndList,
  426. LPTSTR pSelected,
  427. INT action
  428. )
  429. /*++
  430. Routine Description:
  431. Edit the currently selected cover page file or
  432. create a new cover page file
  433. Arguments:
  434. hDlg - Handle to the dialog window on which the list of cover pages is displayed
  435. pCPInfo - Points to cover page information
  436. hwndList - Handle to cover page listbox window
  437. pSelected - Currently selected cover page filename
  438. action - Open an existing cover page file or create a new one
  439. Return Value:
  440. NONE
  441. --*/
  442. {
  443. TCHAR filename[MAX_PATH];
  444. LPTSTR pExecutableName, pDirPath, pFilename;
  445. SHELLEXECUTEINFO shellExeInfo = {
  446. sizeof(SHELLEXECUTEINFO),
  447. SEE_MASK_NOCLOSEPROCESS,
  448. hDlg,
  449. NULL,
  450. NULL,
  451. NULL,
  452. NULL,
  453. SW_SHOWNORMAL,
  454. };
  455. //
  456. // Determine the default directory to run the cover page editor in:
  457. //
  458. if (action == CPACTION_NEW) {
  459. //
  460. // When creating a new cover page, the default directory is either
  461. // the server cover page directory or the user cover page directory
  462. // depending on whether the user is doing server adminstration.
  463. //
  464. pDirPath = pCPInfo->pDirPath[0];
  465. pFilename = NULL;
  466. } else {
  467. INT flags;
  468. //
  469. // If the currently selected file is a link, resolve it first
  470. //
  471. _tcscpy(filename, pSelected);
  472. if (!IsEmptyString(pSelected) &&
  473. (flags = GetSelectedCoverPage(pCPInfo, hwndList, NULL)) > 0 &&
  474. (flags & CPFLAG_LINK) &&
  475. !ResolveShortcut(pSelected, filename))
  476. {
  477. DisplayMessageDialog(hDlg, 0, 0, IDS_RESOLVE_LINK_FAILED, pSelected);
  478. return;
  479. }
  480. //
  481. // Separate the filename into directory and filename components
  482. //
  483. if (pFilename = _tcsrchr(filename, TEXT(PATH_SEPARATOR))) {
  484. *pFilename++ = NUL;
  485. pDirPath = filename;
  486. } else {
  487. pFilename = filename;
  488. pDirPath = NULL;
  489. }
  490. }
  491. //
  492. // Find the "Cover Page Editor" executable
  493. //
  494. if ((pExecutableName = GetCoverPageEditor()) == NULL) {
  495. DisplayMessageDialog(hDlg, 0, 0, IDS_CANNOT_FIND_CPEDITOR);
  496. return;
  497. }
  498. //
  499. // Start cover page editor and wait for it to exit before proceeding
  500. //
  501. shellExeInfo.lpFile = pExecutableName;
  502. shellExeInfo.lpDirectory = pDirPath;
  503. shellExeInfo.lpParameters = MakeQuotedParameterString(pFilename);
  504. Verbose(("Cover page editor: %ws\n", pExecutableName));
  505. Verbose(("Initial working directory: %ws\n", pDirPath));
  506. Verbose(("Cover page filename: %ws\n", shellExeInfo.lpParameters));
  507. if (! ShellExecuteEx(&shellExeInfo)) {
  508. DisplayMessageDialog(hDlg, 0, 0, IDS_CANNOT_OPEN_CPEDITOR, pExecutableName);
  509. MemFree(shellExeInfo.lpParameters);
  510. MemFree(pExecutableName);
  511. return;
  512. }
  513. //
  514. // Refresh the list of cover page files when we're done
  515. //
  516. MemFree(shellExeInfo.lpParameters);
  517. MemFree(pExecutableName);
  518. if (WaitForSingleObject(shellExeInfo.hProcess, INFINITE) != WAIT_FAILED)
  519. InitCoverPageList(pCPInfo, hDlg);
  520. }
  521. VOID
  522. HandleBrowseCoverPage(
  523. HWND hDlg,
  524. PCPDATA pCPInfo,
  525. HWND hwndList,
  526. LPTSTR pSelected
  527. )
  528. /*++
  529. Routine Description:
  530. Remove the currently selected cover page file
  531. Arguments:
  532. hDlg - Handle to the dialog window on which the list of cover pages is displayed
  533. pCPInfo - Points to cover page information
  534. hwndList - Handle to cover page listbox window
  535. pSelected - Currently selected cover page filename
  536. Return Value:
  537. NONE
  538. --*/
  539. {
  540. TCHAR filename[MAX_PATH];
  541. TCHAR title[MAX_TITLE_LEN];
  542. TCHAR filter[MAX_TITLE_LEN];
  543. LPTSTR pExtension, pFilename;
  544. LPTSTR pCPDir;
  545. INT n;
  546. OPENFILENAME ofn = {
  547. sizeof(OPENFILENAME),
  548. hDlg,
  549. ghInstance,
  550. filter,
  551. NULL,
  552. 0,
  553. 1,
  554. filename,
  555. MAX_PATH,
  556. NULL,
  557. 0,
  558. NULL,
  559. title,
  560. OFN_FILEMUSTEXIST | OFN_NODEREFERENCELINKS | OFN_HIDEREADONLY,
  561. 0,
  562. 0,
  563. NULL,
  564. 0,
  565. NULL,
  566. NULL,
  567. };
  568. //
  569. // Figure out what the initial directory should be
  570. //
  571. if (! IsEmptyString(pSelected)) {
  572. INT flags;
  573. //
  574. // Find out if the currently selected cover page file is a
  575. // user cover page and whether it's a link
  576. //
  577. if ((flags = GetSelectedCoverPage(pCPInfo, hwndList, NULL)) > 0 &&
  578. (flags & CPFLAG_LINK) &&
  579. ResolveShortcut(pSelected, filename))
  580. {
  581. //
  582. // Set the initial directory to the link destination
  583. //
  584. _tcscpy(pSelected, filename);
  585. if (pFilename = _tcsrchr(pSelected, TEXT(PATH_SEPARATOR))) {
  586. *pFilename = NUL;
  587. ofn.lpstrInitialDir = pSelected;
  588. }
  589. }
  590. }
  591. //
  592. // Compose the file-type filter string
  593. //
  594. LoadString(ghInstance, IDS_CP_FILETYPE, title, MAX_TITLE_LEN);
  595. wsprintf(filter, TEXT("%s%c*%s%c"), title, NUL, CP_FILENAME_EXT, NUL);
  596. LoadString(ghInstance, IDS_BROWSE_COVERPAGE, title, MAX_TITLE_LEN);
  597. filename[0] = NUL;
  598. //
  599. // Present the "Open File" dialog
  600. //
  601. if (! GetOpenFileName(&ofn))
  602. return;
  603. //
  604. // Make sure the selected filename has the correct extension
  605. //
  606. if ((pExtension = FindFilenameExtension(filename)) == NULL ||
  607. _tcsicmp(pExtension, CP_FILENAME_EXT) != EQUAL_STRING)
  608. {
  609. DisplayMessageDialog(hDlg, 0, 0, IDS_BAD_CP_EXTENSION, CP_FILENAME_EXT);
  610. return;
  611. }
  612. //
  613. // Check if the selected file is already inside one of the
  614. // cover page directories
  615. //
  616. for (n=0; n < pCPInfo->nDirs; n++) {
  617. if (_tcsnicmp(filename, pCPInfo->pDirPath[n], ofn.nFileOffset) == EQUAL_STRING) {
  618. DisplayMessageDialog(hDlg, 0, 0, IDS_CP_DUPLICATE, filename);
  619. return;
  620. }
  621. }
  622. //
  623. // Add the selected cover page file to the first cover page directory
  624. // Create the cover page directory if necessary
  625. //
  626. pCPDir = pCPInfo->pDirPath[0];
  627. if (!pCPDir || IsEmptyString(pCPDir)) {
  628. DisplayMessageDialog(hDlg, 0, 0, IDS_NO_COVERPG_DIR);
  629. return;
  630. }
  631. CreateDirectory(pCPDir, NULL);
  632. pFilename = &filename[ofn.nFileOffset];
  633. _tcscpy(pSelected, pCPDir);
  634. n = _tcslen(pSelected);
  635. if (n + _tcslen(pFilename) >= MAX_PATH - MAX_FILENAME_EXT || pFilename >= pExtension) {
  636. DisplayMessageDialog(hDlg, 0, 0, IDS_FILENAME_TOOLONG);
  637. return;
  638. }
  639. _tcsncpy(pSelected + n, pFilename, pExtension - pFilename);
  640. n += pExtension - pFilename;
  641. if (pCPInfo->serverCP) {
  642. //
  643. // Copy the physical file for server cover pages
  644. //
  645. _tcscpy(pSelected + n, CP_FILENAME_EXT);
  646. if (! CopyFile(filename, pSelected, FALSE)) {
  647. DisplayMessageDialog(hDlg, 0, 0, IDS_COPY_FILE_FAILED, filename, pSelected);
  648. return;
  649. }
  650. } else {
  651. //
  652. // Create the shortcut file for user cover page
  653. //
  654. _tcscpy(pSelected + n, LNK_FILENAME_EXT);
  655. if (! CreateShortcut(pSelected, filename)) {
  656. DisplayMessageDialog(hDlg, 0, 0, IDS_CREATE_LINK_FAILED, pSelected, filename);
  657. return;
  658. }
  659. }
  660. //
  661. // Refresh the cover page list - we're being lazy here in that
  662. // we reset the entire list content
  663. //
  664. InitCoverPageList(pCPInfo, hDlg);
  665. }
  666. VOID
  667. HandleRemoveCoverPage(
  668. HWND hDlg,
  669. PCPDATA pCPInfo,
  670. HWND hwndList,
  671. LPTSTR pFilename
  672. )
  673. /*++
  674. Routine Description:
  675. Remove the currently selected cover page file
  676. Arguments:
  677. hDlg - Handle to the dialog window on which the list of cover pages is displayed
  678. pCPInfo - Points to cover page information
  679. hwndList - Handle to cover page listbox window
  680. pFilename - Currently selected cover page filename
  681. Return Value:
  682. NONE
  683. --*/
  684. {
  685. //
  686. // Display the confirmation dialog before proceeding
  687. //
  688. if (DisplayMessageDialog(hDlg,
  689. MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2,
  690. IDS_CONFIRM_DELETE,
  691. IDS_DELETE_PROMPT,
  692. pFilename) == IDYES)
  693. {
  694. if (DeleteFile(pFilename)) {
  695. //
  696. // Update the list box if the file is successfully removed
  697. //
  698. INT selIndex, count;
  699. if ((selIndex = SendMessage(hwndList, LB_GETCURSEL, 0, 0)) != LB_ERR) {
  700. SendMessage(hwndList, LB_DELETESTRING, selIndex, 0);
  701. if ((count = SendMessage(hwndList, LB_GETCOUNT, 0, 0)) > 0) {
  702. count --;
  703. SendMessage(hwndList, LB_SETCURSEL, min(selIndex, count), 0);
  704. }
  705. }
  706. UpdateCoverPageControls(hDlg);
  707. } else
  708. DisplayMessageDialog(hDlg, 0, 0, IDS_DELETE_FAILED, pFilename);
  709. }
  710. }
  711. VOID
  712. ManageCoverPageList(
  713. HWND hDlg,
  714. PCPDATA pCPInfo,
  715. HWND hwndList,
  716. INT action
  717. )
  718. /*++
  719. Routine Description:
  720. Perform various action to manage the list of cover pages
  721. Arguments:
  722. hDlg - Handle to the dialog window on which the list of cover pages is displayed
  723. pCPInfo - Points to cover page information
  724. hwndList - Handle to cover page listbox window
  725. action - What action to perform on the cover page list
  726. Return Value:
  727. NONE
  728. --*/
  729. {
  730. TCHAR filename[MAX_PATH];
  731. //
  732. // Get the name of currently selected cover page file
  733. //
  734. if (pCPInfo == NULL || hwndList == NULL)
  735. return;
  736. GetSelectedCoverPage(pCPInfo, hwndList, filename);
  737. //
  738. // Call appropriate function depends on the action parameter
  739. //
  740. switch (action) {
  741. case CPACTION_OPEN:
  742. if (IsEmptyString(filename))
  743. break;
  744. case CPACTION_NEW:
  745. HandleOpenCoverPage(hDlg, pCPInfo, hwndList, filename, action);
  746. break;
  747. case CPACTION_BROWSE:
  748. HandleBrowseCoverPage(hDlg, pCPInfo, hwndList, filename);
  749. break;
  750. case CPACTION_REMOVE:
  751. if (! IsEmptyString(filename))
  752. HandleRemoveCoverPage(hDlg, pCPInfo, hwndList, filename);
  753. break;
  754. }
  755. }
  756. VOID
  757. UpdateCoverPageControls(
  758. HWND hDlg
  759. )
  760. /*++
  761. Routine Description:
  762. Enable/disable buttons for manage cover page files
  763. Arguments:
  764. hDlg - Handle to the property page containing the cover page controls
  765. Return Value:
  766. NONE
  767. --*/
  768. {
  769. HWND hwndOpen, hwndRemove;
  770. //
  771. // If all buttons are disabled, leave them alone here
  772. //
  773. if (! IsWindowEnabled(GetDlgItem(hDlg, IDC_COVERPG_NEW)))
  774. return;
  775. hwndOpen = GetDlgItem(hDlg, IDC_COVERPG_OPEN);
  776. hwndRemove = GetDlgItem(hDlg, IDC_COVERPG_REMOVE);
  777. if (SendDlgItemMessage(hDlg, IDC_COVERPG_LIST, LB_GETCURSEL, 0, 0) != LB_ERR) {
  778. EnableWindow(hwndOpen, TRUE);
  779. EnableWindow(hwndRemove, TRUE);
  780. } else {
  781. if (GetFocus() == hwndOpen || GetFocus() == hwndRemove)
  782. SetFocus(GetDlgItem(hDlg, IDC_COVERPG_NEW));
  783. EnableWindow(hwndOpen, FALSE);
  784. EnableWindow(hwndRemove, FALSE);
  785. }
  786. }