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.

279 lines
7.6 KiB

  1. /*
  2. * m s h t u t i l . c p p
  3. *
  4. * Purpose:
  5. * MSHTML utilities
  6. *
  7. * History
  8. *
  9. * Copyright (C) Microsoft Corp. 1995, 1996.
  10. */
  11. #include <pch.hxx>
  12. #include <imm.h>
  13. #include "dllmain.h"
  14. #include "urlmon.h"
  15. #include "wininet.h"
  16. #include "mshtml.h"
  17. #include "mshtmcid.h"
  18. #include "mshtmhst.h"
  19. #include "demand.h"
  20. #include <shlwapi.h>
  21. #include <BadStrFunctions.h>
  22. ASSERTDATA
  23. /*
  24. * t y p e d e f s
  25. */
  26. /*
  27. * m a c r o s
  28. */
  29. /*
  30. * c o n s t a n t s
  31. */
  32. /*
  33. * g l o b a l s
  34. */
  35. /*
  36. * p r o t o t y p e s
  37. */
  38. HRESULT HrCheckTridentMenu(IUnknown *pUnkTrident, ULONG uMenu, ULONG idmFirst, ULONG idmLast, HMENU hMenu)
  39. {
  40. HRESULT hr = S_OK;
  41. if (pUnkTrident == NULL)
  42. return E_INVALIDARG;
  43. switch (uMenu)
  44. {
  45. case TM_TAGMENU:
  46. {
  47. IOleCommandTarget *pCmdTarget = NULL;
  48. VARIANTARG va;
  49. // uncheck stuff incase of failure
  50. CheckMenuRadioItem(hMenu, idmFirst, idmLast, 0, MF_BYCOMMAND|MF_UNCHECKED);
  51. hr = pUnkTrident->QueryInterface(IID_IOleCommandTarget, (LPVOID *)&pCmdTarget);
  52. if (FAILED(hr))
  53. break;
  54. va.vt = VT_BSTR;
  55. va.bstrVal = NULL;
  56. if(SUCCEEDED(pCmdTarget->Exec(&CMDSETID_Forms3, IDM_BLOCKFMT, MSOCMDEXECOPT_DONTPROMPTUSER, NULL, &va)))
  57. {
  58. TCHAR szBufMenu[MAX_PATH];
  59. TCHAR szBufTag[MAX_PATH];
  60. ULONG cmenus = (ULONG)GetMenuItemCount(hMenu);
  61. *szBufTag = 0;
  62. *szBufMenu = 0;
  63. if (va.vt == VT_BSTR && va.bstrVal)
  64. {
  65. WideCharToMultiByte(CP_ACP, 0, (WCHAR*)va.bstrVal, -1, szBufTag, MAX_PATH, NULL, NULL);
  66. szBufTag[MAX_PATH - 1] = 0;
  67. SysFreeString(va.bstrVal);
  68. }
  69. // find the right one in the tag menu to put a radio button.
  70. for(ULONG i = idmFirst; i < cmenus+idmFirst; i++)
  71. {
  72. GetMenuString(hMenu, i, szBufMenu, MAX_PATH, MF_BYCOMMAND);
  73. if(0 == StrCmpI(szBufMenu, szBufTag))
  74. {
  75. CheckMenuRadioItem(hMenu, idmFirst, idmLast, i, MF_BYCOMMAND);
  76. break;
  77. }
  78. }
  79. }
  80. pCmdTarget->Release();
  81. break;
  82. }
  83. default:
  84. hr = E_FAIL;
  85. }
  86. return hr;
  87. }
  88. HRESULT HrCreateTridentMenu(IUnknown *pUnkTrident, ULONG uMenu, ULONG idmFirst, ULONG cMaxItems, HMENU *phMenu)
  89. {
  90. HRESULT hr = NOERROR;
  91. VARIANTARG va;
  92. LONG lLBound,
  93. lUBound,
  94. lIndex;
  95. BSTR bstr;
  96. TCHAR szBuf[CCHMAX_STRINGRES];
  97. SAFEARRAY *psa;
  98. ULONG idmTag=idmFirst;
  99. HMENU hmenu;
  100. IOleCommandTarget *pCmdTarget;
  101. if (phMenu == NULL || pUnkTrident == NULL)
  102. return E_INVALIDARG;
  103. hmenu = CreatePopupMenu();
  104. if (hmenu == NULL)
  105. return E_OUTOFMEMORY;
  106. switch (uMenu)
  107. {
  108. case TM_TAGMENU:
  109. hr = pUnkTrident->QueryInterface(IID_IOleCommandTarget, (LPVOID *)&pCmdTarget);
  110. if (SUCCEEDED(hr))
  111. {
  112. va.vt = VT_ARRAY;
  113. va.parray = NULL;
  114. hr = pCmdTarget->Exec(&CMDSETID_Forms3, IDM_GETBLOCKFMTS, MSOCMDEXECOPT_DONTPROMPTUSER, NULL, &va);
  115. if (SUCCEEDED(hr))
  116. {
  117. psa = V_ARRAY(&va);
  118. SafeArrayGetLBound(psa, 1, &lLBound);
  119. SafeArrayGetUBound(psa, 1, &lUBound);
  120. for (lIndex=lLBound; lIndex<=lUBound; lIndex++)
  121. {
  122. if (idmTag == idmFirst + cMaxItems) // don't append to many
  123. break;
  124. if (SafeArrayGetElement(psa, &lIndex, &bstr)==S_OK)
  125. {
  126. if(WideCharToMultiByte(CP_ACP, 0, bstr, -1, szBuf, CCHMAX_STRINGRES, NULL, NULL))
  127. AppendMenu(hmenu, MF_STRING | MF_BYCOMMAND, idmTag++, (LPCTSTR)szBuf);
  128. szBuf[CCHMAX_STRINGRES - 1] = 0;
  129. SysFreeString(bstr);
  130. }
  131. }
  132. SafeArrayDestroy(psa);
  133. }
  134. pCmdTarget->Release();
  135. }
  136. break;
  137. default:
  138. hr = E_FAIL;
  139. }
  140. if (hr==S_OK)
  141. *phMenu = hmenu;
  142. return hr;
  143. }
  144. HRESULT HrGetElementImpl(IHTMLDocument2 *pDoc, LPCTSTR pszName, IHTMLElement **ppElem)
  145. {
  146. HRESULT hr = E_FAIL;
  147. IHTMLElementCollection *pCollect = NULL;
  148. IDispatch *pDisp = NULL;
  149. VARIANTARG va1, va2;
  150. if (pDoc)
  151. {
  152. pDoc->get_all(&pCollect);
  153. if (pCollect)
  154. {
  155. if (SUCCEEDED(HrLPSZToBSTR(pszName, &va1.bstrVal)))
  156. {
  157. va1.vt = VT_BSTR;
  158. va2.vt = VT_EMPTY;
  159. pCollect->item(va1, va2, &pDisp);
  160. if (pDisp)
  161. {
  162. hr = pDisp->QueryInterface(IID_IHTMLElement, (LPVOID*)ppElem);
  163. pDisp->Release();
  164. }
  165. SysFreeString(va1.bstrVal);
  166. }
  167. pCollect->Release();
  168. }
  169. }
  170. return hr;
  171. }
  172. HRESULT HrGetBodyElement(IHTMLDocument2 *pDoc, IHTMLBodyElement **ppBody)
  173. {
  174. HRESULT hr=E_FAIL;
  175. IHTMLElement *pElem=0;
  176. if (ppBody == NULL)
  177. return E_INVALIDARG;
  178. *ppBody = 0;
  179. if (pDoc)
  180. {
  181. pDoc->get_body(&pElem);
  182. if (pElem)
  183. {
  184. hr = pElem->QueryInterface(IID_IHTMLBodyElement, (LPVOID *)ppBody);
  185. pElem->Release();
  186. }
  187. }
  188. return hr;
  189. }
  190. HRESULT HrSetDirtyFlagImpl(LPUNKNOWN pUnkTrident, BOOL fDirty)
  191. {
  192. VARIANTARG va;
  193. IOleCommandTarget *pCmdTarget;
  194. if (!pUnkTrident)
  195. return E_INVALIDARG;
  196. if (pUnkTrident->QueryInterface(IID_IOleCommandTarget, (LPVOID*)&pCmdTarget)==S_OK)
  197. {
  198. va.vt = VT_BOOL;
  199. va.boolVal = fDirty?VARIANT_TRUE:VARIANT_FALSE;
  200. pCmdTarget->Exec(&CMDSETID_Forms3, IDM_SETDIRTY, MSOCMDEXECOPT_DONTPROMPTUSER, &va, NULL);
  201. pCmdTarget->Release();
  202. }
  203. return S_OK;
  204. }
  205. HRESULT HrGetStyleSheet(IHTMLDocument2 *pDoc, IHTMLStyleSheet **ppStyle)
  206. {
  207. IHTMLStyleSheetsCollection *pStyleSheets;
  208. IHTMLStyleSheet *pStyle;
  209. VARIANTARG va1, va2;
  210. HRESULT hr=E_FAIL;
  211. if (pDoc == NULL || ppStyle == NULL)
  212. return E_INVALIDARG;
  213. *ppStyle=NULL;
  214. if (pDoc->get_styleSheets(&pStyleSheets)==S_OK)
  215. {
  216. va1.vt = VT_I4;
  217. va1.lVal = (LONG)0;
  218. va2.vt = VT_EMPTY;
  219. pStyleSheets->item(&va1, &va2);
  220. if (va2.vt == VT_DISPATCH && va2.pdispVal)
  221. hr = va2.pdispVal->QueryInterface(IID_IHTMLStyleSheet, (LPVOID *)ppStyle);
  222. pStyleSheets->Release();
  223. }
  224. // not found, let's create a new one
  225. if (*ppStyle==NULL)
  226. hr = pDoc->createStyleSheet(NULL, -1, ppStyle);
  227. return hr;
  228. }