2 #include "math_nestinset.h"
3 #include "math_cursor.h"
4 #include "math_mathmlstream.h"
5 #include "math_parser.h"
6 #include "funcrequest.h"
8 #include "BufferView.h"
9 #include "frontends/Painter.h"
12 MathNestInset::MathNestInset(idx_type nargs)
13 : cells_(nargs), lock_(false)
17 MathInset::idx_type MathNestInset::nargs() const
23 MathArray & MathNestInset::cell(idx_type i)
29 MathArray const & MathNestInset::cell(idx_type i) const
35 void MathNestInset::getPos(idx_type idx, pos_type pos, int & x, int & y) const
37 MathArray const & ar = cell(idx);
38 x = ar.xo() + ar.pos2x(pos);
40 // move cursor visually into empty cells ("blue rectangles");
41 if (cell(idx).empty())
46 void MathNestInset::substitute(MathMacro const & m)
48 for (idx_type i = 0; i < nargs(); ++i)
49 cell(i).substitute(m);
53 void MathNestInset::metrics(MetricsInfo const & mi) const
56 for (idx_type i = 0; i < nargs(); ++i)
61 bool MathNestInset::idxNext(idx_type & idx, pos_type & pos) const
63 if (idx + 1 >= nargs())
71 bool MathNestInset::idxRight(idx_type & idx, pos_type & pos) const
73 return idxNext(idx, pos);
77 bool MathNestInset::idxPrev(idx_type & idx, pos_type & pos) const
82 pos = cell(idx).size();
87 bool MathNestInset::idxLeft(idx_type & idx, pos_type & pos) const
89 return idxPrev(idx, pos);
93 bool MathNestInset::idxFirst(idx_type & i, pos_type & pos) const
103 bool MathNestInset::idxLast(idx_type & i, pos_type & pos) const
108 pos = cell(i).size();
113 bool MathNestInset::idxHome(idx_type & /* idx */, pos_type & pos) const
122 bool MathNestInset::idxEnd(idx_type & idx, pos_type & pos) const
124 pos_type n = cell(idx).size();
132 void MathNestInset::dump() const
134 WriteStream os(lyxerr);
135 os << "---------------------------------------------\n";
138 for (idx_type i = 0; i < nargs(); ++i)
139 os << cell(i) << "\n";
140 os << "---------------------------------------------\n";
144 //void MathNestInset::draw(PainterInfo & pi, int x, int y) const
145 void MathNestInset::draw(PainterInfo &, int, int) const
149 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
155 void MathNestInset::drawSelection(PainterInfo & pi,
156 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
159 MathArray const & c = cell(idx1);
160 int x1 = c.xo() + c.pos2x(pos1);
161 int y1 = c.yo() - c.ascent();
162 int x2 = c.xo() + c.pos2x(pos2);
163 int y2 = c.yo() + c.descent();
164 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
166 for (idx_type i = 0; i < nargs(); ++i) {
167 if (idxBetween(i, idx1, idx2)) {
168 MathArray const & c = cell(i);
170 int y1 = c.yo() - c.ascent();
171 int x2 = c.xo() + c.width();
172 int y2 = c.yo() + c.descent();
173 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
180 void MathNestInset::validate(LaTeXFeatures & features) const
182 for (idx_type i = 0; i < nargs(); ++i)
183 cell(i).validate(features);
187 bool MathNestInset::match(MathAtom const & at) const
189 if (nargs() != at->nargs())
191 for (idx_type i = 0; i < nargs(); ++i)
192 if (!cell(i).match(at->cell(i)))
198 void MathNestInset::replace(ReplaceData & rep)
200 for (idx_type i = 0; i < nargs(); ++i)
201 cell(i).replace(rep);
205 bool MathNestInset::contains(MathArray const & ar) const
207 for (idx_type i = 0; i < nargs(); ++i)
208 if (cell(i).contains(ar))
214 bool MathNestInset::editing() const
216 return mathcursor && mathcursor->isInside(this);
220 bool MathNestInset::lock() const
226 void MathNestInset::lock(bool l)
232 bool MathNestInset::isActive() const
238 MathArray MathNestInset::glue() const
241 for (unsigned i = 0; i < nargs(); ++i)
247 void MathNestInset::write(WriteStream & os) const
249 os << '\\' << name().c_str();
250 for (unsigned i = 0; i < nargs(); ++i)
251 os << '{' << cell(i) << '}';
253 os.pendingSpace(true);
254 if (lock_ && !os.latex()) {
256 os.pendingSpace(true);
261 void MathNestInset::normalize(NormalStream & os) const
263 os << '[' << name().c_str();
264 for (unsigned i = 0; i < nargs(); ++i)
265 os << ' ' << cell(i);
270 void MathNestInset::notifyCursorLeaves(idx_type idx)
272 cell(idx).notifyCursorLeaves();
276 dispatch_result MathNestInset::dispatch
277 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
279 BufferView * bv = cmd.view();
281 switch (cmd.action) {
285 mathed_parse_cell(ar, cmd.argument);
286 cell(idx).insert(pos, ar);
291 case LFUN_PASTESELECTION:
294 FuncRequest(bv, LFUN_PASTE, bv->getClipboard()), idx, pos);
296 case LFUN_MOUSE_PRESS:
297 if (cmd.button() == mouse_button::button2)
298 return dispatch(FuncRequest(bv, LFUN_PASTESELECTION), idx, pos);
302 return MathInset::dispatch(cmd, idx, pos);
307 void MathNestInset::metricsMarkers(int) const
314 void MathNestInset::metricsMarkers2(int) const
321 void MathNestInset::drawMarkers(PainterInfo & pi, int x, int y) const
325 int t = x + dim_.width() - 1;
326 int d = y + dim_.descent();
327 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
328 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
329 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
330 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
334 void MathNestInset::drawMarkers2(PainterInfo & pi, int x, int y) const
338 drawMarkers(pi, x, y);
339 int t = x + dim_.width() - 1;
340 int a = y - dim_.ascent();
341 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
342 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
343 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
344 pi.pain.line(t - 3, a, t, a, LColor::mathframe);