Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Commit

Permalink
Fixed 'Bug 676207 - copying texts from other application and pasting
Browse files Browse the repository at this point in the history
twice into texteditor shows strange behavior'.
  • Loading branch information
mkrueger committed Mar 18, 2011
1 parent 856ebb0 commit d4f99c6
Showing 1 changed file with 55 additions and 54 deletions.
109 changes: 55 additions & 54 deletions main/src/core/Mono.Texteditor/Mono.TextEditor/ClipboardActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,66 +357,67 @@ static int PasteFrom (Clipboard clipboard, TextEditorData data, bool preserveSel
int result = -1;
if (!data.CanEdit (data.Document.OffsetToLineNumber (insertionOffset)))
return result;
clipboard.RequestContents (CopyOperation.MD_ATOM, delegate(Clipboard clp, SelectionData selectionData) {
if (selectionData.Length > 0) {
byte[] selBytes = selectionData.Data;

string text = System.Text.Encoding.UTF8.GetString (selBytes, 1, selBytes.Length - 1);
bool pasteBlock = (selBytes[0] & 1) == 1;
bool pasteLine = (selBytes[0] & 2) == 2;
if (!pasteBlock && !pasteLine)
return;

data.Document.BeginAtomicUndo ();
if (preserveSelection && data.IsSomethingSelected)
data.DeleteSelectedText ();

data.Caret.PreserveSelection = true;
if (pasteBlock) {
string[] lines = text.Split ('\r');
int lineNr = data.Document.OffsetToLineNumber (insertionOffset);
int col = insertionOffset - data.Document.GetLine (lineNr).Offset;
int visCol = data.Document.GetLine (lineNr).GetVisualColumn (data, col);
LineSegment curLine;
int lineCol = col;
result = 0;
for (int i = 0; i < lines.Length; i++) {
while (data.Document.LineCount <= lineNr + i) {
data.Insert (data.Document.Length, Environment.NewLine);
result += Environment.NewLine.Length;
}
curLine = data.Document.GetLine (lineNr + i);
if (lines[i].Length > 0) {
lineCol = curLine.GetLogicalColumn (data, visCol);
if (curLine.EditableLength + 1 < lineCol) {
result += lineCol - curLine.EditableLength;
data.Insert (curLine.Offset + curLine.EditableLength, new string (' ', lineCol - curLine.EditableLength));
if (clipboard.WaitIsTargetAvailable (CopyOperation.MD_ATOM)) {
clipboard.RequestContents (CopyOperation.MD_ATOM, delegate(Clipboard clp, SelectionData selectionData) {
if (selectionData.Length > 0) {
byte[] selBytes = selectionData.Data;

string text = System.Text.Encoding.UTF8.GetString (selBytes, 1, selBytes.Length - 1);
bool pasteBlock = (selBytes [0] & 1) == 1;
bool pasteLine = (selBytes [0] & 2) == 2;
if (!pasteBlock && !pasteLine)
return;

data.Document.BeginAtomicUndo ();
if (preserveSelection && data.IsSomethingSelected)
data.DeleteSelectedText ();

data.Caret.PreserveSelection = true;
if (pasteBlock) {
string[] lines = text.Split ('\r');
int lineNr = data.Document.OffsetToLineNumber (insertionOffset);
int col = insertionOffset - data.Document.GetLine (lineNr).Offset;
int visCol = data.Document.GetLine (lineNr).GetVisualColumn (data, col);
LineSegment curLine;
int lineCol = col;
result = 0;
for (int i = 0; i < lines.Length; i++) {
while (data.Document.LineCount <= lineNr + i) {
data.Insert (data.Document.Length, Environment.NewLine);
result += Environment.NewLine.Length;
}
data.Insert (curLine.Offset + lineCol, lines[i]);
result += lines[i].Length;
curLine = data.Document.GetLine (lineNr + i);
if (lines [i].Length > 0) {
lineCol = curLine.GetLogicalColumn (data, visCol);
if (curLine.EditableLength + 1 < lineCol) {
result += lineCol - curLine.EditableLength;
data.Insert (curLine.Offset + curLine.EditableLength, new string (' ', lineCol - curLine.EditableLength));
}
data.Insert (curLine.Offset + lineCol, lines [i]);
result += lines [i].Length;
}
if (!preserveState)
data.Caret.Offset = curLine.Offset + lineCol + lines [i].Length;
}
} else if (pasteLine) {
result = text.Length;
LineSegment curLine = data.Document.GetLine (data.Caret.Line);
data.Insert (curLine.Offset, text + data.EolMarker);
if (!preserveState)
data.Caret.Offset = curLine.Offset + lineCol + lines[i].Length;
data.Caret.Offset += text.Length + data.EolMarker.Length;
}
} else if (pasteLine) {
result += text.Length;
LineSegment curLine = data.Document.GetLine (data.Caret.Line);
data.Insert (curLine.Offset, text + data.EolMarker);
/* data.MainSelection = new Selection (data.Document.OffsetToLocation (insertionOffset),
data.Caret.Location,
lines.Length > 1 ? SelectionMode.Block : SelectionMode.Normal);*/
if (!preserveState)
data.Caret.Offset += text.Length + data.EolMarker.Length;
data.ClearSelection ();
data.Caret.PreserveSelection = false;
data.Document.EndAtomicUndo ();
}
/* data.MainSelection = new Selection (data.Document.OffsetToLocation (insertionOffset),
data.Caret.Location,
lines.Length > 1 ? SelectionMode.Block : SelectionMode.Normal);*/
if (!preserveState)
data.ClearSelection ();
data.Caret.PreserveSelection = false;
data.Document.EndAtomicUndo ();
}
});

if (result < 0) {
clipboard.WaitIsTextAvailable ();
});
}

if (result < 0 && clipboard.WaitIsTextAvailable ()) {
clipboard.RequestText (delegate(Clipboard clp, string text) {
if (string.IsNullOrEmpty (text))
return;
Expand Down

0 comments on commit d4f99c6

Please sign in to comment.