2 #include "math_textinset.h"
6 MathTextInset::MathTextInset()
11 MathInset * MathTextInset::clone() const
13 return new MathTextInset(*this);
17 MathInset::idx_type MathTextInset::pos2row(pos_type pos) const
19 for (pos_type r = 0, n = cache_.nargs(); r < n; ++r)
20 if (pos >= cache_.cellinfo_[r].begin_ && pos <= cache_.cellinfo_[r].end_)
22 lyxerr << "illegal row for pos " << pos << "\n";
27 void MathTextInset::getPos(idx_type, pos_type pos, int & x, int & y) const
29 idx_type const i = pos2row(pos);
30 pos_type const p = pos - cache_.cellinfo_[i].begin_;
31 cache_.getPos(i, p, x, y);
35 bool MathTextInset::idxUpDown(idx_type &, pos_type & pos, bool up,
38 // try to move only one screen row up or down if possible
39 idx_type i = pos2row(pos);
40 MathGridInset::CellInfo const & cell1 = cache_.cellinfo_[i];
41 int const x = cells_[0].pos2x(cell1.begin_, pos, cell1.glue_);
48 if (i == cache_.nargs())
51 MathGridInset::CellInfo const & cell2 = cache_.cellinfo_[i];
52 pos = xcell(0).x2pos(cell2.begin_, x, cell2.glue_);
57 void MathTextInset::metrics(MathMetricsInfo & mi) const
61 // we do our own metrics fiddling
63 cache_ = MathGridInset(1, 0);
70 for (size_type i = 0, n = cell(0).size(); i < n; ++i) {
71 //lyxerr << "at pos: " << i << " of " << n << " safepos: " << safepos
72 // << " curr: " << curr << " safe: " << safe
73 // << " spaces: " << spaces << endl;
76 // <char> <char> <char> <space> <char> <char> <char>
77 // ................... <safe>
78 // ..........................<curr>
79 // ....................<safepos>
81 // Special handling of spaces. We reached a safe position for breaking.
82 char const c = cell(0)[i]->getChar();
84 //lyxerr << "reached safe pos\n";
85 // we don't count the space into the safe pos
87 // we reset to this safepos if the next chunk does not fit
90 // restart chunk with size of the space
91 curr = cell(0)[i]->width();
96 // This is a regular char. Go on if we either don't care for
97 // the width limit or have not reached that limit.
98 curr += cell(0)[i]->width();
99 if (!mi.base.restrictwidth || curr + safe <= mi.base.textwidth)
103 // We passed the limit. Create a row entry.
104 //lyxerr << "passed limit\n";
106 MathXArray & ar = cache_.xcell(cache_.nargs() - 1);
107 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
109 // we are here because we hit a hard newline
112 begin = i + 1; // next chunk starts after the newline
115 // but we had a space break before this position.
116 // so retreat to this position
117 //lyxerr << "... but had safe pos.\n";
119 row.end_ = safepos; // this is position of the safe space
120 i = safepos; // i gets incremented at end of loop
121 begin = i + 1; // next chunk starts after the space
124 // This item is too large and it is the only one.
125 // We have no choice but to produce an overfull box.
126 lyxerr << "... without safe pos\n";
132 MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
133 //lyxerr << "line: " << ar << "\n";
134 // in any case, start the new row with empty boxes
138 // last row: put in everything else
140 MathXArray & ar = cache_.xcell(cache_.nargs() - 1);
141 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
143 row.end_ = cell(0).size();
145 MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
146 //lyxerr << "last line: " << ar.data() << "\n";
150 dim_ = cache_.dimensions();
151 //lyxerr << "outer dim: " << dim_ << endl;
155 void MathTextInset::draw(MathPainterInfo & pi, int x, int y) const
157 cache_.draw(pi, x + 1, y);
161 void MathTextInset::drawSelection(MathPainterInfo & pi,
162 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
164 cache_.drawSelection(pi, idx1, pos1, idx2, pos2);