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 const & a)
145 array->insert(array->begin() + pos,
148 // make room for the data
150 array->merge(a, pos);
153 int pos2 = pos + a.last();
158 while (pos < pos2 && OK()) {
160 if (p_ && p_->Permit(LMPF_ALLOW_CR)) {
161 MathedRowSt * r = new MathedRowSt(ncols + 1);
163 r->setNext(crow_->getNext());
183 void MathedXIter::SetData(MathParInset * pp)
187 array = &p_->GetData();
188 ncols = p_->GetColumns();
189 crow_ = p_->getRowSt();
190 if (p_->Permit(LMPF_ALLOW_CR))
192 if (p_->Permit(LMPF_ALLOW_TAB))
196 x_ = crow_->getTab(0);
197 y_ = crow_->getBaseline();
200 array = new MathedArray; // this leaks
203 size_ = p_->GetStyle();
208 string const MathedXIter::GetString() const
210 string const s = MathedIter::GetString();
211 x_ += mathed_string_width(fcode(), size_, s);
216 bool MathedXIter::Next()
218 // lyxerr << "Ne[" << pos << "]";
219 if (!OK()) return false;
223 MathedInset * px = GetInset();
225 if (px->GetType() == LM_OT_SCRIPT) {
226 if (w > sw_) sw_ = w;
229 sx_ = (px->GetLimits()) ? w : 0;
233 // lyxerr << "WD[" << fcode << " " << size << " " << c << endl;
234 w = mathed_char_width(fcode(), size_, c);
236 if (c == LM_TC_TAB && p_) {
237 // w = p->GetTab(col + 1);
238 w = (crow_) ? crow_->getTab(col + 1) : 0;
239 //lyxerr << "WW[" << w << "]";
241 if (c == LM_TC_CR && p_) {
243 if (crow_ && crow_->getNext()) {
244 crow_ = crow_->getNext();
245 y_ = crow_->getBaseline();
246 w = crow_->getTab(0);
248 // lyxerr << "WW[" << col " " << row << "|" << w << "]";
250 lyxerr << "No hubo w[" << c << "]!";
252 if (MathedIter::Next()) {
253 // lyxerr <<"LNX " << pos << endl;
254 // if (sw>0 && GetChar()!= LM_TC_UP && GetChar()!= LM_TC_DOWN) {
255 // w = (sx>sw) ? 0: sw-sx;
256 if ((sw_ > 0 || sx_ > 0)
257 && GetChar() != LM_TC_UP && GetChar() != LM_TC_DOWN) {
259 w = (sx_ > sw_) ? 0 : sw_ - sx_;
269 void MathedXIter::GoBegin()
275 crow_ = p_->getRowSt();
277 x_ = crow_->getTab(0);
278 y_ = crow_->getBaseline();
284 void MathedXIter::GoLast()
290 void MathedXIter::Adjust()
294 while (posx > pos && OK()) Next();
298 bool MathedXIter::Prev()
300 if (pos == 0 || (pos == 1 && GetChar() >= ' '))
303 int pos2 = pos; // pos1
315 void MathedXIter::goNextColumn()
319 while (Next() && col == colp)
322 //return (col != colp + 1 || rowp != row);
326 bool MathedXIter::Up()
328 if (row == 0) return false;
333 while (row < rowp - 1) Next();
334 while (x_ < xp && OK() && !IsCR()) {
338 if (col > colp) // || (stck.col == colp && stck.x<= xp && x>xp))
345 bool MathedXIter::Down()
351 bool res = (IsCR()) ? true : goNextCode(LM_TC_CR);
355 while (x_ < xp && OK()) {
359 if (col > colp || (stck.col == colp && stck.x <= xp && x_ > xp))
367 void MathedXIter::addRow()
370 lyxerr[Debug::MATHED] << "MathErr: Attempt to insert new"
371 " line in a subparagraph. " << this << endl;
375 // Create new item for the structure
376 MathedRowSt * r = new MathedRowSt(ncols + 1);
378 r->setNext(crow_->getNext());
384 // Fill missed tabs in current row
385 while (col < ncols - 1)
386 insert('T', LM_TC_TAB);
388 insert('K', LM_TC_CR);
392 goNextCode(LM_TC_CR);
394 // Fill missed tabs in new row
395 while (col < ncols - 1)
396 insert('T', LM_TC_TAB);
401 void MathedXIter::delRow()
404 lyxerr[Debug::MATHED] << "MathErr: Attempt to delete a line in a subparagraph." << endl;
407 bool line_empty = true;
413 } else if (!IsTab()) {
417 int const p1 = getPos();
422 MathedRowSt * r = crow_->getNext();
424 crow_->setNext(r->getNext());
436 void MathedXIter::ipush()
444 void MathedXIter::ipop()
450 crow_ = p_->getRowSt();
452 for (int i = 0; i < row; ++i)
453 crow_ = crow_->getNext();
458 void MathedXIter::fitCoord(int /*xx*/, int yy)
466 // first fit vertically
467 while (crow_ && OK()) {
468 if (yy >= yo + y_ - crow_->ascent()
469 && yy <= yo + y_ + crow_->descent())
471 goNextCode(LM_TC_CR);
475 // while (x<xx && Next());
479 void MathedXIter::setTab(int tx, int tab)
481 if (crow_ && tab <= ncols) {
482 crow_->setTab(tab, tx);
484 lyxerr << "MathErr: No tabs allowed here" << endl;
488 void MathedXIter::subMetrics(int a, int d)
491 lyxerr[Debug::MATHED]
492 << "MathErr: Attempt to submetric a subparagraph." << endl;
500 // This function is not recursive, as MathPar::Metrics is
501 void MathedXIter::IMetrics(int pos2, int & width, int & ascent, int & descent)
509 descent = ascent = width = 0;
511 if (array->empty()) return;
512 // if (pos2 > array->last) return;
517 mathed_char_height(fcode(), size_, cx, asc, des);
518 if (asc > ascent) ascent = asc;
519 if (des > descent) descent = des;
522 if (MathIsInset(cx)) {
523 MathedInset * pp = GetInset();
524 if (cx == LM_TC_UP) {
529 static_cast<MathParInset*>(pp)->GetXY(xx, asc);
532 asc += ((limits_) ? pp->Height() + 4 : pp->Ascent());
533 } else if (cx == LM_TC_DOWN) {
538 static_cast<MathParInset*>(pp)->GetXY(xx, des);
539 if (des - pp->Height() < yy && !asc)
540 asc = yy - (des - pp->Height());
543 des += (limit ? pp->Height()+4: pp->Height()-pp->Ascent()/2);
548 if (asc > ascent) ascent = asc;
549 if (des > descent) descent = des;
550 if (cx != LM_TC_UP && cx != LM_TC_DOWN)
551 limit = pp->GetLimits();
552 } else if (cx == LM_TC_TAB) {
555 lyxerr[Debug::MATHED]
556 << "Mathed Sel-Error: Unrecognized code["
557 << cx << ']' << endl;
560 if (pos < pos2) Next();
566 bool MathedXIter::setNumbered(bool numb)
569 crow_->setNumbered(numb);
577 bool MathedXIter::setLabel(string const & label)
580 crow_->setLabel(label);
588 MathedRowSt * MathedXIter::adjustVerticalSt()
592 // lyxerr << " CRW" << ncols << " ";
593 crow_ = new MathedRowSt(ncols + 1); // this leaks
595 // lyxerr<< " CRW[" << crow_ << "] ";
596 MathedRowSt * mrow = crow_;
599 if (col >= ncols) ncols = col + 1;
600 MathedRowSt * r = new MathedRowSt(ncols + 1); // this leaks
601 // r->next = crow_->next;
604 // lyxerr << " CX[" << crow_ << "]";
612 string const & MathedXIter::getLabel() const
614 return crow_ ? crow_->getLabel() : error_label;