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