@@ -422,42 +422,43 @@ static void OnPaste(object target, ExecutedRoutedEventArgs args)
422422 }
423423 if ( dataObject == null )
424424 return ;
425- Debug . WriteLine ( dataObject . GetData ( DataFormats . Html ) as string ) ;
425+
426+ var pastingEventArgs = new DataObjectPastingEventArgs ( dataObject , false , DataFormats . UnicodeText ) ;
427+ textArea . RaiseEvent ( pastingEventArgs ) ;
428+ if ( pastingEventArgs . CommandCancelled )
429+ return ;
430+
431+ dataObject = pastingEventArgs . DataObject ;
432+ if ( dataObject == null )
433+ return ;
426434
427435 // convert text back to correct newlines for this document
428436 string newLine = TextUtilities . GetNewLineFromDocument ( textArea . Document , textArea . Caret . Line ) ;
429437 string text ;
430438 try {
431- text = ( string ) dataObject . GetData ( DataFormats . UnicodeText ) ;
439+ // Try retrieving the text as one of:
440+ // - the FormatToApply
441+ // - UnicodeText
442+ // - Text
443+ // (but don't try the same format twice)
444+ if ( pastingEventArgs . FormatToApply != null && dataObject . GetDataPresent ( pastingEventArgs . FormatToApply ) )
445+ text = ( string ) dataObject . GetData ( pastingEventArgs . FormatToApply ) ;
446+ else if ( pastingEventArgs . FormatToApply != DataFormats . UnicodeText && dataObject . GetDataPresent ( DataFormats . UnicodeText ) )
447+ text = ( string ) dataObject . GetData ( DataFormats . UnicodeText ) ;
448+ else if ( pastingEventArgs . FormatToApply != DataFormats . Text && dataObject . GetDataPresent ( DataFormats . Text ) )
449+ text = ( string ) dataObject . GetData ( DataFormats . Text ) ;
450+ else
451+ return ; // no text data format
432452 text = TextUtilities . NormalizeNewLines ( text , newLine ) ;
433453 } catch ( OutOfMemoryException ) {
454+ // may happen when trying to paste a huge string
434455 return ;
435456 }
436457
437458 if ( ! string . IsNullOrEmpty ( text ) ) {
438459 bool fullLine = textArea . Options . CutCopyWholeLine && dataObject . GetDataPresent ( LineSelectedType ) ;
439460 bool rectangular = dataObject . GetDataPresent ( RectangleSelection . RectangularSelectionDataType ) ;
440461
441- string pasteFormat ;
442- // fill the suggested DataFormat used for the paste action:
443- if ( fullLine )
444- pasteFormat = LineSelectedType ;
445- else if ( rectangular && textArea . Selection . IsEmpty && ! ( textArea . Selection is RectangleSelection ) )
446- pasteFormat = RectangleSelection . RectangularSelectionDataType ;
447- else
448- pasteFormat = DataFormats . UnicodeText ;
449-
450- var pastingEventArgs = new DataObjectPastingEventArgs ( dataObject , false , pasteFormat ) ;
451- textArea . RaiseEvent ( pastingEventArgs ) ;
452- if ( pastingEventArgs . CommandCancelled )
453- return ;
454-
455- // DataObject.PastingEvent handlers might have changed the format to apply.
456- pasteFormat = pastingEventArgs . FormatToApply ;
457-
458- fullLine = pasteFormat == LineSelectedType ;
459- rectangular = pasteFormat == RectangleSelection . RectangularSelectionDataType ;
460-
461462 if ( fullLine ) {
462463 DocumentLine currentLine = textArea . Document . GetLineByNumber ( textArea . Caret . Line ) ;
463464 if ( textArea . ReadOnlySectionProvider . CanInsert ( currentLine . Offset ) ) {
0 commit comments