tdf#160838 editeng: Report IME cursor pos more reliably

`mpIMEInfos` is set when handling the
`CommandEventId::StartExtTextInput` command. However,
the cursor position can also be queried without it
being set, as seen e.g. at least for the fcitx5 use case
from tdf#160838 when using the Qt-based VCL plugins.

If no IME Input has happened yet, just use the current
selection PaM index as the end index.

This makes the fcitx5 language indicator show up at the
current cursor position when positioned in a comment in
Writer, the Calc input bar or a Math formula and
switching the fcitx5 language/keyboard layout using the shortcut
with the Qt-based VCL plugins.

The correct position had no longer been used after welding,
i.e. since

    commit 69c546e1e7a697217f273baa7c1729ff823efd76
    Date:   Fri Dec 4 16:30:31 2020 +0000

        weld annotation window

for Writer comments and since

    commit e087e25f05e689091cbf1c4f91b6e93878ac17ec
    Date:   Mon Oct 5 14:19:05 2020 +0100

        weld InputBar

for the Calc input bar.

The fact that the indicator shows up at the wrong position
before typing any character (e.g. opening Writer, then switching the
keyboard layout right away, as mentioend in tdf#160838
comment 2) is a different issue and will be addressed
separately.

Change-Id: Ibe7698e113cbbea7f273cc929d72ca47fb407a20
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166888
Reviewed-by: Michael Weghorn <m.weghorn@posteo.de>
Tested-by: Jenkins
diff --git a/editeng/source/editeng/impedit2.cxx b/editeng/source/editeng/impedit2.cxx
index da4d851..9568e80 100644
--- a/editeng/source/editeng/impedit2.cxx
+++ b/editeng/source/editeng/impedit2.cxx
@@ -477,36 +477,33 @@ bool ImpEditEngine::Command( const CommandEvent& rCEvt, EditView* pView )
    }
    else if ( rCEvt.GetCommand() == CommandEventId::CursorPos )
    {
        if (mpIMEInfos)
        EditPaM aPaM( pView->getImpl().GetEditSelection().Max() );
        tools::Rectangle aR1 = PaMtoEditCursor( aPaM );

        if ( !IsFormatted() )
            FormatDoc();

        ParaPortion* pParaPortion = GetParaPortions().SafeGetObject( GetEditDoc().GetPos( aPaM.GetNode() ) );
        if (pParaPortion)
        {
            EditPaM aPaM( pView->getImpl().GetEditSelection().Max() );
            tools::Rectangle aR1 = PaMtoEditCursor( aPaM );
            sal_Int32 nLine = pParaPortion->GetLines().FindLine( aPaM.GetIndex(), true );
            const EditLine& rLine = pParaPortion->GetLines()[nLine];

            sal_Int32 nInputEnd = mpIMEInfos->aPos.GetIndex() + mpIMEInfos->nLen;
            sal_Int32 nInputEnd;
            if (mpIMEInfos)
                nInputEnd = mpIMEInfos->aPos.GetIndex() + mpIMEInfos->nLen;
            else
                nInputEnd = aPaM.GetIndex();

            if ( !IsFormatted() )
                FormatDoc();

            ParaPortion* pParaPortion = GetParaPortions().SafeGetObject( GetEditDoc().GetPos( aPaM.GetNode() ) );
            if (pParaPortion)
            {
                sal_Int32 nLine = pParaPortion->GetLines().FindLine( aPaM.GetIndex(), true );
                const EditLine& rLine = pParaPortion->GetLines()[nLine];
                if ( nInputEnd > rLine.GetEnd() )
                    nInputEnd = rLine.GetEnd();
                tools::Rectangle aR2 = PaMtoEditCursor( EditPaM( aPaM.GetNode(), nInputEnd ), CursorFlags{ .bEndOfLine = true });
                tools::Rectangle aRect = pView->getImpl().GetWindowPos( aR1 );
                auto nExtTextInputWidth = aR2.Left() - aR1.Right();
                if (EditViewCallbacks* pEditViewCallbacks = pView->getEditViewCallbacks())
                    pEditViewCallbacks->EditViewCursorRect(aRect, nExtTextInputWidth);
                else if (vcl::Window* pWindow = pView->GetWindow())
                    pWindow->SetCursorRect(&aRect, nExtTextInputWidth);
            }
        }
        else
        {
            if (vcl::Window* pWindow = pView->GetWindow())
              pWindow->SetCursorRect();
            if ( nInputEnd > rLine.GetEnd() )
                nInputEnd = rLine.GetEnd();
            tools::Rectangle aR2 = PaMtoEditCursor( EditPaM( aPaM.GetNode(), nInputEnd ), CursorFlags{ .bEndOfLine = true });
            tools::Rectangle aRect = pView->getImpl().GetWindowPos( aR1 );
            auto nExtTextInputWidth = aR2.Left() - aR1.Right();
            if (EditViewCallbacks* pEditViewCallbacks = pView->getEditViewCallbacks())
                pEditViewCallbacks->EditViewCursorRect(aRect, nExtTextInputWidth);
            else if (vcl::Window* pWindow = pView->GetWindow())
                pWindow->SetCursorRect(&aRect, nExtTextInputWidth);
        }
    }
    else if ( rCEvt.GetCommand() == CommandEventId::SelectionChange )