sc/inc/cellsuno.hxx              |    3 +
 sc/source/ui/unoobj/cellsuno.cxx |   86 ++++++++++++++++++++-------------------
 2 files changed, 49 insertions(+), 40 deletions(-)

New commits:
commit e7895c4a8e630de1ce88b721eef90ba8eb7da179
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Tue Aug 26 09:50:04 2025 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Tue Aug 26 11:43:33 2025 +0200

    Related: tdf#168073 Extract ScEditFieldObj insertion into a function
    
    This makes it easier to see that ScCellObj::insertTextContent attempts
    one of two alternative insertion methods. Additionally, this undoes a
    change of mine, made in commit 1e040b72222012f9a5f1e47d19aaf0c0369e08ad
    (tdf#168073: check if xRange is this, 2025-08-23), which disallowed to
    insert the same ScEditFieldObj secont time. I did that to follow what
    writer does; but the method doesn't specify that behavior, so it was an
    unwarranted change.
    
    Change-Id: Iea0d1ff5039f6e36126a93b0d56cd049ff070945
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/190196
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/sc/inc/cellsuno.hxx b/sc/inc/cellsuno.hxx
index 63640be1351d..032916e44199 100644
--- a/sc/inc/cellsuno.hxx
+++ b/sc/inc/cellsuno.hxx
@@ -670,6 +670,9 @@ private:
 
     SvxUnoTextRangeBase*
     getSvxUnoTextRange(const css::uno::Reference<css::text::XTextRange>& 
xRange);
+    bool insertScEditFieldObj(const 
css::uno::Reference<css::text::XTextRange>& xRange,
+                              const 
css::uno::Reference<css::text::XTextContent>& xContent,
+                              bool bAbsorb);
 
 public:
     static const SvxItemPropertySet* GetEditPropertySet();
diff --git a/sc/source/ui/unoobj/cellsuno.cxx b/sc/source/ui/unoobj/cellsuno.cxx
index a713268d4f67..c69fca51af52 100644
--- a/sc/source/ui/unoobj/cellsuno.cxx
+++ b/sc/source/ui/unoobj/cellsuno.cxx
@@ -5920,55 +5920,61 @@ void SAL_CALL ScCellObj::insertControlCharacter( const 
uno::Reference<text::XTex
     GetUnoText().insertControlCharacter(getSvxUnoTextRange(xRange), 
nControlCharacter, bAbsorb);
 }
 
-void SAL_CALL ScCellObj::insertTextContent( const 
uno::Reference<text::XTextRange >& xRange,
-                                                const 
uno::Reference<text::XTextContent >& xContent,
-                                                sal_Bool bAbsorb )
+bool ScCellObj::insertScEditFieldObj(const uno::Reference<text::XTextRange>& 
xRange,
+                                     const uno::Reference<text::XTextContent>& 
xContent,
+                                     bool bAbsorb)
 {
-    SolarMutexGuard aGuard;
+    DBG_TESTSOLARMUTEX();
+    ScDocShell* pDocSh = GetDocShell();
+    if (!pDocSh)
+        return false;
     ScEditFieldObj* pCellField = dynamic_cast<ScEditFieldObj*>(xContent.get());
-    if (pCellField && pCellField->IsInserted())
-        throw lang::IllegalArgumentException(u"Content already inserted"_ustr, 
getXWeak(), 1);
-    if (ScDocShell* pDocSh = GetDocShell(); pDocSh && pCellField)
-    {
-        if (auto* pTextRange = getSvxUnoTextRange(xRange))
-        {
-            SvxEditSource* pEditSource = pTextRange->GetEditSource();
-            ESelection aSelection(pTextRange->GetSelection());
+    if (!pCellField || pCellField->IsInserted())
+        return false;
+    SvxUnoTextRangeBase* pTextRange = getSvxUnoTextRange(xRange);
+    if (!pTextRange)
+        return false;
 
-            if (!bAbsorb)
-            {
-                //  do not replace -> append
-                aSelection.Adjust();
-                aSelection.CollapseToEnd();
-            }
+    SvxEditSource* pEditSource = pTextRange->GetEditSource();
+    ESelection aSelection(pTextRange->GetSelection());
 
-            if (pCellField->GetFieldType() == text::textfield::Type::TABLE)
-                pCellField->setPropertyValue(SC_UNONAME_TABLEPOS, 
uno::Any(sal_Int32(aCellPos.Tab())));
+    if (!bAbsorb)
+    {
+        //  do not replace -> append
+        aSelection.Adjust();
+        aSelection.CollapseToEnd();
+    }
 
-            SvxFieldItem aItem = pCellField->CreateFieldItem();
-            SvxTextForwarder* pForwarder = pEditSource->GetTextForwarder();
-            pForwarder->QuickInsertField( aItem, aSelection );
-            pEditSource->UpdateData();
+    if (pCellField->GetFieldType() == text::textfield::Type::TABLE)
+        pCellField->setPropertyValue(SC_UNONAME_TABLEPOS, 
uno::Any(sal_Int32(aCellPos.Tab())));
 
-            //  new selection: a digit
-            aSelection.Adjust();
-            aSelection.end.nPara = aSelection.start.nPara;
-            aSelection.end.nIndex = aSelection.start.nIndex + 1;
-            uno::Reference<text::XTextRange> xParent(this);
-            pCellField->InitDoc(
-                xParent, std::make_unique<ScCellEditSource>(pDocSh, aCellPos), 
aSelection);
+    SvxFieldItem aItem = pCellField->CreateFieldItem();
+    SvxTextForwarder* pForwarder = pEditSource->GetTextForwarder();
+    pForwarder->QuickInsertField(aItem, aSelection);
+    pEditSource->UpdateData();
 
-            //  for bAbsorb=FALSE, the new selection must be behind the 
inserted content
-            //  (the xml filter relies on this)
-            if (!bAbsorb)
-                aSelection.start.nIndex = aSelection.end.nIndex;
+    //  new selection: a digit
+    aSelection.Adjust();
+    aSelection.end.nPara = aSelection.start.nPara;
+    aSelection.end.nIndex = aSelection.start.nIndex + 1;
+    uno::Reference<text::XTextRange> xParent(this);
+    pCellField->InitDoc(xParent, std::make_unique<ScCellEditSource>(pDocSh, 
aCellPos), aSelection);
 
-            pTextRange->SetSelection( aSelection );
+    //  for bAbsorb=FALSE, the new selection must be behind the inserted 
content
+    //  (the xml filter relies on this)
+    if (!bAbsorb)
+        aSelection.start.nIndex = aSelection.end.nIndex;
+    pTextRange->SetSelection(aSelection);
+    return true;
+}
 
-            return;
-        }
-    }
-    GetUnoText().insertTextContent(xRange, xContent, bAbsorb);
+void SAL_CALL ScCellObj::insertTextContent( const 
uno::Reference<text::XTextRange >& xRange,
+                                                const 
uno::Reference<text::XTextContent >& xContent,
+                                                sal_Bool bAbsorb )
+{
+    SolarMutexGuard aGuard;
+    if (!insertScEditFieldObj(xRange, xContent, bAbsorb))
+        GetUnoText().insertTextContent(xRange, xContent, bAbsorb);
 }
 
 void SAL_CALL ScCellObj::removeTextContent( const 
uno::Reference<text::XTextContent>& xContent )

Reply via email to