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)
127 container().erase(crow_);
132 if (pos2 <= array->last()) {
140 void MathedXIter::Merge(MathedArray const & a)
143 array->insert(array->begin() + pos,
146 // make room for the data
148 array->merge(a, pos);
151 int pos2 = pos + a.last();
156 while (pos < pos2 && OK()) {
158 if (p_ && p_->Permit(LMPF_ALLOW_CR)) {
159 container().insert(crow_);
175 void MathedXIter::SetData(MathParInset * pp)
177 //if (p_ && pp != p_) {
178 // lyxerr << "MathedXIter::SetData: " << p_ << " " << pp << endl;
182 array = &p_->GetData();
183 //lyxerr << "MathedXIter::SetData: " << p_ << " " << *array << endl;
184 ncols = p_->GetColumns();
185 crow_ = container().begin();
186 if (p_->Permit(LMPF_ALLOW_CR))
188 if (p_->Permit(LMPF_ALLOW_TAB))
192 x_ = crow_->getTab(0);
193 y_ = crow_->getBaseline();
196 array = new MathedArray; // this leaks
199 size_ = p_->GetStyle();
204 string const MathedXIter::GetString() const
206 string const s = MathedIter::GetString();
207 x_ += mathed_string_width(fcode(), size_, s);
212 bool MathedXIter::Next()
214 // lyxerr << "Ne[" << pos << "]";
220 MathedInset * px = GetInset();
222 if (px->GetType() == LM_OT_SCRIPT) {
227 sx_ = (px->GetLimits()) ? w : 0;
231 // lyxerr << "WD[" << fcode << " " << size << " " << c << endl;
232 w = mathed_char_width(fcode(), size_, c);
234 if (c == LM_TC_TAB && p_) {
235 // w = p->GetTab(col + 1);
236 w = (crow_) ? crow_->getTab(col + 1) : 0;
237 //lyxerr << "WW[" << w << "]";
239 if (c == LM_TC_CR && p_) {
241 if (crow_ && !crow_.is_last()) {
243 y_ = crow_->getBaseline();
244 w = crow_->getTab(0);
246 // lyxerr << "WW[" << col " " << row << "|" << w << "]";
248 lyxerr << "No hubo w[" << c << "]!";
250 if (MathedIter::Next()) {
251 // lyxerr <<"LNX " << pos << endl;
252 // if (sw>0 && GetChar()!= LM_TC_UP && GetChar()!= LM_TC_DOWN) {
253 // w = (sx>sw) ? 0: sw-sx;
254 if ((sw_ > 0 || sx_ > 0)
255 && GetChar() != LM_TC_UP && GetChar() != LM_TC_DOWN) {
257 w = (sx_ > sw_) ? 0 : sw_ - sx_;
267 void MathedXIter::GoBegin()
273 crow_ = container().begin();
275 x_ = crow_->getTab(0);
276 y_ = crow_->getBaseline();
282 void MathedXIter::GoLast()
288 void MathedXIter::Adjust()
292 while (posx > pos && OK())
297 bool MathedXIter::Prev()
299 if (pos == 0 || (pos == 1 && GetChar() >= ' '))
302 int pos2 = pos; // pos1
307 } while (pos < pos2);
314 void MathedXIter::goNextColumn()
318 while (Next() && col == colp)
321 //return (col != colp + 1 || rowp != row);
325 bool MathedXIter::Up()
327 if (row == 0) return false;
332 while (row < rowp - 1) Next();
333 while (x_ < xp && OK() && !IsCR()) {
337 if (col > colp) // || (stck.col == colp && stck.x<= xp && x>xp))
344 bool MathedXIter::Down()
350 bool res = (IsCR()) ? true : goNextCode(LM_TC_CR);
354 while (x_ < xp && OK()) {
358 if (col > colp || (stck.col == colp && stck.x <= xp && x_ > xp))
366 void MathedXIter::addRow()
369 lyxerr[Debug::MATHED] << "MathErr: Attempt to insert new"
370 " line in a subparagraph. " << this << endl;
375 // Create new item for the structure
376 container().insert(crow_);
377 // Fill missed tabs in current row
378 while (col < ncols - 1)
379 insert('T', LM_TC_TAB);
381 insert('K', LM_TC_CR);
385 goNextCode(LM_TC_CR);
387 // Fill missed tabs in new row
388 while (col < ncols - 1)
389 insert('T', LM_TC_TAB);
394 void MathedXIter::delRow()
397 lyxerr[Debug::MATHED] << "MathErr: Attempt to delete a line in a subparagraph." << endl;
400 bool line_empty = true;
411 int const p1 = getPos();
415 container().erase(crow_);
425 void MathedXIter::ipush()
433 void MathedXIter::ipop()
439 crow_ = container().begin();
441 for (int i = 0; i < row; ++i)
447 void MathedXIter::fitCoord(int /*xx*/, int yy)
455 // first fit vertically
456 while (crow_ && OK()) {
457 if (yy >= yo + y_ - crow_->ascent()
458 && yy <= yo + y_ + crow_->descent())
460 goNextCode(LM_TC_CR);
464 // while (x<xx && Next());
468 void MathedXIter::setTab(int tx, int tab)
470 if (crow_ && tab <= ncols)
471 crow_->setTab(tab, tx);
473 lyxerr << "MathErr: No tabs allowed here" << endl;
477 void MathedXIter::subMetrics(int a, int d)
480 lyxerr[Debug::MATHED]
481 << "MathErr: Attempt to submetric a subparagraph." << endl;
489 // This function is not recursive, as MathPar::Metrics is
490 void MathedXIter::IMetrics(int pos2, int & width, int & ascent, int & descent)
498 descent = ascent = width = 0;
503 // if (pos2 > array->last) return;
508 mathed_char_height(fcode(), size_, cx, asc, des);
509 if (asc > ascent) ascent = asc;
510 if (des > descent) descent = des;
513 if (MathIsInset(cx)) {
514 MathedInset * pp = GetInset();
515 if (cx == LM_TC_UP) {
520 static_cast<MathParInset*>(pp)->GetXY(xx, asc);
523 asc += ((limits_) ? pp->Height() + 4 : pp->Ascent());
524 } else if (cx == LM_TC_DOWN) {
529 static_cast<MathParInset*>(pp)->GetXY(xx, des);
530 if (des - pp->Height() < yy && !asc)
531 asc = yy - (des - pp->Height());
534 des += (limit ? pp->Height()+4: pp->Height()-pp->Ascent()/2);
539 if (asc > ascent) ascent = asc;
540 if (des > descent) descent = des;
541 if (cx != LM_TC_UP && cx != LM_TC_DOWN)
542 limit = pp->GetLimits();
543 } else if (cx == LM_TC_TAB) {
546 lyxerr[Debug::MATHED]
547 << "Mathed Sel-Error: Unrecognized code["
548 << cx << ']' << endl;
558 bool MathedXIter::setNumbered(bool numb)
561 crow_->setNumbered(numb);
569 bool MathedXIter::setLabel(string const & label)
572 crow_->setLabel(label);
581 string const & MathedXIter::getLabel() const
583 return crow_ ? crow_->getLabel() : error_label;