private static void OnCut(Object sender, ExecutedRoutedEventArgs e)
        {
            EditView editor = sender as EditView;

            if (editor != null && editor.Document != null)
            {
                String targetText = String.Empty;
                if (editor.Selection.IsEmpty)
                {
                    // 剪切整行
                    CopyWholeLine(editor);
                    editor.SelectLine();
                }
                else
                {
                    // 剪切选中的文字
                    CopySelectedText(editor);
                }
                using (editor.Document.AutoUpdate())
                {
                    editor.RemoveSelection();
                    editor.Redraw();
                }
                e.Handled = true;
            }
        }
 public InputHandlerGroup(EditView owner)
 {
     _owner    = owner ?? throw new ArgumentNullException("owner");
     _attached = false;
     _children = new ObservableCollection <IInputHandler>();
     _children.CollectionChanged += OnChildrenChanged;
 }
Esempio n. 3
0
 public AnchorSelection(EditView owner, TextAnchor startAnchor, TextAnchor endAnchor)
     : base(owner)
 {
     _startAnchor = startAnchor ?? throw new ArgumentNullException("startAnchor");
     _startAnchor.MovementType = AnchorMovementType.AfterInsertion;
     _endAnchor = endAnchor ?? throw new ArgumentNullException("endAnchor");
     _endAnchor.MovementType = AnchorMovementType.AfterInsertion;
 }
 public SelectionMouseHandler(EditView owner)
 {
     _owner                   = owner ?? throw new ArgumentNullException("owner");
     _scrollingTimer          = new DispatcherTimer(DispatcherPriority.Input);
     _scrollingTimer.Interval = TimeSpan.FromSeconds(0.1);
     _scrollingTimer.Tick    += OnScrolling;
     _scrolling               = false;
 }
 public InputCommandsHandler(EditView owner)
 {
     _owner           = owner ?? throw new ArgumentNullException("owner");
     _commandBindings = new ObservableCollection <CommandBinding>();
     _commandBindings.CollectionChanged += OnCommandBindingsChanged;
     _inputBindings = new ObservableCollection <InputBinding>();
     _inputBindings.CollectionChanged += OnInputBindingsChanged;
     _attached = false;
 }
        private static void CanRedo(Object sender, CanExecuteRoutedEventArgs e)
        {
            EditView editor = sender as EditView;

            if (editor != null && editor.Document != null && editor.IsFocused)
            {
                e.CanExecute = editor.CanRedo() && editor.Document.CanRedo();
                e.Handled    = true;
            }
        }
Esempio n. 7
0
        private static void OnCancelSelection(Object sender, ExecutedRoutedEventArgs e)
        {
            if (e.Handled)
            {
                return;
            }
            EditView editor = sender as EditView;

            editor.CancelSelection();
            editor.Redraw();
            e.Handled = true;
        }
        private static void OnRedo(Object sender, ExecutedRoutedEventArgs e)
        {
            EditView editor = sender as EditView;

            if (editor != null && editor.Document != null)
            {
                editor.Redo();
                editor.MoveCaretInVisual();
                editor.Redraw();
                e.Handled = true;
            }
        }
Esempio n. 9
0
 private static ExecutedRoutedEventHandler CaretHandler(CaretMovementType caretMovementType, Boolean doSelect)
 {
     return((sender, e) =>
     {
         if (e.Handled)
         {
             return;
         }
         EditView editor = sender as EditView;
         editor.MoveCaret(caretMovementType, doSelect);
         editor.Redraw();
         e.Handled = true;
     });
 }
Esempio n. 10
0
        internal static IInputHandler CreateHandler(EditView owner)
        {
            var handler = new InputCommandsHandler(owner);

            foreach (CommandBinding binding in _commandBindings)
            {
                handler.CommandBindings.Add(binding);
            }
            foreach (InputBinding binding in _inputBindings)
            {
                handler.InputBindings.Add(binding);
            }
            return(handler);
        }
        private static void CopySelectedText(EditView editor)
        {
            String text = editor.Document.GetTextAt(editor.Selection.StartOffset, editor.Selection.Length);
            var    obj  = new DataObject(text);

            try
            {
                Clipboard.SetDataObject(obj, true);
            }
            catch (ExternalException)
            {
                // 有时候会莫名其妙抛出这个异常,微软的控件都是忽略这个异常,所以这里也做同样的事
                return;
            }
        }
        private static void OnEnter(Object sender, ExecutedRoutedEventArgs e)
        {
            if (e.Handled)
            {
                return;
            }
            EditView editor = sender as EditView;

            if (editor != null && editor.Document != null)
            {
                using (editor.Document.AutoUpdate())
                {
                    editor.InsertText(CommonUtilities.LineBreak);
                    editor.Caret.RestartAnimation();
                    editor.Redraw();
                }
                e.Handled = true;
            }
        }
        private static void CopyWholeLine(EditView editor)
        {
            DocumentLine line     = editor.Document.GetLineByOffset(editor.Caret.DocumentOffset);
            String       lineText = editor.Document.GetTextAt(line.StartOffset, line._exactLength);
            var          obj      = new DataObject(lineText);
            MemoryStream ms       = new MemoryStream(1);

            ms.WriteByte(1);
            obj.SetData(LineCopyFormat, ms, false);
            try
            {
                Clipboard.SetDataObject(obj, true);
            }
            catch (ExternalException)
            {
                // 有时候会莫名其妙抛出这个异常,微软的控件都是忽略这个异常,所以这里也做同样的事
                return;
            }
        }
        private static void OnTabBackward(Object sender, ExecutedRoutedEventArgs e)
        {
            if (e.Handled)
            {
                return;
            }
            EditView editor = sender as EditView;

            if (editor != null && editor.Document != null)
            {
                using (editor.Document.AutoUpdate())
                {
                    editor.TabBackward();
                    editor.Caret.RestartAnimation();
                    editor.Redraw();
                }
                e.Handled = true;
            }
        }
        private static void OnCopy(Object sender, ExecutedRoutedEventArgs e)
        {
            EditView editor = sender as EditView;

            if (editor != null && editor.Document != null)
            {
                String targetText = String.Empty;
                if (editor.Selection.IsEmpty)
                {
                    // 拷贝整行
                    CopyWholeLine(editor);
                }
                else
                {
                    // 拷贝选中的文字
                    CopySelectedText(editor);
                }
                e.Handled = true;
            }
        }
        private static void OnPaste(Object sender, ExecutedRoutedEventArgs e)
        {
            EditView editor = sender as EditView;

            if (editor != null && editor.Document != null)
            {
                IDataObject obj = null;
                try
                {
                    obj = Clipboard.GetDataObject();
                }
                catch (ExternalException)
                {
                    return;
                }
                if (obj == null)
                {
                    return;
                }
                String text = (String)obj.GetData(DataFormats.UnicodeText);
                text = CommonUtilities.NormalizeText(new StringReader(text));
                if (!String.IsNullOrEmpty(text))
                {
                    using (editor.Document.AutoUpdate())
                    {
                        if (obj.GetDataPresent(LineCopyFormat))
                        {
                            // 粘贴整行
                            editor.InsertLine(text);
                            editor.Redraw();
                        }
                        else
                        {
                            editor.InsertText(text);
                            editor.Redraw();
                        }
                    }
                }
                e.Handled = true;
            }
        }
 /// <summary>
 /// 统一处理成删除selection,command是一个selection相关的命令。
 /// </summary>
 /// <param name="command"></param>
 /// <returns></returns>
 private static ExecutedRoutedEventHandler RemoveHandler(RoutedUICommand command)
 {
     return((sender, e) =>
     {
         if (e.Handled)
         {
             return;
         }
         EditView editor = sender as EditView;
         if (editor != null && editor.Document != null)
         {
             using (editor.Document.AutoUpdate())
             {
                 if (editor.Selection.IsEmpty)
                 {
                     command.Execute(e.Parameter, editor);
                 }
                 editor.RemoveSelection();
                 editor.Redraw();
             }
             e.Handled = true;
         }
     });
 }
 public FreeSelection(EditView owner)
     : base(owner)
 {
     _startOffset = _endOffset = 0;
 }