public static void GenerateClipBoardData(DataObjectCopyingEventArgs e, TextSelection selection) { string str; using (MemoryStream stream = new MemoryStream()) { TextRange range = new TextRange(selection.Start, selection.End); range.ClearAllProperties(); range.Save(stream, DataFormats.Xaml, true); stream.Flush(); stream.Position = 0L; using (StreamReader reader = new StreamReader(stream)) { str = reader.ReadToEnd(); } } if (!string.IsNullOrEmpty(str)) { string str2 = ReplaceControls.ReplaceGUIWithClipboardControl(str, selection.Start, selection.End); if (!string.IsNullOrEmpty(str2) && (str2 != str)) { e.DataObject.SetData(BamaDataFormat, str2); } } }
/// <summary> /// Delegate called to disable the capability of dragging selected text of the text box. /// </summary> /// <param name="pSender">The modified text box.</param> /// <param name="pEventArgs">The event arguments.</param> private void DisableDragCopy(object pSender, DataObjectCopyingEventArgs pEventArgs) { if (pEventArgs.IsDragDrop) { pEventArgs.CancelCommand(); } }
private void InputBox_Copying(object sender, DataObjectCopyingEventArgs e) { using (new Util_Perf.FunLog("InputBox.InputBox_Copying")) { ClipboardMgr.GenerateClipBoardData(e, base.Selection); } }
void StartDrag() { // prevent nested StartDrag calls mode = SelectionMode.Drag; // mouse capture and Drag'n'Drop doesn't mix textArea.ReleaseMouseCapture(); DataObject dataObject = textArea.Selection.CreateDataObject(textArea); DragDropEffects allowedEffects = DragDropEffects.All; var deleteOnMove = textArea.Selection.Segments.Select(s => new AnchorSegment(textArea.Document, s)).ToList(); foreach (ISegment s in deleteOnMove) { ISegment[] result = textArea.GetDeletableSegments(s); if (result.Length != 1 || result[0].Offset != s.Offset || result[0].EndOffset != s.EndOffset) { allowedEffects &= ~DragDropEffects.Move; } } var copyingEventArgs = new DataObjectCopyingEventArgs(dataObject, true); textArea.RaiseEvent(copyingEventArgs); if (copyingEventArgs.CommandCancelled) return; object dragDescriptor = new object(); this.currentDragDescriptor = dragDescriptor; DragDropEffects resultEffect; using (textArea.AllowCaretOutsideSelection()) { var oldCaretPosition = textArea.Caret.Position; try { Debug.WriteLine("DoDragDrop with allowedEffects=" + allowedEffects); resultEffect = DragDrop.DoDragDrop(textArea, dataObject, allowedEffects); Debug.WriteLine("DoDragDrop done, resultEffect=" + resultEffect); } catch (COMException ex) { // ignore COM errors - don't crash on badly implemented drop targets Debug.WriteLine("DoDragDrop failed: " + ex.ToString()); return; } if (resultEffect == DragDropEffects.None) { // reset caret if drag was aborted textArea.Caret.Position = oldCaretPosition; } } this.currentDragDescriptor = null; if (deleteOnMove != null && resultEffect == DragDropEffects.Move && (allowedEffects & DragDropEffects.Move) == DragDropEffects.Move) { bool draggedInsideSingleDocument = (dragDescriptor == textArea.Document.UndoStack.LastGroupDescriptor); if (draggedInsideSingleDocument) textArea.Document.UndoStack.StartContinuedUndoGroup(null); textArea.Document.BeginUpdate(); try { foreach (ISegment s in deleteOnMove) { textArea.Document.Remove(s.Offset, s.Length); } } finally { textArea.Document.EndUpdate(); if (draggedInsideSingleDocument) textArea.Document.UndoStack.EndUndoGroup(); } } }
static bool CopySelectedText(TextArea textArea) { var data = textArea.Selection.CreateDataObject(textArea); var copyingEventArgs = new DataObjectCopyingEventArgs(data, false); textArea.RaiseEvent(copyingEventArgs); if (copyingEventArgs.CommandCancelled) return false; try { Clipboard.SetDataObject(data, true); } catch (ExternalException) { // Apparently this exception sometimes happens randomly. // The MS controls just ignore it, so we'll do the same. } string text = textArea.Selection.GetText(); text = TextUtilities.NormalizeNewLines(text, Environment.NewLine); textArea.OnTextCopied(new TextEventArgs(text)); return true; }
static bool CopyWholeLine(TextArea textArea, DocumentLine line) { ISegment wholeLine = new SimpleSegment(line.Offset, line.TotalLength); string text = textArea.Document.GetText(wholeLine); // Ensure we use the appropriate newline sequence for the OS text = TextUtilities.NormalizeNewLines(text, Environment.NewLine); DataObject data = new DataObject(); if (ConfirmDataFormat(textArea, data, DataFormats.UnicodeText)) data.SetText(text); // Also copy text in HTML format to clipboard - good for pasting text into Word // or to the SharpDevelop forums. if (ConfirmDataFormat(textArea, data, DataFormats.Html)) { IHighlighter highlighter = textArea.GetService(typeof(IHighlighter)) as IHighlighter; HtmlClipboard.SetHtml(data, HtmlClipboard.CreateHtmlFragment(textArea.Document, highlighter, wholeLine, new HtmlOptions(textArea.Options))); } if (ConfirmDataFormat(textArea, data, LineSelectedType)) { MemoryStream lineSelected = new MemoryStream(1); lineSelected.WriteByte(1); data.SetData(LineSelectedType, lineSelected, false); } var copyingEventArgs = new DataObjectCopyingEventArgs(data, false); textArea.RaiseEvent(copyingEventArgs); if (copyingEventArgs.CommandCancelled) return false; try { Clipboard.SetDataObject(data, true); } catch (ExternalException) { // Apparently this exception sometimes happens randomly. // The MS controls just ignore it, so we'll do the same. return false; } textArea.OnTextCopied(new TextEventArgs(text)); return true; }
private static void NoDragCopy(object sender, DataObjectCopyingEventArgs e) { if (e.IsDragDrop) { e.CancelCommand(); } }
private void DataObjectCopyingHandler(object sender, DataObjectCopyingEventArgs e) { try { Clipboard.SetText(RTBExtensions.GetSelectedInlineText(this.richTextBox)); if (SettingsData.Instance.CutIsTriggered) { this.richTextBox.Selection.Text = string.Empty; SettingsData.Instance.CutIsTriggered = false; } e.CancelCommand(); } catch { } }
private static void OnPasteMultiple(object sender, ExecutedRoutedEventArgs args) { var textEditor = (TextEditor)sender; var textArea = textEditor.TextArea; if (textArea?.Document == null) return; var completionWindow = new CompletionWindow(textArea); // ----- Create completion list. var completionList = completionWindow.CompletionList; var completionData = completionList.CompletionData; var stringBuilder = new StringBuilder(); foreach (var text in ClipboardRing.GetEntries()) { // Replace special characters for display. stringBuilder.Clear(); stringBuilder.Append(text); stringBuilder.Replace("\n", "\\n"); stringBuilder.Replace("\r", "\\r"); stringBuilder.Replace("\t", "\\t"); // Use TextBlock for TextTrimming. var textBlock = new TextBlock { Text = stringBuilder.ToString(), TextTrimming = TextTrimming.CharacterEllipsis, MaxWidth = 135 }; completionData.Add(new CompletionData(text, text, null, textBlock)); } // ----- Show completion window. completionList.SelectedItem = completionData[0]; completionWindow.Show(); // ----- Handle InsertionRequested event. completionList.InsertionRequested += (s, e) => { var ta = completionWindow.TextArea; var cl = completionWindow.CompletionList; var text = cl.SelectedItem.Text; // Copy text into clipboard. var data = new DataObject(); if (EditingCommandHandler.ConfirmDataFormat(ta, data, DataFormats.UnicodeText)) data.SetText(text); var copyingEventArgs = new DataObjectCopyingEventArgs(data, false); ta.RaiseEvent(copyingEventArgs); if (copyingEventArgs.CommandCancelled) return; try { Clipboard.SetDataObject(data, true); } catch (ExternalException) { } ClipboardRing.Add(text); }; }
private static void OnCopy(object sender, DataObjectCopyingEventArgs e) { var control = sender as System.Windows.Controls.TextBox; if (control == null) return; string html = GetHtmlCopyContent(control); html = HTMLClipboardHelper.GetHtmlDataString(html); e.DataObject.SetData(DataFormats.UnicodeText, control.Text.ToString(), true); e.DataObject.SetData(DataFormats.Text, control.Text.ToString(), true); e.DataObject.SetData(DataFormats.OemText, control.Text.ToString(), true); e.DataObject.SetData(DataFormats.Html, html, true); Clipboard.SetDataObject(e.DataObject, true); e.Handled = true; e.CancelCommand(); }
private void CopyCommand(object sender, DataObjectCopyingEventArgs args) { if (!string.IsNullOrEmpty(Selection.Text)) { args.DataObject.SetData(typeof(string), Selection.Text); } args.Handled = true; }