Ticket #6257: layoutArchiving.2.patch

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

Updated patch.

  • 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 */
    56
    6 #include <Layout.h>
    7 
    87#include <new>
    98
     9#include <Message.h>
    1010#include <View.h>
    1111
     12#include <Layout.h>
     13
    1214#include "ViewLayoutItem.h"
    1315
    1416using std::nothrow;
    1517
     18namespace {
     19    const char* kLayoutItemField = "BLayout:_items";
     20    const char* kLayoutDataField = "BLayout:_data";
     21}
    1622
     23
    1724// constructor
    1825BLayout::BLayout()
    19     : fView(NULL),
    20       fItems(20)
     26    :
     27    fView(NULL),
     28    fItems(20)
    2129{
    2230}
    2331
    24 // destructor
     32
     33BLayout::BLayout(BMessage* from)
     34    :
     35    BArchivable(BUnarchiver::PrepareArchive(from)),
     36    fView(NULL),
     37    fItems(20)
     38{
     39    BUnarchiver unarchiver(from);
     40
     41    int32 i = 0;
     42    while (unarchiver.EnsureUnarchived(kLayoutItemField, i++) == B_OK)
     43        ;
     44}
     45
     46
    2547BLayout::~BLayout()
    2648{
    2749    // this deletes all items
    2850    SetView(NULL);
    2951}
    3052
    31 // View
     53
    3254BView*
    3355BLayout::View() const
    3456{
    3557    return fView;
    3658}
    3759
    38 // AddView
     60
    3961BLayoutItem*
    4062BLayout::AddView(BView* child)
    4163{
    4264    return AddView(-1, child);
    4365}
    4466
    45 // AddView
     67
    4668BLayoutItem*
    4769BLayout::AddView(int32 index, BView* child)
    4870{
     
    5476    return NULL;
    5577}
    5678
    57 // AddItem
     79
    5880bool
    5981BLayout::AddItem(BLayoutItem* item)
    6082{
    6183    return AddItem(-1, item);
    6284}
    6385
    64 // AddItem
     86
    6587bool
    6688BLayout::AddItem(int32 index, BLayoutItem* item)
    6789{
    6890    if (!fView || !item || fItems.HasItem(item))
    6991        return false;
    7092
    71     // if the item refers to a BView, we make sure, it is added to the parent
     93    // if the item refers to a BView, we make sure it is added to the parent
    7294    // view
    7395    BView* view = item->View();
    7496    if (view && view->fParent != fView && !fView->_AddChild(view, NULL))
     
    86108    return true;
    87109}
    88110
    89 // RemoveView
     111
    90112bool
    91113BLayout::RemoveView(BView* child)
    92114{
     
    107129    return removed;
    108130}
    109131
    110 // RemoveItem
     132
    111133bool
    112134BLayout::RemoveItem(BLayoutItem* item)
    113135{
     
    115137    return (index >= 0 ? RemoveItem(index) : false);
    116138}
    117139
    118 // RemoveItem
     140
    119141BLayoutItem*
    120142BLayout::RemoveItem(int32 index)
    121143{
     
    137159    return item;
    138160}
    139161
    140 // ItemAt
     162
    141163BLayoutItem*
    142164BLayout::ItemAt(int32 index) const
    143165{
    144166    return (BLayoutItem*)fItems.ItemAt(index);
    145167}
    146168
    147 // CountItems
     169
    148170int32
    149171BLayout::CountItems() const
    150172{
    151173    return fItems.CountItems();
    152174}
    153175
    154 // IndexOfItem
     176
    155177int32
    156 BLayout::IndexOfItem(BLayoutItem* item) const
     178BLayout::IndexOfItem(const BLayoutItem* item) const
    157179{
    158180    return fItems.IndexOf(item);
    159181}
    160182
    161 // IndexOfView
     183
    162184int32
    163185BLayout::IndexOfView(BView* child) const
    164186{
     
    172194    return -1;
    173195}
    174196
    175 // InvalidateLayout
     197
    176198void
    177199BLayout::InvalidateLayout()
    178200{
     
    180202        fView->InvalidateLayout();
    181203}
    182204
    183 // ItemAdded
     205
     206status_t
     207BLayout::Archive(BMessage* into, bool deep) const
     208{
     209    BArchiver archiver(into);
     210    status_t err = BArchivable::Archive(into, deep);
     211
     212    if (err != B_OK)
     213        return err;
     214   
     215    if (deep) {
     216        int32 count = CountItems();
     217        for (int32 i = 0; i < count; i++) {
     218            err = archiver.AddArchivable(kLayoutItemField, ItemAt(i), deep);
     219
     220            if (err == B_OK) {
     221                BMessage data;
     222                err = ArchiveLayoutData(&data, ItemAt(i));
     223                if (err == B_OK)
     224                    err = into->AddMessage(kLayoutDataField, &data);
     225            }
     226
     227            if (err != B_OK)
     228                return err;
     229        }
     230    }
     231
     232    return archiver.Finish();
     233}
     234
     235
     236status_t
     237BLayout::AllUnarchived(const BMessage* from)
     238{
     239    status_t err = BArchivable::AllUnarchived(from);
     240    BUnarchiver unarchiver(from);
     241
     242    if (err != B_OK)
     243        return err;
     244
     245    for (int32 i = 0; ; i++) {
     246        BArchivable* retriever;
     247        err = unarchiver.FindArchivable(kLayoutItemField, i, &retriever);
     248
     249        if (err == B_BAD_INDEX)
     250            break;
     251
     252        if (err == B_OK) {
     253            BMessage layoutData;
     254            err = from->FindMessage(kLayoutDataField, i, &layoutData);
     255
     256            if (err == B_OK) {
     257                BLayoutItem* item = dynamic_cast<BLayoutItem*>(retriever);
     258                err = RestoreItemAndData(&layoutData, item);
     259            }
     260        }
     261
     262        if (err != B_OK)
     263            return err;
     264    }
     265
     266    return B_OK;
     267}
     268
     269
     270status_t
     271BLayout::ArchiveLayoutData(BMessage* into, const BLayoutItem* of) const
     272{
     273    return B_OK;
     274}
     275
     276
     277status_t
     278BLayout::RestoreItemAndData(const BMessage* from, BLayoutItem* item)
     279{
     280    if (item && AddItem(item))
     281        return B_OK;
     282    return B_ERROR;
     283}
     284
     285
    184286void
    185287BLayout::ItemAdded(BLayoutItem* item)
    186288{
    187289}
    188290
    189 // ItemRemoved
     291
    190292void
    191293BLayout::ItemRemoved(BLayoutItem* item)
    192294{
    193295}
    194296
    195 // SetView
     297
    196298void
    197299BLayout::SetView(BView* view)
    198300{
  • 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 */
     
    910#include <LayoutUtils.h>
    1011
    1112
    12 // constructor
    1313BLayoutItem::BLayoutItem()
    14     : fLayout(NULL),
    15       fLayoutData(NULL)
     14    :
     15    fLayout(NULL),
     16    fLayoutData(NULL)
    1617{
    1718}
    1819
    19 // destructor
     20
     21BLayoutItem::BLayoutItem(BMessage* from)
     22    :
     23    BArchivable(BUnarchiver::PrepareArchive(from)),
     24    fLayout(NULL),
     25    fLayoutData(NULL)
     26{
     27    BUnarchiver(from).Finish();
     28}
     29
     30
    2031BLayoutItem::~BLayoutItem()
    2132{
    2233}
    2334
    24 // Layout
     35
    2536BLayout*
    2637BLayoutItem::Layout() const
    2738{
    2839    return fLayout;
    2940}
    3041
    31 // HasHeightForWidth
     42
    3243bool
    3344BLayoutItem::HasHeightForWidth()
    3445{
     
    3647    return false;
    3748}
    3849
    39 // GetHeightForWidth
     50
    4051void
    4152BLayoutItem::GetHeightForWidth(float width, float* min, float* max,
    4253    float* preferred)
     
    4455    // no "height for width" by default
    4556}
    4657
    47 // View
     58
    4859BView*
    4960BLayoutItem::View()
    5061{
    5162    return NULL;
    5263}
    5364
    54 // InvalidateLayout
     65
    5566void
    5667BLayoutItem::InvalidateLayout()
    5768{
     
    5970        fLayout->InvalidateLayout();
    6071}
    6172
    62 // LayoutData
     73
    6374void*
    6475BLayoutItem::LayoutData() const
    6576{
    6677    return fLayoutData;
    6778}
    6879
    69 // SetLayoutData
     80
    7081void
    7182BLayoutItem::SetLayoutData(void* data)
    7283{
    7384    fLayoutData = data;
    7485}
    7586
    76 // AlignInFrame
     87
    7788void
    7889BLayoutItem::AlignInFrame(BRect frame)
    7990{
     
    101112    SetFrame(BLayoutUtils::AlignInFrame(frame, maxSize, alignment));
    102113}
    103114
    104 // SetLayout
     115
     116status_t
     117BLayoutItem::Archive(BMessage* into, bool deep) const
     118{
     119    BArchiver archiver(into);
     120    status_t err = BArchivable::Archive(into, deep);
     121
     122    if (err == B_OK)
     123        err = archiver.Finish();
     124
     125    return err;
     126}
     127
     128
     129status_t
     130BLayoutItem::AllArchived(BMessage* into) const
     131{
     132    BArchiver archiver(into);
     133    return BArchivable::AllArchived(into);
     134}
     135
     136
     137status_t
     138BLayoutItem::AllUnarchived(const BMessage* from)
     139{
     140    return BArchivable::AllUnarchived(from);
     141}
     142
     143
    105144void
    106145BLayoutItem::SetLayout(BLayout* layout)
    107146{
  • 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) const;
     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            AllArchived(BMessage* into) const;
     55    virtual status_t            AllUnarchived(const BMessage* from);
    5056   
     57   
    5158private:
    5259            friend class BLayout;
    5360