Let getRowNearY use getPitNearY

This avoids code duplication. Return a pointer to a Row instead of a
reference to handle the case where something went wrong.

Make these methods private.
This commit is contained in:
Jean-Marc Lasgouttes 2025-01-13 17:38:18 +01:00
parent a25d94c315
commit 2d59031c3e
2 changed files with 29 additions and 35 deletions

View File

@ -1515,8 +1515,10 @@ pit_type TextMetrics::getPitNearY(int y)
}
Row const & TextMetrics::getRowNearY(int & y, pit_type pit)
Row const * TextMetrics::getRowNearY(int & y)
{
pit_type const pit = getPitNearY(y);
LASSERT(pit != -1, return nullptr);
ParagraphMetrics const & pm = par_metrics_[pit];
int yy = pm.top();
@ -1528,7 +1530,7 @@ Row const & TextMetrics::getRowNearY(int & y, pit_type pit)
if (yy + rit->height() > y)
break;
return *rit;
return &(*rit);
}
@ -1536,17 +1538,16 @@ Row const & TextMetrics::getRowNearY(int & y, pit_type pit)
// sets cursor recursively descending into nested editable insets
Inset * TextMetrics::editXY(Cursor & cur, int x, int y)
{
pit_type const pit = getPitNearY(y);
LASSERT(pit != -1, return 0);
Row const & row = getRowNearY(y, pit);
cur.pit() = pit;
Row const * row = getRowNearY(y);
LASSERT(row != nullptr, return nullptr);
cur.pit() = row->pit();
// Do we cover an inset?
Row::Element const * e = checkInsetHit(row, x);
Row::Element const * e = checkInsetHit(*row, x);
if (!e) {
// No inset, set position in the text
auto [pos, bound] = getPosNearX(row, x);
auto [pos, bound] = getPosNearX(*row, x);
cur.pos() = pos;
cur.boundary(bound);
cur.setCurrentFont();
@ -1570,7 +1571,7 @@ Inset * TextMetrics::editXY(Cursor & cur, int x, int y)
// non-editable inset, set cursor after the inset if x is
// nearer to that position (bug 9628)
// No inset, set position in the text
auto [pos, bound] = getPosNearX(row, x);
auto [pos, bound] = getPosNearX(*row, x);
cur.pos() = pos;
cur.boundary(bound);
cur.setCurrentFont();
@ -1587,12 +1588,10 @@ void TextMetrics::setCursorFromCoordinates(Cursor & cur, int x, int y)
{
LASSERT(text_ == cur.text(), return);
pit_type const pit = getPitNearY(y);
LASSERT(pit != -1, return);
Row const & row = getRowNearY(y, pit);
auto [pos, bound] = getPosNearX(row, x);
text_->setCursor(cur, pit, pos, true, bound);
Row const * row = getRowNearY(y);
LASSERT(row != nullptr, return);
auto [pos, bound] = getPosNearX(*row, x);
text_->setCursor(cur, row->pit(), pos, true, bound);
// remember new position.
cur.setTargetX();
}
@ -1624,10 +1623,9 @@ Row::Element const * TextMetrics::checkInsetHit(Row const & row, int x) const
//takes screen x,y coordinates
Inset * TextMetrics::checkInsetHit(int x, int y)
{
pit_type const pit = getPitNearY(y);
LASSERT(pit != -1, return nullptr);
Row const & row = getRowNearY(y, pit);
Row::Element const * e = checkInsetHit(row, x);
Row const * row = getRowNearY(y);
LASSERT(row != nullptr, return nullptr);
Row::Element const * e = checkInsetHit(*row, x);
return e ? const_cast<Inset *>(e->inset) : nullptr;
}

View File

@ -186,35 +186,31 @@ private:
// Helper function for the other checkInsetHit method.
Row::Element const * checkInsetHit(Row const & row, int x) const;
/// Returns the paragraph number closest to screen y-coordinate.
/// This method uses the paragraph metrics to locate the
/// paragraph. The y-coordinate is allowed to be off-screen and
/// the metrics will be automatically updated if needed. This is
/// the reason why we need a non const BufferView.
/// FIXME: check whether this is still needed
pit_type getPitNearY(int y);
/// returns the row near the specified y-coordinate in a given paragraph
/// (relative to the screen).
Row const * getRowNearY(int & y);
// Temporary public:
public:
/// returns the position near the specified x-coordinate of the row.
/// x is an absolute screen coord, it is set to the real beginning
/// of this column. This takes in account horizontal cursor row scrolling.
std::pair<pos_type, bool> getPosNearX(Row const & row, int & x) const;
/// returns the row near the specified y-coordinate in a given paragraph
/// (relative to the screen).
Row const & getRowNearY(int & y, pit_type pit);
/// returns the paragraph number closest to screen y-coordinate.
/// This method uses the BufferView CoordCache to locate the
/// paragraph. The y-coodinate is allowed to be off-screen and
/// the CoordCache will be automatically updated if needed. This is
/// the reason why we need a non const BufferView.
pit_type getPitNearY(int y);
/// sets cursor recursively descending into nested editable insets
/**
/** sets cursor recursively descending into nested editable insets
\return the inset pointer if x,y is covering that inset
\param x,y are absolute screen coordinates.
\retval inset is null if the cursor is positioned over normal
text in the current Text object. Otherwise it is the inset
that the cursor points to, like for Inset::editXY.
*/
/// FIXME: cleanup to use BufferView::getCoveringInset() and
/// setCursorFromCoordinates() instead of checkInsetHit().
Inset * editXY(Cursor & cur, int x, int y);
/// sets cursor only within this Text.