commit 812e306dada7c5c0565c9428232f329ec4c705d3
Author: Juergen Spitzmueller <sp...@lyx.org>
Date:   Sun Mar 31 12:40:27 2024 +0200

    Factor out shared method
    
    Amends babb5b007bd
---
 src/BufferView.cpp                   | 19 +++++++++++++++++++
 src/BufferView.h                     |  3 +++
 src/frontends/qt/GuiSpellchecker.cpp | 29 ++---------------------------
 src/lyxfind.cpp                      | 25 ++-----------------------
 4 files changed, 26 insertions(+), 50 deletions(-)

diff --git a/src/BufferView.cpp b/src/BufferView.cpp
index ad534eae28..39fffed68e 100644
--- a/src/BufferView.cpp
+++ b/src/BufferView.cpp
@@ -2993,6 +2993,25 @@ void BufferView::putSelectionAt(DocIterator const & cur,
 }
 
 
+void BufferView::setSelection(DocIterator const & from,
+                             DocIterator const & to)
+{
+       if (from.pit() != to.pit()) {
+               // there are multiple paragraphs in selection
+               cursor().setCursor(from);
+               cursor().clearSelection();
+               cursor().selection(true);
+               cursor().setCursor(to);
+               cursor().selection(true);
+       } else {
+               // only single paragraph
+               int const size = to.pos() - from.pos();
+               putSelectionAt(from, size, false);
+       }
+       processUpdateFlags(Update::Force | Update::FitCursor);
+}
+
+
 bool BufferView::selectIfEmpty(DocIterator & cur)
 {
        if ((cur.inTexted() && !cur.paragraph().empty())
diff --git a/src/BufferView.h b/src/BufferView.h
index d239fdd360..b46ade3df5 100644
--- a/src/BufferView.h
+++ b/src/BufferView.h
@@ -302,6 +302,9 @@ public:
         */
        void putSelectionAt(DocIterator const & cur,
                int length, bool backwards);
+       /// set a selection between \p from and \p to
+       void setSelection(DocIterator const & from,
+                        DocIterator const & to);
 
        /// selects the item at cursor if its paragraph is empty.
        bool selectIfEmpty(DocIterator & cur);
diff --git a/src/frontends/qt/GuiSpellchecker.cpp 
b/src/frontends/qt/GuiSpellchecker.cpp
index d58f435b10..66952eb7dc 100644
--- a/src/frontends/qt/GuiSpellchecker.cpp
+++ b/src/frontends/qt/GuiSpellchecker.cpp
@@ -72,8 +72,6 @@ struct SpellcheckerWidget::Private
        void check();
        /// close the spell checker dialog
        void hide() const;
-       /// make/restore a selection between from and to
-       void setSelection(DocIterator const & from, DocIterator const & to) 
const;
        /// if no selection was checked:
        /// ask the user if the check should start over
        bool continueFromBeginning();
@@ -339,7 +337,7 @@ void SpellcheckerWidget::Private::hide() const
        if (isCurrentBuffer(bvcur)) {
                if (!begin_.empty() && !end_.empty()) {
                        // restore previous selection
-                       setSelection(begin_, end_);
+                       bv->setSelection(begin_, end_);
                } else {
                        // restore cursor position
                        bvcur.setCursor(start_);
@@ -349,29 +347,6 @@ void SpellcheckerWidget::Private::hide() const
        }
 }
 
-void SpellcheckerWidget::Private::setSelection(
-       DocIterator const & from, DocIterator const & to) const
-{
-       BufferView * bv = gv_->documentBufferView();
-       DocIterator end = to;
-
-       if (from.pit() != end.pit()) {
-               // there are multiple paragraphs in selection
-               Cursor & bvcur = bv->cursor();
-               bvcur.setCursor(from);
-               bvcur.clearSelection();
-               bvcur.selection(true);
-               bvcur.setCursor(end);
-               bvcur.selection(true);
-       } else {
-               // FIXME LFUN
-               // If we used a LFUN, dispatch would do all of this for us
-               int const size = end.pos() - from.pos();
-               bv->putSelectionAt(from, size, false);
-       }
-       bv->processUpdateFlags(Update::Force | Update::FitCursor);
-}
-
 void SpellcheckerWidget::Private::forward()
 {
        DocIterator const from = cursor();
@@ -632,7 +607,7 @@ void SpellcheckerWidget::Private::check()
                return;
        setLanguage(word_lang.lang());
        // mark misspelled word
-       setSelection(from, to);
+       bv->setSelection(from, to);
        // enable relevant widgets
        updateView();
 }
diff --git a/src/lyxfind.cpp b/src/lyxfind.cpp
index 6281b82abc..6e3b324bfa 100644
--- a/src/lyxfind.cpp
+++ b/src/lyxfind.cpp
@@ -281,27 +281,6 @@ bool searchAllowed(docstring const & str)
        return true;
 }
 
-void setSelection(BufferView * bv, DocIterator const & from, DocIterator const 
& to)
-{
-       DocIterator end = to;
-
-       if (from.pit() != end.pit()) {
-               // there are multiple paragraphs in selection
-               Cursor & bvcur = bv->cursor();
-               bvcur.setCursor(from);
-               bvcur.clearSelection();
-               bvcur.selection(true);
-               bvcur.setCursor(end);
-               bvcur.selection(true);
-       } else {
-               // FIXME LFUN
-               // If we used a LFUN, dispatch would do all of this for us
-               int const size = end.pos() - from.pos();
-               bv->putSelectionAt(from, size, false);
-       }
-       bv->processUpdateFlags(Update::Force | Update::FitCursor);
-}
-
 } // namespace
 
 
@@ -408,7 +387,7 @@ bool findOne(BufferView * bv, docstring const & searchstr,
                // restore original selection
                if (had_selection) {
                        bv->cursor().resetAnchor();
-                       setSelection(bv, startcur, endcur);
+                       bv->setSelection(startcur, endcur);
                }
                return false;
        }
@@ -485,7 +464,7 @@ int replaceAll(BufferView * bv,
        if (had_selection) {
                endcur.fixIfBroken();
                bv->cursor().resetAnchor();
-               setSelection(bv, startcur, endcur);
+               bv->setSelection(startcur, endcur);
        }
 
        return num;
-- 
lyx-cvs mailing list
lyx-cvs@lists.lyx.org
http://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to