* corrected the computation of alignment offsets for centered or right-aligned

textviews, such that the characters are drawn at their correct positions and 
  there  are no longer any mismatches between caret and character positions
* fixed too large widths returned by LineWidth() for lines ending with a
  newline: the newline character must not contribute to the width
This makes the textcontrol in DiskProbe work properly again.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30762 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Oliver Tappe
2009-05-14 17:44:49 +00:00
parent 2660bd9be1
commit 78da6449b0
+27 -22
View File
@@ -1764,15 +1764,16 @@ BTextView::PointAt(int32 inOffset, float *outHeight) const
result.x = 0.0; result.x = 0.0;
result.y = line->origin + fTextRect.top; result.y = line->origin + fTextRect.top;
// Handle the case where there is only one line (no text inserted) bool onEmptyLastLine = _IsOnEmptyLastLine(inOffset);
// TODO: See if we can do this better
if (fStyles->NumRuns() == 0) { if (fStyles->NumRuns() == 0) {
// Handle the case where there is only one line (no text inserted)
fStyles->SyncNullStyle(0); fStyles->SyncNullStyle(0);
height = _NullStyleHeight(); height = _NullStyleHeight();
} else { } else {
height = (line + 1)->origin - line->origin; height = (line + 1)->origin - line->origin;
if (_IsOnEmptyLastLine(inOffset)) { if (onEmptyLastLine) {
// special case: go down one line if inOffset is at the newline // special case: go down one line if inOffset is at the newline
// at the end of the buffer ... // at the end of the buffer ...
result.y += height; result.y += height;
@@ -1802,16 +1803,15 @@ BTextView::PointAt(int32 inOffset, float *outHeight) const
} }
if (fAlignment != B_ALIGN_LEFT) { if (fAlignment != B_ALIGN_LEFT) {
float modifier = fTextRect.right - LineWidth(lineNum); float lineWidth = onEmptyLastLine ? 0.0 : LineWidth(lineNum);
float alignmentOffset = fTextRect.Width() - lineWidth;
if (fAlignment == B_ALIGN_CENTER) if (fAlignment == B_ALIGN_CENTER)
modifier /= 2; alignmentOffset /= 2;
result.x += modifier; result.x += alignmentOffset;
} }
// convert from text rect coordinates // convert from text rect coordinates
// NOTE: I didn't understand why "- 1.0" result.x += fTextRect.left;
// and it works only correct without it on Haiku app_server.
// Feel free to enlighten me though!
result.x += fTextRect.left;// - 1.0;
// round up // round up
result.x = ceilf(result.x); result.x = ceilf(result.x);
@@ -1856,10 +1856,10 @@ BTextView::OffsetAt(BPoint point) const
// convert to text rect coordinates // convert to text rect coordinates
if (fAlignment != B_ALIGN_LEFT) { if (fAlignment != B_ALIGN_LEFT) {
float lineWidth = fTextRect.right - LineWidth(lineNum); float alignmentOffset = fTextRect.Width() - LineWidth(lineNum);
if (fAlignment == B_ALIGN_CENTER) if (fAlignment == B_ALIGN_CENTER)
lineWidth /= 2; alignmentOffset /= 2;
point.x -= lineWidth; point.x -= alignmentOffset;
} }
point.x -= fTextRect.left; point.x -= fTextRect.left;
@@ -1984,7 +1984,14 @@ BTextView::LineWidth(int32 lineNum) const
return 0; return 0;
STELine* line = (*fLines)[lineNum]; STELine* line = (*fLines)[lineNum];
return _StyledWidth(line->offset, (line + 1)->offset - line->offset); int32 length = (line + 1)->offset - line->offset;
// skip newline at the end of the line, if any, as it does no contribute
// to the width
if (ByteAt((line + 1)->offset - 1) == B_ENTER)
length--;
return _StyledWidth(line->offset, length);
} }
@@ -4149,6 +4156,12 @@ BTextView::_DrawLine(BView *view, const int32 &lineNum,
} else } else
startLeft = PointAt(startOffset).x; startLeft = PointAt(startOffset).x;
} }
else if (fAlignment != B_ALIGN_LEFT) {
float alignmentOffset = fTextRect.Width() - LineWidth(lineNum);
if (fAlignment == B_ALIGN_CENTER)
alignmentOffset /= 2;
startLeft = fTextRect.left + alignmentOffset;
}
int32 length = (line + 1)->offset; int32 length = (line + 1)->offset;
if (startOffset != -1) if (startOffset != -1)
@@ -4159,20 +4172,12 @@ BTextView::_DrawLine(BView *view, const int32 &lineNum,
// DrawString() chokes if you draw a newline // DrawString() chokes if you draw a newline
if (ByteAt((line + 1)->offset - 1) == B_ENTER) if (ByteAt((line + 1)->offset - 1) == B_ENTER)
length--; length--;
if (fAlignment != B_ALIGN_LEFT) {
// B_ALIGN_RIGHT
startLeft = (fTextRect.right - LineWidth(lineNum));
if (fAlignment == B_ALIGN_CENTER)
startLeft /= 2;
startLeft += fTextRect.left;
}
view->MovePenTo(startLeft, line->origin + line->ascent + fTextRect.top + 1); view->MovePenTo(startLeft, line->origin + line->ascent + fTextRect.top + 1);
if (erase) { if (erase) {
eraseRect.top = line->origin + fTextRect.top; eraseRect.top = line->origin + fTextRect.top;
eraseRect.bottom = (line + 1)->origin + fTextRect.top; eraseRect.bottom = (line + 1)->origin + fTextRect.top;
view->FillRect(eraseRect, B_SOLID_LOW); view->FillRect(eraseRect, B_SOLID_LOW);
} }