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(BufferView & bv) const
76 CursorSlice & cur = cursorTip(bv);
77 if (cur.idx() + 1 >= nargs())
85 bool MathNestInset::idxRight(BufferView & bv) const
91 bool MathNestInset::idxPrev(BufferView & bv) const
93 CursorSlice & cur = cursorTip(bv);
97 cur.pos() = cur.lastpos();
102 bool MathNestInset::idxLeft(BufferView & bv) const
108 bool MathNestInset::idxFirst(BufferView & bv) const
110 CursorSlice & cur = cursorTip(bv);
119 bool MathNestInset::idxLast(BufferView & bv) const
121 CursorSlice & cur = cursorTip(bv);
124 cur.idx() = nargs() - 1;
125 cur.pos() = cur.lastpos();
130 bool MathNestInset::idxHome(BufferView & bv) const
132 CursorSlice & cur = cursorTip(bv);
140 bool MathNestInset::idxEnd(BufferView & bv) const
142 CursorSlice & cur = cursorTip(bv);
143 if (cur.lastpos() == cur.lastpos())
145 cur.pos() = cur.lastpos();
150 void MathNestInset::dump() const
152 WriteStream os(lyxerr);
153 os << "---------------------------------------------\n";
156 for (idx_type i = 0; i < nargs(); ++i)
157 os << cell(i) << "\n";
158 os << "---------------------------------------------\n";
162 //void MathNestInset::draw(PainterInfo & pi, int x, int y) const
163 void MathNestInset::draw(PainterInfo &, int, int) const
167 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
173 void MathNestInset::drawSelection(PainterInfo & pi,
174 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
177 MathArray const & c = cell(idx1);
178 int x1 = c.xo() + c.pos2x(pos1);
179 int y1 = c.yo() - c.ascent();
180 int x2 = c.xo() + c.pos2x(pos2);
181 int y2 = c.yo() + c.descent();
182 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
184 for (idx_type i = 0; i < nargs(); ++i) {
185 if (idxBetween(i, idx1, idx2)) {
186 MathArray const & c = cell(i);
188 int y1 = c.yo() - c.ascent();
189 int x2 = c.xo() + c.width();
190 int y2 = c.yo() + c.descent();
191 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
198 void MathNestInset::validate(LaTeXFeatures & features) const
200 for (idx_type i = 0; i < nargs(); ++i)
201 cell(i).validate(features);
205 void MathNestInset::replace(ReplaceData & rep)
207 for (idx_type i = 0; i < nargs(); ++i)
208 cell(i).replace(rep);
212 bool MathNestInset::contains(MathArray const & ar) const
214 for (idx_type i = 0; i < nargs(); ++i)
215 if (cell(i).contains(ar))
221 bool MathNestInset::editing() const
223 return mathcursor && mathcursor->isInside(this);
227 bool MathNestInset::lock() const
233 void MathNestInset::lock(bool l)
239 bool MathNestInset::isActive() const
245 MathArray MathNestInset::glue() const
248 for (unsigned i = 0; i < nargs(); ++i)
254 void MathNestInset::write(WriteStream & os) const
256 os << '\\' << name().c_str();
257 for (unsigned i = 0; i < nargs(); ++i)
258 os << '{' << cell(i) << '}';
260 os.pendingSpace(true);
261 if (lock_ && !os.latex()) {
263 os.pendingSpace(true);
268 void MathNestInset::normalize(NormalStream & os) const
270 os << '[' << name().c_str();
271 for (unsigned i = 0; i < nargs(); ++i)
272 os << ' ' << cell(i);
277 void MathNestInset::notifyCursorLeaves(idx_type idx)
279 cell(idx).notifyCursorLeaves();
284 MathNestInset::priv_dispatch(BufferView & bv, FuncRequest const & cmd)
286 CursorSlice & cur = cursorTip(bv);
287 switch (cmd.action) {
291 mathed_parse_cell(ar, cmd.argument);
292 cur.cell().insert(cur.pos(), ar);
293 cur.pos() += ar.size();
294 return DispatchResult(true, true);
297 case LFUN_PASTESELECTION:
298 return dispatch(bv, FuncRequest(LFUN_PASTE, bv.getClipboard()));
300 case LFUN_MOUSE_PRESS:
301 if (cmd.button() == mouse_button::button2)
302 return priv_dispatch(bv, FuncRequest(LFUN_PASTESELECTION));
303 return DispatchResult(false);
306 return MathInset::priv_dispatch(bv, cmd);
311 void MathNestInset::metricsMarkers(int) const
318 void MathNestInset::metricsMarkers2(int) const
325 void MathNestInset::drawMarkers(PainterInfo & pi, int x, int y) const
329 int t = x + dim_.width() - 1;
330 int d = y + dim_.descent();
331 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
332 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
333 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
334 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
338 void MathNestInset::drawMarkers2(PainterInfo & pi, int x, int y) const
342 drawMarkers(pi, x, y);
343 int t = x + dim_.width() - 1;
344 int a = y - dim_.ascent();
345 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
346 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
347 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
348 pi.pain.line(t - 3, a, t, a, LColor::mathframe);