5 #include "math_nestinset.h"
6 #include "math_cursor.h"
7 #include "math_mathmlstream.h"
8 #include "math_parser.h"
9 #include "funcrequest.h"
11 #include "BufferView.h"
12 #include "frontends/Painter.h"
15 MathNestInset::MathNestInset(idx_type nargs)
16 : MathDimInset(), cells_(nargs), lock_(false)
20 MathInset::idx_type MathNestInset::nargs() const
26 MathArray & MathNestInset::cell(idx_type i)
32 MathArray const & MathNestInset::cell(idx_type i) const
38 void MathNestInset::getPos(idx_type idx, pos_type pos, int & x, int & y) const
40 MathArray const & ar = cell(idx);
41 x = ar.xo() + ar.pos2x(pos);
43 // move cursor visually into empty cells ("blue rectangles");
44 if (cell(idx).empty())
48 void MathNestInset::substitute(MathMacro const & m)
50 for (idx_type i = 0; i < nargs(); ++i)
51 cell(i).substitute(m);
55 void MathNestInset::metrics(MathMetricsInfo const & mi) const
57 MathMetricsInfo m = mi;
58 for (idx_type i = 0; i < nargs(); ++i)
63 void MathNestInset::metricsMarkers(int frame) const
70 void MathNestInset::metricsMarkers2(int frame) const
78 bool MathNestInset::idxNext(idx_type & idx, pos_type & pos) const
80 if (idx + 1 >= nargs())
88 bool MathNestInset::idxRight(idx_type & idx, pos_type & pos) const
90 return idxNext(idx, pos);
94 bool MathNestInset::idxPrev(idx_type & idx, pos_type & pos) const
99 pos = cell(idx).size();
104 bool MathNestInset::idxLeft(idx_type & idx, pos_type & pos) const
106 return idxPrev(idx, pos);
110 bool MathNestInset::idxFirst(idx_type & i, pos_type & pos) const
120 bool MathNestInset::idxLast(idx_type & i, pos_type & pos) const
125 pos = cell(i).size();
130 bool MathNestInset::idxHome(idx_type & /* idx */, pos_type & pos) const
139 bool MathNestInset::idxEnd(idx_type & idx, pos_type & pos) const
141 pos_type n = cell(idx).size();
149 void MathNestInset::dump() const
151 WriteStream os(lyxerr);
152 os << "---------------------------------------------\n";
155 for (idx_type i = 0; i < nargs(); ++i)
156 os << cell(i) << "\n";
157 os << "---------------------------------------------\n";
161 //void MathNestInset::draw(MathPainterInfo & pi, int x, int y) const
162 void MathNestInset::draw(MathPainterInfo &, int, int) const
166 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
172 void MathNestInset::drawSelection(MathPainterInfo & pi,
173 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
176 MathArray const & c = cell(idx1);
177 int x1 = c.xo() + c.pos2x(pos1);
178 int y1 = c.yo() - c.ascent();
179 int x2 = c.xo() + c.pos2x(pos2);
180 int y2 = c.yo() + c.descent();
181 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
183 for (idx_type i = 0; i < nargs(); ++i) {
184 if (idxBetween(i, idx1, idx2)) {
185 MathArray const & c = cell(i);
187 int y1 = c.yo() - c.ascent();
188 int x2 = c.xo() + c.width();
189 int y2 = c.yo() + c.descent();
190 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
197 void MathNestInset::drawMarkers(MathPainterInfo & pi, int x, int y) const
201 int t = x + width() - 1;
202 int d = y + descent();
203 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
204 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
205 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
206 pi.pain.line(t - 2, d, t, d, LColor::mathframe);
210 void MathNestInset::drawMarkers2(MathPainterInfo & pi, int x, int y) const
214 drawMarkers(pi, x, y);
215 int t = x + width() - 1;
216 int a = y - ascent();
217 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
218 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
219 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
220 pi.pain.line(t - 2, a, t, a, LColor::mathframe);
224 void MathNestInset::validate(LaTeXFeatures & features) const
226 for (idx_type i = 0; i < nargs(); ++i)
227 cell(i).validate(features);
231 bool MathNestInset::match(MathAtom const & at) const
233 if (nargs() != at->nargs())
235 for (idx_type i = 0; i < nargs(); ++i)
236 if (!cell(i).match(at->cell(i)))
242 void MathNestInset::replace(ReplaceData & rep)
244 for (idx_type i = 0; i < nargs(); ++i)
245 cell(i).replace(rep);
249 bool MathNestInset::contains(MathArray const & ar) const
251 for (idx_type i = 0; i < nargs(); ++i)
252 if (cell(i).contains(ar))
258 bool MathNestInset::editing() const
260 return mathcursor && mathcursor->isInside(this);
264 bool MathNestInset::lock() const
270 void MathNestInset::lock(bool l)
276 bool MathNestInset::isActive() const
282 MathArray MathNestInset::glue() const
285 for (unsigned i = 0; i < nargs(); ++i)
291 void MathNestInset::write(WriteStream & os) const
293 os << '\\' << name().c_str();
294 for (unsigned i = 0; i < nargs(); ++i)
295 os << '{' << cell(i) << '}';
297 os.pendingSpace(true);
298 if (lock_ && !os.latex()) {
300 os.pendingSpace(true);
305 void MathNestInset::normalize(NormalStream & os) const
307 os << '[' << name().c_str();
308 for (unsigned i = 0; i < nargs(); ++i)
309 os << ' ' << cell(i);
314 void MathNestInset::notifyCursorLeaves()
318 MathInset::result_type MathNestInset::dispatch
319 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
321 BufferView * bv = cmd.view();
323 switch (cmd.action) {
327 mathed_parse_cell(ar, cmd.argument);
328 cell(idx).insert(pos, ar);
333 case LFUN_PASTESELECTION:
336 FuncRequest(bv, LFUN_PASTE, bv->getClipboard()), idx, pos);
338 case LFUN_MOUSE_PRESS:
339 if (cmd.button() == mouse_button::button2)
340 return dispatch(FuncRequest(bv, LFUN_PASTESELECTION), idx, pos);
344 return MathInset::dispatch(cmd, idx, pos);