コード例 #1
0
        //=========================================================================================
        internal TextPoint GetSelectionBoundary(bool first)
        {
            var startPoint = Body.SelectionStart;
            var caretPos   = Caret.Point;

            if (first)
            {
                return(TextPoint.Min(startPoint, caretPos));
            }
            else
            {
                return(TextPoint.Max(startPoint, caretPos));
            }
        }
コード例 #2
0
        //=========================================================================================
        /// <summary>Draw selected text.</summary>
        internal void DrawLinesWithSelection(Point offset, Graphics g)
        {
            //Find start and end of selection
            TextPoint oFirst, oSecond;

            if (this.Body.Caret.Point < this.Body.SelectionStart)
            {
                oFirst  = this.Body.Caret.Point;
                oSecond = this.Body.SelectionStart;
            }
            else
            {
                oFirst  = this.Body.SelectionStart;
                oSecond = this.Body.Caret.Point;
            }

            const int I_BEFORE_SELECTION = 0;
            const int I_UNDER_SELECTION  = 1;
            const int I_AFTER_SELECTION  = 2;

            int iState;

            {
                //Determine position type of the first token
                var oToken = this.Viewer.Tokens[this.Body.FirstVisibleToken];
                if (oToken.Start <= oFirst)
                {
                    iState = I_BEFORE_SELECTION;
                }
                else if (oToken.Start >= oSecond)
                {
                    iState = I_AFTER_SELECTION;
                }
                else
                {
                    iState = I_UNDER_SELECTION;
                }
            }

            for (int i = this.Body.FirstVisibleToken; i <= this.Body.LastVisibleToken; i++)
            {
                var   oToken = this.Viewer.Tokens[i];
                Point point  = this.Body.GetXYByTextPoint(oToken.Start);
                point.Offset(offset);
                switch (iState)
                {
                case I_BEFORE_SELECTION:
                    if (oToken.End <= oFirst)
                    {
                        this.DrawText(g, oToken.Text, oToken.Start.Col, point, oToken.Style.ForeColor);
                    }
                    else
                    {
                        TextPoint oTextSelectionStart;
                        if (oToken.Start < oFirst)
                        {
                            //Unselected part of first token
                            string sText = this.Viewer.Document.GetText(oToken.Start, oFirst);
                            this.DrawText(g, sText, oToken.Start.Col, point, oToken.Style.ForeColor);
                            oTextSelectionStart = oFirst;
                        }
                        else
                        {
                            oTextSelectionStart = oToken.Start;
                        }

                        {
                            //Selected part of the token
                            string sText = this.Viewer.Document.GetText(oTextSelectionStart, TextPoint.Min(oToken.End, oSecond));
                            Point  p     = this.Body.GetXYByTextPoint(oTextSelectionStart);
                            p.Offset(offset);
                            this.DrawText(g, sText, oTextSelectionStart.Col, p, SystemColors.HighlightText);
                        }

                        if (oSecond < oToken.End)
                        {
                            //If there is unselected end of the token
                            string sText = this.Viewer.Document.GetText(oSecond, oToken.End);
                            Point  p     = this.Body.GetXYByTextPoint(oSecond);
                            p.Offset(offset);
                            this.DrawText(g, sText, oSecond.Col, p, oToken.Style.ForeColor);
                        }
                        iState = I_UNDER_SELECTION;
                    }
                    break;

                case I_UNDER_SELECTION:
                    if (oToken.End <= oSecond)
                    {
                        this.DrawText(g, oToken.Text, oToken.Start.Col, point, SystemColors.HighlightText);
                    }
                    else
                    {
                        iState = I_AFTER_SELECTION;
                        if (oToken.Start >= oSecond)
                        {
                            this.DrawText(g, oToken.Text, oToken.Start.Col, point, oToken.Style.ForeColor);
                        }
                        else
                        {
                            {
                                //draw selected part of the token
                                string sText = this.Viewer.Document.GetText(oToken.Start, TextPoint.Min(oToken.End, oSecond));
                                Point  p     = this.Body.GetXYByTextPoint(oToken.Start);
                                p.Offset(offset);
                                this.DrawText(g, sText, oToken.Start.Col, p, SystemColors.HighlightText);
                            }
                            {
                                //draw token part after selection
                                string sText = this.Viewer.Document.GetText(oSecond, oToken.End);
                                Point  p     = this.Body.GetXYByTextPoint(oSecond);
                                p.Offset(offset);
                                this.DrawText(g, sText, oSecond.Col, p, oToken.Style.ForeColor);
                            }
                        }
                    }
                    break;

                case I_AFTER_SELECTION:
                    this.DrawText(g, oToken.Text, oToken.Start.Col, point, oToken.Style.ForeColor);
                    break;
                }
            }
        }