protected void OnDragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(string))) { _textArea.BeginUpdate(); _textArea.Document.UndoStack.StartUndoGroup(); try { int offset = _textArea.Caret.Offset; if (_textArea.IsReadOnly(offset)) { // prevent dragging text into readonly section return; } if (e.Data.GetDataPresent(typeof(DefaultSelection))) { ISelection sel = (ISelection)e.Data.GetData(typeof(DefaultSelection)); if (sel.ContainsPosition(_textArea.Caret.Position)) { return; } if (GetDragDropEffect(e) == DragDropEffects.Move) { if (SelectionManager.SelectionIsReadOnly(_textArea.Document, sel)) { // prevent dragging text out of readonly section return; } int len = sel.Length; _textArea.Document.Remove(sel.Offset, len); if (sel.Offset < offset) { offset -= len; } } } _textArea.SelectionManager.ClearSelection(); InsertString(offset, (string)e.Data.GetData(typeof(string))); _textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea)); } finally { _textArea.Document.UndoStack.EndUndoGroup(); _textArea.EndUpdate(); } } }
/// <summary> /// Pastes the content of the clipboard into the document. /// </summary> public void Paste() { if (!_textArea.EnableCutOrPaste) { return; } // Clipboard.GetDataObject may throw an exception... for (int i = 0; ; i++) { try { IDataObject data = Clipboard.GetDataObject(); if (data == null) { return; } bool fullLine = data.GetDataPresent(LineSelectedType); if (data.GetDataPresent(DataFormats.UnicodeText)) { string text = (string)data.GetData(DataFormats.UnicodeText); if (text.Length > 0) { _textArea.Document.UndoStack.StartUndoGroup(); try { if (_textArea.SelectionManager.HasSomethingSelected) { _textArea.Caret.Position = _textArea.SelectionManager.Selections[0].StartPosition; _textArea.SelectionManager.RemoveSelectedText(); } if (fullLine) { int col = _textArea.Caret.Column; _textArea.Caret.Column = 0; if (!_textArea.IsReadOnly(_textArea.Caret.Offset)) { _textArea.InsertString(text); } _textArea.Caret.Column = col; } else { // _textArea.EnableCutOrPaste already checked readonly for this case _textArea.InsertString(text); } } finally { _textArea.Document.UndoStack.EndUndoGroup(); } } } return; } catch (ExternalException) { // GetDataObject does not provide RetryTimes parameter if (i > 5) { throw; } } } }