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