Browse Source

Ignore ExternalException when clipboard cannot be opened for pasting.

4.1
Daniel Grunwald 15 years ago
parent
commit
acefecc74e
  1. 19
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/EditingCommandHandler.cs
  2. 2
      src/Main/ICSharpCode.Core.WinForms/Util/ClipboardWrapper.cs

19
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/EditingCommandHandler.cs

@ -365,6 +365,8 @@ namespace ICSharpCode.AvalonEdit.Editing
if (textArea != null && textArea.Document != null) { if (textArea != null && textArea.Document != null) {
args.CanExecute = textArea.ReadOnlySectionProvider.CanInsert(textArea.Caret.Offset) args.CanExecute = textArea.ReadOnlySectionProvider.CanInsert(textArea.Caret.Offset)
&& Clipboard.ContainsText(); && Clipboard.ContainsText();
// WPF Clipboard.ContainsText() is safe to call without catching ExternalExceptions
// because it doesn't try to lock the clipboard - it just peeks inside with IsClipboardFormatAvailable().
args.Handled = true; args.Handled = true;
} }
} }
@ -373,15 +375,24 @@ namespace ICSharpCode.AvalonEdit.Editing
{ {
TextArea textArea = GetTextArea(target); TextArea textArea = GetTextArea(target);
if (textArea != null && textArea.Document != null) { if (textArea != null && textArea.Document != null) {
Debug.WriteLine( Clipboard.GetText(TextDataFormat.Html) ); IDataObject dataObject;
try {
dataObject = Clipboard.GetDataObject();
} catch (ExternalException) {
return;
}
if (dataObject == null)
return;
Debug.WriteLine( dataObject.GetData(DataFormats.Html) as string );
// convert text back to correct newlines for this document // convert text back to correct newlines for this document
string newLine = TextUtilities.GetNewLineFromDocument(textArea.Document, textArea.Caret.Line); string newLine = TextUtilities.GetNewLineFromDocument(textArea.Document, textArea.Caret.Line);
string text = TextUtilities.NormalizeNewLines(Clipboard.GetText(), newLine); string text = (string)dataObject.GetData(DataFormats.UnicodeText);
text = TextUtilities.NormalizeNewLines(text, newLine);
if (!string.IsNullOrEmpty(text)) { if (!string.IsNullOrEmpty(text)) {
bool fullLine = textArea.Options.CutCopyWholeLine && Clipboard.ContainsData(LineSelectedType); bool fullLine = textArea.Options.CutCopyWholeLine && dataObject.GetDataPresent(LineSelectedType);
bool rectangular = Clipboard.ContainsData(RectangleSelection.RectangularSelectionDataType); bool rectangular = dataObject.GetDataPresent(RectangleSelection.RectangularSelectionDataType);
if (fullLine) { if (fullLine) {
DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line); DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line);
if (textArea.ReadOnlySectionProvider.CanInsert(currentLine.Offset)) { if (textArea.ReadOnlySectionProvider.CanInsert(currentLine.Offset)) {

2
src/Main/ICSharpCode.Core.WinForms/Util/ClipboardWrapper.cs

@ -12,6 +12,8 @@ namespace ICSharpCode.Core.WinForms
/// </summary> /// </summary>
public static class ClipboardWrapper public static class ClipboardWrapper
{ {
[Obsolete("Avoid using this property: it is problematic because it requires exclusive clipboard access. " +
"The Clipboard.ContainsText() implementation in WPF is much better than the one in WinForms.")]
public static bool ContainsText { public static bool ContainsText {
get { get {
try { try {

Loading…
Cancel
Save