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)
179 array = &p_->GetData();
180 ncols = p_->GetColumns();
181 crow_ = container().begin();
182 if (p_->Permit(LMPF_ALLOW_CR))
184 if (p_->Permit(LMPF_ALLOW_TAB))
188 x_ = crow_->getTab(0);
189 y_ = crow_->getBaseline();
192 array = new MathedArray; // this leaks
195 size_ = p_->GetStyle();
200 string const MathedXIter::GetString() const
202 string const s = MathedIter::GetString();
203 x_ += mathed_string_width(fcode(), size_, s);
208 bool MathedXIter::Next()
210 // lyxerr << "Ne[" << pos << "]";
216 MathedInset * px = GetInset();
218 if (px->GetType() == LM_OT_SCRIPT) {
223 sx_ = (px->GetLimits()) ? w : 0;
227 // lyxerr << "WD[" << fcode << " " << size << " " << c << endl;
228 w = mathed_char_width(fcode(), size_, c);
230 if (c == LM_TC_TAB && p_) {
231 // w = p->GetTab(col + 1);
232 w = (crow_) ? crow_->getTab(col + 1) : 0;
233 //lyxerr << "WW[" << w << "]";
235 if (c == LM_TC_CR && p_) {
237 if (crow_ && !crow_.is_last()) {
239 y_ = crow_->getBaseline();
240 w = crow_->getTab(0);
242 // lyxerr << "WW[" << col " " << row << "|" << w << "]";
244 lyxerr << "No hubo w[" << c << "]!";
246 if (MathedIter::Next()) {
247 // lyxerr <<"LNX " << pos << endl;
248 // if (sw>0 && GetChar()!= LM_TC_UP && GetChar()!= LM_TC_DOWN) {
249 // w = (sx>sw) ? 0: sw-sx;
250 if ((sw_ > 0 || sx_ > 0)
251 && GetChar() != LM_TC_UP && GetChar() != LM_TC_DOWN) {
253 w = (sx_ > sw_) ? 0 : sw_ - sx_;
263 void MathedXIter::GoBegin()
269 crow_ = container().begin();
271 x_ = crow_->getTab(0);
272 y_ = crow_->getBaseline();
278 void MathedXIter::GoLast()
284 void MathedXIter::Adjust()
288 while (posx > pos && OK())
293 bool MathedXIter::Prev()
295 if (pos == 0 || (pos == 1 && GetChar() >= ' '))
298 int pos2 = pos; // pos1
310 void MathedXIter::goNextColumn()
314 while (Next() && col == colp)
317 //return (col != colp + 1 || rowp != row);
321 bool MathedXIter::Up()
323 if (row == 0) return false;
328 while (row < rowp - 1) Next();
329 while (x_ < xp && OK() && !IsCR()) {
333 if (col > colp) // || (stck.col == colp && stck.x<= xp && x>xp))
340 bool MathedXIter::Down()
346 bool res = (IsCR()) ? true : goNextCode(LM_TC_CR);
350 while (x_ < xp && OK()) {
354 if (col > colp || (stck.col == colp && stck.x <= xp && x_ > xp))
362 void MathedXIter::addRow()
365 lyxerr[Debug::MATHED] << "MathErr: Attempt to insert new"
366 " line in a subparagraph. " << this << endl;
371 // Create new item for the structure
372 container().insert(crow_);
373 // Fill missed tabs in current row
374 while (col < ncols - 1)
375 insert('T', LM_TC_TAB);
377 insert('K', LM_TC_CR);
381 goNextCode(LM_TC_CR);
383 // Fill missed tabs in new row
384 while (col < ncols - 1)
385 insert('T', LM_TC_TAB);
390 void MathedXIter::delRow()
393 lyxerr[Debug::MATHED] << "MathErr: Attempt to delete a line in a subparagraph." << endl;
396 bool line_empty = true;
402 } else if (!IsTab()) {
407 int const p1 = getPos();
411 container().erase(crow_);
421 void MathedXIter::ipush()
429 void MathedXIter::ipop()
435 crow_ = container().begin();
437 for (int i = 0; i < row; ++i)
443 void MathedXIter::fitCoord(int /*xx*/, int yy)
451 // first fit vertically
452 while (crow_ && OK()) {
453 if (yy >= yo + y_ - crow_->ascent()
454 && yy <= yo + y_ + crow_->descent())
456 goNextCode(LM_TC_CR);
460 // while (x<xx && Next());
464 void MathedXIter::setTab(int tx, int tab)
466 if (crow_ && tab <= ncols) {
467 crow_->setTab(tab, tx);
469 lyxerr << "MathErr: No tabs allowed here" << endl;
473 void MathedXIter::subMetrics(int a, int d)
476 lyxerr[Debug::MATHED]
477 << "MathErr: Attempt to submetric a subparagraph." << endl;
485 // This function is not recursive, as MathPar::Metrics is
486 void MathedXIter::IMetrics(int pos2, int & width, int & ascent, int & descent)
494 descent = ascent = width = 0;
496 if (array->empty()) return;
497 // if (pos2 > array->last) return;
502 mathed_char_height(fcode(), size_, cx, asc, des);
503 if (asc > ascent) ascent = asc;
504 if (des > descent) descent = des;
507 if (MathIsInset(cx)) {
508 MathedInset * pp = GetInset();
509 if (cx == LM_TC_UP) {
514 static_cast<MathParInset*>(pp)->GetXY(xx, asc);
517 asc += ((limits_) ? pp->Height() + 4 : pp->Ascent());
518 } else if (cx == LM_TC_DOWN) {
523 static_cast<MathParInset*>(pp)->GetXY(xx, des);
524 if (des - pp->Height() < yy && !asc)
525 asc = yy - (des - pp->Height());
528 des += (limit ? pp->Height()+4: pp->Height()-pp->Ascent()/2);
533 if (asc > ascent) ascent = asc;
534 if (des > descent) descent = des;
535 if (cx != LM_TC_UP && cx != LM_TC_DOWN)
536 limit = pp->GetLimits();
537 } else if (cx == LM_TC_TAB) {
540 lyxerr[Debug::MATHED]
541 << "Mathed Sel-Error: Unrecognized code["
542 << cx << ']' << endl;
552 bool MathedXIter::setNumbered(bool numb)
555 crow_->setNumbered(numb);
563 bool MathedXIter::setLabel(string const & label)
566 crow_->setLabel(label);
575 string const & MathedXIter::getLabel() const
577 return crow_ ? crow_->getLabel() : error_label;