예제 #1
0
        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;
                }

                var pastingEventArgs = new DataObjectPastingEventArgs(dataObject, false, DataFormats.UnicodeText);
                textArea.RaiseEvent(pastingEventArgs);
                if (pastingEventArgs.CommandCancelled)
                {
                    return;
                }

                string text = GetTextToPaste(pastingEventArgs, textArea);

                if (string.IsNullOrEmpty(text))
                {
                    return;
                }

                dataObject = pastingEventArgs.DataObject;
                bool fullLine    = textArea.Options.CutCopyWholeLine && dataObject.GetDataPresent(LineSelectedType);
                bool rectangular = dataObject.GetDataPresent(RectangleSelection.RectangularSelectionDataType);

                bool isTextPasted = false;
                if (fullLine)
                {
                    DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line);
                    if (textArea.ReadOnlySectionProvider.CanInsert(currentLine.Offset))
                    {
                        textArea.Document.Insert(currentLine.Offset, text);
                        isTextPasted = true;
                    }
                }
                else if (rectangular && textArea.Selection.IsEmpty && !(textArea.Selection is RectangleSelection))
                {
                    if (!RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Position, text, false))
                    {
                        textArea.ReplaceSelectionWithText(text);
                        isTextPasted = true;
                    }
                }
                else
                {
                    textArea.ReplaceSelectionWithText(text);
                    isTextPasted = true;
                }

                if (isTextPasted)
                {
                    textArea.Caret.BringCaretToView();
                    textArea.OnTextPasted(new TextEventArgs(text));
                }
                args.Handled = true;
            }
        }