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