2 * \file math_nestinset.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_nestinset.h"
14 #include "math_cursor.h"
15 #include "math_mathmlstream.h"
16 #include "math_parser.h"
17 #include "BufferView.h"
18 #include "dispatchresult.h"
20 #include "funcrequest.h"
22 #include "frontends/Painter.h"
25 MathNestInset::MathNestInset(idx_type nargs)
26 : cells_(nargs), lock_(false)
30 MathInset::idx_type MathNestInset::nargs() const
36 MathArray & MathNestInset::cell(idx_type i)
42 MathArray const & MathNestInset::cell(idx_type i) const
48 void MathNestInset::getScreenPos(idx_type idx, pos_type pos, int & x, int & y) const
50 MathArray const & ar = cell(idx);
51 x = ar.xo() + ar.pos2x(pos);
53 // move cursor visually into empty cells ("blue rectangles");
54 if (cell(idx).empty())
59 void MathNestInset::substitute(MathMacro const & m)
61 for (idx_type i = 0; i < nargs(); ++i)
62 cell(i).substitute(m);
66 void MathNestInset::metrics(MetricsInfo const & mi) const
69 for (idx_type i = 0; i < nargs(); ++i)
74 bool MathNestInset::idxNext(LCursor & cur) const
76 if (cur.idx() + 1 >= nargs())
84 bool MathNestInset::idxRight(LCursor & cur) const
90 bool MathNestInset::idxPrev(LCursor & cur) const
95 cur.pos() = cur.lastpos();
100 bool MathNestInset::idxLeft(LCursor & cur) const
106 bool MathNestInset::idxFirst(LCursor & cur) const
116 bool MathNestInset::idxLast(LCursor & cur) const
120 cur.idx() = nargs() - 1;
121 cur.pos() = cur.lastpos();
126 bool MathNestInset::idxHome(LCursor & cur) const
135 bool MathNestInset::idxEnd(LCursor & cur) const
137 if (cur.lastpos() == cur.lastpos())
139 cur.pos() = cur.lastpos();
144 void MathNestInset::dump() const
146 WriteStream os(lyxerr);
147 os << "---------------------------------------------\n";
150 for (idx_type i = 0; i < nargs(); ++i)
151 os << cell(i) << "\n";
152 os << "---------------------------------------------\n";
156 //void MathNestInset::draw(PainterInfo & pi, int x, int y) const
157 void MathNestInset::draw(PainterInfo &, int, int) const
161 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
167 void MathNestInset::drawSelection(PainterInfo & pi,
168 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
171 MathArray const & c = cell(idx1);
172 int x1 = c.xo() + c.pos2x(pos1);
173 int y1 = c.yo() - c.ascent();
174 int x2 = c.xo() + c.pos2x(pos2);
175 int y2 = c.yo() + c.descent();
176 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
178 for (idx_type i = 0; i < nargs(); ++i) {
179 if (idxBetween(i, idx1, idx2)) {
180 MathArray const & c = cell(i);
182 int y1 = c.yo() - c.ascent();
183 int x2 = c.xo() + c.width();
184 int y2 = c.yo() + c.descent();
185 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
192 void MathNestInset::validate(LaTeXFeatures & features) const
194 for (idx_type i = 0; i < nargs(); ++i)
195 cell(i).validate(features);
199 void MathNestInset::replace(ReplaceData & rep)
201 for (idx_type i = 0; i < nargs(); ++i)
202 cell(i).replace(rep);
206 bool MathNestInset::contains(MathArray const & ar) const
208 for (idx_type i = 0; i < nargs(); ++i)
209 if (cell(i).contains(ar))
215 bool MathNestInset::editing() const
217 return mathcursor && mathcursor->isInside(this);
221 bool MathNestInset::lock() const
227 void MathNestInset::lock(bool l)
233 bool MathNestInset::isActive() const
239 MathArray MathNestInset::glue() const
242 for (unsigned i = 0; i < nargs(); ++i)
248 void MathNestInset::write(WriteStream & os) const
250 os << '\\' << name().c_str();
251 for (unsigned i = 0; i < nargs(); ++i)
252 os << '{' << cell(i) << '}';
254 os.pendingSpace(true);
255 if (lock_ && !os.latex()) {
257 os.pendingSpace(true);
262 void MathNestInset::normalize(NormalStream & os) const
264 os << '[' << name().c_str();
265 for (unsigned i = 0; i < nargs(); ++i)
266 os << ' ' << cell(i);
271 void MathNestInset::notifyCursorLeaves(idx_type idx)
273 cell(idx).notifyCursorLeaves();
278 MathNestInset::priv_dispatch(BufferView & bv, FuncRequest const & cmd)
280 CursorSlice & cur = cursorTip(bv);
281 switch (cmd.action) {
285 mathed_parse_cell(ar, cmd.argument);
286 cur.cell().insert(cur.pos(), ar);
287 cur.pos() += ar.size();
288 return DispatchResult(true, true);
291 case LFUN_PASTESELECTION:
292 return dispatch(bv, FuncRequest(LFUN_PASTE, bv.getClipboard()));
294 case LFUN_MOUSE_PRESS:
295 if (cmd.button() == mouse_button::button2)
296 return priv_dispatch(bv, FuncRequest(LFUN_PASTESELECTION));
297 return DispatchResult(false);
300 return MathInset::priv_dispatch(bv, cmd);
305 void MathNestInset::metricsMarkers(int) const
312 void MathNestInset::metricsMarkers2(int) const
319 void MathNestInset::drawMarkers(PainterInfo & pi, int x, int y) const
323 int t = x + dim_.width() - 1;
324 int d = y + dim_.descent();
325 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
326 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
327 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
328 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
332 void MathNestInset::drawMarkers2(PainterInfo & pi, int x, int y) const
336 drawMarkers(pi, x, y);
337 int t = x + dim_.width() - 1;
338 int a = y - dim_.ascent();
339 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
340 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
341 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
342 pi.pain.line(t - 3, a, t, a, LColor::mathframe);