commit c36ada6b9682f43f1c0840bcbd3d8396590af3cb
Author: Guillaume Munch <[email protected]>
Date:   Sun Sep 4 22:17:32 2016 +0100

    Helpers to set selections to arbitrary DocIterators
---
 src/BufferView.cpp |   12 ++++++++++++
 src/BufferView.h   |    2 ++
 src/Cursor.cpp     |   26 +++++++++++++++++++++++++-
 src/Cursor.h       |    3 +++
 4 files changed, 42 insertions(+), 1 deletions(-)

diff --git a/src/BufferView.cpp b/src/BufferView.cpp
index fad0154..dc4f8a1 100644
--- a/src/BufferView.cpp
+++ b/src/BufferView.cpp
@@ -2478,6 +2478,18 @@ void BufferView::setCursor(DocIterator const & dit)
 }
 
 
+void BufferView::setCursorSelectionTo(DocIterator const & dit)
+{
+       size_t const n = dit.depth();
+       for (size_t i = 0; i < n; ++i)
+               dit[i].inset().edit(d->cursor_, true);
+
+       d->cursor_.selection(true);
+       d->cursor_.setCursorSelectionTo(dit);
+       d->cursor_.setCurrentFont();
+}
+
+
 bool BufferView::checkDepm(Cursor & cur, Cursor & old)
 {
        // Would be wrong to delete anything if we have a selection.
diff --git a/src/BufferView.h b/src/BufferView.h
index b368685..e6a29de 100644
--- a/src/BufferView.h
+++ b/src/BufferView.h
@@ -249,6 +249,8 @@ public:
        /// sets cursor.
        /// This will also open all relevant collapsable insets.
        void setCursor(DocIterator const &);
+       /// set the selection up to dit.
+       void setCursorSelectionTo(DocIterator const & dit);
        /// Check deleteEmptyParagraphMechanism and update metrics if needed.
        /// \retval true if an update was needed.
        bool checkDepm(Cursor & cur, Cursor & old);
diff --git a/src/Cursor.cpp b/src/Cursor.cpp
index b13b6a5..bdd6986 100644
--- a/src/Cursor.cpp
+++ b/src/Cursor.cpp
@@ -194,13 +194,37 @@ void Cursor::reset()
 }
 
 
-// this (intentionally) does neither touch anchor nor selection status
 void Cursor::setCursor(DocIterator const & cur)
 {
        DocIterator::operator=(cur);
 }
 
 
+void Cursor::setCursorSelectionTo(DocIterator dit)
+{
+       size_t i = 0;
+       // normalise dit
+       while (i < dit.depth() && i < anchor_.depth() && dit[i] == anchor_[i])
+               ++i;
+       if (i != dit.depth()) {
+               // otherwise the cursor is already normal
+               if (i == anchor_.depth())
+                       // dit is a proper extension of the anchor_
+                       dit.cutOff(i - 1);
+               else if (i + 1 < dit.depth()) {
+                       // one has dit[i] != anchor_[i] but either dit[i-1] == 
anchor_[i-1]
+                       // or i == 0. Remove excess.
+                       dit.cutOff(i);
+                       if (dit[i] > anchor_[i])
+                               // place dit after the inset it was in
+                               ++dit.pos();
+               }
+       }
+       setCursor(dit);
+       setSelection();
+}
+
+
 void Cursor::setCursorToAnchor()
 {
        if (selection()) {
diff --git a/src/Cursor.h b/src/Cursor.h
index aa31f91..cc5e46e 100644
--- a/src/Cursor.h
+++ b/src/Cursor.h
@@ -149,7 +149,10 @@ public:
        /// set the cursor data
        void setCursorData(CursorData const & data);
        /// sets cursor part
+       /// this (intentionally) does neither touch anchor nor selection status
        void setCursor(DocIterator const & it);
+       /// set the cursor to dit normalised against the anchor, and set 
selection.
+       void setCursorSelectionTo(DocIterator dit);
        /// sets the cursor to the normalized selection anchor
        void setCursorToAnchor();
 

Reply via email to