Fixed LineWidth(): now it calls StyledWidth(). It's not an issue, since charachter widths are cached by _BWidthBuffer_.

Marc Flerackers lend me a hand and now alignments should work much better (if not perfectly)

Some cleanups.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@9119 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini 2004-09-30 12:49:34 +00:00
parent 9bf0b0b085
commit 5302d71190
2 changed files with 31 additions and 28 deletions

View File

@ -1,5 +1,5 @@
//------------------------------------------------------------------------------
// Copyright (c) 2001-2003, OpenBeOS
// Copyright (c) 2001-2004, Haiku, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
@ -24,25 +24,15 @@
// Description: Line storage used by BTextView
//------------------------------------------------------------------------------
// Standard Includes -----------------------------------------------------------
// System Includes -------------------------------------------------------------
#include <SupportDefs.h>
#include "TextViewSupportBuffer.h"
// Project Includes ------------------------------------------------------------
// Local Includes --------------------------------------------------------------
// Local Defines ---------------------------------------------------------------
typedef struct STELine {
long offset; // offset of first character of line
float origin; // pixel position of top of line
float ascent; // maximum ascent for line
float width; // width of line
} STELine, *STELinePtr;
// Globals ---------------------------------------------------------------------
// _BLineBuffer_ class ---------------------------------------------------------
class _BLineBuffer_ : public _BTextViewSupportBuffer_<STELine> {
@ -64,19 +54,21 @@ virtual ~_BLineBuffer_();
long NumLines() const;
const STELinePtr operator[](int32 index) const;
};
//------------------------------------------------------------------------------
inline int32
_BLineBuffer_::NumLines() const
{
return fItemCount - 1;
}
//------------------------------------------------------------------------------
inline const STELinePtr
_BLineBuffer_::operator[](int32 index) const
{
return &fBuffer[index];
}
//------------------------------------------------------------------------------
/*
* $Log $

View File

@ -35,8 +35,7 @@
// to refresh only changed parts of text (currently we often redraw the whole text)
// Known Bugs:
// - PointAt(), OffsetAt(), and possibly some other functions don't work right yet
// when alignment is different than B_ALIGN_LEFT.
// - some issues when alignment is different than B_ALIGN_LEFT.
// - visual artifacts appears when you highlight some text which spans between
// multiple lines and with mixed sizes/styles (could be something in GetTextRegion())
@ -1845,12 +1844,11 @@ BTextView::PointAt(int32 inOffset, float *outHeight) const
}
if (fAlignment != B_ALIGN_LEFT) {
float modifier = fTextRect.right - StyledWidth(line->offset,
(line + 1)->offset - line->offset);
float modifier = fTextRect.right - LineWidth(lineNum);
if (fAlignment == B_ALIGN_CENTER)
modifier /= 2;
result.x += modifier;
}
}
// convert from text rect coordinates
result.x += fTextRect.left - 1.0;
@ -1894,6 +1892,13 @@ BTextView::OffsetAt(BPoint point) const
}
// convert to text rect coordinates
if (fAlignment != B_ALIGN_LEFT) {
float lineWidth = fTextRect.right - LineWidth(lineNum);
if (fAlignment == B_ALIGN_CENTER)
lineWidth /= 2;
point.x -= lineWidth;
}
point.x -= fTextRect.left;
point.x = max_c(point.x, 0.0);
@ -2055,8 +2060,10 @@ BTextView::LineWidth(int32 lineNum) const
CALLED();
if (lineNum < 0 || lineNum >= fLines->NumLines())
return 0;
else
return (*fLines)[lineNum]->width;
else {
STELinePtr line = (*fLines)[lineNum];
return StyledWidth(line->offset, (line + 1)->offset - line->offset);
}
}
@ -2734,7 +2741,6 @@ BTextView::InsertText(const char *inText, int32 inLength, int32 inOffset,
fStyles->BumpOffset(inLength, fStyles->OffsetToRun(inOffset - 1) + 1);
if (inRuns != NULL)
//SetStyleRange(inOffset, inOffset + inLength, inStyles, false);
SetRunArray(inOffset, inOffset + inLength, inRuns);
else {
// apply nullStyle to inserted text
@ -3572,10 +3578,14 @@ BTextView::StyledWidth(int32 fromOffset, int32 length, float *outAscent,
maxAscent = max_c(ascent, maxAscent);
maxDescent = max_c(descent, maxDescent);
// TODO: Change this with a call to _BWidthBuffer_::StringWidth()
// as soon as everything else work
result += font->StringWidth(fText->Text() + fromOffset, numChars);
// Use _BWidthBuffer_ if possible
if (sWidths != NULL) {
LockWidthBuffer();
result += sWidths->StringWidth(fText->Text(), fromOffset, numChars, font);
UnlockWidthBuffer();
} else
result += font->StringWidth(fText->Text() + fromOffset, numChars);
fromOffset += numChars;
length -= numChars;
}
@ -3673,9 +3683,10 @@ BTextView::DrawLines(int32 startLine, int32 endLine, int32 startOffset,
if (fAlignment != B_ALIGN_LEFT) {
// B_ALIGN_RIGHT
startLeft = (fTextRect.right - StyledWidth(line->offset, length));
startLeft = (fTextRect.right - LineWidth(i));
if (fAlignment == B_ALIGN_CENTER)
startLeft /= 2;
startLeft /= 2;
startLeft += fTextRect.left;
}
MovePenTo(startLeft, line->origin + line->ascent + fTextRect.top);