Esempio n. 1
0
        //[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.WholeWord;
                        if (shift && !(TextArea.Selection is RectangleSelection))
                        {
                            TextArea.Selection = TextArea.Selection.StartSelectionOrSetEndpoint(oldPosition, TextArea.Caret.Position);
                        }
                    }
                    else if (e.ClickCount == 1 && modifiers == InputModifiers.LeftMouseButton)
                    {
                        _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;
            }
        }