Ticket #5525: viewLayoutItem.patch

File viewLayoutItem.patch, 4.5 KB (added by yourpalal, 11 years ago)

implement archiving of BViewLayoutItem class.

  • src/kits/interface/ViewLayoutItem.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
    67#include "ViewLayoutItem.h"
    78
     9#include <Layout.h>
    810#include <View.h>
    911
     12#include <new>
    1013
    11 // constructor
     14namespace {
     15    const char* kViewField = "ViewLayoutItem:view";
     16}
     17
     18
    1219BViewLayoutItem::BViewLayoutItem(BView* view)
    13     : fView(view)
     20    :
     21    fView(view)
    1422{
    1523}
    1624
    17 // destructor
     25
     26BViewLayoutItem::BViewLayoutItem(BMessage* from)
     27    :
     28    BLayoutItem(BUnarchiver::PrepareArchive(from)),
     29    fView(NULL)
     30{
     31    BUnarchiver unarchiver(from);
     32    unarchiver.Finish(unarchiver.FindObject<BView>(kViewField, 0,
     33        BUnarchiver::B_DONT_ASSUME_OWNERSHIP, fView));
     34}
     35
     36
    1837BViewLayoutItem::~BViewLayoutItem()
    1938{
    2039}
    2140
    22 // MinSize
     41
    2342BSize
    2443BViewLayoutItem::MinSize()
    2544{
    2645    return fView->MinSize();
    2746}
    2847
    29 // MaxSize
     48
    3049BSize
    3150BViewLayoutItem::MaxSize()
    3251{
    3352    return fView->MaxSize();
    3453}
    3554
    36 // PreferredSize
     55
    3756BSize
    3857BViewLayoutItem::PreferredSize()
    3958{
    4059    return fView->PreferredSize();
    4160}
    4261
    43 // Alignment
     62
    4463BAlignment
    4564BViewLayoutItem::Alignment()
    4665{
    4766    return fView->LayoutAlignment();
    4867}
    4968
    50 // SetExplicitMinSize
     69
    5170void
    5271BViewLayoutItem::SetExplicitMinSize(BSize size)
    5372{
    5473    fView->SetExplicitMinSize(size);
    5574}
    5675
    57 // SetExplicitMaxSize
     76
    5877void
    5978BViewLayoutItem::SetExplicitMaxSize(BSize size)
    6079{
    6180    fView->SetExplicitMaxSize(size);
    6281}
    6382
    64 // SetExplicitPreferredSize
     83
    6584void
    6685BViewLayoutItem::SetExplicitPreferredSize(BSize size)
    6786{
    6887    fView->SetExplicitPreferredSize(size);
    6988}
    7089
    71 // SetExplicitAlignment
     90
    7291void
    7392BViewLayoutItem::SetExplicitAlignment(BAlignment alignment)
    7493{
    7594    fView->SetExplicitAlignment(alignment);
    7695}
    7796
    78 // IsVisible
     97
    7998bool
    8099BViewLayoutItem::IsVisible()
    81100{
    82101    return !fView->IsHidden(fView);
    83102}
    84103
    85 // SetVisible
     104
    86105void
    87106BViewLayoutItem::SetVisible(bool visible)
    88107{
     
    94113    }
    95114}
    96115
    97 // Frame
     116
    98117BRect
    99118BViewLayoutItem::Frame()
    100119{
    101120    return fView->Frame();
    102121}
    103122
    104 // SetFrame
     123
    105124void
    106125BViewLayoutItem::SetFrame(BRect frame)
    107126{
     
    109128    fView->ResizeTo(frame.Width(), frame.Height());
    110129}
    111130
    112 // HasHeightForWidth
     131
    113132bool
    114133BViewLayoutItem::HasHeightForWidth()
    115134{
    116135    return fView->HasHeightForWidth();
    117136}
    118137
    119 // GetHeightForWidth
     138
    120139void
    121140BViewLayoutItem::GetHeightForWidth(float width, float* min, float* max,
    122141    float* preferred)
     
    124143    fView->GetHeightForWidth(width, min, max, preferred);
    125144}
    126145
    127 // View
     146
    128147BView*
    129148BViewLayoutItem::View()
    130149{
    131150    return fView;
    132151}
    133152
    134 // InvalidateLayout
     153
    135154void
    136155BViewLayoutItem::InvalidateLayout()
    137156{
    138157    fView->InvalidateLayout();
    139158}
     159
     160
     161status_t
     162BViewLayoutItem::Archive(BMessage* into, bool deep) const
     163{
     164    BArchiver archiver(into);
     165    status_t err = BLayoutItem::Archive(into, deep);
     166
     167    return archiver.Finish(err);
     168}
     169
     170
     171status_t
     172BViewLayoutItem::AllArchived(BMessage* into) const
     173{
     174    BArchiver archiver(into);
     175    status_t err = BLayoutItem::AllArchived(into);
     176
     177    if (err == B_OK) {
     178        if (archiver.IsArchived(fView))
     179            err = archiver.AddArchivable(kViewField, fView);
     180        else
     181            err = B_NAME_NOT_FOUND;
     182    }
     183
     184    return err;
     185}
     186
     187
     188status_t
     189BViewLayoutItem::AllUnarchived(const BMessage* from)
     190{
     191    if (!fView)
     192        return B_ERROR;
     193
     194    return BLayoutItem::AllUnarchived(from);
     195}
     196
     197
     198BArchivable*
     199BViewLayoutItem::Instantiate(BMessage* from)
     200{
     201    if (validate_instantiation(from, "BViewLayoutItem"))
     202        return new(std::nothrow) BViewLayoutItem(from);
     203    return NULL;
     204}
  • src/kits/interface/ViewLayoutItem.h

     
    11/*
    2  * Copyright 2006, Haiku Inc.
     2 * Copyright 2006-2010, Haiku Inc.
    33 * Distributed under the terms of the MIT License.
    44 */
    55#ifndef _VIEW_LAYOUT_ITEM_H
     
    1111class BViewLayoutItem : public BLayoutItem {
    1212public:
    1313                                BViewLayoutItem(BView* view);
     14                                BViewLayoutItem(BMessage* from);
    1415    virtual                     ~BViewLayoutItem();
    1516
    1617    virtual BSize               MinSize();
     
    3738
    3839    virtual void                InvalidateLayout();
    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    static  BArchivable*        Instantiate(BMessage* from);
     45
    4046private:
    4147            BView*              fView;
    4248};