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 "math_data.h"
15 #include "metricsinfo.h"
22 MathTextInset::MathTextInset()
27 auto_ptr<InsetBase> MathTextInset::clone() const
29 return auto_ptr<InsetBase>(new MathTextInset(*this));
33 MathInset::idx_type MathTextInset::pos2row(pos_type pos) const
35 for (pos_type r = 0, n = cache_.nargs(); r < n; ++r)
36 if (pos >= cache_.cellinfo_[r].begin_ && pos <= cache_.cellinfo_[r].end_)
38 lyxerr << "illegal row for pos " << pos << endl;
43 void MathTextInset::getScreenPos(idx_type /*idx*/, pos_type pos, int & x, int & y) const
45 idx_type const i = pos2row(pos);
46 pos_type const p = pos - cache_.cellinfo_[i].begin_;
47 cache_.getScreenPos(i, p, x, y);
48 y = cache_.cell(i).yo();
53 bool MathTextInset::idxUpDown2(LCursor & pos, bool up) const
55 // try to move only one screen row up or down if possible
56 idx_type i = pos2row(pos);
57 //lyxerr << "\nMathTextInset::idxUpDown() i: " << i << endl;
58 MathGridInset::CellInfo const & cell1 = cache_.cellinfo_[i];
59 int const x = cache_.cell(i).pos2x(pos - cell1.begin_, cell1.glue_);
66 if (i == cache_.nargs())
69 MathGridInset::CellInfo const & cell2 = cache_.cellinfo_[i];
70 pos = cell2.begin_ + cache_.cell(i).x2pos(x, cell2.glue_);
76 void MathTextInset::metrics(MetricsInfo & mi, Dimension & dim) const
80 // we do our own metrics fiddling
81 // save old positional information
82 int const old_xo = cache_.cell(0).xo();
83 int const old_yo = cache_.cell(0).yo();
86 cache_ = MathGridInset(1, 0);
93 for (size_type i = 0, n = cell(0).size(); i < n; ++i) {
94 //lyxerr << "at pos: " << i << " of " << n << " safepos: " << safepos
95 // << " curr: " << curr << " safe: " << safe
96 // << " spaces: " << spaces << endl;
99 // <char> <char> <char> <space> <char> <char> <char>
100 // ................... <safe>
101 // ..........................<curr>
102 // ....................<safepos>
104 // Special handling of spaces. We reached a safe position for breaking.
105 char const c = cell(0)[i]->getChar();
107 //lyxerr << "reached safe pos" << endl;
108 // we don't count the space into the safe pos
110 // we reset to this safepos if the next chunk does not fit
113 // restart chunk with size of the space
114 curr = cell(0)[i].width_;
119 // This is a regular char. Go on if we either don't care for
120 // the width limit or have not reached that limit.
121 curr += cell(0)[i].width_;
122 if (curr + safe <= mi.base.textwidth)
126 // We passed the limit. Create a row entry.
127 //lyxerr << "passed limit" << endl;
129 MathArray & ar = cache_.cell(cache_.nargs() - 1);
130 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
132 // we are here because we hit a hard newline
135 begin = i + 1; // next chunk starts after the newline
138 // but we had a space break before this position.
139 // so retreat to this position
140 //lyxerr << "... but had safe pos." << endl;
142 row.end_ = safepos; // this is position of the safe space
143 i = safepos; // i gets incremented at end of loop
144 begin = i + 1; // next chunk starts after the space
147 // This item is too large and it is the only one.
148 // We have no choice but to produce an overfull box.
149 lyxerr << "... without safe pos" << endl;
154 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
155 //lyxerr << "line: " << ar << endl;
156 // in any case, start the new row with empty boxes
160 // last row: put in everything else
162 MathArray & ar = cache_.cell(cache_.nargs() - 1);
163 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
165 row.end_ = cell(0).size();
166 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
167 //lyxerr << "last line: " << ar.data() << endl;
170 cache_.metrics(mi, dim_);
171 //lyxerr << "outer dim: " << dim_ << endl;
173 // reset position cache
174 for (idx_type i = 0; i < cache_.nargs(); ++i)
175 cache_.cell(i).setXY(old_xo, old_yo);
181 void MathTextInset::draw(PainterInfo & pi, int x, int y) const
183 cache_.draw(pi, x + 1, y);
187 void MathTextInset::drawSelection(PainterInfo & pi,
188 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
190 cache_.drawSelection(pi, idx1, pos1, idx2, pos2);