Patch by Alex Wilson (with minor changes by myself):

* Added support for archiving/unarchiving.
* Coding style cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37545 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2010-07-17 00:41:56 +00:00
parent a3d9e70188
commit 3fb74eb796
2 changed files with 119 additions and 30 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2006, Haiku, Inc. All rights reserved. * Copyright 2006-2010, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef _GROUP_LAYOUT_H #ifndef _GROUP_LAYOUT_H
@ -11,6 +11,7 @@ class BGroupLayout : public BTwoDimensionalLayout {
public: public:
BGroupLayout(enum orientation orientation, BGroupLayout(enum orientation orientation,
float spacing = 0.0f); float spacing = 0.0f);
BGroupLayout(BMessage* from);
virtual ~BGroupLayout(); virtual ~BGroupLayout();
float Spacing() const; float Spacing() const;
@ -34,6 +35,15 @@ public:
virtual bool AddItem(int32 index, BLayoutItem* item, virtual bool AddItem(int32 index, BLayoutItem* item,
float weight); float weight);
virtual status_t Archive(BMessage* into, bool deep = true) const;
virtual status_t AllUnarchived(const BMessage* from);
static BArchivable* Instantiate(BMessage* from);
virtual status_t ItemArchived(BMessage* into, BLayoutItem* item,
int32 index) const;
virtual status_t ItemUnarchived(const BMessage* from,
BLayoutItem* item, int32 index);
protected: protected:
virtual void ItemAdded(BLayoutItem* item); virtual void ItemAdded(BLayoutItem* item);
virtual void ItemRemoved(BLayoutItem* item); virtual void ItemRemoved(BLayoutItem* item);
@ -46,7 +56,7 @@ protected:
enum orientation orientation, enum orientation orientation,
int32 index, int32 index,
ColumnRowConstraints* constraints); ColumnRowConstraints* constraints);
virtual void GetItemDimensions(BLayoutItem* item, virtual void GetItemDimensions(BLayoutItem* item,
Dimensions* dimensions); Dimensions* dimensions);
private: private:

View File

@ -1,14 +1,24 @@
/* /*
* Copyright 2010, Haiku, Inc.
* Copyright 2006, Ingo Weinhold <bonefish@cs.tu-berlin.de>. * Copyright 2006, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
* All rights reserved. Distributed under the terms of the MIT License. * All rights reserved. Distributed under the terms of the MIT License.
*/ */
#include <GroupLayout.h> #include <GroupLayout.h>
#include <LayoutItem.h> #include <LayoutItem.h>
#include <Message.h>
#include <new>
namespace {
const char* kWeightField = "BGroupLayoutData:weight";
const char* kVerticalField = "BGroupLayout:vertical";
}
// ItemLayoutData
struct BGroupLayout::ItemLayoutData { struct BGroupLayout::ItemLayoutData {
float weight; float weight;
@ -18,27 +28,39 @@ struct BGroupLayout::ItemLayoutData {
} }
}; };
// constructor
BGroupLayout::BGroupLayout(enum orientation orientation, float spacing) BGroupLayout::BGroupLayout(enum orientation orientation, float spacing)
: BTwoDimensionalLayout(), :
fOrientation(orientation) BTwoDimensionalLayout(),
fOrientation(orientation)
{ {
SetSpacing(spacing); SetSpacing(spacing);
} }
// destructor
BGroupLayout::BGroupLayout(BMessage* from)
:
BTwoDimensionalLayout(from)
{
bool isVertical;
if (from->FindBool(kVerticalField, &isVertical) != B_OK)
isVertical = false;
fOrientation = isVertical ? B_VERTICAL : B_HORIZONTAL;
}
BGroupLayout::~BGroupLayout() BGroupLayout::~BGroupLayout()
{ {
} }
// Spacing
float float
BGroupLayout::Spacing() const BGroupLayout::Spacing() const
{ {
return fHSpacing; return fHSpacing;
} }
// SetSpacing
void void
BGroupLayout::SetSpacing(float spacing) BGroupLayout::SetSpacing(float spacing)
{ {
@ -49,14 +71,14 @@ BGroupLayout::SetSpacing(float spacing)
} }
} }
// Orientation
orientation orientation
BGroupLayout::Orientation() const BGroupLayout::Orientation() const
{ {
return fOrientation; return fOrientation;
} }
// SetOrientation
void void
BGroupLayout::SetOrientation(enum orientation orientation) BGroupLayout::SetOrientation(enum orientation orientation)
{ {
@ -67,7 +89,7 @@ BGroupLayout::SetOrientation(enum orientation orientation)
} }
} }
// ItemWeight
float float
BGroupLayout::ItemWeight(int32 index) const BGroupLayout::ItemWeight(int32 index) const
{ {
@ -78,7 +100,7 @@ BGroupLayout::ItemWeight(int32 index) const
return (data ? data->weight : 0); return (data ? data->weight : 0);
} }
// SetItemWeight
void void
BGroupLayout::SetItemWeight(int32 index, float weight) BGroupLayout::SetItemWeight(int32 index, float weight)
{ {
@ -91,28 +113,28 @@ BGroupLayout::SetItemWeight(int32 index, float weight)
InvalidateLayout(); InvalidateLayout();
} }
// AddView
BLayoutItem* BLayoutItem*
BGroupLayout::AddView(BView* child) BGroupLayout::AddView(BView* child)
{ {
return BTwoDimensionalLayout::AddView(child); return BTwoDimensionalLayout::AddView(child);
} }
// AddView
BLayoutItem* BLayoutItem*
BGroupLayout::AddView(int32 index, BView* child) BGroupLayout::AddView(int32 index, BView* child)
{ {
return BTwoDimensionalLayout::AddView(index, child); return BTwoDimensionalLayout::AddView(index, child);
} }
// AddView
BLayoutItem* BLayoutItem*
BGroupLayout::AddView(BView* child, float weight) BGroupLayout::AddView(BView* child, float weight)
{ {
return AddView(-1, child, weight); return AddView(-1, child, weight);
} }
// AddView
BLayoutItem* BLayoutItem*
BGroupLayout::AddView(int32 index, BView* child, float weight) BGroupLayout::AddView(int32 index, BView* child, float weight)
{ {
@ -123,28 +145,28 @@ BGroupLayout::AddView(int32 index, BView* child, float weight)
return item; return item;
} }
// AddItem
bool bool
BGroupLayout::AddItem(BLayoutItem* item) BGroupLayout::AddItem(BLayoutItem* item)
{ {
return BTwoDimensionalLayout::AddItem(item); return BTwoDimensionalLayout::AddItem(item);
} }
// AddItem
bool bool
BGroupLayout::AddItem(int32 index, BLayoutItem* item) BGroupLayout::AddItem(int32 index, BLayoutItem* item)
{ {
return BTwoDimensionalLayout::AddItem(index, item); return BTwoDimensionalLayout::AddItem(index, item);
} }
// AddItem
bool bool
BGroupLayout::AddItem(BLayoutItem* item, float weight) BGroupLayout::AddItem(BLayoutItem* item, float weight)
{ {
return AddItem(-1, item, weight); return AddItem(-1, item, weight);
} }
// AddItem
bool bool
BGroupLayout::AddItem(int32 index, BLayoutItem* item, float weight) BGroupLayout::AddItem(int32 index, BLayoutItem* item, float weight)
{ {
@ -157,14 +179,71 @@ BGroupLayout::AddItem(int32 index, BLayoutItem* item, float weight)
return success; return success;
} }
// ItemAdded
status_t
BGroupLayout::Archive(BMessage* into, bool deep) const
{
BArchiver archiver(into);
status_t err = BTwoDimensionalLayout::Archive(into, deep);
if (err == B_OK)
err = into->AddBool(kVerticalField, fOrientation == B_VERTICAL);
return archiver.Finish(err);
}
status_t
BGroupLayout::AllUnarchived(const BMessage* from)
{
return BTwoDimensionalLayout::AllUnarchived(from);
}
BArchivable*
BGroupLayout::Instantiate(BMessage* from)
{
if (validate_instantiation(from, "BGroupLayout"))
return new(std::nothrow) BGroupLayout(from);
return NULL;
}
status_t
BGroupLayout::ItemArchived(BMessage* into,
BLayoutItem* item, int32 index) const
{
BGroupLayout::ItemLayoutData* data =
(BGroupLayout::ItemLayoutData*)item->LayoutData();
if (!data) // TODO: remove this once ItemAdded() returns a bool
return B_BAD_VALUE;
return into->AddFloat(kWeightField, data->weight);
}
status_t
BGroupLayout::ItemUnarchived(const BMessage* from,
BLayoutItem* item, int32 index)
{
float weight;
status_t err = from->FindFloat(kWeightField, index, &weight);
if (err == B_OK)
_LayoutDataForItem(item)->weight = weight;
return err;
}
void void
BGroupLayout::ItemAdded(BLayoutItem* item) BGroupLayout::ItemAdded(BLayoutItem* item)
{ {
item->SetLayoutData(new ItemLayoutData); item->SetLayoutData(new(std::nothrow) ItemLayoutData);
} }
// ItemRemoved
void void
BGroupLayout::ItemRemoved(BLayoutItem* item) BGroupLayout::ItemRemoved(BLayoutItem* item)
{ {
@ -174,7 +253,7 @@ BGroupLayout::ItemRemoved(BLayoutItem* item)
} }
} }
// PrepareItems
void void
BGroupLayout::PrepareItems(enum orientation orientation) BGroupLayout::PrepareItems(enum orientation orientation)
{ {
@ -188,21 +267,21 @@ BGroupLayout::PrepareItems(enum orientation orientation)
} }
} }
// InternalCountColumns
int32 int32
BGroupLayout::InternalCountColumns() BGroupLayout::InternalCountColumns()
{ {
return (fOrientation == B_HORIZONTAL ? fVisibleItems.CountItems() : 1); return (fOrientation == B_HORIZONTAL ? fVisibleItems.CountItems() : 1);
} }
// InternalCountRows
int32 int32
BGroupLayout::InternalCountRows() BGroupLayout::InternalCountRows()
{ {
return (fOrientation == B_VERTICAL ? fVisibleItems.CountItems() : 1); return (fOrientation == B_VERTICAL ? fVisibleItems.CountItems() : 1);
} }
// GetColumnRowConstraints
void void
BGroupLayout::GetColumnRowConstraints(enum orientation orientation, int32 index, BGroupLayout::GetColumnRowConstraints(enum orientation orientation, int32 index,
ColumnRowConstraints* constraints) ColumnRowConstraints* constraints)
@ -218,7 +297,7 @@ BGroupLayout::GetColumnRowConstraints(enum orientation orientation, int32 index,
} }
} }
// ItemDimensions
void void
BGroupLayout::GetItemDimensions(BLayoutItem* item, Dimensions* dimensions) BGroupLayout::GetItemDimensions(BLayoutItem* item, Dimensions* dimensions)
{ {
@ -239,7 +318,7 @@ BGroupLayout::GetItemDimensions(BLayoutItem* item, Dimensions* dimensions)
} }
} }
// _LayoutDataForItem
BGroupLayout::ItemLayoutData* BGroupLayout::ItemLayoutData*
BGroupLayout::_LayoutDataForItem(BLayoutItem* item) const BGroupLayout::_LayoutDataForItem(BLayoutItem* item) const
{ {