Finally nailed that bug which caused text not to be drawn in some cases... OffsetAt() was returning 1 instead of 0 if you clicked on an empty BTextView due to a bug in NextInitialByte() . Some cleanups

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@18836 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini
2006-09-14 13:17:47 +00:00
parent c5ede1427a
commit 3a70724f43
2 changed files with 23 additions and 24 deletions
+22 -15
View File
@@ -1689,7 +1689,7 @@ BTextView::PointAt(int32 inOffset, float *outHeight) const
height = (line + 1)->origin - line->origin; height = (line + 1)->origin - line->origin;
// special case: go down one line if inOffset is a newline // special case: go down one line if inOffset is a newline
if (inOffset == textLength && (*fText)[inOffset - 1] == '\n') { if (inOffset == textLength && (*fText)[inOffset - 1] == B_ENTER) {
float ascent, descent; float ascent, descent;
StyledWidth(inOffset, 1, &ascent, &descent); StyledWidth(inOffset, 1, &ascent, &descent);
@@ -1749,9 +1749,11 @@ BTextView::PointAt(int32 inOffset, float *outHeight) const
int32 int32
BTextView::OffsetAt(BPoint point) const BTextView::OffsetAt(BPoint point) const
{ {
const int32 textLength = fText->Length();
// should we even bother? // should we even bother?
if (point.y >= fTextRect.bottom) if (point.y >= fTextRect.bottom)
return fText->Length(); return textLength;
else if (point.y < fTextRect.top) else if (point.y < fTextRect.top)
return 0; return 0;
@@ -1764,7 +1766,7 @@ BTextView::OffsetAt(BPoint point) const
// (can happen for newlines) // (can happen for newlines)
if (lineNum == (fLines->NumLines() - 1)) { if (lineNum == (fLines->NumLines() - 1)) {
if (point.y >= ((line + 1)->origin + fTextRect.top)) if (point.y >= ((line + 1)->origin + fTextRect.top))
return (fText->Length()); return textLength;
} }
// convert to text rect coordinates // convert to text rect coordinates
@@ -1781,12 +1783,14 @@ BTextView::OffsetAt(BPoint point) const
// TODO: The following code isn't very efficient because it always starts from the left end, // TODO: The following code isn't very efficient because it always starts from the left end,
// so when the point is near the right end it's very slow. // so when the point is near the right end it's very slow.
int32 offset = line->offset; int32 offset = line->offset;
int32 limit = (line + 1)->offset; const int32 limit = (line + 1)->offset;
int32 saveOffset; int32 saveOffset;
float location = 0; float location = 0;
do { do {
const int32 nextInitial = NextInitialByte(offset);
saveOffset = offset; saveOffset = offset;
int32 nextInitial = NextInitialByte(offset);
float width = 0; float width = 0;
if (ByteAt(offset) == B_TAB) if (ByteAt(offset) == B_TAB)
width = ActualTabWidth(location); width = ActualTabWidth(location);
@@ -1805,15 +1809,15 @@ BTextView::OffsetAt(BPoint point) const
if (offset == (line + 1)->offset) { if (offset == (line + 1)->offset) {
// special case: newlines aren't visible // special case: newlines aren't visible
// return the offset of the character preceding the newline // return the offset of the character preceding the newline
if ((*fText)[offset - 1] == '\n') if (ByteAt(offset - 1) == B_ENTER)
return --offset; return --offset;
// special case: return the offset preceding any spaces that // special case: return the offset preceding any spaces that
// aren't at the end of the buffer // aren't at the end of the buffer
if (offset != fText->Length() && (*fText)[offset - 1] == B_SPACE) if (offset != textLength && ByteAt(offset - 1) == B_SPACE)
return --offset; return --offset;
} }
return offset; return offset;
} }
@@ -1919,7 +1923,7 @@ BTextView::TextHeight(int32 startLine, int32 endLine) const
float height = (*fLines)[endLine + 1]->origin - (*fLines)[startLine]->origin; float height = (*fLines)[endLine + 1]->origin - (*fLines)[startLine]->origin;
if (endLine == numLines - 1 && (*fText)[fText->Length() - 1] == '\n') if (endLine == numLines - 1 && (*fText)[fText->Length() - 1] == B_ENTER)
height += (*fLines)[endLine + 1]->origin - (*fLines)[endLine]->origin; height += (*fLines)[endLine + 1]->origin - (*fLines)[endLine]->origin;
return ceilf(height); return ceilf(height);
@@ -3313,7 +3317,7 @@ BTextView::FindLineBreak(int32 fromOffset, float *outAscent, float *outDescent,
// Just find the offset of the first \n character // Just find the offset of the first \n character
if (!fWrap) { if (!fWrap) {
offset = limit - fromOffset; offset = limit - fromOffset;
fText->FindChar('\n', fromOffset, &offset); fText->FindChar(B_ENTER, fromOffset, &offset);
offset += fromOffset; offset += fromOffset;
offset = (offset < limit) ? offset + 1 : limit; offset = (offset < limit) ? offset + 1 : limit;
@@ -3344,7 +3348,7 @@ BTextView::FindLineBreak(int32 fromOffset, float *outAscent, float *outDescent,
if (!CanEndLine(offset + delta)) if (!CanEndLine(offset + delta))
break; break;
if (theChar == '\n') { if (theChar == B_ENTER) {
// found a newline, we're done! // found a newline, we're done!
done = true; done = true;
delta++; delta++;
@@ -3389,7 +3393,7 @@ BTextView::FindLineBreak(int32 fromOffset, float *outAscent, float *outDescent,
int32 pos = delta - 1; int32 pos = delta - 1;
if ((*fText)[offset + pos] != B_SPACE && if ((*fText)[offset + pos] != B_SPACE &&
(*fText)[offset + pos] != B_TAB && (*fText)[offset + pos] != B_TAB &&
(*fText)[offset + pos] != '\n') (*fText)[offset + pos] != B_ENTER)
break; break;
strWidth -= (deltaWidth + tabWidth); strWidth -= (deltaWidth + tabWidth);
@@ -3398,7 +3402,7 @@ BTextView::FindLineBreak(int32 fromOffset, float *outAscent, float *outDescent,
uchar theChar = (*fText)[offset + pos]; uchar theChar = (*fText)[offset + pos];
if (theChar != B_SPACE && if (theChar != B_SPACE &&
theChar != B_TAB && theChar != B_TAB &&
theChar != '\n') theChar != B_ENTER)
break; break;
} }
@@ -3413,7 +3417,7 @@ BTextView::FindLineBreak(int32 fromOffset, float *outAscent, float *outDescent,
break; break;
} }
if ( (offset + delta) < limit && if ( (offset + delta) < limit &&
(*fText)[offset + delta] == '\n' ) (*fText)[offset + delta] == B_ENTER )
delta++; delta++;
} }
// get the ascent and descent of the spaces/tabs // get the ascent and descent of the spaces/tabs
@@ -3604,7 +3608,7 @@ BTextView::DrawLines(int32 startLine, int32 endLine, int32 startOffset, bool era
length -= line->offset; length -= line->offset;
// DrawString() chokes if you draw a newline // DrawString() chokes if you draw a newline
if ((*fText)[(line + 1)->offset - 1] == '\n') if ((*fText)[(line + 1)->offset - 1] == B_ENTER)
length--; length--;
if (fAlignment != B_ALIGN_LEFT) { if (fAlignment != B_ALIGN_LEFT) {
@@ -4197,6 +4201,9 @@ BTextView::CharClassification(int32 offset) const
int32 int32
BTextView::NextInitialByte(int32 offset) const BTextView::NextInitialByte(int32 offset) const
{ {
if (ByteAt(offset) == '\0')
return offset;
for (++offset; (ByteAt(offset) & 0xC0) == 0x80; ++offset) for (++offset; (ByteAt(offset) & 0xC0) == 0x80; ++offset)
; ;
@@ -138,8 +138,7 @@ _BStyleRecordBuffer_::RemoveRecord(int32 index)
bool bool
_BStyleRecordBuffer_::MatchRecord(const BFont *inFont, _BStyleRecordBuffer_::MatchRecord(const BFont *inFont, const rgb_color *inColor, int32 *outIndex)
const rgb_color *inColor, int32 *outIndex)
{ {
for (int32 i = 0; i < fItemCount; i++) { for (int32 i = 0; i < fItemCount; i++) {
if (*inFont == fBuffer[i].style.font if (*inFont == fBuffer[i].style.font
@@ -314,13 +313,6 @@ _BStyleBuffer_::SetStyleRange(int32 fromOffset, int32 toOffset,
} }
runIndex++; runIndex++;
if (offset == runEnd) {
// TODO: this hides a bug somewhere else in the code that
// should probably be fixed...
// Can't reproduce this anymore...
printf("_BStyleBuffer_::SetStyleRange(): offset == runEnd!\n");
break;
}
offset = runEnd; offset = runEnd;
} while (offset < toOffset); } while (offset < toOffset);