2 #include "math_parboxinset.h"
3 #include "math_mathmlstream.h"
4 #include "math_streamstr.h"
10 MathParboxInset::MathParboxInset()
11 : MathNestInset(1), lyx_width_(0), tex_width_("0mm"),
14 lyxerr << "constructing MathParboxInset\n";
18 MathInset * MathParboxInset::clone() const
20 return new MathParboxInset(*this);
24 void MathParboxInset::setPosition(string const & p)
26 position_ = p.size() > 0 ? p[0] : 'c';
30 void MathParboxInset::setWidth(string const & w)
33 lyx_width_ = LyXLength(w).inBP();
34 lyxerr << "setting " << w << " to " << lyx_width_ << " pixel\n";
38 int MathParboxInset::screenrows() const
44 int MathParboxInset::pos2row(pos_type pos) const
46 for (int r = 0, n = rows_.size(); r < n; ++r)
47 if (pos >= rows_[r].begin && pos <= rows_[r].end)
49 lyxerr << "illegal row for pos " << pos << "\n";
54 void MathParboxInset::getPos(idx_type idx, pos_type pos, int & x, int & y) const
56 int const r = pos2row(pos);
57 MathXArray const & ar = cells_[idx];
58 x = ar.xo() + ar.pos2x(rows_[r].begin, pos, rows_[r].glue);
59 y = ar.yo() + rows_[r].yo;
60 // move cursor visually into empty cells ("blue rectangles");
63 //lyxerr << "getPos cursor at pos " << pos << " in row " << r
64 // << " x: " << x << " y: " << y << "\n";
68 bool MathParboxInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
71 // try to move only one screen row up or down if possible
72 int row = pos2row(pos);
73 int const x = cells_[idx].pos2x(rows_[row].begin, pos, rows_[row].glue);
80 if (row == screenrows())
83 pos = xcell(0).x2pos(rows_[row].begin, x, rows_[row].glue);
88 void MathParboxInset::metrics(MathMetricsInfo & mi) const
90 MathFontSetChanger dummy(mi.base, "textnormal");
92 // we do our own metrics fiddling
98 dim_ = xcell(0).metrics(mi);
102 vector<Dimension> dims;
103 xcell(0).metricsExternal(mi, dims);
108 safe.clear(mi.base.font);
109 curr.clear(mi.base.font);
113 for (size_type i = 0, n = cell(0).size(); i < n; ++i) {
114 //lyxerr << "at pos: " << i << " of " << n << " safepos: " << safepos
115 // << " curr: " << curr << " safe: " << safe
116 // << " spaces: " << spaces << endl;
120 // <char> <char> <char> <space> <char> <char> <char>
121 // ................... <safe>
122 // ..........................<curr>
123 // ....................<safepos>
125 // Special handling of spaces. We reached a safe position for breaking.
126 if (cell(0)[i]->getChar() == ' ') {
127 //lyxerr << "reached safe pos\n";
128 // we don't count the space into the safe pos
130 // we reset to this safepos if the next chunk does not fit
133 // restart chunk with size of the space
134 curr.clear(mi.base.font);
139 // This is a regular char. Go on if we either don't care for
140 // the width limit or have not reached that limit.
142 if (curr.w + safe.w <= lyx_width_)
145 // We passed the limit. Create a row entry.
146 //lyxerr << "passed limit\n";
149 // but we had a space break before this position.
150 // so retreat to this position
151 int glue = lyx_width_ - safe.w + dims[safepos].w;
153 row.glue = glue / spaces;
155 row.end = safepos; // this is position of the safe space
156 i = safepos; // i gets incremented at end of loop
157 begin = i + 1; // next chunk starts after the space
158 //lyxerr << "... but had safe pos. glue: " << row.glue << "\n";
159 //lyxerr << " safe.w: " << safe.w
160 // << " dim.w: " << dims[safepos].w << " spaces: " << spaces << "\n";
163 lyxerr << "... without safe pos\n";
164 // This item is too large and it is the only one.
165 // We have no choice but to produce an overfull box.
166 row.dim = curr; // safe should be 0.
167 row.glue = 0; // does not matter
173 yo += row.dim.height();
174 rows_.push_back(row);
175 // in any case, start the new row with empty boxes
176 curr.clear(mi.base.font);
177 safe.clear(mi.base.font);
179 // last row: put in everything else
184 row.end = cell(0).size();
185 row.glue = 0; // last line is left aligned
187 rows_.push_back(row);
191 dim_.a = rows_.front().dim.a;
192 dim_.d = rows_.back().dim.d + yo;
194 xcell(0).setDim(dim_);
199 void MathParboxInset::draw(MathPainterInfo & pi, int x, int y) const
201 MathFontSetChanger dummy(pi.base, "textnormal");
203 xcell(0).draw(pi, x + 1, y);
205 xcell(0).drawExternal(pi, x + 1, y, rows_);
207 drawMarkers2(pi, x, y);
211 void MathParboxInset::drawSelection(MathPainterInfo & pi,
212 idx_type, pos_type pos1, idx_type, pos_type pos2) const
214 int row1 = pos2row(pos1);
215 int row2 = pos2row(pos2);
218 MathXArray & c = xcell(0);
219 int x1 = c.xo() + c.pos2x(i1.pos_);
220 int y1 = c.yo() - c.ascent();
221 int x2 = c.xo() + c.pos2x(i2.pos_);
222 int y2 = c.yo() + c.descent();
223 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
225 vector<MathInset::idx_type> indices = idxBetween(idx1, idx2);
226 for (unsigned i = 0; i < indices.size(); ++i) {
227 MathXArray & c = i1.xcell(indices[i]);
229 int y1 = c.yo() - c.ascent();
230 int x2 = c.xo() + c.width();
231 int y2 = c.yo() + c.descent();
232 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
239 void MathParboxInset::write(WriteStream & os) const
242 if (position_ != 'c')
243 os << '[' << position_ << ']';
244 os << '{' << tex_width_ << "}{" << cell(0) << '}';
248 void MathParboxInset::infoize(std::ostream & os) const
250 os << "Box: Parbox " << tex_width_;