3 #include "math_xiter.h"
4 #include "math_parinset.h"
5 #include "math_rowst.h"
7 #include "mathed/support.h"
12 string MathedXIter::error_label = "$mathed-error$";
14 MathedXIter::MathedXIter()
15 : MathedIter(), size_(0), x_(0), y_(0), p_(0), sx_(0), sw_(0),
18 // should limits_ be initialized?
22 MathedXIter::MathedXIter(MathParInset * pp)
23 : x_(0), y_(0), p_(pp), sx_(0), sw_(0), limits_(false)
35 MathParInset * MathedXIter::getPar() const
41 void MathedXIter::GetPos(int & xx, int & yy) const
54 int MathedXIter::GetX() const
63 int MathedXIter::GetY() const
72 void MathedXIter::GetIncPos(int & xx, int & yy) const
79 void MathedXIter::getAD(int & a, int & d) const
94 void MathedXIter::Clean(int pos2)
97 lyxerr << "Math error: Attempting to clean a void array." << endl;
105 while (pos < pos2 && OK()) {
113 while (OK() && pos < pos2) {
115 MathedInset * inset = GetInset();
117 if (inset->GetType()!= LM_OT_MACRO_ARG)
123 MathedRowSt * r = crow_->getNext();
125 crow_->setNext(r->getNext());
134 if (pos2 <= array->last()) {
142 void MathedXIter::Merge(MathedArray * a0)
145 lyxerr[Debug::MATHED]
146 << "Math error: Attempting to merge a void array." << endl;
150 // All insets must be clonned
152 MathedArray * a = it.Copy();
155 array->insert(array->begin() + pos,
156 a->begin(), a->end());
158 // make room for the data
160 array->mergeF(a, pos, a->last());
163 int pos2 = pos + a->last();
168 while (pos < pos2 && OK()) {
170 if (p_ && p_->Permit(LMPF_ALLOW_CR)) {
171 MathedRowSt * r = new MathedRowSt(ncols + 1);
173 r->setNext(crow_->getNext());
195 void MathedXIter::SetData(MathParInset * pp)
199 array = p_->GetData();
200 ncols = p_->GetColumns();
201 crow_ = p_->getRowSt();
202 if (p_->Permit(LMPF_ALLOW_CR))
204 if (p_->Permit(LMPF_ALLOW_TAB))
208 x_ = crow_->getTab(0);
209 y_ = crow_->getBaseline();
212 array = new MathedArray; // this leaks
215 size_ = p_->GetStyle();
220 string const MathedXIter::GetString() const
222 string const s = MathedIter::GetString();
223 x_ += mathed_string_width(fcode(), size_, s);
228 bool MathedXIter::Next()
230 // lyxerr << "Ne[" << pos << "]";
231 if (!OK()) return false;
235 MathedInset * px = GetInset();
237 if (px->GetType() == LM_OT_SCRIPT) {
238 if (w > sw_) sw_ = w;
241 sx_ = (px->GetLimits()) ? w : 0;
245 // lyxerr << "WD[" << fcode << " " << size << " " << c << endl;
246 w = mathed_char_width(fcode(), size_, c);
248 if (c == LM_TC_TAB && p_) {
249 // w = p->GetTab(col + 1);
250 w = (crow_) ? crow_->getTab(col + 1) : 0;
251 //lyxerr << "WW[" << w << "]";
253 if (c == LM_TC_CR && p_) {
255 if (crow_ && crow_->getNext()) {
256 crow_ = crow_->getNext();
257 y_ = crow_->getBaseline();
258 w = crow_->getTab(0);
260 // lyxerr << "WW[" << col " " << row << "|" << w << "]";
262 lyxerr << "No hubo w[" << c << "]!";
264 if (MathedIter::Next()) {
265 // lyxerr <<"LNX " << pos << endl;
266 // if (sw>0 && GetChar()!= LM_TC_UP && GetChar()!= LM_TC_DOWN) {
267 // w = (sx>sw) ? 0: sw-sx;
268 if ((sw_ > 0 || sx_ > 0)
269 && GetChar() != LM_TC_UP && GetChar() != LM_TC_DOWN) {
271 w = (sx_ > sw_) ? 0 : sw_ - sx_;
281 void MathedXIter::GoBegin()
287 crow_ = p_->getRowSt();
289 x_ = crow_->getTab(0);
290 y_ = crow_->getBaseline();
296 void MathedXIter::GoLast()
302 void MathedXIter::Adjust()
306 while (posx > pos && OK()) Next();
310 bool MathedXIter::Prev()
312 if (pos == 0 || (pos == 1 && GetChar() >= ' '))
315 int pos2 = pos; // pos1
327 void MathedXIter::goNextColumn()
331 while (Next() && col == colp)
334 //return (col != colp + 1 || rowp != row);
338 bool MathedXIter::Up()
340 if (row == 0) return false;
345 while (row < rowp - 1) Next();
346 while (x_ < xp && OK() && !IsCR()) {
350 if (col > colp) // || (stck.col == colp && stck.x<= xp && x>xp))
357 bool MathedXIter::Down()
363 bool res = (IsCR()) ? true : goNextCode(LM_TC_CR);
367 while (x_ < xp && OK()) {
371 if (col > colp || (stck.col == colp && stck.x <= xp && x_ > xp))
379 void MathedXIter::addRow()
382 lyxerr[Debug::MATHED] << "MathErr: Attempt to insert new"
383 " line in a subparagraph. " << this << endl;
387 // Create new item for the structure
388 MathedRowSt * r = new MathedRowSt(ncols + 1);
390 r->setNext(crow_->getNext());
396 // Fill missed tabs in current row
397 while (col < ncols - 1)
398 insert('T', LM_TC_TAB);
400 insert('K', LM_TC_CR);
404 goNextCode(LM_TC_CR);
406 // Fill missed tabs in new row
407 while (col < ncols - 1)
408 insert('T', LM_TC_TAB);
413 void MathedXIter::delRow()
416 lyxerr[Debug::MATHED] << "MathErr: Attempt to delete a line in a subparagraph." << endl;
419 bool line_empty = true;
425 } else if (!IsTab()) {
429 int const p1 = getPos();
434 MathedRowSt * r = crow_->getNext();
436 crow_->setNext(r->getNext());
448 void MathedXIter::ipush()
456 void MathedXIter::ipop()
462 crow_ = p_->getRowSt();
464 for (int i = 0; i < row; ++i)
465 crow_ = crow_->getNext();
470 void MathedXIter::fitCoord(int /*xx*/, int yy)
478 // first fit vertically
479 while (crow_ && OK()) {
480 if (yy >= yo + y_ - crow_->ascent()
481 && yy <= yo + y_ + crow_->descent())
483 goNextCode(LM_TC_CR);
487 // while (x<xx && Next());
491 void MathedXIter::setTab(int tx, int tab)
493 if (crow_ && tab <= ncols) {
494 crow_->setTab(tab, tx);
496 lyxerr << "MathErr: No tabs allowed here" << endl;
500 void MathedXIter::subMetrics(int a, int d)
503 lyxerr[Debug::MATHED]
504 << "MathErr: Attempt to submetric a subparagraph." << endl;
512 // This function is not recursive, as MathPar::Metrics is
513 void MathedXIter::IMetrics(int pos2, int & width, int & ascent, int & descent)
521 descent = ascent = width = 0;
523 if (array->empty()) return;
524 // if (pos2 > array->last) return;
529 mathed_char_height(fcode(), size_, cx, asc, des);
530 if (asc > ascent) ascent = asc;
531 if (des > descent) descent = des;
534 if (MathIsInset(cx)) {
535 MathedInset * pp = GetInset();
536 if (cx == LM_TC_UP) {
541 static_cast<MathParInset*>(pp)->GetXY(xx, asc);
544 asc += ((limits_) ? pp->Height() + 4 : pp->Ascent());
545 } else if (cx == LM_TC_DOWN) {
550 static_cast<MathParInset*>(pp)->GetXY(xx, des);
551 if (des - pp->Height() < yy && !asc)
552 asc = yy - (des - pp->Height());
555 des += (limit ? pp->Height()+4: pp->Height()-pp->Ascent()/2);
560 if (asc > ascent) ascent = asc;
561 if (des > descent) descent = des;
562 if (cx != LM_TC_UP && cx != LM_TC_DOWN)
563 limit = pp->GetLimits();
564 } else if (cx == LM_TC_TAB) {
567 lyxerr[Debug::MATHED]
568 << "Mathed Sel-Error: Unrecognized code["
569 << cx << ']' << endl;
572 if (pos < pos2) Next();
578 bool MathedXIter::setNumbered(bool numb)
581 crow_->setNumbered(numb);
589 bool MathedXIter::setLabel(string const & label)
592 crow_->setLabel(label);
600 MathedRowSt * MathedXIter::adjustVerticalSt()
604 // lyxerr << " CRW" << ncols << " ";
605 crow_ = new MathedRowSt(ncols + 1); // this leaks
607 // lyxerr<< " CRW[" << crow_ << "] ";
608 MathedRowSt * mrow = crow_;
611 if (col >= ncols) ncols = col + 1;
612 MathedRowSt * r = new MathedRowSt(ncols + 1); // this leaks
613 // r->next = crow_->next;
616 // lyxerr << " CX[" << crow_ << "]";
624 string const & MathedXIter::getLabel() const
626 return crow_ ? crow_->getLabel() : error_label;