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"
19 #include "funcrequest.h"
21 #include "frontends/Painter.h"
24 MathNestInset::MathNestInset(idx_type nargs)
25 : cells_(nargs), lock_(false)
29 MathInset::idx_type MathNestInset::nargs() const
35 MathArray & MathNestInset::cell(idx_type i)
41 MathArray const & MathNestInset::cell(idx_type i) const
47 void MathNestInset::getPos(idx_type idx, pos_type pos, int & x, int & y) const
49 MathArray const & ar = cell(idx);
50 x = ar.xo() + ar.pos2x(pos);
52 // move cursor visually into empty cells ("blue rectangles");
53 if (cell(idx).empty())
58 void MathNestInset::substitute(MathMacro const & m)
60 for (idx_type i = 0; i < nargs(); ++i)
61 cell(i).substitute(m);
65 void MathNestInset::metrics(MetricsInfo const & mi) const
68 for (idx_type i = 0; i < nargs(); ++i)
73 bool MathNestInset::idxNext(idx_type & idx, pos_type & pos) const
75 if (idx + 1 >= nargs())
83 bool MathNestInset::idxRight(idx_type & idx, pos_type & pos) const
85 return idxNext(idx, pos);
89 bool MathNestInset::idxPrev(idx_type & idx, pos_type & pos) const
94 pos = cell(idx).size();
99 bool MathNestInset::idxLeft(idx_type & idx, pos_type & pos) const
101 return idxPrev(idx, pos);
105 bool MathNestInset::idxFirst(idx_type & idx, pos_type & pos) const
115 bool MathNestInset::idxLast(idx_type & idx, pos_type & pos) const
120 pos = cell(idx).size();
125 bool MathNestInset::idxHome(idx_type & /* idx */, pos_type & pos) const
134 bool MathNestInset::idxEnd(idx_type & idx, pos_type & pos) const
136 pos_type n = cell(idx).size();
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 bool MathNestInset::match(MathAtom const & at) const
201 if (nargs() != at->nargs())
203 for (idx_type i = 0; i < nargs(); ++i)
204 if (!cell(i).match(at->cell(i)))
210 void MathNestInset::replace(ReplaceData & rep)
212 for (idx_type i = 0; i < nargs(); ++i)
213 cell(i).replace(rep);
217 bool MathNestInset::contains(MathArray const & ar) const
219 for (idx_type i = 0; i < nargs(); ++i)
220 if (cell(i).contains(ar))
226 bool MathNestInset::editing() const
228 return mathcursor && mathcursor->isInside(this);
232 bool MathNestInset::lock() const
238 void MathNestInset::lock(bool l)
244 bool MathNestInset::isActive() const
250 MathArray MathNestInset::glue() const
253 for (unsigned i = 0; i < nargs(); ++i)
259 void MathNestInset::write(WriteStream & os) const
261 os << '\\' << name().c_str();
262 for (unsigned i = 0; i < nargs(); ++i)
263 os << '{' << cell(i) << '}';
265 os.pendingSpace(true);
266 if (lock_ && !os.latex()) {
268 os.pendingSpace(true);
273 void MathNestInset::normalize(NormalStream & os) const
275 os << '[' << name().c_str();
276 for (unsigned i = 0; i < nargs(); ++i)
277 os << ' ' << cell(i);
282 void MathNestInset::notifyCursorLeaves(idx_type idx)
284 cell(idx).notifyCursorLeaves();
288 dispatch_result MathNestInset::dispatch
289 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
291 BufferView * bv = cmd.view();
293 switch (cmd.action) {
297 mathed_parse_cell(ar, cmd.argument);
298 cell(idx).insert(pos, ar);
303 case LFUN_PASTESELECTION:
306 FuncRequest(bv, LFUN_PASTE, bv->getClipboard()), idx, pos);
308 case LFUN_MOUSE_PRESS:
309 if (cmd.button() == mouse_button::button2)
310 return dispatch(FuncRequest(bv, LFUN_PASTESELECTION), idx, pos);
314 return MathInset::priv_dispatch(cmd, idx, pos);
319 void MathNestInset::metricsMarkers(int) const
326 void MathNestInset::metricsMarkers2(int) const
333 void MathNestInset::drawMarkers(PainterInfo & pi, int x, int y) const
337 int t = x + dim_.width() - 1;
338 int d = y + dim_.descent();
339 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
340 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
341 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
342 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
346 void MathNestInset::drawMarkers2(PainterInfo & pi, int x, int y) const
350 drawMarkers(pi, x, y);
351 int t = x + dim_.width() - 1;
352 int a = y - dim_.ascent();
353 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
354 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
355 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
356 pi.pain.line(t - 3, a, t, a, LColor::mathframe);