Windows NT 4.0 source code leak
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.

790 lines
24 KiB

4 years ago
  1. /************************************************************************\
  2. *
  3. * FUNCTION: SepDlgProc();
  4. *
  5. * PURPOSE: Handle the Separator Page dialog box messages, and record
  6. * settings into Registry
  7. *
  8. * Created: 7-27-1995
  9. *
  10. \************************************************************************/
  11. #include <windows.h>
  12. #include <winspool.h>
  13. #include <commdlg.h>
  14. #include "sepdlg.h"
  15. #include "textbox.h"
  16. #include "tables.h"
  17. LRESULT APIENTRY SepDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  18. LRESULT APIENTRY SepPreviewDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  19. LRESULT SepDlgInitialize(HWND hDlg);
  20. void SepEnableExtraControls(HWND hDlg);
  21. void SepEnableAttachControls(HWND hDlg);
  22. BOOL SepIsValidSepFile(PTCHAR szFileName);
  23. void SepSelectAttachSepFile(HWND hDlg);
  24. void SepSelectExtraSepFile(HWND hDlg);
  25. BOOL SepSelectFileDlg(HWND hDlg, PTCHAR szFileName);
  26. void SepSetExtraPageProperties(HWND hDlg);
  27. HANDLE SepGetDefaultPrinterHandle();
  28. void SepInitializePrinterInfo();
  29. void SepCustomLayout(HWND hDlg);
  30. void SepSetDlgBoldFont(HWND hDlg);
  31. void SepSetDefaultSettings(HWND hDlg);
  32. void SepSetDefaultDevMode();
  33. BOOL SepSaveSettings(HWND hDlg);
  34. BOOL SepValidateSettings(HWND hDlg);
  35. BOOL SepLoadSettings(HWND hDlg);
  36. void SepTestExtraPage(HWND hDlg);
  37. void SepTestAttachPage(HWND hDlg);
  38. void SepPreviewExtraPage(HWND hDlg);
  39. void SepPreviewAttachPage(HWND hDlg);
  40. void SepPrintSepFile(PTCHAR szFileName, DEVMODE* pDevMode);
  41. void SepPreviewSepFile(HWND hDlg, PTCHAR szFileName, DEVMODE* pDevMode);
  42. void SepPreviewInit(HWND hDlg);
  43. void SepPreviewPaint(HWND hDlg);
  44. void SepDrawSepPage(HDC hDC, RECT* pBound, PTCHAR szFileName);
  45. static HANDLE hSepPrinter;
  46. static PRINTER_INFO_2 * pSepPrinter;
  47. static DEVMODE* pSepDevMode;
  48. static HDC hSepPrtDC;
  49. static RECT rcSepView;
  50. static TCHAR szSepViewFile[MAX_PATH+1];
  51. static const PTCHAR szSepRegistryRoot = L"System\\CurrentControlSet\\Control\\Print\\Printers\\";
  52. static const PTCHAR szSepKey = L"Separator Page";
  53. LRESULT APIENTRY SepDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  54. {
  55. switch (uMsg)
  56. {
  57. case WM_INITDIALOG:
  58. return SepDlgInitialize(hDlg);
  59. case WM_SYSCOMMAND:
  60. if (wParam == SC_CLOSE)
  61. {
  62. EndDialog (hDlg, TRUE);
  63. return TRUE;
  64. }
  65. break;
  66. case WM_COMMAND:
  67. switch (LOWORD(wParam))
  68. {
  69. case IDOK:
  70. if (!SepValidateSettings(hDlg))
  71. {
  72. MessageBox(hDlg, L"Invalid Separator Page Settings",
  73. L"Separator Page Settings", MB_OK | MB_ICONEXCLAMATION);
  74. break;
  75. }
  76. if (!SepSaveSettings(hDlg))
  77. {
  78. MessageBox(hDlg, L"Can't Save Current Settings\nCheck Registry Permissions",
  79. L"Separator Page Settings", MB_OK | MB_ICONEXCLAMATION);
  80. break;
  81. }
  82. EndDialog(hDlg, IDOK);
  83. case IDCANCEL:
  84. EndDialog(hDlg, IDCANCEL);
  85. case IDC_EXTRACHECK:
  86. if (HIWORD(wParam) == BN_CLICKED) SepEnableExtraControls(hDlg);
  87. break;
  88. case IDC_ATTACHCHECK:
  89. if (HIWORD(wParam) == BN_CLICKED) SepEnableAttachControls(hDlg);
  90. break;
  91. case IDC_EXTRAFILEEDIT:
  92. if (HIWORD(wParam) == EN_CHANGE) SepEnableExtraControls(hDlg);
  93. break;
  94. case IDC_ATTACHFILEEDIT:
  95. if (HIWORD(wParam) == EN_CHANGE) SepEnableAttachControls(hDlg);
  96. break;
  97. case IDC_EXTRAFILEBUTTON:
  98. if (HIWORD(wParam) == BN_CLICKED) SepSelectExtraSepFile(hDlg);
  99. break;
  100. case IDC_ATTACHFILEBUTTON:
  101. if (HIWORD(wParam) == BN_CLICKED) SepSelectAttachSepFile(hDlg);
  102. break;
  103. case IDC_BEGINRADIO:
  104. case IDC_ENDRADIO:
  105. if (HIWORD(wParam) == BN_CLICKED) SepEnableExtraControls(hDlg);
  106. break;
  107. case IDC_EXTRATESTBUTTON:
  108. if (HIWORD(wParam) == BN_CLICKED) SepTestExtraPage(hDlg);
  109. break;
  110. case IDC_ATTACHTESTBUTTON:
  111. if (HIWORD(wParam) == BN_CLICKED) SepTestAttachPage(hDlg);
  112. break;
  113. case IDC_EXTRAPREVIEWBUTTON:
  114. if (HIWORD(wParam) == BN_CLICKED) SepPreviewExtraPage(hDlg);
  115. break;
  116. case IDC_ATTACHPREVIEWBUTTON:
  117. if (HIWORD(wParam) == BN_CLICKED) SepPreviewAttachPage(hDlg);
  118. break;
  119. case IDC_EXTRAPAGEBUTTON:
  120. if (HIWORD(wParam) == BN_CLICKED) SepSetExtraPageProperties(hDlg);
  121. break;
  122. case IDC_EXTRACUSTOMBUTTON:
  123. if (HIWORD(wParam) == BN_CLICKED) SepCustomLayout(hDlg);
  124. break;
  125. case IDC_ATTACHCUSTOMBUTTON:
  126. if (HIWORD(wParam) == BN_CLICKED) SepCustomLayout(hDlg);
  127. break;
  128. case IDC_DEFAULTBUTTON:
  129. if (HIWORD(wParam) == BN_CLICKED) SepSetDefaultSettings(hDlg);
  130. break;
  131. default:
  132. return FALSE;
  133. }
  134. return TRUE;
  135. }
  136. return FALSE;
  137. }
  138. LRESULT SepDlgInitialize(HWND hDlg)
  139. {
  140. // to be replaced by appropriate resource definition
  141. SepSetDlgBoldFont(hDlg);
  142. // to be replaced by handle of the printer undergoing configuration
  143. // in printer manager
  144. hSepPrinter = SepGetDefaultPrinterHandle();
  145. // may also need minor change to incorporate into printman
  146. SepInitializePrinterInfo();
  147. // load current settings
  148. if (!SepLoadSettings(hDlg)) SepSetDefaultSettings(hDlg);
  149. // disable sub-controls when main control not checked
  150. SepEnableExtraControls(hDlg);
  151. SepEnableAttachControls(hDlg);
  152. // set initial focus to OK button
  153. SetFocus(GetDlgItem(hDlg, IDOK));
  154. return FALSE;
  155. }
  156. void SepSetDlgBoldFont(HWND hDlg)
  157. {
  158. HFONT hFont;
  159. LOGFONT lfFont;
  160. HWND hWnd;
  161. hFont = (HFONT)SendMessage(hDlg, WM_GETFONT, 0, 0);
  162. if (hFont)
  163. {
  164. if (GetObject(hFont, sizeof(LOGFONT), &lfFont))
  165. {
  166. lfFont.lfWeight = FW_BOLD;
  167. if (hFont = CreateFontIndirect(&lfFont))
  168. for (hWnd=GetDlgItem(hDlg, IDC_EXTRACHECK); hWnd; hWnd = GetNextWindow(hWnd, GW_HWNDNEXT))
  169. SendMessage(hWnd, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(FALSE,0));
  170. }
  171. }
  172. }
  173. void SepEnableExtraControls(HWND hDlg)
  174. {
  175. int iId;
  176. BOOL bChecked;
  177. BOOL bFileExist = FALSE;
  178. TCHAR szFileName[MAX_PATH+1];
  179. // get the extra check button state
  180. bChecked = IsDlgButtonChecked(hDlg, IDC_EXTRACHECK);
  181. // check if sep file is valid
  182. if (bChecked && GetDlgItemText(hDlg, IDC_EXTRAFILEEDIT, szFileName, MAX_PATH))
  183. bFileExist = SepIsValidSepFile(szFileName);
  184. // enable/disable controls
  185. for (iId=IDC_EXTRAFILELABEL; iId <= IDC_EXTRAPAGEBUTTON; iId++)
  186. {
  187. switch (iId)
  188. {
  189. case IDC_EXTRATESTBUTTON:
  190. case IDC_EXTRAPREVIEWBUTTON:
  191. EnableWindow(GetDlgItem(hDlg, iId), bChecked && bFileExist);
  192. break;
  193. case IDC_EXTRABETWEEN:
  194. case IDC_ADJRADIO:
  195. case IDC_DIFFRADIO:
  196. EnableWindow(GetDlgItem(hDlg, iId), bChecked &&
  197. IsDlgButtonChecked(hDlg, IDC_BEGINRADIO));
  198. break;
  199. default:
  200. EnableWindow(GetDlgItem(hDlg, iId), bChecked);
  201. break;
  202. }
  203. }
  204. }
  205. void SepEnableAttachControls(HWND hDlg)
  206. {
  207. int iId;
  208. BOOL bChecked;
  209. BOOL bFileExist = FALSE;
  210. TCHAR szFileName[MAX_PATH+1];
  211. // get the attach check button state
  212. bChecked = IsDlgButtonChecked(hDlg, IDC_ATTACHCHECK);
  213. // check if sep file is valid
  214. if (bChecked && GetDlgItemText(hDlg, IDC_ATTACHFILEEDIT, szFileName, MAX_PATH))
  215. bFileExist = SepIsValidSepFile(szFileName);
  216. // enable/disable controls
  217. for (iId=IDC_ATTACHFILELABEL; iId <= IDC_ATTACHCUSTOMBUTTON; iId++)
  218. {
  219. switch (iId)
  220. {
  221. case IDC_ATTACHTESTBUTTON:
  222. case IDC_ATTACHPREVIEWBUTTON:
  223. EnableWindow(GetDlgItem(hDlg, iId), bChecked && bFileExist);
  224. break;
  225. default:
  226. EnableWindow(GetDlgItem(hDlg, iId), bChecked);
  227. break;
  228. }
  229. }
  230. }
  231. BOOL SepIsValidSepFile(PTCHAR szFileName)
  232. {
  233. DWORD dwAttr;
  234. dwAttr = GetFileAttributes(szFileName);
  235. if (dwAttr == 0xFFFFFFFF) return FALSE;
  236. return !(dwAttr & FILE_ATTRIBUTE_DIRECTORY) && !(dwAttr & FILE_ATTRIBUTE_SYSTEM);
  237. }
  238. void SepSelectExtraSepFile(HWND hDlg)
  239. {
  240. TCHAR szFileName[MAX_PATH+1];
  241. if (!GetDlgItemText(hDlg, IDC_EXTRAFILEEDIT, szFileName, MAX_PATH))
  242. szFileName[0] = 0;
  243. if (SepSelectFileDlg(hDlg, szFileName))
  244. {
  245. SetDlgItemText(hDlg, IDC_EXTRAFILEEDIT, szFileName);
  246. SepEnableExtraControls(hDlg);
  247. }
  248. }
  249. void SepSelectAttachSepFile(HWND hDlg)
  250. {
  251. TCHAR szFileName[MAX_PATH+1];
  252. if (!GetDlgItemText(hDlg, IDC_ATTACHFILEEDIT, szFileName, MAX_PATH))
  253. szFileName[0] = 0;
  254. if (SepSelectFileDlg(hDlg, szFileName))
  255. {
  256. SetDlgItemText(hDlg, IDC_ATTACHFILEEDIT, szFileName);
  257. SepEnableAttachControls(hDlg);
  258. }
  259. }
  260. BOOL SepSelectFileDlg(HWND hDlg, PTCHAR szFileName)
  261. {
  262. OPENFILENAME ofn;
  263. memset(&ofn, 0, sizeof(OPENFILENAME));
  264. ofn.lStructSize = sizeof(OPENFILENAME);
  265. ofn.hwndOwner = hDlg;
  266. ofn.lpstrFilter = L"Separator Files (*.SEP)\0*.SEP\0";
  267. ofn.lpstrFile = szFileName;
  268. ofn.nMaxFile = MAX_PATH;
  269. ofn.lpstrTitle = L"Select Separator File";
  270. ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
  271. return GetOpenFileName(&ofn);
  272. }
  273. void SepSetExtraPageProperties(HWND hDlg)
  274. {
  275. LONG lSize;
  276. DEVMODE* pOutDevMode;
  277. if (!pSepDevMode) return;
  278. lSize = DocumentProperties(NULL, hSepPrinter, pSepPrinter->pPrinterName, NULL, NULL, 0);
  279. if (!(pOutDevMode = (DEVMODE*)HeapAlloc(GetProcessHeap(), 0, lSize))) return;
  280. if (DocumentProperties(hDlg, hSepPrinter, pSepPrinter->pPrinterName, pOutDevMode,
  281. pSepDevMode, DM_IN_BUFFER |DM_IN_PROMPT | DM_OUT_BUFFER) == IDOK)
  282. {
  283. HeapFree(GetProcessHeap(), 0, pSepDevMode);
  284. pSepDevMode = pOutDevMode;
  285. }
  286. else
  287. HeapFree(GetProcessHeap(), 0, pOutDevMode);
  288. }
  289. HANDLE SepGetDefaultPrinterHandle()
  290. {
  291. PRINTDLG pd;
  292. HANDLE hPrinter;
  293. DEVMODE* pDevMode;
  294. memset(&pd,0,sizeof(PRINTDLG));
  295. pd.lStructSize = sizeof(PRINTDLG);
  296. pd.Flags = PD_RETURNDEFAULT;
  297. if (!PrintDlg(&pd)) return NULL;
  298. if (!(pDevMode = (DEVMODE*)GlobalLock(pd.hDevMode))) return NULL;
  299. if (!OpenPrinter(pDevMode->dmDeviceName, &hPrinter, NULL)) hPrinter = NULL;
  300. GlobalUnlock(pd.hDevMode);
  301. return hPrinter;
  302. }
  303. void SepInitializePrinterInfo()
  304. {
  305. DWORD cbSize;
  306. pSepPrinter = NULL;
  307. if (!hSepPrinter) return;
  308. if (!GetPrinter(hSepPrinter, 2, NULL, 0, &cbSize)) // get required size
  309. if (pSepPrinter = (PRINTER_INFO_2*)HeapAlloc(GetProcessHeap(), 0, cbSize))
  310. GetPrinter(hSepPrinter, 2, (LPBYTE)pSepPrinter, cbSize, &cbSize);
  311. }
  312. void SepCustomLayout(HWND hDlg)
  313. {
  314. HKEY hKey;
  315. DWORD dwType, cbSize = MAX_PATH;
  316. TCHAR szPath[MAX_PATH+1];
  317. LONG lQuery;
  318. STARTUPINFO start;
  319. PROCESS_INFORMATION process;
  320. PTCHAR pImage;
  321. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Classes\\SepEdt\\Shell\\Open\\Command",
  322. 0, KEY_READ, &hKey) != ERROR_SUCCESS) return;
  323. lQuery = RegQueryValueEx(hKey, NULL, NULL, &dwType, (LPBYTE)szPath, &cbSize);
  324. RegCloseKey(hKey);
  325. if (lQuery != ERROR_SUCCESS) return;
  326. while (wcsrchr(szPath, L' ')) *wcsrchr(szPath, L' ') = 0;
  327. while (wcsrchr(szPath, L'\"')) *wcsrchr(szPath, L'\"') = 0;
  328. for (pImage = szPath; !*pImage;)
  329. pImage++;
  330. memset(&start, 0, sizeof(STARTUPINFO));
  331. start.cb = sizeof(STARTUPINFO);
  332. CreateProcess(pImage/*L"d:\\author\\windebug\\sepedt.exe"*/, NULL, NULL, NULL, FALSE, 0,
  333. NULL, NULL, &start, &process);
  334. }
  335. void SepSetDefaultSettings(HWND hDlg)
  336. {
  337. TCHAR szDir[MAX_PATH+1];
  338. if (!GetSystemDirectory(szDir, MAX_PATH)) szDir[0] = 0;
  339. if (szDir[wcslen(szDir)-1] != L'\\') wcscat(szDir, L"\\");
  340. CheckDlgButton(hDlg, IDC_EXTRACHECK, TRUE);
  341. SetDlgItemText(hDlg, IDC_EXTRAFILEEDIT, wcscat(szDir, L"DEFAULT.SEP"));
  342. CheckDlgButton(hDlg, IDC_BEGINRADIO, TRUE);
  343. CheckDlgButton(hDlg, IDC_ENDRADIO, FALSE);
  344. CheckDlgButton(hDlg, IDC_ADJRADIO, TRUE);
  345. CheckDlgButton(hDlg, IDC_DIFFRADIO, FALSE);
  346. SepEnableExtraControls(hDlg);
  347. CheckDlgButton(hDlg, IDC_ATTACHCHECK, FALSE);
  348. SetDlgItemText(hDlg, IDC_ATTACHFILEEDIT, L"");
  349. CheckDlgButton(hDlg, IDC_FIRSTRADIO, FALSE);
  350. CheckDlgButton(hDlg, IDC_EVERYRADIO, TRUE);
  351. CheckDlgButton(hDlg, IDC_LASTRADIO, FALSE);
  352. SepEnableAttachControls(hDlg);
  353. SepSetDefaultDevMode();
  354. }
  355. BOOL SepValidateSettings(HWND hDlg)
  356. {
  357. TCHAR szFileName[MAX_PATH+1];
  358. if (IsDlgButtonChecked(hDlg, IDC_EXTRACHECK))
  359. {
  360. if (!GetDlgItemText(hDlg, IDC_EXTRAFILEEDIT, szFileName, MAX_PATH)) return FALSE;
  361. if (!SepIsValidSepFile(szFileName)) return FALSE;
  362. }
  363. if (IsDlgButtonChecked(hDlg, IDC_ATTACHCHECK))
  364. {
  365. if (!GetDlgItemText(hDlg, IDC_ATTACHFILEEDIT, szFileName, MAX_PATH)) return FALSE;
  366. if (!SepIsValidSepFile(szFileName)) return FALSE;
  367. }
  368. return TRUE;
  369. }
  370. BOOL SepSaveSettings(HWND hDlg)
  371. {
  372. TCHAR szRegParent[MAX_PATH+1];
  373. TCHAR szAttachFile[MAX_PATH+1], szExtraFile[MAX_PATH+1];
  374. HKEY hKey, hParentKey;
  375. DWORD dwOptions = 0;
  376. BOOL bResult;
  377. GetDlgItemText(hDlg, IDC_EXTRAFILEEDIT, szExtraFile, MAX_PATH);
  378. GetDlgItemText(hDlg, IDC_ATTACHFILEEDIT, szAttachFile, MAX_PATH);
  379. if (IsDlgButtonChecked(hDlg, IDC_EXTRACHECK)) dwOptions |= 0x20;
  380. if (IsDlgButtonChecked(hDlg, IDC_BEGINRADIO)) dwOptions |= 0x10;
  381. if (IsDlgButtonChecked(hDlg, IDC_ADJRADIO)) dwOptions |= 0x08;
  382. if (IsDlgButtonChecked(hDlg, IDC_ATTACHCHECK)) dwOptions |= 0x04;
  383. if (IsDlgButtonChecked(hDlg, IDC_FIRSTRADIO)) dwOptions |= 0x02;
  384. if (IsDlgButtonChecked(hDlg, IDC_EVERYRADIO)) dwOptions |= 0x01;
  385. if (!pSepPrinter) return FALSE;
  386. wcscat(wcscpy(szRegParent, szSepRegistryRoot), pSepPrinter->pPrinterName);
  387. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegParent, 0, KEY_WRITE, &hParentKey)
  388. != ERROR_SUCCESS) return FALSE;
  389. bResult = RegCreateKeyEx(hParentKey, szSepKey, 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL);
  390. RegCloseKey(hParentKey);
  391. if (bResult != ERROR_SUCCESS) return FALSE;
  392. bResult = (RegSetValueEx(hKey, L"Options", 0, REG_DWORD, (LPBYTE)&dwOptions,
  393. sizeof(DWORD)) == ERROR_SUCCESS) &&
  394. (RegSetValueEx(hKey, L"Extra File", 0, REG_SZ, (LPBYTE)szExtraFile,
  395. (wcslen(szExtraFile)+1)*sizeof(TCHAR)) == ERROR_SUCCESS) &&
  396. (RegSetValueEx(hKey, L"Attach File", 0, REG_SZ, (LPBYTE)szAttachFile,
  397. (wcslen(szAttachFile)+1)*sizeof(TCHAR)) == ERROR_SUCCESS) &&
  398. (RegSetValueEx(hKey, L"Extra Dev Mode", 0, REG_BINARY, (LPBYTE)pSepDevMode,
  399. pSepDevMode ? HeapSize(GetProcessHeap(), 0, pSepDevMode) : 0) == ERROR_SUCCESS);
  400. RegCloseKey(hKey);
  401. return bResult;
  402. }
  403. BOOL SepLoadSettings(HWND hDlg)
  404. {
  405. TCHAR szRegPath[MAX_PATH+1];
  406. TCHAR szAttachFile[MAX_PATH+1], szExtraFile[MAX_PATH+1];
  407. HKEY hKey;
  408. DWORD dwOptions = 0;
  409. BOOL bResult;
  410. DWORD cbSize, dwType;
  411. if (!pSepPrinter) return FALSE;
  412. wcscat(wcscpy(szRegPath, szSepRegistryRoot), pSepPrinter->pPrinterName);
  413. wcscat(wcscat(szRegPath, L"\\"), szSepKey);
  414. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegPath, 0, KEY_READ, &hKey)
  415. != ERROR_SUCCESS) return FALSE;
  416. cbSize = sizeof(DWORD);
  417. bResult = RegQueryValueEx(hKey, L"Options", 0, &dwType, (LPBYTE)&dwOptions,
  418. &cbSize) == ERROR_SUCCESS && dwType == REG_DWORD;
  419. cbSize = MAX_PATH*sizeof(TCHAR);
  420. bResult = bResult && RegQueryValueEx(hKey, L"Extra File", 0, &dwType, (LPBYTE)szExtraFile,
  421. &cbSize) == ERROR_SUCCESS && (dwType & REG_SZ);
  422. cbSize = MAX_PATH*sizeof(TCHAR);
  423. bResult = bResult && RegQueryValueEx(hKey, L"Attach File", 0, &dwType, (LPBYTE)szAttachFile,
  424. &cbSize) == ERROR_SUCCESS && (dwType & REG_SZ);
  425. cbSize=0;
  426. bResult = bResult && RegQueryValueEx(hKey, L"Extra Dev Mode", 0, NULL, NULL,
  427. &cbSize) == ERROR_SUCCESS;
  428. if (bResult && cbSize)
  429. {
  430. bResult = bResult && (pSepDevMode = (DEVMODE*)HeapAlloc(GetProcessHeap(), 0, cbSize));
  431. if (bResult)
  432. bResult = bResult && RegQueryValueEx(hKey, L"Extra Dev Mode", 0, &dwType, (LPBYTE)pSepDevMode,
  433. &cbSize) == ERROR_SUCCESS && (dwType & REG_BINARY);
  434. }
  435. RegCloseKey(hKey);
  436. if (!bResult)
  437. {if (pSepDevMode) HeapFree(GetProcessHeap(), 0, pSepDevMode); return FALSE;}
  438. // successfully got settings from registry
  439. if (!cbSize) SepSetDefaultDevMode(); // empty devmode
  440. SetDlgItemText(hDlg, IDC_EXTRAFILEEDIT, szExtraFile);
  441. SetDlgItemText(hDlg, IDC_ATTACHFILEEDIT, szAttachFile);
  442. CheckDlgButton(hDlg, IDC_EXTRACHECK, dwOptions & 0x20);
  443. CheckDlgButton(hDlg, IDC_BEGINRADIO, dwOptions & 0x10);
  444. CheckDlgButton(hDlg, IDC_ENDRADIO, !(dwOptions & 0x10));
  445. CheckDlgButton(hDlg, IDC_ADJRADIO, dwOptions & 0x08);
  446. CheckDlgButton(hDlg, IDC_DIFFRADIO, !(dwOptions & 0x08));
  447. CheckDlgButton(hDlg, IDC_ATTACHCHECK, dwOptions & 0x04);
  448. CheckDlgButton(hDlg, IDC_FIRSTRADIO, dwOptions & 0x02);
  449. CheckDlgButton(hDlg, IDC_EVERYRADIO, dwOptions & 0x01);
  450. CheckDlgButton(hDlg, IDC_LASTRADIO, !(dwOptions & 0x03));
  451. return TRUE;
  452. }
  453. void SepSetDefaultDevMode()
  454. {
  455. LONG lSize;
  456. pSepDevMode = NULL;
  457. if (!pSepPrinter) return;
  458. lSize = DocumentProperties(NULL, hSepPrinter, pSepPrinter->pPrinterName, NULL, NULL, 0);
  459. if (!(pSepDevMode = (PDEVMODE)HeapAlloc(GetProcessHeap(), 0, lSize))) return;
  460. // get default devmode
  461. if (DocumentProperties(NULL, hSepPrinter, pSepPrinter->pPrinterName, pSepDevMode,
  462. NULL, DM_OUT_BUFFER) != IDOK)
  463. {
  464. HeapFree(GetProcessHeap(), 0, pSepDevMode);
  465. pSepDevMode = NULL;
  466. }
  467. }
  468. void SepTestExtraPage(HWND hDlg)
  469. {
  470. TCHAR szFileName[MAX_PATH+1];
  471. GetDlgItemText(hDlg, IDC_EXTRAFILEEDIT, szFileName, MAX_PATH);
  472. SepPrintSepFile(szFileName, pSepDevMode);
  473. }
  474. void SepTestAttachPage(HWND hDlg)
  475. {
  476. TCHAR szFileName[MAX_PATH+1];
  477. GetDlgItemText(hDlg, IDC_ATTACHFILEEDIT, szFileName, MAX_PATH);
  478. SepPrintSepFile(szFileName, NULL);
  479. }
  480. void SepPrintSepFile(PTCHAR szFileName, DEVMODE* pDevMode)
  481. {
  482. HDC hDC;
  483. DOCINFO DocInfo;
  484. RECT rectPaper;
  485. if (!pSepPrinter) return;
  486. // start separator page document
  487. hDC = CreateDC(pSepPrinter->pDriverName, pSepPrinter->pPrinterName, L"", pDevMode);
  488. if (!hDC) return;
  489. DocInfo.lpszDocName = L"Separator Page Test";
  490. DocInfo.lpszOutput = 0;
  491. DocInfo.lpszDatatype = NULL;
  492. DocInfo.fwType = 0;
  493. DocInfo.cbSize = sizeof(DOCINFO);
  494. StartDoc(hDC, (LPDOCINFO)&DocInfo);
  495. StartPage(hDC);
  496. rectPaper.left = rectPaper.top = 0;
  497. rectPaper.right = GetDeviceCaps(hDC, HORZRES);
  498. rectPaper.bottom = GetDeviceCaps(hDC, VERTRES);
  499. // draw sep page on this DC
  500. SepDrawSepPage(hDC, &rectPaper, szFileName);
  501. // end page
  502. EndPage(hDC);
  503. EndDoc(hDC);
  504. DeleteDC(hDC);
  505. }
  506. void SepPreviewExtraPage(HWND hDlg)
  507. {
  508. TCHAR szFileName[MAX_PATH+1];
  509. GetDlgItemText(hDlg, IDC_EXTRAFILEEDIT, szFileName, MAX_PATH);
  510. SepPreviewSepFile(hDlg, szFileName, pSepDevMode);
  511. }
  512. void SepPreviewAttachPage(HWND hDlg)
  513. {
  514. TCHAR szFileName[MAX_PATH+1];
  515. GetDlgItemText(hDlg, IDC_ATTACHFILEEDIT, szFileName, MAX_PATH);
  516. SepPreviewSepFile(hDlg, szFileName, NULL);
  517. }
  518. void SepPreviewSepFile(HWND hDlg, PTCHAR szFileName, DEVMODE* pDevMode)
  519. {
  520. if (!pSepPrinter) return;
  521. // Start separator page document
  522. hSepPrtDC = CreateDC(pSepPrinter->pDriverName, pSepPrinter->pPrinterName, L"", pDevMode);
  523. if (!hSepPrtDC) return;
  524. wcscpy(szSepViewFile, szFileName);
  525. DialogBox((HINSTANCE)GetWindowLong(hDlg, GWL_HINSTANCE), L"SepPreViewDlg", hDlg, SepPreviewDlgProc);
  526. DeleteDC(hSepPrtDC);
  527. }
  528. LRESULT APIENTRY SepPreviewDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  529. {
  530. switch (uMsg)
  531. {
  532. case WM_INITDIALOG:
  533. SepPreviewInit(hDlg);
  534. return TRUE;
  535. case WM_SYSCOMMAND:
  536. if (wParam == SC_CLOSE)
  537. {
  538. EndDialog (hDlg, TRUE);
  539. return TRUE;
  540. }
  541. break;
  542. case WM_COMMAND:
  543. switch (LOWORD(wParam))
  544. {
  545. case IDOK:
  546. case IDCANCEL:
  547. EndDialog(hDlg, IDOK);
  548. default:
  549. return FALSE;
  550. }
  551. return TRUE;
  552. case WM_PAINT:
  553. SepPreviewPaint(hDlg);
  554. return FALSE;
  555. }
  556. return FALSE;
  557. }
  558. void SepPreviewInit(HWND hDlg)
  559. {
  560. int hPrtSize, vPrtSize;
  561. int hScrSize, vScrSize;
  562. int hViewSize, vViewSize;
  563. int hScrPixel, vScrPixel;
  564. int hViewPixel, vViewPixel;
  565. int hDlgPixel, vDlgPixel;
  566. RECT rectButton;
  567. RECT rectClient;
  568. HDC hScrDC;
  569. hScrDC = GetDC(hDlg);
  570. hPrtSize = GetDeviceCaps(hSepPrtDC, HORZSIZE);
  571. vPrtSize = GetDeviceCaps(hSepPrtDC, VERTSIZE);
  572. hScrSize = GetDeviceCaps(hScrDC, HORZSIZE);
  573. vScrSize = GetDeviceCaps(hScrDC, VERTSIZE);
  574. hScrPixel = GetDeviceCaps(hScrDC, HORZRES);
  575. vScrPixel = GetDeviceCaps(hScrDC, VERTRES);
  576. vViewPixel = vScrPixel - 100;
  577. vViewSize = MulDiv(vViewPixel, vScrSize, vScrPixel);
  578. hViewSize = MulDiv(vViewSize, hPrtSize, vPrtSize);
  579. hViewPixel = MulDiv(hViewSize, hScrPixel, hScrSize);
  580. if (hViewPixel > hScrPixel - 20)
  581. {
  582. hViewPixel = hScrPixel - 20;
  583. hViewSize = MulDiv(hViewPixel, hScrSize, hScrPixel);
  584. vViewSize = MulDiv(hViewSize, vPrtSize, hPrtSize);
  585. vViewPixel = MulDiv(vViewSize, vScrPixel, vScrSize);
  586. }
  587. hDlgPixel = min(hViewPixel + 50, hScrPixel);
  588. vDlgPixel = min(vViewPixel + 80, vScrPixel);
  589. MoveWindow(hDlg, (hScrPixel - hDlgPixel)/2, (vScrPixel - vDlgPixel)/2,
  590. hDlgPixel, vDlgPixel, FALSE);
  591. GetWindowRect(GetDlgItem(hDlg, IDOK), &rectButton);
  592. GetClientRect(hDlg, &rectClient);
  593. MoveWindow(GetDlgItem(hDlg, IDOK), (rectClient.right - (rectButton.right - rectButton.left))/2,
  594. rectClient.bottom - (rectButton.bottom - rectButton.top) - 5,
  595. rectButton.right - rectButton.left, rectButton.bottom - rectButton.top, TRUE);
  596. rcSepView.left = (rectClient.right - hViewPixel)/2;
  597. rcSepView.top = (rectClient.bottom - vViewPixel -(rectButton.bottom - rectButton.top) - 5)/2;
  598. rcSepView.right = rcSepView.left + hViewPixel;
  599. rcSepView.bottom = rcSepView.top + vViewPixel;
  600. ReleaseDC(hDlg, hScrDC);
  601. }
  602. void SepPreviewPaint(HWND hDlg)
  603. {
  604. HDC hDC;
  605. hDC = GetDC(hDlg);
  606. Rectangle(hDC, rcSepView.left, rcSepView.top, rcSepView.right, rcSepView.bottom);
  607. SepDrawSepPage(hDC, &rcSepView, szSepViewFile);
  608. ReleaseDC(hDlg, hDC);
  609. }
  610. void SepDrawSepPage(HDC hDC, RECT* pBound, PTCHAR szFileName)
  611. {
  612. HENHMETAFILE hEmf;
  613. HANDLE hFile;
  614. HANDLE hFileMapping;
  615. DWORD dwFileSizeLo;
  616. PCHAR pFileStart;
  617. DWORD cbRead;
  618. DWORD dwEmfLen, dwTextRecords;
  619. SIZE sizePage;
  620. TEXTBOX* pTextRec;
  621. RECT rect, trect;
  622. HFONT hFont, hOldFont;
  623. TCHAR text[2000];
  624. // open separator file
  625. hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ,
  626. NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  627. if (hFile==INVALID_HANDLE_VALUE) return;
  628. // get EMF and text size
  629. ReadFile(hFile, &dwEmfLen, sizeof(DWORD), &cbRead, NULL);
  630. if (cbRead != sizeof(DWORD)) { CloseHandle(hFile); return;}
  631. ReadFile(hFile, &dwTextRecords, sizeof(DWORD), &cbRead, NULL);
  632. if (cbRead != sizeof(DWORD)) { CloseHandle(hFile); return;}
  633. ReadFile(hFile, &sizePage, sizeof(SIZE), &cbRead, NULL);
  634. if (cbRead != sizeof(SIZE)) { CloseHandle(hFile); return;}
  635. // create mapping
  636. dwFileSizeLo = GetFileSize(hFile, NULL);
  637. hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  638. if (!hFileMapping || dwFileSizeLo==-1) { CloseHandle(hFile); return;}
  639. // map view of file
  640. pFileStart = (char *)MapViewOfFile(hFileMapping, FILE_MAP_READ,
  641. 0, 0, 2 * sizeof(DWORD) + sizeof(SIZE) + dwEmfLen + dwTextRecords * sizeof(TEXTBOX));
  642. if (!pFileStart) { CloseHandle(hFileMapping); CloseHandle(hFile); return;}
  643. // create EMF
  644. hEmf = SetEnhMetaFileBits(dwEmfLen, pFileStart + 2 * sizeof(DWORD) + sizeof(SIZE));
  645. if (hEmf)
  646. {
  647. // play EMF
  648. PlayEnhMetaFile(hDC, hEmf, pBound);
  649. // delete EMF
  650. DeleteEnhMetaFile(hEmf);
  651. }
  652. // Set Map Mode
  653. SetMapMode(hDC, MM_ANISOTROPIC);
  654. SetViewportExtEx(hDC, pBound->right - pBound->left, pBound->bottom - pBound->top, NULL);
  655. SetWindowExtEx(hDC, sizePage.cx, -sizePage.cy, NULL);
  656. SetViewportOrgEx(hDC, pBound->left, pBound->top, NULL);
  657. SetWindowOrgEx(hDC, -sizePage.cx/2, sizePage.cy/2, NULL);
  658. // Process Text Objects
  659. pTextRec = (TEXTBOX*) (pFileStart + 2 * sizeof(DWORD) + sizeof(SIZE) + dwEmfLen);
  660. while (dwTextRecords--)
  661. {
  662. // get draw rectangle
  663. rect = pTextRec -> position;
  664. // normalize rectangle
  665. trect.left = min(rect.left, rect.right);
  666. trect.top = max(rect.top, rect.bottom);
  667. trect.right = max(rect.left, rect.right);
  668. trect.bottom = min(rect.top, rect.bottom);
  669. // leave margins to border, as in editor
  670. trect.left = min(trect.left + 2, trect.right);
  671. trect.top = max(trect.top - 2, trect.bottom);
  672. trect.right = max(trect.left, trect.right - 2);
  673. trect.bottom = min(trect.top, trect.bottom + 2);
  674. // create font
  675. hFont = CreateFontIndirectA(&(pTextRec->lf));
  676. if (hFont)
  677. hOldFont = SelectObject(hDC, hFont);
  678. // set text color
  679. SetTextColor(hDC, pTextRec->color);
  680. // convert text to UNICODE if defined
  681. wsprintf(text, L"%hs", pTextRec->text);
  682. // draw text
  683. DrawText(hDC, text,-1,&trect,DT_WORDBREAK|AlignTable[pTextRec->align].drawstyle);
  684. // restore and delete font
  685. if (hFont)
  686. {
  687. SelectObject(hDC, hOldFont);
  688. DeleteObject(hFont);
  689. }
  690. pTextRec++;
  691. }
  692. // close file
  693. UnmapViewOfFile(pFileStart);
  694. CloseHandle(hFileMapping);
  695. CloseHandle(hFile);
  696. }