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.

389 lines
13 KiB

  1. /*
  2. +-------------------------------------------------------------------------+
  3. | MDI Text File Viewer - Printing Routines |
  4. +-------------------------------------------------------------------------+
  5. | (c) Copyright 1994 |
  6. | Microsoft Corp. |
  7. | All rights reserved |
  8. | |
  9. | Program : [FVPrint.c] |
  10. | Programmer : Arthur Hanson |
  11. | Original Program Date : [Feb 11, 1994] |
  12. | Last Update : [Feb 11, 1994] |
  13. | |
  14. | Version: 0.10 |
  15. | |
  16. | Description: |
  17. | |
  18. | History: |
  19. | arth Jul 27, 1993 0.10 Original Version. |
  20. | |
  21. +-------------------------------------------------------------------------+
  22. */
  23. #include "LogView.h"
  24. BOOL fAbort;
  25. HWND hwndPDlg;
  26. CHAR szDevice[160];
  27. PSTR szDriver;
  28. PSTR szPort;
  29. PSTR szTitle;
  30. INT iPrinter = 0; // level of available printer support.
  31. // 0 - no printer available
  32. // 1 - printer available
  33. // 2 - driver supports 3.0 device initialization
  34. HANDLE hInitData=NULL;
  35. CHAR szExtDeviceMode[] = "EXTDEVICEMODE";
  36. /*+-------------------------------------------------------------------------+
  37. | GetPrinterDC() |
  38. | |
  39. | Creates a printer display context for the default device. As a side |
  40. | effect, it sets the szDevice and szPort variables. It also sets |
  41. | iPrinter to the supported level of printing. |
  42. | |
  43. +-------------------------------------------------------------------------+*/
  44. HDC APIENTRY GetPrinterDC(BOOL bInformation) {
  45. HDC hdc;
  46. LPDEVMODE lpdevmode = NULL;
  47. iPrinter = 0;
  48. // Get the printer information from win.ini into a buffer and null terminate it.
  49. GetProfileString ( TEXT("windows"), TEXT("device"), TEXT(""), szDevice, sizeof(szDevice));
  50. for (szDriver = szDevice; *szDriver && *szDriver != TEXT(','); szDriver++)
  51. ;
  52. if (*szDriver)
  53. *szDriver++ = 0;
  54. // From the current position in the buffer, null teminate the list of ports
  55. for (szPort = szDriver; *szPort && *szPort != TEXT(','); szPort++)
  56. ;
  57. if (*szPort)
  58. *szPort++ = 0;
  59. // if the device, driver and port buffers all contain meaningful data, proceed.
  60. if (!*szDevice || !*szDriver || !*szPort){
  61. *szDevice = 0;
  62. return NULL;
  63. }
  64. // Create the printer display context
  65. if (hInitData){
  66. // Get a pointer to the initialization data
  67. lpdevmode = (LPDEVMODE) LocalLock (hInitData);
  68. if (lstrcmp (szDevice, (LPSTR)lpdevmode)) {
  69. // User has changed the device... cancel this setup, as it is invalid
  70. // (although if we worked harder we could retain some of it).
  71. lpdevmode = NULL;
  72. LocalUnlock (hInitData);
  73. LocalFree (hInitData);
  74. hInitData = NULL;
  75. }
  76. }
  77. if (bInformation)
  78. hdc = CreateIC (szDriver, szDevice, szPort, lpdevmode);
  79. else
  80. hdc = CreateDC (szDriver, szDevice, szPort, lpdevmode);
  81. // Unlock initialization data
  82. if (hInitData)
  83. LocalUnlock (hInitData);
  84. if (!hdc)
  85. return NULL;
  86. iPrinter = 1;
  87. // Find out if ExtDeviceMode() is supported and set flag appropriately
  88. if (GetProcAddress (LoadLibrary(szDriver), szExtDeviceMode))
  89. iPrinter = 2;
  90. return hdc;
  91. } // GetPrinterDC
  92. /*+-------------------------------------------------------------------------+
  93. | AbortProc() |
  94. | |
  95. | Checks for user abort. |
  96. | |
  97. +-------------------------------------------------------------------------+*/
  98. INT APIENTRY AbortProc ( HDC hdc, WORD reserved) {
  99. MSG msg;
  100. // Allow other apps to run, or get abort messages
  101. while (!fAbort && PeekMessage (&msg, NULL, 0, 0, TRUE))
  102. if (!hwndPDlg || !IsDialogMessage (hwndPDlg, &msg)) {
  103. TranslateMessage (&msg);
  104. DispatchMessage (&msg);
  105. }
  106. return !fAbort;
  107. UNREFERENCED_PARAMETER(hdc);
  108. UNREFERENCED_PARAMETER(reserved);
  109. } // AbortProc
  110. /*+-------------------------------------------------------------------------+
  111. | PrintDlgProc() |
  112. | |
  113. | Print/Cancel dialog box. |
  114. | |
  115. +-------------------------------------------------------------------------+*/
  116. BOOL APIENTRY PrintDlgProc(HWND hwnd, UINT msg, WORD wParam, LONG lParam) {
  117. switch (msg) {
  118. case WM_INITDIALOG:
  119. // Set up information in dialog box
  120. SetDlgItemText (hwnd, IDD_PRINTTITLE, (LPSTR)szTitle);
  121. break;
  122. case WM_COMMAND:
  123. // abort printing if the only button gets hit
  124. fAbort = TRUE;
  125. break;
  126. default:
  127. return FALSE;
  128. }
  129. return TRUE;
  130. UNREFERENCED_PARAMETER(wParam);
  131. UNREFERENCED_PARAMETER(lParam);
  132. } // PrintDlgProc
  133. /*+-------------------------------------------------------------------------+
  134. | PrintFile() |
  135. | |
  136. | Prints the contents of the edit control. |
  137. | |
  138. +-------------------------------------------------------------------------+*/
  139. VOID APIENTRY PrintFile(HWND hwnd) {
  140. HDC hdc;
  141. INT yExtPage;
  142. CHAR sz[32];
  143. int cch;
  144. WORD ich;
  145. PSTR pch;
  146. WORD iLine;
  147. WORD nLinesEc;
  148. WORD i;
  149. HANDLE hT;
  150. HWND hwndPDlg;
  151. DWORD dy;
  152. INT yExtSoFar;
  153. WORD fError = TRUE;
  154. HWND hwndEdit;
  155. HFONT hFont, hOldFont;
  156. hwndEdit = (HWND)GetWindowLong(hwnd,GWL_HWNDEDIT);
  157. // Create the job title by loading the title string from STRINGTABLE
  158. cch = LoadString (hInst, IDS_PRINTJOB, sz, sizeof(sz));
  159. szTitle = sz + cch;
  160. cch += GetWindowText (hwnd, sz + cch, 32 - cch);
  161. sz[31] = 0;
  162. // Initialize the printer
  163. hdc = GetPrinterDC(FALSE);
  164. if (!hdc)
  165. goto getout5;
  166. SetMapMode(hdc, MM_TEXT);
  167. SelectObject(hdc, GetStockObject(ANSI_FIXED_FONT));
  168. // Disable the main application window and create the Cancel dialog
  169. EnableWindow (hwndFrame, FALSE);
  170. hwndPDlg = CreateDialog (hInst, IDD_PRINT, hwnd, (DLGPROC) PrintDlgProc);
  171. if (!hwndPDlg)
  172. goto getout3;
  173. ShowWindow (hwndPDlg, SW_SHOW);
  174. UpdateWindow (hwndPDlg);
  175. // Allow the app. to inform GDI of the escape function to call
  176. if (Escape(hdc, SETABORTPROC, 0, (LPSTR)AbortProc, NULL) < 0)
  177. goto getout1;
  178. // Initialize the document
  179. if (Escape(hdc, STARTDOC, cch, (LPSTR)sz, NULL) < 0)
  180. goto getout1;
  181. // Get the height of one line and the height of a page
  182. {
  183. SIZE tmp;
  184. GetTextExtentPoint(hdc, "CC", 2, &tmp );
  185. dy = tmp.cy;
  186. }
  187. yExtPage = GetDeviceCaps(hdc, VERTRES);
  188. // Get the lines in document and and a handle to the text buffer
  189. iLine = 0;
  190. yExtSoFar = 0;
  191. nLinesEc = (WORD)SendMessage (hwndEdit, EM_GETLINECOUNT, 0, 0L);
  192. hT = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);
  193. // While more lines print out the text
  194. while (iLine < nLinesEc) {
  195. if (yExtSoFar + (int) dy > yExtPage) {
  196. // Reached the end of a page. Tell the device driver to eject a page
  197. if (Escape(hdc, NEWFRAME, 0, NULL, NULL) < 0 || fAbort)
  198. goto getout2;
  199. yExtSoFar = 0;
  200. }
  201. // Get the length and position of the line in the buffer and lock from that
  202. // offset into the buffer.
  203. ich = (WORD)SendMessage (hwndEdit, EM_LINEINDEX, iLine, 0L);
  204. cch = (WORD)SendMessage (hwndEdit, EM_LINELENGTH, ich, 0L);
  205. pch = (PSTR)LocalLock(hT) + ich;
  206. // Print the line and unlock the text handle
  207. TextOut (hdc, 0, yExtSoFar, (LPSTR)pch, cch);
  208. LocalUnlock (hT);
  209. // Test and see if the Abort flag has been set. If yes, exit.
  210. if (fAbort)
  211. goto getout2;
  212. // Move down the page
  213. yExtSoFar += dy;
  214. iLine++;
  215. }
  216. // Eject the last page.
  217. if (Escape(hdc, NEWFRAME, 0, NULL, NULL) < 0)
  218. goto getout2;
  219. // Complete the document.
  220. if (Escape(hdc, ENDDOC, 0, NULL, NULL) < 0) {
  221. getout2:
  222. // Ran into a problem before NEWFRAME? Abort the document
  223. Escape( hdc, ABORTDOC, 0, NULL, NULL);
  224. } else
  225. fError=FALSE;
  226. getout3:
  227. // Close the cancel dialog and re-enable main app. window
  228. EnableWindow (hwndFrame, TRUE);
  229. DestroyWindow (hwndPDlg);
  230. getout1:
  231. DeleteDC(hdc);
  232. getout5:
  233. #ifdef WIN16
  234. // Get rid of dialog procedure instances
  235. FreeProcInstance (lpfnPDlg);
  236. #endif
  237. #ifdef WIN16
  238. getout4:
  239. FreeProcInstance (lpfnAbort);
  240. getout:
  241. #endif
  242. // Error? make sure the user knows...
  243. if (fError)
  244. MPError (hwnd, MB_OK | MB_ICONEXCLAMATION, IDS_PRINTERROR, (LPSTR)szTitle);
  245. return;
  246. UNREFERENCED_PARAMETER(i);
  247. } // PrintFile
  248. /*+-------------------------------------------------------------------------+
  249. | GetInitializationData() |
  250. | |
  251. | Gets DC initialization data from a printer driver supporting |
  252. | ExtDeviceMode(). Called in response to the File/Printer setup menu |
  253. | selection. |
  254. | |
  255. | This function allows the user to change the printer settings FOR |
  256. | LOGVIEW ONLY. This allows LogView to print in a variety of settings |
  257. | without messing up any othe applications. |
  258. | |
  259. +-------------------------------------------------------------------------+*/
  260. BOOL APIENTRY GetInitializationData( HWND hwnd ) {
  261. LPSTR lpOld;
  262. LPSTR lpNew;
  263. FARPROC lpfn;
  264. HANDLE hT,hDrv;
  265. CHAR sz[32];
  266. int cb;
  267. INT flag;
  268. // Pop up dialog for user and retain data in app buffer
  269. flag = DM_PROMPT | DM_COPY;
  270. // Load the device driver and find the ExtDeviceMode() function
  271. wsprintf (sz, "%s.drv", (LPSTR)szDriver);
  272. if (!(hDrv = LoadLibrary (sz)))
  273. return FALSE;
  274. if (!(lpfn = (FARPROC) GetProcAddress (hDrv, szExtDeviceMode)))
  275. return FALSE;
  276. if (hInitData) {
  277. // We have some old data... we want to modify the previously specified
  278. // setup rather than starting with the default setup.
  279. lpOld = (LPSTR)LocalLock(hInitData);
  280. flag |= DM_MODIFY;
  281. }
  282. else
  283. lpOld = NULL;
  284. // Get the number of bytes needed for the init data
  285. cb = (int) (*lpfn) (hwnd, hDrv, (LPDEVMODE)NULL, (LPSTR)szDevice, (LPSTR)szPort, (LPDEVMODE)NULL, (LPSTR)NULL, 0);
  286. // Grab some memory for the new data and lock it.
  287. hT = LocalAlloc (LHND,cb);
  288. if(!hT) {
  289. MessageBox(hwnd, TEXT("<GetInitializationData> Not enough memory."), NULL, MB_OK | MB_ICONHAND);
  290. LocalUnlock(hInitData);
  291. LocalFree(hInitData);
  292. FreeLibrary(hDrv);
  293. return(FALSE);
  294. }
  295. lpNew = (LPSTR)LocalLock (hT);
  296. // Post the device mode dialog. 0 flag iff user hits OK button
  297. if ((*lpfn) (hwnd, hDrv, (LPDEVMODE)lpNew, (LPSTR)szDevice, (LPSTR)szPort, (LPDEVMODE)lpOld, (LPSTR)NULL, flag) == IDOK)
  298. flag = 0;
  299. // Unlock the input structures
  300. LocalUnlock (hT);
  301. if (hInitData)
  302. LocalUnlock (hInitData);
  303. // If the user hit OK and everything worked, free the original init. data and
  304. // retain the new one. Otherwise, toss the new buffer.
  305. if (flag)
  306. LocalFree (hT);
  307. else {
  308. if (hInitData)
  309. LocalFree (hInitData);
  310. hInitData = hT;
  311. }
  312. FreeLibrary(hDrv);
  313. return (!flag);
  314. } // GetInitializationData