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 virtual ~BALMLayout(); 43 44 BReference<XTab> AddXTab(); 45 void AddXTabs(BReference<XTab>* tabs, uint32 count); 46 BReference<YTab> AddYTab(); 47 void AddYTabs(BReference<YTab>* tabs, uint32 count); 48 49 int32 CountXTabs() const; 50 int32 CountYTabs() const; 51 XTab* XTabAt(int32 index, bool ordered = false); 52 XTab* XTabAt(int32 index) const; 53 YTab* YTabAt(int32 index, bool ordered = false); 54 YTab* YTabAt(int32 index) const; 55 56 int32 IndexOf(XTab* tab, bool ordered = false); 57 int32 IndexOf(YTab* tab, bool ordered = false); 58 59 Row* AddRow(YTab* top, YTab* bottom); 60 Column* AddColumn(XTab* left, XTab* right); 61 62 XTab* Left() const; 63 XTab* Right() const; 64 YTab* Top() const; 65 YTab* Bottom() const; 66 67 LinearProgramming::LinearSpec* Solver() const; 68 69 void SetInsets(float insets); 70 void SetInsets(float x, float y); 71 void SetInsets(float left, float top, float right, 72 float bottom); 73 void GetInsets(float* left, float* top, float* right, 74 float* bottom) const; 75 76 void SetSpacing(float hSpacing, float vSpacing); 77 void GetSpacing(float* _hSpacing, 78 float* _vSpacing) const; 79 80 Area* AreaFor(int32 id) const; 81 Area* AreaFor(const BView* view) const; 82 Area* AreaFor(const BLayoutItem* item) const; 83 int32 CountAreas() const; 84 Area* AreaAt(int32 index) const; 85 86 XTab* LeftOf(const BView* view) const; 87 XTab* LeftOf(const BLayoutItem* item) const; 88 XTab* RightOf(const BView* view) const; 89 XTab* RightOf(const BLayoutItem* item) const; 90 YTab* TopOf(const BView* view) const; 91 YTab* TopOf(const BLayoutItem* item) const; 92 YTab* BottomOf(const BView* view) const; 93 YTab* BottomOf(const BLayoutItem* item) const; 94 95 virtual BLayoutItem* AddView(BView* child); 96 virtual BLayoutItem* AddView(int32 index, BView* child); 97 virtual Area* AddView(BView* view, XTab* left, YTab* top, 98 XTab* right = NULL, YTab* bottom = NULL); 99 virtual Area* AddView(BView* view, Row* row, Column* column); 100 101 virtual bool AddItem(BLayoutItem* item); 102 virtual bool AddItem(int32 index, BLayoutItem* item); 103 virtual Area* AddItem(BLayoutItem* item, XTab* left, 104 YTab* top, XTab* right = NULL, 105 YTab* bottom = NULL); 106 virtual Area* AddItem(BLayoutItem* item, Row* row, 107 Column* column); 108 109 bool SaveLayout(BMessage* archive) const; 110 bool RestoreLayout(const BMessage* archive); 111 struct BadLayoutPolicy; 112 113 void SetBadLayoutPolicy(BadLayoutPolicy* policy); 114 BadLayoutPolicy* GetBadLayoutPolicy() const; 115 116 virtual BSize BaseMinSize(); 117 virtual BSize BaseMaxSize(); 118 virtual BSize BasePreferredSize(); 119 virtual BAlignment BaseAlignment(); 120 121 virtual status_t Perform(perform_code d, void* arg); 122 123 protected: 124 virtual bool ItemAdded(BLayoutItem* item, int32 atIndex); 125 virtual void ItemRemoved(BLayoutItem* item, int32 fromIndex); 126 127 virtual void LayoutInvalidated(bool children); 128 virtual void DoLayout(); 129 130 public: 131 struct BadLayoutPolicy { 132 virtual ~BadLayoutPolicy(); 133 /* return false to abandon layout, true to use layout */ 134 virtual bool OnBadLayout(BALMLayout* layout, 135 LinearProgramming::ResultType result, BLayoutContext* context) = 0; 136 }; 137 138 struct DefaultPolicy : public BadLayoutPolicy { 139 virtual ~DefaultPolicy(); 140 virtual bool OnBadLayout(BALMLayout* layout, 141 LinearProgramming::ResultType result, BLayoutContext* context); 142 }; 143 144 private: 145 146 // FBC padding 147 virtual void _ReservedALMLayout1(); 148 virtual void _ReservedALMLayout2(); 149 virtual void _ReservedALMLayout3(); 150 virtual void _ReservedALMLayout4(); 151 virtual void _ReservedALMLayout5(); 152 virtual void _ReservedALMLayout6(); 153 virtual void _ReservedALMLayout7(); 154 virtual void _ReservedALMLayout8(); 155 virtual void _ReservedALMLayout9(); 156 virtual void _ReservedALMLayout10(); 157 158 // forbidden methods 159 BALMLayout(const BALMLayout&); 160 void operator =(const BALMLayout&); 161 162 private: 163 template <class T> 164 struct TabAddTransaction; 165 166 template <class T> 167 friend class TabAddTransaction; 168 friend class BPrivate::SharedSolver; 169 170 friend class XTab; 171 friend class YTab; 172 friend class Area; 173 174 float InsetForTab(XTab* tab) const; 175 float InsetForTab(YTab* tab) const; 176 177 void UpdateConstraints(BLayoutContext* context); 178 179 void _RemoveSelfFromTab(XTab* tab); 180 void _RemoveSelfFromTab(YTab* tab); 181 bool _HasTabInLayout(XTab* tab); 182 bool _HasTabInLayout(YTab* tab); 183 bool _AddedTab(XTab* tab); 184 bool _AddedTab(YTab* tab); 185 186 BLayoutItem* _LayoutItemToAdd(BView* view); 187 188 BPrivate::SharedSolver* fSolver; 189 190 BReference<XTab> fLeft; 191 BReference<XTab> fRight; 192 BReference<YTab> fTop; 193 BReference<YTab> fBottom; 194 BSize fMinSize; 195 BSize fMaxSize; 196 BSize fPreferredSize; 197 198 float fLeftInset; 199 float fRightInset; 200 float fTopInset; 201 float fBottomInset; 202 203 float fHSpacing; 204 float fVSpacing; 205 206 XTabList fXTabList; 207 bool fXTabsSorted; 208 209 YTabList fYTabList; 210 bool fYTabsSorted; 211 212 RowColumnManager* fRowColumnManager; 213 214 BadLayoutPolicy* fBadLayoutPolicy; 215 uint32 _reserved[5]; 216 }; 217 218 } // namespace BALM 219 220 221 using BALM::BALMLayout; 222 223 224 #endif // ALM_LAYOUT_H 225