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.

344 lines
7.6 KiB

  1. /* File: D:\WACKER\tdll\printhdl.c (Created: 10-Dec-1993)
  2. *
  3. * Copyright 1994 by Hilgraeve Inc. -- Monroe, MI
  4. * All rights reserved
  5. *
  6. * $Revision: 5 $
  7. * $Date: 6/13/01 4:12p $
  8. */
  9. #include <windows.h>
  10. #pragma hdrstop
  11. #include <term\res.h>
  12. #include "stdtyp.h"
  13. #include "mc.h"
  14. #include "assert.h"
  15. #include "print.h"
  16. #include "print.hh"
  17. #include "sf.h"
  18. #include "tdll.h"
  19. #include "htchar.h"
  20. #include "term.h"
  21. #include "session.h"
  22. #include "sess_ids.h"
  23. #include "statusbr.h"
  24. #include "globals.h"
  25. #include "errorbox.h"
  26. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  27. * FUNCTION:
  28. * printCreateHdl
  29. *
  30. * DESCRIPTION:
  31. * Creates a print handle.
  32. *
  33. *
  34. * ARGUMENTS:
  35. * hSession - Exteranl session handle
  36. *
  37. * RETURNS:
  38. * Returns an External Print Handle, or 0 if an error.
  39. *
  40. */
  41. HPRINT printCreateHdl(const HSESSION hSession)
  42. {
  43. HHPRINT hhPrint = 0;
  44. hhPrint = malloc(sizeof(*hhPrint));
  45. if (hhPrint == 0)
  46. {
  47. assert(FALSE);
  48. return 0;
  49. }
  50. memset(hhPrint, 0, sizeof(*hhPrint));
  51. hhPrint->hSession = hSession;
  52. InitializeCriticalSection(&hhPrint->csPrint);
  53. if (printInitializeHdl((HPRINT)hhPrint) != 0)
  54. {
  55. printDestroyHdl((HPRINT)hhPrint);
  56. hhPrint = NULL;
  57. return 0;
  58. }
  59. return (HPRINT)hhPrint;
  60. }
  61. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  62. * FUNCTION:
  63. * printInitializeHdl
  64. *
  65. * DESCRIPTION:
  66. *
  67. *
  68. * ARGUMENTS:
  69. * hPrint - External print handle.
  70. *
  71. * RETURNS:
  72. * 0 if successful, otherwise -1
  73. *
  74. */
  75. int printInitializeHdl(const HPRINT hPrint)
  76. {
  77. unsigned long lSize;
  78. const HHPRINT hhPrint = (HHPRINT)hPrint;
  79. TCHAR *tmp = 0;
  80. TCHAR achBuf[256];
  81. TCHAR *pszString;
  82. int nCharSet;
  83. if (hhPrint == 0)
  84. return -1;
  85. hhPrint->nLnIdx = 0;
  86. // Initialize the printer name to the default.
  87. //
  88. if (GetProfileString("Windows", "Device", ",,,", achBuf,
  89. sizeof(achBuf)) && (pszString = strtok(achBuf, ",")))
  90. {
  91. StrCharCopyN(hhPrint->achPrinterName, pszString, sizeof(hhPrint->achPrinterName) / sizeof(TCHAR));
  92. }
  93. else
  94. {
  95. // Just to let you know, there is no printer.
  96. //
  97. assert(FALSE);
  98. hhPrint->achPrinterName[0] = TEXT('\0');
  99. }
  100. if (hhPrint->pstDevMode != NULL)
  101. {
  102. free(hhPrint->pstDevMode);
  103. hhPrint->pstDevMode = NULL;
  104. }
  105. if (hhPrint->pstDevNames != NULL)
  106. {
  107. free(hhPrint->pstDevNames);
  108. hhPrint->pstDevNames = NULL;
  109. }
  110. lSize = sizeof(hhPrint->achPrinterName);
  111. sfGetSessionItem(sessQuerySysFileHdl(hhPrint->hSession),
  112. SFID_PRINTSET_NAME,
  113. &lSize,
  114. hhPrint->achPrinterName);
  115. lSize = 0;
  116. if (sfGetSessionItem(sessQuerySysFileHdl(hhPrint->hSession),
  117. SFID_PRINTSET_DEVMODE,
  118. &lSize,
  119. 0) == 0 && lSize)
  120. {
  121. if ((hhPrint->pstDevMode = malloc(lSize)))
  122. {
  123. sfGetSessionItem(sessQuerySysFileHdl(hhPrint->hSession),
  124. SFID_PRINTSET_DEVMODE,
  125. &lSize,
  126. hhPrint->pstDevMode);
  127. }
  128. }
  129. lSize = 0;
  130. if (sfGetSessionItem(sessQuerySysFileHdl(hhPrint->hSession),
  131. SFID_PRINTSET_DEVNAMES,
  132. &lSize,
  133. 0) == 0 && lSize)
  134. {
  135. if ((hhPrint->pstDevNames = malloc(lSize)))
  136. {
  137. sfGetSessionItem(sessQuerySysFileHdl(hhPrint->hSession),
  138. SFID_PRINTSET_DEVNAMES,
  139. &lSize,
  140. hhPrint->pstDevNames);
  141. }
  142. }
  143. //
  144. // get the font and margin settings
  145. //
  146. memset(&hhPrint->lf, 0, sizeof(LOGFONT));
  147. memset(&hhPrint->margins, 0, sizeof(RECT));
  148. hhPrint->hFont = NULL;
  149. lSize = sizeof(hhPrint->margins);
  150. sfGetSessionItem( sessQuerySysFileHdl(hhPrint->hSession),
  151. SFID_PRINTSET_MARGINS,
  152. &lSize, &hhPrint->margins );
  153. lSize = sizeof(hhPrint->lf);
  154. sfGetSessionItem( sessQuerySysFileHdl(hhPrint->hSession),
  155. SFID_PRINTSET_FONT,
  156. &lSize, &hhPrint->lf );
  157. lSize = sizeof(hhPrint->iFontPointSize);
  158. sfGetSessionItem( sessQuerySysFileHdl(hhPrint->hSession),
  159. SFID_PRINTSET_FONT_HEIGHT,
  160. &lSize, &hhPrint->iFontPointSize );
  161. //
  162. // use default if we have no value stored
  163. //
  164. if (hhPrint->lf.lfHeight == 0)
  165. {
  166. TCHAR faceName[100];
  167. if ( LoadString(glblQueryDllHinst(), IDS_PRINT_DEF_FONT,
  168. faceName, sizeof (hhPrint->lf.lfFaceName) / sizeof(TCHAR)) )
  169. {
  170. StrCharCopyN( hhPrint->lf.lfFaceName, faceName, sizeof (faceName) / sizeof(TCHAR) );
  171. hhPrint->lf.lfFaceName[sizeof(hhPrint->lf.lfFaceName)/sizeof(TCHAR)-1] = TEXT('\0');
  172. }
  173. hhPrint->lf.lfHeight = -17;
  174. hhPrint->iFontPointSize = 100;
  175. //mpt:2-4-98 changed to use resources so that dbcs fonts print correctly
  176. if (LoadString(glblQueryDllHinst(), IDS_PRINT_DEF_CHARSET,
  177. achBuf, sizeof(achBuf) / sizeof(TCHAR)))
  178. {
  179. nCharSet = atoi(achBuf);
  180. hhPrint->lf.lfCharSet = (BYTE)nCharSet;
  181. }
  182. }
  183. return 0;
  184. }
  185. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  186. * FUNCTION:
  187. * printSaveHdl
  188. *
  189. * DESCRIPTION:
  190. * Saves the name of the selected printer in the session file.
  191. *
  192. * ARGUMENTS:
  193. * hPrint - The external printer handle.
  194. *
  195. * RETURNS:
  196. * void
  197. *
  198. */
  199. void printSaveHdl(const HPRINT hPrint)
  200. {
  201. const HHPRINT hhPrint = (HHPRINT)hPrint;
  202. unsigned long ulSize;
  203. TCHAR *sz;
  204. sfPutSessionItem(sessQuerySysFileHdl(hhPrint->hSession),
  205. SFID_PRINTSET_NAME,
  206. StrCharGetByteCount(hhPrint->achPrinterName) +
  207. sizeof(TCHAR),
  208. hhPrint->achPrinterName);
  209. if (hhPrint->pstDevMode)
  210. {
  211. ulSize = hhPrint->pstDevMode->dmSize +
  212. hhPrint->pstDevMode->dmDriverExtra;
  213. sfPutSessionItem(sessQuerySysFileHdl(hhPrint->hSession),
  214. SFID_PRINTSET_DEVMODE,
  215. ulSize,
  216. hhPrint->pstDevMode);
  217. }
  218. if (hhPrint->pstDevNames)
  219. {
  220. // Getting the size of a DEVNAMES structure is harder.
  221. //
  222. sz = (TCHAR *)hhPrint->pstDevNames +
  223. hhPrint->pstDevNames->wOutputOffset;
  224. sz += StrCharGetByteCount((LPCSTR)sz) + sizeof(TCHAR);
  225. ulSize = (unsigned long)(sz - (TCHAR *)hhPrint->pstDevNames);
  226. sfPutSessionItem(sessQuerySysFileHdl(hhPrint->hSession),
  227. SFID_PRINTSET_DEVNAMES,
  228. ulSize,
  229. hhPrint->pstDevNames);
  230. }
  231. //
  232. // save the font and margin settings
  233. //
  234. sfPutSessionItem( sessQuerySysFileHdl(hhPrint->hSession),
  235. SFID_PRINTSET_MARGINS,
  236. sizeof(hhPrint->margins), &hhPrint->margins );
  237. sfPutSessionItem( sessQuerySysFileHdl(hhPrint->hSession),
  238. SFID_PRINTSET_FONT,
  239. sizeof(hhPrint->lf), &hhPrint->lf );
  240. sfPutSessionItem( sessQuerySysFileHdl(hhPrint->hSession),
  241. SFID_PRINTSET_FONT_HEIGHT,
  242. sizeof(hhPrint->iFontPointSize),
  243. &hhPrint->iFontPointSize );
  244. return;
  245. }
  246. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  247. * FUNCTION:
  248. * printDestroyHdl
  249. *
  250. * DESCRIPTION:
  251. * Destroys a valid print handle.
  252. *
  253. * ARGUMENTS:
  254. * hPrint - AN External Print Handle.
  255. *
  256. * RETURNS:
  257. * void
  258. *
  259. */
  260. void printDestroyHdl(const HPRINT hPrint)
  261. {
  262. const HHPRINT hhPrint = (HHPRINT)hPrint;
  263. if (hhPrint == 0)
  264. return;
  265. if (hhPrint->hFont)
  266. {
  267. DeleteObject(hhPrint->hFont);
  268. }
  269. printEchoClose(hPrint);
  270. DeleteCriticalSection(&hhPrint->csPrint);
  271. //
  272. // Don't forget to free the Printer Device and Printer Name,
  273. // or else there will be a memory leak. REV: 06/13/2001.
  274. //
  275. if (hhPrint->pstDevMode != NULL)
  276. {
  277. free(hhPrint->pstDevMode);
  278. hhPrint->pstDevMode = NULL;
  279. }
  280. if (hhPrint->pstDevNames != NULL)
  281. {
  282. free(hhPrint->pstDevNames);
  283. hhPrint->pstDevNames = NULL;
  284. }
  285. free(hhPrint);
  286. return;
  287. }