// /// <summary> /// try set caret x pos to nearest request value /// </summary> /// <param name="xpos"></param> public void TrySetCaretPos(int xpos, int ypos) { //-------- _textFlowLayer.NotifyHitOnSolidTextRun(null); //-------- if (_currentTextRun == null) { caret_char_index = 0; _caretXPos = 0; _rCharOffset = 0; _rPixelOffset = 0; return; } int pixDiff = xpos - _caretXPos; if (pixDiff > 0) { do { int thisTextRunPixelLength = _currentTextRun.Width; if (_rPixelOffset + thisTextRunPixelLength > xpos) { EditableRunCharLocation foundLocation = EditableRun.InnerGetCharacterFromPixelOffset(_currentTextRun, xpos - _rPixelOffset); _caretXPos = _rPixelOffset + foundLocation.pixelOffset; caret_char_index = _rCharOffset + foundLocation.RunCharIndex; //for solid text run //we can send some event to it SolidTextRun solidTextRun = _currentTextRun as SolidTextRun; if (solidTextRun != null) { _textFlowLayer.NotifyHitOnSolidTextRun(solidTextRun); } //if (foundLocation.charIndex == -1) //{ // if (!(MoveToPreviousTextRun())) // { // caretXPos = 0; // caret_char_index = 0; // } //} //else //{ // caretXPos = rPixelOffset + foundLocation.pixelOffset; // caret_char_index = rCharOffset + foundLocation.charIndex; //} return; } } while (MoveToNextTextRun()); //to the last _caretXPos = _rPixelOffset + _currentTextRun.Width; caret_char_index = _rCharOffset + _currentTextRun.CharacterCount; return; } else if (pixDiff < 0) { do { if (xpos >= _rPixelOffset) { EditableRunCharLocation foundLocation = EditableRun.InnerGetCharacterFromPixelOffset(_currentTextRun, xpos - _rPixelOffset); _caretXPos = _rPixelOffset + foundLocation.pixelOffset; caret_char_index = _rCharOffset + foundLocation.RunCharIndex; //for solid text run //we can send some event to it SolidTextRun solidTextRun = _currentTextRun as SolidTextRun; if (solidTextRun != null) { _textFlowLayer.NotifyHitOnSolidTextRun(solidTextRun); } //if (foundLocation.charIndex == -1) //{ // if (!MoveToPreviousTextRun()) // { // caret_char_index = 0; // caretXPos = 0; // } //} //else //{ // caretXPos = rPixelOffset + foundLocation.pixelOffset; // caret_char_index = rCharOffset + foundLocation.RunCharIndex; //} return; } } while (MoveToPreviousTextRun());// _caretXPos = 0; caret_char_index = 0; return; } }