diff --git a/sw/qa/extras/uiwriter/uiwriter2.cxx b/sw/qa/extras/uiwriter/uiwriter2.cxx index 854cb288c617..dfa98003007b 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 "beforeafter", 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 xPara = getParagraph(1); - CPPUNIT_ASSERT_EQUAL(OUString("Text"), - getProperty(getRun(xPara, 1), "TextPortionType")); CPPUNIT_ASSERT_EQUAL(OUString("Annotation"), - getProperty(getRun(xPara, 2), "TextPortionType")); + getProperty(getRun(xPara, 1), "TextPortionType")); CPPUNIT_ASSERT_EQUAL(OUString("Frame"), - getProperty(getRun(xPara, 3), "TextPortionType")); + getProperty(getRun(xPara, 2), "TextPortionType")); CPPUNIT_ASSERT_EQUAL(OUString("AnnotationEnd"), - getProperty(getRun(xPara, 4), "TextPortionType")); + getProperty(getRun(xPara, 3), "TextPortionType")); CPPUNIT_ASSERT_EQUAL(OUString("Text"), - getProperty(getRun(xPara, 5), "TextPortionType")); + getProperty(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 ce49c2ec315f..ec000c050552 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 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())); } } }