Ticket #5525: cardLayout.patch

File cardLayout.patch, 4.4 KB (added by yourpalal, 11 years ago)

implement archiving of BCardLayout class.

  • src/kits/interface/CardLayout.cpp

     
    66#include <CardLayout.h>
    77
    88#include <LayoutItem.h>
     9#include <Message.h>
    910#include <View.h>
    1011
    11 // constructor
     12
     13namespace {
     14    const char* kVisibleItemField = "BCardLayout:visibleItem";
     15}
     16
     17
    1218BCardLayout::BCardLayout()
    13     : BLayout(),
    14       fMin(0, 0),
    15       fMax(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED),
    16       fPreferred(0, 0),
    17       fVisibleItem(NULL),
    18       fMinMaxValid(false)
     19    :
     20    BLayout(),
     21    fMin(0, 0),
     22    fMax(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED),
     23    fPreferred(0, 0),
     24    fVisibleItem(NULL),
     25    fMinMaxValid(false)
    1926{
    2027}
    2128
    22 // destructor
     29
     30BCardLayout::BCardLayout(BMessage* from)
     31    :
     32    BLayout(BUnarchiver::PrepareArchive(from)),
     33    fMin(0, 0),
     34    fMax(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED),
     35    fPreferred(0, 0),
     36    fVisibleItem(NULL),
     37    fMinMaxValid(false)
     38{
     39    BUnarchiver(from).Finish();
     40}
     41
     42
    2343BCardLayout::~BCardLayout()
    2444{
    2545}
    2646
    27 // VisibleItem
     47
    2848BLayoutItem*
    2949BCardLayout::VisibleItem() const
    3050{
    3151    return fVisibleItem;
    3252}
    3353
    34 // VisibleIndex
     54
    3555int32
    3656BCardLayout::VisibleIndex() const
    3757{
    3858    return IndexOfItem(fVisibleItem);
    3959}
    4060
    41 // SetVisibleItem
     61
    4262void
    4363BCardLayout::SetVisibleItem(int32 index)
    4464{
    4565    SetVisibleItem(ItemAt(index));
    4666}
    4767
    48 // SetVisibleItem
     68
    4969void
    5070BCardLayout::SetVisibleItem(BLayoutItem* item)
    5171{
     
    6787    }
    6888}
    6989
    70 // MinSize
     90
    7191BSize
    7292BCardLayout::MinSize()
    7393{
     
    7595    return fMin;
    7696}
    7797
    78 // MaxSize
     98
    7999BSize
    80100BCardLayout::MaxSize()
    81101{
     
    83103    return fMax;
    84104}
    85105
    86 // PreferredSize
     106
    87107BSize
    88108BCardLayout::PreferredSize()
    89109{
     
    91111    return fPreferred;
    92112}
    93113
    94 // Alignment
     114
    95115BAlignment
    96116BCardLayout::Alignment()
    97117{
    98118    return BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT);
    99119}
    100120
    101 // HasHeightForWidth
     121
    102122bool
    103123BCardLayout::HasHeightForWidth()
    104124{
     
    111131    return false;
    112132}
    113133
    114 // GetHeightForWidth
     134
    115135void
    116136BCardLayout::GetHeightForWidth(float width, float* min, float* max,
    117137    float* preferred)
     
    152172        *preferred = preferredHeight;
    153173}
    154174
    155 // InvalidateLayout
     175
    156176void
    157177BCardLayout::InvalidateLayout()
    158178{
     
    161181    fMinMaxValid = false;
    162182}
    163183
    164 // LayoutView
     184
    165185void
    166186BCardLayout::LayoutView()
    167187{
     
    175195        fVisibleItem->AlignInFrame(BRect(0, 0, size.width, size.height));
    176196}
    177197
    178 // ItemAdded
     198
     199status_t
     200BCardLayout::Archive(BMessage* into, bool deep) const
     201{
     202    BArchiver archiver(into);
     203    status_t err = BLayout::Archive(into, deep);
     204
     205    if (err == B_OK && deep)
     206        err = into->AddInt32(kVisibleItemField, IndexOfItem(fVisibleItem));
     207
     208    return archiver.Finish(err);
     209}
     210
     211
     212status_t
     213BCardLayout::AllUnarchived(const BMessage* from)
     214{
     215    status_t err = BLayout::AllUnarchived(from);
     216    if (err != B_OK)
     217        return err;
     218
     219    int32 visibleIndex;
     220    err = from->FindInt32(kVisibleItemField, &visibleIndex);
     221    if (err == B_OK)
     222        SetVisibleItem(visibleIndex);
     223
     224    return err;
     225}
     226
     227
     228BArchivable*
     229BCardLayout::Instantiate(BMessage* from)
     230{
     231    if (validate_instantiation(from, "BCardLayout"))
     232        return new BCardLayout(from);
     233    return NULL;
     234}
     235   
     236
    179237void
    180238BCardLayout::ItemAdded(BLayoutItem* item)
    181239{
    182240    item->SetVisible(false);
    183241}
    184242
    185 // ItemRemoved
     243
    186244void
    187245BCardLayout::ItemRemoved(BLayoutItem* item)
    188246{
     
    192250    }
    193251}
    194252
    195 // _ValidateMinMax
     253
    196254void
    197255BCardLayout::_ValidateMinMax()
    198256{
  • headers/os/interface/CardLayout.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 _CARD_LAYOUT_H
     
    1111class BCardLayout : public BLayout {
    1212public:
    1313                                BCardLayout();
     14                                BCardLayout(BMessage* from);
    1415    virtual                     ~BCardLayout();
    1516
    1617            BLayoutItem*        VisibleItem() const;
     
    3031    virtual void                InvalidateLayout();
    3132    virtual void                LayoutView();
    3233
     34    virtual status_t            Archive(BMessage* into, bool deep = true) const;
     35    virtual status_t            AllUnarchived(const BMessage* from);
     36    static  BArchivable*        Instantiate(BMessage* from);
     37
    3338protected:
    3439    virtual void                ItemAdded(BLayoutItem* item);
    3540    virtual void                ItemRemoved(BLayoutItem* item);