Author: phoudoin Date: 2010-09-09 17:13:37 +0200 (Thu, 09 Sep 2010) New Revision: 38591 Changeset: http://dev.haiku-os.org/changeset/38591 Modified: haiku/trunk/headers/os/interface/StringView.h haiku/trunk/src/kits/interface/StringView.cpp Log: Cache text width to avoid calling StringWidth() too much while both text and font don't change. Modified: haiku/trunk/headers/os/interface/StringView.h =================================================================== --- haiku/trunk/headers/os/interface/StringView.h 2010-09-08 22:15:37 UTC (rev 38590) +++ haiku/trunk/headers/os/interface/StringView.h 2010-09-09 15:13:37 UTC (rev 38591) @@ -78,6 +78,7 @@ private: char* fText; + float fTextWidth; alignment fAlign; BSize fPreferredSize; Modified: haiku/trunk/src/kits/interface/StringView.cpp =================================================================== --- haiku/trunk/src/kits/interface/StringView.cpp 2010-09-08 22:15:37 UTC (rev 38590) +++ haiku/trunk/src/kits/interface/StringView.cpp 2010-09-09 15:13:37 UTC (rev 38591) @@ -29,6 +29,7 @@ uint32 resizeMask, uint32 flags) : BView(frame, name, resizeMask, flags | B_FULL_UPDATE_ON_RESIZE), fText(text ? strdup(text) : NULL), + fTextWidth(text ? StringWidth(text) : 0.0), fAlign(B_ALIGN_LEFT), fPreferredSize(-1, -1) { @@ -38,6 +39,7 @@ BStringView::BStringView(const char* name, const char* text, uint32 flags) : BView(name, flags | B_FULL_UPDATE_ON_RESIZE), fText(text ? strdup(text) : NULL), + fTextWidth(text ? StringWidth(text) : 0.0), fAlign(B_ALIGN_LEFT), fPreferredSize(-1, -1) { @@ -47,6 +49,7 @@ BStringView::BStringView(BMessage* data) : BView(data), fText(NULL), + fTextWidth(0.0), fPreferredSize(-1, -1) { int32 align; @@ -241,11 +244,11 @@ float x; switch (fAlign) { case B_ALIGN_RIGHT: - x = bounds.Width() - StringWidth(fText); + x = bounds.Width() - fTextWidth; break; case B_ALIGN_CENTER: - x = (bounds.Width() - StringWidth(fText)) / 2.0; + x = (bounds.Width() - fTextWidth) / 2.0; break; default: @@ -294,13 +297,14 @@ if ((text && fText && !strcmp(text, fText)) || (!text && !fText)) return; - float oldWidth = StringWidth(fText); - free(fText); fText = text ? strdup(text) : NULL; - if (oldWidth != StringWidth(fText)) + float newTextWidth = StringWidth(fText); + if (fTextWidth != newTextWidth) { + fTextWidth = newTextWidth; InvalidateLayout(); + } Invalidate(); } @@ -348,6 +352,8 @@ { BView::SetFont(font, mask); + fTextWidth = StringWidth(fText); + Invalidate(); InvalidateLayout(); } @@ -434,13 +440,13 @@ { if (fPreferredSize.width < 0) { // width - fPreferredSize.width = ceilf(StringWidth(fText)); + fPreferredSize.width = ceilf(fTextWidth); // height font_height fontHeight; GetFontHeight(&fontHeight); - - fPreferredSize.height = ceilf(fontHeight.ascent + fontHeight.descent + + fPreferredSize.height = ceilf(fontHeight.ascent + fontHeight.descent + fontHeight.leading); ResetLayoutInvalidation();