6 #include "math_textinset.h"
7 #include "math_metricsinfo.h"
11 MathTextInset::MathTextInset()
16 MathInset * MathTextInset::clone() const
18 return new MathTextInset(*this);
22 MathInset::idx_type MathTextInset::pos2row(pos_type pos) const
24 for (pos_type r = 0, n = cache_.nargs(); r < n; ++r)
25 if (pos >= cache_.cellinfo_[r].begin_ && pos <= cache_.cellinfo_[r].end_)
27 lyxerr << "illegal row for pos " << pos << "\n";
32 void MathTextInset::getPos(idx_type, pos_type pos, int & x, int & y) const
34 idx_type const i = pos2row(pos);
35 pos_type const p = pos - cache_.cellinfo_[i].begin_;
36 cache_.getPos(i, p, x, y);
40 bool MathTextInset::idxUpDown(idx_type &, pos_type & pos, bool up,
41 int /*targetx*/) const
43 // try to move only one screen row up or down if possible
44 idx_type i = pos2row(pos);
45 MathGridInset::CellInfo const & cell1 = cache_.cellinfo_[i];
46 int const x = cells_[0].pos2x(cell1.begin_, pos, cell1.glue_);
53 if (i == cache_.nargs())
56 MathGridInset::CellInfo const & cell2 = cache_.cellinfo_[i];
57 pos = cell(0).x2pos(cell2.begin_, x, cell2.glue_);
62 void MathTextInset::metrics(MathMetricsInfo & mi) const
66 // we do our own metrics fiddling
68 cache_ = MathGridInset(1, 0);
75 for (size_type i = 0, n = cell(0).size(); i < n; ++i) {
76 //lyxerr << "at pos: " << i << " of " << n << " safepos: " << safepos
77 // << " curr: " << curr << " safe: " << safe
78 // << " spaces: " << spaces << endl;
81 // <char> <char> <char> <space> <char> <char> <char>
82 // ................... <safe>
83 // ..........................<curr>
84 // ....................<safepos>
86 // Special handling of spaces. We reached a safe position for breaking.
87 char const c = cell(0)[i]->getChar();
89 //lyxerr << "reached safe pos\n";
90 // we don't count the space into the safe pos
92 // we reset to this safepos if the next chunk does not fit
95 // restart chunk with size of the space
96 curr = cell(0)[i]->width();
101 // This is a regular char. Go on if we either don't care for
102 // the width limit or have not reached that limit.
103 curr += cell(0)[i]->width();
104 if (!mi.base.restrictwidth || curr + safe <= mi.base.textwidth)
108 // We passed the limit. Create a row entry.
109 //lyxerr << "passed limit\n";
111 MathArray & ar = cache_.cell(cache_.nargs() - 1);
112 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
114 // we are here because we hit a hard newline
117 begin = i + 1; // next chunk starts after the newline
120 // but we had a space break before this position.
121 // so retreat to this position
122 //lyxerr << "... but had safe pos.\n";
124 row.end_ = safepos; // this is position of the safe space
125 i = safepos; // i gets incremented at end of loop
126 begin = i + 1; // next chunk starts after the space
129 // This item is too large and it is the only one.
130 // We have no choice but to produce an overfull box.
131 lyxerr << "... without safe pos\n";
136 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
137 //lyxerr << "line: " << ar << "\n";
138 // in any case, start the new row with empty boxes
142 // last row: put in everything else
144 MathArray & ar = cache_.cell(cache_.nargs() - 1);
145 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
147 row.end_ = cell(0).size();
148 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
149 //lyxerr << "last line: " << ar.data() << "\n";
153 dim_ = cache_.dimensions();
154 //lyxerr << "outer dim: " << dim_ << endl;
158 void MathTextInset::draw(MathPainterInfo & pi, int x, int y) const
160 cache_.draw(pi, x + 1, y);
164 void MathTextInset::drawSelection(MathPainterInfo & pi,
165 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
167 cache_.drawSelection(pi, idx1, pos1, idx2, pos2);