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.

328 lines
8.5 KiB

  1. /*************************************************************************
  2. **
  3. ** OLE 2 Standard Utilities
  4. **
  5. ** olestd.c
  6. **
  7. ** This file contains utilities that are useful for dealing with
  8. ** target devices.
  9. **
  10. ** (c) Copyright Microsoft Corp. 1992 All Rights Reserved
  11. **
  12. *************************************************************************/
  13. #define STRICT 1
  14. #include "ole2ui.h"
  15. #ifndef WIN32
  16. #include <print.h>
  17. #endif
  18. /*
  19. * OleStdCreateDC()
  20. *
  21. * Purpose:
  22. *
  23. * Parameters:
  24. *
  25. * Return Value:
  26. * SCODE - S_OK if successful
  27. */
  28. STDAPI_(HDC) OleStdCreateDC(DVTARGETDEVICE FAR* ptd)
  29. {
  30. HDC hdc=NULL;
  31. LPDEVNAMES lpDevNames;
  32. LPDEVMODE lpDevMode;
  33. LPTSTR lpszDriverName;
  34. LPTSTR lpszDeviceName;
  35. LPTSTR lpszPortName;
  36. if (ptd == NULL) {
  37. hdc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
  38. goto errReturn;
  39. }
  40. lpDevNames = (LPDEVNAMES) ptd; // offset for size field
  41. if (ptd->tdExtDevmodeOffset == 0) {
  42. lpDevMode = NULL;
  43. }else{
  44. lpDevMode = (LPDEVMODE) ((LPSTR)ptd + ptd->tdExtDevmodeOffset);
  45. }
  46. lpszDriverName = (LPTSTR) lpDevNames + ptd->tdDriverNameOffset;
  47. lpszDeviceName = (LPTSTR) lpDevNames + ptd->tdDeviceNameOffset;
  48. lpszPortName = (LPTSTR) lpDevNames + ptd->tdPortNameOffset;
  49. hdc = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, lpDevMode);
  50. errReturn:
  51. return hdc;
  52. }
  53. /*
  54. * OleStdCreateIC()
  55. *
  56. * Purpose: Same as OleStdCreateDC, except that information context is
  57. * created, rather than a whole device context. (CreateIC is
  58. * used rather than CreateDC).
  59. * OleStdDeleteDC is still used to delete the information context.
  60. *
  61. * Parameters:
  62. *
  63. * Return Value:
  64. * SCODE - S_OK if successful
  65. */
  66. STDAPI_(HDC) OleStdCreateIC(DVTARGETDEVICE FAR* ptd)
  67. {
  68. HDC hdcIC=NULL;
  69. LPDEVNAMES lpDevNames;
  70. LPDEVMODE lpDevMode;
  71. LPTSTR lpszDriverName;
  72. LPTSTR lpszDeviceName;
  73. LPTSTR lpszPortName;
  74. if (ptd == NULL) {
  75. hdcIC = CreateIC(TEXT("DISPLAY"), NULL, NULL, NULL);
  76. goto errReturn;
  77. }
  78. lpDevNames = (LPDEVNAMES) ptd; // offset for size field
  79. lpDevMode = (LPDEVMODE) ((LPTSTR)ptd + ptd->tdExtDevmodeOffset);
  80. lpszDriverName = (LPTSTR) lpDevNames + ptd->tdDriverNameOffset;
  81. lpszDeviceName = (LPTSTR) lpDevNames + ptd->tdDeviceNameOffset;
  82. lpszPortName = (LPTSTR) lpDevNames + ptd->tdPortNameOffset;
  83. hdcIC = CreateIC(lpszDriverName, lpszDeviceName, lpszPortName, lpDevMode);
  84. errReturn:
  85. return hdcIC;
  86. }
  87. #ifdef NEVER
  88. // This code is wrong
  89. /*
  90. * OleStdCreateTargetDevice()
  91. *
  92. * Purpose:
  93. *
  94. * Parameters:
  95. *
  96. * Return Value:
  97. * SCODE - S_OK if successful
  98. */
  99. STDAPI_(DVTARGETDEVICE FAR*) OleStdCreateTargetDevice(LPPRINTDLG lpPrintDlg)
  100. {
  101. DVTARGETDEVICE FAR* ptd=NULL;
  102. LPDEVNAMES lpDevNames, pDN;
  103. LPDEVMODE lpDevMode, pDM;
  104. UINT nMaxOffset;
  105. LPTSTR pszName;
  106. DWORD dwDevNamesSize, dwDevModeSize, dwPtdSize;
  107. if ((pDN = (LPDEVNAMES)GlobalLock(lpPrintDlg->hDevNames)) == NULL) {
  108. goto errReturn;
  109. }
  110. if ((pDM = (LPDEVMODE)GlobalLock(lpPrintDlg->hDevMode)) == NULL) {
  111. goto errReturn;
  112. }
  113. nMaxOffset = (pDN->wDriverOffset > pDN->wDeviceOffset) ?
  114. pDN->wDriverOffset : pDN->wDeviceOffset ;
  115. nMaxOffset = (pDN->wOutputOffset > nMaxOffset) ?
  116. pDN->wOutputOffset : nMaxOffset ;
  117. pszName = (LPTSTR)pDN + nMaxOffset;
  118. dwDevNamesSize = (DWORD)((nMaxOffset+lstrlen(pszName) + 1/* NULL term */)*sizeof(TCHAR));
  119. dwDevModeSize = (DWORD) (pDM->dmSize + pDM->dmDriverExtra);
  120. dwPtdSize = sizeof(DWORD) + dwDevNamesSize + dwDevModeSize;
  121. if ((ptd = (DVTARGETDEVICE FAR*)OleStdMalloc(dwPtdSize)) != NULL) {
  122. // copy in the info
  123. ptd->tdSize = (UINT)dwPtdSize;
  124. lpDevNames = (LPDEVNAMES) &ptd->tdDriverNameOffset;
  125. _fmemcpy(lpDevNames, pDN, (size_t)dwDevNamesSize);
  126. lpDevMode=(LPDEVMODE)((LPTSTR)&ptd->tdDriverNameOffset+dwDevNamesSize);
  127. _fmemcpy(lpDevMode, pDM, (size_t)dwDevModeSize);
  128. ptd->tdDriverNameOffset += 4 ;
  129. ptd->tdDeviceNameOffset += 4 ;
  130. ptd->tdPortNameOffset += 4 ;
  131. ptd->tdExtDevmodeOffset = (UINT)dwDevNamesSize + 4 ;
  132. }
  133. errReturn:
  134. GlobalUnlock(lpPrintDlg->hDevNames);
  135. GlobalUnlock(lpPrintDlg->hDevMode);
  136. return ptd;
  137. }
  138. #endif // NEVER
  139. /*
  140. * OleStdDeleteTargetDevice()
  141. *
  142. * Purpose:
  143. *
  144. * Parameters:
  145. *
  146. * Return Value:
  147. * SCODE - S_OK if successful
  148. */
  149. STDAPI_(BOOL) OleStdDeleteTargetDevice(DVTARGETDEVICE FAR* ptd)
  150. {
  151. BOOL res=TRUE;
  152. if (ptd != NULL) {
  153. OleStdFree(ptd);
  154. }
  155. return res;
  156. }
  157. /*
  158. * OleStdCopyTargetDevice()
  159. *
  160. * Purpose:
  161. * duplicate a TARGETDEVICE struct. this function allocates memory for
  162. * the copy. the caller MUST free the allocated copy when done with it
  163. * using the standard allocator returned from CoGetMalloc.
  164. * (OleStdFree can be used to free the copy).
  165. *
  166. * Parameters:
  167. * ptdSrc pointer to source TARGETDEVICE
  168. *
  169. * Return Value:
  170. * pointer to allocated copy of ptdSrc
  171. * if ptdSrc==NULL then retuns NULL is returned.
  172. * if ptdSrc!=NULL and memory allocation fails, then NULL is returned
  173. */
  174. STDAPI_(DVTARGETDEVICE FAR*) OleStdCopyTargetDevice(DVTARGETDEVICE FAR* ptdSrc)
  175. {
  176. DVTARGETDEVICE FAR* ptdDest = NULL;
  177. if (ptdSrc == NULL) {
  178. return NULL;
  179. }
  180. if ((ptdDest = (DVTARGETDEVICE FAR*)OleStdMalloc(ptdSrc->tdSize)) != NULL) {
  181. _fmemcpy(ptdDest, ptdSrc, (size_t)ptdSrc->tdSize);
  182. }
  183. return ptdDest;
  184. }
  185. /*
  186. * OleStdCopyFormatEtc()
  187. *
  188. * Purpose:
  189. * Copies the contents of a FORMATETC structure. this function takes
  190. * special care to copy correctly copying the pointer to the TARGETDEVICE
  191. * contained within the source FORMATETC structure.
  192. * if the source FORMATETC has a non-NULL TARGETDEVICE, then a copy
  193. * of the TARGETDEVICE will be allocated for the destination of the
  194. * FORMATETC (petcDest).
  195. *
  196. * OLE2NOTE: the caller MUST free the allocated copy of the TARGETDEVICE
  197. * within the destination FORMATETC when done with it
  198. * using the standard allocator returned from CoGetMalloc.
  199. * (OleStdFree can be used to free the copy).
  200. *
  201. * Parameters:
  202. * petcDest pointer to destination FORMATETC
  203. * petcSrc pointer to source FORMATETC
  204. *
  205. * Return Value:
  206. * returns TRUE is copy is successful; retuns FALSE if not successful
  207. */
  208. STDAPI_(BOOL) OleStdCopyFormatEtc(LPFORMATETC petcDest, LPFORMATETC petcSrc)
  209. {
  210. if ((petcDest == NULL) || (petcSrc == NULL)) {
  211. return FALSE;
  212. }
  213. petcDest->cfFormat = petcSrc->cfFormat;
  214. petcDest->ptd = OleStdCopyTargetDevice(petcSrc->ptd);
  215. petcDest->dwAspect = petcSrc->dwAspect;
  216. petcDest->lindex = petcSrc->lindex;
  217. petcDest->tymed = petcSrc->tymed;
  218. return TRUE;
  219. }
  220. // returns 0 for exact match, 1 for no match, -1 for partial match (which is
  221. // defined to mean the left is a subset of the right: fewer aspects, null target
  222. // device, fewer medium).
  223. STDAPI_(int) OleStdCompareFormatEtc(FORMATETC FAR* pFetcLeft, FORMATETC FAR* pFetcRight)
  224. {
  225. BOOL bExact = TRUE;
  226. if (pFetcLeft->cfFormat != pFetcRight->cfFormat)
  227. return 1;
  228. else if (!OleStdCompareTargetDevice (pFetcLeft->ptd, pFetcRight->ptd))
  229. return 1;
  230. if (pFetcLeft->dwAspect == pFetcRight->dwAspect)
  231. // same aspects; equal
  232. ;
  233. else if ((pFetcLeft->dwAspect & ~pFetcRight->dwAspect) != 0)
  234. // left not subset of aspects of right; not equal
  235. return 1;
  236. else
  237. // left subset of right
  238. bExact = FALSE;
  239. if (pFetcLeft->tymed == pFetcRight->tymed)
  240. // same medium flags; equal
  241. ;
  242. else if ((pFetcLeft->tymed & ~pFetcRight->tymed) != 0)
  243. // left not subset of medium flags of right; not equal
  244. return 1;
  245. else
  246. // left subset of right
  247. bExact = FALSE;
  248. return bExact ? 0 : -1;
  249. }
  250. STDAPI_(BOOL) OleStdCompareTargetDevice
  251. (DVTARGETDEVICE FAR* ptdLeft, DVTARGETDEVICE FAR* ptdRight)
  252. {
  253. if (ptdLeft == ptdRight)
  254. // same address of td; must be same (handles NULL case)
  255. return TRUE;
  256. else if ((ptdRight == NULL) || (ptdLeft == NULL))
  257. return FALSE;
  258. else if (ptdLeft->tdSize != ptdRight->tdSize)
  259. // different sizes, not equal
  260. return FALSE;
  261. #ifdef WIN32
  262. else if (memcmp(ptdLeft, ptdRight, ptdLeft->tdSize) != 0)
  263. #else
  264. else if (_fmemcmp(ptdLeft, ptdRight, (int)ptdLeft->tdSize) != 0)
  265. #endif
  266. // not same target device, not equal
  267. return FALSE;
  268. return TRUE;
  269. }