sc/source/ui/inc/viewdata.hxx  |    8 +++++---
 sc/source/ui/view/tabview.cxx  |    8 ++++----
 sc/source/ui/view/tabview2.cxx |    2 +-
 sc/source/ui/view/tabview3.cxx |    4 ++--
 sc/source/ui/view/viewdata.cxx |   14 ++++++++------
 5 files changed, 20 insertions(+), 16 deletions(-)

New commits:
commit 63b2d4aec7f9ceba31a4a14905f64e464ff25b2e
Author:     Marco Cecchetti <[email protected]>
AuthorDate: Sun Jun 22 18:24:36 2025 +0200
Commit:     Marco Cecchetti <[email protected]>
CommitDate: Tue Jun 24 20:59:23 2025 +0200

    lok: CellsAtX, CellsAtY can return wrong values
    
    In lok case the document on the core side is never scrolled, so the
    document position is always the top of the document (that is (0,0))
    That means that the screen width and height can be greater than
    sal_uInt16 max value.
    So, in order to avoid unexpected truncation we need to use tools::Long
    in place of sal_uInt16.
    In fact before this patch we missed to report the correct CellsAtY value
    for cells below row 10000 and the correct CellsAtX value for cells
    beyond column AZZ.
    On cell editing that caused edit area width or height to be not expanded
    properly.
    
    Change-Id: I9f38b55ee9b1edbf9b5f0717155597f363bf9768
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/186799
    Tested-by: Jenkins CollaboraOffice <[email protected]>
    Reviewed-by: Caolán McNamara <[email protected]>
    (cherry picked from commit bfa9d2aacd5188761cbde343748b5f6cd2c12583)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/186916
    Reviewed-by: Marco Cecchetti <[email protected]>
    Tested-by: Jenkins

diff --git a/sc/source/ui/inc/viewdata.hxx b/sc/source/ui/inc/viewdata.hxx
index 403b94e9fa04..c974c31e4f8d 100644
--- a/sc/source/ui/inc/viewdata.hxx
+++ b/sc/source/ui/inc/viewdata.hxx
@@ -28,7 +28,7 @@
 #include <memory>
 #include <o3tl/typed_flags_set.hxx>
 
-#define SC_SIZE_NONE        65535
+constexpr auto SC_SIZE_NONE = std::numeric_limits<tools::Long>::max();
 
 enum class ScFillMode
 {
@@ -629,8 +629,10 @@ public:
     OString         describeCellCursorInPrintTwips() const { return 
describeCellCursorAt(GetCurX(), GetCurY(), false); }
     OString         describeCellCursorAt( SCCOL nCol, SCROW nRow, bool 
bPixelAligned = true ) const;
 
-    SCCOL           CellsAtX( SCCOL nPosX, SCCOL nDir, ScHSplitPos eWhichX, 
sal_uInt16 nScrSizeY = SC_SIZE_NONE ) const;
-    SCROW           CellsAtY( SCROW nPosY, SCROW nDir, ScVSplitPos eWhichY, 
sal_uInt16 nScrSizeX = SC_SIZE_NONE ) const;
+    SCCOL           CellsAtX( SCCOL nPosX, SCCOL nDir, ScHSplitPos eWhichX,
+                              tools::Long nScrSizeY = SC_SIZE_NONE ) const;
+    SCROW           CellsAtY( SCROW nPosY, SCROW nDir, ScVSplitPos eWhichY,
+                              tools::Long nScrSizeX = SC_SIZE_NONE ) const;
 
     SCCOL           VisibleCellsX( ScHSplitPos eWhichX ) const;     // 
Completely visible cell
     SCROW           VisibleCellsY( ScVSplitPos eWhichY ) const;
diff --git a/sc/source/ui/view/tabview.cxx b/sc/source/ui/view/tabview.cxx
index 8eec42624828..e28993653fee 100644
--- a/sc/source/ui/view/tabview.cxx
+++ b/sc/source/ui/view/tabview.cxx
@@ -1776,8 +1776,8 @@ void ScTabView::DoHSplit(tools::Long nSplitPos)
         SCCOL nOldDelta = aViewData.GetPosX( SC_SPLIT_LEFT );
         tools::Long nLeftWidth = nSplitPos - 
pRowBar[SC_SPLIT_BOTTOM]->GetSizePixel().Width();
         if ( nLeftWidth < 0 ) nLeftWidth = 0;
-        SCCOL nNewDelta = nOldDelta + aViewData.CellsAtX( nOldDelta, 1, 
SC_SPLIT_LEFT,
-                        static_cast<sal_uInt16>(nLeftWidth) );
+        SCCOL nNewDelta = nOldDelta + aViewData.CellsAtX( nOldDelta, 1, 
SC_SPLIT_LEFT, nLeftWidth );
+
         ScDocument& rDoc = aViewData.GetDocument();
         if ( nNewDelta > rDoc.MaxCol() )
             nNewDelta = rDoc.MaxCol();
@@ -1849,8 +1849,8 @@ void ScTabView::DoVSplit(tools::Long nSplitPos)
         aViewData.SetPosY( SC_SPLIT_TOP, nOldDelta );
         tools::Long nTopHeight = nSplitPos - 
pColBar[SC_SPLIT_LEFT]->GetSizePixel().Height();
         if ( nTopHeight < 0 ) nTopHeight = 0;
-        SCROW nNewDelta = nOldDelta + aViewData.CellsAtY( nOldDelta, 1, 
SC_SPLIT_TOP,
-                        static_cast<sal_uInt16>(nTopHeight) );
+        SCROW nNewDelta = nOldDelta + aViewData.CellsAtY( nOldDelta, 1, 
SC_SPLIT_TOP, nTopHeight );
+
         ScDocument& rDoc = aViewData.GetDocument();
         if ( nNewDelta > rDoc.MaxRow() )
             nNewDelta = rDoc.MaxRow();
diff --git a/sc/source/ui/view/tabview2.cxx b/sc/source/ui/view/tabview2.cxx
index 63e335ddbc0f..8fb55e1ece22 100644
--- a/sc/source/ui/view/tabview2.cxx
+++ b/sc/source/ui/view/tabview2.cxx
@@ -797,7 +797,7 @@ void ScTabView::GetPageMoveEndPosition(SCCOL nMovX, SCROW 
nMovY, SCCOL& rPageX,
     ScHSplitPos eWhichX = WhichH( eWhich );
     ScVSplitPos eWhichY = WhichV( eWhich );
 
-    sal_uInt16 nScrSizeY = SC_SIZE_NONE;
+    auto nScrSizeY = SC_SIZE_NONE;
     if (comphelper::LibreOfficeKit::isActive() && 
aViewData.GetPageUpDownOffset() > 0) {
         nScrSizeY = ScViewData::ToPixel( aViewData.GetPageUpDownOffset(), 
aViewData.GetPPTX() );
     }
diff --git a/sc/source/ui/view/tabview3.cxx b/sc/source/ui/view/tabview3.cxx
index 09c5e44e0960..1bbb272cced5 100644
--- a/sc/source/ui/view/tabview3.cxx
+++ b/sc/source/ui/view/tabview3.cxx
@@ -1090,14 +1090,14 @@ void ScTabView::AlignToCursor( SCCOL nCurX, SCROW 
nCurY, ScFollowMode eMode,
             case SC_FOLLOW_JUMP_END:
                 if ( nCurX < nDeltaX || nCurX >= nDeltaX+nSizeX )
                 {
-                    nNewDeltaX = nCurX - aViewData.CellsAtX( nCurX, -1, 
eAlignX, static_cast<sal_uInt16>(nSpaceX) );
+                    nNewDeltaX = nCurX - aViewData.CellsAtX( nCurX, -1, 
eAlignX, nSpaceX );
                     if (nNewDeltaX < 0)
                         nNewDeltaX = 0;
                     nSizeX = aViewData.CellsAtX( nNewDeltaX, 1, eAlignX );
                 }
                 if ( nCurY < nDeltaY || nCurY >= nDeltaY+nSizeY || bForceNew )
                 {
-                    nNewDeltaY = nCurY - aViewData.CellsAtY( nCurY, -1, 
eAlignY, static_cast<sal_uInt16>(nSpaceY) );
+                    nNewDeltaY = nCurY - aViewData.CellsAtY( nCurY, -1, 
eAlignY, nSpaceY );
                     if (nNewDeltaY < 0)
                         nNewDeltaY = 0;
                     nSizeY = aViewData.CellsAtY( nNewDeltaY, 1, eAlignY );
diff --git a/sc/source/ui/view/viewdata.cxx b/sc/source/ui/view/viewdata.cxx
index 44d582a50d20..a3b5abfa6939 100644
--- a/sc/source/ui/view/viewdata.cxx
+++ b/sc/source/ui/view/viewdata.cxx
@@ -2677,16 +2677,17 @@ OString ScViewData::describeCellCursorAt(SCCOL nX, 
SCROW nY, bool bPixelAligned)
 }
 
 //      Number of cells on a screen
-SCCOL ScViewData::CellsAtX( SCCOL nPosX, SCCOL nDir, ScHSplitPos eWhichX, 
sal_uInt16 nScrSizeX ) const
+SCCOL ScViewData::CellsAtX( SCCOL nPosX, SCCOL nDir, ScHSplitPos eWhichX, 
tools::Long nScrSizeX ) const
 {
     OSL_ENSURE( nDir==1 || nDir==-1, "wrong CellsAt call" );
+    OSL_ENSURE(nScrSizeX >= 0, "ScViewData::CellsAtX: negative screen width");
 
     if (pView)
         const_cast<ScViewData*>(this)->aScrSize.setWidth( 
pView->GetGridWidth(eWhichX) );
 
     SCCOL  nX;
-    sal_uInt16  nScrPosX = 0;
-    if (nScrSizeX == SC_SIZE_NONE) nScrSizeX = 
static_cast<sal_uInt16>(aScrSize.Width());
+    tools::Long  nScrPosX = 0;
+    if (nScrSizeX == SC_SIZE_NONE) nScrSizeX = aScrSize.Width();
 
     if (nDir==1)
         nX = nPosX;             // forwards
@@ -2705,7 +2706,7 @@ SCCOL ScViewData::CellsAtX( SCCOL nPosX, SCCOL nDir, 
ScHSplitPos eWhichX, sal_uI
             if (nTSize)
             {
                 tools::Long nSizeXPix = ToPixel( nTSize, nPPTX );
-                nScrPosX = sal::static_int_cast<sal_uInt16>( nScrPosX + 
static_cast<sal_uInt16>(nSizeXPix) );
+                nScrPosX = nScrPosX + nSizeXPix;
             }
         }
     }
@@ -2719,14 +2720,15 @@ SCCOL ScViewData::CellsAtX( SCCOL nPosX, SCCOL nDir, 
ScHSplitPos eWhichX, sal_uI
     return nX;
 }
 
-SCROW ScViewData::CellsAtY( SCROW nPosY, SCROW nDir, ScVSplitPos eWhichY, 
sal_uInt16 nScrSizeY ) const
+SCROW ScViewData::CellsAtY( SCROW nPosY, SCROW nDir, ScVSplitPos eWhichY, 
tools::Long nScrSizeY ) const
 {
     OSL_ENSURE( nDir==1 || nDir==-1, "wrong CellsAt call" );
+    OSL_ENSURE(nScrSizeY >= 0, "ScViewData::CellsAtY: negative screen height");
 
     if (pView)
         const_cast<ScViewData*>(this)->aScrSize.setHeight( 
pView->GetGridHeight(eWhichY) );
 
-    if (nScrSizeY == SC_SIZE_NONE) nScrSizeY = 
static_cast<sal_uInt16>(aScrSize.Height());
+    if (nScrSizeY == SC_SIZE_NONE) nScrSizeY = aScrSize.Height();
 
     SCROW nY;
 

Reply via email to