/// <summary> /// Creates a new TextAreaDefaultInputHandler instance. /// </summary> public TextAreaDefaultInputHandler(TextArea textArea) : base(textArea) { this.NestedInputHandlers.Add(CaretNavigation = CaretNavigationCommandHandler.Create(textArea)); this.NestedInputHandlers.Add(Editing = EditingCommandHandler.Create(textArea)); this.NestedInputHandlers.Add(MouseSelection = new SelectionMouseHandler(textArea)); this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo, ExecuteUndo, CanExecuteUndo)); this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Redo, ExecuteRedo, CanExecuteRedo)); }
static ExecutedRoutedEventHandler OnDelete(CaretMovementType caretMovement) { return((target, args) => { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { if (textArea.Selection.IsEmpty) { TextViewPosition startPos = textArea.Caret.Position; bool enableVirtualSpace = textArea.Options.EnableVirtualSpace; // When pressing delete; don't move the caret further into virtual space - instead delete the newline if (caretMovement == CaretMovementType.CharRight) { enableVirtualSpace = false; } double desiredXPos = textArea.Caret.DesiredXPos; TextViewPosition endPos = CaretNavigationCommandHandler.GetNewCaretPosition( textArea.TextView, startPos, caretMovement, enableVirtualSpace, ref desiredXPos); // GetNewCaretPosition may return (0,0) as new position, // thus we need to validate endPos before using it in the selection. if (endPos.Line < 1 || endPos.Column < 1) { endPos = new TextViewPosition(Math.Max(endPos.Line, 1), Math.Max(endPos.Column, 1)); } // Don't do anything if the number of lines of a rectangular selection would be changed by the deletion. if (textArea.Selection is RectangleSelection && startPos.Line != endPos.Line) { return; } // Don't select the text to be deleted; just reuse the ReplaceSelectionWithText logic // Reuse the existing selection, so that we continue using the same logic textArea.Selection.StartSelectionOrSetEndpoint(startPos, endPos) .ReplaceSelectionWithText(string.Empty); } else { textArea.RemoveSelectedText(); } textArea.Caret.BringCaretToView(); args.Handled = true; } }); }
static ExecutedRoutedEventHandler OnDelete(CaretMovementType caretMovement) { return((target, args) => { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { if (textArea.Selection.IsEmpty) { TextViewPosition startPos = textArea.Caret.Position; bool enableVirtualSpace = textArea.Options.EnableVirtualSpace; // When pressing delete; don't move the caret further into virtual space - instead delete the newline if (caretMovement == CaretMovementType.CharRight) { enableVirtualSpace = false; } double desiredXPos = textArea.Caret.DesiredXPos; TextViewPosition endPos = CaretNavigationCommandHandler.GetNewCaretPosition( textArea.TextView, startPos, caretMovement, enableVirtualSpace, ref desiredXPos); // GetNewCaretPosition may return (0,0) as new position, // thus we need to validate endPos before using it in the selection. if (endPos.Line < 1 || endPos.Column < 1) { endPos = new TextViewPosition(Math.Max(endPos.Line, 1), Math.Max(endPos.Column, 1)); } // Don't select the text to be deleted; just reuse the ReplaceSelectionWithText logic var sel = new SimpleSelection(textArea, startPos, endPos); sel.ReplaceSelectionWithText(string.Empty); } else { textArea.RemoveSelectedText(); } textArea.Caret.BringCaretToView(); args.Handled = true; } }); }
// Make life easier for text editor extensions that use a different cursor based on the pressed modifier keys. /// <inheritdoc/> protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); // [DIGITALRUNE] Scroll up/down by one line with Ctrl-Up/Down. if (!e.Handled && Keyboard.Modifiers == ModifierKeys.Control) { if (e.Key == Key.Up) { scrollInfo.LineUp(); // Keep caret within the visible area. if (TextView.VerticalOffset + TextView.ActualHeight - TextView.DefaultLineHeight < TextView.GetVisualTopByDocumentLine(Caret.Line)) { ClearSelection(); CaretNavigationCommandHandler.MoveCaret(this, CaretMovementType.LineUp); } e.Handled = true; } else if (e.Key == Key.Down) { scrollInfo.LineDown(); // Keep caret within the visible area. if (TextView.VerticalOffset > TextView.GetVisualTopByDocumentLine(Caret.Line)) { ClearSelection(); CaretNavigationCommandHandler.MoveCaret(this, CaretMovementType.LineDown); } e.Handled = true; } } TextView.InvalidateCursorIfMouseWithinTextView(); }
private static ExecutedRoutedEventHandler OnDelete(CaretMovementType caretMovement) { return((target, args) => { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { if (textArea.Selection.IsEmpty) { TextViewPosition startPos = textArea.Caret.Position; bool enableVirtualSpace = textArea.Options.EnableVirtualSpace; // When pressing delete; don't move the caret further into virtual space - instead delete the newline if (caretMovement == CaretMovementType.CharRight) { enableVirtualSpace = false; } double desiredXPos = textArea.Caret.DesiredXPos; TextViewPosition endPos = CaretNavigationCommandHandler.GetNewCaretPosition( textArea.TextView, startPos, caretMovement, enableVirtualSpace, ref desiredXPos); // GetNewCaretPosition may return (0,0) as new position, // thus we need to validate endPos before using it in the selection. if (endPos.Line < 1 || endPos.Column < 1) { endPos = new TextViewPosition(Math.Max(endPos.Line, 1), Math.Max(endPos.Column, 1)); } // Don't select the text to be deleted; just reuse the ReplaceSelectionWithText logic if (endPos.Column == 1) { var a = textArea.Document.GetLineByNumber(textArea.Caret.Line); var b = a.PreviousLine; if (b != null) { var c = b.PreviousLine; if (b.obs != null) { var obs = c.obs; var sa = textArea.Document.Text.Substring(a.Offset, a.Length + a.DelimiterLength); var bs = textArea.Document.Text.Substring(b.Offset, b.Length + b.DelimiterLength); var cs = textArea.Document.Text.Substring(c.Offset, c.Length + c.DelimiterLength); textArea.Document.Remove(c.Offset + c.Length, bs.Length + sa.Length /* + c.DelimiterLength*//* + a.DelimiterLength + b.DelimiterLength*/); textArea.Document.Insert(c.Offset + c.Length, sa); } else { var sel = new SimpleSelection(textArea, startPos, endPos); sel.ReplaceSelectionWithText(string.Empty); } } else { var sel = new SimpleSelection(textArea, startPos, endPos); sel.ReplaceSelectionWithText(string.Empty); } } else { var sel = new SimpleSelection(textArea, startPos, endPos); sel.ReplaceSelectionWithText(string.Empty); } } else { textArea.RemoveSelectedText(); } } textArea.Caret.BringCaretToView(); args.Handled = true; }); }