]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
more work on \parbox support
[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         x = cells_[idx].xo() + cells_[idx].pos2x(pos);
54         y = cells_[idx].yo();
55         // move cursor visually into empty cells ("blue rectangles");
56         if (!cells_[idx].data().size())
57                 x += 2;
58 }
59
60 void MathNestInset::substitute(MathMacro const & m)
61 {
62         for (idx_type i = 0; i < nargs(); ++i)
63                 cell(i).substitute(m);
64 }
65
66
67 void MathNestInset::metrics(MathMetricsInfo const & mi) const
68 {
69         MathMetricsInfo m = mi;
70         for (idx_type i = 0; i < nargs(); ++i)
71                 xcell(i).metrics(m);
72 }
73
74
75 void MathNestInset::metricsMarkers(int frame) const
76 {
77         dim_.d += frame;
78         dim_.w += 2 * frame;
79 }
80
81
82 void MathNestInset::metricsMarkers2(int frame) const
83 {
84         dim_.a += frame;
85         dim_.d += frame;
86         dim_.w += 2 * frame;
87 }
88
89
90 bool MathNestInset::idxNext(idx_type & idx, pos_type & pos) const
91 {
92         if (idx + 1 >= nargs())
93                 return false;
94         ++idx;
95         pos = 0;
96         return true;
97 }
98
99
100 bool MathNestInset::idxRight(idx_type & idx, pos_type & pos) const
101 {
102         return idxNext(idx, pos);
103 }
104
105
106 bool MathNestInset::idxPrev(idx_type & idx, pos_type & pos) const
107 {
108         if (idx == 0)
109                 return false;
110         --idx;
111         pos = cell(idx).size();
112         return true;
113 }
114
115
116 bool MathNestInset::idxLeft(idx_type & idx, pos_type & pos) const
117 {
118         return idxPrev(idx, pos);
119 }
120
121
122 bool MathNestInset::idxFirst(idx_type & i, pos_type & pos) const
123 {
124         if (nargs() == 0)
125                 return false;
126         i = 0;
127         pos = 0;
128         return true;
129 }
130
131
132 bool MathNestInset::idxLast(idx_type & i, pos_type & pos) const
133 {
134         if (nargs() == 0)
135                 return false;
136         i = nargs() - 1;
137         pos = cell(i).size();
138         return true;
139 }
140
141
142 bool MathNestInset::idxHome(idx_type & /* idx */, pos_type & pos) const
143 {
144         if (pos == 0)
145                 return false;
146         pos = 0;
147         return true;
148 }
149
150
151 bool MathNestInset::idxEnd(idx_type & idx, pos_type & pos) const
152 {
153         pos_type n = cell(idx).size();
154         if (pos == n)
155                 return false;
156         pos = n;
157         return true;
158 }
159
160
161 void MathNestInset::dump() const
162 {
163         WriteStream os(lyxerr);
164         os << "---------------------------------------------\n";
165         write(os);
166         os << "\n";
167         for (idx_type i = 0; i < nargs(); ++i)
168                 os << cell(i) << "\n";
169         os << "---------------------------------------------\n";
170 }
171
172
173 //void MathNestInset::draw(MathPainterInfo & pi, int x, int y) const
174 void MathNestInset::draw(MathPainterInfo &, int, int) const
175 {
176 #if 0
177         if (lock_)
178                 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
179                                         LColor::mathlockbg);
180 #endif
181 }
182
183
184 void MathNestInset::drawMarkers(MathPainterInfo & pi, int x, int y) const
185 {
186         if (!editing())
187                 return;
188         int t = x + width() - 1;
189         int d = y + descent();
190         pi.pain.line(x, d - 3, x, d, LColor::mathframe); 
191         pi.pain.line(t, d - 3, t, d, LColor::mathframe); 
192         pi.pain.line(x, d, x + 3, d, LColor::mathframe); 
193         pi.pain.line(t - 2, d, t, d, LColor::mathframe); 
194 }
195
196
197 void MathNestInset::drawMarkers2(MathPainterInfo & pi, int x, int y) const
198 {
199         if (!editing())
200                 return;
201         drawMarkers(pi, x, y);  
202         int t = x + width() - 1;
203         int a = y - ascent();
204         pi.pain.line(x, a + 3, x, a, LColor::mathframe); 
205         pi.pain.line(t, a + 3, t, a, LColor::mathframe); 
206         pi.pain.line(x, a, x + 3, a, LColor::mathframe); 
207         pi.pain.line(t - 2, a, t, a, LColor::mathframe); 
208 }
209
210
211 void MathNestInset::validate(LaTeXFeatures & features) const
212 {
213         for (idx_type i = 0; i < nargs(); ++i)
214                 cell(i).validate(features);
215 }
216
217
218 bool MathNestInset::match(MathInset * p) const
219 {
220         if (nargs() != p->nargs())
221                 return false;
222         for (idx_type i = 0; i < nargs(); ++i)
223                 if (!cell(i).match(p->cell(i)))
224                         return false;
225         return true;
226 }
227
228
229 void MathNestInset::replace(ReplaceData & rep)
230 {
231         for (idx_type i = 0; i < nargs(); ++i)
232                 cell(i).replace(rep);
233 }
234
235
236 bool MathNestInset::contains(MathArray const & ar)
237 {
238         for (idx_type i = 0; i < nargs(); ++i)
239                 if (cell(i).contains(ar))
240                         return true;
241         return false;
242 }
243
244
245 bool MathNestInset::editing() const
246 {
247         return mathcursor && mathcursor->isInside(this);
248 }
249
250
251 bool MathNestInset::lock() const
252 {
253         return lock_;
254 }
255
256
257 void MathNestInset::lock(bool l)
258 {
259         lock_ = l;
260 }
261
262
263 bool MathNestInset::isActive() const
264 {
265         return nargs() > 0;
266 }
267
268
269 MathArray MathNestInset::glue() const
270 {
271         MathArray ar;
272         for (unsigned i = 0; i < nargs(); ++i)
273                 ar.push_back(cell(i));
274         return ar;
275 }
276
277
278 void MathNestInset::notifyCursorLeaves()
279 {
280         //lyxerr << "leaving " << *this << "\n";
281         if (!mathcursor || !grfx::Previews::activated())
282                 return;
283
284         InsetFormulaBase * inset = mathcursor->formula();
285         BufferView * bufferview = inset->view();
286         if (!bufferview || !bufferview->buffer())
287                 return;
288
289         grfx::Previews & previews = grfx::Previews::get();
290         grfx::PreviewLoader & loader = previews.loader(bufferview->buffer());
291
292         inset->generatePreview(loader);
293         loader.startLoading();
294 }