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 & arr)
143 #warning quick and really dirty: make sure that we really own our inset
148 array->insert(array->begin() + pos,
151 // make room for the data
153 array->merge(a, pos);
156 int pos2 = pos + a.last();
161 while (pos < pos2 && OK()) {
163 if (p_ && p_->Permit(LMPF_ALLOW_CR)) {
164 container().insert(crow_);
180 void MathedXIter::SetData(MathParInset * pp)
182 //if (p_ && pp != p_) {
183 // lyxerr << "MathedXIter::SetData: " << p_ << " " << pp << endl;
187 array = &p_->GetData();
188 //lyxerr << "MathedXIter::SetData: " << p_ << " " << *array << endl;
189 ncols = p_->GetColumns();
190 crow_ = container().begin();
191 if (p_->Permit(LMPF_ALLOW_CR))
193 if (p_->Permit(LMPF_ALLOW_TAB))
197 x_ = crow_->getTab(0);
198 y_ = crow_->getBaseline();
201 array = new MathedArray; // this leaks
204 size_ = p_->GetStyle();
209 string const MathedXIter::GetString() const
211 string const s = MathedIter::GetString();
212 x_ += mathed_string_width(fcode(), size_, s);
217 bool MathedXIter::Next()
219 // lyxerr << "Ne[" << pos << "]";
225 MathedInset * px = GetInset();
227 if (px->GetType() == LM_OT_SCRIPT) {
232 sx_ = (px->GetLimits()) ? w : 0;
236 // lyxerr << "WD[" << fcode << " " << size << " " << c << endl;
237 w = mathed_char_width(fcode(), size_, c);
239 if (c == LM_TC_TAB && p_) {
240 // w = p->GetTab(col + 1);
241 w = (crow_) ? crow_->getTab(col + 1) : 0;
242 //lyxerr << "WW[" << w << "]";
244 if (c == LM_TC_CR && p_) {
246 if (crow_ && !crow_.is_last()) {
248 y_ = crow_->getBaseline();
249 w = crow_->getTab(0);
251 // lyxerr << "WW[" << col " " << row << "|" << w << "]";
253 lyxerr << "No hubo w[" << c << "]!";
255 if (MathedIter::Next()) {
256 // lyxerr <<"LNX " << pos << endl;
257 // if (sw>0 && GetChar()!= LM_TC_UP && GetChar()!= LM_TC_DOWN) {
258 // w = (sx>sw) ? 0: sw-sx;
259 if ((sw_ > 0 || sx_ > 0)
260 && GetChar() != LM_TC_UP && GetChar() != LM_TC_DOWN) {
262 w = (sx_ > sw_) ? 0 : sw_ - sx_;
272 void MathedXIter::GoBegin()
278 crow_ = container().begin();
280 x_ = crow_->getTab(0);
281 y_ = crow_->getBaseline();
287 void MathedXIter::GoLast()
293 void MathedXIter::Adjust()
297 while (posx > pos && OK())
302 bool MathedXIter::Prev()
304 if (pos == 0 || (pos == 1 && GetChar() >= ' '))
307 int pos2 = pos; // pos1
312 } while (pos < pos2);
319 void MathedXIter::goNextColumn()
323 while (Next() && col == colp)
326 //return (col != colp + 1 || rowp != row);
330 bool MathedXIter::Up()
332 if (row == 0) return false;
337 while (row < rowp - 1) Next();
338 while (x_ < xp && OK() && !IsCR()) {
342 if (col > colp) // || (stck.col == colp && stck.x<= xp && x>xp))
349 bool MathedXIter::Down()
355 bool res = (IsCR()) ? true : goNextCode(LM_TC_CR);
359 while (x_ < xp && OK()) {
363 if (col > colp || (stck.col == colp && stck.x <= xp && x_ > xp))
371 void MathedXIter::addRow()
374 lyxerr[Debug::MATHED] << "MathErr: Attempt to insert new"
375 " line in a subparagraph. " << this << endl;
380 // Create new item for the structure
381 container().insert(crow_);
382 // Fill missed tabs in current row
383 while (col < ncols - 1)
384 insert('T', LM_TC_TAB);
386 insert('K', LM_TC_CR);
390 goNextCode(LM_TC_CR);
392 // Fill missed tabs in new row
393 while (col < ncols - 1)
394 insert('T', LM_TC_TAB);
399 void MathedXIter::delRow()
402 lyxerr[Debug::MATHED] << "MathErr: Attempt to delete a line in a subparagraph." << endl;
405 bool line_empty = true;
416 int const p1 = getPos();
420 container().erase(crow_);
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;
508 // if (pos2 > array->last) return;
513 mathed_char_height(fcode(), size_, cx, asc, des);
514 if (asc > ascent) ascent = asc;
515 if (des > descent) descent = des;
518 if (MathIsInset(cx)) {
519 MathedInset * pp = GetInset();
520 if (cx == LM_TC_UP) {
525 static_cast<MathParInset*>(pp)->GetXY(xx, asc);
528 asc += ((limits_) ? pp->Height() + 4 : pp->Ascent());
529 } else if (cx == LM_TC_DOWN) {
534 static_cast<MathParInset*>(pp)->GetXY(xx, des);
535 if (des - pp->Height() < yy && !asc)
536 asc = yy - (des - pp->Height());
539 des += (limit ? pp->Height()+4: pp->Height()-pp->Ascent()/2);
544 if (asc > ascent) ascent = asc;
545 if (des > descent) descent = des;
546 if (cx != LM_TC_UP && cx != LM_TC_DOWN)
547 limit = pp->GetLimits();
548 } else if (cx == LM_TC_TAB) {
551 lyxerr[Debug::MATHED]
552 << "Mathed Sel-Error: Unrecognized code["
553 << cx << ']' << endl;
563 bool MathedXIter::setNumbered(bool numb)
566 crow_->setNumbered(numb);
574 bool MathedXIter::setLabel(string const & label)
577 crow_->setLabel(label);
586 string const & MathedXIter::getLabel() const
588 return crow_ ? crow_->getLabel() : error_label;