2 * \file math_textinset.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "math_textinset.h"
14 #include "metricsinfo.h"
21 MathTextInset::MathTextInset()
26 auto_ptr<InsetBase> MathTextInset::clone() const
28 return auto_ptr<InsetBase>(new MathTextInset(*this));
32 MathInset::idx_type MathTextInset::pos2row(pos_type pos) const
34 for (pos_type r = 0, n = cache_.nargs(); r < n; ++r)
35 if (pos >= cache_.cellinfo_[r].begin_ && pos <= cache_.cellinfo_[r].end_)
37 lyxerr << "illegal row for pos " << pos << endl;
42 void MathTextInset::getPos(idx_type /*idx*/, pos_type pos, int & x, int & y) const
44 idx_type const i = pos2row(pos);
45 pos_type const p = pos - cache_.cellinfo_[i].begin_;
46 cache_.getPos(i, p, x, y);
47 y = cache_.cell(i).yo();
51 bool MathTextInset::idxUpDown2(idx_type &, pos_type & pos, bool up,
52 int /*targetx*/) const
54 // try to move only one screen row up or down if possible
55 idx_type i = pos2row(pos);
56 //lyxerr << "\nMathTextInset::idxUpDown() i: " << i << endl;
57 MathGridInset::CellInfo const & cell1 = cache_.cellinfo_[i];
58 int const x = cache_.cell(i).pos2x(pos - cell1.begin_, cell1.glue_);
65 if (i == cache_.nargs())
68 MathGridInset::CellInfo const & cell2 = cache_.cellinfo_[i];
69 pos = cell2.begin_ + cache_.cell(i).x2pos(x, cell2.glue_);
74 void MathTextInset::metrics(MetricsInfo & mi, Dimension & dim) const
78 // we do our own metrics fiddling
79 // save old positional information
80 int const old_xo = cache_.cell(0).xo();
81 int const old_yo = cache_.cell(0).yo();
84 cache_ = MathGridInset(1, 0);
91 for (size_type i = 0, n = cell(0).size(); i < n; ++i) {
92 //lyxerr << "at pos: " << i << " of " << n << " safepos: " << safepos
93 // << " curr: " << curr << " safe: " << safe
94 // << " spaces: " << spaces << endl;
97 // <char> <char> <char> <space> <char> <char> <char>
98 // ................... <safe>
99 // ..........................<curr>
100 // ....................<safepos>
102 // Special handling of spaces. We reached a safe position for breaking.
103 char const c = cell(0)[i]->getChar();
105 //lyxerr << "reached safe pos" << endl;
106 // we don't count the space into the safe pos
108 // we reset to this safepos if the next chunk does not fit
111 // restart chunk with size of the space
112 curr = cell(0)[i].width_;
117 // This is a regular char. Go on if we either don't care for
118 // the width limit or have not reached that limit.
119 curr += cell(0)[i].width_;
120 if (curr + safe <= mi.base.textwidth)
124 // We passed the limit. Create a row entry.
125 //lyxerr << "passed limit" << endl;
127 MathArray & ar = cache_.cell(cache_.nargs() - 1);
128 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
130 // we are here because we hit a hard newline
133 begin = i + 1; // next chunk starts after the newline
136 // but we had a space break before this position.
137 // so retreat to this position
138 //lyxerr << "... but had safe pos." << endl;
140 row.end_ = safepos; // this is position of the safe space
141 i = safepos; // i gets incremented at end of loop
142 begin = i + 1; // next chunk starts after the space
145 // This item is too large and it is the only one.
146 // We have no choice but to produce an overfull box.
147 lyxerr << "... without safe pos" << endl;
152 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
153 //lyxerr << "line: " << ar << endl;
154 // in any case, start the new row with empty boxes
158 // last row: put in everything else
160 MathArray & ar = cache_.cell(cache_.nargs() - 1);
161 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
163 row.end_ = cell(0).size();
164 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
165 //lyxerr << "last line: " << ar.data() << endl;
168 cache_.metrics(mi, dim_);
169 //lyxerr << "outer dim: " << dim_ << endl;
171 // reset position cache
172 for (idx_type i = 0; i < cache_.nargs(); ++i)
173 cache_.cell(i).setXY(old_xo, old_yo);
179 void MathTextInset::draw(PainterInfo & pi, int x, int y) const
181 cache_.draw(pi, x + 1, y);
185 void MathTextInset::drawSelection(PainterInfo & pi,
186 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
188 cache_.drawSelection(pi, idx1, pos1, idx2, pos2);