sc/qa/uitest/conditional_format/tdf117899.py |    3 +-
 sw/inc/fesh.hxx                              |    2 -
 sw/source/core/frmedt/fecopy.cxx             |    9 ++++--
 sw/source/uibase/dochdl/swdtflvr.cxx         |   38 ++++++++++++++++++++++++++-
 4 files changed, 47 insertions(+), 5 deletions(-)

New commits:
commit c57fe4069a9c2a7646e17372b9a0a1d28d6104be
Author:     Michael Stahl <[email protected]>
AuthorDate: Thu Aug 14 18:35:02 2025 +0200
Commit:     Michael Stahl <[email protected]>
CommitDate: Tue Aug 19 10:47:33 2025 +0200

    tdf#167833 sw: handle more ways of pasting anchored objects
    
    Handle it in SwTransferable::PasteOLE() for embedded objects from
    non-Writer applications, SwTransferable::PasteSdrFormat() for SdrObjects
    from non-Writer applications, and SwTransferable::PasteGrf() for image
    files from non-LO applications.
    
    This is a bit more difficult because some of these call
    SwTransferable::PasteTargetURL() after the SwFEShell function that
    creates the anchored object, and this will only work if the anchored
    object is selected at that time, so the deselection must be done later
    in SwTransferable.
    
    SwTransferable::PasteTargetURL() can also be called directly and would
    need a check added in that case, but i couldn't figure out what to put
    on the clipboard to get there (has to be INET_IMAGE or NETSCAPE_IMAGE).
    
    Change-Id: If068622b0998893f016b84498d0618c112460db5
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/189629
    Reviewed-by: Michael Stahl <[email protected]>
    Tested-by: Jenkins

diff --git a/sc/qa/uitest/conditional_format/tdf117899.py 
b/sc/qa/uitest/conditional_format/tdf117899.py
index 81c270cc895f..bd6da6d7a46e 100644
--- a/sc/qa/uitest/conditional_format/tdf117899.py
+++ b/sc/qa/uitest/conditional_format/tdf117899.py
@@ -40,7 +40,8 @@ class Tdf117899(UITestCase):
 
             self.xUITest.executeCommand(".uno:Copy")
 
-        with self.ui_test.load_empty_file("writer"):
+        with 
self.ui_test.set_config('/org.openoffice.Office.Writer/Cursor/Option/SelectPastedAnchoredObject',
 True):
+          with self.ui_test.load_empty_file("writer"):
 
             self.xUITest.getTopFocusWindow()
 
diff --git a/sw/inc/fesh.hxx b/sw/inc/fesh.hxx
index a907e64a859f..ae25ec87b3ab 100644
--- a/sw/inc/fesh.hxx
+++ b/sw/inc/fesh.hxx
@@ -572,7 +572,7 @@ public:
      Return value indicates if it was converted. */
     bool GetDrawObjGraphic( SotClipboardFormatId nFormat, Graphic& rGrf ) 
const;
 
-    void Paste( SvStream& rStm, SwPasteSdr nAction, const Point* pPt );
+    SwPasteSdr PasteStream(SvStream& rStm, SwPasteSdr nAction, const Point* 
pPt);
     bool Paste( const Graphic &rGrf, const OUString& rURL );
 
     comphelper::OInterfaceContainerHelper3<css::text::XPasteListener>& 
GetPasteListeners();
diff --git a/sw/source/core/frmedt/fecopy.cxx b/sw/source/core/frmedt/fecopy.cxx
index 45bc4d3de6bd..174e60b3187a 100644
--- a/sw/source/core/frmedt/fecopy.cxx
+++ b/sw/source/core/frmedt/fecopy.cxx
@@ -1378,7 +1378,7 @@ static void lcl_ConvertSdrOle2ObjsToSdrGrafObjs( 
SdrModel& _rModel )
     }
 }
 
-void SwFEShell::Paste( SvStream& rStrm, SwPasteSdr nAction, const Point* pPt )
+SwPasteSdr SwFEShell::PasteStream(SvStream & rStrm, SwPasteSdr nAction, const 
Point* pPt)
 {
     CurrShell aCurr( this );
     StartAllAction();
@@ -1568,7 +1568,11 @@ void SwFEShell::Paste( SvStream& rStrm, SwPasteSdr 
nAction, const Point* pPt )
         pView->Paste(*pModel, aPos, nullptr, SdrInsertFlags::NONE);
 
         const size_t nCnt = pView->GetMarkedObjectList().GetMarkCount();
-        if( nCnt )
+        if (nCnt == 0)
+        {
+            nAction = SwPasteSdr::NONE;
+        }
+        else
         {
             const Point aNull( 0, 0 );
             for( size_t i=0; i < nCnt; ++i )
@@ -1599,6 +1603,7 @@ void SwFEShell::Paste( SvStream& rStrm, SwPasteSdr 
nAction, const Point* pPt )
     }
     EndUndo();
     EndAllAction();
+    return nAction;
 }
 
 bool SwFEShell::Paste(const Graphic &rGrf, const OUString& rURL)
diff --git a/sw/source/uibase/dochdl/swdtflvr.cxx 
b/sw/source/uibase/dochdl/swdtflvr.cxx
index de36a47e7d02..017001efbd10 100644
--- a/sw/source/uibase/dochdl/swdtflvr.cxx
+++ b/sw/source/uibase/dochdl/swdtflvr.cxx
@@ -26,6 +26,8 @@
 #include <com/sun/star/embed/MSOLEObjectSystemCreator.hpp>
 #include <com/sun/star/text/XPasteListener.hpp>
 
+#include <officecfg/Office/Writer.hxx>
+
 #include <svtools/svtresid.hxx>
 #include <svtools/strings.hrc>
 #include <svtools/embedtransfer.hxx>
@@ -2580,12 +2582,25 @@ bool SwTransferable::PasteOLE( const 
TransferableDataHelper& rData, SwWrtShell&
             }
             //End of Hack!
 
+            rSh.Push();
             rSh.InsertOleObject( xObjRef );
             bRet = true;
 
             if( bRet && ( nActionFlags & 
SotExchangeActionFlags::InsertTargetUrl) )
                 SwTransferable::PasteTargetURL( rData, rSh, SwPasteSdr::NONE, 
nullptr, false );
 
+            if 
(!officecfg::Office::Writer::Cursor::Option::SelectPastedAnchoredObject::get())
+            {
+                SwTransferable::SetSelInShell(rSh, false, nullptr);
+                // shell cursor was put at the end of the document because the
+                // fly was not yet positioned; restore stack cursor
+                rSh.Pop(SwCursorShell::PopMode::DeleteCurrent);
+            }
+            else
+            {
+                rSh.Pop(SwCursorShell::PopMode::DeleteStack);
+            }
+
             // let the object be unloaded if possible
             SwOLEObj::UnloadObject( xObj, rSh.GetDoc(), 
embed::Aspects::MSOLE_CONTENT );
         }
@@ -2891,11 +2906,17 @@ bool SwTransferable::PasteSdrFormat(  const 
TransferableDataHelper& rData,
             SwTransferable::SetSelInShell( rSh, true, pPt );
         }
 
-        rSh.Paste( *xStrm, nAction, pPt );
+        nAction = rSh.PasteStream(*xStrm, nAction, pPt);
         bRet = true;
 
         if( bRet && ( nActionFlags & SotExchangeActionFlags::InsertTargetUrl ))
             SwTransferable::PasteTargetURL( rData, rSh, SwPasteSdr::NONE, 
nullptr, false );
+
+        if (nAction == SwPasteSdr::Insert &&
+            
!officecfg::Office::Writer::Cursor::Option::SelectPastedAnchoredObject::get())
+        {
+            SwTransferable::SetSelInShell(rSh, false, pPt);
+        }
     }
     return bRet;
 }
@@ -3034,6 +3055,8 @@ bool SwTransferable::PasteGrf( const 
TransferableDataHelper& rData, SwWrtShell&
         {
             case SwPasteSdr::Insert:
             {
+                rSh.Push();
+
                 SwTransferable::SetSelInShell( rSh, false, pPt );
                 rSh.InsertGraphic(sURL, OUString(), aGraphic, nullptr, 
nAnchorType);
                 break;
@@ -3110,6 +3133,19 @@ bool SwTransferable::PasteGrf( const 
TransferableDataHelper& rData, SwWrtShell&
 
         if( nActionFlags & SotExchangeActionFlags::InsertTargetUrl )
             SwTransferable::PasteTargetURL( rData, rSh, SwPasteSdr::NONE, 
nullptr, false );
+
+        if (nAction == SwPasteSdr::Insert)
+        {
+            if 
(!officecfg::Office::Writer::Cursor::Option::SelectPastedAnchoredObject::get())
+            {
+                SwTransferable::SetSelInShell(rSh, false, pPt);
+                rSh.Pop(SwCursorShell::PopMode::DeleteCurrent);
+            }
+            else
+            {
+                rSh.Pop(SwCursorShell::PopMode::DeleteStack);
+            }
+        }
     }
     else if( bCheckForImageMap )
     {

Reply via email to