xref: /haiku/headers/libs/alm/ALMLayout.h (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
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 
21 namespace LinearProgramming {
22 	class Constraint;
23 };
24 
25 
26 namespace BPrivate {
27 	class SharedSolver;
28 };
29 
30 
31 namespace BALM {
32 
33 
34 class Column;
35 class ALMGroup;
36 class Row;
37 class RowColumnManager;
38 
39 
40 /*!
41  * A GUI layout engine using the Auckland Layout Model (ALM).
42  */
43 class BALMLayout : public BAbstractLayout {
44 public:
45 								BALMLayout(float hSpacing = 0.0f,
46 									float vSpacing = 0.0f,
47 									BALMLayout* friendLayout = NULL);
48 								BALMLayout(BMessage* archive);
49 	virtual						~BALMLayout();
50 
51 			BReference<XTab>	AddXTab();
52 			void				AddXTabs(BReference<XTab>* tabs, uint32 count);
53 			BReference<YTab>	AddYTab();
54 			void				AddYTabs(BReference<YTab>* tabs, uint32 count);
55 
56 			int32				CountXTabs() const;
57 			int32				CountYTabs() const;
58 			XTab*				XTabAt(int32 index, bool ordered = false);
59 			XTab*				XTabAt(int32 index) const;
60 			YTab*				YTabAt(int32 index, bool ordered = false);
61 			YTab*				YTabAt(int32 index) const;
62 	const	XTabList			GetXTabs() const;
63 	const	YTabList			GetYTabs() const;
64 
65 			int32				IndexOf(XTab* tab, bool ordered = false);
66 			int32				IndexOf(YTab* tab, bool ordered = false);
67 
68 			int32				CountConstraints() const;
69 			Constraint*			ConstraintAt(int32 index) const;
70 			bool				AddConstraint(Constraint* constraint);
71 			bool				RemoveConstraint(Constraint* constraint,
72 									bool deleteConstraint = true);
73 
74 			Constraint*			AddConstraint(double coeff1, Variable* var1,
75 									OperatorType op, double rightSide,
76 									double penaltyNeg = -1,
77 									double penaltyPos = -1);
78 			Constraint*			AddConstraint(double coeff1, Variable* var1,
79 									double coeff2, Variable* var2,
80 									OperatorType op, double rightSide,
81 									double penaltyNeg = -1,
82 									double penaltyPos = -1);
83 			Constraint*			AddConstraint(double coeff1, Variable* var1,
84 									double coeff2, Variable* var2,
85 									double coeff3, Variable* var3,
86 									OperatorType op, double rightSide,
87 									double penaltyNeg = -1,
88 									double penaltyPos = -1);
89 			Constraint*			AddConstraint(double coeff1, Variable* var1,
90 									double coeff2, Variable* var2,
91 									double coeff3, Variable* var3,
92 									double coeff4, Variable* var4,
93 									OperatorType op, double rightSide,
94 									double penaltyNeg = -1,
95 									double penaltyPos = -1);
96 
97 			Row*				AddRow(YTab* top, YTab* bottom);
98 			Column*				AddColumn(XTab* left, XTab* right);
99 
100 			XTab*				Left() const;
101 			XTab*				Right() const;
102 			YTab*				Top() const;
103 			YTab*				Bottom() const;
104 
105 			LinearProgramming::LinearSpec* Solver() const;
106 			LinearProgramming::ResultType ValidateLayout();
107 
108 			void				SetInsets(float insets);
109 			void				SetInsets(float x, float y);
110 			void				SetInsets(float left, float top, float right,
111 									float bottom);
112 			void				GetInsets(float* left, float* top, float* right,
113 									float* bottom) const;
114 
115 			void				SetSpacing(float hSpacing, float vSpacing);
116 			void				GetSpacing(float* _hSpacing,
117 									float* _vSpacing) const;
118 
119 			Area*				AreaFor(int32 id) const;
120 			Area*				AreaFor(const BView* view) const;
121 			Area*				AreaFor(const BLayoutItem* item) const;
122 			int32				CountAreas() const;
123 			Area*				AreaAt(int32 index) const;
124 
125 			XTab*				LeftOf(const BView* view) const;
126 			XTab*				LeftOf(const BLayoutItem* item) const;
127 			XTab*				RightOf(const BView* view) const;
128 			XTab*				RightOf(const BLayoutItem* item) const;
129 			YTab*				TopOf(const BView* view) const;
130 			YTab*				TopOf(const BLayoutItem* item) const;
131 			YTab*				BottomOf(const BView* view) const;
132 			YTab*				BottomOf(const BLayoutItem* item) const;
133 
134 	virtual	BLayoutItem*		AddView(BView* child);
135 	virtual	BLayoutItem*		AddView(int32 index, BView* child);
136 	virtual	Area*				AddView(BView* view, XTab* left, YTab* top,
137 									XTab* right = NULL, YTab* bottom = NULL);
138 	virtual	Area*				AddView(BView* view, Row* row, Column* column);
139 
140 	virtual	bool				AddItem(BLayoutItem* item);
141 	virtual	bool				AddItem(int32 index, BLayoutItem* item);
142 	virtual	Area*				AddItem(BLayoutItem* item, XTab* left,
143 									YTab* top, XTab* right = NULL,
144 									YTab* bottom = NULL);
145 	virtual	Area*				AddItem(BLayoutItem* item, Row* row,
146 									Column* column);
147 
148 	struct BadLayoutPolicy;
149 
150 			void				SetBadLayoutPolicy(BadLayoutPolicy* policy);
151 			BadLayoutPolicy*	GetBadLayoutPolicy() const;
152 
153 	virtual	BSize				BaseMinSize();
154 	virtual	BSize				BaseMaxSize();
155 	virtual	BSize				BasePreferredSize();
156 	virtual	BAlignment			BaseAlignment();
157 
158 	virtual	status_t 			Archive(BMessage* into, bool deep = true) const;
159 	static 	BArchivable*		Instantiate(BMessage* archive);
160 
161 	virtual	status_t			Perform(perform_code d, void* arg);
162 
163 protected:
164 	virtual	bool				ItemAdded(BLayoutItem* item, int32 atIndex);
165 	virtual	void				ItemRemoved(BLayoutItem* item, int32 fromIndex);
166 
167 	virtual status_t			ItemArchived(BMessage* into, BLayoutItem* item,
168 									int32 index) const;
169 	virtual	status_t			ItemUnarchived(const BMessage* from,
170 									BLayoutItem* item, int32 index);
171 
172 	virtual	status_t 			AllUnarchived(const BMessage* archive);
173 	virtual	status_t 			AllArchived(BMessage* archive) const;
174 
175 	virtual	void				LayoutInvalidated(bool children);
176 	virtual	void				DoLayout();
177 
178 public:
179 	struct BadLayoutPolicy : public BArchivable {
180 								BadLayoutPolicy();
181 								BadLayoutPolicy(BMessage* archive);
182 		virtual					~BadLayoutPolicy();
183 		/* return false to abandon layout, true to use layout */
184 		virtual bool			OnBadLayout(BALMLayout* layout,
185 									LinearProgramming::ResultType result,
186 									BLayoutContext* context) = 0;
187 	};
188 
189 	struct DefaultPolicy : public BadLayoutPolicy {
190 								DefaultPolicy();
191 								DefaultPolicy(BMessage* archive);
192 		virtual					~DefaultPolicy();
193 		virtual bool			OnBadLayout(BALMLayout* layout,
194 									LinearProgramming::ResultType result,
195 									BLayoutContext* context);
196 		virtual status_t		Archive(BMessage* message,
197 									bool deep = false) const;
198 		static BArchivable*		Instantiate(BMessage* message);
199 	};
200 
201 private:
202 
203 	// FBC padding
204 	virtual	void				_ReservedALMLayout1();
205 	virtual	void				_ReservedALMLayout2();
206 	virtual	void				_ReservedALMLayout3();
207 	virtual	void				_ReservedALMLayout4();
208 	virtual	void				_ReservedALMLayout5();
209 	virtual	void				_ReservedALMLayout6();
210 	virtual	void				_ReservedALMLayout7();
211 	virtual	void				_ReservedALMLayout8();
212 	virtual	void				_ReservedALMLayout9();
213 	virtual	void				_ReservedALMLayout10();
214 
215 	// forbidden methods
216 								BALMLayout(const BALMLayout&);
217 			void				operator =(const BALMLayout&);
218 
219 private:
220 	template <class T>
221 	struct TabAddTransaction;
222 
223 	template <class T>
224 	friend class TabAddTransaction;
225 	friend class BPrivate::SharedSolver;
226 
227 	friend class XTab;
228 	friend class YTab;
229 	friend class Area;
230 
231 			class BALMLayoutSpecListener;
232 	friend	class BALMLayoutSpecListener;
233 
234 			float				InsetForTab(XTab* tab) const;
235 			float				InsetForTab(YTab* tab) const;
236 
237 			void				UpdateConstraints(BLayoutContext* context);
238 
239 			void				_RemoveSelfFromTab(XTab* tab);
240 			void				_RemoveSelfFromTab(YTab* tab);
241 			bool				_HasTabInLayout(XTab* tab);
242 			bool				_HasTabInLayout(YTab* tab);
243 			bool				_AddedTab(XTab* tab);
244 			bool				_AddedTab(YTab* tab);
245 
246 			BLayoutItem*		_LayoutItemToAdd(BView* view);
247 			void				_SetSolver(BPrivate::SharedSolver* solver);
248 
249 			BPrivate::SharedSolver* fSolver;
250 			BALMLayoutSpecListener* fSpecListener;
251 
252 			BReference<XTab>	fLeft;
253 			BReference<XTab>	fRight;
254 			BReference<YTab>	fTop;
255 			BReference<YTab>	fBottom;
256 			BSize				fMinSize;
257 			BSize				fMaxSize;
258 			BSize				fPreferredSize;
259 
260 			float				fLeftInset;
261 			float				fRightInset;
262 			float				fTopInset;
263 			float				fBottomInset;
264 
265 			float				fHSpacing;
266 			float				fVSpacing;
267 
268 			XTabList			fXTabList;
269 			bool				fXTabsSorted;
270 
271 			YTabList			fYTabList;
272 			bool				fYTabsSorted;
273 
274 			BObjectList<Constraint>	fConstraints;
275 
276 			RowColumnManager*	fRowColumnManager;
277 
278 			BadLayoutPolicy*	fBadLayoutPolicy;
279 
280 			uint32				_reserved[5];
281 };
282 
283 }	// namespace BALM
284 
285 
286 using BALM::BALMLayout;
287 
288 
289 #endif	// ALM_LAYOUT_H
290