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