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.

319 lines
7.7 KiB

  1. /***********************************************************************
  2. MODULE : WMFPRINT.C
  3. FUNCTIONS : PrintWMF
  4. GetPrinterDC
  5. AbortProc
  6. AbortDlg
  7. COMMENTS :
  8. ************************************************************************/
  9. #include "windows.h"
  10. #include "mfdcod32.h"
  11. PRINTDLG pd;
  12. /***********************************************************************
  13. FUNCTION : PrintWMF
  14. PARAMETERS : void
  15. PURPOSE : draw the metafile on a printer dc
  16. CALLS : WINDOWS
  17. wsprintf
  18. MessageBox
  19. MakeProcInstance
  20. Escape
  21. CreateDialog
  22. SetMapMode
  23. SetViewportOrg
  24. SetViewportExt
  25. EnableWindow
  26. PlayMetaFile
  27. DestroyWindow
  28. DeleteDC
  29. APP
  30. WaitCursor
  31. GetPrinterDC
  32. SetPlaceableExts
  33. SetClipMetaExts
  34. MESSAGES : none
  35. RETURNS : BOOL - 0 if unable to print 1 if successful
  36. COMMENTS :
  37. HISTORY : 1/16/91 - created - drc
  38. 7/9/93 - modified for win32 and emf
  39. ************************************************************************/
  40. BOOL PrintWMF(BOOL Dialog)
  41. {
  42. char str[50];
  43. POINT lpPT;
  44. SIZE lpSize;
  45. DOCINFO di;
  46. RECT rc;
  47. //
  48. //display the hourglass cursor
  49. //
  50. WaitCursor(TRUE);
  51. //
  52. //get a DC for the printer
  53. //
  54. hPr = GetPrinterDC(Dialog);
  55. //
  56. //if a DC could not be created then report the error and return
  57. //
  58. if (!hPr)
  59. {
  60. WaitCursor(FALSE);
  61. wsprintf((LPSTR)str, "Cannot print %s", (LPSTR)fnameext);
  62. MessageBox(hWndMain, (LPSTR)str, NULL, MB_OK | MB_ICONHAND);
  63. return (FALSE);
  64. }
  65. //
  66. //define the abort function
  67. //
  68. SetAbortProc(hPr, AbortProc);
  69. //
  70. //Initialize the members of a DOCINFO structure.
  71. //
  72. memset(&di, 0, sizeof(di));
  73. di.cbSize = sizeof(DOCINFO);
  74. di.lpszDocName = (bEnhMeta) ? "Print EMF" : "Print WMF";
  75. di.lpszOutput = (LPTSTR) NULL;
  76. //
  77. //Begin a print job by calling the StartDoc
  78. //function.
  79. //
  80. if (SP_ERROR == (StartDoc(hPr, &di)))
  81. {
  82. //if (Escape(hPr, STARTDOC, 4, "Metafile", (LPSTR) NULL) < 0) {
  83. MessageBox(hWndMain, "Unable to start print job",
  84. NULL, MB_OK | MB_ICONHAND);
  85. DeleteDC(hPr);
  86. }
  87. //
  88. //clear the abort flag
  89. //
  90. bAbort = FALSE;
  91. //
  92. //Create the Abort dialog box (modeless)
  93. //
  94. hAbortDlgWnd = CreateDialog(hInst, "AbortDlg", hWndMain, AbortDlg);
  95. //
  96. //if the dialog was not created report the error
  97. //
  98. if (!hAbortDlgWnd)
  99. {
  100. WaitCursor(FALSE);
  101. MessageBox(hWndMain, "NULL Abort window handle",
  102. NULL, MB_OK | MB_ICONHAND);
  103. return (FALSE);
  104. }
  105. //
  106. //show Abort dialog
  107. //
  108. ShowWindow (hAbortDlgWnd, SW_NORMAL);
  109. //
  110. //disable the main window to avoid reentrancy problems
  111. //
  112. EnableWindow(hWndMain, FALSE);
  113. WaitCursor(FALSE);
  114. //
  115. //if we are still committed to printing
  116. //
  117. if (!bAbort)
  118. {
  119. //
  120. //if this is a placeable metafile then set its origins and extents
  121. //
  122. if (bPlaceableMeta)
  123. SetPlaceableExts(hPr, placeableWMFHeader, WMFPRINTER);
  124. //
  125. //if this is a metafile contained within a clipboard file then set
  126. //its origins and extents accordingly
  127. //
  128. if ( (bMetaInRam) && (!bPlaceableMeta) )
  129. SetClipMetaExts(hPr, lpMFP, lpOldMFP, WMFPRINTER);
  130. //
  131. //if this is a "traditional" windows metafile
  132. //
  133. rc.left = 0;
  134. rc.top = 0;
  135. rc.right = GetDeviceCaps(hPr, HORZRES);
  136. rc.bottom = GetDeviceCaps(hPr, VERTRES);
  137. if (!bMetaInRam)
  138. {
  139. SetMapMode(hPr, MM_TEXT);
  140. SetViewportOrgEx(hPr, 0, 0, &lpPT);
  141. //
  142. //set the extents to the driver supplied values for horizontal
  143. //and vertical resolution
  144. //
  145. SetViewportExtEx(hPr, rc.right, rc.bottom, &lpSize );
  146. }
  147. //
  148. //play the metafile directly to the printer.
  149. //No enumeration involved here
  150. //
  151. if (bEnhMeta)
  152. {
  153. DPtoLP(hPr, (LPPOINT)&rc, 2);
  154. PlayEnhMetaFile(hPr, hemf, &rc);
  155. }
  156. else
  157. PlayMetaFile(hPr, hMF);
  158. }
  159. //
  160. //eject page and end the print job
  161. //
  162. Escape(hPr, NEWFRAME, 0, 0L, 0L);
  163. EndDoc(hPr);
  164. EnableWindow(hWndMain, TRUE);
  165. //
  166. //destroy the Abort dialog box
  167. //
  168. DestroyWindow(hAbortDlgWnd);
  169. DeleteDC(hPr);
  170. return(TRUE);
  171. }
  172. /***********************************************************************
  173. FUNCTION : GetPrinterDC
  174. PARAMETERS : BOOL: Do we want to show a print DLG?
  175. PURPOSE : Get hDc for current device on current output port according
  176. to info in WIN.INI.
  177. CALLS : WINDOWS
  178. GetProfileString
  179. AnsiNext
  180. CreateDC
  181. MESSAGES : none
  182. RETURNS : HANDLE - hDC > 0 if success hDC = 0 if failure
  183. COMMENTS : Searches WIN.INI for information about what printer is
  184. connected, and if found, creates a DC for the printer.
  185. HISTORY : 1/16/91 - created - denniscr
  186. ************************************************************************/
  187. HANDLE GetPrinterDC(BOOL Dialog)
  188. {
  189. memset(&pd, 0, sizeof(PRINTDLG));
  190. pd.lStructSize = sizeof(PRINTDLG);
  191. pd.Flags = PD_RETURNDC | (Dialog?0:PD_RETURNDEFAULT);
  192. pd.hwndOwner = hWndMain ;
  193. return ((PrintDlg(&pd) != 0) ? pd.hDC : NULL);
  194. }
  195. /***********************************************************************
  196. FUNCTION : AbortProc
  197. PARAMETERS : HDC hPr - printer DC
  198. int Code - printing status
  199. PURPOSE : process messages for the abort dialog box
  200. CALLS : WINDOWS
  201. PeekMessage
  202. IsDialogMessage
  203. TranslateMessage
  204. DispatchMessage
  205. MESSAGES : none
  206. RETURNS : int
  207. COMMENTS :
  208. HISTORY : 1/16/91 - created - denniscr
  209. ************************************************************************/
  210. BOOL CALLBACK AbortProc(HDC hPr, int Code)
  211. {
  212. MSG msg;
  213. //
  214. //Process messages intended for the abort dialog box
  215. //
  216. while (!bAbort && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  217. if (!IsDialogMessage(hAbortDlgWnd, &msg))
  218. {
  219. TranslateMessage(&msg);
  220. DispatchMessage(&msg);
  221. }
  222. //
  223. //bAbort is TRUE (return is FALSE) if the user has aborted
  224. //
  225. return (!bAbort);
  226. }
  227. /***********************************************************************
  228. FUNCTION : AbortDlg
  229. PARAMETERS : HWND hDlg;
  230. unsigned msg;
  231. WORD wParam;
  232. LONG lParam;
  233. PURPOSE : Processes messages for printer abort dialog box
  234. CALLS : WINDOWS
  235. SetFocus
  236. MESSAGES : WM_INITDIALOG - initialize dialog box
  237. WM_COMMAND - Input received
  238. RETURNS : BOOL
  239. COMMENTS : This dialog box is created while the program is printing,
  240. and allows the user to cancel the printing process.
  241. HISTORY : 1/16/91 - created - denniscr
  242. ************************************************************************/
  243. INT_PTR CALLBACK AbortDlg(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  244. {
  245. switch(msg)
  246. {
  247. //
  248. //Watch for Cancel button, RETURN key, ESCAPE key, or SPACE BAR
  249. //
  250. case WM_INITDIALOG:
  251. //
  252. //Set the focus to the Cancel box of the dialog
  253. //
  254. SetFocus(GetDlgItem(hDlg, IDCANCEL));
  255. return (TRUE);
  256. case WM_COMMAND:
  257. return (bAbort = TRUE);
  258. }
  259. return (FALSE);
  260. }