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 2052fe7266fe6183611948a90d7503e49bb76c0d
Author:     Michael Stahl <michael.st...@collabora.com>
AuthorDate: Thu Aug 14 18:35:02 2025 +0200
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Fri Aug 22 12:07:31 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 <michael.st...@collabora.com>
    Tested-by: Jenkins
    (cherry picked from commit c57fe4069a9c2a7646e17372b9a0a1d28d6104be)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/189907
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>

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 833d6510c03f..3453478855d0 100644
--- a/sw/inc/fesh.hxx
+++ b/sw/inc/fesh.hxx
@@ -570,7 +570,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 956efcc9b33c..d7e517441b1d 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 1cef436f973e..86a841d71818 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/embedtransfer.hxx>
 #include <svtools/insdlg.hxx>
 #include <unotools/tempfile.hxx>
@@ -2514,12 +2516,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 );
         }
@@ -2824,11 +2839,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;
 }
@@ -2967,6 +2988,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;
@@ -3043,6 +3066,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