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