* Cache a fPreferredSize following the example of BButton. Changed
GetPreferredSize() accordingly. * No longer adds margins to the left/right side and top/bottom. These will make it difficult to make exact visual alignments with other controls and labels. * Invalidate the layout in SetText(). git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26422 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -19,6 +19,8 @@ class BStringView : public BView{
|
|||||||
const char* text,
|
const char* text,
|
||||||
uint32 resizeFlags = B_FOLLOW_LEFT | B_FOLLOW_TOP,
|
uint32 resizeFlags = B_FOLLOW_LEFT | B_FOLLOW_TOP,
|
||||||
uint32 flags = B_WILL_DRAW);
|
uint32 flags = B_WILL_DRAW);
|
||||||
|
BStringView(const char* name, const char* text,
|
||||||
|
uint32 flags = B_WILL_DRAW);
|
||||||
BStringView(BMessage* data);
|
BStringView(BMessage* data);
|
||||||
virtual ~BStringView();
|
virtual ~BStringView();
|
||||||
|
|
||||||
@@ -53,7 +55,15 @@ class BStringView : public BView{
|
|||||||
virtual void AllDetached();
|
virtual void AllDetached();
|
||||||
virtual status_t GetSupportedSuites(BMessage* data);
|
virtual status_t GetSupportedSuites(BMessage* data);
|
||||||
|
|
||||||
|
// TODO: should be implemented and invalidate the layout
|
||||||
|
// virtual void SetFont(const BFont* font,
|
||||||
|
// uint32 mask = B_FONT_ALL);
|
||||||
|
|
||||||
|
virtual void InvalidateLayout(bool descendants = false);
|
||||||
|
|
||||||
|
virtual BSize MinSize();
|
||||||
virtual BSize MaxSize();
|
virtual BSize MaxSize();
|
||||||
|
virtual BSize PreferredSize();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual status_t Perform(perform_code d, void* arg);
|
virtual status_t Perform(perform_code d, void* arg);
|
||||||
@@ -61,11 +71,14 @@ class BStringView : public BView{
|
|||||||
virtual void _ReservedStringView2();
|
virtual void _ReservedStringView2();
|
||||||
virtual void _ReservedStringView3();
|
virtual void _ReservedStringView3();
|
||||||
|
|
||||||
|
BSize _ValidatePreferredSize();
|
||||||
|
|
||||||
BStringView &operator=(const BStringView&);
|
BStringView &operator=(const BStringView&);
|
||||||
|
|
||||||
char* fText;
|
char* fText;
|
||||||
alignment fAlign;
|
alignment fAlign;
|
||||||
uint32 _reserved[3];
|
BSize fPreferredSize;
|
||||||
|
uint32 _reserved[1];
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _STRING_VIEW_H
|
#endif // _STRING_VIEW_H
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2001-2005, Haiku Inc.
|
* Copyright 2001-2008, Haiku Inc. All rights reserved.
|
||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
* Frans van Nispen ([email protected])
|
* Frans van Nispen ([email protected])
|
||||||
|
* Ingo Weinhold <[email protected]>
|
||||||
|
* Stephan Aßmus <[email protected]>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** BStringView draw a non-editable text string */
|
//! BStringView draws a non-editable text string.
|
||||||
|
|
||||||
|
|
||||||
#include <StringView.h>
|
#include <StringView.h>
|
||||||
@@ -22,17 +24,28 @@
|
|||||||
|
|
||||||
|
|
||||||
BStringView::BStringView(BRect frame, const char* name, const char* text,
|
BStringView::BStringView(BRect frame, const char* name, const char* text,
|
||||||
uint32 resizeMask, uint32 flags)
|
uint32 resizeMask, uint32 flags)
|
||||||
: BView(frame, name, resizeMask, flags)
|
: BView(frame, name, resizeMask, flags),
|
||||||
|
fText(text ? strdup(text) : NULL),
|
||||||
|
fAlign(B_ALIGN_LEFT),
|
||||||
|
fPreferredSize(-1, -1)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BStringView::BStringView(const char* name, const char* text, uint32 flags)
|
||||||
|
: BView(name, flags),
|
||||||
|
fText(text ? strdup(text) : NULL),
|
||||||
|
fAlign(B_ALIGN_LEFT),
|
||||||
|
fPreferredSize(-1, -1)
|
||||||
{
|
{
|
||||||
fText = text ? strdup(text) : NULL;
|
|
||||||
fAlign = B_ALIGN_LEFT;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BStringView::BStringView(BMessage* data)
|
BStringView::BStringView(BMessage* data)
|
||||||
: BView(data),
|
: BView(data),
|
||||||
fText(NULL)
|
fText(NULL),
|
||||||
|
fPreferredSize(-1, -1)
|
||||||
{
|
{
|
||||||
int32 align;
|
int32 align;
|
||||||
if (data->FindInt32("_align", &align) == B_OK)
|
if (data->FindInt32("_align", &align) == B_OK)
|
||||||
@@ -82,11 +95,13 @@ BStringView::~BStringView()
|
|||||||
void
|
void
|
||||||
BStringView::SetText(const char* text)
|
BStringView::SetText(const char* text)
|
||||||
{
|
{
|
||||||
if ((text && fText && !strcmp(text, fText))
|
if ((text && fText && !strcmp(text, fText)) || (!text && !fText))
|
||||||
|| (!text && !fText))
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
free(fText);
|
free(fText);
|
||||||
fText = text ? strdup(text) : NULL;
|
fText = text ? strdup(text) : NULL;
|
||||||
|
|
||||||
|
InvalidateLayout();
|
||||||
Invalidate();
|
Invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,19 +147,22 @@ BStringView::Draw(BRect bounds)
|
|||||||
font_height fontHeight;
|
font_height fontHeight;
|
||||||
GetFontHeight(&fontHeight);
|
GetFontHeight(&fontHeight);
|
||||||
|
|
||||||
float y = Bounds().bottom - ceil(fontHeight.descent);
|
BRect bounds = Bounds();
|
||||||
|
|
||||||
|
float y = (bounds.top + bounds.bottom - ceilf(fontHeight.ascent)
|
||||||
|
- ceilf(fontHeight.descent)) / 2.0 + ceilf(fontHeight.ascent);
|
||||||
float x;
|
float x;
|
||||||
switch (fAlign) {
|
switch (fAlign) {
|
||||||
case B_ALIGN_RIGHT:
|
case B_ALIGN_RIGHT:
|
||||||
x = Bounds().Width() - StringWidth(fText) - 2.0f;
|
x = bounds.Width() - StringWidth(fText);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case B_ALIGN_CENTER:
|
case B_ALIGN_CENTER:
|
||||||
x = (Bounds().Width() - StringWidth(fText)) / 2.0f;
|
x = (bounds.Width() - StringWidth(fText)) / 2.0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
x = 2.0f;
|
x = 0.0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,19 +187,13 @@ BStringView::ResizeToPreferred()
|
|||||||
void
|
void
|
||||||
BStringView::GetPreferredSize(float* _width, float* _height)
|
BStringView::GetPreferredSize(float* _width, float* _height)
|
||||||
{
|
{
|
||||||
if (!fText) {
|
_ValidatePreferredSize();
|
||||||
BView::GetPreferredSize(_width, _height);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_width)
|
if (_width)
|
||||||
*_width = 4.0f + ceil(StringWidth(fText));
|
*_width = fPreferredSize.width;
|
||||||
|
|
||||||
if (_height) {
|
if (_height)
|
||||||
font_height fontHeight;
|
*_height = fPreferredSize.height;
|
||||||
GetFontHeight(&fontHeight);
|
|
||||||
*_height = ceil(fontHeight.ascent + fontHeight.descent + fontHeight.leading) + 2.0f;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -270,13 +282,37 @@ BStringView::GetSupportedSuites(BMessage* message)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BStringView::InvalidateLayout(bool descendants)
|
||||||
|
{
|
||||||
|
// invalidate cached preferred size
|
||||||
|
fPreferredSize.Set(-1, -1);
|
||||||
|
|
||||||
|
BView::InvalidateLayout(descendants);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BSize
|
||||||
|
BStringView::MinSize()
|
||||||
|
{
|
||||||
|
return BLayoutUtils::ComposeSize(ExplicitMinSize(),
|
||||||
|
_ValidatePreferredSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BSize
|
BSize
|
||||||
BStringView::MaxSize()
|
BStringView::MaxSize()
|
||||||
{
|
{
|
||||||
float width, height;
|
return BLayoutUtils::ComposeSize(ExplicitMaxSize(),
|
||||||
GetPreferredSize(&width, &height);
|
_ValidatePreferredSize());
|
||||||
|
}
|
||||||
|
|
||||||
return BLayoutUtils::ComposeSize(ExplicitMaxSize(), BSize(width, height));
|
|
||||||
|
BSize
|
||||||
|
BStringView::PreferredSize()
|
||||||
|
{
|
||||||
|
return BLayoutUtils::ComposeSize(ExplicitPreferredSize(),
|
||||||
|
_ValidatePreferredSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -298,3 +334,22 @@ BStringView::operator=(const BStringView&)
|
|||||||
// Assignment not allowed (private)
|
// Assignment not allowed (private)
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BSize
|
||||||
|
BStringView::_ValidatePreferredSize()
|
||||||
|
{
|
||||||
|
if (fPreferredSize.width < 0) {
|
||||||
|
// width
|
||||||
|
fPreferredSize.width = ceilf(StringWidth(fText));
|
||||||
|
|
||||||
|
// height
|
||||||
|
font_height fontHeight;
|
||||||
|
GetFontHeight(&fontHeight);
|
||||||
|
|
||||||
|
fPreferredSize.height = ceilf(fontHeight.ascent + fontHeight.descent
|
||||||
|
+ fontHeight.leading);
|
||||||
|
}
|
||||||
|
|
||||||
|
return fPreferredSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user