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