Ticket #6257: layoutArchiving.patch

File layoutArchiving.patch, 6.4 KB (added by yourpalal, 14 years ago)

Patch implementing archiving in these classes, on its own this patch does very little though :P

  • src/kits/interface/Layout.cpp

     
    11/*
     2 * Copyright 2010, Haiku Inc.
    23 * Copyright 2006, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
    34 * All rights reserved. Distributed under the terms of the MIT License.
    45 */
     
    1314
    1415using std::nothrow;
    1516
     17namespace {
     18    const char* kLayoutItemArchiveField = "_items";
     19}
    1620
     21
    1722// constructor
    1823BLayout::BLayout()
    19     : fView(NULL),
    20       fItems(20)
     24    :
     25    fView(NULL),
     26    fItems(20)
    2127{
    2228}
    2329
     30// archive constructor
     31BLayout::BLayout(BMessage* from)
     32    :
     33    BArchivable(BUnarchiver::PrepareArchive(from)),
     34    fView(NULL),
     35    fItems(20)
     36{
     37    BUnarchiver unarchiver(from);
     38
     39    for (int32 i = 0;
     40        unarchiver.EnsureUnarchived(kLayoutItemArchiveField, i) == B_OK;
     41        i++)
     42        ;
     43}
     44
    2445// destructor
    2546BLayout::~BLayout()
    2647{
     
    153174
    154175// IndexOfItem
    155176int32
    156 BLayout::IndexOfItem(BLayoutItem* item) const
     177BLayout::IndexOfItem(const BLayoutItem* item) const
    157178{
    158179    return fItems.IndexOf(item);
    159180}
     
    180201        fView->InvalidateLayout();
    181202}
    182203
     204// Archive
     205status_t
     206BLayout::Archive(BMessage* into, bool deep) const
     207{
     208    BArchiver archiver(BArchiver::PrepareArchive(into));
     209    status_t err = BArchivable::Archive(into, deep);
     210
     211    if (err != B_OK)
     212        return err;
     213   
     214    if (deep) {
     215        int32 count = CountItems();
     216        for (int32 i = 0; i < count; i++) {
     217            err = archiver.AddArchivable(kLayoutItemArchiveField,
     218                ItemAt(i), deep);
     219
     220            if (err != B_OK)
     221                return err;
     222        }
     223    }
     224
     225    return B_OK;
     226}
     227
     228// AllUnarchived
     229status_t
     230BLayout::AllUnarchived(const BMessage* from)
     231{
     232    return BArchivable::AllUnarchived(from);
     233
     234    // BLayoutItems call RestoreItemAndData() in their AllUnarchived().
     235    // and get added there.
     236}
     237
     238// ArchiveLayoutData
     239status_t
     240BLayout::ArchiveLayoutData(BMessage* into, const BLayoutItem* of)
     241{
     242    return B_OK;
     243}
     244
     245// RestoreItemAndData
     246status_t
     247BLayout::RestoreItemAndData(const BMessage* from, BLayoutItem* item)
     248{
     249    return B_OK;
     250}
     251
    183252// ItemAdded
    184253void
    185254BLayout::ItemAdded(BLayoutItem* item)
  • src/kits/interface/LayoutItem.cpp

     
    11/*
     2 * Copyright 2010, Haiku, Inc.
    23 * Copyright 2006, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
    34 * All rights reserved. Distributed under the terms of the MIT License.
    45 */
     
    89#include <Layout.h>
    910#include <LayoutUtils.h>
    1011
     12namespace {
     13    const char* kLayoutArchiveField = "layout";
     14}
    1115
    1216// constructor
    1317BLayoutItem::BLayoutItem()
    14     : fLayout(NULL),
    15       fLayoutData(NULL)
     18    :
     19    fLayout(NULL),
     20    fLayoutData(NULL)
    1621{
    1722}
    1823
     24// archive constructor
     25BLayoutItem::BLayoutItem(BMessage* from)
     26    :
     27    BArchivable(BUnarchiver::PrepareArchive(from)),
     28    fLayout(NULL),
     29    fLayoutData(NULL)
     30{
     31    BUnarchiver(from).EnsureUnarchived(kLayoutArchiveField);
     32}
     33
    1934// destructor
    2035BLayoutItem::~BLayoutItem()
    2136{
     
    101116    SetFrame(BLayoutUtils::AlignInFrame(frame, maxSize, alignment));
    102117}
    103118
     119// Archive
     120status_t
     121BLayoutItem::Archive(BMessage* into, bool deep) const
     122{
     123    BArchiver archiver(BArchiver::PrepareArchive(into));
     124    BArchivable::Archive(into, deep);
     125
     126    archiver.AddOptional(kLayoutArchiveField, fLayout);
     127
     128    status_t err = B_OK;
     129    if (fLayout)
     130        err = fLayout->ArchiveLayoutData(into, this);
     131
     132    return err;
     133}
     134
     135// AllUnarchived
     136status_t
     137BLayoutItem::AllUnarchived(const BMessage* from)
     138{
     139    BUnarchiver unarchiver(from);
     140    BArchivable* retriever;
     141
     142    status_t err = B_OK;
     143
     144    if (unarchiver.FindArchivable(kLayoutArchiveField, &retriever) == B_OK) {
     145        BLayout* layout = dynamic_cast<BLayout*>(retriever);
     146        if (layout)
     147            err = layout->RestoreItemAndData(from, this);
     148    }
     149
     150    return err;
     151}
     152
    104153// SetLayout
    105154void
    106155BLayoutItem::SetLayout(BLayout* layout)
  • headers/os/interface/Layout.h

     
    11/*
    2  * Copyright 2006, Haiku, Inc. All rights reserved.
     2 * Copyright 2006-2010, Haiku, Inc. All rights reserved.
    33 * Distributed under the terms of the MIT License.
    44 */
    55#ifndef _LAYOUT_H
    66#define _LAYOUT_H
    77
    88#include <Alignment.h>
     9#include <Archivable.h>
    910#include <List.h>
    1011#include <Size.h>
    1112
     
    1314class BView;
    1415
    1516
    16 class BLayout {
     17class BLayout: public BArchivable {
    1718public:
    1819                                BLayout();
     20                                BLayout(BMessage* archive);
    1921    virtual                     ~BLayout();
    2022
    2123            BView*              View() const;
     
    3234
    3335            BLayoutItem*        ItemAt(int32 index) const;
    3436            int32               CountItems() const;
    35             int32               IndexOfItem(BLayoutItem* item) const;
     37            int32               IndexOfItem(const BLayoutItem* item) const;
    3638            int32               IndexOfView(BView* child) const;
    3739
    3840    virtual BSize               MinSize() = 0;
     
    4850
    4951    virtual void                LayoutView() = 0;
    5052
     53    virtual status_t            Archive(BMessage* into, bool deep = true) const;
     54    virtual status_t            AllUnarchived(const BMessage* from);
     55    virtual status_t            ArchiveLayoutData(BMessage* into,
     56                                    const BLayoutItem* of);
     57    virtual status_t            RestoreItemAndData(const BMessage* from,
     58                                    BLayoutItem* item);
     59
    5160protected:
     61
    5262// TODO: Since memory allocations can fail, we should return a bool and
    5363// undo the addition, if false.
    5464    virtual void                ItemAdded(BLayoutItem* item);
  • headers/os/interface/LayoutItem.h

     
    11/*
    2  * Copyright 2006, Haiku, Inc. All rights reserved.
     2 * Copyright 2006-2010, Haiku, Inc. All rights reserved.
    33 * Distributed under the terms of the MIT License.
    44 */
    55#ifndef _LAYOUT_ITEM_H
    66#define _LAYOUT_ITEM_H
    77
    88#include <Alignment.h>
     9#include <Archivable.h>
    910#include <Rect.h>
    1011#include <Size.h>
    1112
    1213class BLayout;
    1314class BView;
    1415
    15 class BLayoutItem {
     16class BLayoutItem: public BArchivable {
    1617public:
    1718                                BLayoutItem();
     19                                BLayoutItem(BMessage* from);
    1820    virtual                     ~BLayoutItem();
    1921
    2022            BLayout*            Layout() const;
     
    4749            void                SetLayoutData(void* data);
    4850
    4951            void                AlignInFrame(BRect frame);
     52
     53    virtual status_t            Archive(BMessage* into, bool deep = true) const;
     54    virtual status_t            AllUnarchived(const BMessage* from);
    5055   
     56   
    5157private:
    5258            friend class BLayout;
    5359