Example #1
0
        /// <summary>
        /// Calculate index of the location
        /// where the caret should move to after pressing "up" key.
        /// </summary>
        public static int Calc_Up(IViewInternal view)
        {
            Point    pt;
            int      newIndex;
            Document doc = view.Document;

            // get screen location of the caret
            pt = view.GetVirPosFromIndex(doc.CaretIndex);

            // calculate next location
            pt.X     = view.GetDesiredColumn();
            pt.Y    -= view.LineSpacing;
            newIndex = view.GetIndexFromVirPos(pt);
            if (newIndex < 0)
            {
                return(doc.CaretIndex);                // don't move
            }

            // In line selection mode,
            // moving caret across the line which contains the anchor position
            // should select the line and a line above.
            // To select a line above, calculate index of the char at one more line above.
            if (doc.SelectionMode == TextDataType.Line &&
                newIndex == doc.AnchorIndex &&
                view.IsLineHeadIndex(newIndex))
            {
                pt.Y -= view.LineSpacing;
                if (0 <= pt.Y)
                {
                    newIndex = view.GetIndexFromVirPos(pt);
                }
            }

            return(newIndex);
        }
Example #2
0
        /// <summary>
        /// Calculate index of the location
        /// where the caret should move to after pressing "down" key.
        /// </summary>
        public static int Calc_Down(IViewInternal view)
        {
            Point    pt;
            int      newIndex;
            Document doc = view.Document;

            // get screen location of the caret
            pt = view.GetVirPosFromIndex(doc.CaretIndex);

            // calculate next location
            pt.X  = view.GetDesiredColumn();
            pt.Y += view.LineSpacing;

            /* NOT NEEDED because View.GetIndexFromVirPos handles this case.
             * if( view.Height - view.LineSpacing < pt.Y )
             * {
             *      return doc.CaretIndex; // no lines below. don't move.
             * }*/
            newIndex = view.GetIndexFromVirPos(pt);

            // In line selection mode,
            // moving caret across the line which contains the anchor position
            // should select the line and a line below.
            // To select a line below, calculate index of the char at one more line below.
            if (doc.SelectionMode == TextDataType.Line &&
                view.IsLineHeadIndex(newIndex))
            {
                Point pt2             = new Point(pt.X, pt.Y + view.LineSpacing);
                int   skippedNewIndex = view.GetIndexFromVirPos(pt2);
                if (skippedNewIndex == doc.AnchorIndex)
                {
                    newIndex = skippedNewIndex;
                }
            }

            return(newIndex);
        }
Example #3
0
        void SetSelection_Line(int anchor, int caret, IViewInternal view)
        {
            int toLineIndex;

            // get line index of the lines where selection starts and ends
            toLineIndex = view.GetLineIndexFromCharIndex(caret);
            if (ViewParam.LineSelectionAnchor1 < 0 ||
                (anchor != ViewParam.LineSelectionAnchor1 && anchor != ViewParam.LineSelectionAnchor2))
            {
                //-- line selection anchor changed or did not exists --
                // select between head of the line and end of the line
                int fromLineIndex = view.GetLineIndexFromCharIndex(anchor);
                anchor = view.GetLineHeadIndex(fromLineIndex);
                if (fromLineIndex + 1 < view.LineCount)
                {
                    caret = view.GetLineHeadIndex(fromLineIndex + 1);
                }
                else
                {
                    caret = _Document.Length;
                }
                ViewParam.LineSelectionAnchor1 = anchor;
                ViewParam.LineSelectionAnchor2 = anchor;
            }
            else if (ViewParam.LineSelectionAnchor1 < caret)
            {
                //-- selecting to the line (or after) where selection started --
                // select between head of the starting line and the end of the destination line
                anchor = view.GetLineHeadIndexFromCharIndex(ViewParam.LineSelectionAnchor1);
                if (view.IsLineHeadIndex(caret) == false)
                {
                    toLineIndex = view.GetLineIndexFromCharIndex(caret);
                    if (toLineIndex + 1 < view.LineCount)
                    {
                        caret = view.GetLineHeadIndex(toLineIndex + 1);
                    }
                    else
                    {
                        caret = _Document.Length;
                    }
                }
            }
            else            // if( caret < LineSelectionAnchor )
            {
                //-- selecting to foregoing lines where selection started --
                // select between head of the destination line and end of the starting line
                int anchorLineIndex;

                caret           = view.GetLineHeadIndex(toLineIndex);
                anchorLineIndex = view.GetLineIndexFromCharIndex(ViewParam.LineSelectionAnchor1);
                if (anchorLineIndex + 1 < view.LineCount)
                {
                    anchor = view.GetLineHeadIndex(anchorLineIndex + 1);
                }
                else
                {
                    anchor = _Document.Length;
                }
                //DO_NOT//ViewParam.LineSelectionAnchor1 = anchor;
                ViewParam.LineSelectionAnchor2 = anchor;
            }

            // apply new selection
            SetSelection_Normal(anchor, caret);
        }