Files
haiku-beta6/src/kits/interface/StatusBar.cpp
T

648 lines
12 KiB
C++
Raw Normal View History

2003-05-14 17:21:46 +00:00
//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// 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: StatusBar.cpp
// Author: Marc Flerackers ([email protected])
// Description: BStatusBar displays a "percentage-of-completion" gauge.
//------------------------------------------------------------------------------
#include <string.h>
#include <stdlib.h>
#include <Debug.h>
2003-05-14 17:21:46 +00:00
#include <Message.h>
#include <StatusBar.h>
2003-05-14 17:21:46 +00:00
BStatusBar::BStatusBar(BRect frame, const char *name, const char *label,
const char *trailingLabel)
: BView(frame, name, B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW),
fLabel(NULL),
fTrailingLabel(NULL),
2003-05-14 17:21:46 +00:00
fText(NULL),
fTrailingText(NULL),
fMax(100.0f),
fCurrent(0.0f),
fBarHeight(-1.0f),
fTrailingWidth(-1.0f),
fEraseText(-1.0f),
fEraseTrailingText(-1.0f),
fCustomBarHeight(false)
{
// TODO: Move initializer list and other stuff to InitObject
InitObject(label, trailingLabel);
2003-05-14 17:21:46 +00:00
fBarColor.red = 50;
2003-05-14 17:21:46 +00:00
fBarColor.green = 150;
fBarColor.blue = 255;
fBarColor.alpha = 255;
}
2003-05-14 17:21:46 +00:00
BStatusBar::BStatusBar(BMessage *archive)
: BView(archive),
fLabel(NULL),
fTrailingLabel(NULL),
fText(NULL),
fTrailingText(NULL),
fMax(100.0f),
fCurrent(0.0f),
fBarHeight(-1.0f),
2003-05-14 17:21:46 +00:00
fTrailingWidth(-1.0f),
fEraseText(-1.0f),
fEraseTrailingText(-1.0f),
fCustomBarHeight(false)
{
const char *label = NULL;
const char *trailingLabel = NULL;
archive->FindString("_label", &label);
archive->FindString("_tlabel", &trailingLabel);
InitObject(label, trailingLabel);
2003-05-14 17:21:46 +00:00
if (archive->FindFloat("_high", &fBarHeight) != B_OK)
fBarHeight = -1.0f;
const void *ptr;
if (archive->FindData("_bcolor", B_INT32_TYPE, &ptr, NULL ) < B_OK) {
2003-05-14 17:21:46 +00:00
fBarColor.red = 50;
fBarColor.green = 150;
fBarColor.blue = 255;
fBarColor.alpha = 255;
} else
2003-05-14 17:21:46 +00:00
memcpy(&fBarColor, ptr, sizeof(rgb_color));
if (archive->FindFloat("_val", &fCurrent) < B_OK)
2003-05-14 17:21:46 +00:00
fCurrent = 0.0f;
if (archive->FindFloat("_max", &fMax) < B_OK)
2003-05-14 17:21:46 +00:00
fMax = 100.0f;
const char *string;
if (archive->FindString("_text", &string) < B_OK)
2003-05-14 17:21:46 +00:00
fText = NULL;
else
fText = strdup(string);
if (archive->FindString("_ttext", &string) < B_OK)
2003-05-14 17:21:46 +00:00
fTrailingText = NULL;
else
fTrailingText = strdup(string);
}
2003-05-14 17:21:46 +00:00
BStatusBar::~BStatusBar()
{
free(fLabel);
free(fTrailingLabel);
free(fText);
free(fTrailingText);
}
2003-05-14 17:21:46 +00:00
BArchivable *
BStatusBar::Instantiate(BMessage *archive)
2003-05-14 17:21:46 +00:00
{
if (validate_instantiation(archive, "BStatusBar"))
return new BStatusBar(archive);
return NULL;
}
status_t
BStatusBar::Archive(BMessage *archive, bool deep) const
2003-05-14 17:21:46 +00:00
{
status_t err = BView::Archive(archive, deep);
if (err < B_OK)
2003-05-14 17:21:46 +00:00
return err;
if (fBarHeight != 16.0f)
err = archive->AddFloat("_high", fBarHeight);
if (err < B_OK)
2003-05-14 17:21:46 +00:00
return err;
// DW: I'm pretty sure we don't need to compare the color with (50, 150, 255) ?
2003-05-14 17:21:46 +00:00
err = archive->AddData("_bcolor", B_INT32_TYPE, &fBarColor, sizeof( int32 ));
if (err < B_OK)
2003-05-14 17:21:46 +00:00
return err;
if (fCurrent != 0.0f)
err = archive->AddFloat("_val", fCurrent);
if (err < B_OK)
2003-05-14 17:21:46 +00:00
return err;
if (fMax != 100.0f )
err = archive->AddFloat("_max", fMax);
if (err < B_OK)
2003-05-14 17:21:46 +00:00
return err;
if (fText )
err = archive->AddString("_text", fText);
if (err < B_OK)
2003-05-14 17:21:46 +00:00
return err;
if (fTrailingText)
err = archive->AddString("_ttext", fTrailingText);
if (err < B_OK)
2003-05-14 17:21:46 +00:00
return err;
if (fLabel)
err = archive->AddString("_label", fLabel);
if (err < B_OK)
2003-05-14 17:21:46 +00:00
return err;
if (fTrailingLabel)
err = archive->AddString ("_tlabel", fTrailingLabel);
return err;
}
void
BStatusBar::AttachedToWindow()
2003-05-14 17:21:46 +00:00
{
float width, height;
GetPreferredSize(&width, &height);
ResizeTo(Frame().Width(), height);
if (Parent()) {
SetViewColor(Parent()->ViewColor());
SetLowColor(Parent()->ViewColor());
}
2003-05-14 17:21:46 +00:00
}
void
BStatusBar::MessageReceived(BMessage *message)
2003-05-14 17:21:46 +00:00
{
switch(message->what) {
2003-05-14 17:21:46 +00:00
case B_UPDATE_STATUS_BAR:
{
float delta;
const char *text = NULL, *trailing_text = NULL;
message->FindFloat("delta", &delta);
message->FindString("text", &text);
message->FindString("trailing_text", &trailing_text);
2003-05-14 17:21:46 +00:00
Update(delta, text, trailing_text);
break;
}
case B_RESET_STATUS_BAR:
2003-05-14 17:21:46 +00:00
{
const char *label = NULL, *trailing_label = NULL;
message->FindString("label", &label);
message->FindString("trailing_label", &trailing_label);
2003-05-14 17:21:46 +00:00
Reset(label, trailing_label);
break;
}
2003-05-14 17:21:46 +00:00
default:
BView::MessageReceived(message);
break;
2003-05-14 17:21:46 +00:00
}
}
void
BStatusBar::Draw(BRect updateRect)
2003-05-14 17:21:46 +00:00
{
float width = Frame().Width();
font_height fh;
GetFontHeight(&fh);
SetHighColor(0, 0, 0);
MovePenTo(2.0f, (float)ceil(fh.ascent) + 1.0f);
if (fLabel)
DrawString(fLabel);
if (fText)
DrawString(fText);
if (fTrailingText) {
if (fTrailingLabel) {
2003-05-14 17:21:46 +00:00
MovePenTo(width - StringWidth(fTrailingText) -
StringWidth(fTrailingLabel) - 2.0f,
(float)ceil(fh.ascent) + 1.0f);
DrawString(fTrailingText);
DrawString(fTrailingLabel);
} else {
2003-05-14 17:21:46 +00:00
MovePenTo(width - StringWidth(fTrailingText) - 2.0f,
(float)ceil(fh.ascent) + 1.0f);
DrawString(fTrailingText);
}
} else if (fTrailingLabel) {
2003-05-14 17:21:46 +00:00
MovePenTo(width - StringWidth(fTrailingLabel) - 2.0f,
(float)ceil(fh.ascent) + 1.0f);
DrawString(fTrailingLabel);
}
BRect rect(0.0f, (float)ceil(fh.ascent) + 4.0f, width,
(float)ceil(fh.ascent) + 4.0f + fBarHeight);
// First bevel
SetHighColor(tint_color(ui_color ( B_PANEL_BACKGROUND_COLOR ), B_DARKEN_1_TINT));
StrokeLine(BPoint(rect.left, rect.bottom), BPoint(rect.left, rect.top));
StrokeLine(BPoint(rect.right, rect.top));
SetHighColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), B_LIGHTEN_2_TINT));
StrokeLine(BPoint(rect.left + 1.0f, rect.bottom), BPoint(rect.right, rect.bottom));
StrokeLine(BPoint(rect.right, rect.top + 1.0f));
rect.InsetBy(1.0f, 1.0f);
// Second bevel
SetHighColor(tint_color(ui_color ( B_PANEL_BACKGROUND_COLOR ), B_DARKEN_4_TINT));
StrokeLine(BPoint(rect.left, rect.bottom), BPoint(rect.left, rect.top));
StrokeLine(BPoint(rect.right, rect.top));
SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR));
StrokeLine(BPoint(rect.left + 1.0f, rect.bottom), BPoint(rect.right, rect.bottom));
StrokeLine(BPoint(rect.right, rect.top + 1.0f));
rect.InsetBy(1.0f, 1.0f);
// Filling
SetHighColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), B_LIGHTEN_MAX_TINT));
FillRect(rect);
if (fCurrent != 0.0f) {
2003-05-14 17:21:46 +00:00
rect.right = rect.left + (float)ceil(fCurrent * (width - 4) / fMax),
// Bevel
SetHighColor(tint_color(fBarColor, B_LIGHTEN_2_TINT));
StrokeLine(BPoint(rect.left, rect.bottom), BPoint(rect.left, rect.top));
StrokeLine(BPoint(rect.right, rect.top));
SetHighColor(tint_color(fBarColor, B_DARKEN_2_TINT));
StrokeLine(BPoint(rect.left, rect.bottom), BPoint(rect.right, rect.bottom));
StrokeLine(BPoint(rect.right, rect.top));
rect.InsetBy(1.0f, 1.0f);
// Filling
SetHighColor(fBarColor);
FillRect(rect);
}
}
void
BStatusBar::SetBarColor(rgb_color color)
2003-05-14 17:21:46 +00:00
{
memcpy(&fBarColor, &color, sizeof(rgb_color));
Invalidate();
}
void
BStatusBar::SetBarHeight(float height)
2003-05-14 17:21:46 +00:00
{
BRect frame = Frame();
fBarHeight = height;
fCustomBarHeight = true;
ResizeTo(frame.Width(), fBarHeight + 16);
}
void
2006-04-05 19:15:54 +00:00
BStatusBar::SetText(const char *string)
{
SetTextData(&fText, string);
2003-05-14 17:21:46 +00:00
Invalidate();
}
void
BStatusBar::SetTrailingText(const char *string)
{
SetTextData(&fTrailingText, string);
2003-05-14 17:21:46 +00:00
Invalidate();
}
void
BStatusBar::SetMaxValue(float max)
2003-05-14 17:21:46 +00:00
{
fMax = max;
Invalidate();
}
void
BStatusBar::Update(float delta, const char *text, const char *trailingText)
2003-05-14 17:21:46 +00:00
{
fCurrent += delta;
if (fCurrent > fMax)
fCurrent = fMax;
2003-06-25 19:40:45 +00:00
// Passing NULL for the text or trailingText argument retains the previous
// text or trailing text string.
if (text)
SetTextData(&fText, text);
2003-05-14 17:21:46 +00:00
if (trailingText)
SetTextData(&fTrailingText, trailingText);
2003-05-14 17:21:46 +00:00
Invalidate();
}
void
BStatusBar::Reset(const char *label, const char *trailingLabel)
2003-05-14 17:21:46 +00:00
{
2003-06-25 19:40:45 +00:00
// Reset replaces the label and trailing label with copies of the
// strings passed as arguments. If either argument is NULL, the
// label or trailing label will be deleted and erased.
SetTextData(&fLabel, label);
SetTextData(&fTrailingLabel, trailingLabel);
2003-06-25 19:40:45 +00:00
// Reset deletes and erases any text or trailing text
SetTextData(&fText, NULL);
SetTextData(&fTrailingText, NULL);
2003-05-14 17:21:46 +00:00
fCurrent = 0.0f;
fMax = 100.0f;
2003-05-14 17:21:46 +00:00
Invalidate();
}
float
BStatusBar::CurrentValue() const
2003-05-14 17:21:46 +00:00
{
return fCurrent;
}
float
BStatusBar::MaxValue() const
2003-05-14 17:21:46 +00:00
{
return fMax;
}
rgb_color
BStatusBar::BarColor() const
2003-05-14 17:21:46 +00:00
{
return fBarColor;
}
float
BStatusBar::BarHeight() const
2003-05-14 17:21:46 +00:00
{
if (!fCustomBarHeight && fBarHeight == -1.0f) {
2003-05-14 17:21:46 +00:00
font_height fh;
GetFontHeight(&fh);
const_cast<BStatusBar *>(this)->fBarHeight = fh.ascent + fh.descent + 6.0f;
2003-05-14 17:21:46 +00:00
}
return fBarHeight;
}
const char *
BStatusBar::Text() const
2003-05-14 17:21:46 +00:00
{
return fText;
}
const char *
BStatusBar::TrailingText() const
2003-05-14 17:21:46 +00:00
{
return fTrailingText;
}
const char *
BStatusBar::Label() const
2003-05-14 17:21:46 +00:00
{
return fLabel;
}
const char *
BStatusBar::TrailingLabel() const
2003-05-14 17:21:46 +00:00
{
return fTrailingLabel;
}
void
BStatusBar::MouseDown(BPoint point)
2003-05-14 17:21:46 +00:00
{
BView::MouseDown(point);
}
void
BStatusBar::MouseUp(BPoint point)
2003-05-14 17:21:46 +00:00
{
BView::MouseUp(point);
}
void
BStatusBar::WindowActivated(bool state)
2003-05-14 17:21:46 +00:00
{
BView::WindowActivated(state);
}
void
BStatusBar::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
2003-05-14 17:21:46 +00:00
{
BView::MouseMoved(point, transit, message);
}
void
BStatusBar::DetachedFromWindow()
2003-05-14 17:21:46 +00:00
{
BView::DetachedFromWindow();
}
void
BStatusBar::FrameMoved(BPoint newPosition)
2003-05-14 17:21:46 +00:00
{
BView::FrameMoved(newPosition);
2003-05-14 17:21:46 +00:00
}
void
BStatusBar::FrameResized(float newWidth, float newHeight)
2003-05-14 17:21:46 +00:00
{
BView::FrameResized(newWidth, newHeight);
2003-05-14 17:21:46 +00:00
}
BHandler *
BStatusBar::ResolveSpecifier(BMessage *message, int32 index,
2003-05-14 17:21:46 +00:00
BMessage *specifier,
int32 what, const char *property)
{
return BView::ResolveSpecifier(message, index, specifier, what, property);
}
void
BStatusBar::ResizeToPreferred()
2003-05-14 17:21:46 +00:00
{
BView::ResizeToPreferred();
}
void
BStatusBar::GetPreferredSize(float* _width, float* _height)
2003-05-14 17:21:46 +00:00
{
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();
}
2003-05-14 17:21:46 +00:00
}
void
BStatusBar::MakeFocus(bool state)
2003-05-14 17:21:46 +00:00
{
BView::MakeFocus(state);
}
void
BStatusBar::AllAttached()
2003-05-14 17:21:46 +00:00
{
BView::AllAttached();
}
void
BStatusBar::AllDetached()
2003-05-14 17:21:46 +00:00
{
BView::AllDetached();
}
status_t
BStatusBar::GetSupportedSuites(BMessage *data)
2003-05-14 17:21:46 +00:00
{
return BView::GetSupportedSuites(data);
}
status_t
BStatusBar::Perform(perform_code d, void *arg)
2003-05-14 17:21:46 +00:00
{
return BView::Perform(d, arg);
2003-05-14 17:21:46 +00:00
}
2003-05-14 17:21:46 +00:00
void BStatusBar::_ReservedStatusBar1() {}
void BStatusBar::_ReservedStatusBar2() {}
void BStatusBar::_ReservedStatusBar3() {}
void BStatusBar::_ReservedStatusBar4() {}
BStatusBar &
BStatusBar::operator=(const BStatusBar &)
2003-05-14 17:21:46 +00:00
{
return *this;
}
void
BStatusBar::InitObject(const char *label, const char *trailingLabel)
2003-05-14 17:21:46 +00:00
{
SetTextData(&fLabel, label);
SetTextData(&fTrailingLabel, trailingLabel);
2003-05-14 17:21:46 +00:00
}
void
BStatusBar::SetTextData(char **dest, const char *source)
2003-05-14 17:21:46 +00:00
{
ASSERT(dest != NULL);
if (*dest != NULL) {
free(*dest);
*dest = NULL;
}
if (source != NULL && source[0] != NULL)
*dest = strdup(source);
2003-05-14 17:21:46 +00:00
}
void
BStatusBar::FillBar(BRect rect)
2003-05-14 17:21:46 +00:00
{
// TODO:
}
void
BStatusBar::Resize()
2003-05-14 17:21:46 +00:00
{
// TODO:
}
void
BStatusBar::_Draw(BRect updateRect, bool barOnly)
2003-05-14 17:21:46 +00:00
{
// TODO:
}