xref: /haiku/src/libs/alm/Column.cpp (revision 16d5c24e533eb14b7b8a99ee9f3ec9ba66335b1e)
1 /*
2  * Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz
3  * Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz
4  * Distributed under the terms of the MIT License.
5  */
6 
7 #include "Column.h"
8 #include "BALMLayout.h"
9 #include "OperatorType.h"
10 #include "XTab.h"
11 
12 #include <SupportDefs.h>
13 
14 
15 /**
16  * The left boundary of the column.
17  */
18 XTab*
19 Column::Left() const
20 {
21 	return fLeft;
22 }
23 
24 
25 /**
26  * The right boundary of the column.
27  */
28 XTab*
29 Column::Right() const
30 {
31 	return fRight;
32 }
33 
34 
35 /**
36  * Gets the column directly to the left of this column.
37  */
38 Column*
39 Column::Previous() const
40 {
41 	return fPrevious;
42 }
43 
44 
45 /**
46  * Sets the column directly to the left of this column.
47  * May be null.
48  */
49 void
50 Column::SetPrevious(Column* value)
51 {
52 	// if there should be no column directly left of this column, then we have to
53 	// separate any such column and can remove any constraint that was used
54 	// to glue this column to it
55 	if (value == NULL) {
56 		if (fPrevious == NULL) return;
57 		fPrevious->fNext = NULL;
58 		fPrevious->fNextGlue = NULL;
59 		fPrevious = NULL;
60 		delete fPreviousGlue;
61 		fPreviousGlue = NULL;
62 		return;
63 	}
64 
65 	// otherwise we have to set up the pointers and the glue constraint accordingly
66 	if (value->fNext != NULL)
67 		value->SetNext(NULL);
68 	if (fPrevious != NULL)
69 		SetPrevious(NULL);
70 
71 	fPrevious = value;
72 	fPrevious->fNext = this;
73 	value->fNextGlue = value->fRight->IsEqual(fLeft);
74 	fPreviousGlue = value->fNextGlue;
75 }
76 
77 
78 /**
79  * Gets the column directly to the right of this column.
80  */
81 Column*
82 Column::Next() const
83 {
84 	return fNext;
85 }
86 
87 
88 /**
89  * Sets the column directly to the right of this column.
90  * May be null.
91  */
92 void
93 Column::SetNext(Column* value)
94 {
95 	// if there should be no column directly right of this column, then we have to
96 	// separate any such column and can remove any constraint that was used
97 	// to glue this column to it
98 	if (value == NULL) {
99 		if (fNext == NULL) return;
100 		fNext->fPrevious = NULL;
101 		fNext->fPreviousGlue = NULL;
102 		fNext = NULL;
103 		delete fNextGlue;
104 		fNextGlue = NULL;
105 		return;
106 	}
107 
108 	// otherwise we have to set up the pointers and the glue constraint accordingly
109 	if (value->fPrevious != NULL)
110 		value->SetPrevious(NULL);
111 	if (fNext != NULL)
112 		SetNext(NULL);
113 
114 	fNext = value;
115 	fNext->fPrevious = this;
116 	value->fPreviousGlue = fRight->IsEqual(value->fLeft);
117 	fNextGlue = value->fPreviousGlue;
118 }
119 
120 
121 //~ string Column::ToString() {
122 	//~ return "Column(" + fLeft.ToString() + ", " + fRight.ToString() + ")";
123 //~ }
124 
125 
126 /**
127  * Inserts the given column directly to the left of this column.
128  *
129  * @param column	the column to insert
130  */
131 void
132 Column::InsertBefore(Column* column)
133 {
134 	SetPrevious(column->fPrevious);
135 	SetNext(column);
136 }
137 
138 
139 /**
140  * Inserts the given column directly to the right of this column.
141  *
142  * @param column	the column to insert
143  */
144 void
145 Column::InsertAfter(Column* column)
146 {
147 	SetNext(column->fNext);
148 	SetPrevious(column);
149 }
150 
151 
152 /**
153  * Constrains this column to have the same width as the given column.
154  *
155  * @param column	the column that should have the same width
156  * @return the resulting same-width constraint
157  */
158 Constraint*
159 Column::HasSameWidthAs(Column* column)
160 {
161 	Constraint* constraint = fLS->AddConstraint(
162 		-1.0, fLeft, 1.0, fRight, 1.0, column->fLeft, -1.0, column->fRight,
163 		OperatorType(EQ), 0.0);
164 	fConstraints->AddItem(constraint);
165 	return constraint;
166 }
167 
168 
169 /**
170  * Gets the constraints.
171  */
172 BList*
173 Column::Constraints() const
174 {
175 	return fConstraints;
176 }
177 
178 
179 /**
180  * Sets the constraints.
181  */
182 void
183 Column::SetConstraints(BList* constraints)
184 {
185 	fConstraints = constraints;
186 }
187 
188 
189 /**
190  * Destructor.
191  * Removes the column from the specification.
192  */
193 Column::~Column()
194 {
195 	if (fPrevious != NULL)
196 		fPrevious->SetNext(fNext);
197 	for (int32 i = 0; i < fConstraints->CountItems(); i++)
198 		delete (Constraint*)fConstraints->ItemAt(i);
199 	delete fLeft;
200 	delete fRight;
201 }
202 
203 
204 /**
205  * Constructor.
206  */
207 Column::Column(BALMLayout* ls)
208 {
209 	fLS = ls;
210 	fLeft = new XTab(ls);
211 	fRight = new XTab(ls);
212 	fConstraints = new BList(1);
213 }
214 
215