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 "funcrequest.h"
19 #include "BufferView.h"
20 #include "frontends/Painter.h"
23 MathNestInset::MathNestInset(idx_type nargs)
24 : cells_(nargs), lock_(false)
28 MathInset::idx_type MathNestInset::nargs() const
34 MathArray & MathNestInset::cell(idx_type i)
40 MathArray const & MathNestInset::cell(idx_type i) const
46 void MathNestInset::getPos(idx_type idx, pos_type pos, int & x, int & y) const
48 MathArray const & ar = cell(idx);
49 x = ar.xo() + ar.pos2x(pos);
51 // move cursor visually into empty cells ("blue rectangles");
52 if (cell(idx).empty())
57 void MathNestInset::substitute(MathMacro const & m)
59 for (idx_type i = 0; i < nargs(); ++i)
60 cell(i).substitute(m);
64 void MathNestInset::metrics(MetricsInfo const & mi) const
67 for (idx_type i = 0; i < nargs(); ++i)
72 bool MathNestInset::idxNext(idx_type & idx, pos_type & pos) const
74 if (idx + 1 >= nargs())
82 bool MathNestInset::idxRight(idx_type & idx, pos_type & pos) const
84 return idxNext(idx, pos);
88 bool MathNestInset::idxPrev(idx_type & idx, pos_type & pos) const
93 pos = cell(idx).size();
98 bool MathNestInset::idxLeft(idx_type & idx, pos_type & pos) const
100 return idxPrev(idx, pos);
104 bool MathNestInset::idxFirst(idx_type & idx, pos_type & pos) const
114 bool MathNestInset::idxLast(idx_type & idx, pos_type & pos) const
119 pos = cell(idx).size();
124 bool MathNestInset::idxHome(idx_type & /* idx */, pos_type & pos) const
133 bool MathNestInset::idxEnd(idx_type & idx, pos_type & pos) const
135 pos_type n = cell(idx).size();
143 void MathNestInset::dump() const
145 WriteStream os(lyxerr);
146 os << "---------------------------------------------\n";
149 for (idx_type i = 0; i < nargs(); ++i)
150 os << cell(i) << "\n";
151 os << "---------------------------------------------\n";
155 //void MathNestInset::draw(PainterInfo & pi, int x, int y) const
156 void MathNestInset::draw(PainterInfo &, int, int) const
160 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
166 void MathNestInset::drawSelection(PainterInfo & pi,
167 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
170 MathArray const & c = cell(idx1);
171 int x1 = c.xo() + c.pos2x(pos1);
172 int y1 = c.yo() - c.ascent();
173 int x2 = c.xo() + c.pos2x(pos2);
174 int y2 = c.yo() + c.descent();
175 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
177 for (idx_type i = 0; i < nargs(); ++i) {
178 if (idxBetween(i, idx1, idx2)) {
179 MathArray const & c = cell(i);
181 int y1 = c.yo() - c.ascent();
182 int x2 = c.xo() + c.width();
183 int y2 = c.yo() + c.descent();
184 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
191 void MathNestInset::validate(LaTeXFeatures & features) const
193 for (idx_type i = 0; i < nargs(); ++i)
194 cell(i).validate(features);
198 bool MathNestInset::match(MathAtom const & at) const
200 if (nargs() != at->nargs())
202 for (idx_type i = 0; i < nargs(); ++i)
203 if (!cell(i).match(at->cell(i)))
209 void MathNestInset::replace(ReplaceData & rep)
211 for (idx_type i = 0; i < nargs(); ++i)
212 cell(i).replace(rep);
216 bool MathNestInset::contains(MathArray const & ar) const
218 for (idx_type i = 0; i < nargs(); ++i)
219 if (cell(i).contains(ar))
225 bool MathNestInset::editing() const
227 return mathcursor && mathcursor->isInside(this);
231 bool MathNestInset::lock() const
237 void MathNestInset::lock(bool l)
243 bool MathNestInset::isActive() const
249 MathArray MathNestInset::glue() const
252 for (unsigned i = 0; i < nargs(); ++i)
258 void MathNestInset::write(WriteStream & os) const
260 os << '\\' << name().c_str();
261 for (unsigned i = 0; i < nargs(); ++i)
262 os << '{' << cell(i) << '}';
264 os.pendingSpace(true);
265 if (lock_ && !os.latex()) {
267 os.pendingSpace(true);
272 void MathNestInset::normalize(NormalStream & os) const
274 os << '[' << name().c_str();
275 for (unsigned i = 0; i < nargs(); ++i)
276 os << ' ' << cell(i);
281 void MathNestInset::notifyCursorLeaves(idx_type idx)
283 cell(idx).notifyCursorLeaves();
287 dispatch_result MathNestInset::dispatch
288 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
290 BufferView * bv = cmd.view();
292 switch (cmd.action) {
296 mathed_parse_cell(ar, cmd.argument);
297 cell(idx).insert(pos, ar);
302 case LFUN_PASTESELECTION:
305 FuncRequest(bv, LFUN_PASTE, bv->getClipboard()), idx, pos);
307 case LFUN_MOUSE_PRESS:
308 if (cmd.button() == mouse_button::button2)
309 return dispatch(FuncRequest(bv, LFUN_PASTESELECTION), idx, pos);
313 return MathInset::dispatch(cmd, idx, pos);
318 void MathNestInset::metricsMarkers(int) const
325 void MathNestInset::metricsMarkers2(int) const
332 void MathNestInset::drawMarkers(PainterInfo & pi, int x, int y) const
336 int t = x + dim_.width() - 1;
337 int d = y + dim_.descent();
338 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
339 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
340 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
341 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
345 void MathNestInset::drawMarkers2(PainterInfo & pi, int x, int y) const
349 drawMarkers(pi, x, y);
350 int t = x + dim_.width() - 1;
351 int a = y - dim_.ascent();
352 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
353 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
354 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
355 pi.pain.line(t - 3, a, t, a, LColor::mathframe);