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