sw comments on frames: fix comment insert for as-char frame at para start

This adapts SwWrtShell::InsertPostIt() to behave similar to commit
86fd893e32ef7a737b2c4b60e0938146b102fc07 (sw comments on frames: delete
comment of frame when deleting frame, 2019-07-03), i.e. instead of
hoping that the cursor will be at the end of paragraph and traveling
back, just set the cursor to the remembered anchor position.

This is cleaner, and as a side-effect also fixes the scenario when
creating a comment on an as-char image, which happens to be at the start
of the paragraph.

Change-Id: Iedca0feb62242677b6e8b69ef7b813d6da72c8eb
Reviewed-on: https://gerrit.libreoffice.org/75093
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
diff --git a/sw/qa/extras/uiwriter/uiwriter2.cxx b/sw/qa/extras/uiwriter/uiwriter2.cxx
index 854cb28..dfa9800 100644
--- a/sw/qa/extras/uiwriter/uiwriter2.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter2.cxx
@@ -1569,6 +1569,12 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest2, testImageComment)
    SwDoc* pDoc = createDoc("image-comment.odt");
    SwView* pView = pDoc->GetDocShell()->GetView();

    // Test document has "before<image>after", remove the content before the image.
    SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
    pWrtShell->SttEndDoc(/*bStart=*/true);
    pWrtShell->Right(CRSR_SKIP_CHARS, /*bSelect=*/true, 6, /*bBasicCall=*/false);
    pWrtShell->Delete();

    // Select the image.
    pView->GetViewFrame()->GetDispatcher()->Execute(FN_CNTNT_TO_NEXT_FRAME, SfxCallMode::SYNCHRON);

@@ -1578,17 +1584,17 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest2, testImageComment)
    // Verify that the comment is around the image.
    // Without the accompanying fix in place, this test would have failed, as FN_POSTIT was disabled
    // in the frame shell.
    // Then this test would have failed, as in case the as-char anchored image was at the start of
    // the paragraph, the comment of the image covered the character after the image, not the image.
    uno::Reference<text::XTextRange> xPara = getParagraph(1);
    CPPUNIT_ASSERT_EQUAL(OUString("Text"),
                         getProperty<OUString>(getRun(xPara, 1), "TextPortionType"));
    CPPUNIT_ASSERT_EQUAL(OUString("Annotation"),
                         getProperty<OUString>(getRun(xPara, 2), "TextPortionType"));
                         getProperty<OUString>(getRun(xPara, 1), "TextPortionType"));
    CPPUNIT_ASSERT_EQUAL(OUString("Frame"),
                         getProperty<OUString>(getRun(xPara, 3), "TextPortionType"));
                         getProperty<OUString>(getRun(xPara, 2), "TextPortionType"));
    CPPUNIT_ASSERT_EQUAL(OUString("AnnotationEnd"),
                         getProperty<OUString>(getRun(xPara, 4), "TextPortionType"));
                         getProperty<OUString>(getRun(xPara, 3), "TextPortionType"));
    CPPUNIT_ASSERT_EQUAL(OUString("Text"),
                         getProperty<OUString>(getRun(xPara, 5), "TextPortionType"));
                         getProperty<OUString>(getRun(xPara, 4), "TextPortionType"));

    // Insert content to the comment, and select the image again.
    SfxStringItem aItem(FN_INSERT_STRING, "x");
diff --git a/sw/source/uibase/wrtsh/wrtsh1.cxx b/sw/source/uibase/wrtsh/wrtsh1.cxx
index ce49c2e..ec000c0 100644
--- a/sw/source/uibase/wrtsh/wrtsh1.cxx
+++ b/sw/source/uibase/wrtsh/wrtsh1.cxx
@@ -1912,6 +1912,21 @@ void SwWrtShell::InsertPostIt(SwFieldMgr& rFieldMgr, SfxRequest& rReq)
        {
            SwFlyFrame* pFly = GetSelectedFlyFrame();

            // Remember the anchor of the selected object before deletion.
            std::unique_ptr<SwPosition> pAnchor;
            if (pFly)
            {
                SwFrameFormat* pFormat = pFly->GetFormat();
                if (pFormat)
                {
                    RndStdIds eAnchorId = pFormat->GetAnchor().GetAnchorId();
                    if ((eAnchorId == RndStdIds::FLY_AS_CHAR || eAnchorId == RndStdIds::FLY_AT_CHAR) && pFormat->GetAnchor().GetContentAnchor())
                    {
                        pAnchor.reset(new SwPosition(*pFormat->GetAnchor().GetContentAnchor()));
                    }
                }
            }

            // A frame is selected, end frame selection.
            EnterStdMode();
            GetView().AttrChangedNotify(this);
@@ -1920,6 +1935,7 @@ void SwWrtShell::InsertPostIt(SwFieldMgr& rFieldMgr, SfxRequest& rReq)
            // comment.
            if (pFly)
            {
                *GetCurrentShellCursor().GetPoint() = *pAnchor;
                SwFrameFormat* pFormat = pFly->GetFormat();
                if (pFormat && pFormat->GetAnchor().GetAnchorId() == RndStdIds::FLY_AS_CHAR)
                {
@@ -1927,21 +1943,8 @@ void SwWrtShell::InsertPostIt(SwFieldMgr& rFieldMgr, SfxRequest& rReq)
                }
                else if (pFormat && pFormat->GetAnchor().GetAnchorId() == RndStdIds::FLY_AT_CHAR)
                {
                    // Ending the frame selection positions the cursor at the end of the paragraph,
                    // move it to the anchor position.
                    sal_Int32 nCursor = GetCurrentShellCursor().GetPoint()->nContent.GetIndex();
                    const SwPosition* pAnchor = pFormat->GetAnchor().GetContentAnchor();
                    if (pAnchor)
                    {
                        sal_Int32 nDiff = nCursor - pAnchor->nContent.GetIndex();
                        if (nDiff > 0)
                        {
                            Left(CRSR_SKIP_CELLS, /*bSelect=*/false, nDiff, /*bBasicCall=*/false,
                                 /*bVisual=*/true);
                            aData.m_pAnnotationRange.reset(new SwPaM(
                                *GetCurrentShellCursor().Start(), *GetCurrentShellCursor().End()));
                        }
                    }
                    aData.m_pAnnotationRange.reset(new SwPaM(*GetCurrentShellCursor().Start(),
                                                             *GetCurrentShellCursor().End()));
                }
            }
        }