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 : MathDimInset(), 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(MathMetricsInfo const & mi) const
55 MathMetricsInfo m = mi;
56 for (idx_type i = 0; i < nargs(); ++i)
61 void MathNestInset::metricsMarkers(int frame) const
68 void MathNestInset::metricsMarkers2(int frame) const
76 bool MathNestInset::idxNext(idx_type & idx, pos_type & pos) const
78 if (idx + 1 >= nargs())
86 bool MathNestInset::idxRight(idx_type & idx, pos_type & pos) const
88 return idxNext(idx, pos);
92 bool MathNestInset::idxPrev(idx_type & idx, pos_type & pos) const
97 pos = cell(idx).size();
102 bool MathNestInset::idxLeft(idx_type & idx, pos_type & pos) const
104 return idxPrev(idx, pos);
108 bool MathNestInset::idxFirst(idx_type & i, pos_type & pos) const
118 bool MathNestInset::idxLast(idx_type & i, pos_type & pos) const
123 pos = cell(i).size();
128 bool MathNestInset::idxHome(idx_type & /* idx */, pos_type & pos) const
137 bool MathNestInset::idxEnd(idx_type & idx, pos_type & pos) const
139 pos_type n = cell(idx).size();
147 void MathNestInset::dump() const
149 WriteStream os(lyxerr);
150 os << "---------------------------------------------\n";
153 for (idx_type i = 0; i < nargs(); ++i)
154 os << cell(i) << "\n";
155 os << "---------------------------------------------\n";
159 //void MathNestInset::draw(MathPainterInfo & pi, int x, int y) const
160 void MathNestInset::draw(MathPainterInfo &, int, int) const
164 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
170 void MathNestInset::drawSelection(MathPainterInfo & pi,
171 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
174 MathArray const & c = cell(idx1);
175 int x1 = c.xo() + c.pos2x(pos1);
176 int y1 = c.yo() - c.ascent();
177 int x2 = c.xo() + c.pos2x(pos2);
178 int y2 = c.yo() + c.descent();
179 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
181 for (idx_type i = 0; i < nargs(); ++i) {
182 if (idxBetween(i, idx1, idx2)) {
183 MathArray const & c = cell(i);
185 int y1 = c.yo() - c.ascent();
186 int x2 = c.xo() + c.width();
187 int y2 = c.yo() + c.descent();
188 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
195 void MathNestInset::drawMarkers(MathPainterInfo & pi, int x, int y) const
199 int t = x + width() - 1;
200 int d = y + descent();
201 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
202 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
203 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
204 pi.pain.line(t - 2, d, t, d, LColor::mathframe);
208 void MathNestInset::drawMarkers2(MathPainterInfo & pi, int x, int y) const
212 drawMarkers(pi, x, y);
213 int t = x + width() - 1;
214 int a = y - ascent();
215 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
216 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
217 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
218 pi.pain.line(t - 2, a, t, a, LColor::mathframe);
222 void MathNestInset::validate(LaTeXFeatures & features) const
224 for (idx_type i = 0; i < nargs(); ++i)
225 cell(i).validate(features);
229 bool MathNestInset::match(MathAtom const & at) const
231 if (nargs() != at->nargs())
233 for (idx_type i = 0; i < nargs(); ++i)
234 if (!cell(i).match(at->cell(i)))
240 void MathNestInset::replace(ReplaceData & rep)
242 for (idx_type i = 0; i < nargs(); ++i)
243 cell(i).replace(rep);
247 bool MathNestInset::contains(MathArray const & ar) const
249 for (idx_type i = 0; i < nargs(); ++i)
250 if (cell(i).contains(ar))
256 bool MathNestInset::editing() const
258 return mathcursor && mathcursor->isInside(this);
262 bool MathNestInset::lock() const
268 void MathNestInset::lock(bool l)
274 bool MathNestInset::isActive() const
280 MathArray MathNestInset::glue() const
283 for (unsigned i = 0; i < nargs(); ++i)
289 void MathNestInset::write(WriteStream & os) const
291 os << '\\' << name().c_str();
292 for (unsigned i = 0; i < nargs(); ++i)
293 os << '{' << cell(i) << '}';
295 os.pendingSpace(true);
296 if (lock_ && !os.latex()) {
298 os.pendingSpace(true);
303 void MathNestInset::normalize(NormalStream & os) const
305 os << '[' << name().c_str();
306 for (unsigned i = 0; i < nargs(); ++i)
307 os << ' ' << cell(i);
312 void MathNestInset::notifyCursorLeaves(idx_type idx)
314 cell(idx).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);