/// <summary> /// Creates a new AnchorElement. /// </summary> public AnchorElement(AnchorSegment segment, string text, string name, InsertionContext context) { this.segment = segment; this.context = context; this.Text = text; this.Name = name; }
public BoundActiveElement(InsertionContext context, SnippetReplaceableTextElement targetSnippetElement, SnippetBoundElement boundElement, AnchorSegment segment) { this.context = context; this.targetSnippetElement = targetSnippetElement; this.boundElement = boundElement; this.segment = segment; }
/// <inheritdoc /> public override void Insert(InsertionContext context) { TextAnchor start = context.Document.CreateAnchor(context.InsertionPosition); start.MovementType = AnchorMovementType.BeforeInsertion; start.SurviveDeletion = true; AnchorSegment segment = new AnchorSegment(start, start); context.RegisterActiveElement(this, new AnchorElement(segment, Name, context)); }
/// <inheritdoc/> protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) { if (selecting) { selecting = false; selectionStart = null; ReleaseMouseCapture(); e.Handled = true; } base.OnMouseLeftButtonUp(e); }
protected override void OnPointerReleased(PointerReleasedEventArgs e) { if (_selecting) { _selecting = false; _selectionStart = null; e.Device.Capture(null); e.Handled = true; } base.OnPointerReleased(e); }
/// <inheritdoc/> public override void Insert(InsertionContext context) { if (targetElement != null) { int start = context.InsertionPosition; string inputText = targetElement.Text; if (inputText != null) { context.InsertText(ConvertText(inputText)); } int end = context.InsertionPosition; AnchorSegment segment = new AnchorSegment(context.Document, start, end - start); context.RegisterActiveElement(this, new BoundActiveElement(context, targetElement, this, segment)); } }
/// <inheritdoc/> public override void Insert(InsertionContext context) { if (targetElement != null) { TextAnchor start = context.Document.CreateAnchor(context.InsertionPosition); start.MovementType = AnchorMovementType.BeforeInsertion; start.SurviveDeletion = true; string inputText = targetElement.Text; if (inputText != null) { context.InsertText(ConvertText(inputText)); } TextAnchor end = context.Document.CreateAnchor(context.InsertionPosition); end.MovementType = AnchorMovementType.BeforeInsertion; end.SurviveDeletion = true; AnchorSegment segment = new AnchorSegment(start, end); context.RegisterActiveElement(this, new BoundActiveElement(context, targetElement, this, segment)); } }
public void RaiseInsertionCompleted(EventArgs e) { if (currentStatus != Status.Insertion) { throw new InvalidOperationException(); } if (e == null) { e = EventArgs.Empty; } currentStatus = Status.RaisingInsertionCompleted; int endPosition = this.InsertionPosition; this.wholeSnippetAnchor = new AnchorSegment(Document, startPosition, endPosition - startPosition); TextDocumentWeakEventManager.UpdateFinished.AddListener(Document, this); deactivateIfSnippetEmpty = (endPosition != startPosition); foreach (IActiveElement element in registeredElements) { element.OnInsertionCompleted(); } if (InsertionCompleted != null) { InsertionCompleted(this, e); } currentStatus = Status.Interactive; if (registeredElements.Count == 0) { // deactivate immediately if there are no interactive elements Deactivate(new SnippetEventArgs(DeactivateReason.NoActiveElements)); } else { myInputHandler = new SnippetInputHandler(this); // disable existing snippet input handlers - there can be only 1 active snippet foreach (TextAreaStackedInputHandler h in TextArea.StackedInputHandlers) { if (h is SnippetInputHandler) { TextArea.PopStackedInputHandler(h); } } TextArea.PushStackedInputHandler(myInputHandler); } }
void targetElement_TextChanged(object sender, EventArgs e) { // Don't copy text if the segments overlap (we would get an endless loop). // This can happen if the user deletes the text between the replaceable element and the bound element. if (segment.GetOverlap(targetElement.Segment) == SimpleSegment.Invalid) { int offset = segment.Offset; int length = segment.Length; string text = boundElement.ConvertText(targetElement.Text); context.Document.Replace(offset, length, text); if (length == 0) { // replacing an empty anchor segment with text won't enlarge it, so we have to recreate it segment = new AnchorSegment(context.Document, offset, text.Length); } } }
void targetElement_TextChanged(object sender, EventArgs e) { // Don't copy text if the segments overlap (we would get an endless loop). // This can happen if the user deletes the text between the replaceable element and the bound element. if (segment.GetOverlap(targetElement.Segment) == SimpleSegment.Invalid) { int offset = segment.Offset; int length = segment.Length; string text = boundElement.ConvertText(targetElement.Text); if (length != text.Length || text != context.Document.GetText(offset, length)) { // Call replace only if we're actually changing something. // Without this check, we would generate an empty undo group when the user pressed undo. context.Document.Replace(offset, length, text); if (length == 0) { // replacing an empty anchor segment with text won't enlarge it, so we have to recreate it segment = new AnchorSegment(context.Document, offset, text.Length); } } } }
/// <inheritdoc/> protected void OnMouseLeftButtonDowns(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); if (!e.Handled && TextView != null && textArea != null) { e.Handled = true; textArea.Focus(); SimpleSegment currentSeg = GetTextLineSegment(e); if (currentSeg == SimpleSegment.Invalid) { return; } textArea.Caret.Offset = currentSeg.Offset + currentSeg.Length; int i = 0; while (Char.IsWhiteSpace(textArea.Document.Text[currentSeg.Offset + i]) && i < currentSeg.Length) { i++; } if (CaptureMouse()) { selecting = true; selectionStart = new AnchorSegment(Document, currentSeg.Offset + i, currentSeg.Length - i); if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) { SimpleSelection simpleSelection = textArea.Selection as SimpleSelection; if (simpleSelection != null) { selectionStart = new AnchorSegment(Document, simpleSelection.SurroundingSegment); } } textArea.Selection = Selection.Create(textArea, selectionStart); if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) { ExtendSelection(currentSeg); } textArea.Caret.BringCaretToView(0.0); } } }
protected override void OnPointerPressed(PointerPressedEventArgs e) { base.OnPointerPressed(e); if (!e.Handled && e.MouseButton == MouseButton.Left && TextView != null && TextArea != null) { e.Handled = true; TextArea.Focus(); var currentSeg = GetTextLineSegment(e); if (currentSeg == SimpleSegment.Invalid) { return; } TextArea.Caret.Offset = currentSeg.Offset + currentSeg.Length; e.Device.Capture(this); if (e.Device.Captured == this) { _selecting = true; _selectionStart = new AnchorSegment(Document, currentSeg.Offset, currentSeg.Length); if (e.InputModifiers.HasFlag(InputModifiers.Shift)) { if (TextArea.Selection is SimpleSelection simpleSelection) { _selectionStart = new AnchorSegment(Document, simpleSelection.SurroundingSegment); } } TextArea.Selection = Selection.Create(TextArea, _selectionStart); if (e.InputModifiers.HasFlag(InputModifiers.Shift)) { ExtendSelection(currentSeg); } TextArea.Caret.BringCaretToView(5.0); } } }
/// <inheritdoc/> protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); if (!e.Handled && TextView != null && textArea != null) { e.Handled = true; textArea.Focus(); SimpleSegment currentSeg = GetTextLineSegment(e); if (currentSeg == SimpleSegment.Invalid) return; textArea.Caret.Offset = currentSeg.Offset + currentSeg.Length; if (CaptureMouse()) { selecting = true; selectionStart = new AnchorSegment(Document, currentSeg.Offset, currentSeg.Length); if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) { SimpleSelection simpleSelection = textArea.Selection as SimpleSelection; if (simpleSelection != null) selectionStart = new AnchorSegment(Document, simpleSelection); } textArea.Selection = new SimpleSelection(selectionStart); if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) { ExtendSelection(currentSeg); } } } }
void textArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { mode = SelectionMode.None; if (!e.Handled && e.ChangedButton == MouseButton.Left) { ModifierKeys modifiers = Keyboard.Modifiers; bool shift = (modifiers & ModifierKeys.Shift) == ModifierKeys.Shift; if (enableTextDragDrop && e.ClickCount == 1 && !shift) { int visualColumn; int offset = GetOffsetFromMousePosition(e, out visualColumn); if (textArea.Selection.Contains(offset)) { if (textArea.CaptureMouse()) { mode = SelectionMode.PossibleDragStart; possibleDragStartMousePos = e.GetPosition(textArea); } e.Handled = true; return; } } var oldPosition = textArea.Caret.Position; SetCaretOffsetToMousePosition(e); if (!shift) { textArea.ClearSelection(); } if (textArea.CaptureMouse()) { if ((modifiers & ModifierKeys.Alt) == ModifierKeys.Alt && textArea.Options.EnableRectangularSelection) { mode = SelectionMode.Rectangular; if (shift && textArea.Selection is RectangleSelection) { textArea.Selection = textArea.Selection.StartSelectionOrSetEndpoint(oldPosition, textArea.Caret.Position); } } else if (e.ClickCount == 1 && ((modifiers & ModifierKeys.Control) == 0)) { mode = SelectionMode.Normal; if (shift && !(textArea.Selection is RectangleSelection)) { textArea.Selection = textArea.Selection.StartSelectionOrSetEndpoint(oldPosition, textArea.Caret.Position); } } else { SimpleSegment startWord; if (e.ClickCount == 3) { mode = SelectionMode.WholeLine; startWord = GetLineAtMousePosition(e); } else { mode = SelectionMode.WholeWord; startWord = GetWordAtMousePosition(e); } if (startWord == SimpleSegment.Invalid) { mode = SelectionMode.None; textArea.ReleaseMouseCapture(); return; } if (shift && !textArea.Selection.IsEmpty) { if (startWord.Offset < textArea.Selection.SurroundingSegment.Offset) { textArea.Selection = textArea.Selection.SetEndpoint(new TextViewPosition(textArea.Document.GetLocation(startWord.Offset))); } else if (startWord.EndOffset > textArea.Selection.SurroundingSegment.EndOffset) { textArea.Selection = textArea.Selection.SetEndpoint(new TextViewPosition(textArea.Document.GetLocation(startWord.EndOffset))); } this.startWord = new AnchorSegment(textArea.Document, textArea.Selection.SurroundingSegment); } else { textArea.Selection = Selection.Create(textArea, startWord.Offset, startWord.EndOffset); this.startWord = new AnchorSegment(textArea.Document, startWord.Offset, startWord.Length); } } } } e.Handled = true; }
void textArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { mode = SelectionMode.None; if (!e.Handled && e.ChangedButton == MouseButton.Left) { ModifierKeys modifiers = Keyboard.Modifiers; bool shift = (modifiers & ModifierKeys.Shift) == ModifierKeys.Shift; if (enableTextDragDrop && e.ClickCount == 1 && !shift) { int visualColumn; bool isAtEndOfLine; int offset = GetOffsetFromMousePosition(e, out visualColumn, out isAtEndOfLine); if (textArea.Selection.Contains(offset)) { if (textArea.CaptureMouse()) { mode = SelectionMode.PossibleDragStart; possibleDragStartMousePos = e.GetPosition(textArea); } e.Handled = true; return; } } var oldPosition = textArea.Caret.Position; SetCaretOffsetToMousePosition(e); if (!shift) { textArea.ClearSelection(); } if (textArea.CaptureMouse()) { if ((modifiers & ModifierKeys.Alt) == ModifierKeys.Alt && textArea.Options.EnableRectangularSelection) { mode = SelectionMode.Rectangular; if (shift && textArea.Selection is RectangleSelection) { textArea.Selection = textArea.Selection.StartSelectionOrSetEndpoint(oldPosition, textArea.Caret.Position); } } else if (e.ClickCount == 1 && ((modifiers & ModifierKeys.Control) == 0)) { mode = SelectionMode.Normal; if (shift && !(textArea.Selection is RectangleSelection)) { textArea.Selection = textArea.Selection.StartSelectionOrSetEndpoint(oldPosition, textArea.Caret.Position); } } else { SimpleSegment startWord; if (e.ClickCount == 3) { mode = SelectionMode.WholeLine; startWord = GetLineAtMousePosition(e); } else { mode = SelectionMode.WholeWord; startWord = GetWordAtMousePosition(e); } if (startWord == SimpleSegment.Invalid) { mode = SelectionMode.None; textArea.ReleaseMouseCapture(); return; } if (shift && !textArea.Selection.IsEmpty) { if (startWord.Offset < textArea.Selection.SurroundingSegment.Offset) { textArea.Selection = textArea.Selection.SetEndpoint(new TextViewPosition(textArea.Document.GetLocation(startWord.Offset))); } else if (startWord.EndOffset > textArea.Selection.SurroundingSegment.EndOffset) { textArea.Selection = textArea.Selection.SetEndpoint(new TextViewPosition(textArea.Document.GetLocation(startWord.EndOffset))); } this.startWord = new AnchorSegment(textArea.Document, textArea.Selection.SurroundingSegment); } else { textArea.Selection = Selection.Create(textArea, startWord.Offset, startWord.EndOffset); this.startWord = new AnchorSegment(textArea.Document, startWord.Offset, startWord.Length); } } } } e.Handled = true; }
//[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] //void textArea_DragEnter(object sender, DragEventArgs e) //{ // try { // e.Effects = GetEffect(e); // textArea.Caret.Show(); // } catch (Exception ex) { // OnDragException(ex); // } //} //[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] //void textArea_DragOver(object sender, DragEventArgs e) //{ // try { // e.Effects = GetEffect(e); // } catch (Exception ex) { // OnDragException(ex); // } //} //DragDropEffects GetEffect(DragEventArgs e) //{ // if (e.Data.GetDataPresent(DataFormats.UnicodeText, true)) { // e.Handled = true; // int visualColumn; // bool isAtEndOfLine; // int offset = GetOffsetFromMousePosition(e.GetPosition(textArea.TextView), out visualColumn, out isAtEndOfLine); // if (offset >= 0) { // textArea.Caret.Position = new TextViewPosition(textArea.Document.GetLocation(offset), visualColumn) { IsAtEndOfLine = isAtEndOfLine }; // textArea.Caret.DesiredXPos = double.NaN; // if (textArea.ReadOnlySectionProvider.CanInsert(offset)) { // if ((e.AllowedEffects & DragDropEffects.Move) == DragDropEffects.Move // && (e.KeyStates & DragDropKeyStates.ControlKey) != DragDropKeyStates.ControlKey) // { // return DragDropEffects.Move; // } else { // return e.AllowedEffects & DragDropEffects.Copy; // } // } // } // } // return DragDropEffects.None; //} //[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] //void textArea_DragLeave(object sender, DragEventArgs e) //{ // try { // e.Handled = true; // if (!textArea.IsKeyboardFocusWithin) // textArea.Caret.Hide(); // } catch (Exception ex) { // OnDragException(ex); // } //} //[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] //void textArea_Drop(object sender, DragEventArgs e) //{ // try { // DragDropEffects effect = GetEffect(e); // e.Effects = effect; // if (effect != DragDropEffects.None) { // int start = textArea.Caret.Offset; // if (mode == SelectionMode.Drag && textArea.Selection.Contains(start)) { // Debug.WriteLine("Drop: did not drop: drop target is inside selection"); // e.Effects = DragDropEffects.None; // } else { // Debug.WriteLine("Drop: insert at " + start); // var pastingEventArgs = new DataObjectPastingEventArgs(e.Data, true, DataFormats.UnicodeText); // textArea.RaiseEvent(pastingEventArgs); // if (pastingEventArgs.CommandCancelled) // return; // string text = EditingCommandHandler.GetTextToPaste(pastingEventArgs, textArea); // if (text == null) // return; // bool rectangular = pastingEventArgs.DataObject.GetDataPresent(RectangleSelection.RectangularSelectionDataType); // // Mark the undo group with the currentDragDescriptor, if the drag // // is originating from the same control. This allows combining // // the undo groups when text is moved. // textArea.Document.UndoStack.StartUndoGroup(this.currentDragDescriptor); // try { // if (rectangular && RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Position, text, true)) { // } else { // textArea.Document.Insert(start, text); // textArea.Selection = Selection.Create(textArea, start, start + text.Length); // } // } finally { // textArea.Document.UndoStack.EndUndoGroup(); // } // } // e.Handled = true; // } // } catch (Exception ex) { // OnDragException(ex); // } //} //void OnDragException(Exception ex) //{ // // swallows exceptions during drag'n'drop or reports them incorrectly, so // // we re-throw them later to allow the application's unhandled exception handler // // to catch them // textArea.Dispatcher.BeginInvoke( // DispatcherPriority.Send, // new Action(delegate { // throw new DragDropException("Exception during drag'n'drop", ex); // })); //} //[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] //void textArea_GiveFeedback(object sender, GiveFeedbackEventArgs e) //{ // try { // e.UseDefaultCursors = true; // e.Handled = true; // } catch (Exception ex) { // OnDragException(ex); // } //} //[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] //void textArea_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) //{ // try { // if (e.EscapePressed) { // e.Action = DragAction.Cancel; // } else if ((e.KeyStates & DragDropKeyStates.LeftMouseButton) != DragDropKeyStates.LeftMouseButton) { // e.Action = DragAction.Drop; // } else { // e.Action = DragAction.Continue; // } // e.Handled = true; // } catch (Exception ex) { // OnDragException(ex); // } //} #endregion #region Start Drag //object currentDragDescriptor; //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; // } // } // var copyingEventArgs = new DataObjectCopyingEventArgs(dataObject, true); // textArea.RaiseEvent(copyingEventArgs); // if (copyingEventArgs.CommandCancelled) // return; // 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(); // } // } //} #endregion #region QueryCursor // provide the IBeam Cursor for the text area //void textArea_QueryCursor(object sender, QueryCursorEventArgs e) //{ // if (!e.Handled) { // if (mode != SelectionMode.None) { // // during selection, use IBeam cursor even outside the text area // e.Cursor = Cursors.IBeam; // e.Handled = true; // } else if (textArea.TextView.VisualLinesValid) { // // Only query the cursor if the visual lines are valid. // // If they are invalid, the cursor will get re-queried when the visual lines // // get refreshed. // Point p = e.GetPosition(textArea.TextView); // if (p.X >= 0 && p.Y >= 0 && p.X <= textArea.TextView.ActualWidth && p.Y <= textArea.TextView.ActualHeight) { // int visualColumn; // bool isAtEndOfLine; // int offset = GetOffsetFromMousePosition(e, out visualColumn, out isAtEndOfLine); // if (enableTextDragDrop && textArea.Selection.Contains(offset)) // e.Cursor = Cursors.Arrow; // else // e.Cursor = Cursors.IBeam; // e.Handled = true; // } // } // } //} #endregion #region LeftButtonDown private void TextArea_MouseLeftButtonDown(object sender, PointerPressedEventArgs e) { _mode = SelectionMode.None; if (!e.Handled && e.MouseButton == MouseButton.Left) { var modifiers = e.InputModifiers; var shift = modifiers.HasFlag(InputModifiers.Shift); if (_enableTextDragDrop && e.ClickCount == 1 && !shift) { var offset = GetOffsetFromMousePosition(e, out _, out _); if (TextArea.Selection.Contains(offset)) { if (TextArea.CapturePointer(e.Device)) { _mode = SelectionMode.PossibleDragStart; _possibleDragStartMousePos = e.GetPosition(TextArea); } e.Handled = true; return; } } var oldPosition = TextArea.Caret.Position; SetCaretOffsetToMousePosition(e); if (!shift) { TextArea.ClearSelection(); } if (TextArea.CapturePointer(e.Device)) { if (modifiers.HasFlag(InputModifiers.Alt) && TextArea.Options.EnableRectangularSelection) { _mode = SelectionMode.Rectangular; if (shift && TextArea.Selection is RectangleSelection) { TextArea.Selection = TextArea.Selection.StartSelectionOrSetEndpoint(oldPosition, TextArea.Caret.Position); } } else if (e.ClickCount == 1 && modifiers.HasFlag(InputModifiers.Control)) { _mode = SelectionMode.Normal; if (shift && !(TextArea.Selection is RectangleSelection)) { TextArea.Selection = TextArea.Selection.StartSelectionOrSetEndpoint(oldPosition, TextArea.Caret.Position); } } else { SimpleSegment startWord; if (e.ClickCount == 3) { _mode = SelectionMode.WholeLine; startWord = GetLineAtMousePosition(e); } else { _mode = SelectionMode.WholeWord; startWord = GetWordAtMousePosition(e); } if (startWord == SimpleSegment.Invalid) { _mode = SelectionMode.None; TextArea.ReleasePointerCapture(e.Device); return; } if (shift && !TextArea.Selection.IsEmpty) { if (startWord.Offset < TextArea.Selection.SurroundingSegment.Offset) { TextArea.Selection = TextArea.Selection.SetEndpoint(new TextViewPosition(TextArea.Document.GetLocation(startWord.Offset))); } else if (startWord.EndOffset > TextArea.Selection.SurroundingSegment.EndOffset) { TextArea.Selection = TextArea.Selection.SetEndpoint(new TextViewPosition(TextArea.Document.GetLocation(startWord.EndOffset))); } _startWord = new AnchorSegment(TextArea.Document, TextArea.Selection.SurroundingSegment); } else { TextArea.Selection = Selection.Create(TextArea, startWord.Offset, startWord.EndOffset); _startWord = new AnchorSegment(TextArea.Document, startWord.Offset, startWord.Length); } } } } e.Handled = true; }
void textArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { mode = SelectionMode.None; if (!e.Handled && e.ChangedButton == MouseButton.Left) { bool shift = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift; if (AllowTextDragDrop && e.ClickCount == 1 && !shift) { int visualColumn; int offset = GetOffsetFromMousePosition(e, out visualColumn); if (textArea.Selection.Contains(offset)) { if (textArea.CaptureMouse()) { mode = SelectionMode.PossibleDragStart; possibleDragStartMousePos = e.GetPosition(textArea); } e.Handled = true; return; } } int oldOffset = textArea.Caret.Offset; SetCaretOffsetToMousePosition(e); if (!shift) { textArea.Selection = Selection.Empty; } if (textArea.CaptureMouse()) { if (e.ClickCount == 1) { mode = SelectionMode.Normal; if (shift) { textArea.Selection = textArea.Selection.StartSelectionOrSetEndpoint(oldOffset, textArea.Caret.Offset); } } else { mode = SelectionMode.WholeWord; var startWord = GetWordAtMousePosition(e); if (startWord == SimpleSegment.Invalid) { mode = SelectionMode.None; textArea.ReleaseMouseCapture(); return; } if (shift && !textArea.Selection.IsEmpty) { if (startWord.Offset < textArea.Selection.SurroundingSegment.Offset) { textArea.Selection = textArea.Selection.SetEndpoint(startWord.Offset); } else if (startWord.EndOffset > textArea.Selection.SurroundingSegment.EndOffset) { textArea.Selection = textArea.Selection.SetEndpoint(startWord.EndOffset); } this.startWord = new AnchorSegment(textArea.Document, textArea.Selection.SurroundingSegment); } else { textArea.Selection = new SimpleSelection(startWord.Offset, startWord.EndOffset); this.startWord = new AnchorSegment(textArea.Document, startWord.Offset, startWord.Length); } } } } e.Handled = true; }
/// <summary> /// Creates a new AnchorElement. /// </summary> public AnchorElement(AnchorSegment segment, string name, InsertionContext context) { _segment = segment; _context = context; Name = name; }
/// <summary> /// Creates a new AnchorElement. /// </summary> public AnchorElement(AnchorSegment segment, string name, InsertionContext context) { this.segment = segment; this.context = context; this.Name = name; }
void targetElement_TextChanged(object sender, EventArgs e) { // Don't copy text if the segments overlap (we would get an endless loop). // This can happen if the user deletes the text between the replaceable element and the bound element. if (SimpleSegment.GetOverlap(segment, targetElement.Segment) == SimpleSegment.Invalid) { int offset = segment.Offset; int length = segment.Length; string text = boundElement.ConvertText(targetElement.Text); if (length != text.Length || text != context.Document.GetText(offset, length)) { // Call replace only if we're actually changing something. // Without this check, we would generate an empty undo group when the user pressed undo. context.Document.Replace(offset, length, text); if (length == 0) { // replacing an empty anchor segment with text won't enlarge it, so we have to recreate it segment = new AnchorSegment(context.Document, offset, text.Length); } } } }
/// <inheritdoc /> public override void Insert(InsertionContext context) { int start = context.InsertionPosition; AnchorSegment segment = new AnchorSegment(context.Document, start, 0); context.RegisterActiveElement(this, new AnchorElement(segment, "", Name, context)); }
public void RaiseInsertionCompleted(EventArgs e) { if (currentStatus != Status.Insertion) throw new InvalidOperationException(); if (e == null) e = EventArgs.Empty; currentStatus = Status.RaisingInsertionCompleted; int endPosition = this.InsertionPosition; this.wholeSnippetAnchor = new AnchorSegment(Document, startPosition, endPosition - startPosition); TextDocumentWeakEventManager.UpdateFinished.AddListener(Document, this); deactivateIfSnippetEmpty = (endPosition != startPosition); foreach (IActiveElement element in registeredElements) { element.OnInsertionCompleted(); } if (InsertionCompleted != null) InsertionCompleted(this, e); currentStatus = Status.Interactive; if (registeredElements.Count == 0) { // deactivate immediately if there are no interactive elements Deactivate(new SnippetEventArgs(DeactivateReason.NoActiveElements)); } else { myInputHandler = new SnippetInputHandler(this); // disable existing snippet input handlers - there can be only 1 active snippet foreach (TextAreaStackedInputHandler h in TextArea.StackedInputHandlers) { if (h is SnippetInputHandler) TextArea.PopStackedInputHandler(h); } TextArea.PushStackedInputHandler(myInputHandler); } }