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), crow_()
34 MathParInset * MathedXIter::getPar() const
39 MathedRowContainer & MathedXIter::container()
42 return p_->getRowSt();
46 void MathedXIter::GetPos(int & xx, int & yy) const
59 int MathedXIter::GetX() const
68 int MathedXIter::GetY() const
77 void MathedXIter::GetIncPos(int & xx, int & yy) const
84 void MathedXIter::getAD(int & a, int & d) const
99 void MathedXIter::Clean(int pos2)
102 lyxerr << "Math error: Attempting to clean a void array." << endl;
110 while (pos < pos2 && OK()) {
118 while (OK() && pos < pos2) {
120 MathedInset * inset = GetInset();
122 if (inset->GetType()!= LM_OT_MACRO_ARG)
128 MathedRowContainer::iterator r = crow_;
131 crow_.st_->next_ = r.st_->next_;
140 if (pos2 <= array->last()) {
148 void MathedXIter::Merge(MathedArray const & a)
151 array->insert(array->begin() + pos,
154 // make room for the data
156 array->merge(a, pos);
159 int pos2 = pos + a.last();
164 while (pos < pos2 && OK()) {
166 if (p_ && p_->Permit(LMPF_ALLOW_CR)) {
167 container().insert_after(crow_, MathedRowSt(ncols + 1));
183 void MathedXIter::SetData(MathParInset * pp)
187 array = &p_->GetData();
188 ncols = p_->GetColumns();
189 crow_ = container().begin();
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_.st_->next_) {
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_ = container().begin();
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;
376 // Create new item for the structure
377 container().insert_after(crow_, MathedRowSt(ncols + 1));
378 // Fill missed tabs in current row
379 while (col < ncols - 1)
380 insert('T', LM_TC_TAB);
382 insert('K', LM_TC_CR);
386 goNextCode(LM_TC_CR);
388 // Fill missed tabs in new row
389 while (col < ncols - 1)
390 insert('T', LM_TC_TAB);
395 void MathedXIter::delRow()
398 lyxerr[Debug::MATHED] << "MathErr: Attempt to delete a line in a subparagraph." << endl;
401 bool line_empty = true;
407 } else if (!IsTab()) {
411 int const p1 = getPos();
416 MathedRowContainer::iterator r( crow_.st_->next_ );
418 crow_.st_->next_ = r.st_->next_;
430 void MathedXIter::ipush()
438 void MathedXIter::ipop()
444 crow_ = container().begin();
446 for (int i = 0; i < row; ++i)
452 void MathedXIter::fitCoord(int /*xx*/, int yy)
460 // first fit vertically
461 while (crow_ && OK()) {
462 if (yy >= yo + y_ - crow_->ascent()
463 && yy <= yo + y_ + crow_->descent())
465 goNextCode(LM_TC_CR);
469 // while (x<xx && Next());
473 void MathedXIter::setTab(int tx, int tab)
475 if (crow_ && tab <= ncols) {
476 crow_->setTab(tab, tx);
478 lyxerr << "MathErr: No tabs allowed here" << endl;
482 void MathedXIter::subMetrics(int a, int d)
485 lyxerr[Debug::MATHED]
486 << "MathErr: Attempt to submetric a subparagraph." << endl;
494 // This function is not recursive, as MathPar::Metrics is
495 void MathedXIter::IMetrics(int pos2, int & width, int & ascent, int & descent)
503 descent = ascent = width = 0;
505 if (array->empty()) return;
506 // if (pos2 > array->last) return;
511 mathed_char_height(fcode(), size_, cx, asc, des);
512 if (asc > ascent) ascent = asc;
513 if (des > descent) descent = des;
516 if (MathIsInset(cx)) {
517 MathedInset * pp = GetInset();
518 if (cx == LM_TC_UP) {
523 static_cast<MathParInset*>(pp)->GetXY(xx, asc);
526 asc += ((limits_) ? pp->Height() + 4 : pp->Ascent());
527 } else if (cx == LM_TC_DOWN) {
532 static_cast<MathParInset*>(pp)->GetXY(xx, des);
533 if (des - pp->Height() < yy && !asc)
534 asc = yy - (des - pp->Height());
537 des += (limit ? pp->Height()+4: pp->Height()-pp->Ascent()/2);
542 if (asc > ascent) ascent = asc;
543 if (des > descent) descent = des;
544 if (cx != LM_TC_UP && cx != LM_TC_DOWN)
545 limit = pp->GetLimits();
546 } else if (cx == LM_TC_TAB) {
549 lyxerr[Debug::MATHED]
550 << "Mathed Sel-Error: Unrecognized code["
551 << cx << ']' << endl;
554 if (pos < pos2) Next();
560 bool MathedXIter::setNumbered(bool numb)
563 crow_->setNumbered(numb);
571 bool MathedXIter::setLabel(string const & label)
574 crow_->setLabel(label);
582 MathedRowSt * MathedXIter::adjustVerticalSt()
586 crow_.st_ = new MathedRowSt(ncols + 1); // this leaks
588 MathedRowSt * mrow = crow_.st_;
593 MathedRowSt * r = new MathedRowSt(ncols + 1); // this leaks
594 crow_.st_->next_ = r;
603 string const & MathedXIter::getLabel() const
605 return crow_ ? crow_->getLabel() : error_label;