2 #include "math_textinset.h"
3 #include "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 /*idx*/, 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);
33 y = cache_.cell(i).yo();
37 bool MathTextInset::idxUpDown2(idx_type &, pos_type & pos, bool up,
38 int /*targetx*/) const
40 // try to move only one screen row up or down if possible
41 idx_type i = pos2row(pos);
42 //lyxerr << "\nMathTextInset::idxUpDown() i: " << i << endl;
43 MathGridInset::CellInfo const & cell1 = cache_.cellinfo_[i];
44 int const x = cache_.cell(i).pos2x(pos - cell1.begin_, cell1.glue_);
51 if (i == cache_.nargs())
54 MathGridInset::CellInfo const & cell2 = cache_.cellinfo_[i];
55 pos = cell2.begin_ + cache_.cell(i).x2pos(x, cell2.glue_);
60 void MathTextInset::metrics(MetricsInfo & mi) const
64 // we do our own metrics fiddling
65 // save old positional information
66 int const old_xo = cache_.cell(0).xo();
67 int const old_yo = cache_.cell(0).yo();
70 cache_ = MathGridInset(1, 0);
77 for (size_type i = 0, n = cell(0).size(); i < n; ++i) {
78 //lyxerr << "at pos: " << i << " of " << n << " safepos: " << safepos
79 // << " curr: " << curr << " safe: " << safe
80 // << " spaces: " << spaces << endl;
83 // <char> <char> <char> <space> <char> <char> <char>
84 // ................... <safe>
85 // ..........................<curr>
86 // ....................<safepos>
88 // Special handling of spaces. We reached a safe position for breaking.
89 char const c = cell(0)[i]->getChar();
91 //lyxerr << "reached safe pos\n";
92 // we don't count the space into the safe pos
94 // we reset to this safepos if the next chunk does not fit
97 // restart chunk with size of the space
98 curr = cell(0)[i]->width();
103 // This is a regular char. Go on if we either don't care for
104 // the width limit or have not reached that limit.
105 curr += cell(0)[i]->width();
106 if (!mi.base.restrictwidth || curr + safe <= mi.base.textwidth)
110 // We passed the limit. Create a row entry.
111 //lyxerr << "passed limit\n";
113 MathArray & ar = cache_.cell(cache_.nargs() - 1);
114 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
116 // we are here because we hit a hard newline
119 begin = i + 1; // next chunk starts after the newline
122 // but we had a space break before this position.
123 // so retreat to this position
124 //lyxerr << "... but had safe pos.\n";
126 row.end_ = safepos; // this is position of the safe space
127 i = safepos; // i gets incremented at end of loop
128 begin = i + 1; // next chunk starts after the space
131 // This item is too large and it is the only one.
132 // We have no choice but to produce an overfull box.
133 lyxerr << "... without safe pos\n";
138 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
139 //lyxerr << "line: " << ar << "\n";
140 // in any case, start the new row with empty boxes
144 // last row: put in everything else
146 MathArray & ar = cache_.cell(cache_.nargs() - 1);
147 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
149 row.end_ = cell(0).size();
150 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
151 //lyxerr << "last line: " << ar.data() << "\n";
155 dim_ = cache_.dimensions();
156 //lyxerr << "outer dim: " << dim_ << endl;
158 // reset position cache
159 for (idx_type i = 0; i < cache_.nargs(); ++i)
160 cache_.cell(i).setXY(old_xo, old_yo);
165 void MathTextInset::draw(PainterInfo & pi, int x, int y) const
167 cache_.draw(pi, x + 1, y);
171 void MathTextInset::drawSelection(PainterInfo & pi,
172 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
174 cache_.drawSelection(pi, idx1, pos1, idx2, pos2);