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.

565 lines
16 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: Pbhandlr.C -- Native data based handler (for Pbrush server)
  3. *
  4. * PURPOSE: Contains handler routines for Pbrush server. This handler makes
  5. * use of most of the standard library methods. It replaces only the "Draw",
  6. * "QueryBounds", "CopyToClipboard" methods of the OLE object. Note that this
  7. * handler draws the picture from the native data.
  8. *
  9. * Created: December 1990
  10. *
  11. * Copyright (c) 1990 Microsoft Corporation
  12. *
  13. * History:
  14. * SriniK (../12/1990) Original
  15. * curts created portable version for WIN16/32
  16. *
  17. \***************************************************************************/
  18. #include <windows.h>
  19. #include "dll.h"
  20. OLESTATUS FAR PASCAL _LOADDS PbDraw (LPOLEOBJECT, HDC, OLE_CONST RECT FAR*, OLE_CONST RECT FAR*, HDC);
  21. OLESTATUS FAR PASCAL _LOADDS PbQueryBounds (LPOLEOBJECT, LPRECT);
  22. OLESTATUS FAR PASCAL _LOADDS PbCopyToClipboard (LPOLEOBJECT);
  23. OLESTATUS FAR PASCAL _LOADDS PbGetData (LPOLEOBJECT, OLECLIPFORMAT, HANDLE FAR *);
  24. OLECLIPFORMAT FAR PASCAL _LOADDS PbEnumFormats (LPOLEOBJECT, OLECLIPFORMAT);
  25. extern OLESTATUS FARINTERNAL wDibDraw (HANDLE, HDC, LPRECT, LPRECT, HDC, BOOL);
  26. void PbGetExtents (LPSTR, LPPOINT);
  27. void PbReplaceFunctions (LPOLEOBJECT);
  28. HANDLE PbGetPicture (LPOLEOBJECT);
  29. BOOL IsStandardPict (LPOLEOBJECT);
  30. extern void FARINTERNAL DibGetExtents(LPSTR, LPPOINT);
  31. OLEOBJECTVTBL vtblDLL;
  32. extern OLECLIPFORMAT cfOwnerLink;
  33. extern OLECLIPFORMAT cfObjectLink;
  34. extern OLECLIPFORMAT cfNative;
  35. OLESTATUS (FAR PASCAL *DefQueryBounds) (LPOLEOBJECT, LPRECT);
  36. OLESTATUS (FAR PASCAL *DefDraw) (LPOLEOBJECT, HDC, LPRECT, LPRECT, HDC);
  37. OLESTATUS (FAR PASCAL *DefCopyToClipboard) (LPOLEOBJECT);
  38. OLECLIPFORMAT (FAR PASCAL *DefEnumFormats) (LPOLEOBJECT, OLECLIPFORMAT);
  39. OLESTATUS (FAR PASCAL *DefGetData) (LPOLEOBJECT, OLECLIPFORMAT, HANDLE FAR *);
  40. OLESTATUS FAR PASCAL PbLoadFromStream (
  41. LPOLESTREAM lpstream,
  42. LPSTR lpprotocol,
  43. LPOLECLIENT lpclient,
  44. LHCLIENTDOC lhclientdoc,
  45. LPSTR lpobjname,
  46. LPOLEOBJECT FAR * lplpobj,
  47. LONG objType,
  48. ATOM aClass,
  49. OLECLIPFORMAT cfFormat
  50. ){
  51. OLESTATUS retVal;
  52. if (objType == OT_LINK)
  53. retVal = DefLoadFromStream (lpstream, lpprotocol, lpclient,
  54. lhclientdoc, lpobjname, lplpobj,
  55. objType, aClass, cfNative);
  56. else
  57. retVal = DefLoadFromStream (lpstream, lpprotocol, lpclient,
  58. lhclientdoc, lpobjname, lplpobj,
  59. objType, aClass, cfFormat);
  60. if (retVal <= OLE_WAIT_FOR_RELEASE)
  61. PbReplaceFunctions (*lplpobj);
  62. return retVal;
  63. }
  64. OLESTATUS FAR PASCAL PbCreateFromClip (
  65. LPSTR lpprotocol,
  66. LPOLECLIENT lpclient,
  67. LHCLIENTDOC lhclientdoc,
  68. LPSTR lpobjname,
  69. LPOLEOBJECT FAR * lplpobj,
  70. OLEOPT_RENDER optRender,
  71. OLECLIPFORMAT cfFormat,
  72. LONG objType
  73. ){
  74. OLESTATUS retVal;
  75. if ((optRender == olerender_draw)
  76. && (IsClipboardFormatAvailable (cfNative))) {
  77. if (objType == OT_EMBEDDED)
  78. retVal = DefCreateFromClip (lpprotocol, lpclient,
  79. lhclientdoc, lpobjname, lplpobj,
  80. olerender_none, 0, objType);
  81. else
  82. retVal = DefCreateFromClip (lpprotocol, lpclient,
  83. lhclientdoc, lpobjname, lplpobj,
  84. olerender_format, cfNative, objType);
  85. }
  86. else {
  87. retVal = DefCreateFromClip (lpprotocol, lpclient,
  88. lhclientdoc, lpobjname, lplpobj,
  89. optRender, cfFormat, objType);
  90. }
  91. if (retVal <= OLE_WAIT_FOR_RELEASE)
  92. PbReplaceFunctions (*lplpobj);
  93. return retVal;
  94. }
  95. OLESTATUS FAR PASCAL PbCreateLinkFromClip (
  96. LPSTR lpprotocol,
  97. LPOLECLIENT lpclient,
  98. LHCLIENTDOC lhclientdoc,
  99. LPSTR lpobjname,
  100. LPOLEOBJECT FAR * lplpobj,
  101. OLEOPT_RENDER optRender,
  102. OLECLIPFORMAT cfFormat
  103. ){
  104. OLESTATUS retVal;
  105. if ((optRender == olerender_draw)
  106. && (IsClipboardFormatAvailable (cfNative))) {
  107. retVal = DefCreateLinkFromClip (lpprotocol, lpclient,
  108. lhclientdoc, lpobjname, lplpobj,
  109. olerender_format, cfNative);
  110. }
  111. else {
  112. retVal = DefCreateLinkFromClip (lpprotocol, lpclient,
  113. lhclientdoc, lpobjname, lplpobj,
  114. optRender, cfFormat);
  115. }
  116. if (retVal <= OLE_WAIT_FOR_RELEASE)
  117. PbReplaceFunctions (*lplpobj);
  118. return retVal;
  119. }
  120. OLESTATUS FAR PASCAL PbCreateFromTemplate (
  121. LPSTR lpprotocol,
  122. LPOLECLIENT lpclient,
  123. LPSTR lptemplate,
  124. LHCLIENTDOC lhclientdoc,
  125. LPSTR lpobjname,
  126. LPOLEOBJECT FAR * lplpobj,
  127. OLEOPT_RENDER optRender,
  128. OLECLIPFORMAT cfFormat
  129. ){
  130. OLESTATUS retVal;
  131. if (optRender == olerender_draw)
  132. retVal = DefCreateFromTemplate (lpprotocol, lpclient, lptemplate,
  133. lhclientdoc, lpobjname, lplpobj,
  134. olerender_none, 0);
  135. else
  136. retVal = DefCreateFromTemplate (lpprotocol, lpclient, lptemplate,
  137. lhclientdoc, lpobjname, lplpobj,
  138. optRender, cfFormat);
  139. if (retVal <= OLE_WAIT_FOR_RELEASE)
  140. PbReplaceFunctions (*lplpobj);
  141. return retVal;
  142. }
  143. OLESTATUS FAR PASCAL PbCreate (
  144. LPSTR lpprotocol,
  145. LPOLECLIENT lpclient,
  146. LPSTR lpclass,
  147. LHCLIENTDOC lhclientdoc,
  148. LPSTR lpobjname,
  149. LPOLEOBJECT FAR * lplpobj,
  150. OLEOPT_RENDER optRender,
  151. OLECLIPFORMAT cfFormat
  152. ){
  153. OLESTATUS retVal;
  154. if (optRender == olerender_draw)
  155. retVal = DefCreate (lpprotocol, lpclient, lpclass,
  156. lhclientdoc, lpobjname, lplpobj,
  157. olerender_none, 0);
  158. else
  159. retVal = DefCreate (lpprotocol, lpclient, lpclass,
  160. lhclientdoc, lpobjname, lplpobj,
  161. optRender, cfFormat);
  162. if (retVal <= OLE_WAIT_FOR_RELEASE)
  163. PbReplaceFunctions (*lplpobj);
  164. return retVal;
  165. }
  166. OLESTATUS FAR PASCAL PbCreateFromFile (
  167. LPSTR lpprotocol,
  168. LPOLECLIENT lpclient,
  169. LPSTR lpclass,
  170. LPSTR lpfile,
  171. LHCLIENTDOC lhclientdoc,
  172. LPSTR lpobjname,
  173. LPOLEOBJECT FAR * lplpobj,
  174. OLEOPT_RENDER optRender,
  175. OLECLIPFORMAT cfFormat
  176. ){
  177. OLESTATUS retVal;
  178. if (optRender == olerender_draw)
  179. retVal = DefCreateFromFile (lpprotocol, lpclient, lpclass, lpfile,
  180. lhclientdoc, lpobjname, lplpobj,
  181. olerender_none, 0);
  182. else
  183. retVal = DefCreateFromFile (lpprotocol, lpclient, lpclass, lpfile,
  184. lhclientdoc, lpobjname, lplpobj,
  185. optRender, cfFormat);
  186. if (retVal <= OLE_WAIT_FOR_RELEASE)
  187. PbReplaceFunctions (*lplpobj);
  188. return retVal;
  189. }
  190. OLESTATUS FAR PASCAL PbCreateLinkFromFile (
  191. LPSTR lpprotocol,
  192. LPOLECLIENT lpclient,
  193. LPSTR lpclass,
  194. LPSTR lpfile,
  195. LPSTR lpitem,
  196. LHCLIENTDOC lhclientdoc,
  197. LPSTR lpobjname,
  198. LPOLEOBJECT FAR * lplpobj,
  199. OLEOPT_RENDER optRender,
  200. OLECLIPFORMAT cfFormat
  201. ){
  202. OLESTATUS retVal;
  203. if (optRender == olerender_draw)
  204. retVal = DefCreateLinkFromFile (lpprotocol, lpclient,
  205. lpclass, lpfile, lpitem,
  206. lhclientdoc, lpobjname, lplpobj,
  207. olerender_format, cfNative);
  208. else
  209. retVal = DefCreateLinkFromFile (lpprotocol, lpclient,
  210. lpclass, lpfile, lpitem,
  211. lhclientdoc, lpobjname, lplpobj,
  212. optRender, cfFormat);
  213. if (retVal <= OLE_WAIT_FOR_RELEASE)
  214. PbReplaceFunctions (*lplpobj);
  215. return retVal;
  216. }
  217. OLESTATUS FAR PASCAL PbCreateInvisible (
  218. LPSTR lpprotocol,
  219. LPOLECLIENT lpclient,
  220. LPSTR lpclass,
  221. LHCLIENTDOC lhclientdoc,
  222. LPSTR lpobjname,
  223. LPOLEOBJECT FAR * lplpobj,
  224. OLEOPT_RENDER optRender,
  225. OLECLIPFORMAT cfFormat,
  226. BOOL fActivate
  227. ){
  228. OLESTATUS retVal;
  229. if (optRender == olerender_draw)
  230. retVal = DefCreateInvisible (lpprotocol, lpclient, lpclass,
  231. lhclientdoc, lpobjname, lplpobj,
  232. olerender_none, 0, fActivate);
  233. else
  234. retVal = DefCreateInvisible (lpprotocol, lpclient, lpclass,
  235. lhclientdoc, lpobjname, lplpobj,
  236. optRender, cfFormat, fActivate);
  237. if (retVal <= OLE_WAIT_FOR_RELEASE)
  238. PbReplaceFunctions (*lplpobj);
  239. return retVal;
  240. }
  241. void PbReplaceFunctions (
  242. LPOLEOBJECT lpobj
  243. ){
  244. if (IsStandardPict (lpobj))
  245. return;
  246. vtblDLL = *lpobj->lpvtbl;
  247. lpobj->lpvtbl = (LPOLEOBJECTVTBL) &vtblDLL;
  248. DefDraw = lpobj->lpvtbl->Draw;
  249. DefQueryBounds = lpobj->lpvtbl->QueryBounds;
  250. DefCopyToClipboard = lpobj->lpvtbl->CopyToClipboard;
  251. DefEnumFormats = lpobj->lpvtbl->EnumFormats;
  252. DefGetData = lpobj->lpvtbl->GetData;
  253. lpobj->lpvtbl->Draw = PbDraw;
  254. lpobj->lpvtbl->QueryBounds = PbQueryBounds;
  255. lpobj->lpvtbl->CopyToClipboard = PbCopyToClipboard;
  256. lpobj->lpvtbl->EnumFormats = PbEnumFormats;
  257. lpobj->lpvtbl->GetData = PbGetData;
  258. }
  259. OLESTATUS FAR PASCAL _LOADDS PbQueryBounds (
  260. LPOLEOBJECT lpobj,
  261. LPRECT lprc
  262. ){
  263. OLESTATUS retVal;
  264. HANDLE hData = NULL;
  265. LPSTR lpData;
  266. POINT point;
  267. HANDLE hbminfo = NULL;
  268. LPBITMAPINFO lpbminfo;
  269. if ((retVal = (*DefQueryBounds) (lpobj, lprc)) == OLE_OK) {
  270. if (lprc->top || lprc->bottom || lprc->right || lprc->left)
  271. return OLE_OK;
  272. }
  273. if ((*DefGetData) (lpobj, cfNative, &hData) != OLE_OK)
  274. return retVal;
  275. if (!hData)
  276. return OLE_ERROR_BLANK;
  277. if (!(lpData = GlobalLock (hData)))
  278. goto error;
  279. if (!(hbminfo = GlobalAlloc(GHND, sizeof(BITMAPINFO))) )
  280. goto error;
  281. if (!(lpbminfo = (LPBITMAPINFO)GlobalLock(hbminfo)) )
  282. goto error;
  283. memcpy((LPSTR)lpbminfo, (LPSTR)(lpData+sizeof(BITMAPFILEHEADER)), sizeof(BITMAPINFO));
  284. DibGetExtents ((LPSTR)lpbminfo, &point);
  285. GlobalUnlock (hData);
  286. GlobalUnlock (hbminfo);
  287. GlobalFree (hbminfo);
  288. lprc->left = 0;
  289. lprc->top = 0;
  290. lprc->right = point.x;
  291. lprc->bottom = point.y;
  292. return OLE_OK;
  293. error:
  294. if (hData)
  295. GlobalUnlock (hData);
  296. if (hbminfo)
  297. {
  298. GlobalUnlock (hbminfo);
  299. GlobalFree (hbminfo);
  300. }
  301. return OLE_ERROR_MEMORY;
  302. }
  303. OLESTATUS FAR PASCAL _LOADDS PbDraw (
  304. LPOLEOBJECT lpobj,
  305. HDC hdc,
  306. OLE_CONST RECT FAR* lprc,
  307. OLE_CONST RECT FAR* lpWrc,
  308. HDC hdcTarget
  309. ){
  310. HANDLE hData;
  311. if ((*DefGetData) (lpobj, cfNative, &hData) != OLE_OK)
  312. return (*DefDraw) (lpobj, hdc, (LPRECT)lprc, (LPRECT)lpWrc, hdcTarget);
  313. return wDibDraw (hData, hdc, (LPRECT)lprc, (LPRECT)lpWrc, hdcTarget, TRUE);
  314. }
  315. OLECLIPFORMAT FAR PASCAL _LOADDS PbEnumFormats (
  316. LPOLEOBJECT lpobj,
  317. OLECLIPFORMAT cfFormat
  318. ){
  319. OLECLIPFORMAT retFormat = 0;
  320. if (cfFormat == CF_METAFILEPICT)
  321. return 0;
  322. if (!(retFormat = (*DefEnumFormats) (lpobj, cfFormat)))
  323. return CF_METAFILEPICT;
  324. return retFormat;
  325. }
  326. OLESTATUS FAR PASCAL _LOADDS PbGetData (
  327. LPOLEOBJECT lpobj,
  328. OLECLIPFORMAT cfFormat,
  329. HANDLE FAR * lpHandle
  330. ){
  331. OLESTATUS retval;
  332. retval = (*DefGetData) (lpobj, cfFormat, lpHandle);
  333. if (retval == OLE_OK || retval == OLE_BUSY || retval == OLE_ERROR_BLANK)
  334. return retval;
  335. if (cfFormat == CF_METAFILEPICT) {
  336. if (*lpHandle = PbGetPicture (lpobj))
  337. return OLE_WARN_DELETE_DATA;
  338. return OLE_ERROR_MEMORY;
  339. }
  340. return OLE_ERROR_FORMAT;
  341. }
  342. OLESTATUS FAR PASCAL _LOADDS PbCopyToClipboard (
  343. LPOLEOBJECT lpobj
  344. ){
  345. OLESTATUS retVal;
  346. HANDLE hPict;
  347. if ((retVal = (*DefCopyToClipboard) (lpobj)) == OLE_OK) {
  348. if (hPict = PbGetPicture (lpobj))
  349. SetClipboardData (CF_METAFILEPICT, hPict);
  350. else
  351. return OLE_ERROR_MEMORY;
  352. }
  353. return retVal;
  354. }
  355. HANDLE PbGetPicture (
  356. LPOLEOBJECT lpobj
  357. ){
  358. HANDLE hMF, hMfp;
  359. HANDLE hData = NULL;
  360. HANDLE hbminfo = NULL;
  361. RECT rc = {0, 0, 0, 0};
  362. POINT point;
  363. HDC hMetaDC;
  364. LPMETAFILEPICT lpmfp;
  365. OLESTATUS retVal;
  366. LPSTR lpData;
  367. LPBITMAPINFO lpbminfo;
  368. if ((*DefGetData) (lpobj, cfNative, &hData) != OLE_OK)
  369. return NULL;
  370. if (!hData)
  371. return NULL;
  372. if (!(lpData = GlobalLock (hData)))
  373. return NULL;
  374. if (!(hbminfo = GlobalAlloc(GHND, sizeof(BITMAPINFO))) )
  375. goto memory_error;
  376. if (!(lpbminfo = (LPBITMAPINFO)GlobalLock(hbminfo)) )
  377. goto memory_error;
  378. memcpy((LPSTR)lpbminfo, (LPSTR)(lpData+sizeof(BITMAPFILEHEADER)), sizeof(BITMAPINFO));
  379. rc.right = (int) lpbminfo->bmiHeader.biWidth;
  380. rc.bottom = (int) lpbminfo->bmiHeader.biHeight;
  381. DibGetExtents((LPSTR)lpbminfo, &point);
  382. GlobalUnlock (hData);
  383. GlobalUnlock (hbminfo);
  384. GlobalFree (hbminfo);
  385. if (!(hMetaDC = CreateMetaFile (NULL)))
  386. return NULL;
  387. MSetWindowOrg (hMetaDC, 0, 0);
  388. MSetWindowExt (hMetaDC, rc.right, rc.bottom);
  389. retVal = PbDraw (lpobj, hMetaDC, &rc, NULL, NULL);
  390. hMF = CloseMetaFile (hMetaDC);
  391. if (retVal != OLE_OK)
  392. goto error;
  393. if (hMF && (hMfp = GlobalAlloc (GMEM_MOVEABLE, sizeof(METAFILEPICT)))
  394. && (lpmfp = (LPMETAFILEPICT) GlobalLock (hMfp))) {
  395. lpmfp->hMF = hMF;
  396. lpmfp->xExt = point.x;
  397. lpmfp->yExt = -point.y;
  398. lpmfp->mm = MM_ANISOTROPIC;
  399. GlobalUnlock (hMfp);
  400. return hMfp;
  401. }
  402. error:
  403. if (hMF)
  404. DeleteMetaFile (hMF);
  405. if (hMfp)
  406. GlobalFree (hMfp);
  407. return NULL;
  408. memory_error:
  409. GlobalUnlock(hData);
  410. if (hbminfo)
  411. {
  412. GlobalUnlock(hbminfo);
  413. GlobalFree(hbminfo);
  414. }
  415. return(NULL);
  416. }
  417. // normal handler can't do this. since this handler is part of olecli.dll, we
  418. // we are doing this.
  419. BOOL IsStandardPict (
  420. LPOLEOBJECT lpobj
  421. ){
  422. LPOBJECT_LE lpLEobj;
  423. LONG type;
  424. lpLEobj = (LPOBJECT_LE) lpobj;
  425. if (!lpLEobj->lpobjPict)
  426. return FALSE;
  427. if ((*lpLEobj->lpobjPict->lpvtbl->QueryType) (lpLEobj->lpobjPict, &type)
  428. == OLE_ERROR_GENERIC)
  429. return FALSE;
  430. return TRUE;
  431. }