Ticket #6257: layoutArchiving.patch
File layoutArchiving.patch, 6.4 KB (added by , 14 years ago) |
---|
-
src/kits/interface/Layout.cpp
1 1 /* 2 * Copyright 2010, Haiku Inc. 2 3 * Copyright 2006, Ingo Weinhold <bonefish@cs.tu-berlin.de>. 3 4 * All rights reserved. Distributed under the terms of the MIT License. 4 5 */ … … 13 14 14 15 using std::nothrow; 15 16 17 namespace { 18 const char* kLayoutItemArchiveField = "_items"; 19 } 16 20 21 17 22 // constructor 18 23 BLayout::BLayout() 19 : fView(NULL), 20 fItems(20) 24 : 25 fView(NULL), 26 fItems(20) 21 27 { 22 28 } 23 29 30 // archive constructor 31 BLayout::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 24 45 // destructor 25 46 BLayout::~BLayout() 26 47 { … … 153 174 154 175 // IndexOfItem 155 176 int32 156 BLayout::IndexOfItem( BLayoutItem* item) const177 BLayout::IndexOfItem(const BLayoutItem* item) const 157 178 { 158 179 return fItems.IndexOf(item); 159 180 } … … 180 201 fView->InvalidateLayout(); 181 202 } 182 203 204 // Archive 205 status_t 206 BLayout::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 229 status_t 230 BLayout::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 239 status_t 240 BLayout::ArchiveLayoutData(BMessage* into, const BLayoutItem* of) 241 { 242 return B_OK; 243 } 244 245 // RestoreItemAndData 246 status_t 247 BLayout::RestoreItemAndData(const BMessage* from, BLayoutItem* item) 248 { 249 return B_OK; 250 } 251 183 252 // ItemAdded 184 253 void 185 254 BLayout::ItemAdded(BLayoutItem* item) -
src/kits/interface/LayoutItem.cpp
1 1 /* 2 * Copyright 2010, Haiku, Inc. 2 3 * Copyright 2006, Ingo Weinhold <bonefish@cs.tu-berlin.de>. 3 4 * All rights reserved. Distributed under the terms of the MIT License. 4 5 */ … … 8 9 #include <Layout.h> 9 10 #include <LayoutUtils.h> 10 11 12 namespace { 13 const char* kLayoutArchiveField = "layout"; 14 } 11 15 12 16 // constructor 13 17 BLayoutItem::BLayoutItem() 14 : fLayout(NULL), 15 fLayoutData(NULL) 18 : 19 fLayout(NULL), 20 fLayoutData(NULL) 16 21 { 17 22 } 18 23 24 // archive constructor 25 BLayoutItem::BLayoutItem(BMessage* from) 26 : 27 BArchivable(BUnarchiver::PrepareArchive(from)), 28 fLayout(NULL), 29 fLayoutData(NULL) 30 { 31 BUnarchiver(from).EnsureUnarchived(kLayoutArchiveField); 32 } 33 19 34 // destructor 20 35 BLayoutItem::~BLayoutItem() 21 36 { … … 101 116 SetFrame(BLayoutUtils::AlignInFrame(frame, maxSize, alignment)); 102 117 } 103 118 119 // Archive 120 status_t 121 BLayoutItem::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 136 status_t 137 BLayoutItem::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 104 153 // SetLayout 105 154 void 106 155 BLayoutItem::SetLayout(BLayout* layout) -
headers/os/interface/Layout.h
1 1 /* 2 * Copyright 2006 , Haiku, Inc. All rights reserved.2 * Copyright 2006-2010, Haiku, Inc. All rights reserved. 3 3 * Distributed under the terms of the MIT License. 4 4 */ 5 5 #ifndef _LAYOUT_H 6 6 #define _LAYOUT_H 7 7 8 8 #include <Alignment.h> 9 #include <Archivable.h> 9 10 #include <List.h> 10 11 #include <Size.h> 11 12 … … 13 14 class BView; 14 15 15 16 16 class BLayout {17 class BLayout: public BArchivable { 17 18 public: 18 19 BLayout(); 20 BLayout(BMessage* archive); 19 21 virtual ~BLayout(); 20 22 21 23 BView* View() const; … … 32 34 33 35 BLayoutItem* ItemAt(int32 index) const; 34 36 int32 CountItems() const; 35 int32 IndexOfItem( BLayoutItem* item) const;37 int32 IndexOfItem(const BLayoutItem* item) const; 36 38 int32 IndexOfView(BView* child) const; 37 39 38 40 virtual BSize MinSize() = 0; … … 48 50 49 51 virtual void LayoutView() = 0; 50 52 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 51 60 protected: 61 52 62 // TODO: Since memory allocations can fail, we should return a bool and 53 63 // undo the addition, if false. 54 64 virtual void ItemAdded(BLayoutItem* item); -
headers/os/interface/LayoutItem.h
1 1 /* 2 * Copyright 2006 , Haiku, Inc. All rights reserved.2 * Copyright 2006-2010, Haiku, Inc. All rights reserved. 3 3 * Distributed under the terms of the MIT License. 4 4 */ 5 5 #ifndef _LAYOUT_ITEM_H 6 6 #define _LAYOUT_ITEM_H 7 7 8 8 #include <Alignment.h> 9 #include <Archivable.h> 9 10 #include <Rect.h> 10 11 #include <Size.h> 11 12 12 13 class BLayout; 13 14 class BView; 14 15 15 class BLayoutItem {16 class BLayoutItem: public BArchivable { 16 17 public: 17 18 BLayoutItem(); 19 BLayoutItem(BMessage* from); 18 20 virtual ~BLayoutItem(); 19 21 20 22 BLayout* Layout() const; … … 47 49 void SetLayoutData(void* data); 48 50 49 51 void AlignInFrame(BRect frame); 52 53 virtual status_t Archive(BMessage* into, bool deep = true) const; 54 virtual status_t AllUnarchived(const BMessage* from); 50 55 56 51 57 private: 52 58 friend class BLayout; 53 59