Ticket #6257: layoutArchiving.2.patch
File layoutArchiving.2.patch, 9.3 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 */ 5 6 6 #include <Layout.h>7 8 7 #include <new> 9 8 9 #include <Message.h> 10 10 #include <View.h> 11 11 12 #include <Layout.h> 13 12 14 #include "ViewLayoutItem.h" 13 15 14 16 using std::nothrow; 15 17 18 namespace { 19 const char* kLayoutItemField = "BLayout:_items"; 20 const char* kLayoutDataField = "BLayout:_data"; 21 } 16 22 23 17 24 // constructor 18 25 BLayout::BLayout() 19 : fView(NULL), 20 fItems(20) 26 : 27 fView(NULL), 28 fItems(20) 21 29 { 22 30 } 23 31 24 // destructor 32 33 BLayout::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 25 47 BLayout::~BLayout() 26 48 { 27 49 // this deletes all items 28 50 SetView(NULL); 29 51 } 30 52 31 // View 53 32 54 BView* 33 55 BLayout::View() const 34 56 { 35 57 return fView; 36 58 } 37 59 38 // AddView 60 39 61 BLayoutItem* 40 62 BLayout::AddView(BView* child) 41 63 { 42 64 return AddView(-1, child); 43 65 } 44 66 45 // AddView 67 46 68 BLayoutItem* 47 69 BLayout::AddView(int32 index, BView* child) 48 70 { … … 54 76 return NULL; 55 77 } 56 78 57 // AddItem 79 58 80 bool 59 81 BLayout::AddItem(BLayoutItem* item) 60 82 { 61 83 return AddItem(-1, item); 62 84 } 63 85 64 // AddItem 86 65 87 bool 66 88 BLayout::AddItem(int32 index, BLayoutItem* item) 67 89 { 68 90 if (!fView || !item || fItems.HasItem(item)) 69 91 return false; 70 92 71 // if the item refers to a BView, we make sure ,it is added to the parent93 // if the item refers to a BView, we make sure it is added to the parent 72 94 // view 73 95 BView* view = item->View(); 74 96 if (view && view->fParent != fView && !fView->_AddChild(view, NULL)) … … 86 108 return true; 87 109 } 88 110 89 // RemoveView 111 90 112 bool 91 113 BLayout::RemoveView(BView* child) 92 114 { … … 107 129 return removed; 108 130 } 109 131 110 // RemoveItem 132 111 133 bool 112 134 BLayout::RemoveItem(BLayoutItem* item) 113 135 { … … 115 137 return (index >= 0 ? RemoveItem(index) : false); 116 138 } 117 139 118 // RemoveItem 140 119 141 BLayoutItem* 120 142 BLayout::RemoveItem(int32 index) 121 143 { … … 137 159 return item; 138 160 } 139 161 140 // ItemAt 162 141 163 BLayoutItem* 142 164 BLayout::ItemAt(int32 index) const 143 165 { 144 166 return (BLayoutItem*)fItems.ItemAt(index); 145 167 } 146 168 147 // CountItems 169 148 170 int32 149 171 BLayout::CountItems() const 150 172 { 151 173 return fItems.CountItems(); 152 174 } 153 175 154 // IndexOfItem 176 155 177 int32 156 BLayout::IndexOfItem( BLayoutItem* item) const178 BLayout::IndexOfItem(const BLayoutItem* item) const 157 179 { 158 180 return fItems.IndexOf(item); 159 181 } 160 182 161 // IndexOfView 183 162 184 int32 163 185 BLayout::IndexOfView(BView* child) const 164 186 { … … 172 194 return -1; 173 195 } 174 196 175 // InvalidateLayout 197 176 198 void 177 199 BLayout::InvalidateLayout() 178 200 { … … 180 202 fView->InvalidateLayout(); 181 203 } 182 204 183 // ItemAdded 205 206 status_t 207 BLayout::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 236 status_t 237 BLayout::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 270 status_t 271 BLayout::ArchiveLayoutData(BMessage* into, const BLayoutItem* of) const 272 { 273 return B_OK; 274 } 275 276 277 status_t 278 BLayout::RestoreItemAndData(const BMessage* from, BLayoutItem* item) 279 { 280 if (item && AddItem(item)) 281 return B_OK; 282 return B_ERROR; 283 } 284 285 184 286 void 185 287 BLayout::ItemAdded(BLayoutItem* item) 186 288 { 187 289 } 188 290 189 // ItemRemoved 291 190 292 void 191 293 BLayout::ItemRemoved(BLayoutItem* item) 192 294 { 193 295 } 194 296 195 // SetView 297 196 298 void 197 299 BLayout::SetView(BView* view) 198 300 { -
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 */ … … 9 10 #include <LayoutUtils.h> 10 11 11 12 12 // constructor13 13 BLayoutItem::BLayoutItem() 14 : fLayout(NULL), 15 fLayoutData(NULL) 14 : 15 fLayout(NULL), 16 fLayoutData(NULL) 16 17 { 17 18 } 18 19 19 // destructor 20 21 BLayoutItem::BLayoutItem(BMessage* from) 22 : 23 BArchivable(BUnarchiver::PrepareArchive(from)), 24 fLayout(NULL), 25 fLayoutData(NULL) 26 { 27 BUnarchiver(from).Finish(); 28 } 29 30 20 31 BLayoutItem::~BLayoutItem() 21 32 { 22 33 } 23 34 24 // Layout 35 25 36 BLayout* 26 37 BLayoutItem::Layout() const 27 38 { 28 39 return fLayout; 29 40 } 30 41 31 // HasHeightForWidth 42 32 43 bool 33 44 BLayoutItem::HasHeightForWidth() 34 45 { … … 36 47 return false; 37 48 } 38 49 39 // GetHeightForWidth 50 40 51 void 41 52 BLayoutItem::GetHeightForWidth(float width, float* min, float* max, 42 53 float* preferred) … … 44 55 // no "height for width" by default 45 56 } 46 57 47 // View 58 48 59 BView* 49 60 BLayoutItem::View() 50 61 { 51 62 return NULL; 52 63 } 53 64 54 // InvalidateLayout 65 55 66 void 56 67 BLayoutItem::InvalidateLayout() 57 68 { … … 59 70 fLayout->InvalidateLayout(); 60 71 } 61 72 62 // LayoutData 73 63 74 void* 64 75 BLayoutItem::LayoutData() const 65 76 { 66 77 return fLayoutData; 67 78 } 68 79 69 // SetLayoutData 80 70 81 void 71 82 BLayoutItem::SetLayoutData(void* data) 72 83 { 73 84 fLayoutData = data; 74 85 } 75 86 76 // AlignInFrame 87 77 88 void 78 89 BLayoutItem::AlignInFrame(BRect frame) 79 90 { … … 101 112 SetFrame(BLayoutUtils::AlignInFrame(frame, maxSize, alignment)); 102 113 } 103 114 104 // SetLayout 115 116 status_t 117 BLayoutItem::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 129 status_t 130 BLayoutItem::AllArchived(BMessage* into) const 131 { 132 BArchiver archiver(into); 133 return BArchivable::AllArchived(into); 134 } 135 136 137 status_t 138 BLayoutItem::AllUnarchived(const BMessage* from) 139 { 140 return BArchivable::AllUnarchived(from); 141 } 142 143 105 144 void 106 145 BLayoutItem::SetLayout(BLayout* layout) 107 146 { -
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) const; 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 AllArchived(BMessage* into) const; 55 virtual status_t AllUnarchived(const BMessage* from); 50 56 57 51 58 private: 52 59 friend class BLayout; 53 60