Cache text width to avoid calling StringWidth() too much while

both text and font don't change.



git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@38591 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Philippe Houdoin
2010-09-09 15:13:37 +00:00
parent 38c4dc0eaa
commit 7ae974066f
2 changed files with 15 additions and 8 deletions
+1
View File
@@ -78,6 +78,7 @@ private:
private: private:
char* fText; char* fText;
float fTextWidth;
alignment fAlign; alignment fAlign;
BSize fPreferredSize; BSize fPreferredSize;
+14 -8
View File
@@ -29,6 +29,7 @@ BStringView::BStringView(BRect frame, const char* name, const char* text,
uint32 resizeMask, uint32 flags) uint32 resizeMask, uint32 flags)
: BView(frame, name, resizeMask, flags | B_FULL_UPDATE_ON_RESIZE), : BView(frame, name, resizeMask, flags | B_FULL_UPDATE_ON_RESIZE),
fText(text ? strdup(text) : NULL), fText(text ? strdup(text) : NULL),
fTextWidth(text ? StringWidth(text) : 0.0),
fAlign(B_ALIGN_LEFT), fAlign(B_ALIGN_LEFT),
fPreferredSize(-1, -1) fPreferredSize(-1, -1)
{ {
@@ -38,6 +39,7 @@ BStringView::BStringView(BRect frame, const char* name, const char* text,
BStringView::BStringView(const char* name, const char* text, uint32 flags) BStringView::BStringView(const char* name, const char* text, uint32 flags)
: BView(name, flags | B_FULL_UPDATE_ON_RESIZE), : BView(name, flags | B_FULL_UPDATE_ON_RESIZE),
fText(text ? strdup(text) : NULL), fText(text ? strdup(text) : NULL),
fTextWidth(text ? StringWidth(text) : 0.0),
fAlign(B_ALIGN_LEFT), fAlign(B_ALIGN_LEFT),
fPreferredSize(-1, -1) fPreferredSize(-1, -1)
{ {
@@ -47,6 +49,7 @@ BStringView::BStringView(const char* name, const char* text, uint32 flags)
BStringView::BStringView(BMessage* data) BStringView::BStringView(BMessage* data)
: BView(data), : BView(data),
fText(NULL), fText(NULL),
fTextWidth(0.0),
fPreferredSize(-1, -1) fPreferredSize(-1, -1)
{ {
int32 align; int32 align;
@@ -241,11 +244,11 @@ BStringView::Draw(BRect updateRect)
float x; float x;
switch (fAlign) { switch (fAlign) {
case B_ALIGN_RIGHT: case B_ALIGN_RIGHT:
x = bounds.Width() - StringWidth(fText); x = bounds.Width() - fTextWidth;
break; break;
case B_ALIGN_CENTER: case B_ALIGN_CENTER:
x = (bounds.Width() - StringWidth(fText)) / 2.0; x = (bounds.Width() - fTextWidth) / 2.0;
break; break;
default: default:
@@ -294,13 +297,14 @@ BStringView::SetText(const char* text)
if ((text && fText && !strcmp(text, fText)) || (!text && !fText)) if ((text && fText && !strcmp(text, fText)) || (!text && !fText))
return; return;
float oldWidth = StringWidth(fText);
free(fText); free(fText);
fText = text ? strdup(text) : NULL; fText = text ? strdup(text) : NULL;
if (oldWidth != StringWidth(fText)) float newTextWidth = StringWidth(fText);
if (fTextWidth != newTextWidth) {
fTextWidth = newTextWidth;
InvalidateLayout(); InvalidateLayout();
}
Invalidate(); Invalidate();
} }
@@ -348,6 +352,8 @@ BStringView::SetFont(const BFont* font, uint32 mask)
{ {
BView::SetFont(font, mask); BView::SetFont(font, mask);
fTextWidth = StringWidth(fText);
Invalidate(); Invalidate();
InvalidateLayout(); InvalidateLayout();
} }
@@ -434,13 +440,13 @@ BStringView::_ValidatePreferredSize()
{ {
if (fPreferredSize.width < 0) { if (fPreferredSize.width < 0) {
// width // width
fPreferredSize.width = ceilf(StringWidth(fText)); fPreferredSize.width = ceilf(fTextWidth);
// height // height
font_height fontHeight; font_height fontHeight;
GetFontHeight(&fontHeight); GetFontHeight(&fontHeight);
fPreferredSize.height = ceilf(fontHeight.ascent + fontHeight.descent fPreferredSize.height = ceilf(fontHeight.ascent + fontHeight.descent
+ fontHeight.leading); + fontHeight.leading);
ResetLayoutInvalidation(); ResetLayoutInvalidation();