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