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.

191 lines
4.8 KiB

  1. //
  2. // class implementation of CDictContext
  3. //
  4. // [2/15/00] created
  5. //
  6. #include "private.h"
  7. #include "globals.h"
  8. #include "dictctxt.h"
  9. //
  10. // ctor/dtor
  11. //
  12. CDictContext::CDictContext(ITfContext *pic, ITfRange *pRange)
  13. {
  14. Assert(pic);
  15. Assert(pRange);
  16. m_cpic = pic;
  17. m_cpRange = pRange;
  18. m_pszText = NULL;
  19. m_ulSel = m_ulStartIP = m_ulCchToFeed = 0;
  20. }
  21. CDictContext::~CDictContext()
  22. {
  23. if (m_pszText)
  24. {
  25. cicMemFree(m_pszText);
  26. }
  27. }
  28. //
  29. // InitializeContext
  30. //
  31. // synopsis: Get Text around an IP and setup character positions
  32. //
  33. HRESULT CDictContext::InitializeContext(TfEditCookie ecReadOnly)
  34. {
  35. CComPtr<ITfRange> cpRangeCloned;
  36. CComPtr<ITfRange> cpRangeEndSel;
  37. HRESULT hr = m_cpRange->Clone(&cpRangeEndSel);
  38. if (S_OK == hr)
  39. {
  40. // create a range to hold the position of current selection
  41. hr = cpRangeEndSel->Collapse(ecReadOnly, TF_ANCHOR_END);
  42. }
  43. if (S_OK == hr)
  44. {
  45. hr = m_cpRange->Clone(&cpRangeCloned);
  46. }
  47. if (S_OK == hr)
  48. {
  49. // we don't want to go beyond an embedded object
  50. // (this is assuming that hc is const, which it should be)
  51. TF_HALTCOND hc = {0};
  52. hc.dwFlags = TF_HF_OBJECT;
  53. ULONG ulcch = 0;
  54. hr = cpRangeCloned->Collapse(ecReadOnly, TF_ANCHOR_START);
  55. if (S_OK == hr)
  56. {
  57. TF_HALTCOND hc2 = {0};
  58. hc2.pHaltRange = cpRangeEndSel;
  59. hc2.aHaltPos = TF_ANCHOR_END;
  60. //
  61. // get the # of characters in selection
  62. //
  63. long cch = 0;
  64. hr = cpRangeCloned->ShiftEnd(ecReadOnly, CCH_FEED_POSTIP, &cch, &hc2);
  65. if (S_OK == hr)
  66. {
  67. m_ulSel = ulcch = cch;
  68. }
  69. }
  70. if (S_OK == hr)
  71. {
  72. long cch;
  73. Assert(ulcch <= CCH_FEED_POSTIP);
  74. hr = cpRangeCloned->ShiftEnd(ecReadOnly, CCH_FEED_POSTIP-ulcch, &cch, &hc);
  75. if (S_OK == hr)
  76. {
  77. ulcch += cch;
  78. }
  79. }
  80. if (S_OK == hr)
  81. {
  82. long cch;
  83. // Get the offset of IP
  84. hr = cpRangeCloned->ShiftStart(ecReadOnly, -CCH_FEED_PREIP, &cch, &hc);
  85. if (S_OK == hr)
  86. {
  87. m_ulStartIP = -cch;
  88. ulcch += -cch;
  89. }
  90. }
  91. if (S_OK == hr)
  92. {
  93. if (m_pszText)
  94. {
  95. cicMemFree(m_pszText);
  96. }
  97. // could make it smarter to alloc mem that is absolutely needed?
  98. m_pszText = (WCHAR *)cicMemAlloc((ulcch + 1)*sizeof(WCHAR));
  99. if (!m_pszText)
  100. {
  101. hr = E_OUTOFMEMORY;
  102. }
  103. else
  104. {
  105. hr = cpRangeCloned->GetText(ecReadOnly, 0, m_pszText, ulcch, &ulcch);
  106. // if we can't get text beyond the IP, it is not worth feeding this context
  107. if (S_OK != hr || ulcch < m_ulStartIP)
  108. {
  109. m_ulCchToFeed = 0;
  110. hr = E_FAIL;
  111. }
  112. else
  113. {
  114. m_ulCchToFeed = ulcch;
  115. }
  116. }
  117. }
  118. }
  119. return hr;
  120. }
  121. //
  122. // FeedContextToGrammar
  123. //
  124. // synopsis: feed this IP context to the given grammar
  125. //
  126. HRESULT CDictContext::FeedContextToGrammar(ISpRecoGrammar *pGram)
  127. {
  128. HRESULT hr = E_FAIL;
  129. Assert(pGram);
  130. SPTEXTSELECTIONINFO tsi = {0};
  131. tsi.ulStartActiveOffset = 0;
  132. tsi.cchActiveChars = m_ulCchToFeed;
  133. tsi.ulStartSelection = m_ulStartIP;
  134. tsi.cchSelection = m_ulSel;
  135. WCHAR *pMemText = (WCHAR *)cicMemAlloc((m_ulCchToFeed+2)*sizeof(WCHAR));
  136. if (pMemText)
  137. {
  138. if (m_ulCchToFeed > 0 && m_pszText)
  139. wcsncpy(pMemText, m_pszText, m_ulCchToFeed);
  140. pMemText[m_ulCchToFeed] = L'\0';
  141. pMemText[m_ulCchToFeed+1] = L'\0';
  142. #ifdef DEBUG
  143. {
  144. TraceMsg(TF_GENERAL, "For SetWordSequenceData: Text=\"%S\" cchActiveChars=%d tsi.ulStartSelection=%d, cchSelection=%d",pMemText,tsi.cchActiveChars, tsi.ulStartSelection, tsi.cchSelection);
  145. }
  146. #endif
  147. hr = pGram->SetWordSequenceData(pMemText, m_ulCchToFeed + 2, &tsi);
  148. /* According to billro, the below code is not necessary.
  149. #ifdef DEBUG
  150. {
  151. TraceMsg(TF_GENERAL, "For SetTextSelection: tsi.ulStartSelection = %d",tsi.ulStartSelection);
  152. }
  153. #endif
  154. // so Fil told me we need to call SetTextSelection again
  155. if (S_OK == hr)
  156. hr = pGram->SetTextSelection(&tsi);
  157. */
  158. cicMemFree(pMemText);
  159. }
  160. return hr;
  161. }