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 bool MathNestInset::match(MathAtom const & at) const
202 if (nargs() != at->nargs())
204 for (idx_type i = 0; i < nargs(); ++i)
205 if (!cell(i).match(at->cell(i)))
211 void MathNestInset::replace(ReplaceData & rep)
213 for (idx_type i = 0; i < nargs(); ++i)
214 cell(i).replace(rep);
218 bool MathNestInset::contains(MathArray const & ar) const
220 for (idx_type i = 0; i < nargs(); ++i)
221 if (cell(i).contains(ar))
227 bool MathNestInset::editing() const
229 return mathcursor && mathcursor->isInside(this);
233 bool MathNestInset::lock() const
239 void MathNestInset::lock(bool l)
245 bool MathNestInset::isActive() const
251 MathArray MathNestInset::glue() const
254 for (unsigned i = 0; i < nargs(); ++i)
260 void MathNestInset::write(WriteStream & os) const
262 os << '\\' << name().c_str();
263 for (unsigned i = 0; i < nargs(); ++i)
264 os << '{' << cell(i) << '}';
266 os.pendingSpace(true);
267 if (lock_ && !os.latex()) {
269 os.pendingSpace(true);
274 void MathNestInset::normalize(NormalStream & os) const
276 os << '[' << name().c_str();
277 for (unsigned i = 0; i < nargs(); ++i)
278 os << ' ' << cell(i);
283 void MathNestInset::notifyCursorLeaves(idx_type idx)
285 cell(idx).notifyCursorLeaves();
290 MathNestInset::priv_dispatch(FuncRequest const & cmd,
291 idx_type & idx, pos_type & pos)
293 BufferView * bv = cmd.view();
295 switch (cmd.action) {
299 mathed_parse_cell(ar, cmd.argument);
300 cell(idx).insert(pos, ar);
305 case LFUN_PASTESELECTION:
308 FuncRequest(bv, LFUN_PASTE, bv->getClipboard()), idx, pos);
310 case LFUN_MOUSE_PRESS:
311 if (cmd.button() == mouse_button::button2)
312 return priv_dispatch(FuncRequest(bv, LFUN_PASTESELECTION), idx, pos);
316 return MathInset::priv_dispatch(cmd, idx, pos);
321 void MathNestInset::metricsMarkers(int) const
328 void MathNestInset::metricsMarkers2(int) const
335 void MathNestInset::drawMarkers(PainterInfo & pi, int x, int y) const
339 int t = x + dim_.width() - 1;
340 int d = y + dim_.descent();
341 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
342 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
343 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
344 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
348 void MathNestInset::drawMarkers2(PainterInfo & pi, int x, int y) const
352 drawMarkers(pi, x, y);
353 int t = x + dim_.width() - 1;
354 int a = y - dim_.ascent();
355 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
356 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
357 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
358 pi.pain.line(t - 3, a, t, a, LColor::mathframe);