sw/inc/txatbase.hxx                 |    2 +-
 sw/sdi/swriter.sdi                  |    4 ++--
 sw/source/core/txtnode/atrref.cxx   |   17 ++++++++---------
 sw/source/uibase/fldui/fldmgr.cxx   |    8 ++++++++
 sw/source/uibase/inc/fldmgr.hxx     |   25 +++++++++++++++----------
 sw/source/uibase/shells/basesh.cxx  |   19 +++++++++++++++++--
 sw/source/uibase/shells/textfld.cxx |    9 ++++++++-
 7 files changed, 59 insertions(+), 25 deletions(-)

New commits:
commit d6b8e20c9cdca69d684390c264e775a708777846
Author:     Pranam Lashkari <lpra...@collabora.com>
AuthorDate: Mon Apr 28 16:26:05 2025 +0530
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Thu May 1 10:49:42 2025 +0200

    sw: introduce field parameter to avoid expanding field
    
    problem:
    in online when a reference mark field is inserted and you start
    typing after it, field always expanded.
    
    problem was detected when using zotero citations
    
    patch reverts some part of 58e5e3208a4257a8d9f2e28d8e2d304677aa6980
    
    Change-Id: Ia664b82dae7a2385d9d0a3cf16276bb3ef39e61c
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/184711
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Tested-by: Caolán McNamara <caolan.mcnam...@collabora.com>
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/sw/inc/txatbase.hxx b/sw/inc/txatbase.hxx
index 2b3595709bf1..33702e952a58 100644
--- a/sw/inc/txatbase.hxx
+++ b/sw/inc/txatbase.hxx
@@ -70,7 +70,6 @@ protected:
     SwTextAttr(const SfxPoolItemHolder& rAttr, sal_Int32 nStart );
     virtual ~SwTextAttr() COVERITY_NOEXCEPT_FALSE;
 
-    void SetLockExpandFlag( bool bFlag )    { m_bLockExpandFlag = bFlag; }
     void SetDontMoveAttr( bool bFlag )      { m_bDontMoveAttr = bFlag; }
     void SetCharFormatAttr( bool bFlag )       { m_bCharFormatAttr = bFlag; }
     void SetOverlapAllowedAttr( bool bFlag ){ m_bOverlapAllowedAttr = bFlag; }
@@ -111,6 +110,7 @@ public:
     void SetFormatIgnoreStart(bool bFlag)   { m_bFormatIgnoreStart = bFlag; }
     void SetFormatIgnoreEnd  (bool bFlag)   { m_bFormatIgnoreEnd   = bFlag; }
     bool HasContent() const                 { return m_bHasContent; }
+    void SetLockExpandFlag(bool bFlag)      { m_bLockExpandFlag = bFlag; }
 
     inline const SfxPoolItem& GetAttr() const;
     inline       SfxPoolItem& GetAttr();
diff --git a/sw/sdi/swriter.sdi b/sw/sdi/swriter.sdi
index 8f39d2f8b957..b0c8176596ee 100644
--- a/sw/sdi/swriter.sdi
+++ b/sw/sdi/swriter.sdi
@@ -3012,7 +3012,7 @@ SfxVoidItem InsertEnvelope FN_ENVELOP
 ]
 
 SfxVoidItem InsertField FN_INSERT_FIELD
-(SfxUInt16Item Type FN_PARAM_FIELD_TYPE,SfxUInt16Item SubType 
FN_PARAM_FIELD_SUBTYPE,SfxStringItem Name FN_INSERT_FIELD,SfxStringItem Content 
FN_PARAM_FIELD_CONTENT,SfxUInt32Item Format FN_PARAM_FIELD_FORMAT,SfxStringItem 
Separator FN_PARAM_3, SfxStringItem TypeName FN_PARAM_4, SfxStringItem Wrapper 
FN_PARAM_5)
+(SfxUInt16Item Type FN_PARAM_FIELD_TYPE,SfxUInt16Item SubType 
FN_PARAM_FIELD_SUBTYPE,SfxStringItem Name FN_INSERT_FIELD,SfxStringItem Content 
FN_PARAM_FIELD_CONTENT,SfxUInt32Item Format FN_PARAM_FIELD_FORMAT,SfxStringItem 
Separator FN_PARAM_3, SfxStringItem TypeName FN_PARAM_4, SfxStringItem Wrapper 
FN_PARAM_5, SfxBoolItem NeverExpand FN_PARAM_6)
 [
     AutoUpdate = FALSE,
     FastCall = FALSE,
@@ -6755,7 +6755,7 @@ SfxVoidItem UpdateCurIndex FN_UPDATE_CUR_TOX
 ]
 
 SfxVoidItem UpdateFields FN_UPDATE_FIELDS
-(SfxStringItem TypeName FN_PARAM_1, SfxStringItem NamePrefix FN_PARAM_2, 
SfxUnoAnyItem Fields FN_PARAM_3)
+(SfxStringItem TypeName FN_PARAM_1, SfxStringItem NamePrefix FN_PARAM_2, 
SfxUnoAnyItem Fields FN_PARAM_3, SfxBoolItem NeverExpand FN_PARAM_6)
 [
     AutoUpdate = FALSE,
     FastCall = TRUE,
diff --git a/sw/source/core/txtnode/atrref.cxx 
b/sw/source/core/txtnode/atrref.cxx
index 46e4cf70c579..66cd252f54b8 100644
--- a/sw/source/core/txtnode/atrref.cxx
+++ b/sw/source/core/txtnode/atrref.cxx
@@ -172,10 +172,11 @@ void SwTextRefMark::UpdateFieldContent(SwDoc* pDoc, 
SwWrtShell& rWrtSh, const OU
     const SwTextNode& rTextNode = this->GetTextNode();
     SwPaM aMarkers(SwPosition(rTextNode, *this->End()));
     IDocumentContentOperations& rIDCO = pDoc->getIDocumentContentOperations();
-  /* FIXME: see above re: expanding behavior
-   *this->SetLockExpandFlag(false);
-   *this->SetDontExpand(false);
-   */
+
+    bool oldLockValue = this->IsLockExpandFlag();
+    bool oldDontExpandValue = this->DontExpand();
+    this->SetLockExpandFlag(false);
+    this->SetDontExpand(false);
     if (rIDCO.InsertString(aMarkers, u"XY"_ustr))
     {
         SwPaM aPasteEnd(SwPosition(rTextNode, *this->End()));
@@ -204,11 +205,9 @@ void SwTextRefMark::UpdateFieldContent(SwDoc* pDoc, 
SwWrtShell& rWrtSh, const OU
         rIDCO.DeleteAndJoin(aStartMarker);
         rIDCO.DeleteAndJoin(aEndMarker);
     }
-    // Restore flags.
-  /* FIXME: see above re: expanding behavior
-   *this->SetDontExpand(true);
-   *this->SetLockExpandFlag(true);
-   */
+    this->SetDontExpand(oldDontExpandValue);
+    this->SetLockExpandFlag(oldLockValue);
+
 }
 
 
diff --git a/sw/source/uibase/fldui/fldmgr.cxx 
b/sw/source/uibase/fldui/fldmgr.cxx
index 74ac10c03e78..e36806605a5f 100644
--- a/sw/source/uibase/fldui/fldmgr.cxx
+++ b/sw/source/uibase/fldui/fldmgr.cxx
@@ -76,6 +76,7 @@
 #include <unotools/useroptions.hxx>
 #include <IDocumentContentOperations.hxx>
 #include <translatehelper.hxx>
+#include <txtrfmrk.hxx>
 
 using namespace com::sun::star::uno;
 using namespace com::sun::star::container;
@@ -1088,6 +1089,13 @@ bool SwFieldMgr::InsertField(
                 }
 
                 pCurShell->SetAttrItem( SwFormatRefMark( rData.m_sPar1 ) );
+                if (rData.m_bNeverExpand)
+                {
+                    SwTextRefMark* xTextRefMark = const_cast<SwTextRefMark*>(
+                        
pCurShell->GetRefMark(rData.m_sPar1)->GetTextRefMark());
+                    xTextRefMark->SetDontExpand(true);
+                    xTextRefMark->SetLockExpandFlag(true);
+                }
 
                 if (!bHadMark && !rRefmarkText.isEmpty())
                 {
diff --git a/sw/source/uibase/inc/fldmgr.hxx b/sw/source/uibase/inc/fldmgr.hxx
index 48d4500513c4..ad5ab436ec1f 100644
--- a/sw/source/uibase/inc/fldmgr.hxx
+++ b/sw/source/uibase/inc/fldmgr.hxx
@@ -83,18 +83,23 @@ struct SwInsertField_Data
     weld::Widget* m_pParent; // parent widget used for 
SwWrtShell::StartInputFieldDlg()
     /// Marks the PostIt field's annotation start/end if it differs from the 
cursor selection.
     std::optional<SwPaM> m_oAnnotationRange;
+    bool m_bNeverExpand;
 
     SwInsertField_Data(SwFieldTypesEnum nType, sal_uInt16 nSub, OUString 
aPar1, OUString aPar2,
-                    sal_uInt32 nFormatId, SwWrtShell* pShell = nullptr, 
sal_Unicode cSep = ' ', bool bIsAutoLanguage = true) :
-        m_nTypeId(nType),
-        m_nSubType(nSub),
-        m_sPar1(std::move(aPar1)),
-        m_sPar2(std::move(aPar2)),
-        m_nFormatId(nFormatId),
-        m_pSh(pShell),
-        m_cSeparator(cSep),
-        m_bIsAutomaticLanguage(bIsAutoLanguage),
-        m_pParent(nullptr) {}
+                       sal_uInt32 nFormatId, SwWrtShell* pShell = nullptr, 
sal_Unicode cSep = ' ',
+                       bool bIsAutoLanguage = true, bool bNeverExpand = false)
+        : m_nTypeId(nType)
+        , m_nSubType(nSub)
+        , m_sPar1(std::move(aPar1))
+        , m_sPar2(std::move(aPar2))
+        , m_nFormatId(nFormatId)
+        , m_pSh(pShell)
+        , m_cSeparator(cSep)
+        , m_bIsAutomaticLanguage(bIsAutoLanguage)
+        , m_pParent(nullptr)
+        , m_bNeverExpand(bNeverExpand)
+    {
+    }
 };
 
 class SW_DLLPUBLIC SwFieldMgr
diff --git a/sw/source/uibase/shells/basesh.cxx 
b/sw/source/uibase/shells/basesh.cxx
index 9f5ffbb52eb0..16d738d023ed 100644
--- a/sw/source/uibase/shells/basesh.cxx
+++ b/sw/source/uibase/shells/basesh.cxx
@@ -806,6 +806,13 @@ bool UpdateFieldContents(SfxRequest& rReq, SwWrtShell& 
rWrtSh)
     {
         return false;
     }
+
+    bool bNeverExpand = false;
+    const SfxBoolItem* pNeverExpand = rReq.GetArg<SfxBoolItem>(FN_PARAM_6);
+    if (pNeverExpand)
+    {
+        bNeverExpand = pNeverExpand->GetValue();
+    }
     uno::Sequence<beans::PropertyValues> aFields;
     pFields->GetValue() >>= aFields;
 
@@ -834,14 +841,22 @@ bool UpdateFieldContents(SfxRequest& rReq, SwWrtShell& 
rWrtSh)
             continue;
         }
 
+        auto pTextRefMark = 
const_cast<SwTextRefMark*>(pRefMark->GetTextRefMark());
+        if (bNeverExpand)
+        {
+            pTextRefMark->SetDontExpand(true);
+            pTextRefMark->SetLockExpandFlag(true);
+        }
+
         if (nFieldIndex >= aFields.getLength())
         {
-            break;
+            // earlier we used to break here
+            // but now we need to set expand flags for each refmark
+            continue;
         }
         comphelper::SequenceAsHashMap aMap(aFields[nFieldIndex++]);
         pRefMark->GetRefName() = aMap[u"Name"_ustr].get<OUString>();
 
-        auto pTextRefMark = 
const_cast<SwTextRefMark*>(pRefMark->GetTextRefMark());
         pTextRefMark->UpdateFieldContent(pDoc, rWrtSh, 
aMap[u"Content"_ustr].get<OUString>());
     }
 
diff --git a/sw/source/uibase/shells/textfld.cxx 
b/sw/source/uibase/shells/textfld.cxx
index 42fd111f2684..4f904d3b3b99 100644
--- a/sw/source/uibase/shells/textfld.cxx
+++ b/sw/source/uibase/shells/textfld.cxx
@@ -376,7 +376,14 @@ void SwTextShell::ExecField(SfxRequest &rReq)
                         GetShellPtr()->InsertFootnote(OUString(), 
/*bEndNote=*/true);
                     }
                 }
-                SwInsertField_Data aData(nType, nSubType, aPar1, aPar2, 
nFormat, GetShellPtr(), cSeparator );
+                bool bNeverExpand = false;
+                const SfxBoolItem* pNeverExpand = 
rReq.GetArg<SfxBoolItem>(FN_PARAM_6);
+                if (pNeverExpand)
+                {
+                    bNeverExpand = pNeverExpand->GetValue();
+                }
+                SwInsertField_Data aData(nType, nSubType, aPar1, aPar2, 
nFormat, GetShellPtr(),
+                                         cSeparator, true, bNeverExpand);
                 bRes = aFieldMgr.InsertField( aData );
             }
             else

Reply via email to