5 #include "math_textinset.h"
6 #include "math_metricsinfo.h"
10 MathTextInset::MathTextInset()
15 MathInset * MathTextInset::clone() const
17 return new MathTextInset(*this);
21 MathInset::idx_type MathTextInset::pos2row(pos_type pos) const
23 for (pos_type r = 0, n = cache_.nargs(); r < n; ++r)
24 if (pos >= cache_.cellinfo_[r].begin_ && pos <= cache_.cellinfo_[r].end_)
26 lyxerr << "illegal row for pos " << pos << "\n";
31 void MathTextInset::getPos(idx_type, pos_type pos, int & x, int & y) const
33 idx_type const i = pos2row(pos);
34 pos_type const p = pos - cache_.cellinfo_[i].begin_;
35 cache_.getPos(i, p, x, y);
39 bool MathTextInset::idxUpDown(idx_type &, pos_type & pos, bool up,
40 int /*targetx*/) const
42 // try to move only one screen row up or down if possible
43 idx_type i = pos2row(pos);
44 MathGridInset::CellInfo const & cell1 = cache_.cellinfo_[i];
45 int const x = cells_[0].pos2x(cell1.begin_, pos, cell1.glue_);
52 if (i == cache_.nargs())
55 MathGridInset::CellInfo const & cell2 = cache_.cellinfo_[i];
56 pos = cell(0).x2pos(cell2.begin_, x, cell2.glue_);
61 void MathTextInset::metrics(MathMetricsInfo & mi) const
65 // we do our own metrics fiddling
67 cache_ = MathGridInset(1, 0);
74 for (size_type i = 0, n = cell(0).size(); i < n; ++i) {
75 //lyxerr << "at pos: " << i << " of " << n << " safepos: " << safepos
76 // << " curr: " << curr << " safe: " << safe
77 // << " spaces: " << spaces << endl;
80 // <char> <char> <char> <space> <char> <char> <char>
81 // ................... <safe>
82 // ..........................<curr>
83 // ....................<safepos>
85 // Special handling of spaces. We reached a safe position for breaking.
86 char const c = cell(0)[i]->getChar();
88 //lyxerr << "reached safe pos\n";
89 // we don't count the space into the safe pos
91 // we reset to this safepos if the next chunk does not fit
94 // restart chunk with size of the space
95 curr = cell(0)[i]->width();
100 // This is a regular char. Go on if we either don't care for
101 // the width limit or have not reached that limit.
102 curr += cell(0)[i]->width();
103 if (!mi.base.restrictwidth || curr + safe <= mi.base.textwidth)
107 // We passed the limit. Create a row entry.
108 //lyxerr << "passed limit\n";
110 MathArray & ar = cache_.cell(cache_.nargs() - 1);
111 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
113 // we are here because we hit a hard newline
116 begin = i + 1; // next chunk starts after the newline
119 // but we had a space break before this position.
120 // so retreat to this position
121 //lyxerr << "... but had safe pos.\n";
123 row.end_ = safepos; // this is position of the safe space
124 i = safepos; // i gets incremented at end of loop
125 begin = i + 1; // next chunk starts after the space
128 // This item is too large and it is the only one.
129 // We have no choice but to produce an overfull box.
130 lyxerr << "... without safe pos\n";
135 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
136 //lyxerr << "line: " << ar << "\n";
137 // in any case, start the new row with empty boxes
141 // last row: put in everything else
143 MathArray & ar = cache_.cell(cache_.nargs() - 1);
144 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
146 row.end_ = cell(0).size();
147 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
148 //lyxerr << "last line: " << ar.data() << "\n";
152 dim_ = cache_.dimensions();
153 //lyxerr << "outer dim: " << dim_ << endl;
157 void MathTextInset::draw(MathPainterInfo & pi, int x, int y) const
159 cache_.draw(pi, x + 1, y);
163 void MathTextInset::drawSelection(MathPainterInfo & pi,
164 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
166 cache_.drawSelection(pi, idx1, pos1, idx2, pos2);