Helpers to set selections to arbitrary DocIterators

This commit is contained in:
Guillaume Munch 2016-09-04 22:17:32 +01:00
parent 3fd2398a28
commit c36ada6b96
4 changed files with 42 additions and 1 deletions

View File

@ -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.

View File

@ -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);

View File

@ -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()) {

View File

@ -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();