Fixed button size and placement issues with the B_WIDTH_FROM_WIDEST mode, refactored code using OpenTracker style.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@11137 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Matthew Wilber
2005-01-29 23:31:36 +00:00
parent 68bf78e015
commit 6c8068247a
+57 -94
View File
@@ -449,14 +449,24 @@ void BAlert::_ReservedAlert3()
; ;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void BAlert::InitObject(const char* text, const char* button0, float width_from_label(BButton *button)
const char* button1, const char* button2, {
button_width width, button_spacing spacing, // BButton::GetPreferredSize() does not return the minimum width
// required to fit the label. Thus, the width is computed here.
return button->StringWidth(button->Label()) + 20.0f;
}
//------------------------------------------------------------------------------
void
BAlert::InitObject(const char* text, const char* button0, const char* button1,
const char* button2, button_width width, button_spacing spacing,
alert_type type) alert_type type)
{ {
BAutolock Autolock(this); BAutolock Autolock(this);
if (Autolock.IsLocked()) if (!Autolock.IsLocked())
{ // Bail out if a lock can't be acquired
return;
fInvoker = NULL; fInvoker = NULL;
fAlertSem = -1; fAlertSem = -1;
fAlertVal = -1; fAlertVal = -1;
@@ -471,138 +481,95 @@ void BAlert::InitObject(const char* text, const char* button0,
AddChild(MasterView); AddChild(MasterView);
MasterView->SetBitmap(InitIcon()); MasterView->SetBitmap(InitIcon());
// Set up the buttons // Must have at least one button
int buttonCount = 0; if (button0 == NULL) {
// Have to have at least one button
if (button0 == NULL)
{
debugger("BAlert's must have at least one button."); debugger("BAlert's must have at least one button.");
button0 = ""; button0 = "";
} }
BMessage ProtoMsg(kAlertButtonMsg); BMessage ProtoMsg(kAlertButtonMsg);
ProtoMsg.AddInt32("which", 0); ProtoMsg.AddInt32("which", 0);
fButtons[0] = new BButton(BRect(0, 0, 0, 0), "_b0_", button0, // Set up the buttons
new BMessage(ProtoMsg), int buttonCount = 0;
B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM); fButtons[buttonCount] = new BButton(BRect(0, 0, 0, 0), "_b0_", button0,
new BMessage(ProtoMsg), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
MasterView->AddChild(fButtons[buttonCount]);
++buttonCount; ++buttonCount;
if (button1) if (button1) {
{
ProtoMsg.ReplaceInt32("which", 1); ProtoMsg.ReplaceInt32("which", 1);
fButtons[buttonCount] = new BButton(BRect(0, 0, 0, 0), "_b1_", button1, fButtons[buttonCount] = new BButton(BRect(0, 0, 0, 0), "_b1_", button1,
new BMessage(ProtoMsg), new BMessage(ProtoMsg), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
MasterView->AddChild(fButtons[buttonCount]);
++buttonCount; ++buttonCount;
} }
if (button2) {
if (button2)
{
ProtoMsg.ReplaceInt32("which", 2); ProtoMsg.ReplaceInt32("which", 2);
fButtons[buttonCount] = new BButton(BRect(0, 0, 0, 0), "_b2_", button2, fButtons[buttonCount] = new BButton(BRect(0, 0, 0, 0), "_b2_", button2,
new BMessage(ProtoMsg), new BMessage(ProtoMsg), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
MasterView->AddChild(fButtons[buttonCount]);
++buttonCount; ++buttonCount;
} }
SetDefaultButton(fButtons[buttonCount - 1]); SetDefaultButton(fButtons[buttonCount - 1]);
float buttonWidth = 0; // Find the widest button only if the widest value needs to be known.
float buttonHeight = 0; float maxWidth = 0;
for (int i = 0; i < buttonCount; ++i) if (fButtonWidth == B_WIDTH_FROM_WIDEST) {
{ for (int i = 0; i < buttonCount; ++i) {
float temp; float temp = width_from_label(fButtons[i]);
fButtons[i]->GetPreferredSize(&temp, &buttonHeight); maxWidth = max(maxWidth, temp);
buttonWidth = max(buttonWidth, temp); }
} }
// Add first, because the buttons will ResizeToPreferred() for (int i = buttonCount - 1; i >= 0; --i) {
// in AttachedToWindow() // Determine the button's size
for (int i = 0; i < buttonCount; ++i) float buttonWidth = 0, buttonHeight = 0;
{
MasterView->AddChild(fButtons[i]);
}
for (int i = buttonCount - 1; i >= 0; --i)
{
switch (fButtonWidth)
{
case B_WIDTH_FROM_WIDEST:
fButtons[i]->ResizeTo(buttonWidth, buttonHeight);
break;
case B_WIDTH_FROM_LABEL:
fButtons[i]->GetPreferredSize(&buttonWidth, &buttonHeight);
// GetPreferredSize() does not return the minimum width required to
// fit the label. Thus, the width is computed here.
buttonWidth = fButtons[i]->StringWidth(fButtons[i]->Label()) + 20.0f;
fButtons[i]->ResizeTo(buttonWidth, buttonHeight);
break;
default: // B_WIDTH_AS_USUAL
fButtons[i]->GetPreferredSize(&buttonWidth, &buttonHeight); fButtons[i]->GetPreferredSize(&buttonWidth, &buttonHeight);
if (fButtonWidth == B_WIDTH_FROM_WIDEST)
buttonWidth = maxWidth;
else if (fButtonWidth == B_WIDTH_FROM_LABEL)
buttonWidth = width_from_label(fButtons[i]);
else // B_WIDTH_AS_USUAL
buttonWidth = max(buttonWidth, kButtonUsualWidth); buttonWidth = max(buttonWidth, kButtonUsualWidth);
fButtons[i]->ResizeTo(buttonWidth, buttonHeight); fButtons[i]->ResizeTo(buttonWidth, buttonHeight);
break;
}
float buttonX; // Determine the button's placement
float buttonY; float buttonX, buttonY;
float temp;
fButtons[i]->GetPreferredSize(&temp, &buttonHeight);
buttonY = Bounds().bottom - buttonHeight; buttonY = Bounds().bottom - buttonHeight;
if (i == buttonCount - 1) {
if (i == buttonCount - 1) // the right-most button // The right-most button
{
buttonX = Bounds().right - fButtons[i]->Frame().Width() - buttonX = Bounds().right - fButtons[i]->Frame().Width() -
kButtonRightOffset; kButtonRightOffset;
buttonY -= kDefButtonBottomOffset; buttonY -= kDefButtonBottomOffset;
} } else {
else
{
buttonX = fButtons[i + 1]->Frame().left - buttonX = fButtons[i + 1]->Frame().left -
fButtons[i]->Frame().Width() - fButtons[i]->Frame().Width() - kButtonSpaceOffset;
kButtonSpaceOffset; buttonY -= kButtonBottomOffset;
if (i == 0) {
if (i == 0)
{
if (spacing == B_OFFSET_SPACING) if (spacing == B_OFFSET_SPACING)
{
buttonX -= kButtonOffsetSpaceOffset; buttonX -= kButtonOffsetSpaceOffset;
}
else if (buttonCount == 3) else if (buttonCount == 3)
{
buttonX -= 3; buttonX -= 3;
} }
} }
buttonY -= kButtonBottomOffset;
}
fButtons[i]->MoveTo(buttonX, buttonY); fButtons[i]->MoveTo(buttonX, buttonY);
} } // for (int i = buttonCount - 1; i >= 0; --i)
// Adjust the window's width, if necessary
// Resize the window, if necessary
float totalWidth = kButtonRightOffset; float totalWidth = kButtonRightOffset;
totalWidth += fButtons[buttonCount - 1]->Frame().right - totalWidth += fButtons[buttonCount - 1]->Frame().right -
fButtons[0]->Frame().left; fButtons[0]->Frame().left;
if (MasterView->Bitmap()) if (MasterView->Bitmap())
{
totalWidth += kIconStripeWidth + kWindowIconOffset; totalWidth += kIconStripeWidth + kWindowIconOffset;
}
else else
{
totalWidth += kWindowMinOffset; totalWidth += kWindowMinOffset;
}
if (spacing == B_OFFSET_SPACING) if (spacing == B_OFFSET_SPACING)
{
totalWidth = max(kWindowOffsetMinWidth, totalWidth); totalWidth = max(kWindowOffsetMinWidth, totalWidth);
} else {
else
{
totalWidth += 5; totalWidth += 5;
totalWidth = max(kWindowMinWidth, totalWidth); totalWidth = max(kWindowMinWidth, totalWidth);
} }
@@ -613,12 +580,9 @@ void BAlert::InitObject(const char* text, const char* button0,
Bounds().right - kTextRightOffset, Bounds().right - kTextRightOffset,
Bounds().bottom - kTextBottomOffset); Bounds().bottom - kTextBottomOffset);
if (MasterView->Bitmap()) if (MasterView->Bitmap())
{
TextViewRect.left = kTextIconOffset; TextViewRect.left = kTextIconOffset;
}
fTextView = new BTextView(TextViewRect, "_tv_", fTextView = new BTextView(TextViewRect, "_tv_", TextViewRect,
TextViewRect,
B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW); B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW);
MasterView->AddChild(fTextView); MasterView->AddChild(fTextView);
@@ -640,7 +604,6 @@ void BAlert::InitObject(const char* text, const char* button0,
AddCommonFilter(new _BAlertFilter_(this)); AddCommonFilter(new _BAlertFilter_(this));
MoveTo(AlertPosition(Frame().Width(), Frame().Height())); MoveTo(AlertPosition(Frame().Width(), Frame().Height()));
}
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BBitmap* BAlert::InitIcon() BBitmap* BAlert::InitIcon()