tdf#148849 sw: fix cursor pos at tracked DeleteRow in Hide Changes

In Hide Changes mode, the text cursor jumped to the start of the
document after tracked deletion of a table row, instead of
remaining in the table in the next table row.

Regression from commit a74c51025fa4519caaf461492e4ed8e68bd34885
"tdf#146962 sw: hide deleted row at deletion in Hide Changes".

Conflicts:
	sw/source/core/frmedt/fetab.cxx
	sw/qa/extras/uiwriter/uiwriter3.cxx

Change-Id: Iff959c9a9940db013827e1c827bcb92c07b4b26a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138683
Tested-by: Jenkins
Reviewed-by: László Németh <nemeth@numbertext.org>
(cherry picked from commit 189aa05c6ea17a8e823b4eab18ea0d1131d9d73e)

Change-Id: I17e11862cf144f150e64082e7c8b22c3877fbc2e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138785
Tested-by: Jenkins
Tested-by: László Németh <nemeth@numbertext.org>
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/sw/qa/extras/uiwriter/data/tdf148849.fodt b/sw/qa/extras/uiwriter/data/tdf148849.fodt
new file mode 100644
index 0000000..07efa8b
--- /dev/null
+++ b/sw/qa/extras/uiwriter/data/tdf148849.fodt
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>

<office:document xmlns:css3t="http://www.w3.org/TR/css3-text/" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:drawooo="http://openoffice.org/2010/draw" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:ooo="http://openoffice.org/2004/office" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:rpt="http://openoffice.org/2005/report" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:officeooo="http://openoffice.org/2009/office" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.text">
 <office:body>
  <office:text>
   <text:tracked-changes text:track-changes="false">
    <text:changed-region xml:id="ct94678778467312" text:id="ct94678778467312">
     <text:deletion>
      <office:change-info>
       <dc:creator>xxx4</dc:creator>
       <dc:date>2021-09-21T14:41:20</dc:date>
      </office:change-info>
     </text:deletion>
    </text:changed-region>
    <text:changed-region xml:id="ct94678778469008" text:id="ct94678778469008">
     <text:insertion>
      <office:change-info>
       <dc:creator>xxx4</dc:creator>
       <dc:date>2021-09-21T14:41:20</dc:date>
      </office:change-info>
     </text:insertion>
    </text:changed-region>
   </text:tracked-changes>
   <text:sequence-decls>
    <text:sequence-decl text:display-outline-level="0" text:name="Illustration"/>
    <text:sequence-decl text:display-outline-level="0" text:name="Table"/>
    <text:sequence-decl text:display-outline-level="0" text:name="Text"/>
    <text:sequence-decl text:display-outline-level="0" text:name="Drawing"/>
    <text:sequence-decl text:display-outline-level="0" text:name="Figure"/>
   </text:sequence-decls>
   <text:p text:style-name="Standard"/>
   <table:table table:name="Table1" table:style-name="Table1">
    <table:table-column table:style-name="Table1.A"/>
    <table:table-row table:style-name="TableLine94678778446544">
     <table:table-cell table:style-name="TableBox94678778448640" office:value-type="string">
      <text:p text:style-name="Standard"><text:change-start text:change-id="ct94678778467312"/>1<text:change-end text:change-id="ct94678778467312"/><text:change-start text:change-id="ct94678778469008"/><text:span text:style-name="T1">2</text:span><text:change-end text:change-id="ct94678778469008"/></text:p>
     </table:table-cell>
    </table:table-row>
    <table:table-row table:style-name="TableLine94678778490512">
     <table:table-cell table:style-name="TableBox94678778448640" office:value-type="string">
      <text:p text:style-name="Standard">Row 2</text:p>
     </table:table-cell>
    </table:table-row>
    <table:table-row table:style-name="TableLine94678778493872">
     <table:table-cell table:style-name="TableBox94678778448640" office:value-type="string">
      <text:p text:style-name="Standard">Row 3</text:p>
     </table:table-cell>
    </table:table-row>
   </table:table>
   <text:p text:style-name="Standard"/>
  </office:text>
 </office:body>
</office:document>
diff --git a/sw/qa/extras/uiwriter/uiwriter3.cxx b/sw/qa/extras/uiwriter/uiwriter3.cxx
index 1d54553..9479e34 100644
--- a/sw/qa/extras/uiwriter/uiwriter3.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter3.cxx
@@ -3530,6 +3530,40 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf146573)
    CPPUNIT_ASSERT_EQUAL(OUString("204"), xCellA4->getString());
}

CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf148849)
{
    // load a document with a table and an empty paragraph before the table
    SwDoc* pDoc = createSwDoc(DATA_DIRECTORY, "tdf148849.fodt");
    SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();

    // record changes
    pDoc->getIDocumentRedlineAccess().SetRedlineFlags(RedlineFlags::On | RedlineFlags::ShowDelete
                                                      | RedlineFlags::ShowInsert);
    CPPUNIT_ASSERT_MESSAGE("redlining should be on",
                           pDoc->getIDocumentRedlineAccess().IsRedlineOn());
    // hide changes
    dispatchCommand(mxComponent, ".uno:ShowTrackedChanges", {});
    CPPUNIT_ASSERT(pWrtShell->GetLayout()->IsHideRedlines());

    uno::Reference<text::XTextTablesSupplier> xTablesSupplier(mxComponent, uno::UNO_QUERY);
    uno::Reference<container::XIndexAccess> xTables(xTablesSupplier->getTextTables(),
                                                    uno::UNO_QUERY);
    CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xTables->getCount());

    // put cursor in the first table row
    pWrtShell->Down(/*bSelect=*/false, /*nCount=*/1);

    // delete a table row
    pWrtShell->DeleteRow();

    // check cursor position

    // This was "", because the text cursor jumped to the start of the document
    // after deleting a table row instead of remaining in the next table row
    SwNode& rNode = pWrtShell->GetCursor()->GetNode();
    CPPUNIT_ASSERT_EQUAL(OUString("Row 2"), rNode.GetTextNode()->GetText());
}

CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf132603)
{
    createSwDoc();
diff --git a/sw/source/core/frmedt/fetab.cxx b/sw/source/core/frmedt/fetab.cxx
index 678ebfc..c505308 100644
--- a/sw/source/core/frmedt/fetab.cxx
+++ b/sw/source/core/frmedt/fetab.cxx
@@ -471,18 +471,11 @@ bool SwFEShell::DeleteRow(bool bCompleteTable)
            if( !pCNd )
                pCNd = GetDoc()->GetNodes().GoNext( &aIdx );

            if( pCNd )
            {
                SwPaM* pPam = GetCursor();
                pPam->GetPoint()->nNode = aIdx;
                pPam->GetPoint()->nContent.Assign( pCNd, 0 );
                pPam->SetMark();            // both want something
                pPam->DeleteMark();
            }

            // remove row frames in Hide Changes mode (and table frames, if needed)
            if ( bRecordAndHideChanges )
            {
                // remove all frames of the table, and make them again without the deleted ones
                // TODO remove only the deleted frames
                pTableNd->DelFrames();
                if ( !pTableNd->GetTable().IsDeleted() )
                {
@@ -491,9 +484,28 @@ bool SwFEShell::DeleteRow(bool bCompleteTable)
                }

                EndAllActionAndCall();

                // put cursor
                SwPaM* pPam = GetCursor();
                pPam->GetPoint()->nNode = aIdx;
                pPam->GetPoint()->nContent.Assign( pCNd, 0 );
                pPam->SetMark();            // both want something
                pPam->DeleteMark();
                if ( SwWrtShell* pWrtShell = dynamic_cast<SwWrtShell*>(this) )
                    pWrtShell->UpdateCursor();

                EndUndo(bCompleteTable ? SwUndoId::UI_TABLE_DELETE : SwUndoId::ROW_DELETE);
                return true;
            }
            else if( pCNd )
            {
                // put cursor
                SwPaM* pPam = GetCursor();
                pPam->GetPoint()->nNode = aIdx;
                pPam->GetPoint()->nContent.Assign( pCNd, 0 );
                pPam->SetMark();            // both want something
                pPam->DeleteMark();
            }
        }

        // now delete the lines