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.

426 lines
12 KiB

  1. /*************************************************
  2. * lcprint.c *
  3. * *
  4. * Copyright (C) 1995-1999 Microsoft Inc. *
  5. * *
  6. *************************************************/
  7. #include <windows.h> // required for all Windows applications
  8. #include <windowsx.h>
  9. #include <stdlib.h>
  10. #include "rc.h"
  11. #include "lctool.h"
  12. #ifndef UNICODE
  13. #define lWordBuff iWordBuff
  14. #endif
  15. #define UM_CANCELPRINT WM_USER+2
  16. #define TOP_SPACE 250
  17. #define WORD_POS 7
  18. #define PHRASE_POS 10
  19. PRINTDLG pdPrint;
  20. HWND hCancelDlg = 0;
  21. int nFromLine = 1;
  22. int nToLine = 1;
  23. int TransNum(TCHAR *);
  24. /* get default printer configuration and save in hWnd extra bytes for use later */
  25. BOOL WINAPI GetPrinterConfig (
  26. HWND hWnd)
  27. {
  28. pdPrint.lStructSize = sizeof (PRINTDLG);
  29. pdPrint.Flags = PD_RETURNDEFAULT;
  30. pdPrint.hwndOwner = hWnd;
  31. pdPrint.hDevMode = NULL;
  32. pdPrint.hDevNames = NULL;
  33. pdPrint.hDC = NULL;
  34. PrintDlg (&pdPrint);
  35. return TRUE;
  36. }
  37. /* abort proc called by gdi during print download process */
  38. int WINAPI AbortProc (
  39. HDC hdc,
  40. int nErr)
  41. {
  42. BOOL fContinue = TRUE;
  43. MSG msg;
  44. /* process messages for cancel dialog and other apps */
  45. while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  46. {
  47. if (msg.message == UM_CANCELPRINT)
  48. {
  49. fContinue = FALSE;
  50. break;
  51. }
  52. else if (!hCancelDlg || !IsDialogMessage (hCancelDlg, &msg))
  53. {
  54. TranslateMessage (&msg);
  55. DispatchMessage (&msg);
  56. }
  57. }
  58. return fContinue;
  59. }
  60. INT_PTR CALLBACK LineDlgProc (
  61. HWND hDlg,
  62. UINT message,
  63. WPARAM wParam,
  64. LPARAM lParam)
  65. {
  66. TCHAR szLine[MAX_PATH];
  67. switch (message)
  68. {
  69. case WM_INITDIALOG:
  70. /* initialize dialog control information */
  71. SetDlgItemText (hDlg,
  72. IDD_FROM_LINE,
  73. _TEXT("1"));
  74. #ifdef UNICODE
  75. wsprintf(szLine,_TEXT("%5d"), lWordBuff);
  76. #else
  77. wsprintf(szLine,_TEXT("%5d"), iWordBuff);
  78. #endif
  79. SetDlgItemText (hDlg,
  80. IDD_TO_LINE,
  81. szLine);
  82. break;
  83. case WM_COMMAND:
  84. switch (wParam) {
  85. case IDOK:
  86. GetDlgItemText(hDlg,IDD_FROM_LINE,
  87. szLine, sizeof(szLine)/sizeof(TCHAR));
  88. nFromLine=TransNum(szLine);
  89. #ifdef UNICODE
  90. if((nFromLine < 1) || (nFromLine > (int)lWordBuff)) {
  91. #else
  92. if((nFromLine < 1) || (nFromLine > (int)iWordBuff)) {
  93. #endif
  94. MessageBeep(0);
  95. SetFocus(GetDlgItem(hDlg, IDD_FROM_LINE));
  96. return TRUE;
  97. }
  98. GetDlgItemText(hDlg,IDD_TO_LINE,
  99. szLine, ARRAYSIZE(szLine));
  100. nToLine=TransNum(szLine);
  101. if((nToLine < nFromLine) || (nToLine > (int)lWordBuff)) {
  102. MessageBeep(0);
  103. SetFocus(GetDlgItem(hDlg, IDD_TO_LINE));
  104. return TRUE;
  105. }
  106. EndDialog (hDlg, TRUE) ;
  107. return TRUE;
  108. case IDCANCEL:
  109. EndDialog (hDlg, FALSE) ;
  110. return TRUE;
  111. }
  112. break ;
  113. case WM_CLOSE:
  114. EndDialog(hDlg, FALSE);
  115. return TRUE;
  116. default:
  117. return FALSE;
  118. }
  119. return TRUE;
  120. }
  121. INT_PTR CALLBACK CancelDlgProc (
  122. HWND hWnd,
  123. UINT uMsg,
  124. WPARAM uParam,
  125. LPARAM lParam)
  126. {
  127. switch (uMsg)
  128. {
  129. case WM_INITDIALOG:
  130. {
  131. TCHAR szStr[MAX_PATH];
  132. TCHAR szShowMsg[MAX_PATH];
  133. /* initialize dialog control information */
  134. LoadString(hInst, IDS_PRINTING, szStr, sizeof(szStr)/sizeof(TCHAR));
  135. wsprintf(szShowMsg, szStr, nFromLine, nToLine);
  136. SetDlgItemText (hWnd,
  137. IDC_PRINTLINE,
  138. szShowMsg);
  139. }
  140. break;
  141. case WM_COMMAND:
  142. /* if cancel button selected, post message to cancel print job */
  143. if (LOWORD (uParam) == IDCANCEL)
  144. {
  145. PostMessage (GetParent (hWnd), UM_CANCELPRINT, 0, 0);
  146. DestroyWindow (hWnd);
  147. }
  148. break;
  149. default:
  150. return FALSE;
  151. }
  152. return TRUE;
  153. }
  154. /* put up the print common dialog, and print */
  155. int WINAPI lcPrint (
  156. HWND hWnd)
  157. {
  158. SIZE sLine;
  159. int yLineExt;
  160. int nLineChar;
  161. int xExt, yExt;
  162. int yPageExt;
  163. int xPageExt;
  164. int xPageOff, yPageOff;
  165. int nStart,nEnd;
  166. DOCINFO diPrint;
  167. TCHAR lpszJobName[MAX_PATH];
  168. TCHAR szStr[MAX_CHAR_NUM+20];
  169. int i,len;
  170. int is_OK;
  171. if(!lcSaveEditText(iDisp_Top, 0))
  172. return TRUE; // Some error, but msg had displayed
  173. /* Display Choose line number dialog */
  174. is_OK=(int)DialogBox(hInst,_TEXT("LineDialog"), hwndMain, LineDlgProc);
  175. if(!is_OK)
  176. return TRUE; // User choose Cancel
  177. /* call common print dialog to get initialized printer DC */
  178. pdPrint.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION;
  179. /* call common print dialog */
  180. if (!PrintDlg (&pdPrint))
  181. return TRUE; // User choose Cancel
  182. /* start cancel dialog box */
  183. hCancelDlg = CreateDialog (hInst,
  184. _TEXT("IDD_CANCELDLG"),
  185. hwndMain,
  186. CancelDlgProc);
  187. if (!hCancelDlg)
  188. return IDS_CANCELDLGFAILED;
  189. ShowWindow (hCancelDlg, SW_SHOW);
  190. UpdateWindow (hCancelDlg);
  191. /* set AbortProc callback */
  192. if (SetAbortProc (pdPrint.hDC, (ABORTPROC)AbortProc) < 0) {
  193. /* on error, clean up and go away */
  194. DestroyWindow (hCancelDlg);
  195. DeleteDC (pdPrint.hDC);
  196. return IDS_SETABORTPROCFAILED;
  197. }
  198. /* initialize printer for job */
  199. GetWindowText (hWnd, lpszJobName, ARRAYSIZE (lpszJobName));
  200. diPrint.cbSize = sizeof (DOCINFO);
  201. diPrint.lpszDocName = lpszJobName;
  202. diPrint.lpszOutput = NULL;
  203. diPrint.lpszDatatype = NULL;
  204. diPrint.fwType = 0;
  205. if (StartDoc(pdPrint.hDC, &diPrint) == SP_ERROR) {
  206. /* on error, clean up and go away */
  207. DestroyWindow (hCancelDlg);
  208. DeleteDC (pdPrint.hDC);
  209. return IDS_STARTDOCFAILED;
  210. }
  211. /* Set Cursor status
  212. SetCursor(hCursorWait);
  213. /* job started, so display cancel dialog */
  214. ShowWindow (hCancelDlg, SW_SHOW);
  215. UpdateWindow (hCancelDlg);
  216. /* retrieve dimensions for printing and init loop variables */
  217. GetTextExtentPoint (pdPrint.hDC,_TEXT("��"), 2, &sLine);
  218. sLine.cx += (sLine.cx % 2);
  219. sLine.cy += (sLine.cy % 2);
  220. yLineExt = sLine.cy+4;
  221. yPageExt = GetDeviceCaps (pdPrint.hDC, VERTRES);
  222. xPageExt = GetDeviceCaps (pdPrint.hDC, HORZRES);
  223. xPageOff = GetDeviceCaps (pdPrint.hDC, PHYSICALOFFSETX);
  224. yPageOff = GetDeviceCaps (pdPrint.hDC, PHYSICALOFFSETY);
  225. nLineChar= (xPageExt - xPageOff * 2)/(1+(sLine.cx>>1)) - 6;
  226. //yExt = TOP_SPACE;
  227. xExt = xPageOff;
  228. yExt = yPageOff + sLine.cy;
  229. if (StartPage(pdPrint.hDC) <= 0)
  230. {
  231. DestroyWindow (hCancelDlg);
  232. DeleteDC (pdPrint.hDC);
  233. return IDS_PRINTABORTED;
  234. }
  235. /* Print Title */
  236. LoadString(hInst, IDS_PRINTINGTITLE, szStr, ARRAYSIZE(szStr));
  237. TextOut (pdPrint.hDC, xExt, yExt, szStr, lstrlen(szStr));
  238. yExt += (yLineExt*2);
  239. FillMemory(szStr, 20, ' ');
  240. /* print text line by line from top to bottom */
  241. for(i=nFromLine; i<=nToLine; i++) {
  242. wsprintf(szStr,_TEXT("%5d"), i);
  243. szStr[5]=' ';
  244. #ifdef UNICODE
  245. szStr[WORD_POS]=lpWord[i-1].wWord;
  246. #else
  247. //szStr[WORD_POS]=HIBYTE(lpWord[i-1].wWord);
  248. //szStr[WORD_POS+1]=LOBYTE(lpWord[i-1].wWord);
  249. #endif
  250. len=lcMem2Disp(i-1, &szStr[PHRASE_POS])+PHRASE_POS;
  251. /* if at end of page, start a new page */
  252. if ((yExt + yLineExt) > (yPageExt - (yPageOff + sLine.cy) * 2))
  253. {
  254. if (EndPage(pdPrint.hDC) == SP_ERROR)
  255. {
  256. DestroyWindow (hCancelDlg);
  257. DeleteDC (pdPrint.hDC);
  258. return IDS_PRINTABORTED;
  259. }
  260. if (StartPage(pdPrint.hDC) <= 0)
  261. {
  262. DestroyWindow (hCancelDlg);
  263. DeleteDC (pdPrint.hDC);
  264. return IDS_PRINTABORTED;
  265. }
  266. yExt = yPageOff + sLine.cy; //TOP_SPACE;
  267. }
  268. if( len <=(nLineChar-PHRASE_POS)) {
  269. /* print current the line and unlock the text handle */
  270. TextOut (pdPrint.hDC, xExt, yExt, szStr, len);
  271. } else {
  272. nStart=nLineChar;
  273. //if(is_DBCS_1st(szStr,nStart-1))
  274. // nStart--;
  275. TextOut (pdPrint.hDC, xExt, yExt, szStr, nStart);
  276. while(nStart < len-1) {
  277. yExt += yLineExt;
  278. /* if at end of page, start a new page */
  279. if ((yExt + yLineExt) > (yPageExt - (yPageOff + sLine.cy) * 2))
  280. {
  281. if (EndPage(pdPrint.hDC) == SP_ERROR)
  282. {
  283. DestroyWindow (hCancelDlg);
  284. DeleteDC (pdPrint.hDC);
  285. return IDS_PRINTABORTED;
  286. }
  287. if (StartPage(pdPrint.hDC) <= 0)
  288. {
  289. DestroyWindow (hCancelDlg);
  290. DeleteDC (pdPrint.hDC);
  291. return IDS_PRINTABORTED;
  292. }
  293. yExt = yPageOff + sLine.cy; //TOP_SPACE;
  294. }
  295. while (szStr[nStart]==' ') nStart++;
  296. nEnd=nStart+nLineChar-PHRASE_POS;
  297. if(nEnd >= len)
  298. nEnd=len;
  299. #ifdef UNICODE
  300. #else
  301. else
  302. if(is_DBCS_1st(szStr,nEnd-1))
  303. nEnd--;
  304. #endif
  305. TextOut (pdPrint.hDC, xExt+sLine.cx*(PHRASE_POS>>1), yExt, &szStr[nStart], nEnd-nStart);
  306. nStart=nEnd;
  307. }
  308. }
  309. /* increment page position */
  310. yExt += yLineExt;
  311. }
  312. /* end the last page and document */
  313. EndPage (pdPrint.hDC);
  314. EndDoc (pdPrint.hDC);
  315. /* end cancel dialog box, clean up and exit */
  316. DestroyWindow (hCancelDlg);
  317. DeleteDC(pdPrint.hDC);
  318. return TRUE;
  319. }
  320. BOOL is_DBCS_1st(
  321. TCHAR *szStr,
  322. int nAddr)
  323. {
  324. #ifndef UNICODE
  325. int i;
  326. BOOL bDBCS=FALSE;
  327. for(i=0; i<=nAddr; i++) {
  328. if(bDBCS)
  329. bDBCS=FALSE;
  330. else
  331. if((szStr[i] >= 0x81) && (szStr[i] <= 0xFE))
  332. bDBCS=TRUE;
  333. }
  334. return bDBCS;
  335. #else
  336. return TRUE;
  337. #endif
  338. }
  339. int TransNum(
  340. TCHAR *szStr)
  341. {
  342. int i,j,nNum;
  343. for(i=0; szStr[i] == ' '; i++) ;
  344. nNum=0;
  345. for(j=i; szStr[j] != 0; j++) {
  346. if((szStr[j] < '0') || (szStr[j] >'9'))
  347. return 0;
  348. nNum=nNum*10+(szStr[j]-'0');
  349. }
  350. return nNum;
  351. }