From b71f118da99ab4b762e3875b0d434a8eca4f4a19 Mon Sep 17 00:00:00 2001 From: Stefano Ceccherini Date: Thu, 9 Jun 2005 12:05:04 +0000 Subject: [PATCH] Improved error checking, style changes git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13028 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/interface/ListItem.cpp | 264 ++++++++++++++++++-------------- 1 file changed, 149 insertions(+), 115 deletions(-) diff --git a/src/kits/interface/ListItem.cpp b/src/kits/interface/ListItem.cpp index 1fb4560ad6..eaebd0e355 100644 --- a/src/kits/interface/ListItem.cpp +++ b/src/kits/interface/ListItem.cpp @@ -1,5 +1,5 @@ //------------------------------------------------------------------------------ -// Copyright (c) 2001-2002, OpenBeOS +// Copyright (c) 2001-2005, Haiku, Inc. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), @@ -22,28 +22,17 @@ // File Name: ListItem.cpp // Author: Ulrich Wimboeck // Marc Flerackers (mflerackers@androme.be) -// Description: BListView represents a one-dimensional list view. +// Description: BListItem is the base class for BListView's items, +// BStringItem is a subclass of BListItem which draws a string. //------------------------------------------------------------------------------ - -// Standard Includes ----------------------------------------------------------- #include #include -// System Includes ------------------------------------------------------------- #include -#include #include -#include +#include -// Project Includes ------------------------------------------------------------ -// Local Includes -------------------------------------------------------------- - -// Local Defines --------------------------------------------------------------- - -// Globals --------------------------------------------------------------------- - -//------------------------------------------------------------------------------ BListItem::BListItem(uint32 level, bool expanded) : fWidth(0), fHeight(0), @@ -55,7 +44,8 @@ BListItem::BListItem(uint32 level, bool expanded) fVisible(true) { } -//------------------------------------------------------------------------------ + + BListItem::BListItem(BMessage *data) : BArchivable(data), fWidth(0), @@ -77,77 +67,99 @@ BListItem::BListItem(BMessage *data) data->FindBool("_li_expanded", &fExpanded); data->FindInt32("_li_outline_level", (int32*)&fLevel); } -//------------------------------------------------------------------------------ + + BListItem::~BListItem() { - } -//------------------------------------------------------------------------------ -status_t BListItem::Archive(BMessage *archive, bool deep) const + + +status_t +BListItem::Archive(BMessage *archive, bool deep) const { - BArchivable::Archive(archive, deep); + status_t status = BArchivable::Archive(archive, deep); - if (fSelected) - archive->AddBool("_sel", true); + if (status == B_OK && fSelected) + status = archive->AddBool("_sel", true); - if (!fEnabled) - archive->AddBool("_disable", true); + if (status == B_OK && !fEnabled) + status = archive->AddBool("_disable", true); - if (fExpanded) - archive->AddBool("_li_expanded", true); + if (status == B_OK && fExpanded) + status = archive->AddBool("_li_expanded", true); - if (fLevel != 0) - archive->AddInt32("_li_outline_level", fLevel); + if (status == B_OK && fLevel != 0) + status = archive->AddInt32("_li_outline_level", fLevel); - return B_OK; + return status; } -//------------------------------------------------------------------------------ -float BListItem::Height() const + + +float +BListItem::Height() const { return fHeight; } -//------------------------------------------------------------------------------ -float BListItem::Width() const + + +float +BListItem::Width() const { return fWidth; } -//------------------------------------------------------------------------------ -bool BListItem::IsSelected() const + + +bool +BListItem::IsSelected() const { return fSelected; } -//------------------------------------------------------------------------------ -void BListItem::Select() + + +void +BListItem::Select() { fSelected = true; } -//------------------------------------------------------------------------------ -void BListItem::Deselect() + + +void +BListItem::Deselect() { fSelected = false; } -//------------------------------------------------------------------------------ -void BListItem::SetEnabled(bool on) + + +void +BListItem::SetEnabled(bool on) { fEnabled = on; } -//------------------------------------------------------------------------------ -bool BListItem::IsEnabled() const + + +bool +BListItem::IsEnabled() const { return fEnabled; } -//------------------------------------------------------------------------------ -void BListItem::SetHeight(float height) + + +void +BListItem::SetHeight(float height) { fHeight = height; } -//------------------------------------------------------------------------------ -void BListItem::SetWidth(float width) + + +void +BListItem::SetWidth(float width) { fWidth = width; } -//------------------------------------------------------------------------------ -void BListItem::Update(BView *owner, const BFont *font) + + +void +BListItem::Update(BView *owner, const BFont *font) { font_height fh; font->GetHeight(&fh); @@ -155,58 +167,74 @@ void BListItem::Update(BView *owner, const BFont *font) SetWidth(owner->Bounds().Width()); SetHeight(fh.ascent + fh.descent + fh.leading); } -//------------------------------------------------------------------------------ -status_t BListItem::Perform(perform_code d, void *arg) + + +status_t +BListItem::Perform(perform_code d, void *arg) { return BArchivable::Perform(d, arg); } -//------------------------------------------------------------------------------ -void BListItem::SetExpanded(bool expanded) + + +void +BListItem::SetExpanded(bool expanded) { fExpanded = expanded; } -//------------------------------------------------------------------------------ -bool BListItem::IsExpanded() const + + +bool +BListItem::IsExpanded() const { return fExpanded; } -//------------------------------------------------------------------------------ -uint32 BListItem::OutlineLevel() const + + +uint32 +BListItem::OutlineLevel() const { return fLevel; } -//------------------------------------------------------------------------------ -bool BListItem::HasSubitems() const + + +bool +BListItem::HasSubitems() const { return fHasSubitems; } -//------------------------------------------------------------------------------ + + void BListItem::_ReservedListItem1() {} void BListItem::_ReservedListItem2() {} -//------------------------------------------------------------------------------ + + BListItem::BListItem(const BListItem &item) { } -//------------------------------------------------------------------------------ -BListItem &BListItem::operator=(const BListItem &) + + +BListItem & +BListItem::operator=(const BListItem &) { return *this; } -//------------------------------------------------------------------------------ -bool BListItem::IsItemVisible() const + + +bool +BListItem::IsItemVisible() const { return fVisible; } -//------------------------------------------------------------------------------ -void BListItem::SetItemVisible(bool visible) + + +void +BListItem::SetItemVisible(bool visible) { fVisible = visible; } -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ +// BStringItem BStringItem::BStringItem(const char *text, uint32 level, bool expanded) : BListItem(level, expanded), fText(NULL), @@ -214,7 +242,8 @@ BStringItem::BStringItem(const char *text, uint32 level, bool expanded) { SetText(text); } -//------------------------------------------------------------------------------ + + BStringItem::BStringItem(BMessage *archive) : BListItem(archive), fText(NULL), @@ -225,32 +254,38 @@ BStringItem::BStringItem(BMessage *archive) if (archive->FindString("_label", &string) == B_OK) SetText(string); } -//------------------------------------------------------------------------------ + + BStringItem::~BStringItem() { - if (fText) - free(fText); + free(fText); } -//------------------------------------------------------------------------------ -BArchivable *BStringItem::Instantiate(BMessage *archive) + + +BArchivable * +BStringItem::Instantiate(BMessage *archive) { if (validate_instantiation(archive, "BStringItem")) return new BStringItem(archive); else return NULL; } -//------------------------------------------------------------------------------ -status_t BStringItem::Archive(BMessage *archive, bool deep) const + + +status_t +BStringItem::Archive(BMessage *archive, bool deep) const { - BListItem::Archive(archive); + status_t status = BListItem::Archive(archive); - if (fText) - archive->AddString("_label", fText); + if (status == B_OK && fText != NULL) + status = archive->AddString("_label", fText); - return B_OK; + return status; } -//------------------------------------------------------------------------------ -void BStringItem::DrawItem(BView *owner, BRect frame, bool complete) + + +void +BStringItem::DrawItem(BView *owner, BRect frame, bool complete) { if (fText == NULL) return; @@ -258,8 +293,7 @@ void BStringItem::DrawItem(BView *owner, BRect frame, bool complete) rgb_color highColor = owner->HighColor(); rgb_color lowColor = owner->LowColor(); - if (IsSelected() || complete) - { + if (IsSelected() || complete) { if (IsSelected()) owner->SetHighColor(tint_color(lowColor, B_DARKEN_2_TINT)); else @@ -282,59 +316,59 @@ void BStringItem::DrawItem(BView *owner, BRect frame, bool complete) owner->SetHighColor(highColor); owner->SetLowColor(lowColor); } -//------------------------------------------------------------------------------ -void BStringItem::SetText(const char *text) -{ - if (fText) - { - free(fText); - fText = NULL; - } + +void +BStringItem::SetText(const char *text) +{ + free(fText); + fText = NULL; + if (text) fText = strdup(text); } -//------------------------------------------------------------------------------ -const char *BStringItem::Text() const + + +const char * +BStringItem::Text() const { return fText; } -//------------------------------------------------------------------------------ -void BStringItem::Update(BView *owner, const BFont *font) + + +void +BStringItem::Update(BView *owner, const BFont *font) { if (fText) SetWidth(owner->StringWidth(fText)); 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) + + +status_t +BStringItem::Perform(perform_code d, void *arg) { - return B_ERROR; + return BListItem::Perform(d, arg); } -//------------------------------------------------------------------------------ + + void BStringItem::_ReservedStringItem1() {} void BStringItem::_ReservedStringItem2() {} -//------------------------------------------------------------------------------ + + BStringItem::BStringItem(const BStringItem &) { } -//------------------------------------------------------------------------------ -BStringItem &BStringItem::operator=(const BStringItem &) + + +BStringItem & +BStringItem::operator=(const BStringItem &) { return *this; } -//------------------------------------------------------------------------------ - -/* - * $Log $ - * - * $Id $ - * - */