From f68be5f8dda0925786c1130f1ed2da9f7bbc2e6c Mon Sep 17 00:00:00 2001 From: Marko Njezic Date: Wed, 18 Jan 2012 21:29:08 +0100 Subject: Add fractional positioning support in wrapped lines to certain methods. This fixes off by one errors that can happen in some cases. Simplify Editor::PositionFromLineX() method. --- src/Editor.cxx | 47 +++++++---------------------------------------- 1 file changed, 7 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/Editor.cxx b/src/Editor.cxx index 35a21c36c..6919d91d7 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -506,7 +506,7 @@ SelectionPosition Editor::SPositionFromLocation(Point pt, bool canReturnInvalid, if (subLine < ll->lines) { int lineStart = ll->LineStart(subLine); int lineEnd = ll->LineLastVisible(subLine); - int subLineStart = ll->positions[lineStart]; + XYPOSITION subLineStart = ll->positions[lineStart]; if (ll->wrapIndent != 0) { if (lineStart != 0) // Wrapped @@ -548,43 +548,6 @@ int Editor::PositionFromLocation(Point pt, bool canReturnInvalid, bool charPosit return SPositionFromLocation(pt, canReturnInvalid, charPosition, false).Position(); } -/** - * Find the document position corresponding to an x coordinate on a particular document line. - * Ensure is between whole characters when document is in multi-byte or UTF-8 mode. - */ -int Editor::PositionFromLineX(int lineDoc, int x) { - RefreshStyleData(); - if (lineDoc >= pdoc->LinesTotal()) - return pdoc->Length(); - //Platform::DebugPrintf("Position of (%d,%d) line = %d top=%d\n", pt.x, pt.y, line, topLine); - AutoSurface surface(this); - AutoLineLayout ll(llc, RetrieveLineLayout(lineDoc)); - int retVal = 0; - if (surface && ll) { - unsigned int posLineStart = pdoc->LineStart(lineDoc); - LayoutLine(lineDoc, surface, vs, ll, wrapWidth); - retVal = ll->numCharsBeforeEOL + posLineStart; - int subLine = 0; - int lineStart = ll->LineStart(subLine); - int lineEnd = ll->LineLastVisible(subLine); - int subLineStart = ll->positions[lineStart]; - - if (ll->wrapIndent != 0) { - if (lineStart != 0) // Wrapped - x -= ll->wrapIndent; - } - int i = ll->FindBefore(x + subLineStart, lineStart, lineEnd); - while (i < lineEnd) { - if ((x + subLineStart) < ((ll->positions[i] + ll->positions[i + 1]) / 2)) { - retVal = pdoc->MovePositionOutsideChar(i + posLineStart, 1); - break; - } - i++; - } - } - return retVal; -} - /** * Find the document position corresponding to an x coordinate on a particular document line. * Ensure is between whole characters when document is in multi-byte or UTF-8 mode. @@ -603,7 +566,7 @@ SelectionPosition Editor::SPositionFromLineX(int lineDoc, int x) { int subLine = 0; int lineStart = ll->LineStart(subLine); int lineEnd = ll->LineLastVisible(subLine); - int subLineStart = ll->positions[lineStart]; + XYPOSITION subLineStart = ll->positions[lineStart]; if (ll->wrapIndent != 0) { if (lineStart != 0) // Wrapped @@ -624,6 +587,10 @@ SelectionPosition Editor::SPositionFromLineX(int lineDoc, int x) { return SelectionPosition(retVal); } +int Editor::PositionFromLineX(int lineDoc, int x) { + return SPositionFromLineX(lineDoc, x).Position(); +} + /** * If painting then abandon the painting because a wider redraw is needed. * @return true if calling code should stop drawing. @@ -2357,7 +2324,7 @@ void Editor::LayoutLine(int line, Surface *surface, ViewStyle &vstyle, LineLayou // Calculate line start positions based upon width. int lastGoodBreak = 0; int lastLineStart = 0; - int startOffset = 0; + XYACCUMULATOR startOffset = 0; int p = 0; while (p < ll->numCharsInLine) { if ((ll->positions[p + 1] - startOffset) >= width) { -- cgit v1.2.3