sc/qa/unit/tiledrendering/tiledrendering2.cxx |   28 ++++++++++++++++++++++++++
 sc/source/ui/view/viewfun2.cxx                |    8 +++++++
 2 files changed, 36 insertions(+)

New commits:
commit 3f510862bfbcfa4b2b7d94ce7ad917016e5662fe
Author:     Jaume Pujantell <jaume.pujant...@collabora.com>
AuthorDate: Tue Mar 25 10:05:02 2025 +0100
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Wed Mar 26 08:54:10 2025 +0100

    sc: avoid cursor jump on failed search
    
    When using the command .uno:ExecuteSearch with the SearchStartPointX/Y
    options, the cursor would change position even if no matching string is
    found in the sheet.
    
    Change-Id: I94471b79b00a882489e0c92295c8359d68ee3372
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/183292
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>

diff --git a/sc/qa/unit/tiledrendering/tiledrendering2.cxx 
b/sc/qa/unit/tiledrendering/tiledrendering2.cxx
index 6e8ce9ea9def..dd3529df8187 100644
--- a/sc/qa/unit/tiledrendering/tiledrendering2.cxx
+++ b/sc/qa/unit/tiledrendering/tiledrendering2.cxx
@@ -13,6 +13,7 @@
 
 #include <vcl/scheduler.hxx>
 #include <comphelper/propertyvalue.hxx>
+#include <comphelper/propertysequence.hxx>
 
 using namespace com::sun::star;
 
@@ -78,6 +79,33 @@ CPPUNIT_TEST_FIXTURE(ScTiledRenderingTest, 
testCopyMultiSelection)
     CPPUNIT_ASSERT(!xTransferable2->isComplex());
 }
 
+CPPUNIT_TEST_FIXTURE(ScTiledRenderingTest, testCursorJumpOnFailedSearch)
+{
+    createDoc("empty.ods");
+    ViewCallback aView;
+
+    // Go to lower cell
+    uno::Sequence<beans::PropertyValue> aPropertyValues = {
+        comphelper::makePropertyValue(u"ToPoint"_ustr, u"$C$3"_ustr),
+    };
+    dispatchCommand(mxComponent, u".uno:GoToCell"_ustr, aPropertyValues);
+
+    tools::Rectangle aInitialCursor = aView.m_aCellCursorBounds;
+
+    // Search for a non-existing string using the start point parameters
+    aPropertyValues = comphelper::InitPropertySequence(
+        { { "SearchItem.SearchString", uno::Any(u"No-existing"_ustr) },
+          { "SearchItem.Backward", uno::Any(false) },
+          { "SearchItem.SearchStartPointX", 
uno::Any(static_cast<sal_Int32>(100)) },
+          { "SearchItem.SearchStartPointY", 
uno::Any(static_cast<sal_Int32>(100)) } });
+    dispatchCommand(mxComponent, u".uno:ExecuteSearch"_ustr, aPropertyValues);
+
+    tools::Rectangle aFinalCursor = aView.m_aCellCursorBounds;
+
+    // Without the fix, the cursor jumps even when no match is found
+    CPPUNIT_ASSERT_EQUAL(aInitialCursor, aFinalCursor);
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/view/viewfun2.cxx b/sc/source/ui/view/viewfun2.cxx
index 3fad2954ca6c..174b6ac4143d 100644
--- a/sc/source/ui/view/viewfun2.cxx
+++ b/sc/source/ui/view/viewfun2.cxx
@@ -1970,6 +1970,9 @@ bool ScViewFunc::SearchAndReplace( const SvxSearchItem* 
pSearchItem,
     if (bAddUndo && !rDoc.IsUndoEnabled())
         bAddUndo = false;
 
+    bool bCursorMoved = false;
+    SCCOL nOrigPosX = GetViewData().GetCurX();
+    SCROW nOrigPosY = GetViewData().GetCurY();
     if ( !rMark.IsMarked() && !rMark.IsMultiMarked() && 
(pSearchItem->HasStartPoint()) )
     {
         // No selection -> but we have a start point (top left corner of the
@@ -1985,6 +1988,7 @@ bool ScViewFunc::SearchAndReplace( const SvxSearchItem* 
pSearchItem,
 
         AlignToCursor( nPosX, nPosY, SC_FOLLOW_JUMP );
         SetCursor( nPosX, nPosY, true );
+        bCursorMoved = true;
     }
 
     SCCOL nCol, nOldCol;
@@ -2260,6 +2264,10 @@ bool ScViewFunc::SearchAndReplace( const SvxSearchItem* 
pSearchItem,
             pDocSh->PostPaintGridAll();                             // mark
         GetFrameWin()->LeaveWait();
     }
+    else if (bCursorMoved)
+    {
+        SetCursor(nOrigPosX, nOrigPosY, true);
+    }
     return bFound;
 }
 

Reply via email to