Ejemplo n.º 1
0
        /// <summary>
        /// Performs a rectangular paste operation.
        /// </summary>
        public static bool PerformRectangularPaste(TextArea textArea, TextViewPosition startPosition, string text, bool selectInsertedText)
        {
            if (textArea == null)
            {
                throw new ArgumentNullException("textArea");
            }
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }
            int          newLineCount = text.Count(c => c == '\n');    // TODO might not work in all cases, but single \r line endings are really rare today.
            TextLocation endLocation  = new TextLocation(startPosition.Line + newLineCount, startPosition.Column);

            if (endLocation.Line <= textArea.Document.LineCount)
            {
                int endOffset = textArea.Document.GetOffset(endLocation);
                if (textArea.Selection.EnableVirtualSpace || textArea.Document.GetLocation(endOffset) == endLocation)
                {
                    RectangleSelection rsel = new RectangleSelection(textArea, startPosition, endLocation.Line, GetXPos(textArea, startPosition));
                    rsel.ReplaceSelectionWithText(text);
                    if (selectInsertedText && textArea.Selection is RectangleSelection)
                    {
                        RectangleSelection sel = (RectangleSelection)textArea.Selection;
                        textArea.Selection = new RectangleSelection(textArea, startPosition, sel.endLine, sel.endXPos);
                    }
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public override bool Equals(object obj)
        {
            RectangleSelection r = obj as RectangleSelection;

            return(r != null && r.textArea == this.textArea &&
                   r.topLeftOffset == this.topLeftOffset && r.bottomRightOffset == this.bottomRightOffset &&
                   r.startLine == this.startLine && r.endLine == this.endLine &&
                   r.startXPos == this.startXPos && r.endXPos == this.endXPos);
        }
        static void OnPaste(object target, ExecutedRoutedEventArgs args)
        {
            TextArea textArea = GetTextArea(target);

            if (textArea != null && textArea.Document != null)
            {
                IDataObject dataObject;
                try {
                    dataObject = Clipboard.GetDataObject();
                } catch (ExternalException) {
                    return;
                }
                if (dataObject == null)
                {
                    return;
                }
                Debug.WriteLine(dataObject.GetData(DataFormats.Html) as string);

                // convert text back to correct newlines for this document
                string newLine = TextUtilities.GetNewLineFromDocument(textArea.Document, textArea.Caret.Line);
                string text;
                try {
                    text = (string)dataObject.GetData(DataFormats.UnicodeText);
                    text = TextUtilities.NormalizeNewLines(text, newLine);
                } catch (OutOfMemoryException) {
                    return;
                }

                if (!string.IsNullOrEmpty(text))
                {
                    bool fullLine    = textArea.Options.CutCopyWholeLine && dataObject.GetDataPresent(LineSelectedType);
                    bool rectangular = dataObject.GetDataPresent(RectangleSelection.RectangularSelectionDataType);
                    if (fullLine)
                    {
                        DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line);
                        if (textArea.ReadOnlySectionProvider.CanInsert(currentLine.Offset))
                        {
                            textArea.Document.Insert(currentLine.Offset, text);
                        }
                    }
                    else if (rectangular && textArea.Selection.IsEmpty && !(textArea.Selection is RectangleSelection))
                    {
                        if (!RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Position, text, false))
                        {
                            textArea.ReplaceSelectionWithText(text);
                        }
                    }
                    else
                    {
                        textArea.ReplaceSelectionWithText(text);
                    }
                }
                textArea.Caret.BringCaretToView();
                args.Handled = true;
            }
        }
        void textArea_Drop(object sender, DragEventArgs e)
        {
            try {
                DragDropEffects effect = GetEffect(e);
                e.Effects = effect;
                if (effect != DragDropEffects.None)
                {
                    string text = e.Data.GetData(DataFormats.UnicodeText, true) as string;
                    if (text != null)
                    {
                        int start = textArea.Caret.Offset;
                        if (mode == SelectionMode.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);

                            bool rectangular = e.Data.GetDataPresent(RectangleSelection.RectangularSelectionDataType);

                            string newLine = TextUtilities.GetNewLineFromDocument(textArea.Document, textArea.Caret.Line);
                            text = TextUtilities.NormalizeNewLines(text, newLine);

                            // 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);
            }
        }