sw/inc/swtable.hxx                           |    7 ++++
 sw/source/core/doc/DocumentFieldsManager.cxx |   12 +-----
 sw/source/core/doc/docsort.cxx               |    5 --
 sw/source/core/doc/tblcpy.cxx                |   17 +--------
 sw/source/core/doc/tblrwcl.cxx               |   14 +-------
 sw/source/core/docnode/ndtbl.cxx             |    4 --
 sw/source/core/edit/edtab.cxx                |    5 --
 sw/source/core/table/swtable.cxx             |   47 +++++++++++++++++++++++++++
 sw/source/core/undo/rolbck.cxx               |   21 ++++++------
 sw/source/core/undo/untbl.cxx                |   11 +++---
 10 files changed, 82 insertions(+), 61 deletions(-)

New commits:
commit b9716112219469fa4e0af33a6edb7eec0ff33026
Author:     Bjoern Michaelsen <bjoern.michael...@libreoffice.org>
AuthorDate: Wed Mar 1 23:15:47 2023 +0100
Commit:     Bjoern Michaelsen <bjoern.michael...@libreoffice.org>
CommitDate: Fri Mar 3 07:55:45 2023 +0000

    Refactor TBL_{REL,}BOXNAME to SwTable
    
    - because those only modify the fields of that one table
    - also add lots of const_cast unfortunately, but not because it makes
      things worse:
      * this was modifying the table before (thus non-const)
      * at least now this is a bit more explicit
    
    Change-Id: I0174daecabcc6de9b7c9867b24c94659c0dbcafe
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148080
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>
    Reviewed-by: Bjoern Michaelsen <bjoern.michael...@libreoffice.org>

diff --git a/sw/inc/swtable.hxx b/sw/inc/swtable.hxx
index 3858c55217be..ee63cf1972e0 100644
--- a/sw/inc/swtable.hxx
+++ b/sw/inc/swtable.hxx
@@ -174,6 +174,8 @@ private:
     void NewSetTabCols( Parm &rP, const SwTabCols &rNew, const SwTabCols &rOld,
                         const SwTableBox *pStart, bool bCurRowOnly );
     void ConvertSubtableBox(sal_uInt16 const nRow, sal_uInt16 const nBox);
+    // Only used for TBL_BOXNAME and TBL_RELBOXNAME for now
+    void UpdateFields(TableFormulaUpdateFlags eFlags);
 
 public:
 
@@ -357,6 +359,11 @@ public:
     // it doesn't contain box content (except single empty nested tables of 
the boxes
     // which could remain after deletion of text content of the selected table)
     bool IsEmpty() const;
+    void SwitchFormulasToExternalRepresentation()
+        { UpdateFields(TBL_BOXNAME); };
+    void SwitchFormulasToRelativeRepresentation()
+        { UpdateFields(TBL_RELBOXNAME); };
+
 
     void dumpAsXml(xmlTextWriterPtr pWriter) const;
 };
diff --git a/sw/source/core/doc/DocumentFieldsManager.cxx 
b/sw/source/core/doc/DocumentFieldsManager.cxx
index 62ed40812160..3ccbbfe00ca4 100644
--- a/sw/source/core/doc/DocumentFieldsManager.cxx
+++ b/sw/source/core/doc/DocumentFieldsManager.cxx
@@ -632,23 +632,15 @@ void DocumentFieldsManager::UpdateTableFields( 
SfxPoolItem* pHt )
                     else
                         pField->ChgValid( false );
                     break;
-                case TBL_BOXNAME:
-                    // is this the wanted table?
-                    if( &pTableNd->GetTable() == pUpdateField->m_pTable )
-                        // to the external representation
-                        pField->PtrToBoxNm( pUpdateField->m_pTable );
-                    break;
                 case TBL_BOXPTR:
                     // to the internal representation
                     // JP 17.06.96: internal representation on all formulas
                     //              (reference to other table!!!)
                     pField->BoxNmToPtr( &pTableNd->GetTable() );
                     break;
+                case TBL_BOXNAME:
                 case TBL_RELBOXNAME:
-                    // is this the wanted table?
-                    if( &pTableNd->GetTable() == pUpdateField->m_pTable )
-                        // to the relative representation
-                        pField->ToRelBoxNm( pUpdateField->m_pTable );
+                    assert(false); // use SwTable::SwitchTo...
                     break;
                 default:
                     break;
diff --git a/sw/source/core/doc/docsort.cxx b/sw/source/core/doc/docsort.cxx
index f4693871c3e3..3601c119bbb3 100644
--- a/sw/source/core/doc/docsort.cxx
+++ b/sw/source/core/doc/docsort.cxx
@@ -506,10 +506,7 @@ bool SwDoc::SortTable(const SwSelBoxes& rBoxes, const 
SwSortOptions& rOpt)
             nStart = 0;
     }
 
-    // Switch to relative Formulas
-    SwTableFormulaUpdate aMsgHint( &pTableNd->GetTable() );
-    aMsgHint.m_eFlags = TBL_RELBOXNAME;
-    getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
+    pTableNd->GetTable().SwitchFormulasToRelativeRepresentation();
 
     // Table as a flat array structure
     FlatFndBox aFlatBox(this, aFndBox);
diff --git a/sw/source/core/doc/tblcpy.cxx b/sw/source/core/doc/tblcpy.cxx
index 0343c9c35b27..c39d9c387a3c 100644
--- a/sw/source/core/doc/tblcpy.cxx
+++ b/sw/source/core/doc/tblcpy.cxx
@@ -673,10 +673,7 @@ bool SwTable::InsNewTable( const SwTable& rCpyTable, const 
SwSelBoxes& rSelBoxes
     aTarget.assignBoxes( aCopyStruct );
 
     {
-        // Change table formulas into relative representation
-        SwTableFormulaUpdate aMsgHint( &rCpyTable );
-        aMsgHint.m_eFlags = TBL_RELBOXNAME;
-        pCpyDoc->getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
+        
const_cast<SwTable*>(&rCpyTable)->SwitchFormulasToRelativeRepresentation();
     }
 
     // delete frames
@@ -722,12 +719,7 @@ bool SwTable::InsTable( const SwTable& rCpyTable, const 
SwNodeIndex& rSttBox,
 
     SwDoc* pCpyDoc = rCpyTable.GetFrameFormat()->GetDoc();
 
-    {
-        // Convert Table formulas to their relative representation
-        SwTableFormulaUpdate aMsgHint( &rCpyTable );
-        aMsgHint.m_eFlags = TBL_RELBOXNAME;
-        pCpyDoc->getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
-    }
+    const_cast<SwTable*>(&rCpyTable)->SwitchFormulasToRelativeRepresentation();
 
     SwTableNumFormatMerge aTNFM( *pCpyDoc, *pDoc );
 
@@ -966,10 +958,7 @@ bool SwTable::InsTable( const SwTable& rCpyTable, const 
SwSelBoxes& rSelBoxes,
     }
 
     {
-        // Convert Table formulas to their relative representation
-        SwTableFormulaUpdate aMsgHint( &rCpyTable );
-        aMsgHint.m_eFlags = TBL_RELBOXNAME;
-        pCpyDoc->getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
+        
const_cast<SwTable*>(&rCpyTable)->SwitchFormulasToRelativeRepresentation();
     }
 
     // Delete the Frames
diff --git a/sw/source/core/doc/tblrwcl.cxx b/sw/source/core/doc/tblrwcl.cxx
index 3f8e46ea7fb0..addf5ec8d41c 100644
--- a/sw/source/core/doc/tblrwcl.cxx
+++ b/sw/source/core/doc/tblrwcl.cxx
@@ -2029,12 +2029,7 @@ void SwTable::CopyHeadlineIntoTable( SwTableNode& 
rTableNd )
     if( aFndBox.GetLines().empty() )
         return;
 
-    {
-        // Convert Table formulas to their relative representation
-        SwTableFormulaUpdate aMsgHint( this );
-        aMsgHint.m_eFlags = TBL_RELBOXNAME;
-        
GetFrameFormat()->GetDoc()->getIDocumentFieldsAccess().UpdateTableFields( 
&aMsgHint );
-    }
+    SwitchFormulasToRelativeRepresentation();
 
     CpyTabFrames aCpyFormat;
     CpyPara aPara( &rTableNd, 1, aCpyFormat );
@@ -2117,12 +2112,7 @@ bool SwTable::MakeCopy( SwDoc& rInsDoc, const 
SwPosition& rPos,
     // Destroy the already created Frames
     pTableNd->DelFrames();
 
-    {
-        // Convert the Table formulas to their relative representation
-        SwTableFormulaUpdate aMsgHint( this );
-        aMsgHint.m_eFlags = TBL_RELBOXNAME;
-        pSrcDoc->getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
-    }
+    const_cast<SwTable*>(this)->SwitchFormulasToRelativeRepresentation();
 
     SwTableNumFormatMerge aTNFM(*pSrcDoc, rInsDoc);
 
diff --git a/sw/source/core/docnode/ndtbl.cxx b/sw/source/core/docnode/ndtbl.cxx
index 60ede1ba576d..fa6598bcfead 100644
--- a/sw/source/core/docnode/ndtbl.cxx
+++ b/sw/source/core/docnode/ndtbl.cxx
@@ -1450,9 +1450,7 @@ bool SwDoc::TableToText( const SwTableNode* pTableNd, 
sal_Unicode cCh )
         pUndo.reset(new SwUndoTableToText( pTableNd->GetTable(), cCh ));
     }
 
-    SwTableFormulaUpdate aMsgHint( &pTableNd->GetTable() );
-    aMsgHint.m_eFlags = TBL_BOXNAME;
-    getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
+    
const_cast<SwTable*>(&pTableNd->GetTable())->SwitchFormulasToExternalRepresentation();
 
     bool bRet = GetNodes().TableToText( aRg, cCh, pUndo.get() );
     if( pUndoRg )
diff --git a/sw/source/core/edit/edtab.cxx b/sw/source/core/edit/edtab.cxx
index 7d4a8a789838..ed653b298896 100644
--- a/sw/source/core/edit/edtab.cxx
+++ b/sw/source/core/edit/edtab.cxx
@@ -327,10 +327,7 @@ bool SwEditShell::GetTableBoxFormulaAttrs( SfxItemSet& 
rSet ) const
             // Convert formulae into external presentation
             const SwTable& rTable = 
pSelBox->GetSttNd()->FindTableNode()->GetTable();
 
-            SwTableFormulaUpdate aTableUpdate( &rTable );
-            aTableUpdate.m_eFlags = TBL_BOXNAME;
-            GetDoc()->getIDocumentFieldsAccess().UpdateTableFields( 
&aTableUpdate );
-
+            
const_cast<SwTable*>(&rTable)->SwitchFormulasToExternalRepresentation();
             rSet.Put( pTableFormat->GetAttrSet() );
         }
         else
diff --git a/sw/source/core/table/swtable.cxx b/sw/source/core/table/swtable.cxx
index aa95d085ae0e..0f98e8a493a4 100644
--- a/sw/source/core/table/swtable.cxx
+++ b/sw/source/core/table/swtable.cxx
@@ -61,6 +61,7 @@
 #include <calbck.hxx>
 #include <o3tl/string_view.hxx>
 #include <svl/numformat.hxx>
+#include <txtfld.hxx>
 
 #ifdef DBG_UTIL
 #define CHECK_TABLE(t) (t).CheckConsistency();
@@ -1612,6 +1613,52 @@ bool SwTable::IsDeleted() const
     return true;
 }
 
+void SwTable::UpdateFields(TableFormulaUpdateFlags eFlags)
+{
+    auto pDoc = GetFrameFormat()->GetDoc();
+    auto pFieldType = 
pDoc->getIDocumentFieldsAccess().GetFieldType(SwFieldIds::Table, OUString(), 
false);
+    if(!pFieldType)
+        return;
+    std::vector<SwFormatField*> vFields;
+    pFieldType->GatherFields(vFields);
+    for(auto pFormatField : vFields)
+    {
+        SwTableField* pField = 
static_cast<SwTableField*>(pFormatField->GetField());
+        // table where this field is located
+        const SwTableNode* pTableNd;
+        const SwTextNode& rTextNd = 
pFormatField->GetTextField()->GetTextNode();
+        pTableNd = rTextNd.FindTableNode();
+        if(pTableNd == nullptr || &pTableNd->GetTable() != this)
+            continue;
+
+        switch(eFlags)
+        {
+            case TBL_BOXNAME:
+                // to the external representation
+                pField->PtrToBoxNm(this);
+                break;
+            case TBL_RELBOXNAME:
+                // to the relative representation
+                pField->ToRelBoxNm(this);
+                break;
+            default:
+                assert(false); // Only TBL_BOXNAME and TBL_RELBOXNAME supported
+                break;
+        }
+    }
+    // process all table box formulas
+    SwTableFormulaUpdate aHint(this);
+    aHint.m_eFlags = eFlags;
+    for(const SfxPoolItem* pItem : 
pDoc->GetAttrPool().GetItemSurrogates(RES_BOXATR_FORMULA))
+    {
+        auto pBoxFormula = dynamic_cast<const SwTableBoxFormula*>(pItem);
+        if(pBoxFormula && pBoxFormula->GetDefinedIn())
+        {
+            const_cast<SwTableBoxFormula*>(pBoxFormula)->ChangeState(&aHint);
+        }
+    }
+}
+
 void SwTable::dumpAsXml(xmlTextWriterPtr pWriter) const
 {
     (void)xmlTextWriterStartElement(pWriter, BAD_CAST("SwTable"));
diff --git a/sw/source/core/undo/rolbck.cxx b/sw/source/core/undo/rolbck.cxx
index 7501f204824e..78547546fb08 100644
--- a/sw/source/core/undo/rolbck.cxx
+++ b/sw/source/core/undo/rolbck.cxx
@@ -102,10 +102,12 @@ SwHistorySetFormat::SwHistorySetFormat( const 
SfxPoolItem* pFormatHt, SwNodeOffs
                     const SwTableNode* pTableNode = pNd->FindTableNode();
                     if (pTableNode)
                     {
-                        SwTableFormulaUpdate aMsgHint( &pTableNode->GetTable() 
);
-                        aMsgHint.m_eFlags = TBL_BOXNAME;
-                        rNew.ChgDefinedIn( rOld.GetDefinedIn() );
-                        rNew.ChangeState( &aMsgHint );
+                        auto pCpyTable = 
const_cast<SwTable*>(&pTableNode->GetTable());
+                        pCpyTable->SwitchFormulasToExternalRepresentation();
+                        rNew.ChgDefinedIn(rOld.GetDefinedIn());
+                        SwTableFormulaUpdate aMsgHint(pCpyTable);
+                        aMsgHint.m_eFlags = TBL_RELBOXNAME;
+                        rNew.ChangeState(&aMsgHint);
                     }
                 }
             }
@@ -876,13 +878,14 @@ SwHistorySetAttrSet::SwHistorySetAttrSet( const 
SfxItemSet& rSet,
                             {
                                 const SwTableNode* pTableNode
                                     = pNd->FindTableNode();
-                                if (pTableNode)
+                                if(pTableNode)
                                 {
-                                    SwTableFormulaUpdate aMsgHint(
-                                        &pTableNode->GetTable() );
+                                    auto pCpyTable = 
const_cast<SwTable*>(&pTableNode->GetTable());
+                                    
pCpyTable->SwitchFormulasToExternalRepresentation();
+                                    rNew.ChgDefinedIn(rOld.GetDefinedIn());
+                                    SwTableFormulaUpdate aMsgHint(pCpyTable);
                                     aMsgHint.m_eFlags = TBL_BOXNAME;
-                                    rNew.ChgDefinedIn( rOld.GetDefinedIn() );
-                                    rNew.ChangeState( &aMsgHint );
+                                    rNew.ChangeState(&aMsgHint);
                                 }
                             }
                         }
diff --git a/sw/source/core/undo/untbl.cxx b/sw/source/core/undo/untbl.cxx
index d1f8c380ccf9..6f40a364c26c 100644
--- a/sw/source/core/undo/untbl.cxx
+++ b/sw/source/core/undo/untbl.cxx
@@ -916,12 +916,13 @@ sal_uInt16 SaveTable::AddFormat( SwFrameFormat* pFormat, 
bool bIsLine )
             pSet->ClearItem( RES_BOXATR_VALUE );
             if (m_pSwTable && m_bSaveFormula)
             {
-                SwTableFormulaUpdate aMsgHint(m_pSwTable);
-                aMsgHint.m_eFlags = TBL_BOXNAME;
+                
const_cast<SwTable*>(m_pSwTable)->SwitchFormulasToExternalRepresentation();
                 SwTableBoxFormula* pFormulaItem = 
const_cast<SwTableBoxFormula*>(pItem);
-                pFormulaItem->ChgDefinedIn( pFormat );
-                pFormulaItem->ChangeState( &aMsgHint );
-                pFormulaItem->ChgDefinedIn( nullptr );
+                pFormulaItem->ChgDefinedIn(pFormat);
+                SwTableFormulaUpdate aMsgHint(m_pSwTable);
+                aMsgHint.m_eFlags = TBL_RELBOXNAME;
+                pFormulaItem->ChangeState(&aMsgHint);
+                pFormulaItem->ChgDefinedIn(nullptr);
             }
         }
         nRet = m_aSets.size();

Reply via email to