BBox: Always offset the top border by the same amount

...so that the top border of BBox's with no labels, BBox's with
text labels, and BBox's with BControl labels will all line up.
This commit is contained in:
John Scipione 2013-05-30 20:38:04 -04:00
parent f9954bfc69
commit 39899cf662
2 changed files with 30 additions and 17 deletions

View File

@ -63,6 +63,7 @@ class BBox : public BView {
virtual void ResizeToPreferred();
virtual void GetPreferredSize(float* _width, float* _height);
virtual void MakeFocus(bool focused = true);
virtual void SetFont(const BFont* font, uint32 mask = B_FONT_ALL);
virtual status_t GetSupportedSuites(BMessage* message);
virtual status_t Perform(perform_code d, void* arg);
@ -96,6 +97,7 @@ class BBox : public BView {
border_style fStyle;
BView* fLabelView;
LayoutData* fLayoutData;
float fLabelHeight;
};
#endif // _BOX_H

View File

@ -158,12 +158,7 @@ BBox::Border() const
float
BBox::TopBorderOffset()
{
_ValidateLayoutData();
if (fLabel != NULL || fLabelView != NULL)
return fLayoutData->label_box.Height() / 2;
return 0;
return fLabelHeight / 2;
}
@ -446,6 +441,18 @@ BBox::MakeFocus(bool focused)
}
void
BBox::SetFont(const BFont* font, uint32 mask)
{
BView::SetFont(font, mask);
// recalculate the label height based on the new font
font_height fontHeight;
GetFontHeight(&fontHeight);
fLabelHeight = ceilf(fontHeight.ascent + fontHeight.descent) + 1;
}
status_t
BBox::GetSupportedSuites(BMessage* message)
{
@ -635,6 +642,11 @@ BBox::_InitObject(BMessage* archive)
if (flags != 0)
SetFont(&font, flags);
else {
font_height fontHeight;
GetFontHeight(&fontHeight);
fLabelHeight = ceilf(fontHeight.ascent + fontHeight.descent) + 1;
}
if (archive != NULL) {
const char* string;
@ -801,8 +813,9 @@ BBox::_ValidateLayoutData()
return;
// compute the label box, width and height
bool label = true;
float labelHeight = 0; // height of the label (pixel count)
bool hasLabel = true;
float labelHeight = 0;
// height of the label (pixel count)
if (fLabel) {
// leave 6 pixels of the frame, and have a gap of 4 pixels between
// the frame and the text on either side
@ -810,14 +823,15 @@ BBox::_ValidateLayoutData()
GetFontHeight(&fontHeight);
fLayoutData->label_box.Set(6.0f, 0, 14.0f + StringWidth(fLabel),
ceilf(fontHeight.ascent));
labelHeight = ceilf(fontHeight.ascent + fontHeight.descent) + 1;
labelHeight = fLabelHeight;
} else if (fLabelView) {
// the label view is placed at (0, 10) at its preferred size
BSize size = fLabelView->PreferredSize();
fLayoutData->label_box.Set(10, 0, 10 + size.width, size.height);
labelHeight = size.height + 1;
} else {
label = false;
labelHeight = fLabelHeight;
hasLabel = false;
}
// border
@ -834,8 +848,8 @@ BBox::_ValidateLayoutData()
break;
}
// if there's a label, the top inset will be dictated by the label
if (label && labelHeight > fLayoutData->insets.top)
// Grow the top inset by the label height
if (labelHeight > fLayoutData->insets.top)
fLayoutData->insets.top = labelHeight;
// total number of pixel the border adds
@ -843,11 +857,8 @@ BBox::_ValidateLayoutData()
float addHeight = fLayoutData->insets.top + fLayoutData->insets.bottom;
// compute the minimal width induced by the label
float minWidth;
if (label)
minWidth = fLayoutData->label_box.right + fLayoutData->insets.right;
else
minWidth = addWidth - 1;
float minWidth = !hasLabel ? addWidth - 1
: fLayoutData->label_box.right + fLayoutData->insets.right;
// finally consider the child constraints, if we shall support layout
BView* child = _Child();