private GetDeletableSegments ( ISegment segment ) : ISegment[] | ||
segment | ISegment | |
리턴 | ISegment[] |
void ReplaceSingleLineText(TextArea textArea, SelectionSegment lineSegment, string newText, out int insertionLength) { if (lineSegment.Length == 0) { if (newText.Length > 0 && textArea.ReadOnlySectionProvider.CanInsert(lineSegment.StartOffset)) { newText = AddSpacesIfRequired(newText, new TextViewPosition(document.GetLocation(lineSegment.StartOffset), lineSegment.StartVisualColumn)); textArea.Document.Insert(lineSegment.StartOffset, newText); } } else { ISegment[] segmentsToDelete = textArea.GetDeletableSegments(lineSegment); for (int i = segmentsToDelete.Length - 1; i >= 0; i--) { if (i == segmentsToDelete.Length - 1) { if (segmentsToDelete[i].Offset == SurroundingSegment.Offset && segmentsToDelete[i].Length == SurroundingSegment.Length) { newText = AddSpacesIfRequired(newText, new TextViewPosition(document.GetLocation(lineSegment.StartOffset), lineSegment.StartVisualColumn)); } textArea.Document.Replace(segmentsToDelete[i], newText); } else { textArea.Document.Remove(segmentsToDelete[i]); } } } insertionLength = newText.Length; }
static void OnCut(object target, ExecutedRoutedEventArgs args) { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { if (textArea.Selection.IsEmpty && textArea.Options.CutCopyWholeLine) { DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line); if (CopyWholeLine(textArea, currentLine)) { ISegment[] segmentsToDelete = textArea.GetDeletableSegments(new SimpleSegment(currentLine.Offset, currentLine.TotalLength)); for (int i = segmentsToDelete.Length - 1; i >= 0; i--) { textArea.Document.Remove(segmentsToDelete[i]); } } } else { if (CopySelectedText(textArea)) { textArea.RemoveSelectedText(); } } textArea.Caret.BringCaretToView(); args.Handled = true; } }
static void ReplaceSingleLineText(TextArea textArea, ISegment lineSegment, string newText) { if (lineSegment.Length == 0) { if (newText.Length > 0 && textArea.ReadOnlySectionProvider.CanInsert(lineSegment.Offset)) { textArea.Document.Insert(lineSegment.Offset, newText); } } else { ISegment[] segmentsToDelete = textArea.GetDeletableSegments(lineSegment); for (int i = segmentsToDelete.Length - 1; i >= 0; i--) { if (i == segmentsToDelete.Length - 1) { textArea.Document.Replace(segmentsToDelete[i], newText); } else { textArea.Document.Remove(segmentsToDelete[i]); } } } }
/// <inheritdoc/> public override void ReplaceSelectionWithText(TextArea textArea, string newText) { if (textArea == null) throw new ArgumentNullException("textArea"); if (newText == null) throw new ArgumentNullException("newText"); using (textArea.Document.RunUpdate()) { if (IsEmpty) { if (newText.Length > 0) { if (textArea.ReadOnlySectionProvider.CanInsert(textArea.Caret.Offset)) { textArea.Document.Insert(textArea.Caret.Offset, newText); } } } else { ISegment[] segmentsToDelete = textArea.GetDeletableSegments(this); for (int i = segmentsToDelete.Length - 1; i >= 0; i--) { if (i == segmentsToDelete.Length - 1) { textArea.Caret.Offset = segmentsToDelete[i].EndOffset; textArea.Document.Replace(segmentsToDelete[i], newText); } else { textArea.Document.Remove(segmentsToDelete[i]); } } if (segmentsToDelete.Length != 0) { textArea.Selection = Selection.Empty; } } } }
static ExecutedRoutedEventHandler OnDelete(RoutedUICommand selectingCommand) { return((target, args) => { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { // call BeginUpdate before running the 'selectingCommand' // so that undoing the delete does not select the deleted character using (textArea.Document.RunUpdate()) { if (textArea.Selection.IsEmpty) { TextViewPosition oldCaretPosition = textArea.Caret.Position; if (textArea.Caret.IsInVirtualSpace && selectingCommand == EditingCommands.SelectRightByCharacter) { EditingCommands.SelectRightByWord.Execute(args.Parameter, textArea); } else { selectingCommand.Execute(args.Parameter, textArea); } bool hasSomethingDeletable = false; foreach (ISegment s in textArea.Selection.Segments) { if (textArea.GetDeletableSegments(s).Length > 0) { hasSomethingDeletable = true; break; } } if (!hasSomethingDeletable) { // If nothing in the selection is deletable; then reset caret+selection // to the previous value. This prevents the caret from moving through read-only sections. textArea.Caret.Position = oldCaretPosition; textArea.ClearSelection(); } } textArea.RemoveSelectedText(); } textArea.Caret.BringCaretToView(); args.Handled = true; } }); }
/// <summary> /// Calls transformLine on all writable segment in the selected range. /// </summary> static void TransformSelectedSegments(Action <TextArea, ISegment> transformSegment, object target, ExecutedRoutedEventArgs args, DefaultSegmentType defaultSegmentType) { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { using (textArea.Document.RunUpdate()) { IEnumerable <ISegment> segments; if (textArea.Selection.IsEmpty) { if (defaultSegmentType == DefaultSegmentType.CurrentLine) { segments = new ISegment[] { textArea.Document.GetLineByNumber(textArea.Caret.Line) }; } else if (defaultSegmentType == DefaultSegmentType.WholeDocument) { segments = textArea.Document.Lines.Cast <ISegment>(); } else { segments = null; } } else { segments = textArea.Selection.Segments.Cast <ISegment>(); } if (segments != null) { foreach (ISegment segment in segments.Reverse()) { foreach (ISegment writableSegment in textArea.GetDeletableSegments(segment).Reverse()) { transformSegment(textArea, writableSegment); } } } } textArea.Caret.BringCaretToView(); args.Handled = true; } }
/// <inheritdoc/> public override void ReplaceSelectionWithText(TextArea textArea, string newText) { if (textArea == null) { throw new ArgumentNullException("textArea"); } if (newText == null) { throw new ArgumentNullException("newText"); } using (textArea.Document.RunUpdate()) { if (IsEmpty) { if (newText.Length > 0) { if (textArea.ReadOnlySectionProvider.CanInsert(textArea.Caret.Offset)) { textArea.Document.Insert(textArea.Caret.Offset, newText); } } } else { ISegment[] segmentsToDelete = textArea.GetDeletableSegments(this); for (int i = segmentsToDelete.Length - 1; i >= 0; i--) { if (i == segmentsToDelete.Length - 1) { textArea.Caret.Offset = segmentsToDelete[i].EndOffset; textArea.Document.Replace(segmentsToDelete[i], newText); } else { textArea.Document.Remove(segmentsToDelete[i]); } } if (segmentsToDelete.Length != 0) { textArea.Selection = Selection.Empty; } } } }
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; } } 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(); } } } }
private void ReplaceSingleLineText(TextArea textArea, SelectionSegment lineSegment, string newText, out int insertionLength) { if (lineSegment.Length == 0) { if (newText.Length > 0 && textArea.ReadOnlySectionProvider.CanInsert(lineSegment.StartOffset)) { newText = AddSpacesIfRequired(newText, new TextViewPosition(document.GetLocation(lineSegment.StartOffset), lineSegment.StartVisualColumn), new TextViewPosition(document.GetLocation(lineSegment.EndOffset), lineSegment.EndVisualColumn)); textArea.Document.Insert(lineSegment.StartOffset, newText); } } else { ISegment[] segmentsToDelete = textArea.GetDeletableSegments(lineSegment); for (int i = segmentsToDelete.Length - 1; i >= 0; i--) { if (i == segmentsToDelete.Length - 1) { if (segmentsToDelete[i].Offset == SurroundingSegment.Offset && segmentsToDelete[i].Length == SurroundingSegment.Length) { newText = AddSpacesIfRequired(newText, new TextViewPosition(document.GetLocation(lineSegment.StartOffset), lineSegment.StartVisualColumn), new TextViewPosition(document.GetLocation(lineSegment.EndOffset), lineSegment.EndVisualColumn)); } textArea.Document.Replace(segmentsToDelete[i], newText); } else { textArea.Document.Remove(segmentsToDelete[i]); } } } insertionLength = newText.Length; }
static void ReplaceSingleLineText(TextArea textArea, ISegment lineSegment, string newText) { if (lineSegment.Length == 0) { if (newText.Length > 0 && textArea.ReadOnlySectionProvider.CanInsert(lineSegment.Offset)) { textArea.Document.Insert(lineSegment.Offset, newText); } } else { ISegment[] segmentsToDelete = textArea.GetDeletableSegments(lineSegment); for (int i = segmentsToDelete.Length - 1; i >= 0; i--) { if (i == segmentsToDelete.Length - 1) { textArea.Document.Replace(segmentsToDelete[i], newText); } else { textArea.Document.Remove(segmentsToDelete[i]); } } } }