xref: /haiku/headers/libs/alm/ALMLayout.h (revision 7a74a5df454197933bc6e80a542102362ee98703)
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 : public BArchivable {
144 								BadLayoutPolicy();
145 								BadLayoutPolicy(BMessage* archive);
146 		virtual					~BadLayoutPolicy();
147 		/* return false to abandon layout, true to use layout */
148 		virtual bool			OnBadLayout(BALMLayout* layout,
149 									LinearProgramming::ResultType result,
150 									BLayoutContext* context) = 0;
151 	};
152 
153 	struct DefaultPolicy : public BadLayoutPolicy {
154 								DefaultPolicy();
155 								DefaultPolicy(BMessage* archive);
156 		virtual					~DefaultPolicy();
157 		virtual bool			OnBadLayout(BALMLayout* layout,
158 									LinearProgramming::ResultType result,
159 									BLayoutContext* context);
160 		virtual status_t		Archive(BMessage* message,
161 									bool deep = false) const;
162 		static BArchivable*		Instantiate(BMessage* message);
163 	};
164 
165 private:
166 
167 	// FBC padding
168 	virtual	void				_ReservedALMLayout1();
169 	virtual	void				_ReservedALMLayout2();
170 	virtual	void				_ReservedALMLayout3();
171 	virtual	void				_ReservedALMLayout4();
172 	virtual	void				_ReservedALMLayout5();
173 	virtual	void				_ReservedALMLayout6();
174 	virtual	void				_ReservedALMLayout7();
175 	virtual	void				_ReservedALMLayout8();
176 	virtual	void				_ReservedALMLayout9();
177 	virtual	void				_ReservedALMLayout10();
178 
179 	// forbidden methods
180 								BALMLayout(const BALMLayout&);
181 			void				operator =(const BALMLayout&);
182 
183 private:
184 	template <class T>
185 	struct TabAddTransaction;
186 
187 	template <class T>
188 	friend class TabAddTransaction;
189 	friend class BPrivate::SharedSolver;
190 
191 	friend class XTab;
192 	friend class YTab;
193 	friend class Area;
194 
195 			float				InsetForTab(XTab* tab) const;
196 			float				InsetForTab(YTab* tab) const;
197 
198 			void				UpdateConstraints(BLayoutContext* context);
199 
200 			void				_RemoveSelfFromTab(XTab* tab);
201 			void				_RemoveSelfFromTab(YTab* tab);
202 			bool				_HasTabInLayout(XTab* tab);
203 			bool				_HasTabInLayout(YTab* tab);
204 			bool				_AddedTab(XTab* tab);
205 			bool				_AddedTab(YTab* tab);
206 
207 			BLayoutItem*		_LayoutItemToAdd(BView* view);
208 			void				_SetSolver(BPrivate::SharedSolver* solver);
209 
210 			BPrivate::SharedSolver* fSolver;
211 
212 			BReference<XTab>	fLeft;
213 			BReference<XTab>	fRight;
214 			BReference<YTab>	fTop;
215 			BReference<YTab>	fBottom;
216 			BSize				fMinSize;
217 			BSize				fMaxSize;
218 			BSize				fPreferredSize;
219 
220 			float				fLeftInset;
221 			float				fRightInset;
222 			float				fTopInset;
223 			float				fBottomInset;
224 
225 			float				fHSpacing;
226 			float				fVSpacing;
227 
228 			XTabList			fXTabList;
229 			bool				fXTabsSorted;
230 
231 			YTabList			fYTabList;
232 			bool				fYTabsSorted;
233 
234 			RowColumnManager*	fRowColumnManager;
235 
236 			BadLayoutPolicy*	fBadLayoutPolicy;
237 			uint32				_reserved[5];
238 };
239 
240 }	// namespace BALM
241 
242 
243 using BALM::BALMLayout;
244 
245 
246 #endif	// ALM_LAYOUT_H
247