예제 #1
0
		static int PasteFrom (Clipboard clipboard, TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState)
		{
			int result = -1;
			if (!data.CanEdit (data.Document.OffsetToLineNumber (insertionOffset)))
				return result;
			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;
						
//						var clearSelection = data.IsSomethingSelected ? data.MainSelection.SelectionMode != SelectionMode.Block : true;
						if (pasteBlock) {
							using (var undo = data.OpenUndoGroup ()) {
								var version = data.Document.Version;
								data.DeleteSelectedText (!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block);
								data.EnsureCaretIsNotVirtual ();
								insertionOffset = version.MoveOffsetTo (data.Document.Version, insertionOffset);

								data.Caret.PreserveSelection = true;
							
								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);
								DocumentLine curLine;
								int lineCol = col;
								result = 0;
								for (int i = 0; i < lines.Length; i++) {
									while (data.Document.LineCount <= lineNr + i) {
										data.Insert (data.Document.TextLength, Environment.NewLine);
										result += Environment.NewLine.Length;
									}
									curLine = data.Document.GetLine (lineNr + i);
									if (lines [i].Length > 0) {
										lineCol = curLine.GetLogicalColumn (data, visCol);
										if (curLine.Length + 1 < lineCol) {
											result += lineCol - curLine.Length;
											data.Insert (curLine.Offset + curLine.Length, new string (' ', lineCol - curLine.Length));
										}
										data.Insert (curLine.Offset + lineCol, lines [i]);
										result += lines [i].Length;
									}
									if (!preserveState)
										data.Caret.Offset = curLine.Offset + lineCol + lines [i].Length;
								}
								if (!preserveState)
									data.ClearSelection ();
								data.Caret.PreserveSelection = false;
							}
						} else if (pasteLine) {
							using (var undo = data.OpenUndoGroup ()) {
								data.DeleteSelectedText (!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block);
								data.EnsureCaretIsNotVirtual ();

								data.Caret.PreserveSelection = true;
								result = text.Length;
								DocumentLine curLine = data.Document.GetLine (data.Caret.Line);
								data.Insert (curLine.Offset, text + data.EolMarker);
								if (!preserveState)
									data.ClearSelection ();
								data.Caret.PreserveSelection = false;
							}
						} else {
							result = PastePlainText (data, insertionOffset, text);
						}
					}
				});
				// we got MD_ATOM text - no need to request text. (otherwise buffer may get copied twice).
				return result;
			}
			
			if (result < 0 && clipboard.WaitIsTextAvailable ()) {
				clipboard.RequestText (delegate(Clipboard clp, string text) {
					if (string.IsNullOrEmpty (text))
						return;
					using (var undo = data.OpenUndoGroup ()) {
						result = PastePlainText (data, insertionOffset, text);
					}
				});
			}
			
			return result;
		}
예제 #2
0
		static int PasteFrom (Clipboard clipboard, TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState)
		{
			int result = -1;
			if (!data.CanEdit (data.Document.OffsetToLineNumber (insertionOffset)))
				return result;
			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;
						
						using (var undo = data.OpenUndoGroup ()) {
							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);
								DocumentLine curLine;
								int lineCol = col;
								result = 0;
								for (int i = 0; i < lines.Length; i++) {
									while (data.Document.LineCount <= lineNr + i) {
										data.Insert (data.Document.TextLength, Environment.NewLine);
										result += Environment.NewLine.Length;
									}
									curLine = data.Document.GetLine (lineNr + i);
									if (lines [i].Length > 0) {
										lineCol = curLine.GetLogicalColumn (data, visCol);
										if (curLine.Length + 1 < lineCol) {
											result += lineCol - curLine.Length;
											data.Insert (curLine.Offset + curLine.Length, new string (' ', lineCol - curLine.Length));
										}
										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;
								DocumentLine curLine = data.Document.GetLine (data.Caret.Line);
								data.Insert (curLine.Offset, text + data.EolMarker);
							} else {
								int offset = data.Caret.Offset;
								data.InsertAtCaret (text);
								data.PasteText (offset, text, data.Caret.Offset - offset);
							}
							/*				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;
						}
					}
				});
				// we got MD_ATOM text - no need to request text. (otherwise buffer may get copied twice).
				return result;
			}
			
			if (result < 0 && clipboard.WaitIsTextAvailable ()) {
				clipboard.RequestText (delegate(Clipboard clp, string text) {
					if (string.IsNullOrEmpty (text))
						return;
					using (var undo = data.OpenUndoGroup ()) {
						int caretPos = data.Caret.Offset;
						if (data.IsSomethingSelected && data.MainSelection.SelectionMode == SelectionMode.Block) {
							data.Caret.PreserveSelection = true;
							data.DeleteSelectedText (false);
							int textLength = 0;
							int minLine = data.MainSelection.MinLine;
							int maxLine = data.MainSelection.MaxLine;
							var visualInsertLocation = data.LogicalToVisualLocation (data.Caret.Location);
							for (int lineNumber = minLine; lineNumber <= maxLine; lineNumber++) {
								DocumentLine lineSegment = data.GetLine (lineNumber);
								int insertOffset = lineSegment.GetLogicalColumn (data, visualInsertLocation.Column) - 1;
								if (lineSegment.Length < insertOffset) {
									int visualLastColumn = lineSegment.GetVisualColumn (data, lineSegment.Length + 1);
									int charsToInsert = visualInsertLocation.Column - visualLastColumn;
									int spaceCount = charsToInsert % data.Options.TabSize;
									string textToInsert = new string ('\t', (charsToInsert - spaceCount) / data.Options.TabSize) + new string (' ', spaceCount) + text;
									insertOffset = lineSegment.Length;
									int insertedChars = data.Insert (lineSegment.Offset + insertOffset, textToInsert);
									data.PasteText (lineSegment.Offset + insertOffset, textToInsert, insertedChars);
								} else {
									textLength = data.Insert (lineSegment.Offset + insertOffset, text);
									data.PasteText (lineSegment.Offset + insertOffset, text, textLength);
								}
							}
							
							data.MainSelection.Anchor = new DocumentLocation (System.Math.Max (DocumentLocation.MinLine, data.Caret.Line == minLine ? maxLine : minLine), System.Math.Max (DocumentLocation.MinColumn, data.Caret.Column - textLength));
							data.MainSelection.Lead = new DocumentLocation (data.Caret.Line, data.Caret.Column);
							data.Caret.PreserveSelection = false;
							data.Document.CommitMultipleLineUpdate (data.MainSelection.MinLine, data.MainSelection.MaxLine);
						} else {
							TextSegment selection = data.SelectionRange;
							if (preserveSelection && data.IsSomethingSelected)
								data.DeleteSelectedText ();
							data.Caret.PreserveSelection = true;
							//int oldLine = data.Caret.Line;
							int textLength = data.Insert (insertionOffset, text);
							result = textLength;
		
							if (data.IsSomethingSelected && data.SelectionRange.Offset >= insertionOffset)
								data.SelectionRange = new TextSegment (data.SelectionRange.Offset + textLength, data.SelectionRange.Length);
							if (data.IsSomethingSelected && data.MainSelection.GetAnchorOffset (data) >= insertionOffset)
								data.MainSelection.Anchor = data.Document.OffsetToLocation (data.MainSelection.GetAnchorOffset (data) + textLength);
							
							data.Caret.PreserveSelection = false;
							if (!preserveState) {
							} else {
								if (!selection.IsInvalid) {
									int offset = selection.Offset;
									if (offset >= insertionOffset)
										offset += textLength;
									data.SelectionRange = new TextSegment (offset, selection.Length);
								}
							}
							data.PasteText (insertionOffset, text, textLength);
						}
					}
				});
			}
			
			return result;
		}
예제 #3
0
		static int PasteFrom (Clipboard clipboard, TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState)
		{
			int result = -1;
			if (!data.CanEdit (data.Document.OffsetToLineNumber (insertionOffset)))
				return result;
			if (clipboard.WaitIsTargetAvailable (CopyOperation.MD_ATOM)) {
				clipboard.RequestContents (CopyOperation.MD_ATOM, delegate(Clipboard clp, SelectionData selectionData) {
					if (selectionData.Length > 0) {
						byte[] selBytes = selectionData.Data;
						var upperBound = System.Math.Max (0, System.Math.Min (selBytes [1], selBytes.Length - 2));
						byte[] copyData = new byte[upperBound];
						Array.Copy (selBytes, 2, copyData, 0, copyData.Length);
						var rawTextOffset = 1 + 1 + copyData.Length;
						string text = Encoding.UTF8.GetString (selBytes, rawTextOffset, selBytes.Length - rawTextOffset);
						bool pasteBlock = (selBytes [0] & 1) == 1;
						bool pasteLine = (selBytes [0] & 2) == 2;
						if (pasteBlock) {
							using (var undo = data.OpenUndoGroup ()) {
								var version = data.Document.Version;
								if (!preserveSelection)
									data.DeleteSelectedText (!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block);
								int startLine = data.Caret.Line;
								data.EnsureCaretIsNotVirtual ();
								insertionOffset = version.MoveOffsetTo (data.Document.Version, insertionOffset);

								data.Caret.PreserveSelection = true;
								var lines = new List<string> ();
								int offset = 0;
								while (true) {
									var delimiter = LineSplitter.NextDelimiter (text, offset);
									if (delimiter.IsInvalid)
										break;

									int delimiterEndOffset = delimiter.EndOffset;
									lines.Add (text.Substring (offset, delimiter.Offset - offset));
									offset = delimiterEndOffset;
								}
								if (offset < text.Length)
									lines.Add (text.Substring (offset, text.Length - offset));

								int lineNr = data.Document.OffsetToLineNumber (insertionOffset);
								int col = insertionOffset - data.Document.GetLine (lineNr).Offset;
								int visCol = data.Document.GetLine (lineNr).GetVisualColumn (data, col);
								DocumentLine curLine;
								int lineCol = col;
								result = 0;
								for (int i = 0; i < lines.Count; i++) {
									while (data.Document.LineCount <= lineNr + i) {
										data.Insert (data.Document.TextLength, Environment.NewLine);
										result += Environment.NewLine.Length;
									}
									curLine = data.Document.GetLine (lineNr + i);
									if (lines [i].Length > 0) {
										lineCol = curLine.GetLogicalColumn (data, visCol);
										if (curLine.Length + 1 < lineCol) {
											result += lineCol - curLine.Length;
											data.Insert (curLine.Offset + curLine.Length, new string (' ', lineCol - curLine.Length));
										}
										data.Insert (curLine.Offset + lineCol, lines [i]);
										result += lines [i].Length;
									}
									if (!preserveState)
										data.Caret.Offset = curLine.Offset + lineCol + lines [i].Length;
								}
								if (!preserveState)
									data.ClearSelection ();
								data.FixVirtualIndentation (startLine); 
								data.Caret.PreserveSelection = false;
							}
						} else if (pasteLine) {
							using (var undo = data.OpenUndoGroup ()) {
								if (!preserveSelection)
									data.DeleteSelectedText (!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block);
								data.EnsureCaretIsNotVirtual ();

								data.Caret.PreserveSelection = true;
								result = text.Length;
								DocumentLine curLine = data.Document.GetLine (data.Caret.Line);

								result = PastePlainText (data, curLine.Offset,  text + data.EolMarker, preserveSelection, copyData);
								if (!preserveState)
									data.ClearSelection ();
								data.Caret.PreserveSelection = false;
								data.FixVirtualIndentation (curLine.LineNumber); 
							}
						} else {
							result = PastePlainText (data, insertionOffset, text, preserveSelection, copyData);
						}
					}
				});
				// we got MD_ATOM text - no need to request text. (otherwise buffer may get copied twice).
				return result;
			}
			
			if (result < 0 && clipboard.WaitIsTextAvailable ()) {
				clipboard.RequestText (delegate(Clipboard clp, string text) {
					if (string.IsNullOrEmpty (text))
						return;
					result = PastePlainText (data, insertionOffset, text, preserveSelection);
				});
			}
			
			return result;
		}
예제 #4
0
		static int PasteFrom (Clipboard clipboard, TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState)
		{
			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));
								}
								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 += text.Length + data.EolMarker.Length;
					}
					/*				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 ();
				clipboard.RequestText (delegate(Clipboard clp, string text) {
					if (string.IsNullOrEmpty (text))
						return;
					data.Document.BeginAtomicUndo ();
					int caretPos = data.Caret.Offset;
					if (data.IsSomethingSelected && data.MainSelection.SelectionMode == SelectionMode.Block) {
						data.Caret.PreserveSelection = true;
						if (!data.MainSelection.IsDirty) {
							data.DeleteSelectedText (false);
							data.MainSelection.IsDirty = true;
						}
						int textLength = 0;
						int column = data.Caret.Column;
						int minLine = data.MainSelection.MinLine;
						int maxLine = data.MainSelection.MaxLine;
						for (int lineNumber = minLine; lineNumber <= maxLine; lineNumber++) {
							int offset = data.Document.GetLine (lineNumber).Offset + column;
							textLength = data.Insert (offset, text);
							data.PasteText (offset, text);
						}
						
						data.Caret.Offset += textLength;
						data.MainSelection.Anchor = new DocumentLocation (data.Caret.Line == minLine ? maxLine : minLine, data.Caret.Column - textLength);
						data.MainSelection.Lead = new DocumentLocation (data.Caret.Line, data.Caret.Column);
						data.Caret.PreserveSelection = false;
						data.Document.CommitMultipleLineUpdate (data.MainSelection.MinLine, data.MainSelection.MaxLine);
					} else {
						ISegment selection = data.SelectionRange;
						if (preserveSelection && data.IsSomethingSelected)
							data.DeleteSelectedText ();
						data.Caret.PreserveSelection = true;
						//int oldLine = data.Caret.Line;
						int textLength = data.Insert (insertionOffset, text);
						result = textLength;
	
						if (data.IsSomethingSelected && data.SelectionRange.Offset >= insertionOffset)
							data.SelectionRange.Offset += textLength;
						if (data.IsSomethingSelected && data.MainSelection.GetAnchorOffset (data) >= insertionOffset)
							data.MainSelection.Anchor = data.Document.OffsetToLocation (data.MainSelection.GetAnchorOffset (data) + textLength);
						
						data.Caret.PreserveSelection = false;
						if (!preserveState) {
							data.Caret.Offset += textLength;
						} else {
							if (caretPos >= insertionOffset)
								data.Caret.Offset += textLength;
							if (selection != null) {
								int offset = selection.Offset;
								if (offset >= insertionOffset)
									offset += textLength;
								data.SelectionRange = new Segment (offset, selection.Length);
							}
						}
						data.PasteText (insertionOffset, text);
					}
					data.Document.EndAtomicUndo ();
				});
			}
			
			return result;
		}
 private void OnPasteButtonClick(object obj, EventArgs args)
 {
     _destination = _folder.Filename;
     _clipBoard.RequestContents(Gdk.Atom.Intern("x-special/gnome-copied-files", false), ReceivedFunc);
 }