Example #1
0
        public bool HandleProcessDialogKey(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:
            {
                HandleKeyDown(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)
                {
                    if (!internalTextLayerController.IsOnStartOfLine)
                    {
                        Point prvCaretPos = internalTextLayerController.CaretPos;
                        internalTextLayerController.TryMoveCaretBackward();
                        currentCaretPos = internalTextLayerController.CaretPos;
                    }
                }
                else
                {
                    if (internalTextLayerController.IsOnStartOfLine)
                    {
                        internalTextLayerController.TryMoveCaretBackward();
                        currentCaretPos = internalTextLayerController.CaretPos;
                    }
                    else
                    {
                        if (!internalTextLayerController.IsOnStartOfLine)
                        {
                            Point prvCaretPos = internalTextLayerController.CaretPos;
                            internalTextLayerController.TryMoveCaretBackward();
                            currentCaretPos = internalTextLayerController.CaretPos;
                        }
                    }
                }
                //-------------------
                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++;
                    internalTextLayerController.TryMoveCaretForward();
                    currentCaretPos = internalTextLayerController.CaretPos;
                    //if (currentCaretPos.X != prvCaretPos.X)
                    //{
                    //    int nextCharWidth = internalTextLayerController.GetNextCharacterWidth();
                    //    if (nextCharWidth > 0)
                    //    {
                    //        break;
                    //    }
                    //}
                    //}
                }
                else
                {
                    if (internalTextLayerController.IsOnEndOfLine)
                    {
                        internalTextLayerController.TryMoveCaretForward();
                        currentCaretPos = internalTextLayerController.CaretPos;
                    }
                    else
                    {
                        //while (!internalTextLayerController.IsOnEndOfLine)
                        //{
                        Point prvCaretPos = internalTextLayerController.CaretPos;
                        internalTextLayerController.TryMoveCaretForward();
                        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.TryMoveCaretTo(internalTextLayerController.CurrentLineCharCount - 1);
                    }
                    else
                    {
                        internalTextLayerController.TryMoveCaretTo(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.TryMoveCaretTo(internalTextLayerController.CurrentLineCharCount - 1);
                    }
                    else
                    {
                        internalTextLayerController.TryMoveCaretTo(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);
            }
            }
        }