Exemplo n.º 1
0
        void OnMouseDown(object sender, MouseEventArgs e)
        {
            Point mousepos;

            textArea.mousepos = e.Location;
            mousepos          = e.Location;

            if (dodragdrop)
            {
                return;
            }

            if (doubleclick)
            {
                doubleclick = false;
                return;
            }

            if (textArea.TextView.DrawingPosition.Contains(mousepos.X, mousepos.Y))
            {
                gotmousedown = true;
                textArea.SelectionManager.selectFrom.where = WhereFrom.TArea;
                button = e.Button;

                // double-click
                if (button == MouseButtons.Left && e.Clicks == 2)
                {
                    int deltaX = Math.Abs(lastmousedownpos.X - e.X);
                    int deltaY = Math.Abs(lastmousedownpos.Y - e.Y);
                    if (deltaX <= SystemInformation.DoubleClickSize.Width &&
                        deltaY <= SystemInformation.DoubleClickSize.Height)
                    {
                        DoubleClickSelectionExtend();
                        lastmousedownpos = new Point(e.X, e.Y);

                        if (textArea.SelectionManager.selectFrom.where == WhereFrom.Gutter)
                        {
                            if (!minSelection.IsEmpty && !maxSelection.IsEmpty && textArea.SelectionManager.SelectionCollection.Count > 0)
                            {
                                textArea.SelectionManager.SelectionCollection[0].StartPosition = minSelection;
                                textArea.SelectionManager.SelectionCollection[0].EndPosition   = maxSelection;
                                textArea.SelectionManager.SelectionStart = minSelection;

                                minSelection = TextLocation.Empty;
                                maxSelection = TextLocation.Empty;
                            }
                        }
                        return;
                    }
                }
                minSelection = TextLocation.Empty;
                maxSelection = TextLocation.Empty;

                lastmousedownpos = mousedownpos = new Point(e.X, e.Y);

                if (button == MouseButtons.Left)
                {
                    FoldMarker marker = textArea.TextView.GetFoldMarkerFromPosition(mousepos.X - textArea.TextView.DrawingPosition.X,
                                                                                    mousepos.Y - textArea.TextView.DrawingPosition.Y);
                    if (marker != null && marker.IsFolded)
                    {
                        if (textArea.SelectionManager.HasSomethingSelected)
                        {
                            clickedOnSelectedText = true;
                        }

                        textArea.SelectionManager.SetSelection(new DefaultSelection(textArea.TextView.Document, new TextLocation(marker.StartColumn, marker.StartLine), new TextLocation(marker.EndColumn, marker.EndLine)));
                        textArea.Focus();
                        return;
                    }

                    if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
                    {
                        ExtendSelectionToMouse();
                    }
                    else
                    {
                        TextLocation realmousepos = textArea.TextView.GetLogicalPosition(mousepos.X - textArea.TextView.DrawingPosition.X, mousepos.Y - textArea.TextView.DrawingPosition.Y);
                        clickedOnSelectedText = false;

                        int offset = textArea.Document.PositionToOffset(realmousepos);

                        if (textArea.SelectionManager.HasSomethingSelected &&
                            textArea.SelectionManager.IsSelected(offset))
                        {
                            clickedOnSelectedText = true;
                        }
                        else
                        {
                            textArea.SelectionManager.ClearSelection();
                            if (mousepos.Y > 0 && mousepos.Y < textArea.TextView.DrawingPosition.Height)
                            {
                                TextLocation pos = new TextLocation();
                                pos.Y = Math.Min(textArea.Document.TotalNumberOfLines - 1, realmousepos.Y);
                                pos.X = realmousepos.X;
                                textArea.Caret.Position = pos;
                                textArea.SetDesiredColumn();
                            }
                        }
                    }
                }
                else if (button == MouseButtons.Right)
                {
                    // Rightclick sets the cursor to the click position unless
                    // the previous selection was clicked
                    TextLocation realmousepos = textArea.TextView.GetLogicalPosition(mousepos.X - textArea.TextView.DrawingPosition.X, mousepos.Y - textArea.TextView.DrawingPosition.Y);
                    int          offset       = textArea.Document.PositionToOffset(realmousepos);
                    if (!textArea.SelectionManager.HasSomethingSelected ||
                        !textArea.SelectionManager.IsSelected(offset))
                    {
                        textArea.SelectionManager.ClearSelection();
                        if (mousepos.Y > 0 && mousepos.Y < textArea.TextView.DrawingPosition.Height)
                        {
                            TextLocation pos = new TextLocation();
                            pos.Y = Math.Min(textArea.Document.TotalNumberOfLines - 1, realmousepos.Y);
                            pos.X = realmousepos.X;
                            textArea.Caret.Position = pos;
                            textArea.SetDesiredColumn();
                        }
                    }
                }
            }
            textArea.Focus();
        }
Exemplo n.º 2
0
        void ExtendSelectionToMouse()
        {
            Point mousepos;

            mousepos = textArea.mousepos;
            TextLocation realmousepos = textArea.TextView.GetLogicalPosition(
                Math.Max(0, mousepos.X - textArea.TextView.DrawingPosition.X),
                mousepos.Y - textArea.TextView.DrawingPosition.Y);
            int y = realmousepos.Y;

            realmousepos = textArea.Caret.ValidatePosition(realmousepos);
            TextLocation oldPos = textArea.Caret.Position;

            if (oldPos == realmousepos && textArea.SelectionManager.selectFrom.where != WhereFrom.Gutter)
            {
                return;
            }

            // the selection is from the gutter
            if (textArea.SelectionManager.selectFrom.where == WhereFrom.Gutter)
            {
                if (realmousepos.Y < textArea.SelectionManager.SelectionStart.Y)
                {
                    // the selection has moved above the startpoint
                    textArea.Caret.Position = new TextLocation(0, realmousepos.Y);
                }
                else
                {
                    // the selection has moved below the startpoint
                    textArea.Caret.Position = textArea.SelectionManager.NextValidPosition(realmousepos.Y);
                }
            }
            else
            {
                textArea.Caret.Position = realmousepos;
            }

            // moves selection across whole words for double-click initiated selection
            if (!minSelection.IsEmpty && textArea.SelectionManager.SelectionCollection.Count > 0 && textArea.SelectionManager.selectFrom.where == WhereFrom.TArea)
            {
                // Extend selection when selection was started with double-click
                ISelection   selection = textArea.SelectionManager.SelectionCollection[0];
                TextLocation min       = textArea.SelectionManager.GreaterEqPos(minSelection, maxSelection) ? maxSelection : minSelection;
                TextLocation max       = textArea.SelectionManager.GreaterEqPos(minSelection, maxSelection) ? minSelection : maxSelection;
                if (textArea.SelectionManager.GreaterEqPos(max, realmousepos) && textArea.SelectionManager.GreaterEqPos(realmousepos, min))
                {
                    textArea.SelectionManager.SetSelection(min, max);
                }
                else if (textArea.SelectionManager.GreaterEqPos(max, realmousepos))
                {
                    int moff = textArea.Document.PositionToOffset(realmousepos);
                    min = textArea.Document.OffsetToPosition(FindWordStart(textArea.Document, moff));
                    textArea.SelectionManager.SetSelection(min, max);
                }
                else
                {
                    int moff = textArea.Document.PositionToOffset(realmousepos);
                    max = textArea.Document.OffsetToPosition(FindWordEnd(textArea.Document, moff));
                    textArea.SelectionManager.SetSelection(min, max);
                }
            }
            else
            {
                textArea.SelectionManager.ExtendSelection(oldPos, textArea.Caret.Position);
            }
            textArea.SetDesiredColumn();
        }