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