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 MathedRowContainer::iterator r( new MathedRowSt(ncols + 1) );
169 r.st_->next_ = crow_.st_->next_;
170 crow_.st_->next_ = r.st_;
189 void MathedXIter::SetData(MathParInset * pp)
193 array = &p_->GetData();
194 ncols = p_->GetColumns();
195 crow_ = container().begin();
196 if (p_->Permit(LMPF_ALLOW_CR))
198 if (p_->Permit(LMPF_ALLOW_TAB))
202 x_ = crow_->getTab(0);
203 y_ = crow_->getBaseline();
206 array = new MathedArray; // this leaks
209 size_ = p_->GetStyle();
214 string const MathedXIter::GetString() const
216 string const s = MathedIter::GetString();
217 x_ += mathed_string_width(fcode(), size_, s);
222 bool MathedXIter::Next()
224 // lyxerr << "Ne[" << pos << "]";
225 if (!OK()) return false;
229 MathedInset * px = GetInset();
231 if (px->GetType() == LM_OT_SCRIPT) {
232 if (w > sw_) sw_ = w;
235 sx_ = (px->GetLimits()) ? w : 0;
239 // lyxerr << "WD[" << fcode << " " << size << " " << c << endl;
240 w = mathed_char_width(fcode(), size_, c);
242 if (c == LM_TC_TAB && p_) {
243 // w = p->GetTab(col + 1);
244 w = (crow_) ? crow_->getTab(col + 1) : 0;
245 //lyxerr << "WW[" << w << "]";
247 if (c == LM_TC_CR && p_) {
249 if (crow_ && crow_.st_->next_) {
251 y_ = crow_->getBaseline();
252 w = crow_->getTab(0);
254 // lyxerr << "WW[" << col " " << row << "|" << w << "]";
256 lyxerr << "No hubo w[" << c << "]!";
258 if (MathedIter::Next()) {
259 // lyxerr <<"LNX " << pos << endl;
260 // if (sw>0 && GetChar()!= LM_TC_UP && GetChar()!= LM_TC_DOWN) {
261 // w = (sx>sw) ? 0: sw-sx;
262 if ((sw_ > 0 || sx_ > 0)
263 && GetChar() != LM_TC_UP && GetChar() != LM_TC_DOWN) {
265 w = (sx_ > sw_) ? 0 : sw_ - sx_;
275 void MathedXIter::GoBegin()
281 crow_ = container().begin();
283 x_ = crow_->getTab(0);
284 y_ = crow_->getBaseline();
290 void MathedXIter::GoLast()
296 void MathedXIter::Adjust()
300 while (posx > pos && OK()) Next();
304 bool MathedXIter::Prev()
306 if (pos == 0 || (pos == 1 && GetChar() >= ' '))
309 int pos2 = pos; // pos1
321 void MathedXIter::goNextColumn()
325 while (Next() && col == colp)
328 //return (col != colp + 1 || rowp != row);
332 bool MathedXIter::Up()
334 if (row == 0) return false;
339 while (row < rowp - 1) Next();
340 while (x_ < xp && OK() && !IsCR()) {
344 if (col > colp) // || (stck.col == colp && stck.x<= xp && x>xp))
351 bool MathedXIter::Down()
357 bool res = (IsCR()) ? true : goNextCode(LM_TC_CR);
361 while (x_ < xp && OK()) {
365 if (col > colp || (stck.col == colp && stck.x <= xp && x_ > xp))
373 void MathedXIter::addRow()
376 lyxerr[Debug::MATHED] << "MathErr: Attempt to insert new"
377 " line in a subparagraph. " << this << endl;
381 // Create new item for the structure
382 MathedRowContainer::iterator r( new MathedRowSt(ncols + 1) );
384 r.st_->next_ = crow_.st_->next_;
385 crow_.st_->next_ = r.st_;
390 // Fill missed tabs in current row
391 while (col < ncols - 1)
392 insert('T', LM_TC_TAB);
394 insert('K', LM_TC_CR);
398 goNextCode(LM_TC_CR);
400 // Fill missed tabs in new row
401 while (col < ncols - 1)
402 insert('T', LM_TC_TAB);
407 void MathedXIter::delRow()
410 lyxerr[Debug::MATHED] << "MathErr: Attempt to delete a line in a subparagraph." << endl;
413 bool line_empty = true;
419 } else if (!IsTab()) {
423 int const p1 = getPos();
428 MathedRowContainer::iterator r( crow_.st_->next_ );
430 crow_.st_->next_ = r.st_->next_;
442 void MathedXIter::ipush()
450 void MathedXIter::ipop()
456 crow_ = container().begin();
458 for (int i = 0; i < row; ++i)
464 void MathedXIter::fitCoord(int /*xx*/, int yy)
472 // first fit vertically
473 while (crow_ && OK()) {
474 if (yy >= yo + y_ - crow_->ascent()
475 && yy <= yo + y_ + crow_->descent())
477 goNextCode(LM_TC_CR);
481 // while (x<xx && Next());
485 void MathedXIter::setTab(int tx, int tab)
487 if (crow_ && tab <= ncols) {
488 crow_->setTab(tab, tx);
490 lyxerr << "MathErr: No tabs allowed here" << endl;
494 void MathedXIter::subMetrics(int a, int d)
497 lyxerr[Debug::MATHED]
498 << "MathErr: Attempt to submetric a subparagraph." << endl;
506 // This function is not recursive, as MathPar::Metrics is
507 void MathedXIter::IMetrics(int pos2, int & width, int & ascent, int & descent)
515 descent = ascent = width = 0;
517 if (array->empty()) return;
518 // if (pos2 > array->last) return;
523 mathed_char_height(fcode(), size_, cx, asc, des);
524 if (asc > ascent) ascent = asc;
525 if (des > descent) descent = des;
528 if (MathIsInset(cx)) {
529 MathedInset * pp = GetInset();
530 if (cx == LM_TC_UP) {
535 static_cast<MathParInset*>(pp)->GetXY(xx, asc);
538 asc += ((limits_) ? pp->Height() + 4 : pp->Ascent());
539 } else if (cx == LM_TC_DOWN) {
544 static_cast<MathParInset*>(pp)->GetXY(xx, des);
545 if (des - pp->Height() < yy && !asc)
546 asc = yy - (des - pp->Height());
549 des += (limit ? pp->Height()+4: pp->Height()-pp->Ascent()/2);
554 if (asc > ascent) ascent = asc;
555 if (des > descent) descent = des;
556 if (cx != LM_TC_UP && cx != LM_TC_DOWN)
557 limit = pp->GetLimits();
558 } else if (cx == LM_TC_TAB) {
561 lyxerr[Debug::MATHED]
562 << "Mathed Sel-Error: Unrecognized code["
563 << cx << ']' << endl;
566 if (pos < pos2) Next();
572 bool MathedXIter::setNumbered(bool numb)
575 crow_->setNumbered(numb);
583 bool MathedXIter::setLabel(string const & label)
586 crow_->setLabel(label);
594 MathedRowSt * MathedXIter::adjustVerticalSt()
598 // lyxerr << " CRW" << ncols << " ";
599 crow_.st_ = new MathedRowSt(ncols + 1); // this leaks
601 // lyxerr<< " CRW[" << crow_ << "] ";
602 MathedRowSt * mrow = crow_.st_;
605 if (col >= ncols) ncols = col + 1;
606 MathedRowSt * r = new MathedRowSt(ncols + 1); // this leaks
607 // r->next = crow_->next;
608 crow_.st_->next_ = r;
610 // lyxerr << " CX[" << crow_ << "]";
618 string const & MathedXIter::getLabel() const
620 return crow_ ? crow_->getLabel() : error_label;