sw/inc/crsrsh.hxx                    |   11 +++++++--
 sw/qa/extras/uiwriter/uiwriter.cxx   |    6 ++---
 sw/qa/extras/uiwriter/uiwriter3.cxx  |    2 -
 sw/source/core/crsr/crstrvl.cxx      |   41 +++++++++++++++++++++++++++++++++--
 sw/source/uibase/docvw/edtwin.cxx    |   17 +++++++-------
 sw/source/uibase/shells/textfld.cxx  |    2 -
 sw/source/uibase/uitest/uiobject.cxx |   29 ++++++++++++++----------
 7 files changed, 79 insertions(+), 29 deletions(-)

New commits:
commit 112592ac580108998a2cd99ae9bbf376c80c10d8
Author:     Michael Stahl <michael.st...@allotropia.de>
AuthorDate: Fri Mar 4 21:53:38 2022 +0100
Commit:     Michael Stahl <michael.st...@allotropia.de>
CommitDate: Mon Mar 7 13:37:32 2022 +0100

    sw_redlinehide: more issues with SwEditWin Surrounding functions
    
    .. and SwEditWinUIObject.
    
    These need to work with TextFrameIndex.  Add some functions to
    SwCursorShell to make it possible.
    
    Change-Id: I884c4822a0e7ecf254ea09a893762e1e6d539534
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131042
    Tested-by: Jenkins
    Reviewed-by: Michael Stahl <michael.st...@allotropia.de>

diff --git a/sw/inc/crsrsh.hxx b/sw/inc/crsrsh.hxx
index dc633a7bc9f2..6d843e7e424d 100644
--- a/sw/inc/crsrsh.hxx
+++ b/sw/inc/crsrsh.hxx
@@ -26,6 +26,9 @@
 #include <vcl/keycod.hxx>
 #include <o3tl/typed_flags_set.hxx>
 
+#ifdef SW_DLLIMPLEMENTATION
+#include <TextFrameIndex.hxx>
+#endif
 #include "IShellCursorSupplier.hxx"
 #include "swdllapi.h"
 #include "docary.hxx"
@@ -789,8 +792,12 @@ public:
     bool GotoINetAttr( const SwTextINetFormat& rAttr );
     const SwFormatINetFormat* FindINetAttr( std::u16string_view rName ) const;
 
-    bool SelectText( const sal_Int32 nStart,
-                        const sal_Int32 nEnd );
+    bool SelectTextModel(sal_Int32 nStart, sal_Int32 nEnd);
+#ifdef SW_DLLIMPLEMENTATION
+    bool SelectTextView(TextFrameIndex nStart, TextFrameIndex nEnd);
+    // result is only valid while cursor isn't moved!
+    TextFrameIndex GetCursorPointAsViewIndex() const;
+#endif
 
     bool CheckTableBoxContent( const SwPosition* pPos = nullptr );
     void SaveTableBoxContent( const SwPosition* pPos = nullptr );
diff --git a/sw/qa/extras/uiwriter/uiwriter.cxx 
b/sw/qa/extras/uiwriter/uiwriter.cxx
index 609db65392e7..90e9d9f9902b 100644
--- a/sw/qa/extras/uiwriter/uiwriter.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter.cxx
@@ -3389,7 +3389,7 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest, 
testUndoDelAsCharTdf107512)
     CPPUNIT_ASSERT(rIDCO.InsertGraphicObject(*pShell->GetCursor(), grf, 
&frameSet, &grfSet));
     CPPUNIT_ASSERT_EQUAL(size_t(2), pDoc->GetFlyCount(FLYCNTTYPE_GRF));
     SvxCharHiddenItem hidden(true, RES_CHRATR_HIDDEN);
-    pShell->SelectText(1, 4);
+    pShell->SelectTextModel(1, 4);
     rIDCO.InsertPoolItem(*pShell->GetCursor(), hidden);
     // now we have "\1foo\1" with the "foo" hidden
     
CPPUNIT_ASSERT(pShell->GetCursor()->GetNode().GetTextNode()->GetTextAttrForCharAt(0,
 RES_TXTATR_FLYCNT));
@@ -3403,7 +3403,7 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest, 
testUndoDelAsCharTdf107512)
     query.ClearItem(RES_CHRATR_HIDDEN);
 
     // delete from the start
-    pShell->SelectText(0, 4);
+    pShell->SelectTextModel(0, 4);
     rIDCO.DeleteAndJoin(*pShell->GetCursor());
     
CPPUNIT_ASSERT(pShell->GetCursor()->GetNode().GetTextNode()->GetTextAttrForCharAt(0,
 RES_TXTATR_FLYCNT));
     CPPUNIT_ASSERT_EQUAL(size_t(1), pDoc->GetFlyCount(FLYCNTTYPE_GRF));
@@ -3446,7 +3446,7 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest, 
testUndoDelAsCharTdf107512)
     query.ClearItem(RES_CHRATR_HIDDEN);
 
     // delete from the end
-    pShell->SelectText(1, 5);
+    pShell->SelectTextModel(1, 5);
     rIDCO.DeleteAndJoin(*pShell->GetCursor());
     
CPPUNIT_ASSERT(pShell->GetCursor()->GetNode().GetTextNode()->GetTextAttrForCharAt(0,
 RES_TXTATR_FLYCNT));
     CPPUNIT_ASSERT_EQUAL(size_t(1), pDoc->GetFlyCount(FLYCNTTYPE_GRF));
diff --git a/sw/qa/extras/uiwriter/uiwriter3.cxx 
b/sw/qa/extras/uiwriter/uiwriter3.cxx
index c5257d5fbbae..28fbddc1147f 100644
--- a/sw/qa/extras/uiwriter/uiwriter3.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter3.cxx
@@ -2476,7 +2476,7 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf140731)
 
     SwCursorShell* pShell(pDoc->GetEditShell());
 
-    pShell->SelectText(1, 500);
+    pShell->SelectTextModel(1, 500);
 
     dispatchCommand(mxComponent, ".uno:ChangeCaseToTitleCase", {});
     Scheduler::ProcessEventsToIdle();
diff --git a/sw/source/core/crsr/crstrvl.cxx b/sw/source/core/crsr/crstrvl.cxx
index 7fa05b1920e3..7934ad3bc5da 100644
--- a/sw/source/core/crsr/crstrvl.cxx
+++ b/sw/source/core/crsr/crstrvl.cxx
@@ -1996,7 +1996,7 @@ bool SwContentAtPos::IsInRTLText()const
     return bRet;
 }
 
-bool SwCursorShell::SelectText( const sal_Int32 nStart,
+bool SwCursorShell::SelectTextModel( const sal_Int32 nStart,
                                  const sal_Int32 nEnd )
 {
     CurrShell aCurr( this );
@@ -2020,6 +2020,43 @@ bool SwCursorShell::SelectText( const sal_Int32 nStart,
     return bRet;
 }
 
+TextFrameIndex SwCursorShell::GetCursorPointAsViewIndex() const
+{
+    SwPosition const*const pPos(GetCursor()->GetPoint());
+    SwTextNode const*const pTextNode(pPos->nNode.GetNode().GetTextNode());
+    assert(pTextNode);
+    SwTextFrame const*const pFrame(static_cast<SwTextFrame 
const*>(pTextNode->getLayoutFrame(GetLayout())));
+    assert(pFrame);
+    return pFrame->MapModelToViewPos(*pPos);
+}
+
+bool SwCursorShell::SelectTextView(TextFrameIndex const nStart,
+                                 TextFrameIndex const nEnd)
+{
+    CurrShell aCurr( this );
+    bool bRet = false;
+
+    SwCallLink aLk( *this );
+    SwCursorSaveState aSaveState( *m_pCurrentCursor );
+
+    SwPosition& rPos = *m_pCurrentCursor->GetPoint();
+    m_pCurrentCursor->DeleteMark();
+    // indexes must correspond to cursor point!
+    SwTextFrame const*const pFrame(static_cast<SwTextFrame 
const*>(m_pCurrentCursor->GetPoint()->nNode.GetNode().GetTextNode()->getLayoutFrame(GetLayout())));
+    assert(pFrame);
+    rPos = pFrame->MapViewToModelPos(nStart);
+    m_pCurrentCursor->SetMark();
+    rPos = pFrame->MapViewToModelPos(nEnd);
+
+    if (!m_pCurrentCursor->IsSelOvr())
+    {
+        UpdateCursor();
+        bRet = true;
+    }
+
+    return bRet;
+}
+
 bool SwCursorShell::SelectTextAttr( sal_uInt16 nWhich,
                                      bool bExpand,
                                      const SwTextAttr* pTextAttr )
@@ -2043,7 +2080,7 @@ bool SwCursorShell::SelectTextAttr( sal_uInt16 nWhich,
         if( pTextAttr )
         {
             const sal_Int32* pEnd = pTextAttr->End();
-            bRet = SelectText( pTextAttr->GetStart(), ( pEnd ? *pEnd : 
pTextAttr->GetStart() + 1 ) );
+            bRet = SelectTextModel(pTextAttr->GetStart(), (pEnd ? *pEnd : 
pTextAttr->GetStart() + 1));
         }
     }
     return bRet;
diff --git a/sw/source/uibase/docvw/edtwin.cxx 
b/sw/source/uibase/docvw/edtwin.cxx
index f40e530a0ae2..8b53ebca4d3e 100644
--- a/sw/source/uibase/docvw/edtwin.cxx
+++ b/sw/source/uibase/docvw/edtwin.cxx
@@ -3736,7 +3736,7 @@ void SwEditWin::MouseButtonDown(const MouseEvent& _rMEvt)
                     // select content of Input Field, but exclude 
CH_TXT_ATR_INPUTFIELDSTART
                     // and CH_TXT_ATR_INPUTFIELDEND
                     rSh.SttSelect();
-                    rSh.SelectText( aFieldAtPos.pFndTextAttr->GetStart() + 1,
+                    rSh.SelectTextModel( aFieldAtPos.pFndTextAttr->GetStart() 
+ 1,
                                  *(aFieldAtPos.pFndTextAttr->End()) - 1 );
                 }
                 // don't reset here any longer so that, in case through 
MouseMove
@@ -3782,7 +3782,7 @@ void SwEditWin::MouseButtonDown(const MouseEvent& _rMEvt)
                 // select content of Input Field, but exclude 
CH_TXT_ATR_INPUTFIELDSTART
                 // and CH_TXT_ATR_INPUTFIELDEND
                 rSh.SttSelect();
-                rSh.SelectText( aFieldAtPos.pFndTextAttr->GetStart() + 1,
+                rSh.SelectTextModel( aFieldAtPos.pFndTextAttr->GetStart() + 1,
                                 *(aFieldAtPos.pFndTextAttr->End()) - 1 );
             }
         }
@@ -6490,13 +6490,13 @@ Selection SwEditWin::GetSurroundingTextSelection() const
 
         // Return the position of the visible cursor in the sentence
         // around the visible cursor.
-        SwPosition *pPos = rSh.GetCursor()->GetPoint();
-        const sal_Int32 nPos = pPos->nContent.GetIndex();
+        TextFrameIndex const nPos(rSh.GetCursorPointAsViewIndex());
+
         rSh.Push();
 
         rSh.HideCursor();
         rSh.GoStartSentence();
-        const sal_Int32 nStartPos = 
rSh.GetCursor()->GetPoint()->nContent.GetIndex();
+        TextFrameIndex const nStartPos(rSh.GetCursorPointAsViewIndex());
 
         rSh.Pop(SwCursorShell::PopMode::DeleteCurrent);
         rSh.ShowCursor();
@@ -6504,7 +6504,7 @@ Selection SwEditWin::GetSurroundingTextSelection() const
         if (bUnLockView)
             rSh.LockView(false);
 
-        return Selection( nPos - nStartPos, nPos - nStartPos );
+        return Selection(sal_Int32(nPos - nStartPos), sal_Int32(nPos - 
nStartPos));
     }
 }
 
@@ -6523,11 +6523,12 @@ bool SwEditWin::DeleteSurroundingText(const Selection& 
rSelection)
     rSh.Push();
     rSh.HideCursor();
     rSh.GoStartSentence();
-    const sal_Int32 nStartPos = 
rSh.GetCursor()->GetPoint()->nContent.GetIndex();
+    TextFrameIndex const nStartPos(rSh.GetCursorPointAsViewIndex());
+
     rSh.Pop(SwCursorShell::PopMode::DeleteCurrent);
     rSh.ShowCursor();
 
-    if (rSh.SelectText(nStartPos + rSelection.Min(), nStartPos + 
rSelection.Max()))
+    if (rSh.SelectTextView(nStartPos + TextFrameIndex(rSelection.Min()), 
nStartPos + TextFrameIndex(rSelection.Max())))
     {
         rSh.Delete();
         return true;
diff --git a/sw/source/uibase/shells/textfld.cxx 
b/sw/source/uibase/shells/textfld.cxx
index ffd638270588..42ca39769015 100644
--- a/sw/source/uibase/shells/textfld.cxx
+++ b/sw/source/uibase/shells/textfld.cxx
@@ -180,7 +180,7 @@ void SwTextShell::ExecField(SfxRequest &rReq)
                                
SwCursorShell::GetTextFieldAtCursor(rSh.GetCursor(), true))))
                     {
                         rSh.SttSelect();
-                        rSh.SelectText(
+                        rSh.SelectTextModel(
                             SwCursorShell::StartOfInputFieldAtPos( 
*(rSh.GetCursor()->Start()) ) + 1,
                             SwCursorShell::EndOfInputFieldAtPos( 
*(rSh.GetCursor()->Start()) ) - 1 );
                     }
diff --git a/sw/source/uibase/uitest/uiobject.cxx 
b/sw/source/uibase/uitest/uiobject.cxx
index 7a19b4c8d88e..5f8a408c4b85 100644
--- a/sw/source/uibase/uitest/uiobject.cxx
+++ b/sw/source/uibase/uitest/uiobject.cxx
@@ -96,25 +96,30 @@ void SwEditWinUIObject::execute(const OUString& rAction,
         {
             auto itr = rParameters.find("START_POS");
             OUString aStartPos = itr->second;
-            sal_Int32 nStartPos = aStartPos.toInt32();
+            TextFrameIndex const nStartPos(aStartPos.toInt32());
 
             itr = rParameters.find("END_POS");
             assert(itr != rParameters.end());
             OUString aEndPos = itr->second;
-            sal_Int32 nEndPos = aEndPos.toInt32();
+            TextFrameIndex const nEndPos(aEndPos.toInt32());
 
             auto & shell = getWrtShell(mxEditWin);
-            sal_Int32 len;
-            if (auto const text = 
shell.GetCursor_()->GetPoint()->nNode.GetNode().GetTextNode()) {
-                len = text->GetText().getLength();
-            } else {
-                len = 0;
+            if (shell.GetCursor_()->GetPoint()->nNode.GetNode().GetTextNode())
+            {
+                shell.Push();
+                shell.MovePara(GoCurrPara, fnParaEnd);
+                TextFrameIndex const len(shell.GetCursorPointAsViewIndex());
+                shell.Pop(SwCursorShell::PopMode::DeleteCurrent);
+                SAL_WARN_IF(
+                    sal_Int32(nStartPos) < 0 || nStartPos > len || 
sal_Int32(nEndPos) < 0 || nEndPos > len, "sw.ui",
+                    "SELECT START/END_POS " << sal_Int32(nStartPos) << ".." << 
sal_Int32(nEndPos) << " outside 0.." << sal_Int32(len));
+                shell.SelectTextView(
+                    std::clamp(nStartPos, TextFrameIndex(0), len), 
std::clamp(nEndPos, TextFrameIndex(0), len));
+            }
+            else
+            {
+                SAL_WARN("sw.ui", "SELECT without SwTextNode");
             }
-            SAL_WARN_IF(
-                nStartPos < 0 || nStartPos > len || nEndPos < 0 || nEndPos > 
len, "sw.ui",
-                "SELECT START/END_POS " << nStartPos << ".." << nEndPos << " 
outside 0.." << len);
-            shell.SelectText(
-                std::clamp(nStartPos, sal_Int32(0), len), std::clamp(nEndPos, 
sal_Int32(0), len));
         }
     }
     else if (rAction == "SIDEBAR")

Reply via email to