Example #1
0
        public void HostInvokeKeyDown(UIKeys keys)
        {
            _isKeyDown = true;
            switch (keys)
            {
            case UIKeys.Delete:
                _line.DoDelete();
                break;

            case UIKeys.Back:
                _line.DoBackspace();
                break;

            case UIKeys.Home:
                _line.DoHome();
                break;

            case UIKeys.End:
                _line.DoEnd();
                break;

            case UIKeys.Left:
                _line.DoLeft();
                break;

            case UIKeys.Right:
                _line.DoRight();
                break;
            }
            UpdateOutput();
        }
Example #2
0
 internal static void NotifyKeyDown(TextSurfaceEventListener listener, UIKeys key)
 {
     if (listener.KeyDown != null)
     {
         listener.KeyDown(listener, new TextDomEventArgs(key));
     }
 }
Example #3
0
 internal static bool NotifyPreviewArrow(TextSurfaceEventListener listener, UIKeys key)
 {
     if (listener.PreviewArrowKeyDown != null)
     {
         TextDomEventArgs e = new TextDomEventArgs(key);
         listener.PreviewArrowKeyDown(listener, e);
         return(e.PreventDefault);
     }
     return(false);
 }
Example #4
0
        //#if DEBUG
        //        static int dbug_preview_dialogKey_count = 0;
        //#endif

        public bool HandleProcessDialogKey(UIKeys keyData)
        {
            //#if DEBUG
            //          System.Diagnostics.Debug.WriteLine("prev_dlgkey" + (dbug_preview_dialogKey_count++));
            //#endif
            _canvasViewport.FullMode = false;
            _keyEventArgs.SetEventInfo((uint)keyData, false, false, false, UIEventName.ProcessDialogKey);//f-f-f will be set later
            bool result = _topWinEventRoot.RootProcessDialogKey(_keyEventArgs);

            if (result)
            {
                PrepareRenderAndFlushAccumGraphics();
            }

            return(result);
        }
Example #5
0
        //


        public override bool HandleProcessDialogKey(UIKeyEventArgs e)
        {
            UIKeys keyData = (UIKeys)e.KeyData;

            SetCaretVisible(true);
            if (_isInVerticalPhase && (keyData != UIKeys.Up || keyData != UIKeys.Down))
            {
                _isInVerticalPhase = false;
            }

            switch (e.KeyCode)
            {
            case UIKeys.Escape:
            case UIKeys.End:
            case UIKeys.Home:
            {
                if (_textSurfaceEventListener != null)
                {
                    return(TextSurfaceEventListener.NotifyPreviewDialogKeyDown(_textSurfaceEventListener, e));
                }
                return(false);
            }

            case UIKeys.Tab:
            {
                if (_textSurfaceEventListener != null &&
                    TextSurfaceEventListener.NotifyPreviewDialogKeyDown(_textSurfaceEventListener, e))
                {
                    return(true);
                }
                //
                DoTab();         //default do tab
                return(true);
            }

            case UIKeys.Return:
            {
                if (_textSurfaceEventListener != null &&
                    TextSurfaceEventListener.NotifyPreviewEnter(_textSurfaceEventListener, e))
                {
                    return(true);
                }

                if (_isEditable)
                {
                    if (_isMultiLine)
                    {
                        if (_editSession.SelectionRange != null)
                        {
                            InvalidateGraphicOfCurrentSelectionArea();
                        }

                        if (_editSession.SelectionRange != null)
                        {
                            //this selection range will be remove first
                        }

                        int lineBeforeSplit     = _editSession.CurrentLineNumber;
                        int lineCharBeforeSplit = _editSession.CurrentLineCharIndex;

                        _editSession.SplitCurrentLineIntoNewLine();

                        if (_textSurfaceEventListener != null)
                        {
                            var splitEventE = new SplitToNewLineEventArgs();
                            splitEventE.LineNumberBeforeSplit    = lineBeforeSplit;
                            splitEventE.LineCharIndexBeforeSplit = lineCharBeforeSplit;

                            TextSurfaceEventListener.NofitySplitNewLine(_textSurfaceEventListener, splitEventE);
                        }

                        Rectangle lineArea = _editSession.CurrentLineArea;
                        if (lineArea.Bottom > this.ViewportBottom)
                        {
                            ScrollOffset(0, lineArea.Bottom - this.ViewportBottom);
                        }
                        else
                        {
                            InvalidateGraphicOfCurrentLineArea();
                        }
                        EnsureCaretVisible();
                    }
                    else
                    {
                        if (_textSurfaceEventListener != null)
                        {
                            TextSurfaceEventListener.NotifyKeyDownOnSingleLineText(_textSurfaceEventListener, e);
                        }
                    }
                }

                return(true);
            }

            case UIKeys.Left:
            {
                if (_textSurfaceEventListener != null &&
                    TextSurfaceEventListener.NotifyPreviewArrow(_textSurfaceEventListener, e))
                {
                    return(true);
                }

                InvalidateGraphicOfCurrentLineArea();
                if (!e.Shift)
                {
                    _editSession.CancelSelect();
                }
                else
                {
                    _editSession.StartSelectIfNoSelection();
                }

                Point currentCaretPos = Point.Empty;
                if (!_isMultiLine)
                {
                    if (!_editSession.IsOnStartOfLine)
                    {
#if DEBUG
                        Point prvCaretPos = _editSession.CaretPos;
#endif
                        _editSession.TryMoveCaretBackward();
                        currentCaretPos = _editSession.CaretPos;
                    }
                }
                else
                {
                    if (_editSession.IsOnStartOfLine)
                    {
                        _editSession.TryMoveCaretBackward();
                        currentCaretPos = _editSession.CaretPos;
                    }
                    else
                    {
                        if (!_editSession.IsOnStartOfLine)
                        {
#if DEBUG
                            Point prvCaretPos = _editSession.CaretPos;
#endif
                            _editSession.TryMoveCaretBackward();
                            currentCaretPos = _editSession.CaretPos;
                        }
                    }
                }
                //-------------------
                if (e.Shift)
                {
                    _editSession.EndSelectIfNoSelection();
                }
                //-------------------

                EnsureCaretVisible();
                if (_textSurfaceEventListener != null)
                {
                    TextSurfaceEventListener.NotifyArrowKeyCaretPosChanged(_textSurfaceEventListener, e.KeyCode);
                }

                return(true);
            }

            case UIKeys.Right:
            {
                if (_textSurfaceEventListener != null &&
                    TextSurfaceEventListener.NotifyPreviewArrow(_textSurfaceEventListener, e))
                {
                    return(true);
                }

                InvalidateGraphicOfCurrentLineArea();
                if (!e.Shift)
                {
                    _editSession.CancelSelect();
                }
                else
                {
                    _editSession.StartSelectIfNoSelection();
                }


                Point currentCaretPos = Point.Empty;
                if (!_isMultiLine)
                {
#if DEBUG
                    Point prvCaretPos = _editSession.CaretPos;
#endif
                    _editSession.TryMoveCaretForward();
                    currentCaretPos = _editSession.CaretPos;
                }
                else
                {
                    if (_editSession.IsOnEndOfLine)
                    {
                        _editSession.TryMoveCaretForward();
                        currentCaretPos = _editSession.CaretPos;
                    }
                    else
                    {
#if DEBUG
                        Point prvCaretPos = _editSession.CaretPos;
#endif
                        _editSession.TryMoveCaretForward();
                        currentCaretPos = _editSession.CaretPos;
                    }
                }
                //-------------------
                if (e.Shift)
                {
                    _editSession.EndSelectIfNoSelection();
                }
                //-------------------

                EnsureCaretVisible();
                if (_textSurfaceEventListener != null)
                {
                    TextSurfaceEventListener.NotifyArrowKeyCaretPosChanged(_textSurfaceEventListener, keyData);
                }

                return(true);
            }

            case UIKeys.PageUp:
            {
                //similar to arrow  up
                if (_textSurfaceEventListener != null &&
                    TextSurfaceEventListener.NotifyPreviewDialogKeyDown(_textSurfaceEventListener, e))
                {
                    return(true);
                }

                if (_isMultiLine)
                {
                    if (!_isInVerticalPhase)
                    {
                        _isInVerticalPhase         = true;
                        _verticalExpectedCharIndex = _editSession.CharIndex;
                    }

                    //----------------------------
                    if (!e.Shift)
                    {
                        _editSession.CancelSelect();
                    }
                    else
                    {
                        _editSession.StartSelectIfNoSelection();
                    }
                    //----------------------------
                    //approximate line per viewport
                    int line_per_viewport = Height / _editSession.CurrentLineArea.Height;
                    if (line_per_viewport > 1)
                    {
                        if (_editSession.CurrentLineNumber - line_per_viewport < 0)
                        {
                            //move to first line
                            _editSession.CurrentLineNumber = 0;
                        }
                        else
                        {
                            _editSession.CurrentLineNumber -= line_per_viewport;
                        }
                    }



                    if (_verticalExpectedCharIndex > _editSession.CurrentLineCharCount - 1)
                    {
                        _editSession.TryMoveCaretTo(_editSession.CurrentLineCharCount);
                    }
                    else
                    {
                        _editSession.TryMoveCaretTo(_verticalExpectedCharIndex);
                    }

                    //----------------------------
                    if (e.Shift)
                    {
                        _editSession.EndSelectIfNoSelection();
                    }

                    Rectangle lineArea = _editSession.CurrentLineArea;
                    if (lineArea.Top < ViewportTop)
                    {
                        ScrollOffset(0, lineArea.Top - ViewportTop);
                    }
                    else
                    {
                        EnsureCaretVisible();
                        InvalidateGraphicOfCurrentLineArea();
                    }
                }
                else
                {
                }
                if (_textSurfaceEventListener != null)
                {
                    TextSurfaceEventListener.NotifyArrowKeyCaretPosChanged(_textSurfaceEventListener, keyData);
                }
                return(true);
            }

            case UIKeys.PageDown:
            {
                //similar to arrow  down
                if (_textSurfaceEventListener != null &&
                    TextSurfaceEventListener.NotifyPreviewDialogKeyDown(_textSurfaceEventListener, e))
                {
                    return(true);
                }

                if (_isMultiLine)
                {
                    if (!_isInVerticalPhase)
                    {
                        _isInVerticalPhase         = true;
                        _verticalExpectedCharIndex = _editSession.CharIndex;
                    }

                    //----------------------------
                    if (!e.Shift)
                    {
                        _editSession.CancelSelect();
                    }
                    else
                    {
                        _editSession.StartSelectIfNoSelection();
                    }
                    //----------------------------

                    int line_per_viewport = Height / _editSession.CurrentLineArea.Height;

                    if (_editSession.CurrentLineNumber + line_per_viewport < _editSession.LineCount)
                    {
                        _editSession.CurrentLineNumber += line_per_viewport;
                    }
                    else
                    {
                        //move to last line
                        _editSession.CurrentLineNumber = _editSession.LineCount - 1;
                    }

                    if (_verticalExpectedCharIndex > _editSession.CurrentLineCharCount - 1)
                    {
                        _editSession.TryMoveCaretTo(_editSession.CurrentLineCharCount);
                    }
                    else
                    {
                        _editSession.TryMoveCaretTo(_verticalExpectedCharIndex);
                    }
                    //----------------------------

                    if (e.Shift)
                    {
                        _editSession.EndSelectIfNoSelection();
                    }
                    //----------------------------
                    Rectangle lineArea = _editSession.CurrentLineArea;
                    if (lineArea.Bottom > this.ViewportBottom)
                    {
                        ScrollOffset(0, lineArea.Bottom - this.ViewportBottom);
                    }
                    else
                    {
                        InvalidateGraphicOfCurrentLineArea();
                    }
                }
                EnsureCaretVisible();
                if (_textSurfaceEventListener != null)
                {
                    TextSurfaceEventListener.NotifyArrowKeyCaretPosChanged(_textSurfaceEventListener, keyData);
                }
                return(true);
            }

            case UIKeys.Down:
            {
                if (_textSurfaceEventListener != null &&
                    TextSurfaceEventListener.NotifyPreviewArrow(_textSurfaceEventListener, e))
                {
                    return(true);
                }
                if (_isMultiLine)
                {
                    if (!_isInVerticalPhase)
                    {
                        _isInVerticalPhase         = true;
                        _verticalExpectedCharIndex = _editSession.CharIndex;
                    }

                    //----------------------------
                    if (!e.Shift)
                    {
                        _editSession.CancelSelect();
                    }
                    else
                    {
                        _editSession.StartSelectIfNoSelection();
                    }
                    //----------------------------

                    _editSession.CurrentLineNumber++;
                    if (_verticalExpectedCharIndex > _editSession.CurrentLineCharCount - 1)
                    {
                        _editSession.TryMoveCaretTo(_editSession.CurrentLineCharCount);
                    }
                    else
                    {
                        _editSession.TryMoveCaretTo(_verticalExpectedCharIndex);
                    }
                    //----------------------------

                    if (e.Shift)
                    {
                        _editSession.EndSelectIfNoSelection();
                    }
                    //----------------------------
                    Rectangle lineArea = _editSession.CurrentLineArea;
                    if (lineArea.Bottom > this.ViewportBottom)
                    {
                        ScrollOffset(0, lineArea.Bottom - this.ViewportBottom);
                    }
                    else
                    {
                        InvalidateGraphicOfCurrentLineArea();
                    }
                }
                EnsureCaretVisible();
                if (_textSurfaceEventListener != null)
                {
                    TextSurfaceEventListener.NotifyArrowKeyCaretPosChanged(_textSurfaceEventListener, keyData);
                    if (!_isMultiLine)
                    {
                        TextSurfaceEventListener.NotifyKeyDownOnSingleLineText(_textSurfaceEventListener, e);
                    }
                }
                return(true);
            }

            case UIKeys.Up:
            {
                if (_textSurfaceEventListener != null &&
                    TextSurfaceEventListener.NotifyPreviewArrow(_textSurfaceEventListener, e))
                {
                    return(true);
                }

                if (_isMultiLine)
                {
                    if (!_isInVerticalPhase)
                    {
                        _isInVerticalPhase         = true;
                        _verticalExpectedCharIndex = _editSession.CharIndex;
                    }

                    //----------------------------
                    if (!e.Shift)
                    {
                        _editSession.CancelSelect();
                    }
                    else
                    {
                        _editSession.StartSelectIfNoSelection();
                    }
                    //----------------------------

                    _editSession.CurrentLineNumber--;
                    if (_verticalExpectedCharIndex > _editSession.CurrentLineCharCount - 1)
                    {
                        _editSession.TryMoveCaretTo(_editSession.CurrentLineCharCount);
                    }
                    else
                    {
                        _editSession.TryMoveCaretTo(_verticalExpectedCharIndex);
                    }

                    //----------------------------
                    if (e.Shift)
                    {
                        _editSession.EndSelectIfNoSelection();
                    }

                    Rectangle lineArea = _editSession.CurrentLineArea;
                    if (lineArea.Top < ViewportTop)
                    {
                        ScrollOffset(0, lineArea.Top - ViewportTop);
                    }
                    else
                    {
                        EnsureCaretVisible();
                        InvalidateGraphicOfCurrentLineArea();
                    }
                }
                else
                {
                }


                if (_textSurfaceEventListener != null)
                {
                    TextSurfaceEventListener.NotifyArrowKeyCaretPosChanged(_textSurfaceEventListener, keyData);
                    if (!_isMultiLine)
                    {
                        TextSurfaceEventListener.NotifyKeyDownOnSingleLineText(_textSurfaceEventListener, e);
                    }
                }
                return(true);
            }

            default:
            {
                return(false);
            }
            }
        }
Example #6
0
        public override void HandleKeyDown(UIKeyEventArgs e)
        {
            this.SetCaretVisible(true);
            if (!e.HasKeyData)
            {
                return;
            }

            switch (e.KeyCode)
            {
            case UIKeys.Home:
            {
                this.DoHome(e.Shift);
            }
            break;

            case UIKeys.End:
            {
                this.DoEnd(e.Shift);
            }
            break;

            case UIKeys.Back:
            {
                if (_isEditable)
                {
                    if (_editSession.SelectionRange != null)
                    {
                        InvalidateGraphicOfCurrentSelectionArea();
                    }
                    else
                    {
                        InvalidateGraphicOfCurrentLineArea();
                    }
                    if (_textSurfaceEventListener == null)
                    {
                        _editSession.DoBackspace();
                    }
                    else
                    {
                        if (!TextSurfaceEventListener.NotifyPreviewBackSpace(_textSurfaceEventListener, e) &&
                            _editSession.DoBackspace())
                        {
                            TextSurfaceEventListener.NotifyCharactersRemoved(_textSurfaceEventListener,
                                                                             new TextDomEventArgs(_editSession._updateJustCurrentLine));
                        }
                    }
                    EnsureCaretVisible();
                }
            }
            break;

            case UIKeys.Delete:
            {
                if (_isEditable)
                {
                    if (_editSession.SelectionRange != null)
                    {
                        InvalidateGraphicOfCurrentSelectionArea();
                    }
                    else
                    {
                        InvalidateGraphicOfCurrentLineArea();
                    }
                    if (_textSurfaceEventListener == null)
                    {
                        _editSession.DoDelete();
                    }
                    else
                    {
                        VisualSelectionRangeSnapShot delpart = _editSession.DoDelete();
                        TextSurfaceEventListener.NotifyCharactersRemoved(_textSurfaceEventListener,
                                                                         new TextDomEventArgs(_editSession._updateJustCurrentLine, delpart));
                    }

                    EnsureCaretVisible();
                }
            }
            break;

            default:
            {
                if (_textSurfaceEventListener != null)
                {
                    UIKeys keycode = e.KeyCode;
                    if (keycode >= UIKeys.F1 && keycode <= UIKeys.F12)
                    {
                        InvalidateGraphicOfCurrentSelectionArea();
                        TextSurfaceEventListener.NotifyFunctionKeyDown(_textSurfaceEventListener, keycode);
                        EnsureCaretVisible();
                    }
                }
            }
            break;
            }

            if (e.HasKeyData && e.Ctrl)
            {
                switch (e.KeyCode)
                {
                case UIKeys.A:
                {
                    //select all
                    //....
                    this.CurrentLineNumber = 0;
                    //start select to end
                    DoHome(false);       //1st simulate
                    DoHome(true);        //2nd
                    this.CurrentLineNumber = this.LineCount - 1;
                    DoEnd(true);         //
                }
                break;

                case UIKeys.C:
                {
                    using (StringBuilderPool <TempTextLineCopyContext> .GetFreeStringBuilder(out StringBuilder stBuilder))
                    {
                        _editSession.CopySelectedTextToPlainText(stBuilder);
                        if (stBuilder != null)
                        {
                            if (stBuilder.Length == 0)
                            {
                                Clipboard.Clear();
                            }
                            else
                            {
                                Clipboard.SetText(stBuilder.ToString());
                            }
                        }
                    }
                }
                break;

                case UIKeys.V:
                {
                    if (_isEditable && Clipboard.ContainsUnicodeText())
                    {
                        //1. we need to parse multi-line to single line
                        //this may need text-break services

                        _editSession.AddTextToCurrentLine(PlainTextDocumentHelper.CreatePlainTextDocument(Clipboard.GetUnicodeText()));

                        EnsureCaretVisible();
                    }
                }
                break;

                case UIKeys.X:
                {
                    if (_isEditable && _editSession.SelectionRange != null)
                    {
                        InvalidateGraphicOfCurrentSelectionArea();

                        using (StringBuilderPool <TempTextLineCopyContext> .GetFreeStringBuilder(out StringBuilder stBuilder))
                        {
                            _editSession.CopySelectedTextToPlainText(stBuilder);
                            if (stBuilder != null)
                            {
                                Clipboard.SetText(stBuilder.ToString());
                            }
                            _editSession.DoDelete();
                            EnsureCaretVisible();
                        }
                    }
                }
                break;

                case UIKeys.Z:
                {
                    if (_isEditable)
                    {
                        _editSession.UndoLastAction();
                        EnsureCaretVisible();
                    }
                }
                break;

                case UIKeys.Y:
                {
                    if (_isEditable)
                    {
                        _editSession.ReverseLastUndoAction();
                        EnsureCaretVisible();
                    }
                }
                break;
                }
            }

            if (_textSurfaceEventListener != null)
            {
                TextSurfaceEventListener.NotifyKeyDown(_textSurfaceEventListener, e);
            }
        }
Example #7
0
 internal static void NotifyFunctionKeyDown(TextSurfaceEventListener listener, UIKeys key)
 {
 }
Example #8
0
 public TextDomEventArgs(UIKeys key)
 {
     this.key = key;
 }
 internal static bool NotifyPreviewArrow(TextSurfaceEventListener listener, UIKeys key)
 {
     if (listener.PreviewArrowKeyDown != null)
     {
         TextDomEventArgs e = new TextDomEventArgs(key);
         listener.PreviewArrowKeyDown(listener, e);
         return e.PreventDefault;
     }
     return false;
 }
 internal static void NotifyFunctionKeyDown(TextSurfaceEventListener listener, UIKeys key)
 {
 }
 public TextDomEventArgs(UIKeys key)
 {
     this.key = key;
 }
 internal static void NotifyKeyDown(TextSurfaceEventListener listener, UIKeys key)
 {
     if (listener.KeyDown != null)
     {
         listener.KeyDown(listener, new TextDomEventArgs(key));
     }
 }
 internal static void NotifyArrowKeyCaretPosChanged(TextSurfaceEventListener listener, UIKeys key)
 {
     if (listener.ArrowKeyCaretPosChanged != null)
     {
         listener.ArrowKeyCaretPosChanged(listener, new TextDomEventArgs(key));
     }
 }
        public bool OnProcessDialogKey(UIKeyEventArgs e)
        {
            UIKeys keyData = (UIKeys)e.KeyData;

            SetCaretState(true);

            if (isInVerticalPhase && (keyData != UIKeys.Up || keyData != UIKeys.Down))
            {
                isInVerticalPhase = false;
            }

            switch (e.KeyCode)
            {
            case UIKeys.Home:
            {
                OnKeyDown(e);
                return(true);
            }

            case UIKeys.Return:
            {
                if (textSurfaceEventListener != null &&
                    TextSurfaceEventListener.NotifyPreviewEnter(textSurfaceEventListener))
                {
                    return(true);
                }
                if (isMultiLine)
                {
                    if (internalTextLayerController.SelectionRange != null)
                    {
                        InvalidateGraphicLocalArea(this, GetSelectionUpdateArea());
                    }
                    internalTextLayerController.SplitCurrentLineIntoNewLine();


                    if (textSurfaceEventListener != null)
                    {
                        TextSurfaceEventListener.NofitySplitNewLine(textSurfaceEventListener, e);
                    }

                    Rectangle lineArea = internalTextLayerController.CurrentLineArea;
                    if (lineArea.Bottom > this.ViewportBottom)
                    {
                        ScrollBy(0, lineArea.Bottom - this.ViewportBottom);
                    }
                    else
                    {
                        InvalidateGraphicOfCurrentLineArea();
                    }
                    EnsureCaretVisible();

                    return(true);
                }
                return(true);
            }

            case UIKeys.Left:
            {
                if (textSurfaceEventListener != null &&
                    TextSurfaceEventListener.NotifyPreviewArrow(textSurfaceEventListener, keyData))
                {
                    return(true);
                }

                InvalidateGraphicOfCurrentLineArea();
                if (!e.Shift)
                {
                    internalTextLayerController.CancelSelect();
                }
                else
                {
                    internalTextLayerController.StartSelectIfNoSelection();
                }

                Point currentCaretPos = Point.Empty;
                if (!isMultiLine)
                {
                    while (!internalTextLayerController.IsOnStartOfLine)
                    {
                        Point prvCaretPos = internalTextLayerController.CaretPos;
                        internalTextLayerController.CharIndex--;
                        currentCaretPos = internalTextLayerController.CaretPos;
                        if (currentCaretPos.X != prvCaretPos.X)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    if (internalTextLayerController.IsOnStartOfLine)
                    {
                        internalTextLayerController.CharIndex--;
                        currentCaretPos = internalTextLayerController.CaretPos;
                    }
                    else
                    {
                        while (!internalTextLayerController.IsOnStartOfLine)
                        {
                            Point prvCaretPos = internalTextLayerController.CaretPos;
                            internalTextLayerController.CharIndex--;
                            currentCaretPos = internalTextLayerController.CaretPos;
                            if (currentCaretPos.X != prvCaretPos.X)
                            {
                                break;
                            }
                        }
                    }
                }
                //-------------------
                if (e.Shift)
                {
                    internalTextLayerController.EndSelectIfNoSelection();
                }
                //-------------------

                EnsureCaretVisible();
                if (textSurfaceEventListener != null)
                {
                    TextSurfaceEventListener.NotifyArrowKeyCaretPosChanged(textSurfaceEventListener, keyData);
                }

                return(true);
            }

            case UIKeys.Right:
            {
                if (textSurfaceEventListener != null &&
                    TextSurfaceEventListener.NotifyPreviewArrow(textSurfaceEventListener, keyData))
                {
                    return(true);
                }

                InvalidateGraphicOfCurrentLineArea();
                if (!e.Shift)
                {
                    internalTextLayerController.CancelSelect();
                }
                else
                {
                    internalTextLayerController.StartSelectIfNoSelection();
                }


                Point currentCaretPos = Point.Empty;
                if (!isMultiLine)
                {
                    while (!internalTextLayerController.IsOnEndOfLine)
                    {
                        Point prvCaretPos = internalTextLayerController.CaretPos;
                        internalTextLayerController.CharIndex++;
                        currentCaretPos = internalTextLayerController.CaretPos;
                        if (currentCaretPos.X != prvCaretPos.X)
                        {
                            int nextCharWidth = internalTextLayerController.GetNextCharacterWidth();
                            if (nextCharWidth > 0)
                            {
                                break;
                            }
                        }
                    }
                }
                else
                {
                    if (internalTextLayerController.IsOnEndOfLine)
                    {
                        internalTextLayerController.CharIndex++;
                        currentCaretPos = internalTextLayerController.CaretPos;
                    }
                    else
                    {
                        while (!internalTextLayerController.IsOnEndOfLine)
                        {
                            Point prvCaretPos = internalTextLayerController.CaretPos;
                            internalTextLayerController.CharIndex++;
                            currentCaretPos = internalTextLayerController.CaretPos;

                            if (currentCaretPos.X != prvCaretPos.X)
                            {
                                //forward check next caret
                                int nextCharWidth = internalTextLayerController.GetNextCharacterWidth();
                                if (nextCharWidth > 0)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
                //-------------------
                if (e.Shift)
                {
                    internalTextLayerController.EndSelectIfNoSelection();
                }
                //-------------------

                EnsureCaretVisible();
                if (textSurfaceEventListener != null)
                {
                    TextSurfaceEventListener.NotifyArrowKeyCaretPosChanged(textSurfaceEventListener, keyData);
                }

                return(true);
            }

            case UIKeys.Down:
            {
                if (textSurfaceEventListener != null &&
                    TextSurfaceEventListener.NotifyPreviewArrow(textSurfaceEventListener, keyData))
                {
                    return(true);
                }
                if (isMultiLine)
                {
                    if (!isInVerticalPhase)
                    {
                        isInVerticalPhase         = true;
                        verticalExpectedCharIndex = internalTextLayerController.CharIndex;
                    }

                    //----------------------------
                    if (!e.Shift)
                    {
                        internalTextLayerController.CancelSelect();
                    }
                    else
                    {
                        internalTextLayerController.StartSelectIfNoSelection();
                    }
                    //----------------------------

                    internalTextLayerController.CurrentLineNumber++;

                    if (verticalExpectedCharIndex > internalTextLayerController.CurrentLineCharCount - 1)
                    {
                        internalTextLayerController.CharIndex = internalTextLayerController.CurrentLineCharCount - 1;
                    }
                    else
                    {
                        internalTextLayerController.CharIndex = verticalExpectedCharIndex;
                    }
                    //----------------------------

                    if (e.Shift)
                    {
                        internalTextLayerController.EndSelectIfNoSelection();
                    }
                    //----------------------------
                    Rectangle lineArea = internalTextLayerController.CurrentLineArea;
                    if (lineArea.Bottom > this.ViewportBottom)
                    {
                        ScrollBy(0, lineArea.Bottom - this.ViewportBottom);
                    }
                    else
                    {
                        InvalidateGraphicOfCurrentLineArea();
                    }
                }

                if (textSurfaceEventListener != null)
                {
                    TextSurfaceEventListener.NotifyArrowKeyCaretPosChanged(textSurfaceEventListener, keyData);
                }
                return(true);
            }

            case UIKeys.Up:
            {
                if (textSurfaceEventListener != null &&
                    TextSurfaceEventListener.NotifyPreviewArrow(textSurfaceEventListener, keyData))
                {
                    return(true);
                }

                if (isMultiLine)
                {
                    if (!isInVerticalPhase)
                    {
                        isInVerticalPhase         = true;
                        verticalExpectedCharIndex = internalTextLayerController.CharIndex;
                    }

                    //----------------------------
                    if (!e.Shift)
                    {
                        internalTextLayerController.CancelSelect();
                    }
                    else
                    {
                        internalTextLayerController.StartSelectIfNoSelection();
                    }
                    //----------------------------

                    internalTextLayerController.CurrentLineNumber--;

                    if (verticalExpectedCharIndex > internalTextLayerController.CurrentLineCharCount - 1)
                    {
                        internalTextLayerController.CharIndex = internalTextLayerController.CurrentLineCharCount - 1;
                    }
                    else
                    {
                        internalTextLayerController.CharIndex = verticalExpectedCharIndex;
                    }

                    //----------------------------
                    if (e.Shift)
                    {
                        internalTextLayerController.EndSelectIfNoSelection();
                    }

                    Rectangle lineArea = internalTextLayerController.CurrentLineArea;

                    if (lineArea.Top < ViewportY)
                    {
                        ScrollBy(0, lineArea.Top - ViewportY);
                    }
                    else
                    {
                        EnsureCaretVisible();
                        InvalidateGraphicOfCurrentLineArea();
                    }
                }
                else
                {
                }
                if (textSurfaceEventListener != null)
                {
                    TextSurfaceEventListener.NotifyArrowKeyCaretPosChanged(textSurfaceEventListener, keyData);
                }
                return(true);
            }

            case UIKeys.Tab:
            {
                DoTab();
                return(true);
            }

            default:
            {
                return(false);
            }
            }
        }
        public void OnKeyDown(UIKeyEventArgs e)
        {
            this.SetCaretState(true);
            if (!e.HasKeyData)
            {
                return;
            }

            switch (e.KeyCode)
            {
            case UIKeys.Home:
            {
                this.DoHome(e.Shift);
            } break;

            case UIKeys.End:
            {
                this.DoEnd(e.Shift);
            } break;

            case UIKeys.Back:
            {
                if (internalTextLayerController.SelectionRange != null)
                {
                    InvalidateGraphicLocalArea(this, GetSelectionUpdateArea());
                }
                else
                {
                    InvalidateGraphicOfCurrentLineArea();
                }
                if (textSurfaceEventListener == null)
                {
                    internalTextLayerController.DoBackspace();
                }
                else
                {
                    if (!TextSurfaceEventListener.NotifyPreviewBackSpace(textSurfaceEventListener) &&
                        internalTextLayerController.DoBackspace())
                    {
                        TextSurfaceEventListener.NotifyCharactersRemoved(textSurfaceEventListener,
                                                                         new TextDomEventArgs(internalTextLayerController.updateJustCurrentLine));
                    }
                }

                EnsureCaretVisible();
            } break;

            case UIKeys.Delete:
            {
                if (internalTextLayerController.SelectionRange != null)
                {
                    InvalidateGraphicLocalArea(this, GetSelectionUpdateArea());
                }
                else
                {
                    InvalidateGraphicOfCurrentLineArea();
                }
                if (textSurfaceEventListener == null)
                {
                    internalTextLayerController.DoDelete();
                }
                else
                {
                    VisualSelectionRangeSnapShot delpart = internalTextLayerController.DoDelete();
                    TextSurfaceEventListener.NotifyCharactersRemoved(textSurfaceEventListener,
                                                                     new TextDomEventArgs(internalTextLayerController.updateJustCurrentLine));
                }

                EnsureCaretVisible();
            } break;

            default:
            {
                if (textSurfaceEventListener != null)
                {
                    UIKeys keycode = e.KeyCode;
                    if (keycode >= UIKeys.F1 && keycode <= UIKeys.F12)
                    {
                        InvalidateGraphicLocalArea(this, GetSelectionUpdateArea());
                        TextSurfaceEventListener.NotifyFunctionKeyDown(textSurfaceEventListener, keycode);
                        EnsureCaretVisible();
                    }
                }
            } break;
            }

            if (e.HasKeyData && e.Ctrl)
            {
                switch (e.KeyCode)
                {
                case UIKeys.C:
                {
                    StringBuilder stBuilder = GetFreeStringBuilder();
                    internalTextLayerController.CopySelectedTextToPlainText(stBuilder);
                    if (stBuilder != null)
                    {
                        if (stBuilder.Length == 0)
                        {
                            Clipboard.Clear();
                        }
                        else
                        {
                            Clipboard.SetText(stBuilder.ToString());
                        }
                    }
                    ReleaseStringBuilder(stBuilder);
                } break;

                case UIKeys.V:
                {
                    if (Clipboard.ContainUnicodeText())
                    {
                        internalTextLayerController.AddTextRunsToCurrentLine(
                            new EditableRun[] {
                                new EditableTextRun(this.Root,
                                                    Clipboard.GetUnicodeText(), this.CurrentTextSpanStyle)
                            });
                        EnsureCaretVisible();
                    }
                } break;

                case UIKeys.X:
                {
                    if (internalTextLayerController.SelectionRange != null)
                    {
                        if (internalTextLayerController.SelectionRange != null)
                        {
                            InvalidateGraphicLocalArea(this, GetSelectionUpdateArea());
                        }
                        StringBuilder stBuilder = GetFreeStringBuilder();
                        internalTextLayerController.CopySelectedTextToPlainText(stBuilder);
                        if (stBuilder != null)
                        {
                            Clipboard.SetText(stBuilder.ToString());
                        }

                        internalTextLayerController.DoDelete();


                        EnsureCaretVisible();
                        ReleaseStringBuilder(stBuilder);
                    }
                } break;

                case UIKeys.Z:
                {
                    internalTextLayerController.UndoLastAction();
                    EnsureCaretVisible();
                } break;

                case UIKeys.Y:
                {
                    internalTextLayerController.ReverseLastUndoAction();
                    EnsureCaretVisible();
                } break;

                case UIKeys.B:
                {
                    //TextSpanStyle style = internalTextLayerController.GetFirstTextStyleInSelectedRange();
                    //TextSpanStyle textStyle = null;

                    ////test only ***
                    ////TODO: make this more configurable
                    //if (style != null)
                    //{
                    //    TextSpanStyle defaultBeh = ((TextSpanStyle)style);
                    //    if (defaultBeh.FontBold)
                    //    {
                    //        textStyle = StyleHelper.CreateNewStyle(Color.Black);
                    //    }
                    //    else
                    //    {
                    //        textStyle = StyleHelper.CreateNewStyle(Color.Blue);
                    //    }
                    //}
                    //else
                    //{
                    //    textStyle = StyleHelper.CreateNewStyle(Color.Blue);
                    //}

                    //internalTextLayerController.DoFormatSelection(textStyle);

                    //if (internalTextLayerController.updateJustCurrentLine)
                    //{

                    //    InvalidateGraphicOfCurrentLineArea();
                    //}
                    //else
                    //{
                    //    InvalidateGraphics();
                    //}
                } break;
                }
            }
            if (textSurfaceEventListener != null)
            {
                TextSurfaceEventListener.NotifyKeyDown(textSurfaceEventListener, e.KeyCode);
            }
        }
Example #16
0
 public void HostInvokeKeyUp(UIKeys keys)
 {
     _isKeyDown = false;
 }
Example #17
0
 internal static void NotifyArrowKeyCaretPosChanged(TextSurfaceEventListener listener, UIKeys key)
 {
     if (listener.ArrowKeyCaretPosChanged != null)
     {
         listener.ArrowKeyCaretPosChanged(listener, new TextDomEventArgs(key));
     }
 }
Example #18
0
        public void HandleKeyDown(UIKeyEventArgs e)
        {
            this.SetCaretState(true);
            if (!e.HasKeyData)
            {
                return;
            }

            switch (e.KeyCode)
            {
            case UIKeys.Home:
            {
                this.DoHome(e.Shift);
            }
            break;

            case UIKeys.End:
            {
                this.DoEnd(e.Shift);
            }
            break;

            case UIKeys.Back:
            {
                if (internalTextLayerController.SelectionRange != null)
                {
                    InvalidateGraphicLocalArea(this, GetSelectionUpdateArea());
                }
                else
                {
                    InvalidateGraphicOfCurrentLineArea();
                }
                if (textSurfaceEventListener == null)
                {
                    internalTextLayerController.DoBackspace();
                }
                else
                {
                    if (!TextSurfaceEventListener.NotifyPreviewBackSpace(textSurfaceEventListener) &&
                        internalTextLayerController.DoBackspace())
                    {
                        TextSurfaceEventListener.NotifyCharactersRemoved(textSurfaceEventListener,
                                                                         new TextDomEventArgs(internalTextLayerController.updateJustCurrentLine));
                    }
                }

                EnsureCaretVisible();
            }
            break;

            case UIKeys.Delete:
            {
                if (internalTextLayerController.SelectionRange != null)
                {
                    InvalidateGraphicLocalArea(this, GetSelectionUpdateArea());
                }
                else
                {
                    InvalidateGraphicOfCurrentLineArea();
                }
                if (textSurfaceEventListener == null)
                {
                    internalTextLayerController.DoDelete();
                }
                else
                {
                    VisualSelectionRangeSnapShot delpart = internalTextLayerController.DoDelete();
                    TextSurfaceEventListener.NotifyCharactersRemoved(textSurfaceEventListener,
                                                                     new TextDomEventArgs(internalTextLayerController.updateJustCurrentLine));
                }

                EnsureCaretVisible();
            }
            break;

            default:
            {
                if (textSurfaceEventListener != null)
                {
                    UIKeys keycode = e.KeyCode;
                    if (keycode >= UIKeys.F1 && keycode <= UIKeys.F12)
                    {
                        InvalidateGraphicLocalArea(this, GetSelectionUpdateArea());
                        TextSurfaceEventListener.NotifyFunctionKeyDown(textSurfaceEventListener, keycode);
                        EnsureCaretVisible();
                    }
                }
            }
            break;
            }

            if (e.HasKeyData && e.Ctrl)
            {
                switch (e.KeyCode)
                {
                case UIKeys.A:
                {
                    //select all
                    //....
                    this.CurrentLineNumber = 0;
                    //start select to end
                    DoEnd(true);        //simulate
                    this.CurrentLineNumber = this.LineCount - 1;
                    DoEnd(true);
                }
                break;

                case UIKeys.C:
                {
                    StringBuilder stBuilder = GetFreeStringBuilder();
                    internalTextLayerController.CopySelectedTextToPlainText(stBuilder);
                    if (stBuilder != null)
                    {
                        if (stBuilder.Length == 0)
                        {
                            Clipboard.Clear();
                        }
                        else
                        {
                            Clipboard.SetText(stBuilder.ToString());
                        }
                    }
                    ReleaseStringBuilder(stBuilder);
                }
                break;

                case UIKeys.V:
                {
                    if (Clipboard.ContainUnicodeText())
                    {
                        //1. we need to parse multi-line to single line
                        //this may need text-break services

                        internalTextLayerController.AddUnformattedStringToCurrentLine(
                            Clipboard.GetUnicodeText(), this.currentSpanStyle);

                        EnsureCaretVisible();
                    }
                }
                break;

                case UIKeys.X:
                {
                    if (internalTextLayerController.SelectionRange != null)
                    {
                        if (internalTextLayerController.SelectionRange != null)
                        {
                            InvalidateGraphicLocalArea(this, GetSelectionUpdateArea());
                        }
                        StringBuilder stBuilder = GetFreeStringBuilder();
                        internalTextLayerController.CopySelectedTextToPlainText(stBuilder);
                        if (stBuilder != null)
                        {
                            Clipboard.SetText(stBuilder.ToString());
                        }

                        internalTextLayerController.DoDelete();
                        EnsureCaretVisible();
                        ReleaseStringBuilder(stBuilder);
                    }
                }
                break;

                case UIKeys.Z:
                {
                    internalTextLayerController.UndoLastAction();
                    EnsureCaretVisible();
                }
                break;

                case UIKeys.Y:
                {
                    internalTextLayerController.ReverseLastUndoAction();
                    EnsureCaretVisible();
                }
                break;

                case UIKeys.B:
                {
                    //
                    //test add markers
                    //
                    //if (internalTextLayerController.SelectionRange != null)
                    //{
                    //    //
                    //    internalTextLayerController.SelectionRange.SwapIfUnOrder();
                    //    VisualMarkerSelectionRange markerSelRange =
                    //        VisualMarkerSelectionRange.CreateFromSelectionRange(
                    //            internalTextLayerController.SelectionRange.GetSelectionRangeSnapshot());
                    //    //then add to the marker layers
                    //    markerSelRange.BindToTextLayer(textLayer);

                    //    internalTextLayerController.VisualMarkers.Add(markerSelRange);
                    //}

                    //
                    //TextSpanStyle style = internalTextLayerController.GetFirstTextStyleInSelectedRange();
                    //TextSpanStyle textStyle = null;

                    ////test only ***
                    ////TODO: make this more configurable
                    //if (style != null)
                    //{
                    //    TextSpanStyle defaultBeh = ((TextSpanStyle)style);
                    //    if (defaultBeh.FontBold)
                    //    {
                    //        textStyle = StyleHelper.CreateNewStyle(Color.Black);
                    //    }
                    //    else
                    //    {
                    //        textStyle = StyleHelper.CreateNewStyle(Color.Blue);
                    //    }
                    //}
                    //else
                    //{
                    //    textStyle = StyleHelper.CreateNewStyle(Color.Blue);
                    //}

                    //internalTextLayerController.DoFormatSelection(textStyle);

                    //if (internalTextLayerController.updateJustCurrentLine)
                    //{

                    //    InvalidateGraphicOfCurrentLineArea();
                    //}
                    //else
                    //{
                    //    InvalidateGraphics();
                    //}
                }
                break;
                }
            }
            if (textSurfaceEventListener != null)
            {
                TextSurfaceEventListener.NotifyKeyDown(textSurfaceEventListener, e.KeyCode);
            }
        }