From 45b013192a6c00c924d63bb9769f65a08b8745b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Tue, 22 Nov 2005 19:02:56 +0000 Subject: [PATCH] fix crashes when the text pointer is NULL, this lets you for example pick a background image with the Backrounds preflet, though it doesn't work further down git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@15076 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/interface/StringView.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/kits/interface/StringView.cpp b/src/kits/interface/StringView.cpp index a08cd6dd99..6242dd5631 100644 --- a/src/kits/interface/StringView.cpp +++ b/src/kits/interface/StringView.cpp @@ -49,7 +49,7 @@ BStringView::BStringView(BRect frame, const char* name, const char* text, uint32 resizeMask, uint32 flags) : BView(frame, name, resizeMask, flags) { - fText = strdup(text); + fText = text ? strdup(text) : NULL; fAlign = B_ALIGN_LEFT; } //------------------------------------------------------------------------------ @@ -99,7 +99,7 @@ BStringView::~BStringView() void BStringView::SetText(const char* text) { free(fText); - fText = strdup(text); + fText = text ? strdup(text) : NULL; Invalidate(); } //------------------------------------------------------------------------------ @@ -128,6 +128,9 @@ void BStringView::AttachedToWindow() //------------------------------------------------------------------------------ void BStringView::Draw(BRect bounds) { + if (!fText) + return; + SetLowColor(ViewColor()); BFont font; GetFont(&font); @@ -136,14 +139,13 @@ void BStringView::Draw(BRect bounds) float y = Bounds().bottom - ceil(fh.descent); float x; - switch (fAlign) - { + switch (fAlign) { case B_ALIGN_RIGHT: x = Bounds().Width() - font.StringWidth(fText) - 2.0f; break; case B_ALIGN_CENTER: - x = (Bounds().Width() - font.StringWidth(fText))/2.0f; + x = (Bounds().Width() - font.StringWidth(fText)) / 2.0f; break; default: @@ -151,7 +153,7 @@ void BStringView::Draw(BRect bounds) break; } - DrawString( fText, BPoint(x,y) ); + DrawString(fText, BPoint(x,y)); } //------------------------------------------------------------------------------ void BStringView::ResizeToPreferred() @@ -163,6 +165,9 @@ void BStringView::ResizeToPreferred() //------------------------------------------------------------------------------ void BStringView::GetPreferredSize(float* width, float* height) { + if (!fText) + return BView::GetPreferredSize(width, height); + BFont font; GetFont(&font); font_height fh;