Ticket #5525: 2dlayout.patch

File 2dlayout.patch, 8.0 KB (added by yourpalal, 11 years ago)

implement archiving of BTwoDimensionalLayout class

  • headers/os/interface/TwoDimensionalLayout.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 _TWO_DIMENSIONAL_LAYOUT_H
     
    1414class BTwoDimensionalLayout : public BLayout {
    1515public:
    1616                                BTwoDimensionalLayout();
     17                                BTwoDimensionalLayout(BMessage* from);
    1718    virtual                     ~BTwoDimensionalLayout();
    1819
    1920            void                SetInsets(float left, float top, float right,
     
    3738
    3839    virtual void                LayoutView();
    3940
     41    virtual status_t            Archive(BMessage* into, bool deep = true) const;
     42    virtual status_t            AllArchived(BMessage* into) const;
     43    virtual status_t            AllUnarchived(const BMessage* from);
     44
    4045protected:
    4146            struct ColumnRowConstraints {
    4247                float   weight;
     
    6671                                    enum orientation orientation,
    6772                                    int32 index,
    6873                                    ColumnRowConstraints* constraints) = 0;
    69     virtual void                GetItemDimensions(BLayoutItem* item,
     74    virtual void                GetItemDimensions(BLayoutItem* item,
    7075                                    Dimensions* dimensions) = 0;
    7176
    7277private:
  • src/kits/interface/TwoDimensionalLayout.cpp

     
    1212#include <LayoutItem.h>
    1313#include <LayoutUtils.h>
    1414#include <List.h>
     15#include <Message.h>
    1516#include <View.h>
    1617
    1718#include <Referenceable.h>
     
    2122#include "SimpleLayouter.h"
    2223
    2324
     25// Archiving constants
     26namespace {
     27    const char* kHAlignedLayoutField = "B2DLayout:halignedlayout";
     28    const char* kVAlignedLayoutField = "B2DLayout:valignedlayout";
     29}
     30
     31
    2432// Some words of explanation:
    2533//
    2634// This class is the base class for BLayouts that organize their items
     
    6775            void                RemoveLocalLayouter(
    6876                                    LocalLayouter* localLayouter);
    6977
     78            status_t            AddAlignedLayoutsToArchive(BArchiver* archiver,
     79                                    LocalLayouter* requestedBy);
     80
    7081            void                AbsorbCompoundLayouter(CompoundLayouter* other);
    7182
    7283    virtual void                InvalidateLayout();
     
    164175            void                AlignWith(LocalLayouter* other,
    165176                                    enum orientation orientation);
    166177
     178    // Archiving stuff
     179            status_t            AddAlignedLayoutsToArchive(BArchiver* archiver);
     180            status_t            AddOwnerToArchive(BArchiver* archiver,
     181                                    CompoundLayouter* requestedBy,
     182                                    bool& _wasAvailable);
     183            status_t            AlignLayoutsFromArchive(BUnarchiver* unarchiver,
     184                                    orientation posture);
    167185
     186
    168187    // interface for the compound layout context
    169188
    170189            void                PrepareItems(
     
    197216
    198217    // implementation private
    199218private:
    200             BTwoDimensionalLayout*  fLayout;
    201             CompoundLayouter*   fHLayouter;
    202             VerticalCompoundLayouter* fVLayouter;
    203             BList               fHeightForWidthItems;
     219            BTwoDimensionalLayout*      fLayout;
     220            CompoundLayouter*           fHLayouter;
     221            VerticalCompoundLayouter*   fVLayouter;
     222            BList                       fHeightForWidthItems;
    204223
    205224    // active layout context when doing last horizontal layout
    206225            BLayoutContext*     fHorizontalLayoutContext;
     
    217236
    218237// #pragma mark -
    219238
     239// archiving constants
     240namespace {
     241    const char* kLeftInsetField = "B2Dlayout:leftInset";
     242    const char* kRightInsetField = "B2Dlayout:rightInset";
     243    const char* kTopInsetField = "B2Dlayout:topInset";
     244    const char* kBottomInsetField = "B2Dlayout:bottomInset";
     245    const char* kHSpacingField = "B2Dlayout:hspacing";
     246    const char* kVSpacingField = "B2Dlayout:vspacing";
     247}
    220248
     249
    221250BTwoDimensionalLayout::BTwoDimensionalLayout()
    222251    :
    223252    fLeftInset(0),
     
    231260}
    232261
    233262
     263BTwoDimensionalLayout::BTwoDimensionalLayout(BMessage* from)
     264    :
     265    BLayout(from),
     266    fLeftInset(0),
     267    fRightInset(0),
     268    fTopInset(0),
     269    fBottomInset(0),
     270    fHSpacing(0),
     271    fVSpacing(0),
     272    fLocalLayouter(new LocalLayouter(this))
     273{
     274    float LeftInset;
     275    float RightInset;
     276    float TopInset;
     277    float BottomInset;
     278    if (from->FindFloat(kLeftInsetField, &LeftInset) == B_OK
     279        && from->FindFloat(kRightInsetField, &RightInset) == B_OK
     280        && from->FindFloat(kTopInsetField, &TopInset) == B_OK
     281        && from->FindFloat(kBottomInsetField, &BottomInset) == B_OK)
     282        SetInsets(LeftInset, TopInset, RightInset, BottomInset);
     283
     284
     285    from->FindFloat(kHSpacingField, &fHSpacing);
     286    from->FindFloat(kVSpacingField, &fVSpacing);
     287}
     288
     289
    234290BTwoDimensionalLayout::~BTwoDimensionalLayout()
    235291{
    236292    delete fLocalLayouter;
     
    393449}
    394450
    395451
     452status_t
     453BTwoDimensionalLayout::Archive(BMessage* into, bool deep) const
     454{
     455    BArchiver archiver(into);
     456    status_t err = BLayout::Archive(into, deep);
     457   
     458    if (err == B_OK)
     459        err = into->AddFloat(kLeftInsetField, fLeftInset);
     460
     461    if (err == B_OK)
     462        err = into->AddFloat(kRightInsetField, fRightInset);
     463
     464    if (err == B_OK)
     465        err = into->AddFloat(kTopInsetField, fTopInset);
     466
     467    if (err == B_OK)
     468        err = into->AddFloat(kBottomInsetField, fBottomInset);
     469
     470    if (err == B_OK)
     471        err = into->AddFloat(kHSpacingField, fHSpacing);
     472
     473    if (err == B_OK)
     474        err = into->AddFloat(kVSpacingField, fVSpacing);
     475
     476    return archiver.Finish(err);
     477}
     478
     479
     480status_t
     481BTwoDimensionalLayout::AllArchived(BMessage* into) const
     482{
     483    BArchiver archiver(into);
     484
     485    status_t err = BLayout::AllArchived(into);
     486    if (err == B_OK)
     487        err = fLocalLayouter->AddAlignedLayoutsToArchive(&archiver);
     488    return err;
     489}
     490
     491
     492status_t
     493BTwoDimensionalLayout::AllUnarchived(const BMessage* from)
     494{
     495    status_t err = BLayout::AllUnarchived(from);
     496    if (err != B_OK)
     497        return err;
     498
     499    BUnarchiver unarchiver(from);
     500    err = fLocalLayouter->AlignLayoutsFromArchive(&unarchiver, B_HORIZONTAL);
     501    if (err == B_OK)
     502        err = fLocalLayouter->AlignLayoutsFromArchive(&unarchiver, B_VERTICAL);
     503
     504    return err;
     505}
     506
     507
    396508BSize
    397509BTwoDimensionalLayout::AddInsets(BSize size)
    398510{
     
    531643}
    532644
    533645
     646status_t
     647BTwoDimensionalLayout::CompoundLayouter::AddAlignedLayoutsToArchive(
     648    BArchiver* archiver, LocalLayouter* requestedBy)
     649{
     650    // The LocalLayouter* that really owns us is at index 0, layouts
     651    // at other indices are aligned to this one.
     652    if (requestedBy != fLocalLayouters.ItemAt(0))
     653        return B_OK;
     654
     655    status_t err;
     656    for (int32 i = fLocalLayouters.CountItems() - 1; i > 0; i--) {
     657        LocalLayouter* layouter = (LocalLayouter*)fLocalLayouters.ItemAt(i);
     658
     659        bool wasAvailable;
     660        err = layouter->AddOwnerToArchive(archiver, this, wasAvailable);
     661        if (err != B_OK && wasAvailable)
     662            return err;
     663    }
     664    return B_OK;
     665}
     666
     667
    534668void
    535669BTwoDimensionalLayout::CompoundLayouter::AbsorbCompoundLayouter(
    536670    CompoundLayouter* other)
     
    10131147}
    10141148
    10151149
     1150status_t
     1151BTwoDimensionalLayout::LocalLayouter::AddAlignedLayoutsToArchive(
     1152    BArchiver* archiver)
     1153{
     1154    status_t err = fHLayouter->AddAlignedLayoutsToArchive(archiver, this);
     1155
     1156    if (err == B_OK)
     1157        err = fVLayouter->AddAlignedLayoutsToArchive(archiver, this);
     1158
     1159    return err;
     1160}
     1161
     1162
     1163status_t
     1164BTwoDimensionalLayout::LocalLayouter::AddOwnerToArchive(BArchiver* archiver,
     1165    CompoundLayouter* requestedBy, bool& _wasAvailable)
     1166{
     1167    const char* field = kHAlignedLayoutField;
     1168    if (requestedBy == fVLayouter)
     1169        field = kVAlignedLayoutField;
     1170
     1171    if ((_wasAvailable = archiver->IsArchived(fLayout)))
     1172        return archiver->AddArchivable(field, fLayout);
     1173
     1174    return B_NAME_NOT_FOUND;
     1175}
     1176
     1177
     1178status_t
     1179BTwoDimensionalLayout::LocalLayouter::AlignLayoutsFromArchive(
     1180    BUnarchiver* unarchiver, orientation posture)
     1181{
     1182    const char* field = kHAlignedLayoutField;
     1183    if (posture == B_VERTICAL)
     1184        field = kVAlignedLayoutField;
     1185
     1186    int32 count;
     1187    status_t err = unarchiver->ArchiveMessage()->GetInfo(field, NULL, &count);
     1188    if (err == B_NAME_NOT_FOUND)
     1189        return B_OK;
     1190   
     1191    BTwoDimensionalLayout* retriever;
     1192    for (int32 i = 0; i < count && err == B_OK; i++) {
     1193        err  = unarchiver->FindObject(field, i,
     1194            BUnarchiver::B_DONT_ASSUME_OWNERSHIP, retriever);
     1195
     1196        if (err == B_OK)
     1197            retriever->AlignLayoutWith(fLayout, posture);
     1198    }
     1199
     1200    return err;
     1201}
     1202
     1203
    10161204void
    10171205BTwoDimensionalLayout::LocalLayouter::PrepareItems(
    10181206    CompoundLayouter* compoundLayouter)