xref: /haiku/headers/libs/alm/ALMLayout.h (revision a25ffa4f046fb495c316d29f0645bd00c82751c8)
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 GroupItem;
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 			void				BuildLayout(GroupItem& item, XTab* left = NULL,
90 									YTab* top = NULL, XTab* right = NULL,
91 									YTab* bottom = NULL);
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 
110 	virtual	BSize				BaseMinSize();
111 	virtual	BSize				BaseMaxSize();
112 	virtual	BSize				BasePreferredSize();
113 	virtual	BAlignment			BaseAlignment();
114 
115 protected:
116 	virtual	bool				ItemAdded(BLayoutItem* item, int32 atIndex);
117 	virtual	void				ItemRemoved(BLayoutItem* item, int32 fromIndex);
118 
119 	virtual	void				LayoutInvalidated(bool children);
120 	virtual	void				DoLayout();
121 
122 private:
123 	friend class XTab;
124 	friend class YTab;
125 	friend class Area;
126 
127 			float				InsetForTab(XTab* tab);
128 			float				InsetForTab(YTab* tab);
129 
130 			BLayoutItem*		_LayoutItemToAdd(BView* view);
131 
132 			void				_UpdateAreaConstraints();
133 
134 			BSize				_CalculateMinSize();
135 			BSize				_CalculateMaxSize();
136 			BSize				_CalculatePreferredSize();
137 
138 			void				_ParseGroupItem(GroupItem& item,
139 									BReference<XTab> left, BReference<YTab> top,
140 									BReference<XTab> right,
141 									BReference<YTab> bottom);
142 
143 			LinearProgramming::LinearSpec*	fSolver;
144 			LinearProgramming::LinearSpec	fOwnSolver;
145 
146 			BReference<XTab>	fLeft;
147 			BReference<XTab>	fRight;
148 			BReference<YTab>	fTop;
149 			BReference<YTab>	fBottom;
150 			BSize				fMinSize;
151 			BSize				fMaxSize;
152 			BSize				fPreferredSize;
153 
154 			float				fLeftInset;
155 			float				fRightInset;
156 			float				fTopInset;
157 			float				fBottomInset;
158 
159 			float				fHSpacing;
160 			float				fVSpacing;
161 
162 			XTabList			fXTabList;
163 			YTabList			fYTabList;
164 
165 			RowColumnManager*	fRowColumnManager;
166 };
167 
168 }	// namespace BALM
169 
170 
171 using BALM::BALMLayout;
172 
173 
174 #endif	// ALM_LAYOUT_H
175