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