sw/inc/cellatr.hxx                           |    1 
 sw/inc/swtable.hxx                           |    3 +-
 sw/source/core/attr/cellatr.cxx              |   17 +++++++++++----
 sw/source/core/doc/DocumentFieldsManager.cxx |   20 ++++++------------
 sw/source/core/docnode/ndtbl.cxx             |   29 +++++----------------------
 sw/source/core/layout/frmtool.cxx            |    4 ---
 sw/source/core/table/swtable.cxx             |   15 +++++++++++--
 sw/source/core/undo/untbl.cxx                |   23 ++++-----------------
 8 files changed, 46 insertions(+), 66 deletions(-)

New commits:
commit 7c954d09f70d262d44b2c63fff3376dc89722c60
Author:     Bjoern Michaelsen <bjoern.michael...@libreoffice.org>
AuthorDate: Sat Mar 4 16:51:32 2023 +0100
Commit:     Michael Stahl <michael.st...@allotropia.de>
CommitDate: Wed Mar 15 11:07:23 2023 +0000

    Add SwTable::SwitchFormulasToInternalRepresentation
    
    - and get rid of most of the TBL_BOXPTR stuff it replaces
    - instead of using a TBL_BOXPTR on UpdateTableFields
      SwTable::SwitchFormulasToInternalRepresentation should
      be used
    - TBL_BOXPTR is not used via UpdateTableFields anymore and
      an assert assures it will never be
    
    Change-Id: I56eca0e339e0d87a60009ea4dd53f4fe96fe639a
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148249
    Tested-by: Jenkins
    Reviewed-by: Michael Stahl <michael.st...@allotropia.de>

diff --git a/sw/inc/cellatr.hxx b/sw/inc/cellatr.hxx
index f5d4909fd1b6..d1de3bbb9136 100644
--- a/sw/inc/cellatr.hxx
+++ b/sw/inc/cellatr.hxx
@@ -71,6 +71,7 @@ public:
     const SwTableBox* GetTableBox() const
         { return const_cast<SwTableBoxFormula*>(this)->GetTableBox(); }
 
+    void TryBoxNmToPtr();
     void ChangeState( const SfxPoolItem* pItem );
     void Calc( SwTableCalcPara& rCalcPara, double& rValue );
 };
diff --git a/sw/inc/swtable.hxx b/sw/inc/swtable.hxx
index ee63cf1972e0..39e54edb2f33 100644
--- a/sw/inc/swtable.hxx
+++ b/sw/inc/swtable.hxx
@@ -363,7 +363,8 @@ public:
         { UpdateFields(TBL_BOXNAME); };
     void SwitchFormulasToRelativeRepresentation()
         { UpdateFields(TBL_RELBOXNAME); };
-
+    void SwitchFormulasToInternalRepresentation()
+        { UpdateFields(TBL_BOXPTR); }
 
     void dumpAsXml(xmlTextWriterPtr pWriter) const;
 };
diff --git a/sw/source/core/attr/cellatr.cxx b/sw/source/core/attr/cellatr.cxx
index 4e4e9440463f..02b2923b9f88 100644
--- a/sw/source/core/attr/cellatr.cxx
+++ b/sw/source/core/attr/cellatr.cxx
@@ -94,6 +94,16 @@ SwTableBox* SwTableBoxFormula::GetTableBox()
     return m_pDefinedIn ? 
static_cast<SwTableBoxFormat*>(m_pDefinedIn)->GetTableBox() : nullptr;
 }
 
+void SwTableBoxFormula::TryBoxNmToPtr()
+{
+    const SwNode* pNd = GetNodeOfFormula();
+    if (!pNd || &pNd->GetNodes() != &pNd->GetDoc().GetNodes())
+        return;
+    if(const SwTableNode* pTableNd = pNd->FindTableNode())
+    {
+        BoxNmToPtr(&pTableNd->GetTable());
+    }
+}
 void SwTableBoxFormula::ChangeState( const SfxPoolItem* pItem )
 {
     if( !m_pDefinedIn )
@@ -124,13 +134,10 @@ void SwTableBoxFormula::ChangeState( const SfxPoolItem* 
pItem )
         // reset value flag
         ChgValid( false );
         break;
+    case TBL_BOXPTR:
     case TBL_RELBOXNAME:
     case TBL_BOXNAME:
-        assert(false); // PtrToBoxNm and ToRelBoxNm are both public -- use 
just them directly
-        break;
-    case TBL_BOXPTR:
-        // internal rendering
-        BoxNmToPtr( &pTableNd->GetTable() );
+        assert(false); // PtrToBoxNm, ToRelBoxNm and BoxNmToPtr are all public 
-- use just them directly
         break;
 
     case TBL_SPLITTBL:
diff --git a/sw/source/core/doc/DocumentFieldsManager.cxx 
b/sw/source/core/doc/DocumentFieldsManager.cxx
index 3ccbbfe00ca4..6c983e891544 100644
--- a/sw/source/core/doc/DocumentFieldsManager.cxx
+++ b/sw/source/core/doc/DocumentFieldsManager.cxx
@@ -601,14 +601,15 @@ void DocumentFieldsManager::UpdateTableFields( 
SfxPoolItem* pHt )
     OSL_ENSURE( !pHt || RES_TABLEFML_UPDATE  == pHt->Which(),
             "What MessageItem is this?" );
 
+    SwTableFormulaUpdate* pUpdateField = nullptr;
+    if( pHt && RES_TABLEFML_UPDATE == pHt->Which() )
+        pUpdateField = static_cast<SwTableFormulaUpdate*>(pHt);
+    assert(!pUpdateField || pUpdateField->m_eFlags != TBL_BOXPTR); // use 
SwTable::SwitchFormulasToInternalRepresentation
     auto pFieldType = GetFieldType( SwFieldIds::Table, OUString(), false );
     if(pFieldType)
     {
         std::vector<SwFormatField*> vFields;
         pFieldType->GatherFields(vFields);
-        SwTableFormulaUpdate* pUpdateField = nullptr;
-        if( pHt && RES_TABLEFML_UPDATE == pHt->Which() )
-            pUpdateField = static_cast<SwTableFormulaUpdate*>(pHt);
         for(auto pFormatField : vFields)
         {
             SwTableField* pField = 
static_cast<SwTableField*>(pFormatField->GetField());
@@ -633,11 +634,6 @@ void DocumentFieldsManager::UpdateTableFields( 
SfxPoolItem* pHt )
                         pField->ChgValid( false );
                     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:
                     assert(false); // use SwTable::SwitchTo...
@@ -654,11 +650,9 @@ void DocumentFieldsManager::UpdateTableFields( 
SfxPoolItem* pHt )
     // process all table box formulas
     for (const SfxPoolItem* pItem : 
m_rDoc.GetAttrPool().GetItemSurrogates(RES_BOXATR_FORMULA))
     {
-        auto pBoxFormula = dynamic_cast<const SwTableBoxFormula*>(pItem);
-        if( pBoxFormula && pBoxFormula->GetDefinedIn() )
-        {
-            const_cast<SwTableBoxFormula*>(pBoxFormula)->ChangeState( pHt );
-        }
+        auto pBoxFormula = 
const_cast<SwTableBoxFormula*>(pItem->DynamicWhichCast(RES_BOXATR_FORMULA));
+        if(pBoxFormula && pBoxFormula->GetDefinedIn())
+            pBoxFormula->ChangeState(pHt);
     }
 
     SwRootFrame const* pLayout(nullptr);
diff --git a/sw/source/core/docnode/ndtbl.cxx b/sw/source/core/docnode/ndtbl.cxx
index ee071c198d39..116ec8c6f62f 100644
--- a/sw/source/core/docnode/ndtbl.cxx
+++ b/sw/source/core/docnode/ndtbl.cxx
@@ -1718,10 +1718,7 @@ bool SwDoc::InsertCol( const SwSelBoxes& rBoxes, 
sal_uInt16 nCnt, bool bBehind )
     {
         ::sw::UndoGuard const undoGuard(GetIDocumentUndoRedo());
 
-        SwTableFormulaUpdate aMsgHint( &rTable );
-        aMsgHint.m_eFlags = TBL_BOXPTR;
-        getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
-
+        rTable.SwitchFormulasToInternalRepresentation();
         bRet = rTable.InsertCol(*this, rBoxes, nCnt, bBehind);
         if (bRet)
         {
@@ -1772,10 +1769,7 @@ bool SwDoc::InsertRow( const SwSelBoxes& rBoxes, 
sal_uInt16 nCnt, bool bBehind )
     bool bRet(false);
     {
         ::sw::UndoGuard const undoGuard(GetIDocumentUndoRedo());
-
-        SwTableFormulaUpdate aMsgHint( &rTable );
-        aMsgHint.m_eFlags = TBL_BOXPTR;
-        getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
+        rTable.SwitchFormulasToInternalRepresentation();
 
         bRet = rTable.InsertRow( this, rBoxes, nCnt, bBehind );
         if (bRet)
@@ -2098,10 +2092,7 @@ bool SwDoc::DeleteRowCol(const SwSelBoxes& rBoxes, 
RowColMode const eMode)
     bool bRet(false);
     {
         ::sw::UndoGuard const undoGuard(GetIDocumentUndoRedo());
-
-        SwTableFormulaUpdate aMsgHint( &pTableNd->GetTable() );
-        aMsgHint.m_eFlags = TBL_BOXPTR;
-        getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
+        rTable.SwitchFormulasToInternalRepresentation();
 
         if (rTable.IsNewModel())
         {
@@ -2168,10 +2159,7 @@ bool SwDoc::SplitTable( const SwSelBoxes& rBoxes, bool 
bVert, sal_uInt16 nCnt,
     bool bRet(false);
     {
         ::sw::UndoGuard const undoGuard(GetIDocumentUndoRedo());
-
-        SwTableFormulaUpdate aMsgHint( &rTable );
-        aMsgHint.m_eFlags = TBL_BOXPTR;
-        getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
+        rTable.SwitchFormulasToInternalRepresentation();
 
         if (bVert)
             bRet = rTable.SplitCol(*this, rBoxes, nCnt);
@@ -2286,9 +2274,7 @@ TableMergeErr SwDoc::MergeTable( SwPaM& rPam )
         }
 
         // Merge them
-        SwTableFormulaUpdate aMsgHint( &pTableNd->GetTable() );
-        aMsgHint.m_eFlags = TBL_BOXPTR;
-        getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
+        pTableNd->GetTable().SwitchFormulasToInternalRepresentation();
 
         if( pTableNd->GetTable().Merge( this, aBoxes, aMerged, pMergeBox, 
pUndo.get() ))
         {
@@ -3963,10 +3949,7 @@ void SwDoc::SetColRowWidthHeight( SwTableBox& 
rCurrentBox, TableChgWidthHeightTy
     SwTableNode* pTableNd = 
const_cast<SwTableNode*>(rCurrentBox.GetSttNd()->FindTableNode());
     std::unique_ptr<SwUndo> pUndo;
 
-    SwTableFormulaUpdate aMsgHint( &pTableNd->GetTable() );
-    aMsgHint.m_eFlags = TBL_BOXPTR;
-    getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
-
+    pTableNd->GetTable().SwitchFormulasToInternalRepresentation();
     bool const bUndo(GetIDocumentUndoRedo().DoesUndo());
     bool bRet = false;
     switch( extractPosition(eType) )
diff --git a/sw/source/core/layout/frmtool.cxx 
b/sw/source/core/layout/frmtool.cxx
index 966500ecfeac..86816c0f3c7d 100644
--- a/sw/source/core/layout/frmtool.cxx
+++ b/sw/source/core/layout/frmtool.cxx
@@ -1691,9 +1691,7 @@ void InsertCnt_( SwLayoutFrame *pLay, SwDoc *pDoc,
             // #108116# loading may produce table structures that GCLines
             // needs to clean up. To keep table formulas correct, change
             // all table formulas to internal (BOXPTR) representation.
-            SwTableFormulaUpdate aMsgHint( &pTableNode->GetTable() );
-            aMsgHint.m_eFlags = TBL_BOXPTR;
-            pDoc->getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
+            pTableNode->GetTable().SwitchFormulasToInternalRepresentation();
             pTableNode->GetTable().GCLines();
 
             if( pPageMaker )
diff --git a/sw/source/core/table/swtable.cxx b/sw/source/core/table/swtable.cxx
index 0f98e8a493a4..867e76deed1c 100644
--- a/sw/source/core/table/swtable.cxx
+++ b/sw/source/core/table/swtable.cxx
@@ -1641,8 +1641,14 @@ void SwTable::UpdateFields(TableFormulaUpdateFlags 
eFlags)
                 // to the relative representation
                 pField->ToRelBoxNm(this);
                 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;
             default:
-                assert(false); // Only TBL_BOXNAME and TBL_RELBOXNAME supported
+                assert(false); // Only TBL_BOXNAME, TBL_RELBOXNAME and 
TBL_BOXPTR are supported
                 break;
         }
     }
@@ -1651,10 +1657,13 @@ void SwTable::UpdateFields(TableFormulaUpdateFlags 
eFlags)
     aHint.m_eFlags = eFlags;
     for(const SfxPoolItem* pItem : 
pDoc->GetAttrPool().GetItemSurrogates(RES_BOXATR_FORMULA))
     {
-        auto pBoxFormula = dynamic_cast<const SwTableBoxFormula*>(pItem);
+        auto pBoxFormula = 
const_cast<SwTableBoxFormula*>(pItem->DynamicWhichCast(RES_BOXATR_FORMULA));
         if(pBoxFormula && pBoxFormula->GetDefinedIn())
         {
-            const_cast<SwTableBoxFormula*>(pBoxFormula)->ChangeState(&aHint);
+            if(eFlags == TBL_BOXPTR)
+                pBoxFormula->TryBoxNmToPtr();
+            else
+                pBoxFormula->ChangeState(&aHint);
         }
     }
 }
diff --git a/sw/source/core/undo/untbl.cxx b/sw/source/core/undo/untbl.cxx
index 1d135cef7d28..7e9d2614ac02 100644
--- a/sw/source/core/undo/untbl.cxx
+++ b/sw/source/core/undo/untbl.cxx
@@ -1656,10 +1656,7 @@ void SwUndoTableNdsChg::UndoImpl(::sw::UndoRedoContext & 
rContext)
 
     SwTableNode *const pTableNd = aIdx.GetNode().GetTableNode();
     OSL_ENSURE( pTableNd, "no TableNode" );
-
-    SwTableFormulaUpdate aMsgHint( &pTableNd->GetTable() );
-    aMsgHint.m_eFlags = TBL_BOXPTR;
-    rDoc.getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
+    pTableNd->GetTable().SwitchFormulasToInternalRepresentation();
 
     CHECK_TABLE( pTableNd->GetTable() )
 
@@ -1823,10 +1820,8 @@ void SwUndoTableNdsChg::RedoImpl(::sw::UndoRedoContext & 
rContext)
     case SwUndoId::ROW_DELETE:
     case SwUndoId::COL_DELETE:
         {
-            SwTableFormulaUpdate aMsgHint( &pTableNd->GetTable() );
-            aMsgHint.m_eFlags = TBL_BOXPTR;
-            rDoc.getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
             SwTable &rTable = pTableNd->GetTable();
+            rTable.SwitchFormulasToInternalRepresentation();
             if( m_nMax > m_nMin && rTable.IsNewModel() )
                 rTable.PrepareDeleteCol( m_nMin, m_nMax );
             rTable.DeleteSel( &rDoc, aSelBoxes, nullptr, this, true, true );
@@ -1864,9 +1859,7 @@ void SwUndoTableMerge::UndoImpl(::sw::UndoRedoContext & 
rContext)
     SwTableNode *const pTableNd = aIdx.GetNode().GetTableNode();
     OSL_ENSURE( pTableNd, "no TableNode" );
 
-    SwTableFormulaUpdate aMsgHint( &pTableNd->GetTable() );
-    aMsgHint.m_eFlags = TBL_BOXPTR;
-    rDoc.getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
+    pTableNd->GetTable().SwitchFormulasToInternalRepresentation();
 
     // ? TL_CHART2: notification or locking of controller required ?
 
@@ -2856,10 +2849,7 @@ void SwUndoSplitTable::UndoImpl(::sw::UndoRedoContext & 
rContext)
     rPtPos.Assign( m_nTableNode + m_nOffset );
     SwTableNode* pTableNd = rPtPos.GetNode().GetTableNode();
     SwTable& rTable = pTableNd->GetTable();
-
-    SwTableFormulaUpdate aMsgHint( &rTable );
-    aMsgHint.m_eFlags = TBL_BOXPTR;
-    pDoc->getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
+    rTable.SwitchFormulasToInternalRepresentation();
 
     switch( m_nMode )
     {
@@ -2973,10 +2963,7 @@ void SwUndoMergeTable::UndoImpl(::sw::UndoRedoContext & 
rContext)
 
     SwTableNode* pTableNd = rPtPos.GetNode().FindTableNode();
     SwTable* pTable = &pTableNd->GetTable();
-
-    SwTableFormulaUpdate aMsgHint( pTable );
-    aMsgHint.m_eFlags = TBL_BOXPTR;
-    pDoc->getIDocumentFieldsAccess().UpdateTableFields( &aMsgHint );
+    pTable->SwitchFormulasToInternalRepresentation();
 
     // get lines for layout update
     FndBox_ aFndBox( nullptr, nullptr );

Reply via email to