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