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.

317 lines
7.7 KiB

  1. /*
  2. * o l e u t i l . c p p
  3. *
  4. * Purpose:
  5. * OLE utilities
  6. *
  7. * History
  8. * Feb '97: brettm - created
  9. *
  10. * Copyright (C) Microsoft Corp. 1995, 1996.
  11. */
  12. #include <pch.hxx>
  13. #include <resource.h>
  14. #include <oleutil.h>
  15. #include <mimeole.h>
  16. #include <richedit.h>
  17. #include <richole.h>
  18. ASSERTDATA
  19. #define HIMETRIC_PER_INCH 2540 // number HIMETRIC units per inch
  20. #define MAP_PIX_TO_LOGHIM(x,ppli) MulDiv(HIMETRIC_PER_INCH, (x), (ppli))
  21. #define MAP_LOGHIM_TO_PIX(x,ppli) MulDiv((ppli), (x), HIMETRIC_PER_INCH)
  22. HRESULT HrGetDataStream(LPUNKNOWN pUnk, CLIPFORMAT cf, LPSTREAM *ppstm)
  23. {
  24. LPDATAOBJECT pDataObj=0;
  25. HRESULT hr;
  26. FORMATETC fetc={cf, 0, DVASPECT_CONTENT, -1, TYMED_ISTREAM};
  27. STGMEDIUM stgmed;
  28. ZeroMemory(&stgmed, sizeof(STGMEDIUM));
  29. if (!pUnk || !ppstm)
  30. return E_INVALIDARG;
  31. hr=pUnk->QueryInterface(IID_IDataObject, (LPVOID *)&pDataObj);
  32. if(FAILED(hr))
  33. goto error;
  34. hr=pDataObj->GetData(&fetc, &stgmed);
  35. if(FAILED(hr))
  36. goto error;
  37. Assert(stgmed.pstm);
  38. *ppstm = stgmed.pstm;
  39. (*ppstm)->AddRef();
  40. // addref the pUnk as it will be release in releasestgmed
  41. if(stgmed.pUnkForRelease)
  42. stgmed.pUnkForRelease->AddRef();
  43. ReleaseStgMedium(&stgmed);
  44. error:
  45. ReleaseObj(pDataObj);
  46. return hr;
  47. }
  48. HRESULT HrInitNew(LPUNKNOWN pUnk)
  49. {
  50. LPPERSISTSTREAMINIT ppsi=0;
  51. HRESULT hr;
  52. if (!pUnk)
  53. return E_INVALIDARG;
  54. hr=pUnk->QueryInterface(IID_IPersistStreamInit, (LPVOID *)&ppsi);
  55. if (FAILED(hr))
  56. goto error;
  57. hr = ppsi->InitNew();
  58. error:
  59. ReleaseObj(ppsi);
  60. return hr;
  61. }
  62. HRESULT HrIPersistFileSave(LPUNKNOWN pUnk, LPSTR pszFile)
  63. {
  64. HRESULT hr = S_OK;
  65. LPWSTR pwszFile = NULL;
  66. Assert(pUnk);
  67. Assert(lstrlen(pszFile) <= MAX_PATH);
  68. if ((NULL == pszFile) || (0 == *pszFile))
  69. return E_INVALIDARG;
  70. IF_NULLEXIT(pwszFile = PszToUnicode(CP_ACP, pszFile));
  71. hr = HrIPersistFileSaveW(pUnk, pwszFile);
  72. exit:
  73. MemFree(pwszFile);
  74. return hr;
  75. }
  76. HRESULT HrIPersistFileSaveW(LPUNKNOWN pUnk, LPWSTR pwszFile)
  77. {
  78. HRESULT hr;
  79. LPPERSISTFILE ppf=0;
  80. Assert(pUnk);
  81. Assert(lstrlenW(pwszFile) <= MAX_PATH);
  82. if ((NULL == pwszFile) || (NULL == *pwszFile))
  83. return E_INVALIDARG;
  84. hr=pUnk->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf);
  85. if (FAILED(hr))
  86. goto error;
  87. hr=ppf->Save(pwszFile, FALSE);
  88. if (FAILED(hr))
  89. goto error;
  90. error:
  91. ReleaseObj(ppf);
  92. return hr;
  93. }
  94. HRESULT HrIPersistFileLoad(LPUNKNOWN pUnk, LPSTR lpszFile)
  95. {
  96. LPWSTR lpwszFile;
  97. HRESULT hr = S_OK;
  98. if (lpszFile == NULL || *lpszFile == NULL)
  99. return E_INVALIDARG;
  100. IF_NULLEXIT(lpwszFile = PszToUnicode(CP_ACP, lpszFile));
  101. hr = HrIPersistFileLoadW(pUnk, lpwszFile);
  102. exit:
  103. MemFree(lpwszFile);
  104. return hr;
  105. }
  106. HRESULT HrIPersistFileLoadW(LPUNKNOWN pUnk, LPWSTR lpwszFile)
  107. {
  108. HRESULT hr;
  109. LPPERSISTFILE ppf=0;
  110. WCHAR szFileW[MAX_PATH];
  111. Assert(lstrlenW(lpwszFile) <= MAX_PATH);
  112. if (lpwszFile == NULL || *lpwszFile == NULL)
  113. return E_INVALIDARG;
  114. hr=pUnk->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf);
  115. if (FAILED(hr))
  116. goto error;
  117. hr=ppf->Load(lpwszFile, STGM_READ|STGM_SHARE_DENY_NONE);
  118. if (FAILED(hr))
  119. goto error;
  120. error:
  121. ReleaseObj(ppf);
  122. return hr;
  123. }
  124. #ifdef DEBUG
  125. /////////////////////////////////////////////////////////////////////////////
  126. //
  127. // BEGIN Debug Code
  128. //
  129. void DbgPrintInterface(REFIID riid, char *szPrefix, int iLevel)
  130. {
  131. LPOLESTR pszW=0;
  132. char szGuid[MAX_PATH];
  133. char szTmp[MAX_PATH];
  134. char szOut[MAX_PATH];
  135. LONG cb=MAX_PATH;
  136. HKEY hk=0;
  137. AssertSz(szPrefix, "At least pass a null string!");
  138. StringFromIID(riid, &pszW);
  139. WideCharToMultiByte(CP_ACP, 0, pszW, -1, szGuid, MAX_PATH, NULL, NULL);
  140. StrCpyN(szTmp, "SOFTWARE\\Classes\\Interface\\", ARRAYSIZE(szTmp));
  141. StrCatBuff(szTmp, szGuid, ARRAYSIZE(szTmp));
  142. if((RegOpenKeyEx(HKEY_LOCAL_MACHINE, szTmp, 0, KEY_QUERY_VALUE, &hk)==ERROR_SUCCESS) &&
  143. RegQueryValue(hk, NULL, szTmp, &cb)==ERROR_SUCCESS)
  144. {
  145. wnsprintf(szOut, ARRAYSIZE(szOut), "%s: {%s}", szPrefix, szTmp);
  146. }
  147. else
  148. {
  149. wnsprintf(szOut, ARRAYSIZE(szOut), "%s: [notfound] %s", szPrefix, szGuid);
  150. }
  151. DOUTL(iLevel, szOut);
  152. if(hk)
  153. RegCloseKey(hk);
  154. if(pszW)
  155. CoTaskMemFree(pszW);
  156. }
  157. //
  158. // END Debug Code
  159. //
  160. /////////////////////////////////////////////////////////////////////////////
  161. #endif // DEBUG
  162. // OLE Utilities:
  163. void XformSizeInPixelsToHimetric(HDC hDC, LPSIZEL lpSizeInPix, LPSIZEL lpSizeInHiMetric)
  164. {
  165. int iXppli; //Pixels per logical inch along width
  166. int iYppli; //Pixels per logical inch along height
  167. BOOL fSystemDC=FALSE;
  168. if(!hDC||!GetDeviceCaps(hDC, LOGPIXELSX))
  169. {
  170. hDC=GetDC(NULL);
  171. fSystemDC=TRUE;
  172. }
  173. iXppli = GetDeviceCaps (hDC, LOGPIXELSX);
  174. iYppli = GetDeviceCaps (hDC, LOGPIXELSY);
  175. //We got pixel units, convert them to logical HIMETRIC along the display
  176. lpSizeInHiMetric->cx = (long)MAP_PIX_TO_LOGHIM((int)lpSizeInPix->cx, iXppli);
  177. lpSizeInHiMetric->cy = (long)MAP_PIX_TO_LOGHIM((int)lpSizeInPix->cy, iYppli);
  178. if (fSystemDC)
  179. ReleaseDC(NULL, hDC);
  180. return;
  181. }
  182. void XformSizeInHimetricToPixels( HDC hDC,
  183. LPSIZEL lpSizeInHiMetric,
  184. LPSIZEL lpSizeInPix)
  185. {
  186. int iXppli; //Pixels per logical inch along width
  187. int iYppli; //Pixels per logical inch along height
  188. BOOL fSystemDC=FALSE;
  189. if (NULL==hDC ||
  190. GetDeviceCaps(hDC, LOGPIXELSX) == 0)
  191. {
  192. hDC=GetDC(NULL);
  193. fSystemDC=TRUE;
  194. }
  195. iXppli = GetDeviceCaps (hDC, LOGPIXELSX);
  196. iYppli = GetDeviceCaps (hDC, LOGPIXELSY);
  197. //We got logical HIMETRIC along the display, convert them to pixel units
  198. lpSizeInPix->cx = (long)MAP_LOGHIM_TO_PIX((int)lpSizeInHiMetric->cx, iXppli);
  199. lpSizeInPix->cy = (long)MAP_LOGHIM_TO_PIX((int)lpSizeInHiMetric->cy, iYppli);
  200. if (fSystemDC)
  201. ReleaseDC(NULL, hDC);
  202. }
  203. void DoNoteOleVerb(int iVerb)
  204. {
  205. HWND hwndRE=GetFocus();
  206. HRESULT hr;
  207. POINT pt;
  208. RECT rc;
  209. REOBJECT reobj={0};
  210. LPRICHEDITOLE preole = NULL;
  211. if(!hwndRE)
  212. return;
  213. reobj.cbStruct = sizeof(REOBJECT);
  214. // ~~~~ Might be able to go off of the PHCI object tied to the RichEdit
  215. if(!SendMessage(hwndRE, EM_GETOLEINTERFACE, 0, (LPARAM) &preole))
  216. return;
  217. Assert(preole);
  218. AssertSz(SendMessage(hwndRE, EM_SELECTIONTYPE, 0, 0) == SEL_OBJECT, "MORE THAN ONE OLEOBJECT SELECTED, How did we get here??");
  219. hr=preole->GetObject(REO_IOB_SELECTION, &reobj, REO_GETOBJ_POLEOBJ|REO_GETOBJ_POLESITE);
  220. if(FAILED(hr))
  221. goto Cleanup;
  222. SendMessage(hwndRE, EM_POSFROMCHAR, (WPARAM) &pt, reobj.cp);
  223. // BUGBUG: $brettm is this really necessary???
  224. XformSizeInHimetricToPixels(NULL, &reobj.sizel, &reobj.sizel);
  225. rc.left = rc.top = 0;
  226. rc.right = (INT) reobj.sizel.cx;
  227. rc.bottom = (INT) reobj.sizel.cy;
  228. OffsetRect(&rc, pt.x, pt.y);
  229. hr= reobj.poleobj->DoVerb(iVerb, NULL, reobj.polesite, 0, hwndRE, &rc);
  230. Cleanup:
  231. ReleaseObj(reobj.poleobj);
  232. ReleaseObj(reobj.polesite);
  233. ReleaseObj(preole);
  234. }