示例#1
0
        /// <summary>
        /// Calculates char-index from screen line/column index.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">Specified index was out of range.</exception>
        public override int GetCharIndexFromLineColumnIndex(int lineIndex, int columnIndex)
        {
            if (lineIndex < 0 || LineCount < lineIndex)
            {
                throw new ArgumentOutOfRangeException("lineIndex", "Invalid index was given (lineIndex:" + lineIndex + ", LineCount:" + LineCount + ").");
            }
            if (columnIndex < 0)
            {
                throw new ArgumentOutOfRangeException("columnIndex", "Invalid index was given (columnIndex:" + columnIndex + ").");
            }

            return(LineLogic.GetCharIndexFromLineColumnIndex(
                       Document.InternalBuffer, PLHI, lineIndex, columnIndex
                       ));
        }
示例#2
0
        /// <summary>
        /// Gets char-index of the char at the point specified by location in the virtual space.
        /// </summary>
        /// <returns>The index of the character at specified location.</returns>
        public override int GetIndexFromVirPos(IGraphics g, Point pt)
        {
            int lineIndex, columnIndex;
            int drawableTextLen;

            // calc line index
            lineIndex = (pt.Y / LineSpacing);
            if (lineIndex < 0)
            {
                lineIndex = 0;
            }
            else if (PLHI.Count <= lineIndex &&
                     Document.LineCount != 0)
            {
                // the point indicates beyond the final line.
                // treat as if the final line was specified
                lineIndex = PLHI.Count - 1;
            }

            // calc column index
            columnIndex = 0;
            if (0 < pt.X)
            {
                int    begin, end;
                string line;
                bool   isWrapLine = false;

                // get content of the line
                LineLogic.GetLineRange(Document.InternalBuffer, PLHI, lineIndex, out begin, out end);
                line = Document.GetTextInRange(begin, end);
                if (end + 1 < Document.Length &&
                    !LineLogic.IsEolChar(Document[end]))
                {
                    isWrapLine = true;
                }

                // calc maximum length of chars in line
                int rightLimitX   = pt.X;
                int leftPartWidth = MeasureTokenEndX(g, line, 0, rightLimitX, out drawableTextLen);
                columnIndex = drawableTextLen;

                // if the location is nearer to the NEXT of that char,
                // we should return the index of next one.
                if (drawableTextLen < line.Length)
                {
                    string nextChar      = line[drawableTextLen].ToString();
                    int    nextCharWidth = MeasureTokenEndX(g, nextChar, leftPartWidth) - leftPartWidth;
                    if (leftPartWidth + nextCharWidth / 2 < pt.X)                    // == "x of middle of next char" < "x of click in virtual text area"
                    {
                        columnIndex = drawableTextLen + 1;
                    }
                }
                // if the whole line can be drawn and is a wrapped line,
                // decrease column to avoid indicating invalid position
                else if (isWrapLine)
                {
                    columnIndex--;
                }
            }

            return(LineLogic.GetCharIndexFromLineColumnIndex(Document.InternalBuffer, PLHI, lineIndex, columnIndex));
        }