]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
some \parbox improvements
[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::drawMarkers(MathPainterInfo & pi, int x, int y) const
186 {
187         if (!editing())
188                 return;
189         int t = x + width() - 1;
190         int d = y + descent();
191         pi.pain.line(x, d - 3, x, d, LColor::mathframe); 
192         pi.pain.line(t, d - 3, t, d, LColor::mathframe); 
193         pi.pain.line(x, d, x + 3, d, LColor::mathframe); 
194         pi.pain.line(t - 2, d, t, d, LColor::mathframe); 
195 }
196
197
198 void MathNestInset::drawMarkers2(MathPainterInfo & pi, int x, int y) const
199 {
200         if (!editing())
201                 return;
202         drawMarkers(pi, x, y);  
203         int t = x + width() - 1;
204         int a = y - ascent();
205         pi.pain.line(x, a + 3, x, a, LColor::mathframe); 
206         pi.pain.line(t, a + 3, t, a, LColor::mathframe); 
207         pi.pain.line(x, a, x + 3, a, LColor::mathframe); 
208         pi.pain.line(t - 2, a, t, a, LColor::mathframe); 
209 }
210
211
212 void MathNestInset::validate(LaTeXFeatures & features) const
213 {
214         for (idx_type i = 0; i < nargs(); ++i)
215                 cell(i).validate(features);
216 }
217
218
219 bool MathNestInset::match(MathInset * p) const
220 {
221         if (nargs() != p->nargs())
222                 return false;
223         for (idx_type i = 0; i < nargs(); ++i)
224                 if (!cell(i).match(p->cell(i)))
225                         return false;
226         return true;
227 }
228
229
230 void MathNestInset::replace(ReplaceData & rep)
231 {
232         for (idx_type i = 0; i < nargs(); ++i)
233                 cell(i).replace(rep);
234 }
235
236
237 bool MathNestInset::contains(MathArray const & ar)
238 {
239         for (idx_type i = 0; i < nargs(); ++i)
240                 if (cell(i).contains(ar))
241                         return true;
242         return false;
243 }
244
245
246 bool MathNestInset::editing() const
247 {
248         return mathcursor && mathcursor->isInside(this);
249 }
250
251
252 bool MathNestInset::lock() const
253 {
254         return lock_;
255 }
256
257
258 void MathNestInset::lock(bool l)
259 {
260         lock_ = l;
261 }
262
263
264 bool MathNestInset::isActive() const
265 {
266         return nargs() > 0;
267 }
268
269
270 MathArray MathNestInset::glue() const
271 {
272         MathArray ar;
273         for (unsigned i = 0; i < nargs(); ++i)
274                 ar.push_back(cell(i));
275         return ar;
276 }
277
278
279 void MathNestInset::notifyCursorLeaves()
280 {
281         // Generate a preview only if previews are active and we are leaving
282         // the InsetFormula itself
283         if (!grfx::Previews::activated() ||
284             !mathcursor || mathcursor->depth() != 1)
285                 return;
286
287         InsetFormulaBase * inset = mathcursor->formula();
288         BufferView * bufferview = inset->view();
289
290         // Paranoia check
291         if (!bufferview || !bufferview->buffer())
292                 return;
293
294         grfx::Previews & previews = grfx::Previews::get();
295         grfx::PreviewLoader & loader = previews.loader(bufferview->buffer());
296
297         inset->generatePreview(loader);
298         loader.startLoading();
299 }