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