Пример #1
0
        /// <inheritdoc/>
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            if (!e.Handled && TextView != null && textArea != null)
            {
                e.Handled = true;
                textArea.Focus();

                SimpleSegment currentSeg = GetTextLineSegment(e);
                if (currentSeg == SimpleSegment.Invalid)
                {
                    return;
                }
                textArea.Caret.Offset = currentSeg.Offset + currentSeg.Length;
                if (CaptureMouse())
                {
                    selecting      = true;
                    selectionStart = new AnchorSegment(Document, currentSeg.Offset, currentSeg.Length);
                    if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
                    {
                        SimpleSelection simpleSelection = textArea.Selection as SimpleSelection;
                        if (simpleSelection != null)
                        {
                            selectionStart = new AnchorSegment(Document, simpleSelection.SurroundingSegment);
                        }
                    }
                    textArea.Selection = Selection.Create(textArea, selectionStart);
                    if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
                    {
                        ExtendSelection(currentSeg);
                    }
                    textArea.Caret.BringCaretToView(5.0);
                }
            }
        }
        void textArea_Drop(object sender, DragEventArgs e)
        {
            try {
                DragDropEffects effect = GetEffect(e);
                e.Effects = effect;
                if (effect != DragDropEffects.None)
                {
                    int start = textArea.Caret.Offset;
                    if (mode == MouseSelectionMode.Drag && textArea.Selection.Contains(start))
                    {
                        Debug.WriteLine("Drop: did not drop: drop target is inside selection");
                        e.Effects = DragDropEffects.None;
                    }
                    else
                    {
                        Debug.WriteLine("Drop: insert at " + start);

                        var pastingEventArgs = new DataObjectPastingEventArgs(e.Data, true, DataFormats.UnicodeText);
                        textArea.RaiseEvent(pastingEventArgs);
                        if (pastingEventArgs.CommandCancelled)
                        {
                            return;
                        }

                        string text = EditingCommandHandler.GetTextToPaste(pastingEventArgs, textArea);
                        if (text == null)
                        {
                            return;
                        }
                        bool rectangular = pastingEventArgs.DataObject.GetDataPresent(RectangleSelection.RectangularSelectionDataType);

                        // Mark the undo group with the currentDragDescriptor, if the drag
                        // is originating from the same control. This allows combining
                        // the undo groups when text is moved.
                        textArea.Document.UndoStack.StartUndoGroup(this.currentDragDescriptor);
                        try {
                            if (rectangular && RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Position, text, true))
                            {
                            }
                            else
                            {
                                textArea.Document.Insert(start, text);
                                textArea.Selection = Selection.Create(textArea, start, start + text.Length);
                            }
                        } finally {
                            textArea.Document.UndoStack.EndUndoGroup();
                        }
                    }
                    e.Handled = true;
                }
            } catch (Exception ex) {
                OnDragException(ex);
            }
        }
Пример #3
0
 void ExtendSelection(SimpleSegment currentSeg)
 {
     if (currentSeg.Offset < selectionStart.Offset)
     {
         textArea.Caret.Offset = currentSeg.Offset;
         textArea.Selection    = Selection.Create(textArea, currentSeg.Offset, selectionStart.Offset + selectionStart.Length);
     }
     else
     {
         textArea.Caret.Offset = currentSeg.Offset + currentSeg.Length;
         textArea.Selection    = Selection.Create(textArea, selectionStart.Offset, currentSeg.Offset + currentSeg.Length);
     }
 }
        void ExtendSelectionToMouse(MouseEventArgs e)
        {
            TextViewPosition oldPosition = textArea.Caret.Position;

            if (mode == MouseSelectionMode.Normal || mode == MouseSelectionMode.Rectangular)
            {
                SetCaretOffsetToMousePosition(e);
                if (mode == MouseSelectionMode.Normal && textArea.Selection is RectangleSelection)
                {
                    textArea.Selection = new SimpleSelection(textArea, oldPosition, textArea.Caret.Position);
                }
                else if (mode == MouseSelectionMode.Rectangular && !(textArea.Selection is RectangleSelection))
                {
                    textArea.Selection = new RectangleSelection(textArea, oldPosition, textArea.Caret.Position);
                }
                else
                {
                    textArea.Selection = textArea.Selection.StartSelectionOrSetEndpoint(oldPosition, textArea.Caret.Position);
                }
            }
            else if (mode == MouseSelectionMode.WholeWord || mode == MouseSelectionMode.WholeLine)
            {
                var newWord = (mode == MouseSelectionMode.WholeLine) ? GetLineAtMousePosition(e) : GetWordAtMousePosition(e);
                if (newWord != SimpleSegment.Invalid)
                {
                    textArea.Selection = Selection.Create(textArea,
                                                          Math.Min(newWord.Offset, startWord.Offset),
                                                          Math.Max(newWord.EndOffset, startWord.EndOffset));
                    // moves caret to start or end of selection
                    if (newWord.Offset < startWord.Offset)
                    {
                        textArea.Caret.Offset = newWord.Offset;
                    }
                    else
                    {
                        textArea.Caret.Offset = Math.Max(newWord.EndOffset, startWord.EndOffset);
                    }
                }
            }
            textArea.Caret.BringCaretToView(5.0);
        }
Пример #5
0
        /// <inheritdoc/>
        public override Selection UpdateOnDocumentChange(DocumentChangeEventArgs e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }
            int newStartOffset, newEndOffset;

            if (startOffset <= endOffset)
            {
                newStartOffset = e.GetNewOffset(startOffset, AnchorMovementType.Default);
                newEndOffset   = Math.Max(newStartOffset, e.GetNewOffset(endOffset, AnchorMovementType.BeforeInsertion));
            }
            else
            {
                newEndOffset   = e.GetNewOffset(endOffset, AnchorMovementType.Default);
                newStartOffset = Math.Max(newEndOffset, e.GetNewOffset(startOffset, AnchorMovementType.BeforeInsertion));
            }
            return(Selection.Create(
                       textArea,
                       new TextViewPosition(textArea.Document.GetLocation(newStartOffset), start.VisualColumn),
                       new TextViewPosition(textArea.Document.GetLocation(newEndOffset), end.VisualColumn)
                       ));
        }
        void textArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            mode = MouseSelectionMode.None;
            if (textArea.Document == null)
            {
                // Avoid entering any selection mode when there's no document attached.
                return;
            }
            if (!e.Handled && e.ChangedButton == MouseButton.Left)
            {
                ModifierKeys modifiers = Keyboard.Modifiers;
                bool         shift     = (modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
                if (enableTextDragDrop && e.ClickCount == 1 && !shift)
                {
                    int  visualColumn;
                    bool isAtEndOfLine;
                    int  offset = GetOffsetFromMousePosition(e, out visualColumn, out isAtEndOfLine);
                    if (textArea.Selection.Contains(offset))
                    {
                        if (textArea.CaptureMouse())
                        {
                            mode = MouseSelectionMode.PossibleDragStart;
                            possibleDragStartMousePos = e.GetPosition(textArea);
                        }
                        e.Handled = true;
                        return;
                    }
                }

                var oldPosition = textArea.Caret.Position;
                SetCaretOffsetToMousePosition(e);


                if (!shift)
                {
                    textArea.ClearSelection();
                }
                if (textArea.CaptureMouse())
                {
                    if ((modifiers & ModifierKeys.Alt) == ModifierKeys.Alt && textArea.Options.EnableRectangularSelection)
                    {
                        mode = MouseSelectionMode.Rectangular;
                        if (shift && textArea.Selection is RectangleSelection)
                        {
                            textArea.Selection = textArea.Selection.StartSelectionOrSetEndpoint(oldPosition, textArea.Caret.Position);
                        }
                    }
                    else if (e.ClickCount == 1 && ((modifiers & ModifierKeys.Control) == 0))
                    {
                        mode = MouseSelectionMode.Normal;
                        if (shift && !(textArea.Selection is RectangleSelection))
                        {
                            textArea.Selection = textArea.Selection.StartSelectionOrSetEndpoint(oldPosition, textArea.Caret.Position);
                        }
                    }
                    else
                    {
                        SimpleSegment startWord;
                        if (e.ClickCount == 3)
                        {
                            mode      = MouseSelectionMode.WholeLine;
                            startWord = GetLineAtMousePosition(e);
                        }
                        else
                        {
                            mode      = MouseSelectionMode.WholeWord;
                            startWord = GetWordAtMousePosition(e);
                        }
                        if (startWord == SimpleSegment.Invalid)
                        {
                            mode = MouseSelectionMode.None;
                            textArea.ReleaseMouseCapture();
                            return;
                        }
                        if (shift && !textArea.Selection.IsEmpty)
                        {
                            if (startWord.Offset < textArea.Selection.SurroundingSegment.Offset)
                            {
                                textArea.Selection = textArea.Selection.SetEndpoint(new TextViewPosition(textArea.Document.GetLocation(startWord.Offset)));
                            }
                            else if (startWord.EndOffset > textArea.Selection.SurroundingSegment.EndOffset)
                            {
                                textArea.Selection = textArea.Selection.SetEndpoint(new TextViewPosition(textArea.Document.GetLocation(startWord.EndOffset)));
                            }
                            this.startWord = new AnchorSegment(textArea.Document, textArea.Selection.SurroundingSegment);
                        }
                        else
                        {
                            textArea.Selection = Selection.Create(textArea, startWord.Offset, startWord.EndOffset);
                            this.startWord     = new AnchorSegment(textArea.Document, startWord.Offset, startWord.Length);
                        }
                    }
                }
            }
            e.Handled = true;
        }