5 #include "math_nestinset.h"
6 #include "math_cursor.h"
7 #include "math_mathmlstream.h"
8 #include "formulabase.h"
9 #include "BufferView.h"
11 #include "frontends/Painter.h"
12 #include "graphics/PreviewLoader.h"
13 #include "graphics/Previews.h"
18 MathNestInset::MathNestInset(idx_type nargs)
19 : MathDimInset(), cells_(nargs), lock_(false)
23 MathInset::idx_type MathNestInset::nargs() const
29 MathXArray & MathNestInset::xcell(idx_type i)
35 MathXArray const & MathNestInset::xcell(idx_type i) const
41 MathArray & MathNestInset::cell(idx_type i)
43 return cells_[i].data();
47 MathArray const & MathNestInset::cell(idx_type i) const
49 return cells_[i].data();
53 void MathNestInset::getPos(idx_type idx, pos_type pos, int & x, int & y) const
55 MathXArray const & ar = xcell(idx);
56 x = ar.xo() + ar.pos2x(pos);
58 // move cursor visually into empty cells ("blue rectangles");
59 if (cell(idx).empty())
63 void MathNestInset::substitute(MathMacro const & m)
65 for (idx_type i = 0; i < nargs(); ++i)
66 cell(i).substitute(m);
70 void MathNestInset::metrics(MathMetricsInfo const & mi) const
72 MathMetricsInfo m = mi;
73 for (idx_type i = 0; i < nargs(); ++i)
78 void MathNestInset::metricsMarkers(int frame) const
85 void MathNestInset::metricsMarkers2(int frame) const
93 bool MathNestInset::idxNext(idx_type & idx, pos_type & pos) const
95 if (idx + 1 >= nargs())
103 bool MathNestInset::idxRight(idx_type & idx, pos_type & pos) const
105 return idxNext(idx, pos);
109 bool MathNestInset::idxPrev(idx_type & idx, pos_type & pos) const
114 pos = cell(idx).size();
119 bool MathNestInset::idxLeft(idx_type & idx, pos_type & pos) const
121 return idxPrev(idx, pos);
125 bool MathNestInset::idxFirst(idx_type & i, pos_type & pos) const
135 bool MathNestInset::idxLast(idx_type & i, pos_type & pos) const
140 pos = cell(i).size();
145 bool MathNestInset::idxHome(idx_type & /* idx */, pos_type & pos) const
154 bool MathNestInset::idxEnd(idx_type & idx, pos_type & pos) const
156 pos_type n = cell(idx).size();
164 void MathNestInset::dump() const
166 WriteStream os(lyxerr);
167 os << "---------------------------------------------\n";
170 for (idx_type i = 0; i < nargs(); ++i)
171 os << cell(i) << "\n";
172 os << "---------------------------------------------\n";
176 //void MathNestInset::draw(MathPainterInfo & pi, int x, int y) const
177 void MathNestInset::draw(MathPainterInfo &, int, int) const
181 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
187 void MathNestInset::drawSelection(MathPainterInfo & pi,
188 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
191 MathXArray const & c = xcell(idx1);
192 int x1 = c.xo() + c.pos2x(pos1);
193 int y1 = c.yo() - c.ascent();
194 int x2 = c.xo() + c.pos2x(pos2);
195 int y2 = c.yo() + c.descent();
196 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
198 vector<MathInset::idx_type> indices = idxBetween(idx1, idx2);
199 for (unsigned i = 0; i < indices.size(); ++i) {
200 MathXArray const & c = xcell(indices[i]);
202 int y1 = c.yo() - c.ascent();
203 int x2 = c.xo() + c.width();
204 int y2 = c.yo() + c.descent();
205 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
211 void MathNestInset::drawMarkers(MathPainterInfo & pi, int x, int y) const
215 int t = x + width() - 1;
216 int d = y + descent();
217 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
218 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
219 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
220 pi.pain.line(t - 2, d, t, d, LColor::mathframe);
224 void MathNestInset::drawMarkers2(MathPainterInfo & pi, int x, int y) const
228 drawMarkers(pi, x, y);
229 int t = x + width() - 1;
230 int a = y - ascent();
231 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
232 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
233 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
234 pi.pain.line(t - 2, a, t, a, LColor::mathframe);
238 void MathNestInset::validate(LaTeXFeatures & features) const
240 for (idx_type i = 0; i < nargs(); ++i)
241 cell(i).validate(features);
245 bool MathNestInset::match(MathInset * p) const
247 if (nargs() != p->nargs())
249 for (idx_type i = 0; i < nargs(); ++i)
250 if (!cell(i).match(p->cell(i)))
256 void MathNestInset::replace(ReplaceData & rep)
258 for (idx_type i = 0; i < nargs(); ++i)
259 cell(i).replace(rep);
263 bool MathNestInset::contains(MathArray const & ar)
265 for (idx_type i = 0; i < nargs(); ++i)
266 if (cell(i).contains(ar))
272 bool MathNestInset::editing() const
274 return mathcursor && mathcursor->isInside(this);
278 bool MathNestInset::lock() const
284 void MathNestInset::lock(bool l)
290 bool MathNestInset::isActive() const
296 MathArray MathNestInset::glue() const
299 for (unsigned i = 0; i < nargs(); ++i)
300 ar.push_back(cell(i));
305 void MathNestInset::notifyCursorLeaves()
307 // Generate a preview only if previews are active and we are leaving
308 // the InsetFormula itself
309 if (!grfx::Previews::activated() ||
310 !mathcursor || mathcursor->depth() != 1)
313 InsetFormulaBase * inset = mathcursor->formula();
314 BufferView * bufferview = inset->view();
317 if (!bufferview || !bufferview->buffer())
320 grfx::Previews & previews = grfx::Previews::get();
321 grfx::PreviewLoader & loader = previews.loader(bufferview->buffer());
323 inset->generatePreview(loader);
324 loader.startLoading();