]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
the up/down stuff reworked
[lyx.git] / src / mathed / math_nestinset.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_nestinset.h"
6 #include "math_cursor.h"
7 #include "math_mathmlstream.h"
8 #include "formulabase.h"
9 #include "BufferView.h"
10 #include "debug.h"
11 #include "frontends/Painter.h"
12 #include "graphics/PreviewLoader.h"
13 #include "graphics/Previews.h"
14
15 using std::vector;
16
17
18 MathNestInset::MathNestInset(idx_type nargs)
19         : MathDimInset(), cells_(nargs), lock_(false)
20 {}
21
22
23 MathInset::idx_type MathNestInset::nargs() const
24 {
25         return cells_.size();
26 }
27
28
29 MathXArray & MathNestInset::xcell(idx_type i)
30 {
31         return cells_[i];
32 }
33
34
35 MathXArray const & MathNestInset::xcell(idx_type i) const
36 {
37         return cells_[i];
38 }
39
40
41 MathArray & MathNestInset::cell(idx_type i)
42 {
43         return cells_[i].data();
44 }
45
46
47 MathArray const & MathNestInset::cell(idx_type i) const
48 {
49         return cells_[i].data();
50 }
51
52
53 void MathNestInset::getPos(idx_type idx, pos_type pos, int & x, int & y) const
54 {
55         MathXArray const & ar = xcell(idx);
56         x = ar.xo() + ar.pos2x(pos);
57         y = ar.yo();
58         // move cursor visually into empty cells ("blue rectangles");
59         if (cell(idx).empty())
60                 x += 2;
61 }
62
63 void MathNestInset::substitute(MathMacro const & m)
64 {
65         for (idx_type i = 0; i < nargs(); ++i)
66                 cell(i).substitute(m);
67 }
68
69
70 void MathNestInset::metrics(MathMetricsInfo const & mi) const
71 {
72         MathMetricsInfo m = mi;
73         for (idx_type i = 0; i < nargs(); ++i)
74                 xcell(i).metrics(m);
75 }
76
77
78 void MathNestInset::metricsMarkers(int frame) const
79 {
80         dim_.d += frame;
81         dim_.w += 2 * frame;
82 }
83
84
85 void MathNestInset::metricsMarkers2(int frame) const
86 {
87         dim_.a += frame;
88         dim_.d += frame;
89         dim_.w += 2 * frame;
90 }
91
92
93 bool MathNestInset::idxNext(idx_type & idx, pos_type & pos) const
94 {
95         if (idx + 1 >= nargs())
96                 return false;
97         ++idx;
98         pos = 0;
99         return true;
100 }
101
102
103 bool MathNestInset::idxRight(idx_type & idx, pos_type & pos) const
104 {
105         return idxNext(idx, pos);
106 }
107
108
109 bool MathNestInset::idxPrev(idx_type & idx, pos_type & pos) const
110 {
111         if (idx == 0)
112                 return false;
113         --idx;
114         pos = cell(idx).size();
115         return true;
116 }
117
118
119 bool MathNestInset::idxLeft(idx_type & idx, pos_type & pos) const
120 {
121         return idxPrev(idx, pos);
122 }
123
124
125 bool MathNestInset::idxFirst(idx_type & i, pos_type & pos) const
126 {
127         if (nargs() == 0)
128                 return false;
129         i = 0;
130         pos = 0;
131         return true;
132 }
133
134
135 bool MathNestInset::idxLast(idx_type & i, pos_type & pos) const
136 {
137         if (nargs() == 0)
138                 return false;
139         i = nargs() - 1;
140         pos = cell(i).size();
141         return true;
142 }
143
144
145 bool MathNestInset::idxHome(idx_type & /* idx */, pos_type & pos) const
146 {
147         if (pos == 0)
148                 return false;
149         pos = 0;
150         return true;
151 }
152
153
154 bool MathNestInset::idxEnd(idx_type & idx, pos_type & pos) const
155 {
156         pos_type n = cell(idx).size();
157         if (pos == n)
158                 return false;
159         pos = n;
160         return true;
161 }
162
163
164 void MathNestInset::dump() const
165 {
166         WriteStream os(lyxerr);
167         os << "---------------------------------------------\n";
168         write(os);
169         os << "\n";
170         for (idx_type i = 0; i < nargs(); ++i)
171                 os << cell(i) << "\n";
172         os << "---------------------------------------------\n";
173 }
174
175
176 //void MathNestInset::draw(MathPainterInfo & pi, int x, int y) const
177 void MathNestInset::draw(MathPainterInfo &, int, int) const
178 {
179 #if 0
180         if (lock_)
181                 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
182                                         LColor::mathlockbg);
183 #endif
184 }
185
186
187 void MathNestInset::drawSelection(MathPainterInfo & pi,
188                 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
189 {
190         if (idx1 == idx2) {
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);
197         } else {
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]);
201                         int x1 = c.xo();
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);
206                 }
207         }
208 }
209
210
211 void MathNestInset::drawMarkers(MathPainterInfo & pi, int x, int y) const
212 {
213         if (!editing())
214                 return;
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);
221 }
222
223
224 void MathNestInset::drawMarkers2(MathPainterInfo & pi, int x, int y) const
225 {
226         if (!editing())
227                 return;
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);
235 }
236
237
238 void MathNestInset::validate(LaTeXFeatures & features) const
239 {
240         for (idx_type i = 0; i < nargs(); ++i)
241                 cell(i).validate(features);
242 }
243
244
245 bool MathNestInset::match(MathInset * p) const
246 {
247         if (nargs() != p->nargs())
248                 return false;
249         for (idx_type i = 0; i < nargs(); ++i)
250                 if (!cell(i).match(p->cell(i)))
251                         return false;
252         return true;
253 }
254
255
256 void MathNestInset::replace(ReplaceData & rep)
257 {
258         for (idx_type i = 0; i < nargs(); ++i)
259                 cell(i).replace(rep);
260 }
261
262
263 bool MathNestInset::contains(MathArray const & ar)
264 {
265         for (idx_type i = 0; i < nargs(); ++i)
266                 if (cell(i).contains(ar))
267                         return true;
268         return false;
269 }
270
271
272 bool MathNestInset::editing() const
273 {
274         return mathcursor && mathcursor->isInside(this);
275 }
276
277
278 bool MathNestInset::lock() const
279 {
280         return lock_;
281 }
282
283
284 void MathNestInset::lock(bool l)
285 {
286         lock_ = l;
287 }
288
289
290 bool MathNestInset::isActive() const
291 {
292         return nargs() > 0;
293 }
294
295
296 MathArray MathNestInset::glue() const
297 {
298         MathArray ar;
299         for (unsigned i = 0; i < nargs(); ++i)
300                 ar.append(cell(i));
301         return ar;
302 }
303
304
305 void MathNestInset::notifyCursorLeaves()
306 {
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)
311                 return;
312
313         InsetFormulaBase * inset = mathcursor->formula();
314         BufferView * bufferview = inset->view();
315
316         // Paranoia check
317         if (!bufferview || !bufferview->buffer())
318                 return;
319
320         grfx::Previews & previews = grfx::Previews::get();
321         grfx::PreviewLoader & loader = previews.loader(bufferview->buffer());
322
323         inset->generatePreview(loader);
324         loader.startLoading();
325 }