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::getPos(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(idx_type & idx, pos_type & pos) const
76 if (idx + 1 >= nargs())
84 bool MathNestInset::idxRight(idx_type & idx, pos_type & pos) const
86 return idxNext(idx, pos);
90 bool MathNestInset::idxPrev(idx_type & idx, pos_type & pos) const
95 pos = cell(idx).size();
100 bool MathNestInset::idxLeft(idx_type & idx, pos_type & pos) const
102 return idxPrev(idx, pos);
106 bool MathNestInset::idxFirst(idx_type & idx, pos_type & pos) const
116 bool MathNestInset::idxLast(idx_type & idx, pos_type & pos) const
121 pos = cell(idx).size();
126 bool MathNestInset::idxHome(idx_type & /* idx */, pos_type & pos) const
135 bool MathNestInset::idxEnd(idx_type & idx, pos_type & pos) const
137 pos_type n = cell(idx).size();
145 void MathNestInset::dump() const
147 WriteStream os(lyxerr);
148 os << "---------------------------------------------\n";
151 for (idx_type i = 0; i < nargs(); ++i)
152 os << cell(i) << "\n";
153 os << "---------------------------------------------\n";
157 //void MathNestInset::draw(PainterInfo & pi, int x, int y) const
158 void MathNestInset::draw(PainterInfo &, int, int) const
162 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
168 void MathNestInset::drawSelection(PainterInfo & pi,
169 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
172 MathArray const & c = cell(idx1);
173 int x1 = c.xo() + c.pos2x(pos1);
174 int y1 = c.yo() - c.ascent();
175 int x2 = c.xo() + c.pos2x(pos2);
176 int y2 = c.yo() + c.descent();
177 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
179 for (idx_type i = 0; i < nargs(); ++i) {
180 if (idxBetween(i, idx1, idx2)) {
181 MathArray const & c = cell(i);
183 int y1 = c.yo() - c.ascent();
184 int x2 = c.xo() + c.width();
185 int y2 = c.yo() + c.descent();
186 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
193 void MathNestInset::validate(LaTeXFeatures & features) const
195 for (idx_type i = 0; i < nargs(); ++i)
196 cell(i).validate(features);
200 void MathNestInset::replace(ReplaceData & rep)
202 for (idx_type i = 0; i < nargs(); ++i)
203 cell(i).replace(rep);
207 bool MathNestInset::contains(MathArray const & ar) const
209 for (idx_type i = 0; i < nargs(); ++i)
210 if (cell(i).contains(ar))
216 bool MathNestInset::editing() const
218 return mathcursor && mathcursor->isInside(this);
222 bool MathNestInset::lock() const
228 void MathNestInset::lock(bool l)
234 bool MathNestInset::isActive() const
240 MathArray MathNestInset::glue() const
243 for (unsigned i = 0; i < nargs(); ++i)
249 void MathNestInset::write(WriteStream & os) const
251 os << '\\' << name().c_str();
252 for (unsigned i = 0; i < nargs(); ++i)
253 os << '{' << cell(i) << '}';
255 os.pendingSpace(true);
256 if (lock_ && !os.latex()) {
258 os.pendingSpace(true);
263 void MathNestInset::normalize(NormalStream & os) const
265 os << '[' << name().c_str();
266 for (unsigned i = 0; i < nargs(); ++i)
267 os << ' ' << cell(i);
272 void MathNestInset::notifyCursorLeaves(idx_type idx)
274 cell(idx).notifyCursorLeaves();
279 MathNestInset::priv_dispatch(FuncRequest const & cmd,
280 idx_type & idx, pos_type & pos)
282 BufferView * bv = cmd.view();
284 switch (cmd.action) {
288 mathed_parse_cell(ar, cmd.argument);
289 cell(idx).insert(pos, ar);
291 return DispatchResult(true, true);
294 case LFUN_PASTESELECTION:
297 FuncRequest(bv, LFUN_PASTE, bv->getClipboard()), idx, pos);
299 case LFUN_MOUSE_PRESS:
300 if (cmd.button() == mouse_button::button2)
301 return priv_dispatch(FuncRequest(bv, LFUN_PASTESELECTION), idx, pos);
302 return DispatchResult(false);
305 return MathInset::priv_dispatch(cmd, idx, pos);
310 void MathNestInset::metricsMarkers(int) const
317 void MathNestInset::metricsMarkers2(int) const
324 void MathNestInset::drawMarkers(PainterInfo & pi, int x, int y) const
328 int t = x + dim_.width() - 1;
329 int d = y + dim_.descent();
330 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
331 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
332 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
333 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
337 void MathNestInset::drawMarkers2(PainterInfo & pi, int x, int y) const
341 drawMarkers(pi, x, y);
342 int t = x + dim_.width() - 1;
343 int a = y - dim_.ascent();
344 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
345 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
346 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
347 pi.pain.line(t - 3, a, t, a, LColor::mathframe);