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

341 lines
9.6 KiB
C++
Raw Normal View History

2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
2002-07-09 12:24:59 +00:00
//
2003-06-16 07:43:42 +00:00
// 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:
2002-07-09 12:24:59 +00:00
//
2003-06-16 07:43:42 +00:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2002-07-09 12:24:59 +00:00
//
2003-06-16 07:43:42 +00:00
// 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.
2002-07-09 12:24:59 +00:00
//
2003-06-16 07:43:42 +00:00
// File Name: ListItem.cpp
// Author: Ulrich Wimboeck
// Marc Flerackers ([email protected])
// Description: BListView represents a one-dimensional list view.
//------------------------------------------------------------------------------
2002-07-09 12:24:59 +00:00
2003-06-16 07:43:42 +00:00
// Standard Includes -----------------------------------------------------------
2003-06-16 07:55:04 +00:00
#include <stdlib.h>
#include <string.h>
2003-06-16 07:43:42 +00:00
// System Includes -------------------------------------------------------------
#include <ListItem.h>
2002-07-09 12:24:59 +00:00
#include <View.h>
2003-06-16 07:43:42 +00:00
#include <Message.h>
#include <Errors.h>
2002-07-09 12:24:59 +00:00
2003-06-16 07:43:42 +00:00
// Project Includes ------------------------------------------------------------
2002-07-09 12:24:59 +00:00
2003-06-16 07:43:42 +00:00
// Local Includes --------------------------------------------------------------
// Local Defines ---------------------------------------------------------------
2002-07-09 12:24:59 +00:00
2003-06-16 07:43:42 +00:00
// Globals ---------------------------------------------------------------------
//------------------------------------------------------------------------------
BListItem::BListItem(uint32 level, bool expanded)
: fWidth(0),
fHeight(0),
fLevel(level),
fSelected(false),
fEnabled(true),
fExpanded(expanded),
fHasSubitems(false),
fVisible(true)
2002-07-09 12:24:59 +00:00
{
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
BListItem::BListItem(BMessage *data)
: BArchivable(data),
fWidth(0),
fHeight(0),
fLevel(0),
fSelected(false),
fEnabled(true),
fExpanded(false),
fHasSubitems(false),
fVisible(true)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:43:42 +00:00
data->FindBool("_sel", &fSelected);
if (data->FindBool("_disable", &fEnabled) != B_OK)
fEnabled = true;
else
fEnabled = false;
data->FindBool("_li_expanded", &fExpanded);
data->FindInt32("_li_outline_level", (int32*)&fLevel);
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
2002-07-09 12:24:59 +00:00
BListItem::~BListItem()
{
2003-06-16 07:43:42 +00:00
}
//------------------------------------------------------------------------------
status_t BListItem::Archive(BMessage *archive, bool deep) const
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:43:42 +00:00
BArchivable::Archive(archive, deep);
2002-07-09 12:24:59 +00:00
2003-06-16 07:43:42 +00:00
if (fSelected)
archive->AddBool("_sel", true);
if (!fEnabled)
archive->AddBool("_disable", true);
2002-07-09 12:24:59 +00:00
2003-06-16 07:43:42 +00:00
if (fExpanded)
archive->AddBool("_li_expanded", true);
2002-07-09 12:24:59 +00:00
2003-06-16 07:43:42 +00:00
if (fLevel != 0)
archive->AddInt32("_li_outline_level", fLevel);
2002-07-09 12:24:59 +00:00
2003-06-16 07:43:42 +00:00
return B_OK;
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
float BListItem::Height() const
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:43:42 +00:00
return fHeight;
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
float BListItem::Width() const
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:43:42 +00:00
return fWidth;
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
bool BListItem::IsSelected() const
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:43:42 +00:00
return fSelected;
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
void BListItem::Select()
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:43:42 +00:00
fSelected = true;
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
void BListItem::Deselect()
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:43:42 +00:00
fSelected = false;
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
void BListItem::SetEnabled(bool on)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:43:42 +00:00
fEnabled = on;
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
bool BListItem::IsEnabled() const
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:43:42 +00:00
return fEnabled;
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
2002-07-09 12:24:59 +00:00
void BListItem::SetHeight(float height)
{
2003-06-16 07:43:42 +00:00
fHeight = height;
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
void BListItem::SetWidth(float width)
{
fWidth = width;
}
//------------------------------------------------------------------------------
void BListItem::Update(BView *owner, const BFont *font)
{
font_height fh;
font->GetHeight(&fh);
2002-07-09 12:24:59 +00:00
2003-06-16 07:43:42 +00:00
SetWidth(owner->Bounds().Width());
SetHeight(fh.ascent + fh.descent + fh.leading);
}
//------------------------------------------------------------------------------
status_t BListItem::Perform(perform_code d, void *arg)
{
return BArchivable::Perform(d, arg);
}
//------------------------------------------------------------------------------
void BListItem::SetExpanded(bool expanded)
{
fExpanded = expanded;
}
//------------------------------------------------------------------------------
bool BListItem::IsExpanded() const
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:43:42 +00:00
return fExpanded;
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
uint32 BListItem::OutlineLevel() const
{
return fLevel;
}
//------------------------------------------------------------------------------
bool BListItem::HasSubitems() const
{
return fHasSubitems;
}
//------------------------------------------------------------------------------
void BListItem::_ReservedListItem1() {}
void BListItem::_ReservedListItem2() {}
//------------------------------------------------------------------------------
BListItem::BListItem(const BListItem &item)
{
}
//------------------------------------------------------------------------------
BListItem &BListItem::operator=(const BListItem &)
{
return *this;
}
//------------------------------------------------------------------------------
bool BListItem::IsItemVisible() const
{
return fVisible;
}
//------------------------------------------------------------------------------
void BListItem::SetItemVisible(bool visible)
{
fVisible = visible;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BStringItem::BStringItem(const char *text, uint32 level, bool expanded)
: BListItem(level, expanded),
fText(NULL),
fBaselineOffset(0)
{
SetText(text);
}
//------------------------------------------------------------------------------
BStringItem::BStringItem(BMessage *archive)
: BListItem(archive),
fText(NULL),
fBaselineOffset(0)
{
const char *string;
2002-07-09 12:24:59 +00:00
2003-06-16 07:43:42 +00:00
if (archive->FindString("_label", &string) == B_OK)
SetText(string);
}
//------------------------------------------------------------------------------
BStringItem::~BStringItem()
{
if (fText)
free(fText);
}
//------------------------------------------------------------------------------
BArchivable *BStringItem::Instantiate(BMessage *archive)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:43:42 +00:00
if (validate_instantiation(archive, "BStringItem"))
return new BStringItem(archive);
else
return NULL;
}
//------------------------------------------------------------------------------
status_t BStringItem::Archive(BMessage *archive, bool deep) const
{
BListItem::Archive(archive);
if (fText)
archive->AddString("_label", fText);
2002-07-09 12:24:59 +00:00
2003-06-16 07:43:42 +00:00
return B_OK;
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
void BStringItem::DrawItem(BView *owner, BRect frame, bool complete)
{
if (fText == NULL)
return;
rgb_color highColor = owner->HighColor();
rgb_color lowColor = owner->LowColor();
2002-07-09 12:24:59 +00:00
2003-06-16 07:43:42 +00:00
if (IsSelected() || complete)
{
if (IsSelected())
owner->SetHighColor(tint_color(lowColor, B_DARKEN_2_TINT));
else
owner->SetHighColor(lowColor);
owner->FillRect(frame);
}
owner->MovePenTo(frame.left, frame.top + fBaselineOffset);
rgb_color black = {0, 0, 0, 255};
if (!IsEnabled())
owner->SetHighColor(tint_color(black, B_LIGHTEN_2_TINT));
else
owner->SetHighColor(black);
owner->DrawString(fText);
owner->SetHighColor(highColor);
owner->SetLowColor(lowColor);
}
//------------------------------------------------------------------------------
void BStringItem::SetText(const char *text)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:43:42 +00:00
if (fText)
{
free(fText);
fText = NULL;
}
if (text)
fText = strdup(text);
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
const char *BStringItem::Text() const
{
return fText;
}
//------------------------------------------------------------------------------
void BStringItem::Update(BView *owner, const BFont *font)
{
if (fText)
SetWidth(owner->StringWidth(fText));
2002-07-09 12:24:59 +00:00
2003-06-16 07:43:42 +00:00
font_height fheight;
font->GetHeight(&fheight);
fBaselineOffset = fheight.ascent + fheight.leading;
SetHeight((float)ceil(fheight.ascent + fheight.descent +
fheight.leading) + 4);
}
//------------------------------------------------------------------------------
status_t BStringItem::Perform(perform_code d, void *arg)
{
return B_ERROR;
}
//------------------------------------------------------------------------------
void BStringItem::_ReservedStringItem1() {}
void BStringItem::_ReservedStringItem2() {}
//------------------------------------------------------------------------------
BStringItem::BStringItem(const BStringItem &)
{
}
//------------------------------------------------------------------------------
BStringItem &BStringItem::operator=(const BStringItem &)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:43:42 +00:00
return *this;
2002-07-09 12:24:59 +00:00
}
2003-06-16 07:43:42 +00:00
//------------------------------------------------------------------------------
/*
* $Log $
*
* $Id $
*
*/