public VisualPointInfo GetTextPointInfoFromCaretPoint(int caretX) { int accTextRunWidth = 0; int accTextRunCharCount = 0; Run lastestTextRun = null; foreach (Run t in _runs) { lastestTextRun = t; int thisTextRunWidth = t.Width; if (accTextRunWidth + thisTextRunWidth > caretX) { CharLocation localPointInfo = t.GetCharacterFromPixelOffset(caretX - thisTextRunWidth); var pointInfo = new EditableVisualPointInfo(this, accTextRunCharCount + localPointInfo.RunCharIndex, t); pointInfo.SetAdditionVisualInfo(accTextRunCharCount, caretX, accTextRunWidth); return(pointInfo); } else { accTextRunWidth += thisTextRunWidth; accTextRunCharCount += t.CharacterCount; } } if (lastestTextRun != null) { return(null); } else { EditableVisualPointInfo pInfo = new EditableVisualPointInfo(this, -1, null); pInfo.SetAdditionVisualInfo(accTextRunCharCount, caretX, accTextRunWidth); return(pInfo); } }
// /// <summary> /// try set caret x pos to nearest request value /// </summary> /// <param name="xpos"></param> public void TrySetCaretPos(int xpos, int ypos) { //-------- _textFlowLayer.LatestHitRun = 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) { CharLocation foundLocation = Run.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 _textFlowLayer.LatestHitRun = _currentTextRun; //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) { CharLocation foundLocation = Run.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 _textFlowLayer.LatestHitRun = _currentTextRun; //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; } }