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