All controls/views now accept NULL arguments for "width" and "height" in GetPreferredSize().

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@15091 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2005-11-23 17:29:39 +00:00
parent f97761575a
commit 8643b0987c
9 changed files with 161 additions and 141 deletions

View File

@ -328,24 +328,27 @@ BBox::ResizeToPreferred()
void
BBox::GetPreferredSize(float *width, float *height)
BBox::GetPreferredSize(float *_width, float *_height)
{
float width, height;
bool label = true;
// acount for label
if (fLabelView) {
fLabelView->GetPreferredSize(width, height);
*width += 10.0;
fLabelView->GetPreferredSize(&width, &height);
width += 10.0;
// the label view is placed 10 pixels from the left
} else if (fLabel) {
font_height fh;
GetFontHeight(&fh);
*width += ceilf(StringWidth(fLabel));
*height += ceilf(fh.ascent + fh.descent);
width += ceilf(StringWidth(fLabel));
height += ceilf(fh.ascent + fh.descent);
} else {
label = false;
*width = 0;
*height = 0;
width = 0;
height = 0;
}
// acount for border
switch (fStyle) {
case B_NO_BORDER:
@ -353,23 +356,28 @@ BBox::GetPreferredSize(float *width, float *height)
case B_PLAIN_BORDER:
// label: (1 pixel for border + 1 pixel for padding) * 2
// no label: (1 pixel for border) * 2 + 1 pixel for padding
*width += label ? 4 : 3;
width += label ? 4 : 3;
// label: 1 pixel for bottom border + 1 pixel for padding
// no label: (1 pixel for border) * 2 + 1 pixel for padding
*height += label ? 2 : 3;
height += label ? 2 : 3;
break;
case B_FANCY_BORDER:
// label: (2 pixel for border + 1 pixel for padding) * 2
// no label: (2 pixel for border) * 2 + 1 pixel for padding
*width += label ? 6 : 5;
width += label ? 6 : 5;
// label: 2 pixel for bottom border + 1 pixel for padding
// no label: (2 pixel for border) * 2 + 1 pixel for padding
*height += label ? 3 : 5;
height += label ? 3 : 5;
break;
}
// NOTE: children are ignored, you can use BBox::GetPreferredSize()
// to get the minimum size of this object, then add the size
// of your child(ren) plus inner padding for the final size
if (_width)
*_width = width;
if (_height)
*_height;
}

View File

@ -357,18 +357,22 @@ BCheckBox::SetValue(int32 value)
void
BCheckBox::GetPreferredSize(float *width, float *height)
BCheckBox::GetPreferredSize(float* _width, float* _height)
{
font_height fh;
GetFontHeight(&fh);
font_height fontHeight;
GetFontHeight(&fontHeight);
*height = (float)ceil(6.0f + fh.ascent + fh.descent);
*width = 12.0f + fh.ascent;
if (Label())
*width += StringWidth(Label());
if (_width) {
float width = 12.0f + fontHeight.ascent;
*width = (float)ceil(*width);
if (Label())
width += StringWidth(Label());
*_width = (float)ceil(width);
}
if (_height)
*_height = (float)ceil(6.0f + fontHeight.ascent + fontHeight.descent);
}

View File

@ -1333,18 +1333,22 @@ BMenu::ComputeLayout(int32 index, bool bestFit, bool moveItems,
break;
}
// This is for BMenuBar.
if ((ResizingMode() & B_FOLLOW_LEFT_RIGHT) == B_FOLLOW_LEFT_RIGHT) {
if (Parent())
*_width = Parent()->Frame().Width() + 1;
else if (Window())
*_width = Window()->Frame().Width() + 1;
else
*_width = Bounds().Width();
} else
*_width = frame.Width();
// This is for BMenuBar
*_height = frame.Height();
if (_width) {
if ((ResizingMode() & B_FOLLOW_LEFT_RIGHT) == B_FOLLOW_LEFT_RIGHT) {
if (Parent())
*_width = Parent()->Frame().Width() + 1;
else if (Window())
*_width = Window()->Frame().Width() + 1;
else
*_width = Bounds().Width();
} else
*_width = frame.Width();
}
if (_height)
*_height = frame.Height();
}

View File

@ -486,7 +486,7 @@ BMenuField::GetPreferredSize(float *_width, float *_height)
{
BView::GetPreferredSize(_width, _height);
if (!fFixedSizeMB) {
if (!fFixedSizeMB && _width != NULL) {
BMenu* menu = Menu();
float width = 0;
@ -507,13 +507,15 @@ BMenuField::GetPreferredSize(float *_width, float *_height)
}
// TODO: fix these values (they should match the visual appearance)
*_width = width + 45 + fDivider;
width += 45 + fDivider;
if (Label())
*_width += StringWidth(Label()) + 5;
width += StringWidth(Label()) + 5;
*_width = width;
}
*_height = fMenuBar->Bounds().Height();
if (_height)
*_height = fMenuBar->Bounds().Height();
}

View File

@ -1,34 +1,19 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2003-2004 Haiku
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// File Name: OptionPopUp.cpp
// Author: Stefano Ceccherini (burton666@libero.it)
// Description: An option like control.
//------------------------------------------------------------------------------
/*
* Copyright 2003-2005, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Authors:
* Stefano Ceccherini (burton666@libero.it)
*/
#include <MenuField.h>
#include <MenuItem.h>
#include <OptionPopUp.h>
#include <PopUpMenu.h>
#include <cstdio>
#include <stdio.h>
// If enabled, behaves like in BeOS R5, in that when you call
// SelectOptionFor() or SetValue(), the selected item isn't marked, and
@ -49,9 +34,8 @@ const float kHeightModifier = 10.0;
\param flags View flags. They will be passed to the base class.
*/
BOptionPopUp::BOptionPopUp(BRect frame, const char *name, const char *label,
BMessage *message, uint32 resize, uint32 flags)
:
BOptionControl(frame, name, label, message, resize, flags)
BMessage *message, uint32 resize, uint32 flags)
: BOptionControl(frame, name, label, message, resize, flags)
{
BPopUpMenu *popUp = new BPopUpMenu(label, true, true);
_mField = new BMenuField(Bounds(), "_menu", label, popUp);
@ -70,9 +54,8 @@ BOptionPopUp::BOptionPopUp(BRect frame, const char *name, const char *label,
\param flags View flags. They will be passed to the base class.
*/
BOptionPopUp::BOptionPopUp(BRect frame, const char *name, const char *label,
BMessage *message, bool fixed, uint32 resize, uint32 flags)
:
BOptionControl(frame, name, label, message, resize, flags)
BMessage *message, bool fixed, uint32 resize, uint32 flags)
: BOptionControl(frame, name, label, message, resize, flags)
{
BPopUpMenu *popUp = new BPopUpMenu(label, true, true);
_mField = new BMenuField(Bounds(), "_menu", label, popUp, fixed);
@ -80,9 +63,6 @@ BOptionPopUp::BOptionPopUp(BRect frame, const char *name, const char *label,
}
/*! \brief Frees the allocated resources.
It does nothing.
*/
BOptionPopUp::~BOptionPopUp()
{
}
@ -112,7 +92,7 @@ BOptionPopUp::GetOptionAt(int32 index, const char **outName, int32 *outValue)
{
bool result = false;
BMenu *menu = _mField->Menu();
if (menu != NULL) {
BMenuItem *item = menu->ItemAt(index);
if (item != NULL) {
@ -124,7 +104,7 @@ BOptionPopUp::GetOptionAt(int32 index, const char **outName, int32 *outValue)
result = true;
}
}
return result;
}
@ -280,36 +260,38 @@ BOptionPopUp::SetEnabled(bool state)
preferred height.
*/
void
BOptionPopUp::GetPreferredSize(float *width, float *height)
BOptionPopUp::GetPreferredSize(float* _width, float* _height)
{
// Calculate control's height, looking at the BMenuField font's height
font_height fontHeight;
_mField->GetFontHeight(&fontHeight);
if (height != NULL)
*height = fontHeight.ascent + fontHeight.descent +
fontHeight.leading + kHeightModifier;
float maxWidth = 0;
BMenu *menu = _mField->Menu();
if (menu == NULL)
return;
// Iterate over all the entries in the control,
// and take the maximum width.
// TODO: Should we call BMenuField::GetPreferredSize() instead ?
int32 numItems = menu->CountItems();
for (int32 i = 0; i < numItems; i++) {
BMenuItem *item = menu->ItemAt(i);
if (item != NULL) {
float stringWidth = menu->StringWidth(item->Label());
maxWidth = max_c(maxWidth, stringWidth);
}
if (_height != NULL) {
font_height fontHeight;
_mField->GetFontHeight(&fontHeight);
*_height = fontHeight.ascent + fontHeight.descent
+ fontHeight.leading + kHeightModifier;
}
if (_width != NULL) {
float maxWidth = 0;
BMenu *menu = _mField->Menu();
if (menu == NULL)
return;
// Iterate over all the entries in the control,
// and take the maximum width.
// TODO: Should we call BMenuField::GetPreferredSize() instead ?
int32 numItems = menu->CountItems();
for (int32 i = 0; i < numItems; i++) {
BMenuItem *item = menu->ItemAt(i);
if (item != NULL) {
float stringWidth = menu->StringWidth(item->Label());
maxWidth = max_c(maxWidth, stringWidth);
}
}
maxWidth += _mField->StringWidth(BControl::Label()) + kLabelSpace + kWidthModifier;
*_width = maxWidth;
}
maxWidth += _mField->StringWidth(BControl::Label()) + kLabelSpace + kWidthModifier;
if (width != NULL)
*width = maxWidth;
}

View File

@ -5,11 +5,11 @@
* Authors:
* Marc Flerackers (mflerackers@androme.be)
* Stephan Aßmus <superstippi@gmx.de>
*
* Description:
* BRadioButton represents a single on/off button.
* All sibling BRadioButton objects comprise a single
* "multiple choice" control.
*/
/** BRadioButton represents a single on/off button.
* All sibling BRadioButton objects comprise a single
* "multiple choice" control.
*/
#include <Box.h>
@ -318,18 +318,23 @@ BRadioButton::SetValue(int32 value)
void
BRadioButton::GetPreferredSize(float *width, float *height)
BRadioButton::GetPreferredSize(float* _width, float* _height)
{
font_height fh;
GetFontHeight(&fh);
if (_width) {
float width = 22.0f; // TODO: check if ascent is included
*height = (float)ceil(fh.ascent + fh.descent) + 6.0f;
*width = 22.0f; // TODO: check if ascent is included
if (Label())
width += StringWidth(Label());
if (Label())
*width += StringWidth(Label());
*_width = (float)ceil(width);
}
*width = (float)ceil(*width);
if (_height) {
font_height fontHeight;
GetFontHeight(&fontHeight);
*_height = (float)ceil(fontHeight.ascent + fontHeight.descent) + 6.0f;
}
}

View File

@ -896,14 +896,18 @@ BScrollBar::ResizeToPreferred()
// GetPreferredSize
void
BScrollBar::GetPreferredSize(float *width, float *height)
BScrollBar::GetPreferredSize(float* _width, float* _height)
{
if (fOrientation == B_VERTICAL) {
*width = B_V_SCROLL_BAR_WIDTH;
*height = Bounds().Height();
if (_width)
*_width = B_V_SCROLL_BAR_WIDTH;
if (_height)
*_height = Bounds().Height();
} else if (fOrientation == B_HORIZONTAL) {
*width = Bounds().Width();
*height = B_H_SCROLL_BAR_HEIGHT;
if (_width)
*_width = Bounds().Width();
if (_height)
*_height = B_H_SCROLL_BAR_HEIGHT;
}
}

View File

@ -1152,11 +1152,12 @@ BSlider::GetPreferredSize(float* _width, float* _height)
font_height fontHeight;
GetFontHeight(&fontHeight);
float width, height;
int32 rows = 0;
if (Orientation() == B_HORIZONTAL) {
*_width = Frame().Width();
*_height = 12.0f + fBarThickness;
width = Frame().Width();
height = 12.0f + fBarThickness;
float labelWidth = 0;
if (Label()) {
@ -1175,21 +1176,21 @@ BSlider::GetPreferredSize(float* _width, float* _height)
minWidth += StringWidth(MaxLimitLabel());
}
if (minWidth > *_width)
*_width = minWidth;
if (labelWidth > *_width)
*_width = labelWidth;
if (*_width < 32.0f)
*_width = 32.0f;
if (minWidth > width)
width = minWidth;
if (labelWidth > width)
width = labelWidth;
if (width < 32.0f)
width = 32.0f;
if (MinLimitLabel() || MaxLimitLabel())
rows++;
*_height += rows * ((float)ceil(fontHeight.ascent + fontHeight.descent) + 4.0f);
height += rows * ((float)ceil(fontHeight.ascent + fontHeight.descent) + 4.0f);
} else {
// B_VERTICAL
*_width = 12.0f + fBarThickness;
*_height = Frame().Height();
width = 12.0f + fBarThickness;
height = Frame().Height();
// find largest label
@ -1211,8 +1212,8 @@ BSlider::GetPreferredSize(float* _width, float* _height)
rows++;
}
if (minWidth > *_width)
*_width = minWidth;
if (minWidth > width)
width = minWidth;
float minHeight = 32.0f + rows
* ((float)ceil(fontHeight.ascent + fontHeight.descent) + 4.0f);
@ -1220,9 +1221,14 @@ BSlider::GetPreferredSize(float* _width, float* _height)
if (Label() && MaxLimitLabel())
minHeight -= 4.0f;
if (minHeight > *_height)
*_height = minHeight;
if (minHeight > height)
height = minHeight;
}
if (_width)
*_width = width;
if (_height)
*_height = height;
}

View File

@ -533,15 +533,20 @@ BStatusBar::ResizeToPreferred()
void
BStatusBar::GetPreferredSize(float *width, float *height)
BStatusBar::GetPreferredSize(float* _width, float* _height)
{
font_height fh;
GetFontHeight(&fh);
*width = (fLabel ? (float)ceil(StringWidth(fLabel)) : 0.0f) +
(fTrailingLabel ? (float)ceil(StringWidth(fTrailingLabel)) : 0.0f) +
7.0f;
*height = fh.ascent + fh.descent + 5.0f + BarHeight();
if (_width) {
*_width = (fLabel ? (float)ceil(StringWidth(fLabel)) : 0.0f)
+ (fTrailingLabel ? (float)ceil(StringWidth(fTrailingLabel)) : 0.0f)
+ 7.0f;
}
if (_height) {
font_height fontHeight;
GetFontHeight(&fontHeight);
*_height = ceil(fontHeight.ascent + fontHeight.descent) + 5.0f + BarHeight();
}
}