Ticket #5525: groupLayout.patch
File groupLayout.patch, 9.6 KB (added by , 14 years ago) |
---|
-
headers/os/interface/GroupLayout.h
1 /* 2 * Copyright 2006, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _GROUP_LAYOUT_H 6 #define _GROUP_LAYOUT_H 7 8 #include <TwoDimensionalLayout.h> 9 10 class BGroupLayout : public BTwoDimensionalLayout { 11 public: 12 BGroupLayout(enum orientation orientation, 13 float spacing = 0.0f); 14 virtual ~BGroupLayout(); 15 16 float Spacing() const; 17 void SetSpacing(float spacing); 18 19 orientation Orientation() const; 20 void SetOrientation(enum orientation orientation); 21 22 float ItemWeight(int32 index) const; 23 void SetItemWeight(int32 index, float weight); 24 25 virtual BLayoutItem* AddView(BView* child); 26 virtual BLayoutItem* AddView(int32 index, BView* child); 27 virtual BLayoutItem* AddView(BView* child, float weight); 28 virtual BLayoutItem* AddView(int32 index, BView* child, 29 float weight); 30 31 virtual bool AddItem(BLayoutItem* item); 32 virtual bool AddItem(int32 index, BLayoutItem* item); 33 virtual bool AddItem(BLayoutItem* item, float weight); 34 virtual bool AddItem(int32 index, BLayoutItem* item, 35 float weight); 36 37 protected: 38 virtual void ItemAdded(BLayoutItem* item); 39 virtual void ItemRemoved(BLayoutItem* item); 40 41 virtual void PrepareItems(enum orientation orientation); 42 43 virtual int32 InternalCountColumns(); 44 virtual int32 InternalCountRows(); 45 virtual void GetColumnRowConstraints( 46 enum orientation orientation, 47 int32 index, 48 ColumnRowConstraints* constraints); 49 virtual void GetItemDimensions(BLayoutItem* item, 50 Dimensions* dimensions); 51 52 private: 53 struct ItemLayoutData; 54 55 ItemLayoutData* _LayoutDataForItem(BLayoutItem* item) const; 56 57 orientation fOrientation; 58 BList fVisibleItems; 59 }; 60 61 #endif // _GROUP_LAYOUT_H 1 /* 2 * Copyright 2006-2010, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _GROUP_LAYOUT_H 6 #define _GROUP_LAYOUT_H 7 8 #include <TwoDimensionalLayout.h> 9 10 class BGroupLayout : public BTwoDimensionalLayout { 11 public: 12 BGroupLayout(enum orientation orientation, 13 float spacing = 0.0f); 14 BGroupLayout(BMessage* from); 15 virtual ~BGroupLayout(); 16 17 float Spacing() const; 18 void SetSpacing(float spacing); 19 20 orientation Orientation() const; 21 void SetOrientation(enum orientation orientation); 22 23 float ItemWeight(int32 index) const; 24 void SetItemWeight(int32 index, float weight); 25 26 virtual BLayoutItem* AddView(BView* child); 27 virtual BLayoutItem* AddView(int32 index, BView* child); 28 virtual BLayoutItem* AddView(BView* child, float weight); 29 virtual BLayoutItem* AddView(int32 index, BView* child, 30 float weight); 31 32 virtual bool AddItem(BLayoutItem* item); 33 virtual bool AddItem(int32 index, BLayoutItem* item); 34 virtual bool AddItem(BLayoutItem* item, float weight); 35 virtual bool AddItem(int32 index, BLayoutItem* item, 36 float weight); 37 38 virtual status_t Archive(BMessage* into, bool deep = true) const; 39 virtual status_t AllUnarchived(const BMessage* from); 40 static BArchivable* Instantiate(BMessage* from); 41 42 virtual status_t ItemArchived(BMessage* into, BLayoutItem* item, 43 int32 index) const; 44 virtual status_t ItemUnarchived(const BMessage* from, 45 BLayoutItem* item, int32 index); 46 47 protected: 48 virtual void ItemAdded(BLayoutItem* item); 49 virtual void ItemRemoved(BLayoutItem* item); 50 51 virtual void PrepareItems(enum orientation orientation); 52 53 virtual int32 InternalCountColumns(); 54 virtual int32 InternalCountRows(); 55 virtual void GetColumnRowConstraints( 56 enum orientation orientation, 57 int32 index, 58 ColumnRowConstraints* constraints); 59 virtual void GetItemDimensions(BLayoutItem* item, 60 Dimensions* dimensions); 61 62 private: 63 struct ItemLayoutData; 64 65 ItemLayoutData* _LayoutDataForItem(BLayoutItem* item) const; 66 67 orientation fOrientation; 68 BList fVisibleItems; 69 }; 70 71 #endif // _GROUP_LAYOUT_H -
src/kits/interface/GroupLayout.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 */ … … 6 7 #include <GroupLayout.h> 7 8 8 9 #include <LayoutItem.h> 10 #include <Message.h> 9 11 12 #include <new> 13 using std::nothrow; 10 14 11 // ItemLayoutData 15 16 namespace { 17 const char* kWeightField = "BGroupLayoutData:weight"; 18 const char* kVerticalField = "BGroupLayout:vertical"; 19 } 20 21 12 22 struct BGroupLayout::ItemLayoutData { 13 23 float weight; 14 24 … … 18 28 } 19 29 }; 20 30 21 // constructor 31 22 32 BGroupLayout::BGroupLayout(enum orientation orientation, float spacing) 23 : BTwoDimensionalLayout(), 24 fOrientation(orientation) 33 : 34 BTwoDimensionalLayout(), 35 fOrientation(orientation) 25 36 { 26 37 SetSpacing(spacing); 27 38 } 28 39 29 // destructor 40 41 BGroupLayout::BGroupLayout(BMessage* from) 42 : 43 BTwoDimensionalLayout(from) 44 { 45 bool isVertical; 46 if (from->FindBool(kVerticalField, &isVertical) != B_OK) 47 isVertical = false; 48 fOrientation = (isVertical) ? B_VERTICAL : B_HORIZONTAL; 49 } 50 51 30 52 BGroupLayout::~BGroupLayout() 31 53 { 32 54 } 33 55 34 // Spacing 56 35 57 float 36 58 BGroupLayout::Spacing() const 37 59 { 38 60 return fHSpacing; 39 61 } 40 62 41 // SetSpacing 63 42 64 void 43 65 BGroupLayout::SetSpacing(float spacing) 44 66 { … … 49 71 } 50 72 } 51 73 52 // Orientation 74 53 75 orientation 54 76 BGroupLayout::Orientation() const 55 77 { 56 78 return fOrientation; 57 79 } 58 80 59 // SetOrientation 81 60 82 void 61 83 BGroupLayout::SetOrientation(enum orientation orientation) 62 84 { … … 67 89 } 68 90 } 69 91 70 // ItemWeight 92 71 93 float 72 94 BGroupLayout::ItemWeight(int32 index) const 73 95 { … … 78 100 return (data ? data->weight : 0); 79 101 } 80 102 81 // SetItemWeight 103 82 104 void 83 105 BGroupLayout::SetItemWeight(int32 index, float weight) 84 106 { … … 91 113 InvalidateLayout(); 92 114 } 93 115 94 // AddView 116 95 117 BLayoutItem* 96 118 BGroupLayout::AddView(BView* child) 97 119 { 98 120 return BTwoDimensionalLayout::AddView(child); 99 121 } 100 122 101 // AddView 123 102 124 BLayoutItem* 103 125 BGroupLayout::AddView(int32 index, BView* child) 104 126 { 105 127 return BTwoDimensionalLayout::AddView(index, child); 106 128 } 107 129 108 // AddView 130 109 131 BLayoutItem* 110 132 BGroupLayout::AddView(BView* child, float weight) 111 133 { 112 134 return AddView(-1, child, weight); 113 135 } 114 136 115 // AddView 137 116 138 BLayoutItem* 117 139 BGroupLayout::AddView(int32 index, BView* child, float weight) 118 140 { … … 123 145 return item; 124 146 } 125 147 126 // AddItem 148 127 149 bool 128 150 BGroupLayout::AddItem(BLayoutItem* item) 129 151 { 130 152 return BTwoDimensionalLayout::AddItem(item); 131 153 } 132 154 133 // AddItem 155 134 156 bool 135 157 BGroupLayout::AddItem(int32 index, BLayoutItem* item) 136 158 { 137 159 return BTwoDimensionalLayout::AddItem(index, item); 138 160 } 139 161 140 // AddItem 162 141 163 bool 142 164 BGroupLayout::AddItem(BLayoutItem* item, float weight) 143 165 { 144 166 return AddItem(-1, item, weight); 145 167 } 146 168 147 // AddItem 169 148 170 bool 149 171 BGroupLayout::AddItem(int32 index, BLayoutItem* item, float weight) 150 172 { … … 157 179 return success; 158 180 } 159 181 160 // ItemAdded 182 183 status_t 184 BGroupLayout::Archive(BMessage* into, bool deep) const 185 { 186 BArchiver archiver(into); 187 status_t err = BTwoDimensionalLayout::Archive(into, deep); 188 189 if (err == B_OK) 190 err = into->AddBool(kVerticalField, fOrientation == B_VERTICAL); 191 192 return archiver.Finish(err); 193 } 194 195 196 status_t 197 BGroupLayout::AllUnarchived(const BMessage* from) 198 { 199 return BTwoDimensionalLayout::AllUnarchived(from); 200 } 201 202 203 BArchivable* 204 BGroupLayout::Instantiate(BMessage* from) 205 { 206 if (validate_instantiation(from, "BGroupLayout")) 207 return new(nothrow) BGroupLayout(from); 208 return NULL; 209 } 210 211 212 status_t 213 BGroupLayout::ItemArchived(BMessage* into, 214 BLayoutItem* item, int32 index) const 215 { 216 BGroupLayout::ItemLayoutData* data = 217 (BGroupLayout::ItemLayoutData*)item->LayoutData(); 218 219 if (!data) // TODO: remove this once ItemAdded() returns a bool 220 return B_BAD_VALUE; 221 222 return into->AddFloat(kWeightField, data->weight); 223 } 224 225 226 status_t 227 BGroupLayout::ItemUnarchived(const BMessage* from, 228 BLayoutItem* item, int32 index) 229 { 230 float weight; 231 status_t err = from->FindFloat(kWeightField, index, &weight); 232 233 if (err == B_OK) 234 _LayoutDataForItem(item)->weight = weight; 235 236 return err; 237 } 238 239 161 240 void 162 241 BGroupLayout::ItemAdded(BLayoutItem* item) 163 242 { 164 item->SetLayoutData(new ItemLayoutData);243 item->SetLayoutData(new(nothrow) ItemLayoutData); 165 244 } 166 245 167 // ItemRemoved 246 168 247 void 169 248 BGroupLayout::ItemRemoved(BLayoutItem* item) 170 249 { … … 174 253 } 175 254 } 176 255 177 // PrepareItems 256 178 257 void 179 258 BGroupLayout::PrepareItems(enum orientation orientation) 180 259 { … … 188 267 } 189 268 } 190 269 191 // InternalCountColumns 270 192 271 int32 193 272 BGroupLayout::InternalCountColumns() 194 273 { 195 274 return (fOrientation == B_HORIZONTAL ? fVisibleItems.CountItems() : 1); 196 275 } 197 276 198 // InternalCountRows 277 199 278 int32 200 279 BGroupLayout::InternalCountRows() 201 280 { 202 281 return (fOrientation == B_VERTICAL ? fVisibleItems.CountItems() : 1); 203 282 } 204 283 205 // GetColumnRowConstraints 284 206 285 void 207 286 BGroupLayout::GetColumnRowConstraints(enum orientation orientation, int32 index, 208 287 ColumnRowConstraints* constraints) … … 218 297 } 219 298 } 220 299 221 // ItemDimensions 300 222 301 void 223 302 BGroupLayout::GetItemDimensions(BLayoutItem* item, Dimensions* dimensions) 224 303 { … … 239 318 } 240 319 } 241 320 242 // _LayoutDataForItem 321 243 322 BGroupLayout::ItemLayoutData* 244 323 BGroupLayout::_LayoutDataForItem(BLayoutItem* item) const 245 324 {