protected void OnDragDrop(object sender, DragEventArgs e) { Point p = _textArea.PointToClient(new Point(e.X, e.Y)); 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; } //TODO2 drag/drop - all this: //if (e.Data.GetDataPresent(typeof(Selection))) //{ // Selection sel = (Selection)e.Data.GetData(typeof(Selection)); // 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.StartOffset, len); // if (sel.StartOffset < 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(); } } }
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(); } } }
public void Paste(object sender, EventArgs e) { 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); // we got NullReferenceExceptions here, apparently the clipboard can contain null strings if (!string.IsNullOrEmpty(text)) { textArea.Document.UndoStack.StartUndoGroup(); try { if (textArea.SelectionManager.HasSomethingSelected) { textArea.Caret.Position = textArea.SelectionManager.SelectionCollection[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; } } } }
protected void OnDragDrop(object sender, DragEventArgs e) { if (!IsSupportedData(e.Data)) { return; } textArea.BeginUpdate(); textArea.Document.UndoStack.StartUndoGroup(); try { var offset = textArea.Caret.Offset; if (textArea.IsReadOnly(offset)) { return; } try { if (e.Data.GetDataPresent(typeof(DefaultSelection))) { var 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)) { return; } var len = sel.Length; textArea.Document.Remove(sel.Offset, len); if (sel.Offset < offset) { offset -= len; } } } } catch (System.InvalidCastException) { /* * If GetDataPresent(typeof(DefaultSelection)) threw this * exception, then it's an interprocess DefaultSelection * COM object that is not serializable! In general, * GetDataPresent(typeof(...)) throws InvalidCastException * for drags and drops from other GitExt processes (maybe * we need to make the data objects [Serializable]?). We * can get around this exception by doing * GetDataPresent(String s) [using the string of the type * name seems to work fine!] Since it is interprocess * data, just get the string data from it - special * handling logic in try {} is only valid for selections * within the current process's text editor! */ } textArea.SelectionManager.ClearSelection(); InsertString(offset, (string)e.Data.GetData("System.String")); textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea)); } finally { textArea.Document.UndoStack.EndUndoGroup(); textArea.EndUpdate(); } }