3 #include "math_textinset.h"
4 #include "metricsinfo.h"
11 MathTextInset::MathTextInset()
16 auto_ptr<InsetBase> MathTextInset::clone() const
18 return auto_ptr<InsetBase>(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 << endl;
32 void MathTextInset::getPos(idx_type /*idx*/, 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);
37 y = cache_.cell(i).yo();
41 bool MathTextInset::idxUpDown2(idx_type &, pos_type & pos, bool up,
42 int /*targetx*/) const
44 // try to move only one screen row up or down if possible
45 idx_type i = pos2row(pos);
46 //lyxerr << "\nMathTextInset::idxUpDown() i: " << i << endl;
47 MathGridInset::CellInfo const & cell1 = cache_.cellinfo_[i];
48 int const x = cache_.cell(i).pos2x(pos - cell1.begin_, cell1.glue_);
55 if (i == cache_.nargs())
58 MathGridInset::CellInfo const & cell2 = cache_.cellinfo_[i];
59 pos = cell2.begin_ + cache_.cell(i).x2pos(x, cell2.glue_);
64 void MathTextInset::metrics(MetricsInfo & mi, Dimension & dim) const
68 // we do our own metrics fiddling
69 // save old positional information
70 int const old_xo = cache_.cell(0).xo();
71 int const old_yo = cache_.cell(0).yo();
74 cache_ = MathGridInset(1, 0);
81 for (size_type i = 0, n = cell(0).size(); i < n; ++i) {
82 //lyxerr << "at pos: " << i << " of " << n << " safepos: " << safepos
83 // << " curr: " << curr << " safe: " << safe
84 // << " spaces: " << spaces << endl;
87 // <char> <char> <char> <space> <char> <char> <char>
88 // ................... <safe>
89 // ..........................<curr>
90 // ....................<safepos>
92 // Special handling of spaces. We reached a safe position for breaking.
93 char const c = cell(0)[i]->getChar();
95 //lyxerr << "reached safe pos" << endl;
96 // we don't count the space into the safe pos
98 // we reset to this safepos if the next chunk does not fit
101 // restart chunk with size of the space
102 curr = cell(0)[i].width_;
107 // This is a regular char. Go on if we either don't care for
108 // the width limit or have not reached that limit.
109 curr += cell(0)[i].width_;
110 if (curr + safe <= mi.base.textwidth)
114 // We passed the limit. Create a row entry.
115 //lyxerr << "passed limit" << endl;
117 MathArray & ar = cache_.cell(cache_.nargs() - 1);
118 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
120 // we are here because we hit a hard newline
123 begin = i + 1; // next chunk starts after the newline
126 // but we had a space break before this position.
127 // so retreat to this position
128 //lyxerr << "... but had safe pos." << endl;
130 row.end_ = safepos; // this is position of the safe space
131 i = safepos; // i gets incremented at end of loop
132 begin = i + 1; // next chunk starts after the space
135 // This item is too large and it is the only one.
136 // We have no choice but to produce an overfull box.
137 lyxerr << "... without safe pos" << endl;
142 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
143 //lyxerr << "line: " << ar << endl;
144 // in any case, start the new row with empty boxes
148 // last row: put in everything else
150 MathArray & ar = cache_.cell(cache_.nargs() - 1);
151 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
153 row.end_ = cell(0).size();
154 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
155 //lyxerr << "last line: " << ar.data() << endl;
158 cache_.metrics(mi, dim_);
159 //lyxerr << "outer dim: " << dim_ << endl;
161 // reset position cache
162 for (idx_type i = 0; i < cache_.nargs(); ++i)
163 cache_.cell(i).setXY(old_xo, old_yo);
169 void MathTextInset::draw(PainterInfo & pi, int x, int y) const
171 cache_.draw(pi, x + 1, y);
175 void MathTextInset::drawSelection(PainterInfo & pi,
176 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
178 cache_.drawSelection(pi, idx1, pos1, idx2, pos2);