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