2 #include "math_textinset.h"
3 #include "math_metricsinfo.h"
7 MathTextInset::MathTextInset()
12 MathInset * MathTextInset::clone() const
14 return new MathTextInset(*this);
18 MathInset::idx_type MathTextInset::pos2row(pos_type pos) const
20 for (pos_type r = 0, n = cache_.nargs(); r < n; ++r)
21 if (pos >= cache_.cellinfo_[r].begin_ && pos <= cache_.cellinfo_[r].end_)
23 lyxerr << "illegal row for pos " << pos << "\n";
28 void MathTextInset::getPos(idx_type, pos_type pos, int & x, int & y) const
30 idx_type const i = pos2row(pos);
31 pos_type const p = pos - cache_.cellinfo_[i].begin_;
32 cache_.getPos(i, p, x, y);
36 bool MathTextInset::idxUpDown(idx_type &, pos_type & pos, bool up,
37 int /*targetx*/) const
39 // try to move only one screen row up or down if possible
40 idx_type i = pos2row(pos);
41 MathGridInset::CellInfo const & cell1 = cache_.cellinfo_[i];
42 int const x = cells_[0].pos2x(cell1.begin_, pos, cell1.glue_);
49 if (i == cache_.nargs())
52 MathGridInset::CellInfo const & cell2 = cache_.cellinfo_[i];
53 pos = cell(0).x2pos(cell2.begin_, x, cell2.glue_);
58 void MathTextInset::metrics(MathMetricsInfo & mi) const
62 // we do our own metrics fiddling
64 cache_ = MathGridInset(1, 0);
71 for (size_type i = 0, n = cell(0).size(); i < n; ++i) {
72 //lyxerr << "at pos: " << i << " of " << n << " safepos: " << safepos
73 // << " curr: " << curr << " safe: " << safe
74 // << " spaces: " << spaces << endl;
77 // <char> <char> <char> <space> <char> <char> <char>
78 // ................... <safe>
79 // ..........................<curr>
80 // ....................<safepos>
82 // Special handling of spaces. We reached a safe position for breaking.
83 char const c = cell(0)[i]->getChar();
85 //lyxerr << "reached safe pos\n";
86 // we don't count the space into the safe pos
88 // we reset to this safepos if the next chunk does not fit
91 // restart chunk with size of the space
92 curr = cell(0)[i]->width();
97 // This is a regular char. Go on if we either don't care for
98 // the width limit or have not reached that limit.
99 curr += cell(0)[i]->width();
100 if (!mi.base.restrictwidth || curr + safe <= mi.base.textwidth)
104 // We passed the limit. Create a row entry.
105 //lyxerr << "passed limit\n";
107 MathArray & ar = cache_.cell(cache_.nargs() - 1);
108 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
110 // we are here because we hit a hard newline
113 begin = i + 1; // next chunk starts after the newline
116 // but we had a space break before this position.
117 // so retreat to this position
118 //lyxerr << "... but had safe pos.\n";
120 row.end_ = safepos; // this is position of the safe space
121 i = safepos; // i gets incremented at end of loop
122 begin = i + 1; // next chunk starts after the space
125 // This item is too large and it is the only one.
126 // We have no choice but to produce an overfull box.
127 lyxerr << "... without safe pos\n";
132 ar = 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 MathArray & ar = cache_.cell(cache_.nargs() - 1);
141 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
143 row.end_ = cell(0).size();
144 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
145 //lyxerr << "last line: " << ar.data() << "\n";
149 dim_ = cache_.dimensions();
150 //lyxerr << "outer dim: " << dim_ << endl;
154 void MathTextInset::draw(MathPainterInfo & pi, int x, int y) const
156 cache_.draw(pi, x + 1, y);
160 void MathTextInset::drawSelection(MathPainterInfo & pi,
161 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
163 cache_.drawSelection(pi, idx1, pos1, idx2, pos2);