BOOL CSampleIME::_FindComposingRange(TfEditCookie ec, _In_ ITfContext *pContext, _In_ ITfRange *pSelection, _Outptr_result_maybenull_ ITfRange **ppRange) { if (ppRange == nullptr) { return FALSE; } *ppRange = nullptr; // find GUID_PROP_COMPOSING ITfProperty* pPropComp = nullptr; IEnumTfRanges* enumComp = nullptr; /* GetProperty(GUID_PROP_COMPOSING, ...) -- nonzero if text is part of a composition */ HRESULT hr = pContext->GetProperty(GUID_PROP_COMPOSING, &pPropComp); if (FAILED(hr) || pPropComp == nullptr) { return FALSE; } /* EnumRanges: Obtains an enumeration of ranges that contain unique values of the property GUID_PROP_COMPOSING within the given range */ hr = pPropComp->EnumRanges(ec, &enumComp, pSelection); if (FAILED(hr) || enumComp == nullptr) { pPropComp->Release(); return FALSE; } BOOL isCompExist = FALSE; VARIANT var; ULONG fetched = 0; while (enumComp->Next(1, ppRange, &fetched) == S_OK && fetched == 1) { hr = pPropComp->GetValue(ec, *ppRange, &var); if (hr == S_OK) { if (var.vt == VT_I4 && var.lVal != 0) { isCompExist = TRUE; break; } } (*ppRange)->Release(); *ppRange = nullptr; } pPropComp->Release(); enumComp->Release(); return isCompExist; }
BOOL CDIME::_FindComposingRange(TfEditCookie ec, _In_ ITfContext *pContext, _In_ ITfRange *pSelection, _Outptr_result_maybenull_ ITfRange **ppRange) { debugPrint(L"CDIME::_FindComposingRange()"); if (pContext == nullptr || ppRange == nullptr) { debugPrint(L"CDIME::_FindComposingRange() failed with null pContext or pRange"); return FALSE; } *ppRange = nullptr; // find GUID_PROP_COMPOSING ITfProperty* pPropComp = nullptr; IEnumTfRanges* enumComp = nullptr; HRESULT hr = pContext->GetProperty(GUID_PROP_COMPOSING, &pPropComp); if (FAILED(hr) || pPropComp == nullptr) { if (FAILED(hr)) debugPrint(L"CDIME::_FindComposingRange() pContext->GetProperty() failed"); else debugPrint(L"CDIME::_FindComposingRange() failed with null pPropComp"); return FALSE; } hr = pPropComp->EnumRanges(ec, &enumComp, pSelection); if (FAILED(hr) || enumComp == nullptr) { if (FAILED(hr)) debugPrint(L"CDIME::_FindComposingRange() pPropComp->EnumRanges failed"); else debugPrint(L"CDIME::_FindComposingRange() failed with null enumComp"); pPropComp->Release(); return FALSE; } BOOL isCompExist = FALSE; VARIANT var; ULONG fetched = 0; while (enumComp->Next(1, ppRange, &fetched) == S_OK && fetched == 1) { debugPrint(L"CDIME::_FindComposingRange() enumComp->Next() "); hr = pPropComp->GetValue(ec, *ppRange, &var); if (hr == S_OK) { if (var.vt == VT_I4 && var.lVal != 0) { WCHAR rangeText[4096]; rangeText[0] = NULL; fetched = 0; hr = (*ppRange)->GetText(ec, 0, rangeText, 4096, &fetched); if (SUCCEEDED(hr)) { if (lastReadingString.GetLength() > 0) // The last reading string is null when composing is just started and the range should be empty. { debugPrint(L"CDIME::_FindComposingRange() text in range = %s with %d character, last reading string = %s with %d characters", rangeText, fetched, lastReadingString.Get(), lastReadingString.GetLength()); UINT cmpCount = (int)lastReadingString.GetLength(); BOOL rangeMatchReadingString = FALSE; if (fetched > 0 && fetched > cmpCount) { LONG shifted = 0; // range contains other characters than last reading string, and these characters should be skipped with shifting the start anchor (*ppRange)->ShiftStart(ec, fetched - cmpCount, &shifted, NULL); debugPrint(L"CDIME::_FindComposingRange() shift start anchor with %d characters", shifted); rangeText[0] = NULL; hr = (*ppRange)->GetText(ec, 0, rangeText, 4096, &fetched); if (SUCCEEDED(hr)) debugPrint(L"CDIME::_FindComposingRange() text in range with shited anchor = %s with %d character", rangeText, fetched); } // The range we are composing should always matched with the last reading string. rangeMatchReadingString = CompareString(GetLocale(), 0, rangeText, cmpCount, lastReadingString.Get(), cmpCount) == CSTR_EQUAL; if (rangeMatchReadingString) { isCompExist = TRUE; debugPrint(L"CDIME::_FindComposingRange() text in this range mathced with last reading string. isCompExist = TRUE"); break; } } } else { debugPrint(L"CDIME::_FindComposingRange() cannot getText from range isCompExist = TRUE"); } } } debugPrint(L"CDIME::_FindComposingRange() release *ppRange"); if(*ppRange) (*ppRange)->Release(); *ppRange = nullptr; } pPropComp->Release(); enumComp->Release(); return isCompExist; }