sw/inc/fmtfld.hxx                            |    1 
 sw/inc/usrfld.hxx                            |    1 
 sw/source/core/doc/DocumentFieldsManager.cxx |    4 
 sw/source/core/doc/docfld.cxx                |    4 
 sw/source/core/fields/docufld.cxx            |    2 
 sw/source/core/fields/usrfld.cxx             |    8 +
 sw/source/core/txtnode/atrfld.cxx            |  173 +++++++++++++++------------
 sw/source/core/unocore/unofield.cxx          |    2 
 8 files changed, 117 insertions(+), 78 deletions(-)

New commits:
commit a10d0ce37be8a9f7fe4270d8b4d61567a9418c24
Author:     Bjoern Michaelsen <bjoern.michael...@libreoffice.org>
AuthorDate: Sat Feb 18 00:30:09 2023 +0100
Commit:     Bjoern Michaelsen <bjoern.michael...@libreoffice.org>
CommitDate: Thu Feb 23 10:35:13 2023 +0000

    refactor SwFormatField again
    
    - strip out some helper functions
    - split of the "forced update" special case
    - extract ensuring user fields to be valid
    
    Change-Id: Iee9aa5b25ab7d8e7c102f0c0950c47a46966bb19
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/147269
    Tested-by: Jenkins
    Reviewed-by: Bjoern Michaelsen <bjoern.michael...@libreoffice.org>

diff --git a/sw/inc/fmtfld.hxx b/sw/inc/fmtfld.hxx
index 5807ed8fba70..1efb7665c953 100644
--- a/sw/inc/fmtfld.hxx
+++ b/sw/inc/fmtfld.hxx
@@ -166,6 +166,7 @@ public:
 
     void dumpAsXml(xmlTextWriterPtr pWriter) const override;
 
+    void ForceUpdateTextNode();
     void UpdateTextNode(const SfxPoolItem* pOld, const SfxPoolItem* pNew);
 };
 
diff --git a/sw/inc/usrfld.hxx b/sw/inc/usrfld.hxx
index d9ea9e407440..faedb80671a8 100644
--- a/sw/inc/usrfld.hxx
+++ b/sw/inc/usrfld.hxx
@@ -69,6 +69,7 @@ public:
 
     virtual void        QueryValue( css::uno::Any& rVal, sal_uInt16 nMId ) 
const override;
     virtual void        PutValue( const css::uno::Any& rVal, sal_uInt16 nMId ) 
override;
+    void EnsureValid();
     void dumpAsXml(xmlTextWriterPtr pWriter) const override;
 
 private:
diff --git a/sw/source/core/doc/DocumentFieldsManager.cxx 
b/sw/source/core/doc/DocumentFieldsManager.cxx
index 0c5bcfb375d6..79852fa52ce0 100644
--- a/sw/source/core/doc/DocumentFieldsManager.cxx
+++ b/sw/source/core/doc/DocumentFieldsManager.cxx
@@ -1264,7 +1264,7 @@ void DocumentFieldsManager::UpdateExpFieldsImpl(
                         pInputField->UnlockNotifyContentChange();
                     }
                 });
-            pFormatField->UpdateTextNode(nullptr, nullptr); // trigger 
formatting
+            pFormatField->ForceUpdateTextNode();
         }
 
         if (pUpdateField == pTextField) // if only this one is updated
@@ -1519,7 +1519,7 @@ void DocumentFieldsManager::SetFixFields( const DateTime* 
pNewDateTime )
 
                 // Trigger formatting
                 if( bChgd )
-                    pFormatField->UpdateTextNode(nullptr, nullptr);
+                    pFormatField->ForceUpdateTextNode();
             }
         }
     }
diff --git a/sw/source/core/doc/docfld.cxx b/sw/source/core/doc/docfld.cxx
index 5cae8a66f8ee..45feaca8d581 100644
--- a/sw/source/core/doc/docfld.cxx
+++ b/sw/source/core/doc/docfld.cxx
@@ -984,7 +984,7 @@ void SwDocUpdateField::MakeFieldList_( SwDoc& rDoc, int 
eGetMode )
 
                         sFormula.clear();
                         // trigger formatting
-                        
const_cast<SwFormatField*>(pFormatField)->UpdateTextNode( nullptr, nullptr );
+                        
const_cast<SwFormatField*>(pFormatField)->ForceUpdateTextNode();
                     }
                     break;
 
@@ -1004,7 +1004,7 @@ void SwDocUpdateField::MakeFieldList_( SwDoc& rDoc, int 
eGetMode )
                         // evaluate field
                         const_cast<SwHiddenTextField*>(static_cast<const 
SwHiddenTextField*>(pField))->Evaluate(rDoc);
                         // trigger formatting
-                        
const_cast<SwFormatField*>(pFormatField)->UpdateTextNode(nullptr, nullptr);
+                        
const_cast<SwFormatField*>(pFormatField)->ForceUpdateTextNode();
                     }
                     break;
 
diff --git a/sw/source/core/fields/docufld.cxx 
b/sw/source/core/fields/docufld.cxx
index 28d239de2a1e..e604985e7d0f 100644
--- a/sw/source/core/fields/docufld.cxx
+++ b/sw/source/core/fields/docufld.cxx
@@ -2363,7 +2363,7 @@ void SwRefPageGetFieldType::UpdateField( SwTextField 
const * pTextField,
         }
     }
     // start formatting
-    
const_cast<SwFormatField&>(pTextField->GetFormatField()).UpdateTextNode(nullptr,
 nullptr);
+    
const_cast<SwFormatField&>(pTextField->GetFormatField()).ForceUpdateTextNode();
 }
 
 // queries for relative page numbering
diff --git a/sw/source/core/fields/usrfld.cxx b/sw/source/core/fields/usrfld.cxx
index 0067d3b6d7c4..33a5e7096dc4 100644
--- a/sw/source/core/fields/usrfld.cxx
+++ b/sw/source/core/fields/usrfld.cxx
@@ -371,6 +371,14 @@ void SwUserFieldType::PutValue( const uno::Any& rAny, 
sal_uInt16 nWhichId )
     }
 }
 
+void SwUserFieldType::EnsureValid()
+{
+    if(IsValid())
+        return;
+    SwCalc aCalc(*GetDoc());
+    GetValue(aCalc);
+}
+
 void SwUserFieldType::dumpAsXml(xmlTextWriterPtr pWriter) const
 {
     (void)xmlTextWriterStartElement(pWriter, BAD_CAST("SwUserFieldType"));
diff --git a/sw/source/core/txtnode/atrfld.cxx 
b/sw/source/core/txtnode/atrfld.cxx
index 74bf184b487f..a1794b78d0b2 100644
--- a/sw/source/core/txtnode/atrfld.cxx
+++ b/sw/source/core/txtnode/atrfld.cxx
@@ -35,7 +35,6 @@
 #include <usrfld.hxx>
 #include <expfld.hxx>
 #include <ndtxt.hxx>
-#include <calc.hxx>
 #include <hints.hxx>
 #include <IDocumentFieldsAccess.hxx>
 #include <IDocumentMarkAccess.hxx>
@@ -305,9 +304,93 @@ void SwFormatField::SwClientNotify( const SwModify& 
rModify, const SfxHint& rHin
     }
 }
 
+namespace
+{
+    bool lcl_ExpandField(const SwFieldIds eId, const bool bHiddenParaPrint)
+    {
+        switch(eId)
+        {
+            case SwFieldIds::HiddenPara:
+                return !bHiddenParaPrint;
+            case SwFieldIds::DbSetNumber:
+            case SwFieldIds::DbNumSet:
+            case SwFieldIds::DbNextSet:
+            case SwFieldIds::DatabaseName:
+                return false;
+            default:
+                return true;
+        }
+
+    };
+    bool lcl_TriggerNode(const SwFieldIds eId)
+    {
+        switch(eId)
+        {
+            case SwFieldIds::HiddenPara:
+            case SwFieldIds::DbSetNumber:
+            case SwFieldIds::DbNumSet:
+            case SwFieldIds::DbNextSet:
+            case SwFieldIds::DatabaseName:
+                return true;
+            default:
+                return false;
+        }
+    }
+    void lcl_EnsureUserFieldValid(SwFieldType& rType)
+    {
+        if(rType.Which() != SwFieldIds::User)
+            return;
+        static_cast<SwUserFieldType*>(&rType)->EnsureValid();
+    }
+    bool lcl_NeedsForcedUpdate(const SwField& rField)
+    {
+        if (rField.GetTyp()->Which() == SwFieldIds::DocInfo)
+        {
+            auto pDocInfoField = static_cast<const SwDocInfoField*>(&rField);
+            sal_uInt16 nSubType = pDocInfoField->GetSubType();
+            // Do not consider extended SubTypes.
+            nSubType &= 0xff;
+            switch (nSubType)
+            {
+                case nsSwDocInfoSubType::DI_TITLE:
+                case nsSwDocInfoSubType::DI_SUBJECT:
+                case nsSwDocInfoSubType::DI_CHANGE:
+                case nsSwDocInfoSubType::DI_CUSTOM:
+                    return false;
+            }
+        }
+        return true;
+    }
+}
+
+void SwFormatField::ForceUpdateTextNode()
+{
+    if (!IsFieldInDoc())
+        return;
+
+    SwTextNode* pTextNd = &mpTextField->GetTextNode();
+    OSL_ENSURE(pTextNd, "Where is my Node?");
+
+    auto pType = mpField->GetTyp();
+    lcl_EnsureUserFieldValid(*pType);
+    if(lcl_TriggerNode(pType->Which()))
+        pTextNd->TriggerNodeUpdate(sw::LegacyModifyHint(nullptr, nullptr));
+    if(!lcl_ExpandField(pType->Which(), false))
+        return;
+
+    // Force notify was added for conditional text fields,
+    // at least the below fields need no forced notify.
+    bool bNeedForced = 
lcl_NeedsForcedUpdate(*mpTextField->GetFormatField().GetField());
+    mpTextField->ExpandTextField(bNeedForced);
+}
 void SwFormatField::UpdateTextNode(const SfxPoolItem* pOld, const SfxPoolItem* 
pNew)
 {
-    if (pOld && (RES_REMOVE_UNO_OBJECT == pOld->Which()))
+    if (pOld == nullptr && pNew == nullptr)
+    {
+        ForceUpdateTextNode();
+        return;
+    }
+    else if (pOld && (RES_REMOVE_UNO_OBJECT == pOld->Which()))
     {   // invalidate cached UNO object
         m_wXTextField.clear();
         // ??? why does this Modify method not already do this?
@@ -323,7 +406,7 @@ void SwFormatField::UpdateTextNode(const SfxPoolItem* pOld, 
const SfxPoolItem* p
         return;
 
     SwTextNode* pTextNd = &mpTextField->GetTextNode();
-    OSL_ENSURE( pTextNd, "Where is my Node?" );
+    OSL_ENSURE(pTextNd, "Where is my Node?");
 
     bool bTriggerNode = false;
     bool bExpand = false;
@@ -333,14 +416,6 @@ void SwFormatField::UpdateTextNode(const SfxPoolItem* 
pOld, const SfxPoolItem* p
     {
         switch(pNew->Which())
         {
-        case RES_REFMARKFLD_UPDATE:
-            // update GetRef fields
-            if( SwFieldIds::GetRef == mpField->GetTyp()->Which() )
-            {
-                // #i81002#
-                static_cast<SwGetRefField*>(mpField.get())->UpdateField( 
mpTextField );
-            }
-            break;
         case RES_DOCPOS_UPDATE:
             // handled in SwTextFrame::Modify()
             bTriggerNode = true;
@@ -353,74 +428,28 @@ void SwFormatField::UpdateTextNode(const SfxPoolItem* 
pOld, const SfxPoolItem* p
             pNodeOld = pOld;
             pNodeNew = pNew;
             break;
+        case RES_REFMARKFLD_UPDATE:
+            // update GetRef fields
+            if(SwFieldIds::GetRef == mpField->GetTyp()->Which())
+            {
+                // #i81002#
+                static_cast<SwGetRefField*>(mpField.get())->UpdateField( 
mpTextField );
+            }
+        [[fallthrough]];
         default:
-            break;
-        }
-    }
-    if(!bTriggerNode)
-    {
-        switch (mpField->GetTyp()->Which())
-        {
-            case SwFieldIds::HiddenPara:
-                if( !pOld || pOld->Which() != RES_HIDDENPARA_PRINT ) {
-                    bExpand =true;
-                    break;
-                }
-                [[fallthrough]];
-            case SwFieldIds::DbSetNumber:
-            case SwFieldIds::DbNumSet:
-            case SwFieldIds::DbNextSet:
-            case SwFieldIds::DatabaseName:
-                bTriggerNode = true;
-                pNodeNew = pNew;
-                break;
-            case SwFieldIds::User:
             {
-                SwUserFieldType* pType = 
static_cast<SwUserFieldType*>(mpField->GetTyp());
-                if(!pType->IsValid())
-                {
-                    SwCalc aCalc( pTextNd->GetDoc() );
-                    pType->GetValue( aCalc );
-                }
-                bExpand = true;
+                auto pType = mpField->GetTyp();
+                lcl_EnsureUserFieldValid(*pType);
+                bTriggerNode = lcl_TriggerNode(pType->Which());
+                pNodeNew = pNew;
+                bExpand = lcl_ExpandField(pType->Which(), pOld && 
pOld->Which() == RES_HIDDENPARA_PRINT);
             }
-            break;
-            default:
-                bExpand = true;
-                break;
         }
     }
     if(bTriggerNode)
         pTextNd->TriggerNodeUpdate(sw::LegacyModifyHint(pNodeOld, pNodeNew));
-    if(!bExpand)
-        return;
-
-    bool bForceNotify = pOld == nullptr && pNew == nullptr;
-    if (bForceNotify)
-    {
-        // Force notify was added for conditional text fields, at least the 
below fields need
-        // no forced notify.
-        const SwField* pField = mpTextField->GetFormatField().GetField();
-        const SwFieldIds nWhich = pField->GetTyp()->Which();
-        if (nWhich == SwFieldIds::DocInfo)
-        {
-            auto pDocInfoField = static_cast<const SwDocInfoField*>(pField);
-            sal_uInt16 nSubType = pDocInfoField->GetSubType();
-            // Do not consider extended SubTypes.
-            nSubType &= 0xff;
-            switch (nSubType)
-            {
-                case nsSwDocInfoSubType::DI_TITLE:
-                case nsSwDocInfoSubType::DI_SUBJECT:
-                case nsSwDocInfoSubType::DI_CHANGE:
-                case nsSwDocInfoSubType::DI_CUSTOM:
-                    bForceNotify = false;
-                    break;
-            }
-        }
-    }
-
-    mpTextField->ExpandTextField(bForceNotify);
+    if(bExpand)
+        mpTextField->ExpandTextField(false);
 }
 
 bool SwFormatField::GetInfo( SfxPoolItem& rInfo ) const
diff --git a/sw/source/core/unocore/unofield.cxx 
b/sw/source/core/unocore/unofield.cxx
index f32c01c4b07f..226a595d0aaf 100644
--- a/sw/source/core/unocore/unofield.cxx
+++ b/sw/source/core/unocore/unofield.cxx
@@ -2541,7 +2541,7 @@ void SAL_CALL SwXTextField::update()
             default: break;
         }
         // Text formatting has to be triggered.
-        m_pImpl->GetFormatField()->UpdateTextNode(nullptr, nullptr);
+        m_pImpl->GetFormatField()->ForceUpdateTextNode();
     }
     else
         m_pImpl->m_bCallUpdate = true;

Reply via email to