From 3fb74eb7962d3e877277dc1a7b0289e3eefdf869 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sat, 17 Jul 2010 00:41:56 +0000 Subject: [PATCH] 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 --- headers/os/interface/GroupLayout.h | 14 ++- src/kits/interface/GroupLayout.cpp | 135 +++++++++++++++++++++++------ 2 files changed, 119 insertions(+), 30 deletions(-) diff --git a/headers/os/interface/GroupLayout.h b/headers/os/interface/GroupLayout.h index 890f23578a..c89712f14e 100644 --- a/headers/os/interface/GroupLayout.h +++ b/headers/os/interface/GroupLayout.h @@ -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. */ #ifndef _GROUP_LAYOUT_H @@ -11,6 +11,7 @@ class BGroupLayout : public BTwoDimensionalLayout { public: BGroupLayout(enum orientation orientation, float spacing = 0.0f); + BGroupLayout(BMessage* from); virtual ~BGroupLayout(); float Spacing() const; @@ -34,6 +35,15 @@ public: virtual bool AddItem(int32 index, BLayoutItem* item, 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: virtual void ItemAdded(BLayoutItem* item); virtual void ItemRemoved(BLayoutItem* item); @@ -46,7 +56,7 @@ protected: enum orientation orientation, int32 index, ColumnRowConstraints* constraints); - virtual void GetItemDimensions(BLayoutItem* item, + virtual void GetItemDimensions(BLayoutItem* item, Dimensions* dimensions); private: diff --git a/src/kits/interface/GroupLayout.cpp b/src/kits/interface/GroupLayout.cpp index 603f2dc9f2..81d0f91612 100644 --- a/src/kits/interface/GroupLayout.cpp +++ b/src/kits/interface/GroupLayout.cpp @@ -1,14 +1,24 @@ /* + * Copyright 2010, Haiku, Inc. * Copyright 2006, Ingo Weinhold . * All rights reserved. Distributed under the terms of the MIT License. */ + #include #include +#include + +#include + + +namespace { + const char* kWeightField = "BGroupLayoutData:weight"; + const char* kVerticalField = "BGroupLayout:vertical"; +} -// ItemLayoutData struct BGroupLayout::ItemLayoutData { float weight; @@ -18,27 +28,39 @@ struct BGroupLayout::ItemLayoutData { } }; -// constructor + BGroupLayout::BGroupLayout(enum orientation orientation, float spacing) - : BTwoDimensionalLayout(), - fOrientation(orientation) + : + BTwoDimensionalLayout(), + fOrientation(orientation) { 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() { } -// Spacing + float BGroupLayout::Spacing() const { return fHSpacing; } -// SetSpacing + void BGroupLayout::SetSpacing(float spacing) { @@ -49,14 +71,14 @@ BGroupLayout::SetSpacing(float spacing) } } -// Orientation + orientation BGroupLayout::Orientation() const { return fOrientation; } -// SetOrientation + void BGroupLayout::SetOrientation(enum orientation orientation) { @@ -67,7 +89,7 @@ BGroupLayout::SetOrientation(enum orientation orientation) } } -// ItemWeight + float BGroupLayout::ItemWeight(int32 index) const { @@ -78,7 +100,7 @@ BGroupLayout::ItemWeight(int32 index) const return (data ? data->weight : 0); } -// SetItemWeight + void BGroupLayout::SetItemWeight(int32 index, float weight) { @@ -91,28 +113,28 @@ BGroupLayout::SetItemWeight(int32 index, float weight) InvalidateLayout(); } -// AddView + BLayoutItem* BGroupLayout::AddView(BView* child) { return BTwoDimensionalLayout::AddView(child); } -// AddView + BLayoutItem* BGroupLayout::AddView(int32 index, BView* child) { return BTwoDimensionalLayout::AddView(index, child); } -// AddView + BLayoutItem* BGroupLayout::AddView(BView* child, float weight) { return AddView(-1, child, weight); } -// AddView + BLayoutItem* BGroupLayout::AddView(int32 index, BView* child, float weight) { @@ -123,28 +145,28 @@ BGroupLayout::AddView(int32 index, BView* child, float weight) return item; } -// AddItem + bool BGroupLayout::AddItem(BLayoutItem* item) { return BTwoDimensionalLayout::AddItem(item); } -// AddItem + bool BGroupLayout::AddItem(int32 index, BLayoutItem* item) { return BTwoDimensionalLayout::AddItem(index, item); } -// AddItem + bool BGroupLayout::AddItem(BLayoutItem* item, float weight) { return AddItem(-1, item, weight); } -// AddItem + bool BGroupLayout::AddItem(int32 index, BLayoutItem* item, float weight) { @@ -157,14 +179,71 @@ BGroupLayout::AddItem(int32 index, BLayoutItem* item, float weight) 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 BGroupLayout::ItemAdded(BLayoutItem* item) { - item->SetLayoutData(new ItemLayoutData); + item->SetLayoutData(new(std::nothrow) ItemLayoutData); } -// ItemRemoved + void BGroupLayout::ItemRemoved(BLayoutItem* item) { @@ -174,7 +253,7 @@ BGroupLayout::ItemRemoved(BLayoutItem* item) } } -// PrepareItems + void BGroupLayout::PrepareItems(enum orientation orientation) { @@ -188,21 +267,21 @@ BGroupLayout::PrepareItems(enum orientation orientation) } } -// InternalCountColumns + int32 BGroupLayout::InternalCountColumns() { return (fOrientation == B_HORIZONTAL ? fVisibleItems.CountItems() : 1); } -// InternalCountRows + int32 BGroupLayout::InternalCountRows() { return (fOrientation == B_VERTICAL ? fVisibleItems.CountItems() : 1); } -// GetColumnRowConstraints + void BGroupLayout::GetColumnRowConstraints(enum orientation orientation, int32 index, ColumnRowConstraints* constraints) @@ -218,7 +297,7 @@ BGroupLayout::GetColumnRowConstraints(enum orientation orientation, int32 index, } } -// ItemDimensions + void BGroupLayout::GetItemDimensions(BLayoutItem* item, Dimensions* dimensions) { @@ -239,7 +318,7 @@ BGroupLayout::GetItemDimensions(BLayoutItem* item, Dimensions* dimensions) } } -// _LayoutDataForItem + BGroupLayout::ItemLayoutData* BGroupLayout::_LayoutDataForItem(BLayoutItem* item) const {