1 /* 2 * Copyright 2006 - 2010, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef ALM_LAYOUT_H 6 #define ALM_LAYOUT_H 7 8 9 #include <AbstractLayout.h> 10 #include <Size.h> 11 12 #include "Area.h" 13 #include "LinearSpec.h" 14 #include "Tab.h" 15 16 17 class BView; 18 class BLayoutItem; 19 20 namespace BPrivate { 21 class SharedSolver; 22 }; 23 24 25 namespace BALM { 26 27 28 class Column; 29 class ALMGroup; 30 class Row; 31 class RowColumnManager; 32 33 34 /*! 35 * A GUI layout engine using the Auckland Layout Model (ALM). 36 */ 37 class BALMLayout : public BAbstractLayout { 38 public: 39 BALMLayout(float hSpacing = 0.0f, 40 float vSpacing = 0.0f, 41 BALMLayout* friendLayout = NULL); 42 BALMLayout(BMessage* archive); 43 virtual ~BALMLayout(); 44 45 BReference<XTab> AddXTab(); 46 void AddXTabs(BReference<XTab>* tabs, uint32 count); 47 BReference<YTab> AddYTab(); 48 void AddYTabs(BReference<YTab>* tabs, uint32 count); 49 50 int32 CountXTabs() const; 51 int32 CountYTabs() const; 52 XTab* XTabAt(int32 index, bool ordered = false); 53 XTab* XTabAt(int32 index) const; 54 YTab* YTabAt(int32 index, bool ordered = false); 55 YTab* YTabAt(int32 index) const; 56 57 int32 IndexOf(XTab* tab, bool ordered = false); 58 int32 IndexOf(YTab* tab, bool ordered = false); 59 60 Row* AddRow(YTab* top, YTab* bottom); 61 Column* AddColumn(XTab* left, XTab* right); 62 63 XTab* Left() const; 64 XTab* Right() const; 65 YTab* Top() const; 66 YTab* Bottom() const; 67 68 LinearProgramming::LinearSpec* Solver() const; 69 70 void SetInsets(float insets); 71 void SetInsets(float x, float y); 72 void SetInsets(float left, float top, float right, 73 float bottom); 74 void GetInsets(float* left, float* top, float* right, 75 float* bottom) const; 76 77 void SetSpacing(float hSpacing, float vSpacing); 78 void GetSpacing(float* _hSpacing, 79 float* _vSpacing) const; 80 81 Area* AreaFor(int32 id) const; 82 Area* AreaFor(const BView* view) const; 83 Area* AreaFor(const BLayoutItem* item) const; 84 int32 CountAreas() const; 85 Area* AreaAt(int32 index) const; 86 87 XTab* LeftOf(const BView* view) const; 88 XTab* LeftOf(const BLayoutItem* item) const; 89 XTab* RightOf(const BView* view) const; 90 XTab* RightOf(const BLayoutItem* item) const; 91 YTab* TopOf(const BView* view) const; 92 YTab* TopOf(const BLayoutItem* item) const; 93 YTab* BottomOf(const BView* view) const; 94 YTab* BottomOf(const BLayoutItem* item) const; 95 96 virtual BLayoutItem* AddView(BView* child); 97 virtual BLayoutItem* AddView(int32 index, BView* child); 98 virtual Area* AddView(BView* view, XTab* left, YTab* top, 99 XTab* right = NULL, YTab* bottom = NULL); 100 virtual Area* AddView(BView* view, Row* row, Column* column); 101 102 virtual bool AddItem(BLayoutItem* item); 103 virtual bool AddItem(int32 index, BLayoutItem* item); 104 virtual Area* AddItem(BLayoutItem* item, XTab* left, 105 YTab* top, XTab* right = NULL, 106 YTab* bottom = NULL); 107 virtual Area* AddItem(BLayoutItem* item, Row* row, 108 Column* column); 109 110 bool SaveLayout(BMessage* archive) const; 111 bool RestoreLayout(const BMessage* archive); 112 struct BadLayoutPolicy; 113 114 void SetBadLayoutPolicy(BadLayoutPolicy* policy); 115 BadLayoutPolicy* GetBadLayoutPolicy() const; 116 117 virtual BSize BaseMinSize(); 118 virtual BSize BaseMaxSize(); 119 virtual BSize BasePreferredSize(); 120 virtual BAlignment BaseAlignment(); 121 122 virtual status_t Archive(BMessage* into, bool deep = true) const; 123 static BArchivable* Instantiate(BMessage* archive); 124 125 virtual status_t Perform(perform_code d, void* arg); 126 127 protected: 128 virtual bool ItemAdded(BLayoutItem* item, int32 atIndex); 129 virtual void ItemRemoved(BLayoutItem* item, int32 fromIndex); 130 131 virtual status_t ItemArchived(BMessage* into, BLayoutItem* item, 132 int32 index) const; 133 virtual status_t ItemUnarchived(const BMessage* from, 134 BLayoutItem* item, int32 index); 135 136 virtual status_t AllUnarchived(const BMessage* archive); 137 virtual status_t AllArchived(BMessage* archive) const; 138 139 virtual void LayoutInvalidated(bool children); 140 virtual void DoLayout(); 141 142 public: 143 struct BadLayoutPolicy { 144 virtual ~BadLayoutPolicy(); 145 /* return false to abandon layout, true to use layout */ 146 virtual bool OnBadLayout(BALMLayout* layout, 147 LinearProgramming::ResultType result, BLayoutContext* context) = 0; 148 }; 149 150 struct DefaultPolicy : public BadLayoutPolicy { 151 virtual ~DefaultPolicy(); 152 virtual bool OnBadLayout(BALMLayout* layout, 153 LinearProgramming::ResultType result, BLayoutContext* context); 154 }; 155 156 private: 157 158 // FBC padding 159 virtual void _ReservedALMLayout1(); 160 virtual void _ReservedALMLayout2(); 161 virtual void _ReservedALMLayout3(); 162 virtual void _ReservedALMLayout4(); 163 virtual void _ReservedALMLayout5(); 164 virtual void _ReservedALMLayout6(); 165 virtual void _ReservedALMLayout7(); 166 virtual void _ReservedALMLayout8(); 167 virtual void _ReservedALMLayout9(); 168 virtual void _ReservedALMLayout10(); 169 170 // forbidden methods 171 BALMLayout(const BALMLayout&); 172 void operator =(const BALMLayout&); 173 174 private: 175 template <class T> 176 struct TabAddTransaction; 177 178 template <class T> 179 friend class TabAddTransaction; 180 friend class BPrivate::SharedSolver; 181 182 friend class XTab; 183 friend class YTab; 184 friend class Area; 185 186 float InsetForTab(XTab* tab) const; 187 float InsetForTab(YTab* tab) const; 188 189 void UpdateConstraints(BLayoutContext* context); 190 191 void _RemoveSelfFromTab(XTab* tab); 192 void _RemoveSelfFromTab(YTab* tab); 193 bool _HasTabInLayout(XTab* tab); 194 bool _HasTabInLayout(YTab* tab); 195 bool _AddedTab(XTab* tab); 196 bool _AddedTab(YTab* tab); 197 198 BLayoutItem* _LayoutItemToAdd(BView* view); 199 void _SetSolver(BPrivate::SharedSolver* solver); 200 201 BPrivate::SharedSolver* fSolver; 202 203 BReference<XTab> fLeft; 204 BReference<XTab> fRight; 205 BReference<YTab> fTop; 206 BReference<YTab> fBottom; 207 BSize fMinSize; 208 BSize fMaxSize; 209 BSize fPreferredSize; 210 211 float fLeftInset; 212 float fRightInset; 213 float fTopInset; 214 float fBottomInset; 215 216 float fHSpacing; 217 float fVSpacing; 218 219 XTabList fXTabList; 220 bool fXTabsSorted; 221 222 YTabList fYTabList; 223 bool fYTabsSorted; 224 225 RowColumnManager* fRowColumnManager; 226 227 BadLayoutPolicy* fBadLayoutPolicy; 228 uint32 _reserved[5]; 229 }; 230 231 } // namespace BALM 232 233 234 using BALM::BALMLayout; 235 236 237 #endif // ALM_LAYOUT_H 238