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())
49 void MathNestInset::substitute(MathMacro const & m)
51 for (idx_type i = 0; i < nargs(); ++i)
52 cell(i).substitute(m);
56 void MathNestInset::metrics(MathMetricsInfo const & mi) const
58 MathMetricsInfo m = mi;
59 for (idx_type i = 0; i < nargs(); ++i)
64 void MathNestInset::metricsMarkers(int frame) const
71 void MathNestInset::metricsMarkers2(int frame) const
79 bool MathNestInset::idxNext(idx_type & idx, pos_type & pos) const
81 if (idx + 1 >= nargs())
89 bool MathNestInset::idxRight(idx_type & idx, pos_type & pos) const
91 return idxNext(idx, pos);
95 bool MathNestInset::idxPrev(idx_type & idx, pos_type & pos) const
100 pos = cell(idx).size();
105 bool MathNestInset::idxLeft(idx_type & idx, pos_type & pos) const
107 return idxPrev(idx, pos);
111 bool MathNestInset::idxFirst(idx_type & i, pos_type & pos) const
121 bool MathNestInset::idxLast(idx_type & i, pos_type & pos) const
126 pos = cell(i).size();
131 bool MathNestInset::idxHome(idx_type & /* idx */, pos_type & pos) const
140 bool MathNestInset::idxEnd(idx_type & idx, pos_type & pos) const
142 pos_type n = cell(idx).size();
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(MathPainterInfo & pi, int x, int y) const
163 void MathNestInset::draw(MathPainterInfo &, int, int) const
167 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
173 void MathNestInset::drawSelection(MathPainterInfo & 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::drawMarkers(MathPainterInfo & pi, int x, int y) const
202 int t = x + width() - 1;
203 int d = y + descent();
204 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
205 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
206 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
207 pi.pain.line(t - 2, d, t, d, LColor::mathframe);
211 void MathNestInset::drawMarkers2(MathPainterInfo & pi, int x, int y) const
215 drawMarkers(pi, x, y);
216 int t = x + width() - 1;
217 int a = y - ascent();
218 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
219 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
220 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
221 pi.pain.line(t - 2, a, t, a, LColor::mathframe);
225 void MathNestInset::validate(LaTeXFeatures & features) const
227 for (idx_type i = 0; i < nargs(); ++i)
228 cell(i).validate(features);
232 bool MathNestInset::match(MathAtom const & at) const
234 if (nargs() != at->nargs())
236 for (idx_type i = 0; i < nargs(); ++i)
237 if (!cell(i).match(at->cell(i)))
243 void MathNestInset::replace(ReplaceData & rep)
245 for (idx_type i = 0; i < nargs(); ++i)
246 cell(i).replace(rep);
250 bool MathNestInset::contains(MathArray const & ar) const
252 for (idx_type i = 0; i < nargs(); ++i)
253 if (cell(i).contains(ar))
259 bool MathNestInset::editing() const
261 return mathcursor && mathcursor->isInside(this);
265 bool MathNestInset::lock() const
271 void MathNestInset::lock(bool l)
277 bool MathNestInset::isActive() const
283 MathArray MathNestInset::glue() const
286 for (unsigned i = 0; i < nargs(); ++i)
292 void MathNestInset::write(WriteStream & os) const
294 os << '\\' << name().c_str();
295 for (unsigned i = 0; i < nargs(); ++i)
296 os << '{' << cell(i) << '}';
298 os.pendingSpace(true);
299 if (lock_ && !os.latex()) {
301 os.pendingSpace(true);
306 void MathNestInset::normalize(NormalStream & os) const
308 os << '[' << name().c_str();
309 for (unsigned i = 0; i < nargs(); ++i)
310 os << ' ' << cell(i);
315 void MathNestInset::notifyCursorLeaves(idx_type idx)
317 cell(idx).notifyCursorLeaves();
321 MathInset::result_type MathNestInset::dispatch
322 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
324 BufferView * bv = cmd.view();
326 switch (cmd.action) {
330 mathed_parse_cell(ar, cmd.argument);
331 cell(idx).insert(pos, ar);
336 case LFUN_PASTESELECTION:
339 FuncRequest(bv, LFUN_PASTE, bv->getClipboard()), idx, pos);
341 case LFUN_MOUSE_PRESS:
342 if (cmd.button() == mouse_button::button2)
343 return dispatch(FuncRequest(bv, LFUN_PASTESELECTION), idx, pos);
347 return MathInset::dispatch(cmd, idx, pos);