Source code of Windows XP (NT5)
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.

343 lines
7.9 KiB

  1. // HIGHLITE.CPP : implementation file
  2. //
  3. // by DougO
  4. //
  5. #include "header.h"
  6. #include "stdio.h"
  7. #include "string.h"
  8. #include "TCHAR.h"
  9. //#include "cprint.h"
  10. #include "secwin.h"
  11. #include "contain.h"
  12. #include "collect.h"
  13. #include "hhtypes.h"
  14. #include "toc.h"
  15. #include "fts.h"
  16. #include "highlite.h"
  17. #include "hhctrl.h"
  18. #define HIGHLIGHT_TIMEOUT 6000
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CSearchHighlight HighlightTerm
  21. //
  22. BOOL CSearchHighlight::HighlightTerm(IHTMLDocument2* pHTMLDocument2, WCHAR *pTerm)
  23. {
  24. HRESULT hr;
  25. if(!pTerm || !*pTerm)
  26. return FALSE;
  27. BSTR pSearchTerm = SysAllocString(pTerm);
  28. IHTMLBodyElement* pBodyElement;
  29. IHTMLElement *pElement;
  30. // get the document element from the document
  31. //
  32. if(FAILED(hr = pHTMLDocument2->get_body(&pElement)))
  33. return FALSE;
  34. if(FAILED(hr = pElement->QueryInterface(IID_IHTMLBodyElement, (void **)&pBodyElement)))
  35. {
  36. pElement->Release();
  37. return FALSE;
  38. }
  39. IHTMLTxtRange *pBeginRange = NULL;
  40. // Create the initial text range
  41. //
  42. if(FAILED(hr = pBodyElement->createTextRange(&pBeginRange)))
  43. {
  44. pBodyElement->Release();
  45. pElement->Release();
  46. return FALSE;
  47. }
  48. VARIANT_BOOL vbRet = VARIANT_TRUE;
  49. VARIANT_BOOL bUI = VARIANT_FALSE;
  50. VARIANT vBackColor, vForeColor;
  51. BSTR bstrCmd = SysAllocString(L"BackColor");
  52. BSTR bstrCmd2 = SysAllocString(L"ForeColor");
  53. vBackColor.vt = vForeColor.vt = VT_I4;
  54. vForeColor.lVal = GetSysColor(COLOR_HIGHLIGHTTEXT);
  55. vBackColor.lVal = GetSysColor(COLOR_HIGHLIGHT);
  56. long lret = 0;
  57. DWORD dwRet = TRUE;
  58. DWORD dwStartTickCount = GetTickCount();
  59. while(vbRet == VARIANT_TRUE)
  60. {
  61. if(FAILED(hr = pBeginRange->findText(pSearchTerm,1000000,2,&vbRet)))
  62. break;
  63. if(vbRet == VARIANT_TRUE)
  64. {
  65. if(FAILED(hr = pBeginRange->execCommand(bstrCmd2, VARIANT_FALSE, vForeColor, &bUI)))
  66. {
  67. dwRet = FALSE;
  68. break;
  69. }
  70. if(GetTickCount() > (dwStartTickCount + HIGHLIGHT_TIMEOUT))
  71. {
  72. dwRet = FALSE;
  73. break;
  74. }
  75. if(FAILED(hr = pBeginRange->execCommand(bstrCmd, VARIANT_FALSE, vBackColor, &bUI)))
  76. {
  77. dwRet = FALSE;
  78. break;
  79. }
  80. if(GetTickCount() > (dwStartTickCount + HIGHLIGHT_TIMEOUT))
  81. {
  82. dwRet = FALSE;
  83. break;
  84. }
  85. if(FAILED(hr = pBeginRange->collapse(FALSE)))
  86. {
  87. dwRet = FALSE;
  88. break;
  89. }
  90. if(GetTickCount() > (dwStartTickCount + HIGHLIGHT_TIMEOUT))
  91. {
  92. dwRet = FALSE;
  93. break;
  94. }
  95. }
  96. }
  97. SysFreeString(bstrCmd);
  98. SysFreeString(bstrCmd2);
  99. SysFreeString(pSearchTerm);
  100. pBodyElement->Release();
  101. pElement->Release();
  102. pBeginRange->Release();
  103. return TRUE;
  104. }
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CSearchHighlight
  107. //
  108. CSearchHighlight::CSearchHighlight(CExCollection *pTitleCollection)
  109. {
  110. m_pTitleCollection = pTitleCollection;
  111. m_iJumpIndex = 0;
  112. m_pTermList = NULL;
  113. m_bHighlightEnabled = !g_fIE3;
  114. m_Initialized = TRUE;
  115. }
  116. /////////////////////////////////////////////////////////////////////////////
  117. // CSearchHighlight constructor
  118. //
  119. CSearchHighlight::~CSearchHighlight()
  120. {
  121. }
  122. /////////////////////////////////////////////////////////////////////////////
  123. // CSearchHighlight HighlightDocument
  124. //
  125. int CSearchHighlight::HighlightDocument(LPDISPATCH lpDispatch)
  126. {
  127. CHourGlass wait;
  128. if(!m_bHighlightEnabled)
  129. return FALSE;
  130. if ( lpDispatch != NULL )
  131. {
  132. // request the "document" object from the object
  133. //
  134. IHTMLDocument2* pHTMLDocument2;
  135. // If this fails, then we are probably running on IE3
  136. //
  137. if(FAILED(lpDispatch->QueryInterface(IID_IHTMLDocument2, (void **)&pHTMLDocument2)))
  138. return FALSE;
  139. int i, cTerms = m_pTitleCollection->m_pFullTextSearch->GetHLTermCount();
  140. DWORD dwStartTickCount = GetTickCount();
  141. for(i=0;i<cTerms;++i)
  142. {
  143. if(!HighlightTerm(pHTMLDocument2,m_pTitleCollection->m_pFullTextSearch->GetHLTermAt(i)))
  144. {
  145. pHTMLDocument2->Release();
  146. return TRUE;
  147. }
  148. if(GetTickCount() > (dwStartTickCount + HIGHLIGHT_TIMEOUT))
  149. break;
  150. }
  151. pHTMLDocument2->Release();
  152. }
  153. return TRUE;
  154. }
  155. /////////////////////////////////////////////////////////////////////////////
  156. // CSearchHighlight enable highlight
  157. //
  158. // bStatus:
  159. // TRUE = Highlighting Enabled
  160. // FALSE = Highlighting Disabled
  161. //
  162. void CSearchHighlight::EnableHighlight(BOOL bStatus)
  163. {
  164. m_bHighlightEnabled = bStatus;
  165. }
  166. /////////////////////////////////////////////////////////////////////////////
  167. // CSearchHighlight jump to first highlight term
  168. //
  169. HRESULT CSearchHighlight::JumpFirst(void)
  170. {
  171. // DOUG TODO
  172. return S_OK;
  173. }
  174. /////////////////////////////////////////////////////////////////////////////
  175. // CSearchHighlight jump to next highlight term
  176. //
  177. HRESULT CSearchHighlight::JumpNext(void)
  178. {
  179. // DOUG TODO
  180. return S_OK;
  181. }
  182. // DoF1Lookup
  183. //
  184. // Get current selection in mshtml and perform a F1 lookup
  185. //
  186. void DoF1Lookup(IWebBrowserAppImpl* pWBApp)
  187. {
  188. LPDISPATCH lpDispatch;
  189. if ( pWBApp && (lpDispatch = pWBApp->GetDocument()) )
  190. {
  191. WCHAR *pText = GetSelectionText(lpDispatch);
  192. if(pText)
  193. {
  194. char szTerm[512] = "";
  195. WideCharToMultiByte(CP_ACP, 0, pText, -1, szTerm, sizeof(szTerm), NULL, NULL);
  196. SysFreeString(pText);
  197. //
  198. // <mc> Find a CExCollection pointer...
  199. //
  200. CExCollection* pExCollection = NULL;
  201. CStr cstr;
  202. pWBApp->GetLocationURL(&cstr);
  203. pExCollection = GetCurrentCollection(NULL, (PCSTR)cstr);
  204. OnWordWheelLookup(szTerm, pExCollection);
  205. }
  206. lpDispatch->Release();
  207. }
  208. }
  209. WCHAR * GetSelectionText(LPDISPATCH lpDispatch)
  210. {
  211. if(lpDispatch)
  212. {
  213. HRESULT hr;
  214. IHTMLSelectionObject *pSelection;
  215. // request the "document" object from the object
  216. //
  217. IHTMLDocument2* pHTMLDocument2;
  218. // If this fails, then we are probably running on IE3
  219. //
  220. if(FAILED(lpDispatch->QueryInterface(IID_IHTMLDocument2, (void **)&pHTMLDocument2)))
  221. return NULL;
  222. if(FAILED(hr = pHTMLDocument2->get_selection(&pSelection)))
  223. {
  224. pHTMLDocument2->Release();
  225. return NULL;
  226. }
  227. IHTMLTxtRange *pBeginRange = NULL;
  228. LPDISPATCH lpRangeDispatch;
  229. BSTR bstr = NULL;
  230. if ( (hr = pSelection->get_type( &bstr )) != S_OK )
  231. {
  232. pHTMLDocument2->Release();
  233. pSelection->Release();
  234. return NULL;
  235. }
  236. if( (bstr == NULL) || wcscmp(bstr,L"Text") )
  237. {
  238. pHTMLDocument2->Release();
  239. pSelection->Release();
  240. SysFreeString( bstr );
  241. return NULL;
  242. }
  243. SysFreeString( bstr );
  244. // Create the initial text range
  245. //
  246. if(FAILED(hr = pSelection->createRange(&lpRangeDispatch)))
  247. {
  248. pSelection->Release();
  249. pHTMLDocument2->Release();
  250. return NULL;
  251. }
  252. if(FAILED(lpRangeDispatch->QueryInterface(IID_IHTMLTxtRange, (void **)&pBeginRange)))
  253. {
  254. pSelection->Release();
  255. pHTMLDocument2->Release();
  256. return NULL;
  257. }
  258. BSTR pSelectedText;
  259. pBeginRange->get_text(&pSelectedText);
  260. pSelection->Release();
  261. pHTMLDocument2->Release();
  262. pBeginRange->Release();
  263. if(!pSelectedText)
  264. return NULL;
  265. if(*pSelectedText == NULL)
  266. {
  267. SysFreeString(pSelectedText);
  268. return NULL;
  269. }
  270. return pSelectedText;
  271. }
  272. else
  273. return NULL;
  274. }