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