Beispiel #1
0
        public VisualPointInfo GetTextPointInfoFromCaretPoint(int caretX)
        {
            int         accTextRunWidth = 0; int accTextRunCharCount = 0;
            EditableRun lastestTextRun = null;

            foreach (EditableRun t in _runs)
            {
                lastestTextRun = t;
                int thisTextRunWidth = t.Width;
                if (accTextRunWidth + thisTextRunWidth > caretX)
                {
                    EditableRunCharLocation localPointInfo = t.GetCharacterFromPixelOffset(caretX - thisTextRunWidth);
                    EditableVisualPointInfo pointInfo      =
                        new EditableVisualPointInfo(this, accTextRunCharCount + localPointInfo.RunCharIndex);
                    pointInfo.SetAdditionVisualInfo(t, accTextRunCharCount, caretX, accTextRunWidth);
                    return(pointInfo);
                }
                else
                {
                    accTextRunWidth     += thisTextRunWidth;
                    accTextRunCharCount += t.CharacterCount;
                }
            }
            if (lastestTextRun != null)
            {
                return(null);
            }
            else
            {
                EditableVisualPointInfo pInfo = new EditableVisualPointInfo(this, -1);
                pInfo.SetAdditionVisualInfo(null, accTextRunCharCount, caretX, accTextRunWidth);
                return(pInfo);
            }
        }
Beispiel #2
0
        //
        /// <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;
            }
        }