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